UNPKG

xiot-core-spec-ts

Version:
79 lines (78 loc) 3.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PropertyTemplateCodec = void 0; const PropertyTemplate_1 = require("../../typedef/template/PropertyTemplate"); const Spec_1 = require("../../typedef/constant/Spec"); const PropertyCodec_1 = require("../instance/PropertyCodec"); const PropertyType_1 = require("../../typedef/definition/urn/PropertyType"); const DescriptionCodec_1 = require("../definition/DescriptionCodec"); const DataFormat_1 = require("../../typedef/definition/property/data/DataFormat"); const Access_1 = require("../../typedef/definition/property/Access"); const PropertyDefinitionCodec_1 = require("../definition/PropertyDefinitionCodec"); const ValueListCodec_1 = require("../definition/ValueListCodec"); const ValueRangeCodec_1 = require("../definition/ValueRangeCodec"); class PropertyTemplateCodec { static decodeArray(array) { const list = []; if (array != null) { for (const o of array) { list.push(PropertyTemplateCodec.decode(o)); } } return list; } static decode(o) { const iid = o[Spec_1.Spec.IID]; const type = new PropertyType_1.PropertyType(o[Spec_1.Spec.TYPE]); const description = DescriptionCodec_1.DescriptionCodec.decode(o[Spec_1.Spec.DESCRIPTION]); const format = (0, DataFormat_1.DataFormatFromString)(o[Spec_1.Spec.FORMAT]); const access = Access_1.Access.create(o[Spec_1.Spec.ACCESS]); let unit = null; let constraintValue = null; let members = []; const required = o[Spec_1.Spec.X_REQUIRED]; const source = o[Spec_1.Spec.X_SOURCE]; if (format === DataFormat_1.DataFormat.COMBINATION) { const members = PropertyDefinitionCodec_1.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)) { constraintValue = ValueListCodec_1.ValueListCodec.decode(format, o[Spec_1.Spec.VALUE_LIST]); } if (o.hasOwnProperty(Spec_1.Spec.VALUE_RANGE)) { constraintValue = ValueRangeCodec_1.ValueRangeCodec.decode(format, o[Spec_1.Spec.VALUE_RANGE]); } } if (o.hasOwnProperty(Spec_1.Spec.UNIT)) { unit = o[Spec_1.Spec.UNIT]; } if (o.hasOwnProperty(Spec_1.Spec.MEMBERS)) { members = o[Spec_1.Spec.MEMBERS]; } const property = new PropertyTemplate_1.PropertyTemplate(iid, type, description, format, access, constraintValue, unit, members, required, source); if (o[Spec_1.Spec.DEFAULT_VALUE] != null) { property.setDefaultValue(o[Spec_1.Spec.DEFAULT_VALUE]); } return property; } static encode(property) { const o = PropertyCodec_1.PropertyCodec.encode(property); o[Spec_1.Spec.IID] = property.iid; o[Spec_1.Spec.X_REQUIRED] = property.required; if (property.source) { o[Spec_1.Spec.X_SOURCE] = property.source; } return o; } static encodeArray(properties) { const array = []; properties.forEach(property => { array.push(PropertyTemplateCodec.encode(property)); }); return array; } } exports.PropertyTemplateCodec = PropertyTemplateCodec;