UNPKG

xiot-core-spec-ts

Version:
72 lines (71 loc) 3.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ServiceDefinitionCodec = void 0; const ServiceDefinition_1 = require("../../typedef/definition/ServiceDefinition"); const ServiceType_1 = require("../../typedef/definition/urn/ServiceType"); const DescriptionCodec_1 = require("./DescriptionCodec"); const PropertyTypeCodec_1 = require("./type/PropertyTypeCodec"); const ActionTypeCodec_1 = require("./type/ActionTypeCodec"); const EventTypeCodec_1 = require("./type/EventTypeCodec"); const Spec_1 = require("../../typedef/constant/Spec"); class ServiceDefinitionCodec { static decodeArray(list) { const array = []; list.forEach(o => { array.push(ServiceDefinitionCodec.decode(o)); }); return array; } static decode(o) { const type = new ServiceType_1.ServiceType(o[Spec_1.Spec.TYPE]); const description = DescriptionCodec_1.DescriptionCodec.decode(o[Spec_1.Spec.DESCRIPTION]); const requiredProperties = PropertyTypeCodec_1.PropertyTypeCodec.decodeArray(o[Spec_1.Spec.REQUIRED_PROPERTIES]); const optionalProperties = PropertyTypeCodec_1.PropertyTypeCodec.decodeArray(o[Spec_1.Spec.OPTIONAL_PROPERTIES]); const requiredActions = ActionTypeCodec_1.ActionTypeCodec.decodeArray(o[Spec_1.Spec.REQUIRED_ACTIONS]); const optionalActions = ActionTypeCodec_1.ActionTypeCodec.decodeArray(o[Spec_1.Spec.OPTIONAL_ACTIONS]); const requiredEvents = EventTypeCodec_1.EventTypeCodec.decodeArray(o[Spec_1.Spec.REQUIRED_EVENTS]); const optionalEvents = EventTypeCodec_1.EventTypeCodec.decodeArray(o[Spec_1.Spec.OPTIONAL_EVENTS]); if (type.ns === 'homekit-spec') { type._property_addable = true; type._action_addable = false; type._event_addable = false; } else { type._property_addable = true; type._action_addable = true; type._event_addable = true; } return new ServiceDefinition_1.ServiceDefinition(type, description, requiredProperties, optionalProperties, requiredActions, optionalActions, requiredEvents, optionalEvents); } static encode(def) { const o = { type: def.type.toString(), description: DescriptionCodec_1.DescriptionCodec.encode(def.description) }; if (def.requiredProperties.length > 0) { o[Spec_1.Spec.REQUIRED_PROPERTIES] = PropertyTypeCodec_1.PropertyTypeCodec.encodeArray(def.requiredProperties); } if (def.optionalProperties.length > 0) { o[Spec_1.Spec.OPTIONAL_PROPERTIES] = PropertyTypeCodec_1.PropertyTypeCodec.encodeArray(def.optionalProperties); } if (def.requiredActions.length > 0) { o[Spec_1.Spec.REQUIRED_ACTIONS] = ActionTypeCodec_1.ActionTypeCodec.encodeArray(def.requiredActions); } if (def.optionalActions.length > 0) { o[Spec_1.Spec.OPTIONAL_ACTIONS] = ActionTypeCodec_1.ActionTypeCodec.encodeArray(def.optionalActions); } if (def.requiredEvents.length > 0) { o[Spec_1.Spec.REQUIRED_EVENTS] = EventTypeCodec_1.EventTypeCodec.encodeArray(def.requiredEvents); } if (def.optionalEvents.length > 0) { o[Spec_1.Spec.OPTIONAL_EVENTS] = EventTypeCodec_1.EventTypeCodec.encodeArray(def.optionalEvents); } return o; } static encodeArray(list) { return list.map(x => { return ServiceDefinitionCodec.encode(x); }); } } exports.ServiceDefinitionCodec = ServiceDefinitionCodec;