import { CONNECT_STATUS, MQTT_EVENT_TYPE, MQTT_EVENT_CALL, MQTT_EVENT_PUBLISH, MQTT_SUBSCRIBE, MQTT_EVENTS_CALL, MQTT_PUBLISH_TOPIC, MQTT_CONNECT_OPTS } from "../interface.uts" import { CocoaMQTT, CocoaMQTTWebSocket, CocoaMQTT5Delegate, CocoaMQTT5, MqttConnectProperties, CocoaMQTTDISCONNECTReasonCode, CocoaMQTTAUTHReasonCode, MqttDecodeUnsubAck, MqttDecodeSubAck, CocoaMQTT5Message, MqttDecodePublish, MqttDecodePubAck, MqttDecodePubRec, MqttDecodeConnAck, CocoaMQTTCONNACKReasonCode, CocoaMQTTConnState, CocoaMQTTQoS, MqttPublishProperties, CocoaMQTTError, CocoaMQTTMessage } from "CocoaMQTT" // https://github.com/anatoliykant/SwiftMQTT import { UInt16 } from 'Swift'; import { Bundle, CFArray, NSDictionary, SecPKCS12Import } from 'Foundation'; import { kCFStreamSSLCertificates } from 'CFNetwork'; import { NSObject } from 'ObjectiveC'; // https://github.com/emqx/CocoaMQTT // https://cocoapods.org/pods/CocoaMQTT // 文档:https://www.emqx.com/en/blog/ios-mqtt5-client export class xMqtt { mqtt:XMqttHelp = new XMqttHelp(); mqttConnectOptions : MqttConnectProperties | null = null; connectStatus : CONNECT_STATUS = 'wait' constructor() { } /** * @param type {MQTT_EVENT_TYPE} 事件名称 * @param call {MQTT_EVENT_CALL} 事件回调 */ @UTSJS.keepAlive addEventListener(type : MQTT_EVENT_TYPE, call : MQTT_EVENT_CALL) : string { @escaping return this.mqtt.addEventListener( type, callback = call); } /** * @param id {string} addEventListener返回的事件id */ removeEventListener(id : string) : xMqtt { this.mqtt.removeEventListener(id) return this; } private buildCallEvents() { @escaping this.mqtt.setCallBack(()=>{ this.connectStatus = this.mqtt.getStatus(); console.log(this.connectStatus) }) } create(opts : MQTT_CONNECT_OPTS) : xMqtt { let optsjson = JSON.parseObject(JSON.stringify(opts)!)!; const realJson = {...optsjson,allowUntrustCACertificate:opts.allowUntrustCACertificate==true,protocol:opts.useSSL?'wss':'ws'} this.mqtt.create(realJson.toMap() as NSDictionary) this.buildCallEvents() return this; } connect() : xMqtt { // if (this.mqtt.getStatus()!='dissconnect'&&this.mqtt.getStatus()!='error') return this; this.mqtt.connect() return this; } /** * 推送消息 * @param call {MQTT_EVENT_PUBLISH} */ publish(msg : MQTT_PUBLISH_TOPIC, call : MQTT_EVENT_PUBLISH) : xMqtt { // if (this.mqtt.getStatus() != 'open' && this.mqtt.getStatus() != 'message') return this; @escaping this.mqtt.publish( msg.topic, message = msg.message, qos = msg.qos.toInt(), retained=msg.retained, completion = (ok:boolean)=>{ console.log('ok',ok) call(ok) } ) return this; } /** * 订阅 * @param data {MQTT_SUBSCRIBE[]} 订阅的消息数组 */ subscribe(data : MQTT_SUBSCRIBE[]) : xMqtt { // if (this.mqtt.getStatus() != 'open' && this.mqtt.getStatus() != 'message') return this; let d:NSDictionary[] = []; data.forEach(el=>{ let p = JSON.parseObject(JSON.stringify(el)!)!.toMap(); let qos = p.get('qos')! as number p.set('qos',qos.toInt()) d.push(p as NSDictionary) }) this.mqtt.subscribe(d) return this; } /** * 取消订阅 * @param topics {string[]} 主题数组 */ unsubscribe(topics : string[]) : xMqtt { // if (this.mqtt.getStatus() != 'open' && this.mqtt.getStatus() != 'message') return this; this.mqtt.unsubscribe(topics) return this; } /** * 断开连接 */ disconnect() : xMqtt { this.mqtt.disconnect(); return this; } }