Source: lib/subscription.js

  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. 'use strict';
  15. const rclnodejs = require('bindings')('rclnodejs');
  16. const Entity = require('./entity.js');
  17. const debug = require('debug')('rclnodejs:subscription');
  18. /**
  19. * @class - Class representing a Subscription in ROS
  20. * @hideconstructor
  21. */
  22. class Subscription extends Entity {
  23. constructor(handle, typeClass, topic, options, callback) {
  24. super(handle, typeClass, options);
  25. this._topic = topic;
  26. this._callback = callback;
  27. }
  28. processResponse(msg) {
  29. this._message = new this._typeClass();
  30. this._message.deserialize(msg);
  31. debug(`Message of topic ${this._topic} received.`);
  32. this._callback(this._message.toPlainObject(this.typedArrayEnabled));
  33. }
  34. static createSubscription(nodeHandle, typeClass, topic, options, callback) {
  35. let type = typeClass.type();
  36. let handle = rclnodejs.createSubscription(
  37. nodeHandle, type.pkgName, type.subFolder, type.interfaceName, topic, options.qos);
  38. return new Subscription(handle, typeClass, topic, options, callback);
  39. }
  40. /**
  41. * @type {string}
  42. */
  43. get topic() {
  44. return this._topic;
  45. }
  46. };
  47. module.exports = Subscription;