Reading addresses from the db works.

Monitoring loop works.
This commit is contained in:
Laur Ivan 2022-03-16 00:52:15 +01:00
parent feb331d6f1
commit 08676075f8
3 changed files with 42 additions and 7 deletions

View File

@ -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)
export const database = new Database(client)

View File

@ -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(), )
}
}
});

View File

@ -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<any> {
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
}