UNPKG

xiot-core-spec-ts

Version:
91 lines (90 loc) 4.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Spec_1 = require("../../typedef/constant/Spec"); const ServiceDefinition_1 = require("../../typedef/definition/ServiceDefinition"); const ServiceType_1 = require("../../typedef/definition/urn/ServiceType"); const ActionTypeCodec_1 = require("./type/ActionTypeCodec"); const PropertyTypeCodec_1 = require("./type/PropertyTypeCodec"); const EventTypeCodec_1 = require("./type/EventTypeCodec"); const DescriptionCodec_1 = require("./DescriptionCodec"); 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; } // if (o[Spec.X_PROPERTY_ADDABLE] != null) { // type._property_addable = o[Spec.X_PROPERTY_ADDABLE]; // } // // if (o[Spec.X_ACTION_ADDABLE] != null) { // type._action_addable = o[Spec.X_ACTION_ADDABLE]; // } // // if (o[Spec.X_EVENT_ADDABLE] != null) { // type._event_addable = o[Spec.X_EVENT_ADDABLE]; // } 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); } // if (def.type._property_addable) { // o[Spec.X_PROPERTY_ADDABLE] = true; // } // // if (def.type._action_addable) { // o[Spec.X_ACTION_ADDABLE] = true; // } // // if (def.type._event_addable) { // o[Spec.X_EVENT_ADDABLE] = true; // } return o; } static encodeArray(list) { return list.map(x => ServiceDefinitionCodec.encode(x)); } } exports.ServiceDefinitionCodec = ServiceDefinitionCodec;