|
| 1 | +function Module() { |
| 2 | + this.localBuffer = new Array(8); |
| 3 | + this.remoteBuffer = new Array(5); |
| 4 | + |
| 5 | + this.LOCAL_MAP = [ |
| 6 | + 'IN1', |
| 7 | + 'IN2', |
| 8 | + 'IN3', |
| 9 | + 'IR', |
| 10 | + 'IN4' |
| 11 | + ]; |
| 12 | + |
| 13 | + this.REMOTE_MAP = [ |
| 14 | + 'OUT1', |
| 15 | + 'OUT2', |
| 16 | + 'OUT3', |
| 17 | + 'DCL', |
| 18 | + 'DCR', |
| 19 | + 'SND', |
| 20 | + 'LED', |
| 21 | + 'OPT' |
| 22 | + ]; |
| 23 | +} |
| 24 | + |
| 25 | +Module.prototype.init = function(handler, config) { |
| 26 | +}; |
| 27 | + |
| 28 | +Module.prototype.requestInitialData = function() { |
| 29 | + return null; |
| 30 | +}; |
| 31 | + |
| 32 | +Module.prototype.checkInitialData = function(data, config) { |
| 33 | + return true; |
| 34 | +}; |
| 35 | + |
| 36 | +Module.prototype.validateLocalData = function(data) { |
| 37 | + var state = false; |
| 38 | + |
| 39 | + for(var i = 0; i < data.length - 1; i++) { |
| 40 | + if(data[i] === 171 && data[i + 1] === 205 ) { |
| 41 | + var dataSet = data.slice(i, i + 8); |
| 42 | + var dataSum = dataSet.reduce(function (result, value, idx) { |
| 43 | + if(idx < 2 || idx >= dataSet.length-1) { |
| 44 | + return result; |
| 45 | + } |
| 46 | + return result + value; |
| 47 | + }, 0); |
| 48 | + if((dataSum & 255) === dataSet[dataSet.length-1]) { |
| 49 | + state = true; |
| 50 | + } |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + return state; |
| 56 | +}; |
| 57 | + |
| 58 | +//로컬 데이터 처리 |
| 59 | +// data: Native Buffer |
| 60 | +Module.prototype.handleLocalData = function(data) { |
| 61 | + var buffer = this.remoteBuffer; |
| 62 | + for(var i = 0; i < data.length - 1; i++) { |
| 63 | + if(data[i] === 171 && data[i + 1] === 205 ) { |
| 64 | + var dataSet = data.slice(i + 2, i + 7); |
| 65 | + |
| 66 | + dataSet.forEach(function (value, idx) { |
| 67 | + buffer[idx] = value; |
| 68 | + }); |
| 69 | + |
| 70 | + break; |
| 71 | + } |
| 72 | + } |
| 73 | +}; |
| 74 | + |
| 75 | +//리모트 데이터 전송 |
| 76 | +Module.prototype.requestRemoteData = function(handler) { |
| 77 | + var buffer = this.remoteBuffer; |
| 78 | + for(var i = 0; i < 5; i++) { |
| 79 | + handler.write(this.LOCAL_MAP[i], buffer[i]); |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +//리모트 데이처 처리 |
| 84 | +Module.prototype.handleRemoteData = function(handler) { |
| 85 | + var buffer = this.localBuffer; |
| 86 | + |
| 87 | + this.REMOTE_MAP.forEach(function (key, idx) { |
| 88 | + buffer[idx] = handler.read(key); |
| 89 | + }); |
| 90 | +}; |
| 91 | + |
| 92 | +//로컬 데이터 전송 |
| 93 | +Module.prototype.requestLocalData = function() { |
| 94 | + var requestData = []; |
| 95 | + var buffer = this.localBuffer; |
| 96 | + |
| 97 | + // 시작 바이트 |
| 98 | + requestData.push(0xCD); // TODO change to EF |
| 99 | + requestData.push(0xAB); |
| 100 | + |
| 101 | + var checksum = 0; |
| 102 | + var isLed = false; // for adding option bit 181013 |
| 103 | + buffer.forEach(function (value, idx) { |
| 104 | + if(idx === 6 && value > 0) { |
| 105 | + isLed = true; |
| 106 | + } else if(idx === 7 && isLed) { |
| 107 | + value = value | 8; |
| 108 | + } |
| 109 | + requestData.push(value); |
| 110 | + checksum += value; |
| 111 | + }); |
| 112 | + |
| 113 | + checksum = checksum & 255; |
| 114 | + //체크썸 |
| 115 | + requestData.push(checksum); |
| 116 | + |
| 117 | + return requestData; |
| 118 | +}; |
| 119 | + |
| 120 | +Module.prototype.reset = function() { |
| 121 | +}; |
| 122 | + |
| 123 | +module.exports = new Module(); |
0 commit comments