Browse Source

Refactors @common/sprinklers/json => schema

update-deps
Alex Mikhalev 7 years ago
parent
commit
00ba7cdf85
  1. 8
      common/sprinklers/mqtt/index.ts
  2. 56
      common/sprinklers/schema/index.ts
  3. 0
      common/sprinklers/schema/list.ts
  4. 6
      server/index.ts

8
common/sprinklers/mqtt/index.ts

@ -3,7 +3,7 @@ import { update } from "serializr";
import logger from "@common/logger"; import logger from "@common/logger";
import * as s from "@common/sprinklers"; 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"; import { checkedIndexOf } from "@common/utils";
const log = logger.child({ source: "mqtt" }); const log = logger.child({ source: "mqtt" });
@ -273,7 +273,7 @@ class MqttSection extends s.Section {
} }
updateFromJSON(json: any) { 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) { 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) { updateFromJSON(json: any) {
update(schema.sectionRunnerSchema, this, json); update(schema.sectionRunner, this, json);
} }
} }

56
common/sprinklers/json/index.ts → common/sprinklers/schema/index.ts

@ -1,13 +1,13 @@
/* tslint:disable:ordered-imports */ /* tslint:disable:ordered-imports object-literal-shorthand */
import { import {
createSimpleSchema, primitive, object, ModelSchema, PropSchema, createSimpleSchema, primitive, object, ModelSchema, PropSchema,
} from "serializr"; } from "serializr";
import list from "./list"; import list from "./list";
import * as s from ".."; import * as s from "..";
export const durationSchema: PropSchema = { export const duration: PropSchema = {
serializer: (duration: s.Duration | null) => serializer: (d: s.Duration | null) =>
duration != null ? duration.toSeconds() : null, d != null ? d.toSeconds() : null,
deserializer: (json: any, done) => { deserializer: (json: any, done) => {
if (typeof json === "number") { if (typeof json === "number") {
done(null, s.Duration.fromSeconds(json)); 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 ? serializer: (jsDate: Date | null) => jsDate != null ?
jsDate.toISOString() : null, jsDate.toISOString() : null,
deserializer: (json: any, done) => { deserializer: (json: any, done) => {
@ -32,7 +32,7 @@ export const dateSchema: PropSchema = {
}, },
}; };
export const dateOfYearSchema: ModelSchema<s.DateOfYear> = { export const dateOfYear: ModelSchema<s.DateOfYear> = {
factory: () => new s.DateOfYear(), factory: () => new s.DateOfYear(),
props: { props: {
year: primitive(), year: primitive(),
@ -41,7 +41,7 @@ export const dateOfYearSchema: ModelSchema<s.DateOfYear> = {
}, },
}; };
export const timeOfDaySchema: ModelSchema<s.TimeOfDay> = { export const timeOfDay: ModelSchema<s.TimeOfDay> = {
factory: () => new s.TimeOfDay(), factory: () => new s.TimeOfDay(),
props: { props: {
hour: primitive(), hour: primitive(),
@ -51,7 +51,7 @@ export const timeOfDaySchema: ModelSchema<s.TimeOfDay> = {
}, },
}; };
export const sectionSchema: ModelSchema<s.Section> = { export const section: ModelSchema<s.Section> = {
factory: (c) => new (c.parentContext.target as s.SprinklersDevice).sectionConstructor( factory: (c) => new (c.parentContext.target as s.SprinklersDevice).sectionConstructor(
c.parentContext.target, c.json.id), c.parentContext.target, c.json.id),
props: { props: {
@ -60,60 +60,60 @@ export const sectionSchema: ModelSchema<s.Section> = {
}, },
}; };
export const sectionRunSchema: ModelSchema<s.SectionRun> = { export const sectionRun: ModelSchema<s.SectionRun> = {
factory: (c) => new s.SectionRun(c.json.id), factory: (c) => new s.SectionRun(c.json.id),
props: { props: {
id: primitive(), id: primitive(),
section: primitive(), section: primitive(),
duration: durationSchema, duration: duration,
startTime: dateSchema, startTime: date,
endTime: dateSchema, endTime: date,
}, },
}; };
export const sectionRunnerSchema: ModelSchema<s.SectionRunner> = { export const sectionRunner: ModelSchema<s.SectionRunner> = {
factory: (c) => new (c.parentContext.target as s.SprinklersDevice).sectionRunnerConstructor( factory: (c) => new (c.parentContext.target as s.SprinklersDevice).sectionRunnerConstructor(
c.parentContext.target), c.parentContext.target),
props: { props: {
queue: list(object(sectionRunSchema)), queue: list(object(sectionRun)),
current: object(sectionRunSchema), current: object(sectionRun),
paused: primitive(), paused: primitive(),
}, },
}; };
export const scheduleSchema: ModelSchema<s.Schedule> = { export const schedule: ModelSchema<s.Schedule> = {
factory: () => new s.Schedule(), factory: () => new s.Schedule(),
props: { props: {
times: list(object(timeOfDaySchema)), times: list(object(timeOfDay)),
weekdays: list(primitive()), weekdays: list(primitive()),
from: object(dateOfYearSchema), from: object(dateOfYear),
to: object(dateOfYearSchema), to: object(dateOfYear),
}, },
}; };
export const programItemSchema: ModelSchema<s.ProgramItem> = { export const programItem: ModelSchema<s.ProgramItem> = {
factory: () => new s.ProgramItem(), factory: () => new s.ProgramItem(),
props: { props: {
section: primitive(), section: primitive(),
duration: durationSchema, duration: duration,
}, },
}; };
export const programSchema: ModelSchema<s.Program> = { export const program: ModelSchema<s.Program> = {
factory: (c) => new (c.parentContext.target as s.SprinklersDevice).programConstructor( factory: (c) => new (c.parentContext.target as s.SprinklersDevice).programConstructor(
c.parentContext.target, c.json.id), c.parentContext.target, c.json.id),
props: { props: {
name: primitive(), name: primitive(),
enabled: primitive(), enabled: primitive(),
schedule: object(scheduleSchema), schedule: object(schedule),
sequence: list(object(programItemSchema)), sequence: list(object(programItem)),
running: primitive(), running: primitive(),
}, },
}; };
export const sprinklersDeviceSchema = createSimpleSchema({ export const sprinklersDevice = createSimpleSchema({
connected: primitive(), connected: primitive(),
sections: list(object(sectionSchema)), sections: list(object(section)),
sectionRunner: object(sectionRunnerSchema), sectionRunner: object(sectionRunner),
programs: list(object(programSchema)), programs: list(object(program)),
}); });

0
common/sprinklers/json/list.ts → common/sprinklers/schema/list.ts

6
server/index.ts

@ -13,14 +13,14 @@ const mqttClient = new mqtt.MqttApiClient("mqtt://localhost:1883");
mqttClient.start(); mqttClient.start();
import * as s from "@common/sprinklers"; 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 * as ws from "@common/sprinklers/websocketData";
import { autorunAsync } from "mobx"; import { autorunAsync } from "mobx";
import { serialize } from "serializr"; import { serialize } from "serializr";
const device = mqttClient.getDevice("grinklers"); const device = mqttClient.getDevice("grinklers");
app.get("/api/grinklers", (req, res) => { app.get("/api/grinklers", (req, res) => {
const j = serialize(sprinklersDeviceSchema, device); const j = serialize(schema.sprinklersDevice, device);
res.send(j); res.send(j);
}); });
@ -62,7 +62,7 @@ async function deviceCallRequest(socket: WebSocket, data: ws.IDeviceCallRequest)
function webSocketHandler(socket: WebSocket) { function webSocketHandler(socket: WebSocket) {
const stop = autorunAsync(() => { const stop = autorunAsync(() => {
const json = serialize(sprinklersDeviceSchema, device); const json = serialize(schema.sprinklersDevice, device);
log.info({ device: json }); log.info({ device: json });
const data = { type: "deviceUpdate", name: "grinklers", data: json }; const data = { type: "deviceUpdate", name: "grinklers", data: json };
socket.send(JSON.stringify(data)); socket.send(JSON.stringify(data));

Loading…
Cancel
Save