forked from velocityzen/node-upnp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response.js
225 lines (193 loc) · 4.9 KB
/
response.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
221
222
223
224
225
const { URL } = require('url');
const parser = require('fast-xml-parser');
const he = require('he');
const eventsValues = require('./events');
function absoluteUrl(baseUrl, url) {
return new URL(url, baseUrl).toString();
}
function getIcons(iconList, url) {
if (!iconList) {
return [];
}
return Array.from(iconList.icon).map(icon => ({
...icon,
url: absoluteUrl(url, icon.url)
}))
}
function getServices(serviceList, url) {
if (!serviceList) {
return {};
}
return Array.from(serviceList.service).reduce((a, { serviceType, serviceId, SCPDURL, controlURL, eventSubURL }) => {
a[serviceId] = {
serviceType,
SCPDURL: absoluteUrl(url, SCPDURL),
controlURL: absoluteUrl(url, controlURL),
eventSubURL: absoluteUrl(url, eventSubURL)
}
return a;
}, {});
}
function parseDeviceDescription(xmlString, url) {
url = new URL(url);
const obj = parser.parse(xmlString, { parseTrueNumberOnly: true });
const {
deviceType,
friendlyName,
manufacturer,
manufacturerURL,
modelName,
modelNumber,
modelDescription,
UDN,
iconList,
serviceList
} = obj.root.device;
return {
deviceType,
friendlyName,
manufacturer,
manufacturerURL,
modelName,
modelNumber,
modelDescription,
UDN,
icons: getIcons(iconList, url),
services: getServices(serviceList, url)
}
}
function getActionArguments(argumentList) {
if (!argumentList) {
return {
inputs: [],
outputs: []
}
}
return Array.from(argumentList.argument).reduce((a, { direction, name, relatedStateVariable }) => {
if (direction === 'in') {
a.inputs.push({
name,
relatedStateVariable
});
} else {
a.outputs.push({
name,
relatedStateVariable
});
}
return a;
}, {
inputs: [],
outputs: []
});
}
function getActions(actionList) {
if (!actionList) {
return {};
}
return Array.from(actionList.action).reduce((a, { name, argumentList }) => {
a[name] = getActionArguments(argumentList);
return a;
}, {});
}
function getAllowedValues(allowedValueList) {
if (!allowedValueList) {
return [];
}
if (Array.isArray(allowedValueList.allowedValue)) {
return allowedValueList.allowedValue;
}
return [ allowedValueList.allowedValue ];
}
function getStateVariables(serviceStateTable) {
if (!serviceStateTable) {
return {};
}
return Array.from(serviceStateTable.stateVariable).reduce((a, { name, __sendEvents, allowedValueList, ...fields }) => {
a[name] = {
...fields,
sendEvents: __sendEvents !== 'no',
allowedValues: getAllowedValues(allowedValueList)
}
return a;
}, {});
}
function parseServiceDescription(xmlString) {
const obj = parser.parse(xmlString, {
attributeNamePrefix: '__',
ignoreAttributes: false
});
const {
actionList,
serviceStateTable
} = obj.scpd;
return {
actions: getActions(actionList),
stateVariables: getStateVariables(serviceStateTable)
}
}
function extractResponseFromEnvelope(envelope, actionName) {
const extractPaths = [
['s:Envelope', 's:Body', `u:${actionName}Response`],
['SOAP-ENV:Envelope', 'SOAP-ENV:Body', `m:${actionName}Response`],
];
for(const path of extractPaths) {
const result = path.reduce((subject, pathItem) => {
if(!subject) {
return null;
}
return subject[pathItem];
}, envelope);
if(result) {
return result;
}
}
throw new Error(`Cannot extract ${actionName} reponse from envelope: ${JSON.stringify(envelope)}`);
}
function parseSOAPResponse(xmlString, actionName, outputs) {
const envelope = parser.parse(xmlString);
const res = extractResponseFromEnvelope(envelope, actionName);
return outputs.reduce((a, { name }) => {
a[name] = res[name];
return a;
}, {});
}
function parseEventInstance({ __val, ...events }) {
return Object.entries(events).map(([ name, data ]) => ({
instanceId: __val,
name,
value: eventsValues[name] ? eventsValues[name](data.__val) : data.__val
}));
}
function parseEventsProperties(prop) {
const entry = Object.entries(prop);
return {
name: entry[0],
value: entry[1]
}
}
function parseEvents(buf) {
const body = parser.parse(buf.toString());
const props = body['e:propertyset']['e:property'];
if (props.LastChange) {
const events = parser.parse(he.decode(props.LastChange), {
attributeNamePrefix: '__',
ignoreAttributes: false,
parseAttributeValue: true
});
const InstanceID = events.Event.InstanceID;
const instances = Array.isArray(InstanceID) ? InstanceID : [ InstanceID ];
return instances.map(parseEventInstance).flat();
}
return props.map(parseEventsProperties)
}
function parseTimeout(header) {
return Number(header.split('-')[1]);
}
module.exports = {
parseDeviceDescription,
parseServiceDescription,
parseSOAPResponse,
parseTimeout,
parseEvents
}