123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- //
- // IHConfigGatawayDetailManager.swift
- // Inhealth
- //
- // Created by weclouds on 2020/8/25.
- // Copyright © 2020 weclouds. All rights reserved.
- //
- import UIKit
- import PKHUD
- let KNonitifiGetAsssociatedDevices = "KNonitifiGetAsssociatedDevices"
- let KNonitifiGetNotAsssociatedDevices = "KNonitifiGetNotAsssociatedDevices"
- class IHConfigGatawayDetailManager: NSObject {
- private lazy var mainView : IHConfigGatawayDetailView = {
- let mainView = Bundle.main.loadNibNamed("IHConfigGatawayDetailView", owner: nil, options: nil)?.last as! IHConfigGatawayDetailView
- mainView.delete = self
- return mainView
- }()
- lazy var clientSocket: GCDAsyncUdpSocket? = {
- var mainQueue = DispatchQueue.main
- var clientSocket = GCDAsyncUdpSocket(delegate: self, delegateQueue: mainQueue)
- return clientSocket
- }()
-
- private var deviceList : [(address: String, type: String)]?
- var connectCallback: (()->Void)?
- private weak var vc : IHConfigGatawayDetailCtr?
- func bindController(_ vc : IHConfigGatawayDetailCtr) {
- self.vc = vc
- vc.view.addSubview(mainView)
- mainView.gateway = vc.gateway
- //扫描添加过的和未添加的
- //scanAllDevice()
- }
- deinit {
- log.debug("IHConfigGatawayDetailManager")
- clientSocket = nil
- }
- }
- extension IHConfigGatawayDetailManager : IHConfigGatawayDetailViewDelete{
- //485灯的传感器工作模式
- func configLightWorkModel(_ device: (address: String, type: String), _ dodgeEnable: String, _ bodyEnable: String) {
-
-
- }
-
- //开关灯(485/cw)
- func setLightTurnOn(_ isTurnOn: Bool, _ device: (address: String, type: String)) {
- IHConfigGatewayDetailService.share.setCWOrOtherLightTurnOn(isTurnOn: isTurnOn, device: device, ip: self.vc!.gateway!.ip!, port : self.vc!.gateway!.port!)
- }
-
- //删除设备
- func deleteDeviceFromGateway(deviceAddress: String) {
- IHConfigGatewayDetailService.share.deleteDeviceFromGateway(deviceAddress: deviceAddress,ip : self.vc!.gateway!.ip!, port : self.vc!.gateway!.port!)
- }
-
- //添加未绑定的设备
- func addDeviceToGateway(deviceUUid: String) {
- IHConfigGatewayDetailService.share.addDeviceToGateway(deviceUUid: deviceUUid,ip : self.vc!.gateway!.ip!, port : self.vc!.gateway!.port!)
- }
-
- func selectIndexGetDeviceFromGateway(index: Int){
- //扫描添加过的
- // DispatchQueue.main.async {
- // HUD.flash(.progress, delay: 8)
- // }
- //
- scanAssociateDevice()
- }
-
- func selectIndexFoundDevice(index: Int) {
- //扫描未添加的
- // DispatchQueue.main.async {
- // HUD.flash(.progress, delay: 8)
- // }
- scanNotAssociateDevice()
- }
-
-
- }
- extension IHConfigGatawayDetailManager {
- fileprivate func scanAssociateDevice(){
- //[0x12, 0xa1, 0x04, 0x00,0xb7]
-
- do{
- //不连接,直接发命令
- try clientSocket?.enableBroadcast(true)
- try clientSocket?.beginReceiving() // 0117-设备地址
- //[0x12, 0xa1, 0x52, 0x06,0x01,0x17,0x10,0x33]
- //"12a1 01 05 01 0001010c c8"
- //12a10203 011d 0400000000da
-
- // let bytes: [UInt8] = [0x12, 0xa1, 0x04, 0x00,0xb7]
- let bytes: [UInt8] = [0x12, 0xa1, 0x04, 0x00,0xb7]
- let command: Data = Data.init(bytes: bytes, count: bytes.count)
- // self.vc?.gateway?.ip! self.vc?.gateway?.port
- clientSocket?.send(command, toHost: self.vc!.gateway!.ip!, port:UInt16(self.vc!.gateway!.port!)! , withTimeout: -1, tag: 0)
-
- }catch{
- log.debug("绑定失败\(error)")
- }
- }
-
- fileprivate func scanNotAssociateDevice(){
- //[0x12, 0xa1, 0x01, 0x00,0xb4]
- do{
- //不连接,直接发命令
- try clientSocket?.enableBroadcast(true)
- try clientSocket?.beginReceiving() // 0117-设备地址
- //[0x12, 0xa1, 0x52, 0x06,0x01,0x17,0x10,0x33]
- //"12a1 01 05 01 0001010c c8"
- //12a10203 011d 0400000000da
-
- let bytes: [UInt8] = [0x12, 0xa1, 0x01, 0x00,0xb4]
- let command: Data = Data.init(bytes: bytes, count: bytes.count)
- // self.vc?.gateway?.ip! self.vc?.gateway?.port
- clientSocket?.send(command, toHost: self.vc!.gateway!.ip!, port:UInt16(self.vc!.gateway!.port!)!, withTimeout: -1, tag: 0)
-
- }catch{
- log.debug("绑定失败\(error)")
- }
- }
-
-
- }
- extension IHConfigGatawayDetailManager : GCDAsyncUdpSocketDelegate{
-
- func udpSocket(_ sock: GCDAsyncUdpSocket, didNotConnect error: Error?) {
- log.debug("didNotConnect")
- }
-
- func udpSocket(_ sock: GCDAsyncUdpSocket, didConnectToAddress address: Data) {
- log.debug("didConnectToAddress:\(String(data: address, encoding: String.Encoding.utf8))")
-
- }
- func udpSocket(_ sock: GCDAsyncUdpSocket, didReceive data: Data, fromAddress address: Data, withFilterContext filterContext: Any?) {
-
- //12 A1 04 01 00 B8
- let hexStr = dataConvertToHexString(data: data)
-
- if self.mainView.currentIndex! == 0 {
- //获取相关设备
- getDeviceFromGateway(respondStr: hexStr)
- }else{
- //获取未相关设备
- foundDevice(respondStr: hexStr)
- }
-
- log.debug("didReceive:\(hexStr)")
- }
- func udpSocket(_ sock: GCDAsyncUdpSocket, didSendDataWithTag tag: Int) {
- log.debug("didSendDataWithTag")
-
-
- }
- func udpSocketDidClose(_ sock: GCDAsyncUdpSocket, withError error: Error?) {
- log.debug("withError")
- }
- func udpSocket(_ sock: GCDAsyncUdpSocket, didNotSendDataWithTag tag: Int, dueToError error: Error?) {
- log.debug("didNotSendDataWithTag")
- }
-
- }
- extension IHConfigGatawayDetailManager{
- //扫描网关返回的设备
- private func getDeviceFromGateway(respondStr : String) {
-
- let str = respondStr as NSString
- //"12a1 04 04 01 0117 01 d5"
- // 12a1 040702011e31011f3161
- let componeStr = str.substring(with: NSRange.init(location: 8, length: 2))
- let check = str.substring(with: NSRange.init(location: 4, length: 2))
- //Found Device Count.
- var deviceArr = [(address : String ,type : String)]()
- if componeStr == "00" && check == "04" {
- log.debug("没有发现添加的设备")
-
- }else{
- //如果 componeStr 是大于或等于01,里面可能是多个了
- // var deviceArr = [(address : String ,type : String)]()
- for i in 0..<Int(componeStr)! {
- let deviceAdress = str.substring(with: NSRange.init(location: 10 + i * 6, length: 4))
- let deviceType = str.substring(with: NSRange.init(location: 14 + i * 6, length: 2))
- deviceArr.append((address : deviceAdress,type : deviceType))
- }
-
- }
- DispatchQueue.main.async {
- HUD.hide()
- // if deviceArr.count == 0{
- // HUD.flash(.label("未发现设备"), delay: 0.6)
- // }
- }
- if self.mainView.assciateDeviceCallBack == nil {
- NotificationCenter.default.post(name: NSNotification.Name(KNonitifiGetAsssociatedDevices), object: nil, userInfo: ["associatedDevice" : deviceArr])
- }else{
- self.mainView.assciateDeviceCallBack?(deviceArr)
- }
-
- }
-
- //扫描未添加的设备
- private func foundDevice(respondStr : String) {
- let str = respondStr as NSString
- //"12 a1 01 01 00 b5"
- //12 A1 01 05 01 00 01 01 69 25
- let componeStr = str.substring(with: NSRange.init(location: 8, length: 2))
- //Found Device Count.
- var deviceUUidArr = [String]()
- if componeStr == "00" {
- log.debug("没有发现新设备")
-
- }else{
- //如果 componeStr 是大于或等于01,里面可能是多个了
- for i in 0..<Int(componeStr)! {
- let compone = str.substring(with: NSRange.init(location: 10 + i * 8, length: 8))
- deviceUUidArr.append(compone)
- }
- }
- // let str1 = "0001010c"
- // deviceUUidArr.append(str1)
- DispatchQueue.main.async {
- HUD.hide()
- if deviceUUidArr.count == 0{
- HUD.flash(.label("未发现设备"), delay: 0.6)
- }
- }
- if self.mainView.notAssciateDeviceCallBack == nil {
- NotificationCenter.default.post(name: NSNotification.Name(KNonitifiGetNotAsssociatedDevices), object: nil, userInfo: ["notAssociatedDevice" : deviceUUidArr])
- }else{
- self.mainView.notAssciateDeviceCallBack?(deviceUUidArr)
- }
-
-
-
- }
- }
|