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.
69 lines
2.1 KiB
69 lines
2.1 KiB
7 years ago
|
import { createSimpleSchema, deserialize, ModelSchema, primitive, serialize } from "serializr";
|
||
7 years ago
|
import * as requests from "../deviceRequests";
|
||
7 years ago
|
import * as common from "./common";
|
||
|
|
||
|
export const withType: ModelSchema<requests.WithType> = createSimpleSchema({
|
||
|
type: primitive(),
|
||
|
});
|
||
|
|
||
|
export const withProgram: ModelSchema<requests.WithProgram> = createSimpleSchema({
|
||
|
...withType.props,
|
||
|
programId: primitive(),
|
||
|
});
|
||
|
|
||
|
export const withSection: ModelSchema<requests.WithSection> = createSimpleSchema({
|
||
|
...withType.props,
|
||
|
sectionId: primitive(),
|
||
|
});
|
||
|
|
||
7 years ago
|
export const updateProgram: ModelSchema<requests.UpdateProgramData> = createSimpleSchema({
|
||
7 years ago
|
...withProgram.props,
|
||
7 years ago
|
data: {
|
||
|
serializer: (data) => data,
|
||
|
deserializer: (json, done) => { done(null, json); },
|
||
|
},
|
||
7 years ago
|
});
|
||
|
|
||
|
export const runSection: ModelSchema<requests.RunSectionData> = createSimpleSchema({
|
||
|
...withSection.props,
|
||
|
duration: common.duration,
|
||
|
});
|
||
|
|
||
|
export const cancelSectionRunId: ModelSchema<requests.CancelSectionRunIdData> = createSimpleSchema({
|
||
|
...withType.props,
|
||
|
runId: primitive(),
|
||
|
});
|
||
|
|
||
|
export const pauseSectionRunner: ModelSchema<requests.PauseSectionRunnerData> = createSimpleSchema({
|
||
|
...withType.props,
|
||
|
paused: primitive(),
|
||
|
});
|
||
|
|
||
|
export function getRequestSchema(request: requests.WithType): ModelSchema<any> {
|
||
|
switch (request.type as requests.RequestType) {
|
||
|
case "runProgram":
|
||
|
case "cancelProgram":
|
||
|
return withProgram;
|
||
|
case "updateProgram":
|
||
7 years ago
|
return updateProgram;
|
||
7 years ago
|
case "runSection":
|
||
|
return runSection;
|
||
|
case "cancelSection":
|
||
|
return withSection;
|
||
|
case "cancelSectionRunId":
|
||
|
return cancelSectionRunId;
|
||
|
case "pauseSectionRunner":
|
||
|
return pauseSectionRunner;
|
||
|
default:
|
||
|
throw new Error(`Cannot serialize request with type "${request.type}"`);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export function seralizeRequest(request: requests.Request): any {
|
||
|
return serialize(getRequestSchema(request), request);
|
||
|
}
|
||
|
|
||
|
export function deserializeRequest(json: any): requests.Request {
|
||
|
return deserialize(getRequestSchema(json), json);
|
||
|
}
|