|
| 1 | +const _ = global.$; |
| 2 | +const BaseModule = require('./baseModule'); |
| 3 | + |
| 4 | +class AsomeKit extends BaseModule { |
| 5 | + constructor() { |
| 6 | + super(); |
| 7 | + |
| 8 | + this.isDraing = false; |
| 9 | + this.sendBuffer = []; |
| 10 | + |
| 11 | + this.receivedText = ""; |
| 12 | + |
| 13 | + this.msg_id = ''; |
| 14 | + this.sendToEntry = { |
| 15 | + msg_id: "", |
| 16 | + distance: 0, |
| 17 | + udp_msg: "", |
| 18 | + }; |
| 19 | + } |
| 20 | + |
| 21 | + connect() { |
| 22 | + } |
| 23 | + |
| 24 | + socketReconnection() { |
| 25 | + this.socket.send(this.handler.encode()); |
| 26 | + } |
| 27 | + |
| 28 | + requestInitialData() { |
| 29 | + console.log("requestInitialData"); |
| 30 | + |
| 31 | + var init_str = "import button;"; |
| 32 | +// var init_str = "import button; import music; import dht11; import tm1637; import vibration_sensor; import hcsr04; light = AnalogPin(1); vs = vibration_sensor.create(1, 1000); tm1637.open(3, 4); ht = dht11.create(5); bt = button.create(6); hcsr04.open(7, 8); buzzer = OutputPin(11); music.open(12);"; |
| 33 | + return Buffer.from(init_str, "ascii"); |
| 34 | + } |
| 35 | + |
| 36 | + setSerialPort(sp) { |
| 37 | + this.sp = sp; |
| 38 | + } |
| 39 | + |
| 40 | + setSocket(socket) { |
| 41 | + this.socket = socket; |
| 42 | + } |
| 43 | + |
| 44 | + checkInitialData(data, config) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + validateLocalData(data) { |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + requestRemoteData(handler) { |
| 53 | + var sendToEntry = this.sendToEntry; |
| 54 | + // console.log("to Entry: ", sendToEntry); |
| 55 | + |
| 56 | + for (var key in sendToEntry) { |
| 57 | + handler.write(key, sendToEntry[key]); |
| 58 | + } |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + handleRemoteData({ receiveHandler = {} }) { |
| 63 | + const { data: handlerData } = receiveHandler; |
| 64 | + if (_.isEmpty(handlerData)) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + if (handlerData.msg_id == undefined) { |
| 69 | + console.log("from handlerData.msg_id == undefined", handlerData); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + if (handlerData.msg_id != this.msg_id) { |
| 74 | + console.log("from Entry: ", handlerData); |
| 75 | + |
| 76 | + this.msg_id = handlerData.msg_id; |
| 77 | + this.sendBuffer.push(Buffer.from(handlerData.msg + "\r", 'ascii')); |
| 78 | + this.sendBuffer.push(Buffer.from("'#I'" + "'D " + handlerData.msg_id + "'\r", 'ascii')); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + requestLocalData() { |
| 83 | + var self = this; |
| 84 | + |
| 85 | + if (!this.isDraing && this.sendBuffer.length > 0) { |
| 86 | + this.isDraing = true; |
| 87 | + var msg = this.sendBuffer.shift(); |
| 88 | + console.log("to AsomeBot: ", msg.toString(), this.sendBuffer.length); |
| 89 | + this.sp.write(msg, function() { |
| 90 | + if (self.sp) { |
| 91 | + self.sp.drain(function() { |
| 92 | + self.isDraing = false; |
| 93 | + }); |
| 94 | + } |
| 95 | + }); |
| 96 | + } |
| 97 | + |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + handleLocalData(data) { |
| 102 | +// console.log("handleLocalData: ", this.receivedText); |
| 103 | + |
| 104 | + this.receivedText = this.receivedText + data.toString(); |
| 105 | + |
| 106 | + var index = this.receivedText.indexOf('\r'); |
| 107 | + while (index >= 0) { |
| 108 | + var line = this.receivedText.substring(0, index); |
| 109 | + this.receivedText = this.receivedText.substring(index + 1); |
| 110 | + |
| 111 | + console.log("from AsomeBot: ", line); |
| 112 | + |
| 113 | + if (line.indexOf('#DT') >= 0) { |
| 114 | + var values = line.split(" "); |
| 115 | + if (values.length > 1) this.sendToEntry.distance = values[1]; |
| 116 | + } |
| 117 | + if (line.indexOf('#UDP') >= 0) { |
| 118 | + var values = line.split(" "); |
| 119 | + if (values.length > 1) { |
| 120 | + this.sendToEntry.udp_id = this.msg_id; |
| 121 | + this.sendToEntry.udp_msg = values[1]; |
| 122 | + } |
| 123 | + } |
| 124 | + if (line.indexOf('#ID') >= 0) this.sendToEntry.msg_id = line; |
| 125 | + |
| 126 | + index = this.receivedText.indexOf('\r'); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + setSocketData({ socketData, data }) { |
| 131 | + } |
| 132 | + |
| 133 | + lostController() { } |
| 134 | + |
| 135 | + disconnect(connect) { |
| 136 | + connect.close(); |
| 137 | + this.sp = null; |
| 138 | + } |
| 139 | + |
| 140 | + reset() { |
| 141 | + this.sp = null; |
| 142 | + this.sendBuffer = []; |
| 143 | + this.receivedText = ""; |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +module.exports = new AsomeKit(); |
0 commit comments