2018-06-27 14:46:03 -06:00
|
|
|
import logger from "@common/logger";
|
2018-07-01 02:00:17 -06:00
|
|
|
import * as mqtt from "@common/sprinklersRpc/mqtt";
|
2018-07-20 00:03:11 -06:00
|
|
|
import { Database } from "./Database";
|
2018-05-24 10:36:16 -06:00
|
|
|
|
2018-06-25 02:20:44 -06:00
|
|
|
export class ServerState {
|
2018-07-13 00:04:59 -06:00
|
|
|
mqttClient: mqtt.MqttRpcClient;
|
2018-06-27 14:46:03 -06:00
|
|
|
database: Database;
|
2018-05-24 10:36:16 -06:00
|
|
|
|
2018-06-27 14:46:03 -06:00
|
|
|
constructor() {
|
2018-06-16 23:54:03 -06:00
|
|
|
const mqttUrl = process.env.MQTT_URL;
|
|
|
|
if (!mqttUrl) {
|
|
|
|
throw new Error("Must specify a MQTT_URL to connect to");
|
|
|
|
}
|
2018-07-13 00:04:59 -06:00
|
|
|
this.mqttClient = new mqtt.MqttRpcClient(mqttUrl);
|
2018-06-27 14:46:03 -06:00
|
|
|
this.database = new Database();
|
|
|
|
}
|
|
|
|
|
|
|
|
async start() {
|
|
|
|
await this.database.connect();
|
|
|
|
await this.database.createAll();
|
|
|
|
logger.info("created database and tables");
|
2018-05-24 10:36:16 -06:00
|
|
|
|
|
|
|
this.mqttClient.start();
|
|
|
|
}
|
2018-06-27 00:59:58 -06:00
|
|
|
}
|