-
Notifications
You must be signed in to change notification settings - Fork 15
/
98_Robotan.pm
417 lines (337 loc) · 10.6 KB
/
98_Robotan.pm
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package main;
use strict;
use warnings;
use HttpUtils;
use JSON;
my $Robotan_hasJSON = 1;
my $queueErrors = 0;
sub Robotan_Initialize($) {
my ($hash) = @_;
$hash->{NOTIFYDEV} = "global";
$hash->{NotifyFn} = "Robotan_Notify";
$hash->{GetFn} = "Robotan_Get";
$hash->{SetFn} = "Robotan_Set";
$hash->{DefFn} = "Robotan_Define";
$hash->{AttrList} = "General_Mowing_Days General_Border_Mowing_Days " .$readingFnAttributes;
eval "use JSON";
$Robotan_hasJSON = 0 if($@);
RemoveInternalTimer($hash);
}
###################################
sub Robotan_Define($$) {
my ($hash, $def) = @_;
return "install JSON to use Robotan" if( !$Robotan_hasJSON );
my @a = split("[ \t][ \t]*", $def);
return "Wrong syntax: use define <name> Robotan <ip-address> <interval>" if(int(@a) != 4);
my $name = $a[0];
my $url=$a[2];
my $interval=$a[3];
$hash->{NAME} = $name;
$hash->{INTERVAL} = $interval if $interval;
$hash->{URL} = $url if $url;
$hash->{httpQueue} = [];
my $defptr = $modules{Robotan}{defptr}{$hash->{URL}};
return "Robotan $hash->{URL} already defined as '$defptr->{NAME}'" if( defined($defptr) && $defptr->{NAME} ne $name);
$modules{Robotan}{defptr}{$hash->{URL}} = $hash;
readingsSingleUpdate($hash, 'state', 'initialized', 1 );
RemoveInternalTimer($hash, "Robotan_GetStatus");
my $nexttimer = gettimeofday()+10;
InternalTimer($nexttimer, "Robotan_GetStatus", $hash);
$hash->{NEXT_UPDATE} = localtime($nexttimer);
return undef;
}
###################################
sub Robotan_Notify {
my ($hash,$dev) = @_;
return if($dev->{NAME} ne "global");
return if(!grep(m/^INITIALIZED|REREADCFG$/, @{$dev->{CHANGED}}));
return undef;
}
###################################
sub Robotan_Undefine($$) {
my ($hash, $arg) = @_;
delete $modules{Robotan}{defptr}{$hash->{IP}};
return undef;
}
###################################
sub Robotan_Attr {
my ($cmd, $name, $attrName, $attrVal) = @_;
if( $cmd eq "set" ) {
$attr{$name}{$attrName} = $attrVal;
return $attrName ." set to ". $attrVal if( $init_done );
}
return;
}
###################################
sub Robotan_GetStatus($) {
my ($hash) = @_;
my $name = $hash->{NAME};
RemoveInternalTimer($hash, "Robotan_GetStatus");
my $interval = $hash->{INTERVAL};
my $nexttimer = gettimeofday()+$interval;
InternalTimer($nexttimer, "Robotan_GetStatus", $hash, 1);
$hash->{NEXT_UPDATE} = localtime($nexttimer);
return if( IsDisabled($name) );
Log3 $name, 3, "$name: getting status";
my $cmd = "/json";
Log3 $name, 4, "$name: adding $cmd to http queue...";
unshift(@{$hash->{httpQueue}}, $cmd);
&Robotan_ProcessHttpQueue($hash);
}
###################################
sub Robotan_ParseStatus($$) {
my ($hash, $data) = @_;
my $name = $hash->{NAME};
my $decoded = eval { decode_json($data) };
readingsBeginUpdate($hash);
foreach my $p ( keys %{$decoded} ) {
my $reading = $decoded->{$p}{name};
my $value = $decoded->{$p}{value};
$reading =~ s/[ \(\)]/_/g;
if ($reading eq "Functionality") {
if ($value gt "0") {
readingsBulkUpdate($hash, "state", "authenticated");
} else {
readingsBulkUpdate($hash, "state", "not authenticated");
}
}
if ($reading =~ /Mowing_Days/) {
my $days = "";
for (my $x=0; $x<8; $x++) {
if (($value & 2**$x) > 0) {
$days .= $x+1 . ",";
}
}
chop ($days);
$value = $days;
}
if (!$attr{$name}{General_Mowing_Days} && $reading eq "Mowing_Days") {
if ($value gt " ") {
$attr{$name}{General_Mowing_Days} = $value;
}
}
if (!$attr{$name}{General_Border_Mowing_Days} && $reading eq "Border_Mowing_Days") {
if ($value lt "0") {
$value = "0";
}
$attr{$name}{General_Border_Mowing_Days} = $value;
}
readingsBulkUpdate($hash, $reading, $value);
}
readingsEndUpdate($hash,1);
}
###################################
sub Robotan_Get($$@) {
my ($hash, $name, $cmd, @params) = @_;
my $list = 'readingsUpdate:noArg';
if( $cmd eq 'readingsUpdate' ) {
Robotan_GetStatus($hash);
return undef;
}
$list =~ s/ $//;
return "Unknown argument $cmd, choose one of $list";
}
###################################
sub Robotan_Set($$@) {
my ($hash, $name, $cmd, @params) = @_;
$hash->{".triggerUsed"} = 1;
my $list = 'Play/Pause:noArg Home/Work:noArg Spiral:noArg Straight:noArg Left:noArg Right:noArg Start_Time_1 End_Time_1 Start_Time_2 End_Time_2 Mowing_Days:multiple-strict,1,2,3,4,5,6,7 Border_Mowing_Days:multiple-strict,0,1,2,3,4,5,6,7 General_Mowing_Days:multiple-strict,1,2,3,4,5,6,7 General_Border_Mowing_Days:multiple-strict,0,1,2,3,4,5,6,7 Mow_Today:0,1';
$list =~ s/ $//;
return "Unknown argument $cmd, choose one of $list" if ($cmd eq "?");
Log3 $name, 3, "$name: setting " . $cmd . " to '" . $params[0]. "'";
if ($cmd eq "General_Mowing_Days" || $cmd eq "General_Border_Mowing_Days") {
$attr{$name}{$cmd} = $params[0];
return undef;
}
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
if ($cmd eq "Mow_Today") {
if ($wday eq "0") {
$wday = "7";
}
my $gmd = $attr{$name}{General_Mowing_Days};
my $md = ReadingsVal($name, "Mowing_Days", "");
my $gbmd = $attr{$name}{General_Border_Mowing_Days};
my $bmd = ReadingsVal($name, "Border_Mowing_Days", "");
my $mow_today = $params[0];
if ((($md !~ /$wday/) && $mow_today==0) || (($md =~ /$wday/) && $mow_today==1)) {
Log3 $name, 3, "$name: Mow_Today: Nothing to do";
return undef;
}
$gmd =~ s/$wday//;
if ($mow_today == 1) {
$gmd .= ",$wday";
}
$gmd =~ s/,,/,/;
$cmd = "Mowing_Days";
$params[0] = $gmd;
&Robotan_Set($hash, $name, $cmd, @params);
# if ((($bmd =~ /$wday/) && $mow_today==0) || (($bmd !~ /$wday/) && ($gbmd =~ /$wday/) && $mow_today==1)) {
# $gbmd =~ s/$wday//;
# if ($params[0] eq "1") {
# $gbmd .= ",$wday";
# }
# $gbmd =~ s/,,/,/;
#
# $cmd = "Border_Mowing_Days";
# $params[0] = $gbmd;
# &Robotan_Set($hash, $name, $cmd, @params);
# }
my $status = ReadingsVal($name, "Status", "0");
if (($status == 255 && $mow_today == 1) || ($status != 255 && $mow_today == 0)) {
Log3 $name, 3, "$name: sending home/work";
$cmd = "Home/Work";
$params[0] = '';
&Robotan_Set($hash, $name, $cmd, @params);
return undef;
}
}
if ($cmd =~ /Mowing_Days/) {
$params[0] = "0" unless defined $params[0];
$params[0] =~ s/^,//;
my $value = 0;
if ($params[0] gt "0") {
my @days = split(',', $params[0]);
foreach my $day (@days) {
if ($day gt "0") {
$value = $value + (2**($day-1));
}
}
}
$params[0] = $value;
}
my $param_nr=0;
if ($cmd =~ /Left/) {
$param_nr=1;
}
if ($cmd =~ /Right/) {
$param_nr=2;
}
if ($cmd =~ /Straight/) {
$param_nr=3;
}
if ($cmd =~ /Spiral/) {
$param_nr=4;
}
if ($cmd =~ /Home/) {
$param_nr=5;
}
if ($cmd =~ /Play/) {
$param_nr=6;
}
if ($cmd eq "Start_Time_1") {
$param_nr=22;
}
if ($cmd eq "End_Time_1") {
$param_nr=23;
}
if ($cmd eq "Start_Time_2") {
$param_nr=24;
}
if ($cmd eq "End_Time_2") {
$param_nr=25;
}
if ($cmd eq "Mowing_Days") {
$param_nr=20;
if ($params[0] lt "1") {
Log3 $name, 3, "$name: cannot set empty mowing days ($params[0])!";
return undef;
}
}
if ($cmd eq "Border_Mowing_Days") {
$param_nr=21;
}
$params[0]="" unless defined $params[0];
$cmd = "/cmd?".$param_nr."=".$params[0];
Log3 $name, 4, "$name: adding $cmd to http queue...";
unshift(@{$hash->{httpQueue}}, $cmd);
&Robotan_ProcessHttpQueue($hash);
RemoveInternalTimer($hash, "Robotan_GetStatus");
my $interval = $hash->{INTERVAL};
my $nexttimer = gettimeofday()+10;
InternalTimer($nexttimer, "Robotan_GetStatus", $hash, 1);
$hash->{NEXT_UPDATE} = localtime($nexttimer);
return undef;
}
###################################
sub Robotan_EvalHttp ($$$) {
my ($param, $err, $data) = @_;
my $hash = $param->{hash};
my $name = $hash->{NAME};
my $origCmd = $param->{origCmd};
my $cmd;
if ($err gt " ") {
readingsSingleUpdate($hash, "state", "error", 1);
$hash->{LAST_ERROR_MSG} = "$err";
$hash->{LAST_ERROR_TIME} = localtime(gettimeofday);
Log3 $name, 3, "$err";
}
$hash->{LAST_HTTP_CODE} = $param->{code};
$hash->{LAST_PATH} = $param->{path};
Log3 $name, 4, "$name: HTTP response code: " . $param->{code};
if ($param->{path} eq "/json") {
&Robotan_ParseStatus($hash, $data);
Log3 $name, 5, "Data: $data";
} else {
Log3 $name, 4, "$data";
}
if ($data =~ /Writing finished/sgi) {
$queueErrors = 0;
}
if ($data =~ /Writing failed/sgi) {
if (ReadingsVal($name, "Status", "0") != 255) {
if ($queueErrors == 0) {
Log3 $name, 3, "Writing failed, sending Pause, original command, Pause...";
$cmd = "/cmd?6="; # Play/Pause
unshift(@{$hash->{httpQueue}}, $cmd);
Log3 $name, 3, "Queueing $cmd";
unshift(@{$hash->{httpQueue}}, $origCmd);
Log3 $name, 3, "Queueing $origCmd";
unshift(@{$hash->{httpQueue}}, $cmd);
Log3 $name, 3, "Queueing $cmd";
} else {
Log3 $name, 3, "Won't try to write because of previous write failures - set to pause manually and change parameter";
}
} else {
Log3 $name, 3, "Writing failed despite robot seems to be charging, requeueing...";
unshift(@{$hash->{httpQueue}}, $origCmd);
Log3 $name, 3, "Queueing $origCmd";
}
$queueErrors++;
readingsSingleUpdate($hash, "state", "writing error - device not in pause/charging?", 1);
}
RemoveInternalTimer($hash, 'Robotan_ProcessHttpQueue');
InternalTimer(gettimeofday(), 'Robotan_ProcessHttpQueue', $hash );
return undef;
}
###################################
sub Robotan_ProcessHttpQueue($) {
my ($hash) = @_;
my $name = $hash->{NAME};
my $url = $hash->{URL};
my $queue_anz = scalar @{$hash->{httpQueue}};
my $cmd = pop(@{$hash->{httpQueue}});
if (!defined($cmd)) {
return "Nothing to process...";
}
Log3 $name, 4, "$name: $queue_anz commands still in http queue.";
my $uri = "http://".$url . $cmd;
Log3 $name, 3, "$name: processing queue item: $cmd";
my $param = {
url => $uri,
timeout => 20,
hash => $hash,
method => "GET",
callback => \&Robotan_EvalHttp
};
$param->{origCmd} = $cmd;
HttpUtils_NonblockingGet($param);
$queue_anz = scalar @{$hash->{httpQueue}};
Log3 $name, 4, "$name: $queue_anz commands still in http queue.";
}
###################################
1;
=pod
=begin html
Robotan module
=end html
=cut