This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathexample.js
209 lines (189 loc) · 6.29 KB
/
example.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
/**
* fritzapi - Fritz goes smartHome
*
* AVM SmartHome nodeJS Control - for AVM Fritz!Box and Dect Devices
*
* @author Andreas Goetz <cpuidle@gmx.de>
*/
/* jshint esversion: 6 */
var Fritz = require('./index.js').Fritz,
commandLineArgs = require('command-line-args'),
getUsage = require('command-line-usage'),
csv = require('csv');
// utility function to sequentialize promises
function sequence(promises) {
var result = Promise.resolve();
promises.forEach(function(promise,i) {
result = result.then(promise);
});
return result;
}
function errorHandler(error) {
if (error == "0000000000000000")
console.error("Did not get session id- invalid username or password?");
else
console.error(error);
}
// display switch information
function switches() {
return fritz.getSwitchList().then(function(switches) {
console.log("Switches: " + switches + "\n");
return sequence(switches.map(function(sw) {
return function() {
return sequence([
function() {
return fritz.getSwitchName(sw).then(function(name) {
console.log("[" + sw + "] " + name);
});
},
function() {
return fritz.getSwitchPresence(sw).then(function(presence) {
console.log("[" + sw + "] presence: " + presence);
});
},
function() {
return fritz.getSwitchState(sw).then(function(state) {
console.log("[" + sw + "] state: " + state);
});
},
function() {
return fritz.getTemperature(sw).then(function(temp) {
temp = isNaN(temp) ? '-' : temp + "°C";
console.log("[" + sw + "] temp: " + temp + "\n");
});
}
]);
};
}));
});
}
// display thermostat information
function thermostats() {
return fritz.getThermostatList().then(function(thermostats) {
console.log("Thermostats: " + thermostats + "\n");
return sequence(thermostats.map(function(thermostat) {
return function() {
return sequence([
// there is no native getThermostatName function- use getDevice instead
function() {
return fritz.getDevice(thermostat).then(function(device) {
console.log("[" + thermostat + "] " + device.name);
});
},
function() {
return fritz.getTemperature(thermostat).then(function(temp) {
temp = isNaN(temp) ? '-' : temp + "°C";
console.log("[" + thermostat + "] temp: " + temp);
});
},
function() {
return fritz.getTempTarget(thermostat).then(function(temp) {
temp = isNaN(temp) ? '-' : temp + "°C";
console.log("[" + thermostat + "] target temp: " + temp + "\n");
});
}
]);
};
}));
});
}
// display bulb information
function bulbs() {
return fritz.getBulbList().then(function(bulbs) {
console.log("Bulbs: " + bulbs + "\n");
return sequence(bulbs.map(function(bulb) {
return function() {
return sequence([
function() {
// same as for thermostats, use getdevice for device information
// Not very efficient, because the complete device list is
// downloaded for each device again, but this is a demo, so what ...
return fritz.getDevice(bulb).then(function(device) {
console.log("[" + bulb + "] " + device.name);
console.log("[" + bulb + "] presence: " + device.present);
onOff = device.simpleonoff.state == '0' ? 'off' : 'on';
console.log("[" + bulb + "] state: " + onOff);
// a bulb has either hue/saturation values
if (device.colorcontrol.hue != '') {
console.log("[" + bulb + "] hue: " + device.colorcontrol.hue);
console.log("[" + bulb + "] saturation: " + device.colorcontrol.saturation);
}
// or a color temperature
else
console.log("[" + bulb + "] temperature: " + device.colorcontrol.temperature);
console.log("[" + bulb + "] level: " + device.levelcontrol.level + '\n');
});
}
]);
};
}));
});
}
// show phone List
function phoneList() {
return fritz.getPhoneList().then(function(body) {
console.log("Phone list as csv: "+body);
// strip first line with delimiter
csv.parse(body.split("\n").slice(1).join("\n"), {
delimiter: ';'
}, function(err, data) {
if (err) {
console.log("Error in converting csv!")
} else {
console.log("Phone list: "+JSON.stringify(data));
}
});
});
}
// display debug information
function debug() {
return fritz.getDeviceList().then(function(devices) {
console.log("Raw devices\n");
console.log(devices);
});
}
// -- main --
const cmdOptionsDefinition = [
{ name: 'username', alias: 'u', type: String },
{ name: 'password', alias: 'p', type: String },
{ name: 'types', alias: 't', type: String, multiple: true, description: 'switches|thermostats|bulbs|debug, default is all, multiple possible' },
{ name: 'url', type: String },
{ name: 'help', alias: 'h', type: Boolean }
];
const cmdOptions = commandLineArgs(cmdOptionsDefinition);
if (cmdOptions.username === undefined || cmdOptions.help) {
const sections = [{
header: 'Example App',
content: 'A simple app demonstrating the fritzapi functions.'
}, {
header: 'Options',
optionList: cmdOptionsDefinition
}];
console.log(getUsage(sections));
return;
}
// parse options
var fritz = new Fritz(cmdOptions.username, cmdOptions.password||"", cmdOptions.url||""),
tasks = [];
if (cmdOptions.types === undefined || cmdOptions.types.indexOf('switches') >= 0) {
tasks.push(function() {
return switches();
});
}
if (cmdOptions.types === undefined || cmdOptions.types.indexOf('thermostats') >= 0) {
tasks.push(function() {
return thermostats();
});
}
if (cmdOptions.types === undefined || cmdOptions.types.indexOf('bulbs') >= 0) {
tasks.push(function() {
return bulbs();
});
}
if (cmdOptions.types === undefined || cmdOptions.types.indexOf('debug') >= 0) {
tasks.push(function() {
return debug();
});
}
// run tasks
sequence(tasks).catch(errorHandler);