-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
executable file
·158 lines (120 loc) · 4.65 KB
/
index.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
// DOMUSTO
import config from '../../config';
import DomustoPlugin from '../../domusto/DomustoPlugin';
import DomustoSignalHub from '../../domusto/DomustoSignalHub';
import DomustoDevicesManager from '../../domusto/DomustoDevicesManager';
// INTERFACES
import { Domusto } from '../../domusto/DomustoTypes';
// PLUGIN SPECIFIC
let AVReceiver = require('marantz-avr');
/**
* Marantz plugin for DOMUSTO Home Automation
* @author Bas van Dijk
* @version 0.0.1
*
* @class DomustoMarantz
* @extends {DomustoPlugin}
*/
class DomustoMarantz extends DomustoPlugin {
/**
* Creates an instance of DomustoMarantz.
* @param {any} Plugin configuration as defined in the config.js file
* @memberof DomustoMarantz
*/
constructor(pluginConfiguration: Domusto.PluginConfiguration) {
super({
plugin: 'Marantz-avr remote',
author: 'Bas van Dijk',
category: Domusto.PluginCategories.audio,
version: '0.0.1',
website: 'http://domusto.com'
});
this.pluginConfiguration = pluginConfiguration;
const isConfigurationValid = this.validateConfigurationAttributes(pluginConfiguration.settings, [
{
attribute: 'ip',
type: /^(?!0)(?!.*\.$)((1?\d?\d|25[0-5]|2[0-4]\d)(\.|$)){4}$/
}
]);
if (isConfigurationValid) {
// Initialize hardware plugin
const receiver = new AVReceiver(pluginConfiguration.settings.ip);
this.hardwareInstance = receiver;
// Poll current receiver status
this.refreshReceiverStatus();
// Start polling receiver on interval
setInterval(() => this.refreshReceiverStatus(), pluginConfiguration.settings.pollInterval | 10000);
this.console.header(`${pluginConfiguration.id} plugin ready for sending / receiving data`);
}
}
onSignalReceivedForPlugin(signal: Domusto.Signal) {
const pluginDevice = signal.deviceId.split('-')[0];
const pluginDeviceValue = signal.deviceId.split('-')[1];
switch (pluginDevice) {
case 'power':
this.hardwareInstance.setPowerState(signal.data['state'] === 'on').then(res => {
this.broadcastSignal('power', {
state: signal.data['state']
});
}, error => this.console.error(error));
break;
case 'source':
this.hardwareInstance.setInputSource(pluginDeviceValue).then(res => {
}, error => this.console.error(error));
break;
case 'mute':
this.hardwareInstance.setMuteState(signal.data['state'] === 'on').then(res => {
this.broadcastSignal('mute', {
state: signal.data['state']
});
}, error => this.console.error(error));
break;
case 'volume':
switch (signal.data['state']) {
case 'off':
this.hardwareInstance.volumeUp().then(res => {
this.broadcastSignal('volume', {
state: signal.data['state']
});
}, error => this.console.error(error));
break;
case 'on':
this.hardwareInstance.volumeDown().then(res => {
this.broadcastSignal('volume', {
state: signal.data['state']
});
}, error => this.console.error(error));
break;
}
break;
}
}
/**
* Polls the receiver and broadcast the status to the Signal Hub
*
* @memberof DomustoMarantz
*/
refreshReceiverStatus() {
this.hardwareInstance.getState().then(
res => {
this.broadcastSignal('power', {
state: res.power ? 'on' : 'off'
});
this.broadcastSignal('input', {
state: res.input
});
this.broadcastSignal('volume', {
state: res.volumeLevel
});
this.broadcastSignal('mute', {
state: res.mute ? 'on' : 'off'
});
this.broadcastSignal('surroundMode', {
state: res.surroundMode
});
},
error => this.console.error(error)
);
}
}
export default DomustoMarantz;