概述
ELSwift for Cocoa(将废弃)
此模块支持ECHONET Lite协议。ECHONET Lite协议是智能家居设备的通信协议。CocoaPods版本,仅支持旧版iOS(大约11版本之前)。
本模块利用CocoaAsyncSocket,曾用于iOS版本13之前。之后,已转移到使用Swift Package Manager的Network.Framework版本。
安装
ELSwift可以通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中
或者指定您需要的版本
use_frameworks!
target 'simpleel' do
pod 'CocoaAsyncSocket'
pod 'ELSwift'
end
或者指定您需要的版本
use_frameworks!
target 'simpleel' do
pod 'CocoaAsyncSocket'
pod 'ELSwift', '2.0.0'
end
示例
要运行示例项目,请首先克隆存储库,然后从Example目录中运行pod install
如果不执行该命令,您需要使用命令 'sudo gem install cocoapods' 或 'sudo gem install -n /usr/local/bin cocoapods' 进行安装。
需求
- Xcode 8.0
- iOS 9.3 (?)
示例控制器
import UIKit
import ELSwift
class ViewController: UIViewController {
let objectList:[String] = ["05ff01"]
@IBOutlet weak var logView: UITextView!
@IBOutlet weak var btnSearch: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
logView.text = ""
do {
try ELSwift.initialize( objectList, { rinfo, els, err in
if let error = err {
print (error)
return
}
// sample 1: udp recv
// els is a EL_STRACTURE
/*
var EHD : [UInt8]
var TID : [UInt8]
var SEOJ : [UInt8]
var DEOJ : [UInt8]
var EDATA: [UInt8] // 下記はEDATAの詳細
var ESV : UInt8
var OPC : UInt8
var DETAIL: [UInt8]
var DETAILs: Dictionary<String, [UInt8]>
*/
if let elsv = els {
let seoj = elsv.SEOJ
let esv = elsv.ESV
let detail = elsv.DETAIL
self.logView.text = "ip:\(rinfo.address), seoj:\(seoj), esv:\(esv), datail:\(detail)" + "\n" + self.logView.text
}
}, 4)
}catch let error{
print( error )
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func btnSearchDown(_ sender: Any) {
ELSwift.search()
}
}
数据结构
public class ELSwift : NSObject {
static var isIPv6 = false
static var inSocket: InSocket!
static var outSocket: OutSocket!
public static let MULTI_IP: String = "224.0.23.0"
public static let PORT: UInt16 = 3360
// define
public static let SETI_SNA = 0x50
public static let SETC_SNA = 0x51
public static let GET_SNA = 0x52
public static let INF_SNA = 0x53
public static let SETGET_SNA = 0x5e
public static let SETI = 0x60
public static let SETC = 0x61
public static let GET = 0x62
public static let INF_REQ = 0x63
public static let SETGET = 0x6e
public static let SET_RES = 0x71
public static let GET_RES = 0x72
public static let INF = 0x73
public static let INFC = 0x74
public static let INFC_RES = 0x7a
public static let SETGET_RES = 0x7e
public static let EL_port = 3610
public static let EL_Multi = "224.0.23.0"
public static let EL_Multi6 = "FF02::1"
static var EL_obj: [String]!
static var EL_cls: [String]!
public static var Node_details: Dictionary
public static var facilities:Dictionary
}
public class EL_STRUCTURE : NSObject{
public var EHD : [UInt8]
public var TID : [UInt8]
public var SEOJ : [UInt8]
public var DEOJ : [UInt8]
public var EDATA: [UInt8] // 下記はEDATAの詳細
public var ESV : UInt8
public var OPC : UInt8
public var DETAIL: [UInt8]
public var DETAILs: Dictionary<String, [UInt8]>
override init() {
EHD = []
TID = []
SEOJ = []
DEOJ = []
EDATA = []
ESV = 0x00
OPC = 0x00
DETAIL = []
DETAILs = [String: [UInt8]]()
}
}
API
初始化,绑定, 初始化
ELSwift.initialize(_ objList: [String], _ callback: ((_ rinfo:(address:String, port:UInt16), _ els: EL_STRUCTURE?, _ err: Error?) -> Void)?, _ ipVer: UInt8? ) throws -> Void
然后可以按照以下方式使用回调。
do {
try ELSwift.initialize( objectList, { rinfo, els, err in
if let error = err {
print (error)
return
}
// ToDo
}, 4)
}catch let error{
print( error )
}
数据表示系统, 数据表示
- ELDATA格式
ELSwift.eldataShow(_ eldata:EL_STRUCTURE ) -> Void
- 字符串, string
ELSwift.stringShow(_ str: String ) throws -> Void
- 字节数据, byte data
ELSwift.bytesShow(_ bytes: [UInt8] ) throws -> Void
转换系统, 转换器
从 | 到 | 函数 |
---|---|---|
Bytes(=Integer[]) | ELDATA | parseBytes(bytes) |
字符串 | ELDATA | parseString(str) |
字符串 | 似EL的字符串 | getSeparatedString_String(str) |
ELDATA | 似EL的字符串 | getSeparatedString_ELDATA(eldata) |
ELDATA | Bytes(=Integer[]) | ELDATA2Array(eldata) |
- 只解析详细部分,内部常用,不知道外部是否使用。
ELSwift.parseDetail( opc:UInt8, str:String ) throws -> Dictionary<String, [UInt8]>
- 输入字节数据将其转换为ELDATA格式
ELSwift.parseBytes(_ bytes:[UInt8] ) throws -> EL_STRUCTURE
- 输入用十六进制表示的字符串将其转换为ELDATA格式
ELSwift.parseString(_ str: String ) throws -> EL_STRUCTURE
- 输入字符串得到似EL的切割方式字符串
ELSwift.getSeparatedString_String(_ str: String ) -> String
- 因为忍受不了字符串操作所以做成(固定1字节字符) ok
ELSwift.substr(_ str:String, _ begginingIndex:UInt, _ count:UInt) -> String
- 输入ELDATA得到似EL的切割方式的字符串
ELSwift.getSeparatedString_ELDATA(_ eldata : EL_STRUCTURE ) -> String
- 从ELDATA格式到数组
ELSwift.ELDATA2Array(_ eldata: EL_STRUCTURE ) throws -> [UInt8]
- 转换表
从 | 到 | 函数 |
---|---|---|
字节 | 十六进表示字符串 | toHexString(byte) |
十六进表示字符串 | 整数数组 | toHexArray(str) |
- 将1字节转换为字符串的十六进表示(1字节总是转换为2个字符)
ELSwift.toHexString(_ byte:UInt8 ) -> String
- 将十六进制字符串转换为数值的字节数组
ELSwift.toHexArray(_ str: String ) -> [UInt8]
- 将字节数组转换为字符串
ELSwift.bytesToString(_ bytes: [UInt8] ) throws -> String
发送, send
- EL发送的基础
ELSwift.sendBase(_ ip:String,_ data:Data ) throws -> Void
- 数组时
ELSwift.sendArray(_ ip:String,_ array:[UInt8] ) throws -> Void
- EL非常典型的OPC单个 doing way
ELSwift.sendOPC1(_ ip:String, _ seoj:[UInt8], _ deoj:[UInt8], _ esv: UInt8, _ epc: UInt8, _ edt:[UInt8]) throws -> Void
例.
try ELSwift.sendOPC1( '192.168.2.150', [0x05,0xff,0x01], [0x01,0x35,0x01], 0x61, 0x80, [0x31]);
- EL非常典型的3个字符串类型的发送
ELSwift.sendString(_ ip:String,_ string:String ) throws -> Void
接收数据完全控制, 完全控制接收数据的方法。
EL接收数据划分,设法解决问题。想完全自己书写EL接收的人,完全改写这个的话就可以了。普通人应该在initialize的userfunc中就足够了。
ELSwift.returner( bytes:[UInt8], rinfo:((address:String, port:UInt16)) ) -> Void
EL,高级通信过程
- 设备搜索
ELSwift.search() -> Void
- 更新网络中所有EL设备的信息
ELSwift.renewFacilities = function( ip, obj, opc, detail )
- 更新网络中所有EL设备的信息,接收到后自动执行 mada,处理JSON比较困难,需要在Dictionary中定义才好
ELSwift.renewFacilities( ip:String, els: EL_STRUCTURE ) throws -> Void
- 获取所有属性映射 ok
ELSwift.getPropertyMaps ( ip:String, eoj:[UInt8] ) throws -> Void
- 解析属性映射表 2
当属性数超过16时,采用形式2,输出到Form1
ELSwift.parseMapForm2(_ bitstr:String ) -> [UInt8]
ECHONET Lite攻略信息()
xxxxx
- 针对控制器开发者
可能最容易使用的接收数据解析是直接读取EL.facilities。例如,直接显示的话,
可能,对接收数据的最便捷分析是显示目录。例如,
console.dir( EL.facilities );
数据是这样的。
接收数据如下,
{ '192.168.2.103':
{ '05ff01': { '80': '', d6: '' },
'0ef001': { '80': '30', d6: '0100' } },
'192.168.2.104': { '0ef001': { d6: '0105ff01' }, '05ff01': { '80': '30' } },
'192.168.2.115': { '0ef001': { '80': '30', d6: '01013501' } } }
此外,在使用数据发送中最简单的方法可能是sendOPC1。结合使用ECHONET Lite则几乎可以执行所有操作。
最简单的发送方法是'sendOPC1'。
try ELSwift.sendOPC1( "192.168.2.103", [0x05,0xff,0x01], [0x01,0x35,0x01], 0x61, 0x80, [0x30]);
作者
神奈川工科大学 创造工学部 家居电子开发学科。
杉村 博
家居电子系,创造性工程学部,神奈川工业大学。
SUGIMURA, Hiroshi
许可
ELSwift在MIT许可下可用。有关更多信息,请参阅LICENSE文件。
日志
- 2.0.0 Swift 5,Xcode 12.3,CocoaAsyncSocket 7.6.5
- 1.0.0 Swift 4,Xcode 8.0,CocoaAsyncSocket 7.6.3
- 0.1.1 README.md
- 0.1.0 首次提交