kahu-signalk 0.0.8 → 0.0.9
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/package.json +1 -1
- package/plugin/connector.js +44 -38
- package/plugin/index.js +1 -1
package/package.json
CHANGED
package/plugin/connector.js
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
const fs = require(
|
|
2
|
-
const { delay } = require(
|
|
3
|
-
const { AvroClient } = require(
|
|
4
|
-
const path = require(
|
|
1
|
+
const fs = require("fs").promises;
|
|
2
|
+
const { delay } = require("./utils");
|
|
3
|
+
const { AvroClient } = require("./avroclient");
|
|
4
|
+
const path = require("path");
|
|
5
5
|
|
|
6
6
|
// Connector acts as a cooperative thread: When created, it starts an
|
|
7
7
|
// async timeout function immediately
|
|
8
8
|
|
|
9
9
|
process.on("unhandledRejection", (reason, promise) => {
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
console.error("Unhandled Rejection at:", promise);
|
|
11
|
+
console.error("Reason:", reason.stack);
|
|
12
12
|
});
|
|
13
13
|
|
|
14
14
|
class Connector {
|
|
15
|
-
constructor({status_function, routecache, plugin_dir, config}) {
|
|
15
|
+
constructor({ status_function, routecache, plugin_dir, config }) {
|
|
16
16
|
this.last_stats = null;
|
|
17
17
|
this.last_status = null;
|
|
18
18
|
this.status_function = status_function;
|
|
@@ -28,7 +28,8 @@ class Connector {
|
|
|
28
28
|
plugin_dir,
|
|
29
29
|
"data",
|
|
30
30
|
"protocol",
|
|
31
|
-
"proto_avro.json"
|
|
31
|
+
"proto_avro.json",
|
|
32
|
+
);
|
|
32
33
|
this.schema = null;
|
|
33
34
|
console.error("Connector created");
|
|
34
35
|
setTimeout(this.main.bind(this), 0);
|
|
@@ -45,7 +46,7 @@ class Connector {
|
|
|
45
46
|
this.last_stats = await this.routecache.connectionStats();
|
|
46
47
|
this.updateStatus();
|
|
47
48
|
}
|
|
48
|
-
|
|
49
|
+
|
|
49
50
|
setStatus(status) {
|
|
50
51
|
this.last_status = status;
|
|
51
52
|
this.updateStatus();
|
|
@@ -55,11 +56,13 @@ class Connector {
|
|
|
55
56
|
const status = [];
|
|
56
57
|
if (this.last_status !== null) status.push(this.last_status);
|
|
57
58
|
if (this.last_stats !== null) {
|
|
58
|
-
status.push(
|
|
59
|
+
status.push(
|
|
60
|
+
`${this.last_stats.unsent_tracks} unsent tracks totalling ${this.last_stats.unsent_datapoints} unsent datapoints`,
|
|
61
|
+
);
|
|
59
62
|
}
|
|
60
63
|
this.status_function?.(status.join(", "));
|
|
61
64
|
}
|
|
62
|
-
|
|
65
|
+
|
|
63
66
|
async read(type) {
|
|
64
67
|
console.error("Connector parsing response");
|
|
65
68
|
|
|
@@ -70,12 +73,16 @@ class Connector {
|
|
|
70
73
|
throw "Received response with wrong callid";
|
|
71
74
|
}
|
|
72
75
|
const content = response.Response;
|
|
73
|
-
|
|
74
|
-
if (content["kahu.ErrorResponseMessage"] !== undefined) {
|
|
76
|
+
|
|
77
|
+
if (content["kahu.ErrorResponseMessage"] !== undefined) {
|
|
75
78
|
throw content.Error.exception;
|
|
76
|
-
} else if (
|
|
77
|
-
|
|
78
|
-
|
|
79
|
+
} else if (type !== undefined && content[type] === undefined) {
|
|
80
|
+
throw (
|
|
81
|
+
"Received response for wrong method: expected " +
|
|
82
|
+
type +
|
|
83
|
+
" but got " +
|
|
84
|
+
Object.keys(content)[0]
|
|
85
|
+
);
|
|
79
86
|
}
|
|
80
87
|
console.error("Connector response parsed");
|
|
81
88
|
return container;
|
|
@@ -83,7 +90,7 @@ class Connector {
|
|
|
83
90
|
|
|
84
91
|
async login() {
|
|
85
92
|
while (!this.config.api_key) await delay(500);
|
|
86
|
-
|
|
93
|
+
|
|
87
94
|
console.error("Connector logging in");
|
|
88
95
|
|
|
89
96
|
await this.client.send({
|
|
@@ -94,15 +101,15 @@ class Connector {
|
|
|
94
101
|
Call: {
|
|
95
102
|
"kahu.LoginMessage": {
|
|
96
103
|
Login: {
|
|
97
|
-
apikey: this.config.api_key
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
+
apikey: this.config.api_key,
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
},
|
|
104
111
|
});
|
|
105
|
-
|
|
112
|
+
|
|
106
113
|
console.error("Send done, gonna parse response\n");
|
|
107
114
|
const response = await this.read("kahu.LoginResponseMessage");
|
|
108
115
|
console.error("Response parsed");
|
|
@@ -110,9 +117,9 @@ class Connector {
|
|
|
110
117
|
}
|
|
111
118
|
|
|
112
119
|
async sendTracks() {
|
|
113
|
-
console.error("Connector sending tracks");
|
|
114
120
|
const submit = await this.routecache.retrieve();
|
|
115
121
|
if (submit === null) return;
|
|
122
|
+
console.error("Connector sending tracks");
|
|
116
123
|
await this.client.send({
|
|
117
124
|
Message: {
|
|
118
125
|
"kahu.Call": {
|
|
@@ -120,14 +127,14 @@ class Connector {
|
|
|
120
127
|
id: ++this.callid,
|
|
121
128
|
Call: {
|
|
122
129
|
"kahu.SubmitMessage": {
|
|
123
|
-
Submit: submit
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
}
|
|
130
|
+
Submit: submit,
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
},
|
|
129
136
|
});
|
|
130
|
-
|
|
137
|
+
|
|
131
138
|
await this.read("kahu.SubmitResponseMessage");
|
|
132
139
|
await this.routecache.markAsSent(submit);
|
|
133
140
|
this.last_track_sent = new Date();
|
|
@@ -137,9 +144,9 @@ class Connector {
|
|
|
137
144
|
|
|
138
145
|
async main() {
|
|
139
146
|
try {
|
|
140
|
-
console.error("Connector running client is null: ",
|
|
147
|
+
console.error("Connector running client is null: ", this.client === null);
|
|
141
148
|
|
|
142
|
-
this.schema = await fs.readFile(this.schema_file,
|
|
149
|
+
this.schema = await fs.readFile(this.schema_file, "utf8");
|
|
143
150
|
|
|
144
151
|
this.client = new AvroClient({
|
|
145
152
|
schema: this.schema,
|
|
@@ -148,12 +155,11 @@ class Connector {
|
|
|
148
155
|
min_reconnect_time: this.config.min_reconnect_time || 100.0,
|
|
149
156
|
max_reconnect_time: this.config.max_reconnect_time || 6000.0,
|
|
150
157
|
connect_function: this.login.bind(this),
|
|
151
|
-
status_function: this.setStatus.bind(this)
|
|
158
|
+
status_function: this.setStatus.bind(this),
|
|
152
159
|
});
|
|
153
160
|
|
|
154
161
|
while (!this.cancelled) {
|
|
155
162
|
try {
|
|
156
|
-
console.error("Connector connecting...");
|
|
157
163
|
await this.client.ensureConnection();
|
|
158
164
|
if (this.cancelled) break;
|
|
159
165
|
await this.sendTracks();
|
|
@@ -170,6 +176,6 @@ class Connector {
|
|
|
170
176
|
console.error(e.stack);
|
|
171
177
|
}
|
|
172
178
|
}
|
|
173
|
-
}
|
|
179
|
+
}
|
|
174
180
|
|
|
175
181
|
module.exports = { Connector };
|
package/plugin/index.js
CHANGED