|
| 1 | +import TCPConnection from "../src/connection/tcp_connection.js"; |
| 2 | +import CayenneLpp from "../src/cayenne_lpp.js"; |
| 3 | + |
| 4 | +// create connection |
| 5 | +const connection = new TCPConnection("10.1.0.75", 5000); |
| 6 | + |
| 7 | +// wait until connected |
| 8 | +connection.on("connected", async () => { |
| 9 | + |
| 10 | + // we are now connected |
| 11 | + console.log("Connected"); |
| 12 | + |
| 13 | + // find contact |
| 14 | + // const contact = await connection.findContactByName("Liam's Water Tank"); |
| 15 | + // const contact = await connection.findContactByPublicKeyPrefix([0xc9, 0xe1, 0x50, 0x58]); |
| 16 | + // const contact = await connection.findContactByPublicKeyPrefix(Buffer.from("c9e15058", "hex")); |
| 17 | + const contact = await connection.findContactByPublicKeyPrefix(Buffer.from("c9e15058af33781743b222bc98105abe7429cf46f7212f767ff2a7ce1bea5dcb", "hex")); |
| 18 | + if(!contact){ |
| 19 | + console.log("Contact not found"); |
| 20 | + await connection.close(); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + // get sensor telemetry |
| 25 | + console.log("Fetching telemetry..."); |
| 26 | + const telemetry = await connection.getTelemetry(contact.publicKey); |
| 27 | + |
| 28 | + // disconnect |
| 29 | + await connection.close(); |
| 30 | + |
| 31 | + // parse telemetry |
| 32 | + const parsedTelemetry = CayenneLpp.parse(telemetry.lppSensorData); |
| 33 | + |
| 34 | + // find telemetry on channel 1 |
| 35 | + const selfVoltageTelemetry = parsedTelemetry.find((item) => item.channel === 1 && item.type === CayenneLpp.LPP_VOLTAGE); |
| 36 | + |
| 37 | + // make sure telemetry found |
| 38 | + if(selfVoltageTelemetry == null){ |
| 39 | + console.log("Telemetry Missing"); |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + console.log({ |
| 44 | + voltage: selfVoltageTelemetry.value, |
| 45 | + }); |
| 46 | + |
| 47 | +}); |
| 48 | + |
| 49 | +// connect to meshcore device |
| 50 | +await connection.connect(); |
0 commit comments