commit a2cc0ee158c78c6b2222c5c04e2a5a6e89ea0ccb Author: s.golasch Date: Tue Aug 1 14:21:39 2023 +0200 first commit diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/.DS_Store differ diff --git a/hello.js b/hello.js new file mode 100644 index 0000000..327e04d --- /dev/null +++ b/hello.js @@ -0,0 +1,7 @@ +!(async function main () { + const coordinator = new NetworkConnection({ port: 9000, host: '192.168.178.95' }) + await coordinator.connect() + const [message, crc, fullMessage] = await coordinator.sendCommand(Buffer.from('fd00030001009e03', 'hex')) + console.log([message, crc, fullMessage]) + coordinator.__emitter.on('event', console.log.bind(console)) +}()) diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..a94d3b2 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,35 @@ +{ + "name": "thingie", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "base64-js": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz", + "integrity": "sha512-mLQ4i2QO1ytvGWFWmcngKO//JXAQueZvwEKtjgQFM4jIK0kU+ytMfplL8j+n5mspOfjHwoAg+9yhb7BwAHm36g==" + }, + "buffer": { + "version": "5.4.2", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.4.2.tgz", + "integrity": "sha512-iy9koArjAFCzGnx3ZvNA6Z0clIbbFgbdWQ0mKD3hO0krOrZh8UgA6qMKcZvwLJxS+D6iVR76+5/pV56yMNYTag==", + "requires": { + "base64-js": "^1.0.2", + "ieee754": "^1.1.4" + } + }, + "crc": { + "version": "3.8.0", + "resolved": "https://registry.npmjs.org/crc/-/crc-3.8.0.tgz", + "integrity": "sha512-iX3mfgcTMIq3ZKLIsVFAbv7+Mc10kxabAGQb8HvjA1o3T1PIYprbakQ65d3I+2HGHt6nSKkM9PYjgoJO2KcFBQ==", + "requires": { + "buffer": "^5.1.0" + } + }, + "ieee754": { + "version": "1.1.13", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.1.13.tgz", + "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..69b9178 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "thingie", + "version": "1.0.0", + "description": "", + "main": "thingie.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "crc": "^3.8.0" + } +} diff --git a/thingie.js b/thingie.js new file mode 100644 index 0000000..c73abc4 --- /dev/null +++ b/thingie.js @@ -0,0 +1,78 @@ +const HMUARTLGW_DST_OS = 0 +const HMUARTLGW_DST_APP = '1' + +const crc = function (message) { + var $crc = 0xd77f + var buf = Buffer.from(message, 'hex') + for (var byte of buf) { + $crc ^= (byte << 8) & 0xff00 + for (var $i = 0; $i < 8; $i++) { + if ($crc & 0x8000) { + $crc = ($crc << 1) & 0xffff + $crc ^= 0x8005 + } else { + $crc = ($crc << 1) & 0xffff + } + } + } + return $crc.toString(16) +} + +let leading = 'fd00' +let dst = String('0' + String(HMUARTLGW_DST_APP)) +let msgCountPerDevice = 0 +let msgCountPerCoordinator = 19 +let length = 0 +let filler = '000000' +let payload = `A0 11 670A2A 19C4DB 0201 C8 0000`.replace(/ /g, '').toString('hex') + +// increase counter +msgCountPerDevice = parseInt(msgCountPerDevice + 1, 10) +msgCountPerCoordinator = parseInt(msgCountPerCoordinator + 1, 10) + +// calculate length +length = ((payload.length + 2) / 2) + +// get hex values from counter +let msgCountPerDeviceHex = parseInt(msgCountPerDevice, 10).toString(16) +let msgCountPerCoordinatorHex = parseInt(msgCountPerCoordinator, 10).toString(16) + +if (msgCountPerDeviceHex.length === 1) { + msgCountPerDeviceHex = '0' + msgCountPerDeviceHex +} + +if (msgCountPerCoordinatorHex.length === 1) { + msgCountPerCoordinatorHex = '0' + msgCountPerCoordinatorHex +} + +// build frame +let frame = `${leading} ${length} ${dst} ${msgCountPerCoordinatorHex} 02 ${filler} ${msgCountPerDeviceHex} ${payload}`.replace(/ /g, '') + +// calculate crc +frame += crc(frame) + +const net = require('net'); +const client = net.createConnection({ port: 9000, host: '192.168.178.95' }, () => { + // 'connect' listener + console.log('connected to server!'); + console.log(Buffer.from(frame, 'hex')) + client.write(Buffer.from(frame, 'hex')); + //client.write(Buffer.from('fd00030001009e03', 'hex')); + //client.write(Buffer.from('fd00030002039409', 'hex')); +}); +client.on('data', (data) => { + console.log(data.toString()) +}); +client.on('end', () => { + console.log('disconnected from server'); +}) + +// C: fd00 03 0001 009e03 +// D: fd00 0e 0001 0402 436f5f4350555f417070 + +// C: fd00 03 0002 039409 +// D: fd00 04 0002 04011916 -> ? +// D: fd00 0d 000000 436f -> Co +// D: 5f4350555f417070d831 -> CPU_APP +// D: fd00 12 0100 050000 -> ? +// D 26a186701ac33300000000ef3fa001 \ No newline at end of file