forked from andig/fritzapi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.js
151 lines (135 loc) · 4.3 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
/**
* fritzapi - Fritz goes smartHome
*
* AVM SmartHome nodeJS Control - for AVM Fritz!Box and Dect Devices
*
* @author Andreas Goetz <[email protected]>
*/
/* jshint esversion: 6 */
var Fritz = require('./index.js').Fritz,
commandLineArgs = require('command-line-args'),
getUsage = require('command-line-usage');
// 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 + "°C");
});
},
function() {
return fritz.getTempTarget(thermostat).then(function(temp) {
temp = isNaN(temp) ? '-' : temp + "°C";
console.log("[" + thermostat + "] target temp " + temp + "°C\n");
});
}
]);
};
}));
});
}
// 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|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('debug') >= 0) {
tasks.push(function() {
return debug();
});
}
// run tasks
sequence(tasks).catch(errorHandler);