UNPKG

xiot-core-spec-ts

Version:
85 lines (84 loc) 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Spec_1 = require("../../typedef/constant/Spec"); const Service_1 = require("../../typedef/instance/Service"); const ServiceType_1 = require("../../typedef/definition/urn/ServiceType"); const PropertyCodec_1 = require("./PropertyCodec"); const ActionCodec_1 = require("./ActionCodec"); const EventCodec_1 = require("./EventCodec"); const DescriptionCodec_1 = require("../definition/DescriptionCodec"); class ServiceCodec { static decodeArray(array) { const list = []; if (array != null) { for (const o of array) { list.push(ServiceCodec.decode(o)); } } return list; } static decode(o) { const iid = o[Spec_1.Spec.IID]; const type = new ServiceType_1.ServiceType(o[Spec_1.Spec.TYPE]); const description = DescriptionCodec_1.DescriptionCodec.decode(o[Spec_1.Spec.DESCRIPTION]); const properties = PropertyCodec_1.PropertyCodec.decodeArray(o[Spec_1.Spec.PROPERTIES]); const actions = ActionCodec_1.ActionCodec.decodeArray(o[Spec_1.Spec.ACTIONS]); const events = EventCodec_1.EventCodec.decodeArray(o[Spec_1.Spec.EVENTS]); // if (o[Spec.X_OPTIONAL] != null) { // type._optional = o[Spec.X_OPTIONAL]; // } // // 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 Service_1.Service(iid, type, description, properties, actions, events); } static encode(service) { const o = { iid: service.iid, type: service.type.toString(), description: DescriptionCodec_1.DescriptionCodec.encode(service.description), }; if (service.properties.size > 0) { o[Spec_1.Spec.PROPERTIES] = PropertyCodec_1.PropertyCodec.encodeArray(service.properties); } if (service.actions.size > 0) { o[Spec_1.Spec.ACTIONS] = ActionCodec_1.ActionCodec.encodeArray(service.actions); } if (service.events.size > 0) { o[Spec_1.Spec.EVENTS] = EventCodec_1.EventCodec.encodeArray(service.events); } // if (service.type._optional) { // o[Spec.X_OPTIONAL] = true; // } // // if (service.type._property_addable) { // o[Spec.X_PROPERTY_ADDABLE] = true; // } // // if (service.type._action_addable) { // o[Spec.X_ACTION_ADDABLE] = true; // } // // if (service.type._event_addable) { // o[Spec.X_EVENT_ADDABLE] = true; // } return o; } static encodeArray(services) { const array = []; services.forEach((service) => { array.push(ServiceCodec.encode(service)); }); return array; } } exports.ServiceCodec = ServiceCodec;