-
Notifications
You must be signed in to change notification settings - Fork 0
/
harmony-xmpp.ts
192 lines (158 loc) · 5.5 KB
/
harmony-xmpp.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
/**
* All the XMPP Stuff and communication with a hub
*
* @module XMPPConnection
*/
/**
*
*/ import * as Debug from 'debug';
import { URL } from 'url';
const debug = Debug('rxharmony:xmpp');
import * as xmpp from 'node-xmpp-client';
import { IQ } from '@xmpp/xml';
const JID = 'guest@x.com/gatorade';
const PASSWORD = 'guest';
import {
IHarmonyHubConnection,
IHarmonyClient,
IHarmonyHubInfo,
} from './harmony.types';
import { encodeRequest, decodeResponse, IHarmonyRequest } from './harmony-utils';
export function getUniqueId(): string {
return Math.floor(Math.random() * 1000000).toString();
}
function buildIqStanza(type, xmlns, mime, body, from?) {
const iq = new IQ({
type: type,
id: getUniqueId(),
from: from,
});
iq.c('oa', {
xmlns: xmlns,
mime: mime,
}).t(body);
return iq;
}
export function buildCommandIqStanza(command, body: IHarmonyRequest = {}) {
const encoded = encodeRequest(body);
debug(
'buildCommandIqStanza for command "' + command + '" with body ',
encoded
);
return buildIqStanza(
'get',
'connect.logitech.com',
'vnd.logitech.harmony/vnd.logitech.harmony.engine?' + command,
encoded
);
}
export function pairHub(url: URL): Promise<IHarmonyHubInfo> {
debug('Pairing hub @', url.toString());
const xmppClient = new xmpp({
jid: JID,
password: PASSWORD,
host: url.hostname,
port: url.port,
disallowTLS: true,
reconnect: false,
});
let iqId;
const retPromise: Promise<IHarmonyHubInfo> = new Promise(
(resolve, reject) => {
xmppClient.once('online', id => {
debug('XMPP client online', id._resource);
const iq = buildIqStanza(
'get',
'connect.logitech.com',
'vnd.logitech.connect/vnd.logitech.pair',
'method=pair:name=rxharmony#iOS6.0.1#iPhone',
'guest'
);
iqId = iq.attr('id');
xmppClient.send(iq);
});
xmppClient.on('error', function(e) {
debug('XMPP client error', e);
reject(e);
});
xmppClient.on('stanza', stanza => {
debug('Stanza received');
debug('Stanza received', stanza.toString());
debug('Response id', stanza.attrs.id, iqId);
if (stanza.attrs.id === iqId.toString()) {
const body = stanza.getChildText('oa');
const response = decodeResponse(body);
debug('Response', response);
if (response && response.identity) {
debug('received identity token: ' + response.ident);
xmppClient.end();
resolve(<IHarmonyHubInfo>response);
} else {
debug('could not find identity token');
xmppClient.end();
reject(new Error('Did not retrieve identity.'));
}
}
});
}
);
return retPromise;
}
export function loginWithIdentity(
url: URL,
info: IHarmonyHubInfo
): Promise<IHarmonyHubConnection> {
debug(
'create xmpp client using retrieved identity token %s at %s for ',
info.identity,
url,
info.friendlyName
);
const xmppClient: IHarmonyClient = new xmpp({
jid: info.identity + '@connect.logitech.com/gatorade',
password: info.identity,
host: url.hostname,
port: url.port,
disallowTLS: true,
reconnect: true,
});
return new Promise((resolve, reject) => {
xmppClient.once('online', () => {
debug('XMPP client successfully connected');
xmppClient.removeAllListeners();
resolve({
friendlyName: info.friendlyName,
url,
client: xmppClient,
});
});
xmppClient.once('error', reject);
});
}
export function createHarmonyClient(
uri: string
): Promise<IHarmonyHubConnection> {
const url = new URL(uri);
return pairHub(url).then(info => loginWithIdentity(url, info));
}
/***
Format 1 (->HUB): key1=value1:key2=value2
Format 2 (-> HUB) a value of format 1 could be {"key"::"value","key"::"value","command"::"VolumeDown"}
Format 3 (-> discover HUB) format 1 with a value {key="value", key="value"}
<iq type="get" id="5e518d07-bcc2-4634-ba3d-c20f338d8927-2">
<oa xmlns="connect.logitech.com" mime="vnd.logitech.harmony/vnd.logitech.harmony.engine?holdAction">
action={"type"::"IRCommand","deviceId"::"11586428","command"::"VolumeDown"}:status=press
</oa>
</iq>
<iq type="get" id="2320426445" from="e01b88af-b4cd-4d1c-8e76-85562ea3fad5">
<oa xmlns="connect.logitech.com" mime="vnd.logitech.harmony/vnd.logitech.harmony.engine?config">
</oa>
</iq>
<iq type="get" id="3174962747" from="guest">
<oa xmlns="connect.logitech.com" mime="vnd.logitech.connect/vnd.logitech.pair">
token=y6jZtSuYYOoQ2XXiU9cYovqtT+cCbcyjhWqGbhQsLV/mWi4dJVglFEBGpm08OjCW:name=1vm7ATw/tN6HXGpQcCs/A5MkuvI#iOS6.0.1#iPhone
</oa>
</iq>
<message from="HarmonyOne_Pop@qa1.com" to="ab903454-7bee-4410-9eea-bb5355bb667e" xmlns:stream="http://etherx.jabber.org/streams">
<event xmlns="connect.logitech.com" type="vnd.logitech.control/vnd.logitech.button?pressType">type=short</event></message>
***/