68 lines
1.9 KiB
TypeScript
68 lines
1.9 KiB
TypeScript
/**
|
|
* Main file to be executed.
|
|
*
|
|
* Monitor and upload different KNX events
|
|
*/
|
|
|
|
import { readAddresses, readMessage, KNXMessage } from 'knx-lib'
|
|
import knx from 'knx';
|
|
import { database } from './appwrite'
|
|
import dotenv from 'dotenv'
|
|
import { DPT } from 'dptlib/lib/definitions';
|
|
import { getDPTforAddress, upsert } from './processTelegram';
|
|
import { MetricDocument } from './definitions'
|
|
import { DPTAndAddressID } from './definitions';
|
|
|
|
// read the addresses and DPTs.
|
|
// let addresses = readAddresses("")
|
|
|
|
dotenv.config({ override: false })
|
|
|
|
// TODO: Create the map address -> DPT
|
|
// TODO: Automatic load addresses to DB
|
|
|
|
async function verify() {
|
|
getDPTforAddress('3/5/6')
|
|
}
|
|
|
|
verify().then(() => console.log("---")).catch(e => console.log('Error: ', e))
|
|
|
|
|
|
|
|
async function main(evt: string, src: string, dest: string, value: Buffer) {
|
|
|
|
const dpt: DPTAndAddressID = (await getDPTforAddress(dest))
|
|
|
|
|
|
const newLocal = (dpt.dpt.decoder(value) === 1) ? true : false;
|
|
const document: MetricDocument = {
|
|
address_id: dpt.addressId,
|
|
dpt: dpt.id,
|
|
timestamp: new Date().getTime(),
|
|
value: dpt.id.startsWith('DPT1.') ? newLocal : dpt.dpt.decoder(value)
|
|
}
|
|
|
|
upsert(document)
|
|
|
|
console.log("%s **** KNX EVENT: %j, src: %j, dest: %j, value: %j (DPT: %j}",
|
|
new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''),
|
|
evt, src, dest, document);
|
|
}
|
|
|
|
// Create the connection
|
|
const connection = new knx.Connection({
|
|
ipAddr: process.env.KNX_GATEWAY_IP,
|
|
ipPort: Number.parseInt(process.env.KNX_GATEWAY_PORT),
|
|
physAddr: process.env.KNX_GATEWAY_KNX_ADDRESS,
|
|
//loglevel: 'trace',
|
|
handlers: {
|
|
connected: function () {
|
|
console.log('Connected!');
|
|
},
|
|
event: function (evt: string, src: string, dest: string, value: Buffer) {
|
|
main(evt, src, dest, value)
|
|
.then(() => { /* This is intentional */ })
|
|
.catch(e => console.log('Error: ', e))
|
|
}
|
|
}
|
|
}); |