From acf88898746043711efdae45fdf72b3cf59f4bb9 Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Sun, 22 Sep 2019 16:57:18 -0400 Subject: [PATCH] Untested submitting events to db --- src/Models/db/event.ts | 4 ++ src/Models/db/eventServiceInstance.ts | 5 ++ .../db/eventServiceInstanceCharacteristics.ts | 7 ++ src/services/monitor.ts | 69 +++++++++++-------- 4 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 src/Models/db/event.ts create mode 100644 src/Models/db/eventServiceInstance.ts create mode 100644 src/Models/db/eventServiceInstanceCharacteristics.ts diff --git a/src/Models/db/event.ts b/src/Models/db/event.ts new file mode 100644 index 0000000..467b538 --- /dev/null +++ b/src/Models/db/event.ts @@ -0,0 +1,4 @@ +export interface IEvent { + id: number; + timestamp: Date; +} \ No newline at end of file diff --git a/src/Models/db/eventServiceInstance.ts b/src/Models/db/eventServiceInstance.ts new file mode 100644 index 0000000..46aff1d --- /dev/null +++ b/src/Models/db/eventServiceInstance.ts @@ -0,0 +1,5 @@ +export interface IEventServiceInstance { + id: number; + eventId: number; + serviceId: string; +} \ No newline at end of file diff --git a/src/Models/db/eventServiceInstanceCharacteristics.ts b/src/Models/db/eventServiceInstanceCharacteristics.ts new file mode 100644 index 0000000..a5abf6a --- /dev/null +++ b/src/Models/db/eventServiceInstanceCharacteristics.ts @@ -0,0 +1,7 @@ +export interface IEventInstanceCharacteristics { + id: number; + instanceId: number; + key: string; + type: string; + value: string; +} \ No newline at end of file diff --git a/src/services/monitor.ts b/src/services/monitor.ts index d3cea0c..c0221db 100644 --- a/src/services/monitor.ts +++ b/src/services/monitor.ts @@ -1,12 +1,15 @@ import { HapClient, HapMonitor } from "../HapClient"; import { log } from '../Types/types'; import { singleton, inject, container } from 'tsyringe'; -import { IServiceType } from "../HapClient/interfaces"; +import { IServiceType, ICharacteristicType } from "../HapClient/interfaces"; import { IConfig } from "../Models/IConfig"; import { Database } from "sqlite3"; import { mkdirSync } from 'fs'; import { join } from 'path'; -import Knex = require("knex"); +import Knex from 'knex'; +import { IEvent } from "../Models/db/event"; +import { IEventServiceInstance } from "../Models/db/eventServiceInstance"; +import { IEventInstanceCharacteristics } from "../Models/db/eventServiceInstanceCharacteristics"; @singleton() @@ -19,6 +22,8 @@ export default class Monitor { private databaseName = 'automation.db'; private db?: Database; private knex?: Knex; + private hapMonitor?: HapMonitor; + private services: Array = []; constructor(@inject("log") log: log, @inject("config") config: IConfig) { this.log = log; @@ -48,12 +53,12 @@ export default class Monitor { private initializeHapMonitor = async (): Promise => { await this.client.discover(); - const services: Array = await this.client.getAllServices(); + this.services = await this.client.getAllServices(); - const hapMonitor = new HapMonitor(this.log, this.log, this.pin, services); + this.hapMonitor = new HapMonitor(this.log, this.log, this.pin, this.services); //Set service updated event - hapMonitor.on('service-updated', this.onServiceUpdated); + this.hapMonitor.on('service-updated', this.onServiceUpdated); this.log("Monitor ready"); } @@ -86,31 +91,22 @@ export default class Monitor { if (!hasTables) { //create databse tables - await this.knex.raw(`CREATE TABLE "event_service_instance_characteristics" ( - "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - "instance_id" INTEGER NOT NULL, - "key" TEXT NOT NULL, - "type" TEXT NOT NULL, - "value" BLOB NOT NULL)`); + await this.knex.raw(`CREATE TABLE "event_instance_characteristics" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + "instanceId" INTEGER NOT NULL, + "key" TEXT NOT NULL, + "type" TEXT NOT NULL, + "value" BLOB NOT NULL)`); await this.knex.raw(`CREATE TABLE "event_service_instances" ( - "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - "event_id" INTEGER NOT NULL, - "service_id" INTEGER NOT NULL)`); + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + "eventId" INTEGER NOT NULL, + "serviceUniqueId" INTEGER NOT NULL)`); await this.knex.raw(`CREATE TABLE "events" ( - "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, - "timestamp" BLOB NOT NULL)`); - - await this.knex.raw(`CREATE TABLE "services" ( - "id" INTEGER NOT NULL UNIQUE, - "uniqueId" TEXT NOT NULL UNIQUE, - "uuid" TEXT NOT NULL UNIQUE, - "type" TEXT NOT NULL, - "humanType" TEXT NOT NULL, - "serviceName" TEXT NOT NULL)`); + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, + "timestamp" BLOB NOT NULL)`); } - console.log(); }; private initDbFile = (path: string): Promise => { @@ -125,12 +121,27 @@ export default class Monitor { }); } - private syncDatabase = async (): Promise => { + private onServiceUpdated = async (updatedServices: Array): Promise => { + const eventId = this.knex && this.knex().into('events').insert({ + timestamp: this.knex && this.knex.fn.now(), + }).returning('id'); - } + await Promise.all(updatedServices.map(async (service: IServiceType): Promise => { + const instanceId = this.knex && this.knex().into('event_service_instances').insert({ + eventId: eventId, + serviceUniqueId: service.uniqueId + }).returning('id'); - private onServiceUpdated = (updatedServices: Array): void => { - console.log("asdf"); + await Promise.all(service.serviceCharacteristics.map(async (characteristic: ICharacteristicType): Promise => { + this.knex && await this.knex().into('event_instance_characteristics').insert({ + instanceId: instanceId, + eventId: eventId, + key: characteristic.serviceName, + type: characteristic.type, + value: characteristic.value + }); + })); + })); } } \ No newline at end of file