xiot-core-spec-ts
Version:
XIOT Specification Codec
86 lines (85 loc) • 3.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const PropertyDefinition_1 = require("../../typedef/definition/PropertyDefinition");
const Spec_1 = require("../../typedef/constant/Spec");
const PropertyType_1 = require("../../typedef/definition/urn/PropertyType");
const DataFormat_1 = require("../../typedef/definition/property/data/DataFormat");
const Access_1 = require("../../typedef/definition/property/Access");
const Unit_1 = require("../../typedef/definition/property/Unit");
const ValueList_1 = require("../../typedef/definition/property/ValueList");
const ValueRange_1 = require("../../typedef/definition/property/ValueRange");
const ValueListCodec_1 = require("./ValueListCodec");
const ValueRangeCodec_1 = require("./ValueRangeCodec");
const DescriptionCodec_1 = require("./DescriptionCodec");
class PropertyDefinitionCodec {
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 = DataFormat_1.DataFormatFromString(o[Spec_1.Spec.FORMAT]);
def.access = Access_1.Access.create(o[Spec_1.Spec.ACCESS]);
def.unit = Unit_1.UnitFromString(o[Spec_1.Spec.UNIT]);
// if (o.hasOwnProperty(Spec.VALUE_LIST) && o.hasOwnProperty(Spec.VALUE_RANGE)) {
// throw new Error('value-list & value-range both exist!');
// }
// if (o.hasOwnProperty(Spec.VALUE_LIST)) {
// def.constraintValue = ValueListCodec.decode(def.format, o[Spec.VALUE_LIST]);
// }
//
// if (o.hasOwnProperty(Spec.VALUE_RANGE)) {
// def.constraintValue = ValueRangeCodec.decode(def.format, o[Spec.VALUE_RANGE]);
// }
if (PropertyDefinitionCodec.hasValueList(o)) {
def.constraintValue = ValueListCodec_1.ValueListCodec.decode(def.format, o[Spec_1.Spec.VALUE_LIST]);
}
if (PropertyDefinitionCodec.hasValueRange(o)) {
def.constraintValue = ValueRangeCodec_1.ValueRangeCodec.decode(def.format, o[Spec_1.Spec.VALUE_RANGE]);
}
return def;
}
static encode(def) {
const o = {
type: def.type.toString(),
description: DescriptionCodec_1.DescriptionCodec.encode(def.description),
format: 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] = def.constraintValue.toJsonArray();
}
if (def.constraintValue instanceof ValueRange_1.ValueRange) {
o[Spec_1.Spec.VALUE_RANGE] = def.constraintValue.toJsonArray();
}
}
if (def.unit !== Unit_1.Unit.NONE) {
o[Spec_1.Spec.UNIT] = Unit_1.UnitToString(def.unit);
}
return o;
}
static encodeArray(list) {
return list.map(x => PropertyDefinitionCodec.encode(x));
}
static hasValueList(o) {
const list = o[Spec_1.Spec.VALUE_LIST];
if (list == null) {
return false;
}
return list.length !== 0;
}
static hasValueRange(o) {
const list = o[Spec_1.Spec.VALUE_RANGE];
if (list == null) {
return false;
}
return list.length !== 0;
}
}
exports.PropertyDefinitionCodec = PropertyDefinitionCodec;