xiot-core-spec-ts
Version:
XIOT Specification Codec
96 lines (95 loc) • 4.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PropertyDefinitionCodec = void 0;
const Spec_1 = require("../../typedef/constant/Spec");
const PropertyType_1 = require("../../typedef/definition/urn/PropertyType");
const PropertyDefinition_1 = require("../../typedef/definition/PropertyDefinition");
const DescriptionCodec_1 = require("./DescriptionCodec");
const DataFormat_1 = require("../../typedef/definition/property/data/DataFormat");
const Access_1 = require("../../typedef/definition/property/Access");
const ValueListCodec_1 = require("./ValueListCodec");
const ValueRangeCodec_1 = require("./ValueRangeCodec");
const ValueList_1 = require("../../typedef/definition/property/ValueList");
const ValueRange_1 = require("../../typedef/definition/property/ValueRange");
const PropertyTypeCodec_1 = require("./type/PropertyTypeCodec");
class PropertyDefinitionCodec {
static decodeMembers(array) {
const list = [];
if (array === null || array === void 0 ? void 0 : array.length) {
for (const o of array) {
// if (typeof o === 'number') {
// list.push(o);
// }
if (typeof o === 'string') {
list.push(new PropertyType_1.PropertyType(o));
}
}
}
return list;
}
static decodeArray(list) {
const array = [];
list.forEach(json => {
array.push(PropertyDefinitionCodec.decode(json));
});
return array;
}
static decode(o) {
const type = new PropertyType_1.PropertyType(o[Spec_1.Spec.TYPE]);
const description = DescriptionCodec_1.DescriptionCodec.decode(o[Spec_1.Spec.DESCRIPTION]);
const def = new PropertyDefinition_1.PropertyDefinition(type, description);
def.format = (0, DataFormat_1.DataFormatFromString)(o[Spec_1.Spec.FORMAT]);
def.access = Access_1.Access.create(o[Spec_1.Spec.ACCESS]);
def.unit = o[Spec_1.Spec.UNIT];
if (def.format === DataFormat_1.DataFormat.COMBINATION) {
def.members = PropertyDefinitionCodec.decodeMembers(o[Spec_1.Spec.MEMBERS]);
}
else {
if (o.hasOwnProperty(Spec_1.Spec.VALUE_LIST) && o.hasOwnProperty(Spec_1.Spec.VALUE_RANGE)) {
throw new Error('value-list & value-range both exist!');
}
if (o.hasOwnProperty(Spec_1.Spec.VALUE_LIST)) {
def.constraintValue = ValueListCodec_1.ValueListCodec.decode(def.format, o[Spec_1.Spec.VALUE_LIST]);
}
if (o.hasOwnProperty(Spec_1.Spec.VALUE_RANGE)) {
def.constraintValue = ValueRangeCodec_1.ValueRangeCodec.decode(def.format, o[Spec_1.Spec.VALUE_RANGE]);
}
}
return def;
}
static encode(def) {
var _a;
const o = {
type: def.type.toString(),
description: DescriptionCodec_1.DescriptionCodec.encode(def.description),
format: (0, DataFormat_1.DataFormatToString)(def.format),
access: def.access.toList()
};
if (def.constraintValue != null) {
if (def.constraintValue instanceof ValueList_1.ValueList) {
o[Spec_1.Spec.VALUE_LIST] = ValueListCodec_1.ValueListCodec.encode(def.constraintValue);
}
if (def.constraintValue instanceof ValueRange_1.ValueRange) {
o[Spec_1.Spec.VALUE_RANGE] = ValueRangeCodec_1.ValueRangeCodec.encode(def.constraintValue);
}
}
if (def.unit != null) {
o[Spec_1.Spec.UNIT] = def.unit;
}
if (((_a = def.members) === null || _a === void 0 ? void 0 : _a.length) > 0) {
if (def.members[0] instanceof PropertyType_1.PropertyType) {
o[Spec_1.Spec.MEMBERS] = PropertyTypeCodec_1.PropertyTypeCodec.encodeArray(def.members);
}
else {
o[Spec_1.Spec.MEMBERS] = def.members;
}
}
return o;
}
static encodeArray(list) {
return list.map(x => {
return PropertyDefinitionCodec.encode(x);
});
}
}
exports.PropertyDefinitionCodec = PropertyDefinitionCodec;