Created
December 3, 2021 13:33
-
-
Save diegofcornejo/72f72289359ab7f9018911cff402aa19 to your computer and use it in GitHub Desktop.
Mclimate T-Valve Uplink decoder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //decoder | |
| function buf2hex(buffer) { // buffer is an ArrayBuffer | |
| return [...new Uint8Array(buffer)] | |
| .map(x => x.toString(16).padStart(2, '0')) | |
| .join(''); | |
| } | |
| let payload = 'YGUAALI='; | |
| // let decoded = Buffer.from(payload, 'base64'); | |
| // let hex = '64620000A4'; | |
| // let hex = '0044'; | |
| // let hex = buf2hex(decoded); | |
| let hex = buf2hex(Buffer.from(payload, 'base64')); | |
| console.log(hex); | |
| function Decoder(bytes){ | |
| let byteArray = bytes.match(/.{1,2}/g).map(byte => | |
| (parseInt(byte, 16).toString(2)).padStart(8, '0') | |
| ) | |
| console.log(byteArray) | |
| let messageTypes = ['keepalive', 'testButtonPressed', 'floodDetected', 'controlButtonPressed', 'fraudDetected']; | |
| const toBool = value => value == '1'; | |
| const shortPackage = (byteArray) => { | |
| return { | |
| reason: messageTypes[parseInt(byteArray[0].slice(0, 2))], | |
| valveState: toBool(byteArray[0][3]), | |
| boxTamper: toBool(byteArray[0][4]), | |
| floodDetectionWireState: toBool(byteArray[0][5]), | |
| flood: toBool(byteArray[0][6]), | |
| magnet: toBool(byteArray[0][7]), | |
| alarmValidated: toBool(byteArray[1][0]), | |
| manualOpenIndicator: toBool(byteArray[1][1]), | |
| manualCloseIndicator: toBool(byteArray[1][2]), | |
| } | |
| } | |
| const longPackage = (byteArray) => { | |
| return { | |
| reason: messageTypes[parseInt(byteArray[0].slice(0, 2))], | |
| valveState: toBool(byteArray[0][3]), | |
| boxTamper: toBool(byteArray[0][4]), | |
| floodDetectionWireState: toBool(byteArray[0][5]), | |
| flood: toBool(byteArray[0][6]), | |
| magnet: toBool(byteArray[0][7]), | |
| alarmValidated: toBool(byteArray[1][0]), | |
| manualOpenIndicator: toBool(byteArray[1][1]), | |
| manualCloseIndicator: toBool(byteArray[1][2]), | |
| closeTime: parseInt(byteArray[2], 2), | |
| openTime: parseInt(byteArray[3], 2), | |
| temp1: parseInt(byteArray[4], 2), | |
| temp2: parseInt(byteArray[5], 2), | |
| battery: (parseInt(byteArray[6], 2) * 4) + 3000, | |
| } | |
| } | |
| if(byteArray.length > 2){ | |
| return longPackage(byteArray); | |
| }else{ | |
| return shortPackage(byteArray); | |
| } | |
| } | |
| Decoder(hex); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment