-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmethods.js
149 lines (115 loc) · 4.78 KB
/
methods.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
"use strict";
const http = require("http");
const https = require("https");
const NAMESPACES = {
"soap-enc": "http://schemas.xmlsoap.org/soap/encoding/",
"soap-env": "http://schemas.xmlsoap.org/soap/envelope/",
"xsd": "http://www.w3.org/2001/XMLSchema",
"xsi": "http://www.w3.org/2001/XMLSchema-instance",
"cwmp": "urn:dslforum-org:cwmp-1-0"
};
const INFORM_PARAMS = [
"Device.DeviceInfo.SpecVersion",
"InternetGatewayDevice.DeviceInfo.SpecVersion",
"Device.DeviceInfo.HardwareVersion",
"InternetGatewayDevice.DeviceInfo.HardwareVersion",
"Device.DeviceInfo.SoftwareVersion",
"InternetGatewayDevice.DeviceInfo.SoftwareVersion",
"Device.DeviceInfo.ProvisioningCode",
"InternetGatewayDevice.DeviceInfo.ProvisioningCode",
"Device.ManagementServer.ParameterKey",
"InternetGatewayDevice.ManagementServer.ParameterKey",
"Device.ManagementServer.ConnectionRequestURL",
"InternetGatewayDevice.ManagementServer.ConnectionRequestURL",
"Device.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.ExternalIPAddress",
"InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANPPPConnection.1.ExternalIPAddress",
"Device.WANDevice.1.WANConnectionDevice.1.WANIPConnection.1.ExternalIPAddress",
"InternetGatewayDevice.WANDevice.1.WANConnectionDevice.1.WANIPConnection.1.ExternalIPAddress"
];
const pending = [];
function getPending() {
return pending.shift();
}
function getSortedPaths(device) {
if (!device._sortedPaths)
device._sortedPaths = Object.keys(device).filter(p => p[0] !== "_").sort();
return device._sortedPaths;
}
function GetParameterNames(device, xmlIn, xmlOut, callback) {
let parameterNames = getSortedPaths(device);
let parameterPath = xmlIn.get("/soap-env:Envelope/soap-env:Body/cwmp:GetParameterNames/ParameterPath", NAMESPACES).text();
let nextLevel = Boolean(JSON.parse(xmlIn.get("/soap-env:Envelope/soap-env:Body/cwmp:GetParameterNames/NextLevel", NAMESPACES).text()));
let parameterList = [];
if (nextLevel) {
for (let p of parameterNames) {
if (p.startsWith(parameterPath) && p.length > parameterPath.length + 1) {
let i = p.indexOf(".", parameterPath.length + 1);
if (i === -1 || i === p.length - 1)
parameterList.push(p);
}
}
} else {
for (let p of parameterNames) {
if (p.startsWith(parameterPath))
parameterList.push(p);
}
}
let getParameterNamesResponseNode = xmlOut.root().childNodes()[1]
.node("cwmp:GetParameterNamesResponse");
let parameterListNode = getParameterNamesResponseNode.node("ParameterList");
parameterListNode.attr({
"soap-enc:arrayType": `cwmp:ParameterInfoStruct[${parameterList.length}]`
});
for (let p of parameterList) {
let parameterInfoStructNode = parameterListNode.node("ParameterInfoStruct");
parameterInfoStructNode.node("Name", p);
parameterInfoStructNode.node("Writable", String(device[p][0]));
}
return callback(xmlOut);
}
function GetParameterValues(settings, entry, xmlOut, callback) {
let body = xmlOut.root().childNodes()[1];
//let parameterNames = xmlIn.find("/soap-env:Envelope/soap-env:Body/cwmp:GetParameterValues/ParameterNames/*", NAMESPACES);
let parameterList = xmlOut.root().childNodes()[1].node("cwmp:GetParameterValues").node("ParameterNames").node("string").text(entry);
// parameterList.attr({
// "SOAP-ENC:arrayType": "cwmp:ParameterValueStruct[" + parameterNames.length + "]"
// });
// for (let p of parameterNames) {
// let name = p.text();
// let valueStruct = parameterList.node("ParameterValueStruct");
// valueStruct.node("Name", name);
// valueStruct.node("Value", device[name][1]).attr({
// "xsi:type": type
// });
//}
return callback(xmlOut);
}
function inform(settings, xmlOut, callback) {
let body = xmlOut.root().childNodes()[1];
let inform = body.node("cwmp:Inform");
let deviceId = inform.node("DeviceId");
let eventStruct = inform.node("Event").attr({
"soap-enc:arrayType": "cwmp:EventStruct[1]"
}).node("EventStruct");
eventStruct.node("EventCode", "2 PERIODIC");
eventStruct.node("CommandKey");
inform.node("MaxEnvelopes", "1");
inform.node("CurrentTime", new Date().toISOString());
inform.node("RetryCount", "0");
let parameterList = inform.node("ParameterList").attr({
"soap-enc:arrayType": "cwmp:ParameterValueStruct[7]"
});
// for (let p of INFORM_PARAMS) {
// let param = device[p];
// if (!param)
// continue;
// let parameterValueStruct = parameterList.node("ParameterValueStruct");
// parameterValueStruct.node("Name", p);
// parameterValueStruct.node("Value", param[1]).attr({"xsi:type": param[2]});
// }
return callback(xmlOut);
}
exports.inform = inform;
exports.getPending = getPending;
exports.GetParameterNames = GetParameterNames;
exports.GetParameterValues = GetParameterValues;