-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.js
293 lines (273 loc) · 9.27 KB
/
main.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
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
/* jshint -W097 */// jshint strict:false
/* jslint node: true */
'use strict';
// You have to require the utils module and call adapter function
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
const ping = require('ping');
const {VieraKeys, Viera} = require('node-panasonic-viera');
// Mapping table for state <> VieraKeys mapping
const stateKeyMap = {
'others.30S_SKIP': VieraKeys.thirty_second_skip,
'others.3D': VieraKeys.toggle_3d,
'others.APPS': VieraKeys.apps,
'others.ASPECT': VieraKeys.aspect,
'basic.BACK': VieraKeys.back,
'basic.BLUE': VieraKeys.blue,
'basic.CANCEL': VieraKeys.cancel,
'others.CC': VieraKeys.cc,
'others.CHAT': VieraKeys.chat_mode,
'channel.CH_DOWN': VieraKeys.ch_down,
'input.CHG_INPUT': VieraKeys.input_key,
'others.CHG_NETWORK': VieraKeys.network,
'channel.CH_UP': VieraKeys.ch_up,
'channel.D0': VieraKeys.num_0,
'channel.D1': VieraKeys.num_1,
'channel.D2': VieraKeys.num_2,
'channel.D3': VieraKeys.num_3,
'channel.D4': VieraKeys.num_4,
'channel.D5': VieraKeys.num_5,
'channel.D6': VieraKeys.num_6,
'channel.D7': VieraKeys.num_7,
'channel.D8': VieraKeys.num_8,
'channel.D9': VieraKeys.num_9,
'others.DIGA_CTL': VieraKeys.diga_control,
'others.DISP_MODE': VieraKeys.display,
'basic.DOWN': VieraKeys.down,
'basic.ENTER': VieraKeys.enter,
'info.EPG': VieraKeys.epg,
'basic.EXIT': VieraKeys.exit,
'others.EZ_SYNC': VieraKeys.ez_sync,
'others.FAVORITE': VieraKeys.favorite,
'player.FF': VieraKeys.fast_forward,
'others.GAME': VieraKeys.game,
'basic.GREEN': VieraKeys.green,
'info.GUIDE': VieraKeys.guide,
'others.HOLD': VieraKeys.hold,
'input.HDMI1': VieraKeys.hdmi1,
'input.HDMI2': VieraKeys.hdmi2,
'input.HDMI3': VieraKeys.hdmi3,
'input.HDMI4': VieraKeys.hdmi4,
'others.HOME': VieraKeys.home,
'info.INDEX': VieraKeys.index,
'info.INFO': VieraKeys.info,
'others.INTERNET': VieraKeys.connect,
'basic.left': VieraKeys.left,
'info.MENU': VieraKeys.menu,
'others.MPX': VieraKeys.mpx,
// 'basic.mute': VieraKeys.mute,
'basic.MUTE': VieraKeys.mute,
'others.NET_BS': VieraKeys.net_bs,
'others.NET_CS': VieraKeys.net_cs,
'others.NET_TD': VieraKeys.net_td,
'others.OFFTIMER': VieraKeys.off_timer,
'player.PAUSE': VieraKeys.pause,
'others.PICTAI': VieraKeys.pictai,
'player.PLAY': VieraKeys.play,
'others.P_NR': VieraKeys.p_nr,
'basic.POWER': VieraKeys.power,
'others.PROG': VieraKeys.program,
'player.REC': VieraKeys.record,
'basic.RED': VieraKeys.red,
'basic.RETURN': VieraKeys.return_key,
'player.REW': VieraKeys.rewind,
'basic.RIGHT': VieraKeys.right,
'others.R_SCREEN': VieraKeys.r_screen,
'others.R_TUNE': VieraKeys.last_view,
'others.SAP': VieraKeys.sap,
'others.SD_CARD': VieraKeys.toggle_sd_card,
'player.SKIP_NEXT': VieraKeys.skip_next,
'player.SKIP_PREV': VieraKeys.skip_prev,
'others.SPLIT': VieraKeys.split,
'player.STOP': VieraKeys.stop,
'others.STTL': VieraKeys.subtitles,
'others.SUBMENU': VieraKeys.option,
'others.SURROUND': VieraKeys.surround,
'others.SWAP': VieraKeys.swap,
'info.TEXT': VieraKeys.text,
'others.TV': VieraKeys.tv,
'others.TV_MUTE_ON': VieraKeys.tv_mute_on,
'others.TV_MUTE_OFF': VieraKeys.tv_mute_off,
'others.DMS_CH_UP': VieraKeys.dms_ch_up,
'others.DMS_CH_DOWN': VieraKeys.dms_ch_down,
'basic.UP': VieraKeys.up,
'others.VIERA_LINK': VieraKeys.link,
// 'basic.VOLDOWN': VieraKeys.volume_down,
// 'basic.VOLUP': VieraKeys.volume_up,
'info.VTOOLS': VieraKeys.vtools,
'basic.YELLOW': VieraKeys.yellow,
'others.RECLIST': VieraKeys.reclist,
'others.DRIVE': VieraKeys.drive,
'others.DATA': VieraKeys.data,
'others.BD': VieraKeys.bd,
'others.VOD': VieraKeys.vod,
'others.ECO': VieraKeys.eco
};
let viera = null;
let isConnected = null;
// You have to call the adapter function and pass a options object
// name has to be set and has to be equal to adapters folder name and main file name excluding extension
// adapter will be restarted automatically every time as the configuration changed, e.g system.adapter.template.0
let adapter;
function startAdapter(options) {
options = options || {};
Object.assign(options, {
name: 'panasonic-viera',
stateChange(id, state) {
if (id && state && !state.ack) {
let cmd = id.split('.').slice(-2).join('.');
adapter.log.debug('state triggered: ' + cmd + ': ' + state.val);
if (typeof (stateKeyMap[cmd]) !== 'undefined') {
cmd = stateKeyMap[cmd];
}
adapter.log.debug('sending command: ' + cmd);
sendCommand(cmd, state.val);
}
},
ready: main
});
adapter = new utils.Adapter(options);
return adapter;
}
function setConnected(_isConnected) {
if (isConnected !== _isConnected) {
isConnected = _isConnected;
adapter.setState('info.connection', {val: isConnected, ack: true});
}
}
function main() {
if (adapter.config.ip && adapter.config.ip !== '0.0.0.0') {
viera = new Viera();
// In this template all states changes inside the adapters namespace are subscribed
adapter.subscribeStates('*');
checkStatus();
setInterval(checkStatus, 30000);
} else {
adapter.log.error('Please configure the Panasonic Viera adapter');
}
}
function checkStatus() {
ping.promise.probe(adapter.config.ip)
.then(result => {
setConnected(result.alive);
if (result.alive) {
sendCommand('getMute');
sendCommand('getVolume');
}
})
.catch(error => {
adapter.log.error(error);
});
}
function sendCommand(cmd, value) {
if (isConnected) {
switch (cmd) {
case 'getMute':
viera.connect(adapter.config.ip, adapter.config.app_id, adapter.config.encryption_key)
.then(() => {
return viera.getMute();
})
.then(mute => {
adapter.setState('basic.mute', {val: mute, ack: true});
adapter.setState('info.tv_on', {val: true, ack: true});
})
.catch(error => {
adapter.log.debug('getMute: ' + error);
adapter.setState('info.tv_on', {val: false, ack: true});
});
break;
case 'basic.mute':
viera.connect(adapter.config.ip, adapter.config.app_id, adapter.config.encryption_key)
.then(() => {
return viera.setMute(value);
})
.then(() => {
return viera.getMute();
})
.then(mute => {
adapter.setState('basic.mute', {val: mute, ack: true});
})
.catch(error => {
adapter.log.error('getMute: ' + error);
});
break;
case 'getVolume':
viera.connect(adapter.config.ip, adapter.config.app_id, adapter.config.encryption_key)
.then(() => {
return viera.getVolume();
})
.then(volume => {
adapter.setState('basic.volume', {val: volume, ack: true});
adapter.setState('info.tv_on', {val: true, ack: true});
})
.catch(error => {
adapter.log.debug('getVolume: ' + error);
adapter.setState('info.tv_on', {val: false, ack: true});
});
break;
case 'VOLUP':
viera.connect(adapter.config.ip, adapter.config.app_id, adapter.config.encryption_key)
.then(() => {
return viera.sendKey(VieraKeys.volume_up);
})
.then(() => {
return viera.getVolume();
})
.then(volume => {
adapter.setState('basic.volume', {val: volume, ack: true});
adapter.setState('info.tv_on', {val: true, ack: true});
})
.catch(error => {
adapter.log.error('getVolume: ' + error);
});
break;
case 'VOLDOWN':
viera.connect(adapter.config.ip, adapter.config.app_id, adapter.config.encryption_key)
.then(() => {
return viera.sendKey(VieraKeys.volume_down);
})
.then(() => {
return viera.getVolume();
})
.then(volume => {
adapter.setState('basic.volume', {val: volume, ack: true});
adapter.setState('info.tv_on', {val: true, ack: true});
})
.catch(error => {
adapter.log.error('getVolume: ' + error);
});
break;
case 'basic.volume':
viera.connect(adapter.config.ip, adapter.config.app_id, adapter.config.encryption_key)
.then(() => {
return viera.setVolume(value);
})
.then(() => {
return viera.getVolume();
})
.then(volume => {
adapter.setState('basic.volume', {val: volume, ack: true});
adapter.setState('info.tv_on', {val: true, ack: true});
})
.catch(error => {
adapter.log.error('getVolume: ' + error);
});
break;
default:
viera.connect(adapter.config.ip, adapter.config.app_id, adapter.config.encryption_key)
.then(() => {
return viera.sendKey(cmd);
})
.catch(error => {
adapter.log.error('sendCommand[' + cmd + ']: ' + error);
});
break;
}
}
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// Or start the instance directly
startAdapter();
}