|
| 1 | +// Copyright (c) 2017 Intel Corporation. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +'use strict'; |
| 16 | + |
| 17 | +const debug = require('debug')('rclnodejs:message_translator'); |
| 18 | + |
| 19 | +/* eslint-disable max-depth */ |
| 20 | +function copyMsgObject(msg, obj) { |
| 21 | + if (typeof obj === 'object') { |
| 22 | + for (let i in obj) { |
| 23 | + if (typeof msg[i] !== 'undefined') { |
| 24 | + const type = typeof obj[i]; |
| 25 | + if (type === 'string' || type === 'number' || type === 'boolean') { |
| 26 | + // A primitive-type value |
| 27 | + msg[i] = obj[i]; |
| 28 | + } else if (Array.isArray(obj[i])) { // TODO(Kenny): deal with TypedArray |
| 29 | + // It's an array |
| 30 | + if (typeof obj[i][0] === 'object') { |
| 31 | + // It's an array of objects: converting to ROS message objects |
| 32 | + |
| 33 | + // 1. Extract the element-type first |
| 34 | + const t = new MessageTranslator(msg[i].classType.elementType); |
| 35 | + // 2. Build the array by translate every elements |
| 36 | + let msgArray = []; |
| 37 | + obj[i].forEach((o) => { |
| 38 | + msgArray.push(t.toROSMessage(o)); |
| 39 | + }); |
| 40 | + // 3. Assign |
| 41 | + msg[i].fill(msgArray); |
| 42 | + } else { |
| 43 | + // It's an array of primitive-type elements |
| 44 | + msg[i] = obj[i]; |
| 45 | + } |
| 46 | + } else { |
| 47 | + // Proceed further of this object |
| 48 | + copyMsgObject(msg[i], obj[i]); // TODO(Kenny): deal with TypedArray |
| 49 | + } |
| 50 | + } else { |
| 51 | + // Extra fields in obj (but not in msg) |
| 52 | + } |
| 53 | + } // for |
| 54 | + } |
| 55 | +} |
| 56 | +/* eslint-enable max-depth */ |
| 57 | + |
| 58 | +class MessageTranslator { |
| 59 | + constructor(typeClass) { |
| 60 | + this._typeClass = typeClass; |
| 61 | + } |
| 62 | + |
| 63 | + static toPlainObject(message) { |
| 64 | + // TODO(Kenny): deal with primitive-type array (convert to TypedArray) |
| 65 | + |
| 66 | + if (message.isROSArray) { |
| 67 | + // It's a ROS message array |
| 68 | + // Note: there won't be any JavaScript array in message |
| 69 | + let array = []; |
| 70 | + const data = message.data; |
| 71 | + data.forEach((e) => { |
| 72 | + // Translate every elements |
| 73 | + array.push(MessageTranslator.toPlainObject(e)); |
| 74 | + }); |
| 75 | + return array; |
| 76 | + // eslint-disable-next-line no-else-return |
| 77 | + } else { |
| 78 | + // It's a ROS message |
| 79 | + const def = message.classType.ROSMessageDef; |
| 80 | + let obj = {}; |
| 81 | + for (let i in def.fields) { |
| 82 | + const name = def.fields[i].name; |
| 83 | + if (def.fields[i].type.isPrimitiveType) { |
| 84 | + // Direct assignment |
| 85 | + obj[name] = message[name]; |
| 86 | + } else { |
| 87 | + // Proceed further |
| 88 | + obj[name] = MessageTranslator.toPlainObject(message[name]); |
| 89 | + } |
| 90 | + } |
| 91 | + return obj; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + toROSMessage(obj) { |
| 96 | + let msg = new this._typeClass(); |
| 97 | + const type = typeof obj; |
| 98 | + if (type === 'string' || type === 'number' || type === 'boolean') { |
| 99 | + msg.data = obj; |
| 100 | + } else if (type === 'object') { |
| 101 | + copyMsgObject(msg, obj); |
| 102 | + } |
| 103 | + return msg; |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +module.exports = { |
| 108 | + MessageTranslator: MessageTranslator |
| 109 | +}; |
0 commit comments