diff --git a/app/index.tsx b/app/index.tsx index 2b8e6cd..2ec112f 100644 --- a/app/index.tsx +++ b/app/index.tsx @@ -4,6 +4,9 @@ import { AppContainer } from "react-hot-loader"; import App from "@app/components/App"; import { ProvideState, State } from "@app/state"; +import log, { setLogger } from "@common/log"; + +setLogger(log.child({ name: "sprinklers3/app" })); const state = new State(); state.start(); diff --git a/app/state/index.ts b/app/state/index.ts index ad62236..77e9893 100644 --- a/app/state/index.ts +++ b/app/state/index.ts @@ -11,7 +11,7 @@ export interface IState { } export class State implements IState { - sprinklersApi: ISprinklersApi = new MqttApiClient(); + sprinklersApi: ISprinklersApi = new MqttApiClient(`ws://${location.hostname}:1884`); uiStore = new UiStore(); constructor() { diff --git a/common/logger.ts b/common/logger.ts new file mode 100644 index 0000000..9c1deb1 --- /dev/null +++ b/common/logger.ts @@ -0,0 +1,9 @@ +import * as pino from "pino"; + +let logger: pino.Logger = pino(); + +export function setLogger(newLogger: pino.Logger) { + logger = newLogger; +} + +export default logger; diff --git a/common/mqtt.ts b/common/mqtt.ts index a7d597b..1404082 100644 --- a/common/mqtt.ts +++ b/common/mqtt.ts @@ -11,16 +11,17 @@ import { SectionRunner, SprinklersDevice, TimeOfDay, -} from "@common/sprinklers"; -import { checkedIndexOf } from "@common/utils"; +} from "./sprinklers"; +import { checkedIndexOf } from "./utils"; export class MqttApiClient implements ISprinklersApi { + readonly mqttUri: string; client: mqtt.Client; connected: boolean; devices: { [prefix: string]: MqttSprinklersDevice } = {}; - constructor() { - // (this.client as any).trace = (m => console.log(m)); + constructor(mqttUri: string) { + this.mqttUri = mqttUri; } private static newClientId() { @@ -30,7 +31,7 @@ export class MqttApiClient implements ISprinklersApi { start() { const clientId = MqttApiClient.newClientId(); console.log("connecting to mqtt with client id %s", clientId); - this.client = mqtt.connect(`ws://${location.hostname}:1884`, { + this.client = mqtt.connect(this.mqttUri, { clientId, }); this.client.on("message", this.onMessageArrived.bind(this)); diff --git a/server/app/serveApp.ts b/server/app/serveApp.ts index 88398ba..d5a07c1 100644 --- a/server/app/serveApp.ts +++ b/server/app/serveApp.ts @@ -3,7 +3,7 @@ import * as webpack from "webpack"; import * as webpackMiddleware from "webpack-dev-middleware"; import * as webpackHotMiddleware from "webpack-hot-middleware"; -import logger from "../log"; +import logger from "../../common/logger"; const log = logger.child({ source: "webpack" }); /* tslint:disable-next-line:no-var-requires */ diff --git a/server/index.ts b/server/index.ts index 8cd7954..ad239b0 100644 --- a/server/index.ts +++ b/server/index.ts @@ -1,16 +1,14 @@ import { Server } from "http"; +import log, { setLogger } from "../common/logger"; import app from "./app"; -import log from "./log"; -import * as mqtt from "mqtt"; +setLogger(log.child({ name: "sprinklers3/server" })); -const mqttClient = mqtt.connect("mqtt://localhost:1882"); -mqttClient.on("connect", () => { - log.info("mqtt connected"); -}); -mqttClient.on("error", (err) => { - log.error("mqtt error: ", err); -}); +import * as mqtt from "../common/mqtt"; + +const mqttClient = new mqtt.MqttApiClient("mqtt://localhost:1882"); + +mqttClient.start(); const server = new Server(app); diff --git a/server/log.ts b/server/log.ts deleted file mode 100644 index 233d383..0000000 --- a/server/log.ts +++ /dev/null @@ -1,5 +0,0 @@ -import * as pino from "pino"; - -const log = pino(); - -export default log; diff --git a/tslint.json b/tslint.json index 389bdbf..1c09b44 100644 --- a/tslint.json +++ b/tslint.json @@ -6,7 +6,7 @@ "jsRules": {}, "rules": { "no-console": [ - false + true ], "max-classes-per-file": [ false