diff --git a/src/appwrite.ts b/src/appwrite.ts index 4ea0d5d..626ccc2 100644 --- a/src/appwrite.ts +++ b/src/appwrite.ts @@ -1,8 +1,12 @@ import { Client, Database } from "node-appwrite"; +import dotenv from 'dotenv' + +dotenv.config({ override: false }) const client: Client = new Client(); client.setEndpoint(process.env.APPWRITE_ENDPOINT_URL) .setProject(process.env.APPWRITE_PROJECT_ID) .setKey(process.env.APPWRITE_API_KEY) -export const database = new Database(client) \ No newline at end of file +export const database = new Database(client) + diff --git a/src/index.ts b/src/index.ts index 1ad6512..c67b547 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,13 +6,36 @@ 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 } from './processTelegram'; +import { mainModule } from 'process'; // 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({ dest_addr: '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: DPT = (await getDPTforAddress({ dest_addr: dest })).dpt + + console.log("%s **** KNX EVENT: %j, src: %j, dest: %j, value: %j (DPT: %j}", + new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''), + evt, src, dest, dpt.decoder(value)); +} // Create the connection const connection = new knx.Connection({ @@ -25,9 +48,11 @@ const connection = new knx.Connection({ console.log('Connected!'); }, event: function (evt: string, src: string, dest: string, value: Buffer) { - console.log("%s **** KNX EVENT: %j, src: %j, dest: %j, value: %j", - new Date().toISOString().replace(/T/, ' ').replace(/\..+/, ''), - evt, src, dest, value); + + main(evt, src, dest, value).then(() => { }).catch(e => console.log('Error: ', e)) + + + //readMessage(value,new Date(), ) } } }); \ No newline at end of file diff --git a/src/processTelegram.ts b/src/processTelegram.ts index 5c239ad..d8d58a5 100644 --- a/src/processTelegram.ts +++ b/src/processTelegram.ts @@ -1,8 +1,11 @@ +import dotenv from 'dotenv' import { Client, Database, Query } from 'node-appwrite' import { DataPointType } from 'dptlib/lib/DataPointType' import { DPT } from 'dptlib/lib/definitions' import { database } from './appwrite' +dotenv.config({ override: false }) + // TODO: Get the map from a list of environment variables const dptMap: { [index: string]: string } = { 'DPT1.1': '61f46d9b81de70ac7ece', // percent @@ -11,9 +14,9 @@ const dptMap: { [index: string]: string } = { } // TODO: add types -export async function getDPTforAddress(record: any) { +export async function getDPTforAddress(record: any): Promise { let list = await database.listDocuments(process.env.APPWRITE_KNX_ADDRESS_COLLECTION, [ - Query.equal('project_id', process.env.APPWRITE_PROJECT_ID), + Query.equal('project_id', process.env.APPWRITE_CONFIGURATION_ID), Query.equal('address', record.dest_addr) ]); @@ -23,7 +26,10 @@ export async function getDPTforAddress(record: any) { throw 'The address is not unique!' } - record.dpt = DataPointType.TYPES[(list.documents[0] as any).dpt] + // TODO: Move thematcher into the DPT lib as a static function + const m = ((list.documents[0] as any).dpt).toUpperCase().match(/(?:DPT)?(\d+)(\.(\d+))?/); + + record.dpt = DataPointType.TYPES[`DPT${m[1]}`] record.addressId = list.documents[0]['$id'] return record }