pubo-node 1.0.156 → 1.0.158

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/es/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  export { JsonStorage } from './storage/json';
2
2
  export { FtpClient, FtpClientPool } from './ftp-client';
3
3
  export { createRpcClient, GrpcList } from './grpc';
4
- export { PuboFileSystem } from './file-system';
5
- export { SIGKILL, isProcessDied, getProcessName, getProcessTree, getProcessByPpid, getProcessCpuUseByPid, getProcessCommandByPid, heartbeat } from './child-process';
4
+ export { SIGKILL, isProcessDied, getProcessName, getProcessTree, getProcessByPpid, getProcessCpuUseByPid, getProcessCommandByPid, heartbeat, } from './child-process';
6
5
  export { isPortAvailable } from './utils';
7
6
  export { getWifiName, getNetworks } from './utils/network';
8
- export { RosTopicManager, RosTopic } from './ros/topic';
7
+ export { RosTopicManager, RosTopic } from './ros/topic';
8
+ export { PuboFileSystem } from './file-system';
package/es/ros/topic.js CHANGED
@@ -2,122 +2,79 @@ import { exec, spawn } from 'child_process';
2
2
  import { Emitter, WatchDog, StringSplit, sleep } from 'pubo-utils';
3
3
  import * as YAML from 'yaml';
4
4
  import { SIGKILL } from '../child-process';
