Untested submitting events to db

This commit is contained in:
watsonb8 2019-09-22 16:57:18 -04:00
parent 0fc390c713
commit acf8889874
4 changed files with 56 additions and 29 deletions

4
src/Models/db/event.ts Normal file
View File

@ -0,0 +1,4 @@
export interface IEvent {
id: number;
timestamp: Date;
}

View File

@ -0,0 +1,5 @@
export interface IEventServiceInstance {
id: number;
eventId: number;
serviceId: string;
}

View File

@ -0,0 +1,7 @@
export interface IEventInstanceCharacteristics {
id: number;
instanceId: number;
key: string;
type: string;
value: string;
}

View File

@ -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<IServiceType> = [];
constructor(@inject("log") log: log, @inject("config") config: IConfig) {
this.log = log;
@ -48,12 +53,12 @@ export default class Monitor {
private initializeHapMonitor = async (): Promise<void> => {
await this.client.discover();
const services: Array<IServiceType> = 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<Database> => {
@ -125,12 +121,27 @@ export default class Monitor {
});
}
private syncDatabase = async (): Promise<void> => {
private onServiceUpdated = async (updatedServices: Array<IServiceType>): Promise<void> => {
const eventId = this.knex && this.knex().into('events').insert<IEvent>({
timestamp: this.knex && this.knex.fn.now(),
}).returning('id');
}
await Promise.all(updatedServices.map(async (service: IServiceType): Promise<void> => {
const instanceId = this.knex && this.knex().into('event_service_instances').insert<IEventServiceInstance>({
eventId: eventId,
serviceUniqueId: service.uniqueId
}).returning('id');
private onServiceUpdated = (updatedServices: Array<IServiceType>): void => {
console.log("asdf");
await Promise.all(service.serviceCharacteristics.map(async (characteristic: ICharacteristicType): Promise<void> => {
this.knex && await this.knex().into('event_instance_characteristics').insert<IEventInstanceCharacteristics>({
instanceId: instanceId,
eventId: eventId,
key: characteristic.serviceName,
type: characteristic.type,
value: characteristic.value
});
}));
}));
}
}