forked from kaklakariada/fritzbox-java-api
/
TestDriver.java
126 lines (112 loc) · 4.21 KB
/
TestDriver.java
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
/**
* A Java API for managing FritzBox HomeAutomation
* Copyright (C) 2017 Christoph Pirkl <christoph at users.sourceforge.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.github.kaklakariada.fritzbox;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.kaklakariada.fritzbox.EnergyStatisticsService.EnergyStatsTimeRange;
import com.github.kaklakariada.fritzbox.model.homeautomation.Device;
import com.github.kaklakariada.fritzbox.model.homeautomation.DeviceList;
import com.github.kaklakariada.fritzbox.model.homeautomation.PowerMeter;
/**
* this class is a test Driver for the home automation
*
*/
public class TestDriver {
private final static Logger LOG = LoggerFactory.getLogger(TestDriver.class);
/**
* main class
*
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
final Config config = ConfigService.readConfig();
LOG.info("Logging in to '{}' with username '{}'", config.baseUrl,
config.username);
final HomeAutomation homeAutomation = HomeAutomation.connect(config);
final DeviceList devices = homeAutomation.getDeviceListInfos();
LOG.info("Found {} devices", devices.getDevices().size());
for (final Device device : devices.getDevices()) {
LOG.info("\t{}", device);
}
final List<String> ids = homeAutomation.getSwitchList();
LOG.info("Found {} device ids: {}", ids.size(), ids);
if (devices.getDevices().isEmpty()) {
homeAutomation.logout();
return;
}
for (final String ain : ids) {
testHomeAutomation(homeAutomation, ain);
// testEnergyStats(homeAutomation.getSession(), ain);
}
}
/**
* test the energy statistics
*
* @param session
* @param deviceId
*/
private static void testEnergyStats(FritzBoxSessionImpl session,
String deviceId) {
final EnergyStatisticsService service = new EnergyStatisticsService(
session);
for (final EnergyStatsTimeRange timeRange : EnergyStatsTimeRange.values()) {
final String energyStatistics = service.getEnergyStatistics(deviceId,
timeRange);
LOG.debug("Statistics {}: {}", timeRange, energyStatistics);
}
}
/**
* test the home automation
*
* @param homeAutomation
* @param ain
*/
private static void testHomeAutomation(final HomeAutomation homeAutomation,
final String ain) {
// homeAutomation.switchPowerState(ain, false);
// homeAutomation.togglePowerState(ain);
LOG.info("Switch {} ", ain);
LOG.info("\t has present state '{}'", homeAutomation.getSwitchPresent(ain));
LOG.info("\t has state '{}'", homeAutomation.getSwitchState(ain));
LOG.info("\t uses {} W", homeAutomation.getSwitchPowerWatt(ain));
LOG.info("\t has used {} Wh", homeAutomation.getSwitchEnergyWattHour(ain));
LOG.info("\t has name '{}'", homeAutomation.getSwitchName(ain));
LOG.info("\t has temperature {} °C", homeAutomation.getTemperature(ain));
LOG.info("---");
}
/**
* test endless
*
* @param homeAutomation
* @throws Exception
*/
public void testEndless(HomeAutomation homeAutomation) throws Exception {
while (true) {
final Device device = homeAutomation.getDeviceListInfos().getDevices()
.get(0);
final PowerMeter powerMeter = device.getPowerMeter();
LOG.debug("State: {}, temp: {}°C, power: {}W, energy: {}Wh",
device.getSwitchState().isOn() ? "on" : "off",
device.getTemperature().getCelsius(), powerMeter.getPowerWatt(),
powerMeter.getEnergyWattHours());
Thread.sleep(1000);
}
}
}