generated from homebridge/homebridge-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphoneXML.model.ts
345 lines (295 loc) · 9.14 KB
/
phoneXML.model.ts
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import { Logger } from 'homebridge';
import { PLATFORM_NAME } from './settings';
import builder = require('xmlbuilder2');
import axios, { AxiosRequestConfig } from 'axios';
/**
* Soft Keys that are part of any response
*/
export class SoftKeyItem {
name: string;
url: string;
urldown?: string;
position: string;
}
/**
* Menu Items
*/
export class MenuItem {
name: string;
url: string;
}
export class InputItem {
name: string;
param: string;
type: string;
}
export class DirectoryItem {
name: string;
phone: string;
}
export class ExecuteItem {
URL: string;
Priority: string;
}
export class XMLOptions {
appid?: string;
title?: string;
prompt?: string;
softkeys?: SoftKeyItem[];
}
/**
* Common attributes to all XML Elements
*/
abstract class CiscoXMLRoot {
protected name: string;
protected appid?: string;
protected title?: string;
protected prompt?: string;
protected softkeys?: SoftKeyItem[];
constructor(options: XMLOptions) {
if ( options.appid !== undefined ) {
this.appid = options.appid;
}
if (options.title !== undefined ) {
this.title = options.title;
}
if ( options.prompt) {
this.prompt = options.prompt;
}
if ( options.softkeys !== undefined ) {
this.softkeys = options.softkeys;
}
}
abstract toXML(): string;
protected _toXML() {
const xml_root = builder.create({ version: '1.0', encoding: 'ISO-8859-1'});
const xml = xml_root.ele(this.name).att('appId', this.appid || PLATFORM_NAME);
if (this.title) {
xml.ele('Title').txt(this.title);
}
if (this.prompt) {
xml.ele('Prompt').txt(this.prompt);
}
if ( this.softkeys && this.softkeys.length > 0) {
this.softkeys.forEach(key => {
const softkey = xml.ele('SoftKeyItem');
softkey.ele('Name').txt(key.name);
softkey.ele('URL').txt(key.url);
if (key.urldown) {
softkey.ele('URLDown').txt(key.urldown);
}
softkey.ele('Position').txt(key.position);
});
}
return (xml);
}
}
export class CiscoIPPhoneMenu extends CiscoXMLRoot {
private menuitems: MenuItem[];
constructor(options: XMLOptions, menuOptions: MenuItem[]) {
super(options);
this.name = 'CiscoIPPhoneMenu';
this.menuitems = menuOptions;
}
toXML() {
const xml_root = super._toXML();
this.menuitems.forEach(mitem => {
const item = xml_root.ele('MenuItem');
item.ele('Name').txt(mitem.name);
item.ele('URL').txt(mitem.url);
});
return xml_root.end();
}
}
export class CiscoIPPhoneText extends CiscoXMLRoot {
private text: string;
constructor(options: XMLOptions, text: string) {
super(options);
this.name = 'CiscoIPPhoneText';
this.text = text;
}
toXML() {
const xml_root = super._toXML();
xml_root.ele('Text').txt(this.text);
return xml_root.end();
}
}
/*
URL: The URL the phone will visit when the user enters the value(s) asked for
fields: An array of objects with the following properties:
name: The name of the field asked for
param: The name of the HTTP GET parameter passed to the URL when done
type: Field type - A for ascii text; T for a telephone number;
N for a numeric value; E for a mathematical expression;
U for uppercase text; L for lowercase text. Add a "P" to the type
to make it a password (phone will obscure entry)
*/
export class CiscoIPPhoneInput extends CiscoXMLRoot {
private url: string;
private inputitems: InputItem[];
constructor(options: XMLOptions, url: string, items: InputItem[]) {
super(options);
this.name = 'CiscoIPPhoneInput';
this.url = url;
this.inputitems = items;
}
toXML() {
const xml_root = super._toXML();
this.inputitems.forEach(inputitem => {
const item = xml_root.ele('InputItem');
item.ele('DisplayName').txt(inputitem.name);
item.ele('QueryStringParam').txt(inputitem.param);
item.ele('InputFlags').txt(inputitem.type);
});
xml_root.ele('URL').txt(this.url);
return xml_root.end();
}
}
export class CiscoIPPhoneDirectory extends CiscoXMLRoot {
private entries: DirectoryItem[];
constructor(options: XMLOptions, entries: DirectoryItem[]) {
super(options);
this.name = 'CiscoIPPhoneDirectory';
this.entries = entries;
}
toXML() {
const xml_root = super._toXML();
this.entries.forEach(entry => {
const item = xml_root.ele('DirectoryEntry');
item.ele('Name').txt(entry.name);
item.ele('Telephone').txt(entry.phone);
});
return xml_root.end();
}
}
/** Builds a CiscoUIPPhoneExecute payload
// This is used to execute arbitrary commands on the phone
// commands: an object, each property being a command (URL), and each key
// being the "priority" of the command, as follows:
// 0: Execute immediatly
// 1: Queue - delay execution until phone is idle
// 2: Execute only if phone is idle
**/
export class CiscoIPPhoneExecute extends CiscoXMLRoot {
commands: ExecuteItem[];
constructor(commands: ExecuteItem[]) {
super({});
this.commands = commands;
}
toXML() {
const xml_root = builder.create({ version: '1.0', encoding: 'ISO-8859-1'});
const ex = xml_root.ele('CiscoIPPhoneExecute');
this.commands.forEach(command => {
ex.ele('ExecuteItem', { URL: command.URL, Priority: command.Priority });
});
return xml_root.end();
}
}
/**
* Represents a phone to communicate with
*/
export class CiscoIPPhone {
host: string;
port?: number;
username?: string;
password?: string;
}
/**
* Send and Poll Phone For Information
*/
export class PhoneCommunicator {
private phone: CiscoIPPhone;
private log: Logger;
constructor(phone: CiscoIPPhone, log: Logger) {
this.phone = phone;
this.log = log;
}
/**
* Send XML to phone to execute. Each result is logged to the HomeKit Console
*
* @param xmlObject Payload to send to the Phone
* @return Status Code from POST
*/
send(xmlObject: CiscoXMLRoot) : Promise<number> {
// convert to XML
const xml = xmlObject.toXML();
const options: AxiosRequestConfig = {
baseURL: 'http://' + this.phone.host + ':' + (this.phone.port || 80),
method: 'POST',
responseType: 'text',
headers: {
'content-type': 'text/xml; charset:ISO-8859-1',
},
};
if (this.phone.username && this.phone.password) {
options.auth = { username: this.phone.username, password: this.phone.password };
}
this.log.debug('Posting ', xml.toString());
return new Promise((resolve, reject)=> {
axios.post('/CGI/Execute', `XML=${xml}`, options).then((response) => {
// Convert to XML Document
const xml = builder.create(response.data);
// Traverse the document and log each repsonse for logging
xml.root().each((element) => {
this.log.info(element.toString());
});
resolve(response.status);
}).catch((err) => {
this.log.error(err);
reject(err);
});
});
}
/**
* Get Device Information like serial, model, etc.
* Most importantly this provided MWI status
*/
public getDeviceInfoX(): Promise<Record<string, string>> {
return this.getDeviceInfo('/DeviceInformationX');
}
/**
* Get the Networking configuration
*/
public getNetworkInfoX(): Promise<Record<string, string>> {
return this.getDeviceInfo('/NetworkConfigurationX');
}
/**
* Get Stream Information, most notibly to determine if the phone
* is on a call or not. Handles the general case.
*/
public getStreamInfoX(): Promise<Record<string, string>> {
return this.getDeviceInfo('/StreamingStatisticsX?n1');
}
/**
* Perform a GET on the phone for information, these operations
* don't require authentication, though we send it if provided
* @param url
*/
private getDeviceInfo(url: string): Promise<Record<string, string>> {
const options: AxiosRequestConfig = {
baseURL: 'http://' + this.phone.host + ':' + (this.phone.port || 80),
method: 'GET',
responseType: 'text',
};
if (this.phone.username && this.phone.password) {
options.auth = { username: this.phone.username, password: this.phone.password };
}
return new Promise((resolve, reject) => {
axios.get(url, options).then((response) => {
this.log.debug('Raw Response is ' + response.data);
// Convert to XML Document
const xml = builder.create(response.data);
// Traverse the document and produce a flat JS Object
const result: Record<string, string> = {};
xml.root().each((element) => {
result[element.node.nodeName] = element.node.textContent;
});
//this.log.info(JSON.stringify(result));
resolve(result);
}).catch((err) => {
this.log.error(err);
reject(err);
});
});
}
}