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
|
#!/usr/bin/perl
#
# https://avm.de/fileadmin/user_upload/Global/Service/Schnittstellen/AHA-HTTP-Interface.pdf
#
use warnings;
use strict;
use POSIX;
use utf8;
use Config::Simple;
use LockFile::Simple;
use LWP::UserAgent;
use XML::Simple;
use Digest::MD5 "md5_hex";
use Data::Dumper;
# config
my $cfg = new Config::Simple( syntax => 'ini' );
$cfg->autosave(1);
$cfg->read($ENV{HOME} . "/.fritz.rc");
my $host = $cfg->param('host');
my $passwd = $cfg->param('passwd');
# global variables
my ($ua, $sid, $devlist);
##############################################################################
sub login {
# get challenge
my $url = "http://${host}/login_sid.lua";
my $req = HTTP::Request->new(GET => $url);
my $res = $ua->request($req);
die "login http request #1 failed" unless $res->is_success;
my $xml = XMLin($res->content);
# construct response
my $cr = $xml->{'Challenge'} . "-" . $passwd;
$cr =~ s/(.)/$1 . chr(0)/eg; # to utf16
my $md5 = lc(md5_hex($cr));
$url = "http://${host}/login_sid.lua?response=" .
$xml->{'Challenge'} . "-" . $md5;
# login and get session id
$req = HTTP::Request->new(GET => $url);
$res = $ua->request($req);
die "login http request #2 failed" unless $res->is_success;
$xml = XMLin($res->content);
$sid = $xml->{'SID'};
die "login failed" if $sid eq "0000000000000000";
# printf "login ok (sid %s)\n\n", $sid;
}
sub get_devlist {
my $url = "http://${host}/webservices/homeautoswitch.lua?" .
"switchcmd=getdevicelistinfos&sid=" . $sid;
my $req = HTTP::Request->new(GET => $url);
my $res = $ua->request($req);
die "devlist http request failed" unless $res->is_success;
$devlist = XMLin($res->content);
}
sub print_devlist {
foreach my $name (sort keys %{$devlist->{'device'}}) {
my $dev = $devlist->{'device'}->{$name};
printf "%-20s %-20s", $name, $dev->{'productname'};
if (defined($dev->{'switch'})) {
printf(" sw: %-3s",
$dev->{'switch'}->{'state'} ? "on" : "off");
}
if (defined($dev->{'temperature'})) {
printf(" temp: %.1f",
$dev->{'temperature'}->{'celsius'} / 10);
}
if (defined($dev->{'present'}) && $dev->{'present'} == 0) {
printf(" (offline)");
}
printf "\n";
}
}
sub set_switch {
my $switch = shift;
my $state = shift;
my $cmd;
die "device $switch not found"
unless defined($devlist->{'device'}->{$switch});
die "device $switch is not a switch"
unless defined($devlist->{'device'}->{$switch}->{'switch'});
$cmd = "setswitchon" if $state =~ m/(1|on)/i;
$cmd = "setswitchoff" if $state =~ m/(0|off)/i;
die "unknown switch state: $state" unless defined($cmd);
my $url = "http://${host}/webservices/homeautoswitch.lua?" .
"switchcmd=" . $cmd . "&sid=" . $sid . "&ain=" .
$devlist->{'device'}->{$switch}->{'identifier'};
my $req = HTTP::Request->new(GET => $url);
my $res = $ua->request($req);
die "setswitch http request failed" unless $res->is_success;
}
sub get_switch {
my $switch = shift;
my $state;
die "device $switch not found"
unless defined($devlist->{'device'}->{$switch});
die "device $switch is not a switch"
unless defined($devlist->{'device'}->{$switch}->{'switch'});
$state = $devlist->{'device'}->{$switch}->{'switch'}->{'state'};
printf "%s\n", $state ? "on" : "off";
}
sub get_temp {
my $sensor = shift;
my $temp;
die "device $sensor not found"
unless defined($devlist->{'device'}->{$sensor});
die "device $sensor is not a temp sensor"
unless defined($devlist->{'device'}->{$sensor}->{'temperature'});
$temp = $devlist->{'device'}->{$sensor}->{'temperature'};
printf "%.1f\n", $temp->{'celsius'} / 10;
}
##############################################################################
sub print_help {
print <<EOF;
usage:
general
fritz help print this text
configure
fritz host <host|ip> set hostname
fritz passwd <pw> set password
query
fritz devlist print device list (default)
fritz rawlist [ <name> ] dump raw device info (all or <name> only)
fritz get-switch <name> print switch state
fritz get-temp <name> print temperature
control
fritz switch <name> <on|off> set switch
EOF
}
##############################################################################
my $mode = shift;
$ua = LWP::UserAgent->new;
# print help
if (defined($mode) && ($mode eq "h" || $mode eq "-h" || $mode eq "help")) {
print_help();
exit;
}
# set host & passwd
if (defined($mode) && ($mode eq "passwd" || $mode eq "host")) {
my $value = shift;
$cfg->param($mode, $value);
exit;
}
# serialize
my $lockmgr = LockFile::Simple->make( -autoclean => 1,
-stale => 1,
-wfunc => undef);
my $lock = $lockmgr->lock($ENV{HOME} . "/.fritz.one") || die "can't get lock";
# start talking to fritzbox
login();
get_devlist();
# print device list
if (!defined($mode) || $mode eq "devlist" || $mode eq "list") {
print_devlist();
exit;
}
if ($mode eq "rawlist" || $mode eq "raw") {
my $dev = shift;
if (defined($dev) && defined($devlist->{'device'}->{$dev})) {
print Dumper($devlist->{'device'}->{$dev});
} else {
print Dumper($devlist);
}
exit;
}
# operate switch
if ($mode eq "switch" || $mode eq "sw") {
my $switch = shift;
my $state = shift;
die "missing switch args"
if !defined($switch) || !defined($state);
set_switch($switch, $state);
exit;
}
# query sensors
if ($mode eq "get-switch" || $mode eq "get-sw") {
my $switch = shift;
die "missing get-switch args"
if !defined($switch);
get_switch($switch);
exit;
}
if ($mode eq "get-temp") {
my $sensor = shift;
die "missing get-temp args"
if !defined($sensor);
get_temp($sensor);
exit;
}
# Huh?
die "unknown command: $mode (try help)";
|