You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.2 KiB
120 lines
3.2 KiB
7 years ago
|
/* tslint:disable:ordered-imports object-literal-shorthand */
|
||
7 years ago
|
import {
|
||
7 years ago
|
createSimpleSchema, primitive, object, ModelSchema, PropSchema,
|
||
7 years ago
|
} from "serializr";
|
||
|
import list from "./list";
|
||
7 years ago
|
import * as s from "..";
|
||
|
|
||
7 years ago
|
export const duration: PropSchema = {
|
||
|
serializer: (d: s.Duration | null) =>
|
||
|
d != null ? d.toSeconds() : null,
|
||
7 years ago
|
deserializer: (json: any, done) => {
|
||
|
if (typeof json === "number") {
|
||
|
done(null, s.Duration.fromSeconds(json));
|
||
|
} else {
|
||
|
done(new Error(`Duration expects a number, not ${json}`), undefined);
|
||
|
}
|
||
|
},
|
||
7 years ago
|
};
|
||
|
|
||
7 years ago
|
export const date: PropSchema = {
|
||
7 years ago
|
serializer: (jsDate: Date | null) => jsDate != null ?
|
||
|
jsDate.toISOString() : null,
|
||
7 years ago
|
deserializer: (json: any, done) => {
|
||
|
if (json === null) {
|
||
7 years ago
|
return done(null, null);
|
||
7 years ago
|
}
|
||
|
try {
|
||
|
done(null, new Date(json));
|
||
|
} catch (e) {
|
||
|
done(e, undefined);
|
||
|
}
|
||
|
},
|
||
7 years ago
|
};
|
||
|
|
||
7 years ago
|
export const dateOfYear: ModelSchema<s.DateOfYear> = {
|
||
7 years ago
|
factory: () => new s.DateOfYear(),
|
||
|
props: {
|
||
|
year: primitive(),
|
||
|
month: primitive(), // this only works if it is represented as a # from 0-12
|
||
|
day: primitive(),
|
||
|
},
|
||
|
};
|
||
|
|
||
7 years ago
|
export const timeOfDay: ModelSchema<s.TimeOfDay> = {
|
||
7 years ago
|
factory: () => new s.TimeOfDay(),
|
||
|
props: {
|
||
|
hour: primitive(),
|
||
|
minute: primitive(),
|
||
|
second: primitive(),
|
||
|
millisecond: primitive(),
|
||
|
},
|
||
|
};
|
||
|
|
||
7 years ago
|
export const section: ModelSchema<s.Section> = {
|
||
7 years ago
|
factory: (c) => new (c.parentContext.target as s.SprinklersDevice).sectionConstructor(
|
||
|
c.parentContext.target, c.json.id),
|
||
|
props: {
|
||
|
name: primitive(),
|
||
|
state: primitive(),
|
||
|
},
|
||
|
};
|
||
|
|
||
7 years ago
|
export const sectionRun: ModelSchema<s.SectionRun> = {
|
||
7 years ago
|
factory: (c) => new s.SectionRun(c.json.id),
|
||
|
props: {
|
||
|
id: primitive(),
|
||
|
section: primitive(),
|
||
7 years ago
|
duration: duration,
|
||
|
startTime: date,
|
||
|
endTime: date,
|
||
7 years ago
|
},
|
||
|
};
|
||
|
|
||
7 years ago
|
export const sectionRunner: ModelSchema<s.SectionRunner> = {
|
||
7 years ago
|
factory: (c) => new (c.parentContext.target as s.SprinklersDevice).sectionRunnerConstructor(
|
||
|
c.parentContext.target),
|
||
|
props: {
|
||
7 years ago
|
queue: list(object(sectionRun)),
|
||
|
current: object(sectionRun),
|
||
7 years ago
|
paused: primitive(),
|
||
|
},
|
||
|
};
|
||
|
|
||
7 years ago
|
export const schedule: ModelSchema<s.Schedule> = {
|
||
7 years ago
|
factory: () => new s.Schedule(),
|
||
|
props: {
|
||
7 years ago
|
times: list(object(timeOfDay)),
|
||
7 years ago
|
weekdays: list(primitive()),
|
||
7 years ago
|
from: object(dateOfYear),
|
||
|
to: object(dateOfYear),
|
||
7 years ago
|
},
|
||
|
};
|
||
|
|
||
7 years ago
|
export const programItem: ModelSchema<s.ProgramItem> = {
|
||
7 years ago
|
factory: () => new s.ProgramItem(),
|
||
|
props: {
|
||
|
section: primitive(),
|
||
7 years ago
|
duration: duration,
|
||
7 years ago
|
},
|
||
|
};
|
||
|
|
||
7 years ago
|
export const program: ModelSchema<s.Program> = {
|
||
7 years ago
|
factory: (c) => new (c.parentContext.target as s.SprinklersDevice).programConstructor(
|
||
|
c.parentContext.target, c.json.id),
|
||
|
props: {
|
||
|
name: primitive(),
|
||
|
enabled: primitive(),
|
||
7 years ago
|
schedule: object(schedule),
|
||
|
sequence: list(object(programItem)),
|
||
7 years ago
|
running: primitive(),
|
||
|
},
|
||
|
};
|
||
|
|
||
7 years ago
|
export const sprinklersDevice = createSimpleSchema({
|
||
7 years ago
|
connected: primitive(),
|
||
7 years ago
|
sections: list(object(section)),
|
||
|
sectionRunner: object(sectionRunner),
|
||
|
programs: list(object(program)),
|
||
7 years ago
|
});
|