diff --git a/common/sprinklers/mqtt/index.ts b/common/sprinklers/mqtt/index.ts index c2a1de9..5307759 100644 --- a/common/sprinklers/mqtt/index.ts +++ b/common/sprinklers/mqtt/index.ts @@ -3,7 +3,7 @@ import { update } from "serializr"; import logger from "@common/logger"; import * as s from "@common/sprinklers"; -import * as schema from "@common/sprinklers/json"; +import * as schema from "@common/sprinklers/schema"; import { checkedIndexOf } from "@common/utils"; const log = logger.child({ source: "mqtt" }); @@ -273,7 +273,7 @@ class MqttSection extends s.Section { } updateFromJSON(json: any) { - update(schema.sectionSchema, this, json); + update(schema.section, this, json); } } @@ -287,7 +287,7 @@ class MqttProgram extends s.Program { } updateFromJSON(json: any) { - update(schema.programSchema, this, json); + update(schema.program, this, json); } } @@ -297,6 +297,6 @@ class MqttSectionRunner extends s.SectionRunner { } updateFromJSON(json: any) { - update(schema.sectionRunnerSchema, this, json); + update(schema.sectionRunner, this, json); } } diff --git a/common/sprinklers/json/index.ts b/common/sprinklers/schema/index.ts similarity index 61% rename from common/sprinklers/json/index.ts rename to common/sprinklers/schema/index.ts index 07efb55..5a90acc 100644 --- a/common/sprinklers/json/index.ts +++ b/common/sprinklers/schema/index.ts @@ -1,13 +1,13 @@ -/* tslint:disable:ordered-imports */ +/* tslint:disable:ordered-imports object-literal-shorthand */ import { createSimpleSchema, primitive, object, ModelSchema, PropSchema, } from "serializr"; import list from "./list"; import * as s from ".."; -export const durationSchema: PropSchema = { - serializer: (duration: s.Duration | null) => - duration != null ? duration.toSeconds() : null, +export const duration: PropSchema = { + serializer: (d: s.Duration | null) => + d != null ? d.toSeconds() : null, deserializer: (json: any, done) => { if (typeof json === "number") { done(null, s.Duration.fromSeconds(json)); @@ -17,7 +17,7 @@ export const durationSchema: PropSchema = { }, }; -export const dateSchema: PropSchema = { +export const date: PropSchema = { serializer: (jsDate: Date | null) => jsDate != null ? jsDate.toISOString() : null, deserializer: (json: any, done) => { @@ -32,7 +32,7 @@ export const dateSchema: PropSchema = { }, }; -export const dateOfYearSchema: ModelSchema = { +export const dateOfYear: ModelSchema = { factory: () => new s.DateOfYear(), props: { year: primitive(), @@ -41,7 +41,7 @@ export const dateOfYearSchema: ModelSchema = { }, }; -export const timeOfDaySchema: ModelSchema = { +export const timeOfDay: ModelSchema = { factory: () => new s.TimeOfDay(), props: { hour: primitive(), @@ -51,7 +51,7 @@ export const timeOfDaySchema: ModelSchema = { }, }; -export const sectionSchema: ModelSchema = { +export const section: ModelSchema = { factory: (c) => new (c.parentContext.target as s.SprinklersDevice).sectionConstructor( c.parentContext.target, c.json.id), props: { @@ -60,60 +60,60 @@ export const sectionSchema: ModelSchema = { }, }; -export const sectionRunSchema: ModelSchema = { +export const sectionRun: ModelSchema = { factory: (c) => new s.SectionRun(c.json.id), props: { id: primitive(), section: primitive(), - duration: durationSchema, - startTime: dateSchema, - endTime: dateSchema, + duration: duration, + startTime: date, + endTime: date, }, }; -export const sectionRunnerSchema: ModelSchema = { +export const sectionRunner: ModelSchema = { factory: (c) => new (c.parentContext.target as s.SprinklersDevice).sectionRunnerConstructor( c.parentContext.target), props: { - queue: list(object(sectionRunSchema)), - current: object(sectionRunSchema), + queue: list(object(sectionRun)), + current: object(sectionRun), paused: primitive(), }, }; -export const scheduleSchema: ModelSchema = { +export const schedule: ModelSchema = { factory: () => new s.Schedule(), props: { - times: list(object(timeOfDaySchema)), + times: list(object(timeOfDay)), weekdays: list(primitive()), - from: object(dateOfYearSchema), - to: object(dateOfYearSchema), + from: object(dateOfYear), + to: object(dateOfYear), }, }; -export const programItemSchema: ModelSchema = { +export const programItem: ModelSchema = { factory: () => new s.ProgramItem(), props: { section: primitive(), - duration: durationSchema, + duration: duration, }, }; -export const programSchema: ModelSchema = { +export const program: ModelSchema = { factory: (c) => new (c.parentContext.target as s.SprinklersDevice).programConstructor( c.parentContext.target, c.json.id), props: { name: primitive(), enabled: primitive(), - schedule: object(scheduleSchema), - sequence: list(object(programItemSchema)), + schedule: object(schedule), + sequence: list(object(programItem)), running: primitive(), }, }; -export const sprinklersDeviceSchema = createSimpleSchema({ +export const sprinklersDevice = createSimpleSchema({ connected: primitive(), - sections: list(object(sectionSchema)), - sectionRunner: object(sectionRunnerSchema), - programs: list(object(programSchema)), + sections: list(object(section)), + sectionRunner: object(sectionRunner), + programs: list(object(program)), }); diff --git a/common/sprinklers/json/list.ts b/common/sprinklers/schema/list.ts similarity index 100% rename from common/sprinklers/json/list.ts rename to common/sprinklers/schema/list.ts diff --git a/server/index.ts b/server/index.ts index fb803df..f6b6e3a 100644 --- a/server/index.ts +++ b/server/index.ts @@ -13,14 +13,14 @@ const mqttClient = new mqtt.MqttApiClient("mqtt://localhost:1883"); mqttClient.start(); import * as s from "@common/sprinklers"; -import { sprinklersDeviceSchema } from "@common/sprinklers/json"; +import * as schema from "@common/sprinklers/schema"; import * as ws from "@common/sprinklers/websocketData"; import { autorunAsync } from "mobx"; import { serialize } from "serializr"; const device = mqttClient.getDevice("grinklers"); app.get("/api/grinklers", (req, res) => { - const j = serialize(sprinklersDeviceSchema, device); + const j = serialize(schema.sprinklersDevice, device); res.send(j); }); @@ -62,7 +62,7 @@ async function deviceCallRequest(socket: WebSocket, data: ws.IDeviceCallRequest) function webSocketHandler(socket: WebSocket) { const stop = autorunAsync(() => { - const json = serialize(sprinklersDeviceSchema, device); + const json = serialize(schema.sprinklersDevice, device); log.info({ device: json }); const data = { type: "deviceUpdate", name: "grinklers", data: json }; socket.send(JSON.stringify(data));