1import * as event from 'events' 2import * as AIOT_DEVICE from 'AIOT_DEVICE' 3import * as AIOT_GATEWAY from 'AIOT_GATEWAY' 4 5class IotDeviceClient extends event.EventEmitter { 6 constructor(options){ 7 super(); 8 if(!options || !options.productKey || !options.deviceName || !options.deviceSecret){ 9 throw new Error('device info error'); 10 } 11 12 this.options = { 13 productKey: options.productKey, 14 deviceName: options.deviceName, 15 deviceSecret: options.deviceSecret, 16 region: options.region || 'cn-shanghai', 17 keepaliveSec: 60 18 } 19 20 this._connect(); 21 } 22 23 _connect() { 24 this.IoTDeviceInstance = AIOT_DEVICE.device(this.options, function(res) { 25 if (!res.handle) { 26 console.log("res.handle is empty"); 27 return; 28 } 29 this.iot_device_handle = res.handle; 30 switch (res.code) { 31 case 0: 32 this.emit('connect'); break; 33 case 1: 34 this.emit('reconnect'); break; 35 case 2: 36 this.emit('disconnect'); break; 37 case 3: 38 this.emit('message', res); break; 39 default : break ; 40 } 41 }.bind(this)); 42 } 43 44 getDeviceHandle() { 45 return this.iot_device_handle; 46 } 47 48 subscribe(options, cb) { 49 console.log("subscribe is called"); 50 var ret = this.IoTDeviceInstance.subscribe(options, cb || function() {}); 51 if (ret < 0) { 52 throw new Error('subscribe topic error', options.topic); 53 } 54 55 return ret; 56 } 57 58 unsubscribe(topic, cb) { 59 var ret = this.IoTDeviceInstance.unsubscribe(topic, cb || function() {}); 60 if (ret < 0) { 61 throw new Error('unsubscribe topic error', topic); 62 } 63 64 return ret; 65 } 66 67 publish(options, cb) { 68 var ret = this.IoTDeviceInstance.publish(options, cb || function() {}); 69 if (ret < 0) { 70 throw new Error('publish topic info error', options.topic); 71 } 72 73 return ret; 74 } 75 76 getNtpTime(cb) { 77 var ret = this.IoTDeviceInstance.getNtpTime(cb || function() {}); 78 if (ret < 0) { 79 throw new Error('get ntp time error'); 80 } 81 return ret; 82 } 83 84 postProps(params, cb) { 85 console.log("postProps is called"); 86 var ret = this.IoTDeviceInstance.postProps(params, cb || function() {}); 87 if (ret < 0) { 88 throw new Error('post props error'); 89 } 90 91 return ret; 92 } 93 94 onProps(cb) { 95 var ret = this.IoTDeviceInstance.onProps(cb); 96 if (ret < 0) { 97 throw new Error('on props error'); 98 } 99 100 return ret; 101 } 102 103 postEvent(options, cb) { 104 console.log("postEvent is called"); 105 var ret = this.IoTDeviceInstance.postEvent(options, cb || function() {}); 106 if (ret < 0) { 107 throw new Error('post event error'); 108 } 109 110 return ret; 111 } 112 113 onService(cb) { 114 var ret = this.IoTDeviceInstance.onService(cb); 115 if (ret < 0) { 116 throw new Error('on service error'); 117 } 118 119 return ret; 120 } 121 122 end(cb) { 123 var ret = this.IoTDeviceInstance.close(cb || function() {}); 124 if (ret < 0) { 125 throw new Error('end iot client error'); 126 } 127 128 return ret; 129 } 130} 131 132 133class IotGatewayClient extends event.EventEmitter{ 134 constructor(options){ 135 super(); 136 if(!options || !options.productKey || !options.deviceName || !options.deviceSecret){ 137 throw new Error('device info error'); 138 } 139 140 this.options = { 141 productKey: options.productKey, 142 deviceName: options.deviceName, 143 deviceSecret: options.deviceSecret, 144 keepaliveSec: options.keepaliveSec || 60, 145 region: options.region || 'cn-shanghai' 146 } 147 148 this._on(); 149 this._connect(); 150 } 151 152 _connect() { 153 this.IoTGatewayInstance = AIOT_GATEWAY.gateway(this.options, function(res) { 154 if (!res.handle || res.code != 0) { 155 return; 156 } 157 this.iot_gateway_handle = res.handle; 158 this.emit('connect'); 159 }.bind(this)); 160 } 161 162 getGatewayHandle() { 163 return this.iot_gateway_handle; 164 } 165 166 _on() { 167 AIOT_GATEWAY.onMqttMessage(function(res) { 168 switch (res.code) { 169 case 1: 170 this.emit('reconnect'); break; 171 case 2: 172 this.emit('disconnect'); break; 173 case 3: 174 this.emit('message', res); break; 175 default : break ; 176 } 177 }.bind(this)); 178 } 179 180 addTopo(options, cb) { 181 var ret = this.IoTGatewayInstance.addTopo(options, cb || function() {}); 182 if (ret < 0) { 183 throw new Error('add topo error'); 184 } 185 186 return ret; 187 } 188 189 getTopo(cb) { 190 var ret = this.IoTGatewayInstance.getTopo(cb || function() {}); 191 if (ret < 0) { 192 throw new Error('get topo error'); 193 } 194 195 return ret; 196 } 197 198 removeTopo(options, cb) { 199 var ret = this.IoTGatewayInstance.removeTopo(options, cb || function() {}); 200 if (ret < 0) { 201 throw new Error('remove topo error'); 202 } 203 204 return ret; 205 } 206 207 login(options, cb) { 208 var ret = this.IoTGatewayInstance.login(options, cb || function() {}); 209 if (ret < 0) { 210 throw new Error('aiot subdev login error'); 211 } 212 213 return ret; 214 } 215 216 logout(options, cb) { 217 var ret = this.IoTGatewayInstance.logout(options, cb || function() {}); 218 if (ret < 0) { 219 throw new Error('aiot subdev logout error'); 220 } 221 222 return ret; 223 } 224 225 registerSubDevice(options, cb) { 226 var ret = this.IoTGatewayInstance.registerSubDevice(options, cb || function() {}); 227 if (ret < 0) { 228 throw new Error('aiot register subdev error'); 229 } 230 231 return ret; 232 } 233 234 subscribe(params, cb) { 235 var ret = this.IoTGatewayInstance.subscribe(params, cb || function() {}); 236 if (ret < 0) { 237 throw new Error('subscribe topic error', options.topic); 238 } 239 240 return ret; 241 } 242 243 unsubscribe(topic, cb) { 244 var ret = this.IoTGatewayInstance.unsubscribe(topic, cb || function() {}); 245 if (ret < 0) { 246 throw new Error('unsubscribe topic error', topic); 247 } 248 249 return ret; 250 } 251 252 publish(options, cb) { 253 var ret = this.IoTGatewayInstance.publish(options, cb || function() {}); 254 if (ret < 0) { 255 throw new Error('publish topic info error', options.topic); 256 } 257 258 return ret; 259 } 260 261 getNtpTime(cb) { 262 var ret = this.IoTGatewayInstance.getNtpTime(cb || function() {}); 263 if (ret < 0) { 264 throw new Error('get ntp time error'); 265 } 266 return ret; 267 } 268} 269 270export function device(options) { 271 console.log("create IotDeviceClient"); 272 return new IotDeviceClient(options); 273} 274 275export function gateway(options){ 276 console.log("create IotGatewayClient"); 277 return new IotGatewayClient(options); 278} 279 280export function dynreg(options, cb) { 281 var ret = AIOT_DEVICE.register(options, cb); 282 if (ret < 0) { 283 throw new Error('dynmic register error'); 284 } 285 286 return ret; 287}