UNPKG

xiot-core-spec-ts

Version:
152 lines (151 loc) 5.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const PropertyOperation_1 = require("../../typedef/operation/PropertyOperation"); const PID_1 = require("../../typedef/xid/PID"); const Spec_1 = require("../../typedef/constant/Spec"); class PropertyOperationCodec { static decodePIDs(pids) { const array = []; if (pids != null) { pids.split(',').forEach(pid => { const o = new PropertyOperation_1.PropertyOperation(); o.pid = PID_1.PID.parseString(pid); array.push(o); }); } return array; } static decodePIDArray(pids) { const array = []; if (pids != null) { pids.forEach(pid => { const o = new PropertyOperation_1.PropertyOperation(); o.pid = PID_1.PID.parseString(pid); array.push(o); }); } return array; } static decodeValues(json) { const array = []; const properties = json['properties']; if (properties != null) { if (properties instanceof Array) { properties.forEach(property => { const o = new PropertyOperation_1.PropertyOperation(); o.pid = PID_1.PID.parseString(property[Spec_1.Spec.PID]); o.status = property[Spec_1.Spec.STATUS]; if (o.status == null) { o.status = 0; } if (o.status === 0) { o.value = property[Spec_1.Spec.VALUE]; } else { o.description = property[Spec_1.Spec.DESCRIPTION]; } o.message = property; array.push(o); }); } } return array; } // static decodeValuesByArray(values: Array<Object>): Array<PropertyOperation> { // const array = []; // // if (values != null) { // values.forEach(value => { // const o = new PropertyOperation(); // o.pid = PID.parseString(o[Spec.PID]); // o.status = o[Spec.STATUS]; // if (o.status === 0) { // o.value = o[Spec.VALUE]; // } else { // o.description = o[Spec.DESCRIPTION]; // } // // array.push(o); // }); // } // // return array; // } static decodeStatus(json) { const array = []; const properties = json['properties']; if (properties != null) { properties.forEach(value => { const o = new PropertyOperation_1.PropertyOperation(); o.pid = PID_1.PID.parseString(value[Spec_1.Spec.PID]); o.status = value[Spec_1.Spec.STATUS]; if (o.status !== 0) { o.description = value[Spec_1.Spec.DESCRIPTION]; } o.message = value; array.push(o); }); } return array; } // static decodeStatusByArray(values: Array<Object>): Array<PropertyOperation> { // const array = []; // // if (values != null) { // values.forEach(value => { // const o = new PropertyOperation(); // o.pid = PID.parseString(o[Spec.PID]); // o.status = o[Spec.STATUS]; // if (o.status !== 0) { // o.description = o[Spec.DESCRIPTION]; // } // // array.push(o); // }); // } // // return array; // } static encodeQueryGETtoString(list) { return list.map(p => p.pid != null ? p.pid.toString() : '').join(','); } static encodeQueryGETtoArray(list) { return list.map(p => p.pid != null ? p.pid.toString() : ''); } static encodeResultGET(list) { return list.map(p => { const object = { pid: p.pid != null ? p.pid.toString() : '' }; if (p.status === 0) { object[Spec_1.Spec.VALUE] = p.value; } else { object[Spec_1.Spec.STATUS] = p.status; object[Spec_1.Spec.DESCRIPTION] = p.description; } return object; }); } static encodeSetProperty(property) { return { pid: property.pid != null ? property.pid.toString() : '', value: property.value }; } static encodeQuerySET(list) { return list .filter(p => p.status === 0) .map(p => PropertyOperationCodec.encodeSetProperty(p)); } static encodeResultSET(list) { return list.map(p => { const object = { pid: p.pid != null ? p.pid.toString() : '', status: p.status }; if (p.status !== 0) { object[Spec_1.Spec.DESCRIPTION] = p.description; } return object; }); } } exports.PropertyOperationCodec = PropertyOperationCodec;