xiot-core-spec-ts
Version:
XIOT Specification Codec
99 lines (98 loc) • 4.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.PropertyCodec = void 0;
const Spec_1 = require("../../typedef/constant/Spec");
const PropertyDefinitionCodec_1 = require("../definition/PropertyDefinitionCodec");
const Property_1 = require("../../typedef/instance/Property");
const DescriptionCodec_1 = require("../definition/DescriptionCodec");
const DataFormat_1 = require("../../typedef/definition/property/data/DataFormat");
const ValueList_1 = require("../../typedef/definition/property/ValueList");
const ValueListCodec_1 = require("../definition/ValueListCodec");
const ValueRange_1 = require("../../typedef/definition/property/ValueRange");
const ValueRangeCodec_1 = require("../definition/ValueRangeCodec");
const PropertyType_1 = require("../../typedef/definition/urn/PropertyType");
const Access_1 = require("../../typedef/definition/property/Access");
class PropertyCodec {
static decodeArray(array) {
const list = [];
if (array != null) {
for (const o of array) {
list.push(PropertyCodec.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 = [];
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 Property_1.Property(iid, type, description, format, access, constraintValue, unit, members);
// property.setDefaultValue(o[Spec.DEFAULT_VALUE]);
// const property: Property = new Property(o[Spec.IID], PropertyDefinitionCodec.decode(o));
if (o[Spec_1.Spec.DEFAULT_VALUE] != null) {
property.setDefaultValue(o[Spec_1.Spec.DEFAULT_VALUE]);
}
return property;
}
static encode(property) {
var _a;
const object = {
iid: property.iid,
type: property.type.toString(),
description: DescriptionCodec_1.DescriptionCodec.encode(property.description),
format: (0, DataFormat_1.DataFormatToString)(property.format),
access: property.access.toList()
};
if (property.constraintValue != null) {
if (property.constraintValue instanceof ValueList_1.ValueList) {
object[Spec_1.Spec.VALUE_LIST] = ValueListCodec_1.ValueListCodec.encode(property.constraintValue);
}
if (property.constraintValue instanceof ValueRange_1.ValueRange) {
object[Spec_1.Spec.VALUE_RANGE] = ValueRangeCodec_1.ValueRangeCodec.encode(property.constraintValue);
}
}
if (property.unit != null) {
object[Spec_1.Spec.UNIT] = property.unit;
}
if (((_a = property.members) === null || _a === void 0 ? void 0 : _a.length) > 0) {
object[Spec_1.Spec.MEMBERS] = property.members;
}
if (property.value.defaultValue != null) {
object[Spec_1.Spec.DEFAULT_VALUE] = property.value.defaultValue.rawValue();
}
return object;
}
static encodeArray(properties) {
const array = [];
properties.forEach(property => {
array.push(PropertyCodec.encode(property));
});
return array;
}
}
exports.PropertyCodec = PropertyCodec;