This repository has been archived by the owner on Feb 7, 2022. It is now read-only.
forked from jackbeagley/mavlink-openmct
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mavlink-telemetry.js
220 lines (188 loc) · 6.9 KB
/
mavlink-telemetry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/**
* This script provides OpenMCT with the Mavlink message hierachy as well as the functions to subscribe and
* receive messages from the Websocket server.
* Given a Mavlink schema with two messages MESSAGE1 and MESSAGE2 the object tree will be populated as:
*
* ROOT
* ├--MESSAGE1
* | ├--FIELD1
* | └─-FIELD2
* └--MESSAGE2
* ├--FIELD1
* └─-FIELD2
*
* There is a composition provider for both the messages and the fields
*/
// Read in the Mavlink dictionary
import mavlinkMessagesDict from './telemetry-messages.json'
// Socket protocol depends on whether the webpage is loaded as HTTP or HTTPS
const socketProtocol = (window.location.protocol === 'https:' ? 'wss:' : 'ws:')
const echoSocketUrl = socketProtocol + '//' + window.location.hostname + ':8080/mavlink-ws'
// Provide the messages that are within the root object
var messageCompositionProvider = {
appliesTo: function (domainObject) {
return (
domainObject.identifier.namespace === "mavlink.taxonomy" &&
domainObject.type === "folder" &&
domainObject.location === "ROOT"
);
},
load: function (domainObject) {
return Promise.resolve(
mavlinkMessagesDict.telemetryGroups.map((group) => {
return {
namespace: "mavlink.taxonomy",
key: group.key,
};
})
);
},
};
// Provide the fields within each message
var fieldCompositionProvider = {
// Check whether a particular OpenMCT domain object is a Mavlink telemetry object
appliesTo: function (domainObject) {
return (
domainObject.identifier.namespace === "mavlink.taxonomy" &&
domainObject.type === "folder" &&
domainObject.location !== "ROOT"
);
},
// Get an array of domain object identifiers from a domain object (ie, the sub-objects)
load: function (domainObject) {
const identifierKeyComponents = domainObject.identifier.key.split(".");
// Get the mavlink message dictionary entry with the metadata about the
// mavlink message in question
const identifierMessage = mavlinkMessagesDict.telemetryGroups.filter(
(group) => {
return group.key === identifierKeyComponents[0];
}
)[0];
// OpenMCT is expecting a Promise so the output map is wrapped in a Promise.resolve()
return Promise.resolve(
// Return an array of objects with the field keys
identifierMessage.measurements.map((field) => {
return {
namespace: "mavlink.taxonomy",
key: field.key,
};
})
);
},
};
var objectProvider = {
get: function (identifier) {
let object;
if (identifier.key === "craft") {
object = {
identifier: identifier,
name: mavlinkMessagesDict.name,
type: "folder",
location: "ROOT",
};
} else {
const identifierKeyComponents = identifier.key.split(".");
if (identifierKeyComponents.length == 1) {
const identifierMessage = mavlinkMessagesDict.telemetryGroups.filter(
(group) => {
return group.key === identifierKeyComponents[0];
}
)[0];
object = {
identifier: identifier,
name: identifierMessage.name,
type: "folder",
location: "mavlink.taxonomy:craft",
};
} else if (identifierKeyComponents[0] === "craft") {
const identifierMessage = mavlinkMessagesDict.telemetryGroups.filter(
(group) => {
return group.key === identifierKeyComponents[1];
}
)[0];
object = {
identifier: identifier,
name: identifierMessage.name,
type: "folder",
location: "mavlink.taxonomy:craft",
};
} else {
const identifierMessage = mavlinkMessagesDict.telemetryGroups.filter(
(group) => {
return group.key === identifierKeyComponents[0];
}
)[0];
const identifierMessageField = identifierMessage.measurements.filter(
(message) => {
return message.key === identifier.key;
}
)[0];
object = {
identifier: identifier,
name: identifierMessageField.name,
type: "mavlink.telemetry",
telemetry: {
values: identifierMessageField.values,
},
location: "mavlink.taxonomy:craft." + identifierKeyComponents[0],
};
}
}
return Promise.resolve(object);
},
};
function install(openmct) {
openmct.objects.addRoot({
namespace: "mavlink.taxonomy",
key: "craft",
});
openmct.objects.addProvider("mavlink.taxonomy", objectProvider);
openmct.composition.addProvider(messageCompositionProvider);
openmct.composition.addProvider(fieldCompositionProvider);
openmct.types.addType("mavlink.telemetry", {
name: "Mavlink Telemetry Point",
description: "Telemetry coming from a Mavlink source.",
cssClass: "icon-telemetry",
});
const socket = new WebSocket(echoSocketUrl);
var subscriberCallbacks = {};
var subscribeQueue = [];
socket.onopen = () => {
subscribeQueue.forEach((subscribeObject) => {
socket.send(subscribeObject);
});
};
socket.onmessage = function (event) {
let telemetryMessage = JSON.parse(event.data);
if (subscriberCallbacks[telemetryMessage.key]) {
subscriberCallbacks[telemetryMessage.key](telemetryMessage);
}
};
var provider = {
supportsSubscribe: function (domainObject) {
return domainObject.type === "mavlink.telemetry";
},
subscribe: function (domainObject, callback) {
subscriberCallbacks[domainObject.identifier.key] = callback;
let subscribeRequest = {
action: "subscribe",
message: domainObject.identifier.key,
};
let unsubscribeRequest = {
action: "unsubscribe",
message: domainObject.identifier.key,
};
let subscribeObject = JSON.stringify(subscribeRequest);
if (socket.readyState === WebSocket.OPEN) {
socket.send(subscribeObject);
} else {
subscribeQueue.push(subscribeObject);
}
return function unsubscribe() {
socket.send(JSON.stringify(unsubscribeRequest));
};
},
};
openmct.telemetry.addProvider(provider);
}
export { install, mavlinkMessagesDict as messages };