5
- function _empty() {}
6
- function _awaitIgnored(value, direct) {
7
- if (!direct) {
8
- return value && value.then ? value.then(_empty) : Promise.resolve();
9
- }
10
- }
11
- function _await(value, then, direct) {
12
- if (direct) {
13
- return then ? then(value) : value;
14
- }
15
- if (!value || !value.then) {
16
- value = Promise.resolve(value);
17
- }
18
- return then ? value.then(then) : value;
19
- }
20
- export var RosTopic = /*#__PURE__*/function () {
21
- function RosTopic(topic, messageType) {
22
- this.topic = void 0;
23
- this.messageType = void 0;
24
- this.emitter = new Emitter();
25
- this.subscribed = false;
26
- this.dog = new WatchDog({
27
- limit: 10,
28
- onTimeout: this.onTimeout.bind(this)
29
- });
30
- this.strSplit = new StringSplit('---');
31
- this.subscribeChildProcess = void 0;
32
- this.topic = topic;
33
- this.messageType = messageType;
34
- this.subscribed = false;
35
- this.emitter = new Emitter();
36
- }
37
- var _proto = RosTopic.prototype;
38
- _proto.onTimeout = function onTimeout() {
39
- try {
40
- var _this = this;
41
- if (!_this.subscribed) {
42
- return _await();
43
- }
44
- return _await(_this.unsubscribe(), function () {
45
- return _await(sleep(1000), function () {
46
- return _awaitIgnored(_this.subscribe());
47
- });
48
- });
49
- } catch (e) {
50
- return Promise.reject(e);
5
+ export class RosTopic {
6
+ topic;
7
+ messageType;
8
+ emitter = new Emitter();
9
+ subscribed = false;
10
+ dog = new WatchDog({ limit: 10, onTimeout: this.onTimeout.bind(this) });
11
+ strSplit = new StringSplit('---');
12
+ subscribeChildProcess;
13
+ constructor(topic, messageType) {
14
+ this.topic = topic;
15
+ this.messageType = messageType;
16
+ this.subscribed = false;
17
+ this.emitter = new Emitter();
51
18
  }
52
- };
53
- _proto.onData = function onData(data) {
54
- var tmp = this.strSplit.split(data.toString()).slice(-1)[0];
55
- if (!tmp) {
56
- return;
19
+ async onTimeout() {
20
+ if (!this.subscribed) {
21
+ return;
22
+ }
23
+ await this.unsubscribe();
24
+ await sleep(1000);
25
+ await this.subscribe();
57
26
  }
58
- this.dog.feed();
59
- var res = YAML.parse(tmp);
60
- this.emitter.emit('message', res);
61
- };
62
- _proto.subscribe = function subscribe() {
63
- try {
64
- var _this2 = this;
65
- if (_this2.subscribeChildProcess) {
66
- return _await();
67
- }
68
- _this2.subscribed = true;
69
- _this2.dog.init();
70
- _this2.subscribeChildProcess = spawn("rostopic", ['echo', _this2.topic]);
71
- _this2.subscribeChildProcess.stdout.on('data', _this2.onData.bind(_this2));
72
- _this2.subscribeChildProcess.stderr.on('data', function (buf) {
73
- return console.log(buf.toString());
74
- });
75
- return _await();
76
- } catch (e) {
77
- return Promise.reject(e);
27
+ onData(data) {
28
+ const tmp = this.strSplit.split(data.toString()).slice(-1)[0];
29
+ if (!tmp) {
30
+ return;
31
+ }
32
+ this.dog.feed();
33
+ const res = YAML.parse(tmp);
34
+ this.emitter.emit('message', res);
78
35
  }
79
- };
80
- _proto.unsubscribe = function unsubscribe() {
81
- try {
82
- var _this3 = this;
83
- if (!_this3.subscribeChildProcess) {
84
- return _await();
85
- }
86
- _this3.dog.stop();
87
- _this3.subscribed = false;
88
- return _await(SIGKILL(_this3.subscribeChildProcess.pid), function () {
89
- _this3.subscribeChildProcess = null;
90
- });
91
- } catch (e) {
92
- return Promise.reject(e);
36
+ async subscribe() {
37
+ if (this.subscribeChildProcess) {
38
+ return;
39
+ }
40
+ this.subscribed = true;
41
+ this.dog.init();
42
+ this.subscribeChildProcess = spawn(`rostopic`, ['echo', this.topic]);
43
+ this.subscribeChildProcess.stdout.on('data', this.onData.bind(this));
44
+ this.subscribeChildProcess.stderr.on('data', (buf) => console.log(buf.toString()));
93
45
  }
94
- };
95
- _proto.publish = function publish(payload) {
96
- var _this4 = this;
97
- var data = YAML.stringify(payload);
98
- return new Promise(function (resolve, reject) {
99
- exec("rostopic pub -1 " + _this4.topic + " " + _this4.messageType + " \"" + data + "\"", function (err, stdout) {
100
- if (err) {
101
- reject(err);
102
- } else {
103
- resolve(stdout);
46
+ async unsubscribe() {
47
+ if (!this.subscribeChildProcess) {
48
+ return;
104
49
  }
105
- });
106
- });
107
- };
108
- return RosTopic;
109
- }();
110
- export var RosTopicManager = {
111
- cache: [],
112
- getTopic: function getTopic(topic, messageType) {
113
- var tmp = this.cache.find(function (item) {
114
- return item.topic === topic;
115
- });
116
- if (tmp) {
117
- return tmp;
50
+ this.dog.stop();
51
+ this.subscribed = false;
52
+ await SIGKILL(this.subscribeChildProcess.pid);
53
+ this.subscribeChildProcess = null;
54
+ }
55
+ publish(payload) {
56
+ const data = YAML.stringify(payload);
57
+ return new Promise((resolve, reject) => {
58
+ exec(`rostopic pub -1 ${this.topic} ${this.messageType} "${data}"`, (err, stdout) => {
59
+ if (err) {
60
+ reject(err);
61
+ }
62
+ else {
63
+ resolve(stdout);
64
+ }
65
+ });
66
+ });
118
67
  }
119
- var instance = new RosTopic(topic, messageType);
120
- this.cache.push(instance);
121
- return instance;
122
- }
123
- };
68
+ }
69
+ export const RosTopicManager = {
70
+ cache: [],
71
+ getTopic: function (topic, messageType) {
72
+ const tmp = this.cache.find((item) => item.topic === topic);
73
+ if (tmp) {
74
+ return tmp;
75
+ }
76
+ const instance = new RosTopic(topic, messageType);
77
+ this.cache.push(instance);
78
+ return instance;
79
+ },
80
+ };