|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const BluePromise = require('bluebird'); |
| 4 | +const net = require('net'); |
| 5 | +const rl = require('readline'); |
| 6 | +const util = require('util'); |
| 7 | + |
| 8 | +const constants = require('./constants'); |
| 9 | +const DevicePort = require('./devicePort'); |
| 10 | + |
| 11 | +module.exports = Device; |
| 12 | + |
| 13 | +function Device(beacon) { |
| 14 | + this.beacon = beacon; |
| 15 | + this.created = Date.now(); |
| 16 | +} |
| 17 | + |
| 18 | +Device.prototype.getUUID = function () { |
| 19 | + return this.beacon[constants.BEACON_UUID]; |
| 20 | +} |
| 21 | + |
| 22 | +Device.prototype.getModel = function () { |
| 23 | + return this.beacon[constants.BEACON_MODEL]; |
| 24 | +} |
| 25 | + |
| 26 | +Device.prototype.getUrl = function () { |
| 27 | + return this.beacon[constants.BEACON_URL]; |
| 28 | +} |
| 29 | + |
| 30 | +Device.prototype.getIpAddress = function () { |
| 31 | + const url = this.beacon[constants.BEACON_URL]; |
| 32 | + const idx = url.indexOf('://'); |
| 33 | + if (idx >= 0) { |
| 34 | + return url.substring(idx + 3); |
| 35 | + } else { |
| 36 | + return url; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +Device.prototype.getCreated = function () { |
| 41 | + return this.created; |
| 42 | +} |
| 43 | + |
| 44 | +Device.prototype.getModulePorts = function () { |
| 45 | + const device = this; |
| 46 | + return new BluePromise(function (resolve, reject) { |
| 47 | + const socket = net.createConnection(constants.PORT_NUMBER, device.getIpAddress()); |
| 48 | + socket |
| 49 | + .on('connect', function () { |
| 50 | + const devices = []; |
| 51 | + const cmd = util.format('%s', constants.COMMAND_GETDEVICES); |
| 52 | + |
| 53 | + rl.createInterface(socket, socket).on('line', function (line) { |
| 54 | + console.log('Received (%s): %s', cmd, line); |
| 55 | + if (line === 'endlistdevices') { |
| 56 | + resolve(devices); |
| 57 | + socket.destroy(); |
| 58 | + } else { |
| 59 | + const args = line.split(','); |
| 60 | + if (args.length === 3 && args[0] === 'device') { |
| 61 | + if (args[1] !== '0') { // ignore the root module (ethernet/wifi) |
| 62 | + switch (args[2]) { |
| 63 | + case constants.LINK_TYPE_3RELAY: |
| 64 | + devices.push(new DevicePort(device, args[1], 1, constants.BEACON_TYPE_CC)); |
| 65 | + devices.push(new DevicePort(device, args[1], 2, constants.BEACON_TYPE_CC)); |
| 66 | + devices.push(new DevicePort(device, args[1], 3, constants.BEACON_TYPE_CC)); |
| 67 | + break; |
| 68 | + case constants.LINK_TYPE_1SERIAL: |
| 69 | + devices.push(new DevicePort(device, args[1], args[1], constants.BEACON_TYPE_SR)); |
| 70 | + break; |
| 71 | + case constants.LINK_TYPE_1IR: |
| 72 | + case constants.LINK_TYPE_1IRBLASTER: |
| 73 | + devices.push(new DevicePort(device, args[1], 1, constants.BEACON_TYPE_IR)); |
| 74 | + break; |
| 75 | + case constants.LINK_TYPE_3IR: |
| 76 | + case constants.LINK_TYPE_1IRTRIPORT: |
| 77 | + case constants.LINK_TYPE_1IRTRIPORTBLASTER: |
| 78 | + devices.push(new DevicePort(device, args[1], 1, constants.BEACON_TYPE_IR)); |
| 79 | + devices.push(new DevicePort(device, args[1], 2, constants.BEACON_TYPE_IR)); |
| 80 | + devices.push(new DevicePort(device, args[1], 3, constants.BEACON_TYPE_IR)); |
| 81 | + break; |
| 82 | + default: |
| 83 | + console.error('Unknown port type: ' + line); |
| 84 | + break; |
| 85 | + } |
| 86 | + } |
| 87 | + } else { |
| 88 | + reject('Unknown response: ' + line); |
| 89 | + socket.destroy(); |
| 90 | + } |
| 91 | + } |
| 92 | + }); |
| 93 | + console.log('Sending %s', cmd); |
| 94 | + socket.write(cmd + '\r'); |
| 95 | + }) |
| 96 | + .on('error', function (err) { |
| 97 | + socket.destroy(); |
| 98 | + reject(err); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}; |
| 102 | + |
| 103 | +Device.prototype.setState = function (module, port, state) { |
| 104 | + const device = this; |
| 105 | + return new BluePromise(function (resolve, reject) { |
| 106 | + const socket = net.createConnection(constants.PORT_NUMBER, device.getIpAddress()); |
| 107 | + socket |
| 108 | + .on('connect', function () { |
| 109 | + const cmd = util.format('%s,%s:%s,%s', constants.COMMAND_SETSTATE, module, port, state); |
| 110 | + |
| 111 | + rl.createInterface(socket, socket).on('line', function (line) { |
| 112 | + console.log('Received (%s): %s', cmd, line); |
| 113 | + const args = line.split(','); |
| 114 | + if (args.length === 3 && (args[0] === constants.RESPONSE_STATE || args[0] === constants.RESPONSE_SETSTATE)) { |
| 115 | + resolve(args[2]); |
| 116 | + } else { |
| 117 | + reject('Unknown response: ' + line); |
| 118 | + } |
| 119 | + socket.destroy(); |
| 120 | + }); |
| 121 | + console.log('Sending %s', cmd); |
| 122 | + socket.write(cmd + '\r'); |
| 123 | + }) |
| 124 | + .on('error', function (err) { |
| 125 | + socket.destroy(); |
| 126 | + reject(err); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}; |
| 130 | + |
| 131 | +Device.prototype.getState = function (module, port) { |
| 132 | + const device = this; |
| 133 | + return new BluePromise(function (resolve, reject) { |
| 134 | + const socket = net.createConnection(constants.PORT_NUMBER, device.getIpAddress()); |
| 135 | + socket |
| 136 | + .on('connect', function () { |
| 137 | + const cmd = util.format('%s,%s:%s', constants.COMMAND_GETSTATE, module, port); |
| 138 | + |
| 139 | + rl.createInterface(socket, socket).on('line', function (line) { |
| 140 | + console.log('Received (%s): %s', cmd, line); |
| 141 | + const args = line.split(','); |
| 142 | + if (args.length === 3 && args[0] === constants.RESPONSE_STATE) { |
| 143 | + resolve(args[2]); |
| 144 | + } else { |
| 145 | + reject('Unknown response: ' + line); |
| 146 | + } |
| 147 | + socket.destroy(); |
| 148 | + }); |
| 149 | + console.log('Sending %s', cmd); |
| 150 | + socket.write(cmd + '\r'); |
| 151 | + }) |
| 152 | + .on('error', function (err) { |
| 153 | + socket.destroy(); |
| 154 | + reject(err); |
| 155 | + }); |
| 156 | + }); |
| 157 | +}; |
| 158 | + |
| 159 | +Device.prototype.sendIR = function (module, port, state) { |
| 160 | + const device = this; |
| 161 | + return new BluePromise(function (resolve, reject) { |
| 162 | + const socket = net.createConnection(constants.PORT_NUMBER, device.getIpAddress()); |
| 163 | + socket |
| 164 | + .on('connect', function () { |
| 165 | + const cmd = util.format('%s,%s:%s,%s', constants.COMMAND_SENDIR, module, port, state); |
| 166 | + |
| 167 | + rl.createInterface(socket, socket).on('line', function (line) { |
| 168 | + console.log('Received (%s): %s', cmd, line); |
| 169 | + const args = line.split(','); |
| 170 | + if (args.length === 3 && (args[0] === constants.RESPONSE_IR)) { |
| 171 | + resolve(args[2]); |
| 172 | + } else { |
| 173 | + reject('Unknown response: ' + line); |
| 174 | + } |
| 175 | + socket.destroy(); |
| 176 | + }); |
| 177 | + console.log('Sending %s', cmd); |
| 178 | + socket.write(cmd + '\r'); |
| 179 | + }) |
| 180 | + .on('error', function (err) { |
| 181 | + socket.destroy(); |
| 182 | + reject(err); |
| 183 | + }); |
| 184 | + }); |
| 185 | +}; |
| 186 | + |
| 187 | +Device.prototype.sendSerial = function (module, serial) { |
| 188 | + const device = this; |
| 189 | + return new BluePromise(function (resolve, reject) { |
| 190 | + const socket = net.createConnection(constants.PORT_NUMBER + module, device.getIpAddress()); |
| 191 | + socket |
| 192 | + .on('connect', function () { |
| 193 | + console.log('Sending %s', serial); |
| 194 | + socket.write(serial); |
| 195 | + socket.destroy(); |
| 196 | + }) |
| 197 | + .on('error', function (err) { |
| 198 | + socket.destroy(); |
| 199 | + reject(err); |
| 200 | + }); |
| 201 | + }); |
| 202 | +}; |
| 203 | + |
| 204 | + |
0 commit comments