xiot-core-spec-ts
Version:
XIOT Specification Codec
75 lines (74 loc) • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RuleCodec = void 0;
const Rule_1 = require("../../typedef/rule/Rule");
const RuleContentParser_1 = require("../../typedef/rule/parser/RuleContentParser");
const RuleType_1 = require("../../typedef/rule/RuleType");
class RuleCodec {
static decode(o) {
const rule = new Rule_1.Rule();
rule.id = o.id;
rule.name = o.name;
rule.description = o.description;
rule.content = RuleContentParser_1.RuleContentParser.parse(o.content);
rule.actuator = o.actuator;
rule.enabled = o.enabled;
rule.appId = o.appId;
rule.ownerId = o.ownerId;
if (o.createAt !== undefined) {
rule.createAt = new Date(o.createAt);
}
if (o.updateAt !== undefined) {
rule.updateAt = new Date(o.updateAt);
}
return rule;
}
static encode(x) {
const o = {
id: x.id,
name: x.name,
description: x.description,
content: x.content.toString()
};
if (x.type !== RuleType_1.RuleType.MANUAL) {
o.enabled = x.enabled;
}
if (x.actuator != null) {
o.actuator = x.actuator;
}
if (x.createAt !== undefined) {
console.log('x.createAt: ', (typeof x.createAt));
o.createAt = x.createAt.getTime();
}
if (x.updateAt !== undefined) {
console.log('x.updateAt: ', (typeof x.updateAt));
o.updateAt = x.updateAt.getTime();
}
if (x.appId != null) {
o.appId = x.appId;
}
if (x.ownerId != null) {
o.ownerId = x.ownerId;
}
return o;
}
static decodeArray(arr) {
const list = [];
if (arr != null) {
for (const o of arr) {
list.push(this.decode(o));
}
}
return list;
}
static encodeArray(x) {
const arr = [];
if (x === null || x === void 0 ? void 0 : x.length) {
for (const p of x) {
arr.push(this.encode(p));
}
}
return arr;
}
}
exports.RuleCodec = RuleCodec;