-
Notifications
You must be signed in to change notification settings - Fork 57
/
hm_autoconf
executable file
·618 lines (527 loc) · 21.3 KB
/
hm_autoconf
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#!/bin/tclsh
set LOCKFILE /var/hm_autoconf.lock
if {[lsearch $argv "debug"] < 0} {
close stdout
open "/tmp/hm_autoconf_[pid].log" {WRONLY CREAT}
puts "[exec date]"
}
#if {[lsearch $argv "test"] < 0} {puts "test mode w/o test argument. exiting."; exit}
proc get_lock {lockfile} {
set has_lock 0
for { set i 0 } { $i < 20 } { incr i } {
catch {
set fd [open $lockfile {RDWR EXCL CREAT}]
puts $fd [pid]
close $fd
set has_lock 1
} errorMsg
if { $has_lock } { break }
puts $errorMsg
puts "Waiting to get lock"
after 5000
}
if { ! $has_lock } {
puts "timeout trying to get lock"
exit
}
if { $i } { puts "Got lock" }
}
proc release_lock {lockfile} {
file delete $lockfile
}
get_lock $LOCKFILE
if { [catch {
load tclrega.so
load tclrpc.so
# List of default functions (german "Gewerke") that will be automatically generated
set FUNCTIONLIST {
LIGHT funcLight
HEATING funcHeating
CLIMATECONTROL funcClimateControl
WEATHER funcWeather
ENVIRONMENT funcEnvironment
SECURITY funcSecurity
LOCK funcLock
BUTTON funcButton
CENTRAL funcCentral
ENERGY funcEnergy
}
# List of default rooms that will be automatically generated
set ROOMLIST {
"roomLivingRoom"
"roomKitchen"
"roomBedroom"
"roomChildrensRoom1"
"roomChildrensRoom2"
"roomOffice"
"roomBathroom"
"roomGarage"
"roomHWR"
"roomGarden"
"roomTerrace"
}
# Mapping of channel type to default function for the channel
array set CHANNEL_FUNCTIONS {
BLIND LIGHT
JALOUSIE LIGHT
DIMMER LIGHT
VIRTUAL_DIMMER LIGHT
KEYMATIC LOCK
KEY BUTTON
ROTARY_HANDLE_SENSOR LOCK
SHUTTER_CONTACT LOCK
SWITCH LIGHT
VIRTUAL_KEY CENTRAL
CENTRAL_KEY CENTRAL
WEATHER WEATHER
WINMATIC LOCK
CLIMATECONTROL_REGULATOR CLIMATECONTROL
CLIMATECONTROL_VENT_DRIVE CLIMATECONTROL
SMOKE_DETECTOR SECURITY
SMOKE_DETECTOR_TEAM SECURITY
DDC LOCK
SENSOR LOCK
RAINDETECTOR WEATHER
RAINDETECTOR_HEAT WEATHER
CLIMATECONTROL_RT_TRANSCEIVER HEATING
}
# Mapping of device type to default function for the channels of this device
array set DEVICE_FUNCTIONS {
HM-CCU-1 CENTRAL
}
proc array_clear {name} {
upvar $name arr
foreach key [array names arr] {
unset arr($key)
}
}
proc conf_device_HM-RCV-50 { url dev_id desc_var } {
rega "dom.GetObject($dev_id).ReadyConfig(true);"
puts "ReadyConfig($dev_id)"
}
proc conf_device_HMW-RCV-50 { url dev_id desc_var } {
rega "dom.GetObject($dev_id).ReadyConfig(true);"
puts "ReadyConfig($dev_id)"
}
proc conf_device_HM-CCU-1 { url dev_id desc_var } {
rega "dom.GetObject($dev_id).ReadyConfig(true);"
puts "ReadyConfig($dev_id)"
}
proc conf_channel_POWER { url ch_id desc_var } {
upvar $desc_var desc
rega "dom.GetObject($ch_id).Name(\"Zentralennetzteil\")"
}
proc conf_channel_SABOTAGE { url ch_id desc_var } {
upvar $desc_var desc
rega "dom.GetObject($ch_id).Name(\"Sabotagekontakt\")"
}
proc conf_value_WEATHER.RAIN_COUNTER { url ch_id ch_desc_var value_desc_var} {
create_rain_calculation $ch_id
}
proc conf_value_DISPLAY.SERVICE_COUNT { url ch_id ch_desc_var value_desc_var} {
create_sysvar_to_value_prg $ch_id SERVICE_COUNT 41 "Servicemeldungen anzeigen"
}
proc conf_value_DISPLAY.ALARM_COUNT { url ch_id ch_desc_var value_desc_var} {
create_sysvar_to_value_prg $ch_id ALARM_COUNT 40 "Alarmmeldungen anzeigen"
}
proc create_objects {collection enum_type names} {
set ids [rega "dom.GetObject($collection).EnumIDs()"]
set i 0
foreach name $names {
set id [lindex $ids $i]
if { "$id" == "" } {
#create object first
set script "\
var new_id = -1;\n\
object o = dom.CreateObject( OT_ENUM, \"new object\" );\n\
if( o ) \{\n\
o.EnumType( $enum_type );\n\
boolean res = dom.GetObject( $collection ).Add( o );\n\
if( res ) \{\n\
new_id = o.ID();\n\
\} else \{\n\
dom.DeleteObject( o.ID() );\n\
\}\n\
\}\
"
#puts "Executing $script"
array set r [rega_script $script]
set id $r(new_id)
if { $id < 0 } {
puts "Error creating object"
}
}
rega "dom.GetObject($id).Name(\"[lindex $names $i]\")"
rega "dom.GetObject($id).Enabled(true)"
incr i
}
return [lsort [rega "dom.GetObject($collection).EnumUsedIDs()"]]
}
proc create_functions {} {
global FUNCTIONLIST
for { set i 1 } { $i < [llength $FUNCTIONLIST] } { incr i 2 } {
lappend names [lindex $FUNCTIONLIST $i]
}
return [create_objects ID_FUNCTIONS etFunction $names]
}
proc create_rooms {} {
global ROOMLIST
return [create_objects ID_ROOMS etRoom $ROOMLIST]
}
proc create_sysvars {} {
rega_script {
object sv=dom.CreateObject(OT_ALARMDP, "${sysVarAlarmZone1}");
dom.GetObject(ID_SYSTEM_VARIABLES).Add(sv.ID());
sv.DPInfo("${sysVarAlarmZone1Msg}");
sv.ValueUnit("");
sv.ValueType( ivtBinary );
sv.ValueSubType( istAlarm );
sv.ValueName1("${sysVarAlarmZone1Triggered}");
sv.ValueName0("${sysVarAlarmZone1NotTriggered}");
sv.AlType(atSystem);
sv.AlArm(true);
sv=dom.GetObject(ID_PRESENT);
dom.GetObject(ID_SYSTEM_VARIABLES).Add(sv.ID());
sv.Name("${sysVarPresence}");
sv.DPInfo("${sysVarPresenceMsg}");
sv.ValueUnit("");
sv.ValueName1("${sysVarPresencePresent}");
sv.ValueName0("${sysVarPresenceNotPresent}");
sv=dom.GetObject(40);
dom.GetObject(ID_SYSTEM_VARIABLES).Add(sv.ID());
sv.Name("${sysVarAlarmMessages}");
sv.DPInfo("${sysVarAlarmMessagesCount}");
sv.Internal(0);
sv=dom.GetObject(41);
dom.GetObject(ID_SYSTEM_VARIABLES).Add(sv.ID());
sv.Name("${sysVarServiceMessages}");
sv.DPInfo("${sysVarServiceMessagesCount}");
sv.Internal(0);
}
}
proc create_sysvar_to_value_prg {ch_id value sv_id prg_name} {
set script "string ch_id=\"$ch_id\";\nstring value_name=\"$value\";\nstring sv_id=$sv_id;\nstring prg_name=\"$prg_name\";\n"
append script {
object sv=dom.GetObject(sv_id);
object channel=dom.GetObject(ch_id);
object dp=channel.DPByHssDP(value_name);
object prgs = dom.GetObject( ID_PROGRAMS );
object prg = dom.CreateObject( OT_PROGRAM );
prg.Name( prg_name );
prg.Internal(true);
prgs.Add( prg.ID() );
object rule=prg.Rule();
rule.RuleOperatorType(2);
object cond=rule.RuleAddCondition();
cond.CndOperatorType(2);
object s_cond=cond.CndAddSingle();
s_cond.OperatorType(1);
s_cond.ConditionType(9);
s_cond.ConditionType2(13);
s_cond.LeftValType(ivtSystemId);
s_cond.LeftVal(sv.ID());
s_cond.RightVal1ValType(ivtInteger);
s_cond.RightVal1(0);
s_cond.RightVal2ValType(ivtInteger);
s_cond.RightVal2(0);
object dest=rule.RuleDestination();
object action=dest.DestAddSingle();
action.DestinationParam(ivtObjectId);
action.DestinationChannel(ch_id);
action.DestinationDP(dp.ID());
action.DestinationValueType(ivtSystemId);
action.DestinationValue(sv.ID());
prg.Active(true);
dom.RTUpdate(0);
}
rega_script $script
}
proc create_rain_calculation {ch_id} {
##
# Führt ein HomeMatic Script (ise Script, ReGa Script) aus und liefert dessen Ausgabe zurück
##
proc _rega_exec_ { script } {
set result {}
catch {
array set scriptResult [rega_script $script]
set result $scriptResult(STDOUT)
}
return $result
}
##
# Liefert die Id des Datenpunkts "RAIN_COUNTER"
##
proc _getRainCounterId_ { channelId } {
set script "var channelId = \"$channelId\";"
append script {
var channel = dom.GetObject(channelId);
var rainCounter = channel.DPByHssDP("RAIN_COUNTER");
Write(rainCounter.ID());
}
return [_rega_exec_ $script]
}
##
# Erzeugt eine Systemvariable, bindet diese an einen Kanal und liefert ihre Id zurück
##
proc _create_sv_ {name info channelId} {
set script "var name = \"$name\";\n"
append script "var info = \"$info\";\n"
append script "var channelId = \"$channelId\";\n"
append script {
var channel = dom.GetObject(channelId);
var sv = dom.CreateObject(OT_VARDP, name);
dom.GetObject(ID_SYSTEM_VARIABLES).Add(sv.ID());
sv.DPInfo(info);
sv.ValueUnit("mm");
sv.ValueType( ivtFloat );
sv.ValueSubType( istGeneric );
sv.ValueMin(0.0);
sv.ValueMax(1000.0);
sv.Channel(channelId);
channel.DPs().Add(sv.ID());
dom.RTUpdate(0);
Write(sv.ID());
}
return [_rega_exec_ $script]
}
##
# Erstellt das Programm "Regentagesmenge zurücksetzen"
##
proc _create_reset_program_ { rainTodayId rainYesterdayId } {
set resetScript [string map "\t \\t \n \\n" "var rainToday = dom.GetObject($rainTodayId);\n"]
append resetScript [string map "\t \\t \n \\n" "var rainYesterday = dom.GetObject($rainYesterdayId);\n"]
append resetScript [string map "\t \\t \n \\n" {
if ( (rainToday) && (rainYesterday) )
{
rainYesterday.State(rainToday.Value());
rainToday.State(0.0);
}
}]
set script "var _script = \"$resetScript\";\n";
append script {
var DP_VALUE_CHECK_UPDATE = 13;
var TIMER_DAILY = 9;
var NOT_SET = 0;
var programs = dom.GetObject(ID_PROGRAMS);
var program = dom.CreateObject(OT_PROGRAM);
program.Name("Regentagesmenge zurücksetzen");
program.Internal(true);
programs.Add(program.ID());
var rule = program.Rule();
rule.RuleOperatorType(OPERATOR_NONE);
var condition = rule.RuleAddCondition();
condition.CndOperatorType(OPERATOR_NONE);
var timeModule = dom.CreateObject(OT_CALENDARDP);
dom.GetObject(ID_CALENDARDPS).Add(timeModule);
timeModule.TimerType(TIMER_DAILY);
timeModule.Time("2000-01-01 07:30:00");
timeModule.CalDuration(NOT_SET);
timeModule.CalRepeatTime(NOT_SET);
timeModule.Weekdays(NOT_SET);
timeModule.Period(NOT_SET);
timeModule.CalRepetitionValue(NOT_SET);
timeModule.Begin("2000-01-01 00:00:00");
timeModule.End(NOT_SET);
timeModule.CalRepetitionCount(NOT_SET);
timeModule.SunOffsetType(NOT_SET);
var singleCondition = condition.CndAddSingle();
singleCondition.OperatorType(OPERATOR_NONE);
singleCondition.ConditionType(3);
singleCondition.ConditionType2(DP_VALUE_CHECK_UPDATE);
singleCondition.LeftValType(ivtCurrentDate);
singleCondition.RightVal1ValType(ivtObjectId);
singleCondition.RightVal1(timeModule.ID());
var destination = rule.RuleDestination();
var action = destination.DestAddSingle();
action.DestinationParam(ivtString);
action.DestinationValueType(ivtString);
action.DestinationValue(_script);
program.Active(true);
dom.RTUpdate(0);
}
rega_script $script
}
##
# Erstellt das Programm zur Aktualisieren der Regentagesmenge
##
proc _create_update_program_ { rainTodayId rainYesterdayId channelId rainCounterId } {
set updateScript [string map "\t \\t \n \\n" "var rainToday = dom.GetObject($rainTodayId);\n"]
append updateScript [string map "\t \\t \n \\n" "var rainYesterday = dom.GetObject($rainYesterdayId);\n"]
append updateScript [string map "\t \\t \n \\n" "var rainCounter = dom.GetObject($rainCounterId);\n"]
append updateScript [string map "\t \\t \n \\n" {
if ( (rainToday) && (rainYesterday) && (rainCounter) )
{
if (rainCounter.LastTimestamp())
{
var diff = rainCounter.Value() - rainCounter.LastValue();
if ( diff < 0.0)
{
diff = diff + rainCounter.ValueMax();
}
rainToday.State(rainToday.State() + diff);
}
else
{
rainToday.State(0.0);
rainYesterday.State(0.0);
}
}
}]
set script "var rainCounterId = $rainCounterId;\n"
append script "var channelId = $channelId;\n"
append script "var _script = \"$updateScript\";\n"
append script {
var DP_VALUE_CHECK_UPDATE = 13;
var programs = dom.GetObject(ID_PROGRAMS);
var program = dom.CreateObject(OT_PROGRAM);
program.Name("Regentagesmenge aktualisieren");
program.Internal(true);
programs.Add(program.ID());
var rule = program.Rule();
rule.RuleOperatorType(OPERATOR_NONE);
var condition = rule.RuleAddCondition();
condition.CndOperatorType(OPERATOR_NONE);
var singleCondition = condition.CndAddSingle();
singleCondition.OperatorType(OPERATOR_NONE);
singleCondition.ConditionType(9);
singleCondition.ConditionType2(DP_VALUE_CHECK_UPDATE);
singleCondition.LeftValType(ivtObjectId);
singleCondition.LeftVal(rainCounterId);
singleCondition.RightVal1ValType(ivtFloat);
singleCondition.RightVal1(0.0);
singleCondition.RightVal2ValType(ivtFloat);
singleCondition.RightVal2(0.0);
singleCondition.ConditionChannel(channelId);
var destination = rule.RuleDestination();
var action = destination.DestAddSingle();
action.DestinationParam(ivtString);
action.DestinationValueType(ivtString);
action.DestinationValue(_script);
program.Active(true);
_script = "";
}
rega_script $script
}
catch {
set rainCounterId [_getRainCounterId_ $ch_id]
set rainTodayId [_create_sv_ "\${sysVarRainToday}" "\${sysVarRainTodayDescr}" $ch_id]
set rainYesterdayId [_create_sv_ "\${sysVarRainYesterday}" "\${sysVarRainYesterdayDescr}" $ch_id]
if { [catch { _create_reset_program_ $rainTodayId $rainYesterdayId } errMsg]} then {
exec -- logger -t hm_deldev -p debug "_create_reset_program_ $errMsg"
}
if { [catch { _create_update_program_ $rainTodayId $rainYesterdayId $ch_id $rainCounterId} errMsg]} then {
exec -- logger -t hm_deldev -p debug "_create_update_program_ $errMsg"
}
}
}
array set result [rega_script {
string if_id;
string ifaces;
foreach(if_id, dom.GetObject(ID_INTERFACES).EnumUsedIDs()){
object o=dom.GetObject(if_id);
ifaces = ifaces # if_id # " {" # o.InterfaceUrl() # " " # o.Name() # "} ";
}
string dev_id;
string devs;
foreach(dev_id, dom.GetObject(ID_DEVICES).EnumUsedIDs()){
object dev=dom.GetObject(dev_id);
if( (dev.Interface() != 65535) && (dev.Address() != "") && dev.CreatingCompleted() ) {
if( dev.MetaData("AUTOCONF")==null) {
devs = devs # " " # dev_id # " {" # dev.Address() # " " # dev.Interface() # " {";
string channel_id;
foreach(channel_id, dev.Channels().EnumIDs()){
devs = devs # " " # channel_id # " " # dom.GetObject(channel_id).Address();
}
devs = devs # "}}";
}
}
}
string functions=dom.GetObject(ID_FUNCTIONS).EnumUsedIDs();
string rooms=dom.GetObject(ID_ROOMS).EnumUsedIDs();
string sysvars=dom.GetObject(ID_SYSTEM_VARIABLES).EnumUsedIDs();
}]
array set ifmap $result(ifaces)
array set devmap $result(devs)
set ise_functions [lsort $result(functions)]
if { [llength $ise_functions] == 0 } {
puts "No functions defined, creating default functions"
set ise_functions [create_functions]
}
if { [llength $result(rooms)] == 0 } {
puts "No rooms defined, creating default rooms"
create_rooms
}
if { [llength $result(sysvars)] == 0 } {
puts "No system variables defined, creating default system variables"
create_sysvars
}
set i 0
foreach function_id $ise_functions {
if { $i >= [llength $FUNCTIONLIST] } break;
set FUNCTIONMAP([lindex $FUNCTIONLIST $i]) $function_id
incr i 2
}
# We need a catch because OSRAM Lightify causes an error (/tmp/hm_autoconf_xxx.log) because a list must have an even number of elements.
# set dev $devmap($ise_dev_id) produces something like that:
# OL-SurfaceTW 0 1008 { 19913 OL-SurfaceTW 0:0 19914 OL-SurfaceTW 0:1}
#
# It has to be like this:
# JEQ0067507 1007 { 1655 JEQ0067507:1}
catch {
foreach ise_dev_id [array names devmap] {
set dev $devmap($ise_dev_id)
set dev_address [lindex $dev 0]
set if_id [lindex $dev 1]
array_clear channels
array set channels [lindex $dev 2]
set if_info $ifmap($if_id)
set url [lindex $if_info 0]
array_clear dev_descr
array set dev_descr [xmlrpc $url getDeviceDescription $dev_address]
set procname "conf_device_$dev_descr(TYPE)"
if {[llength [info procs $procname]]} {
if [catch { $procname $url $ise_dev_id dev_descr } errMsg] {
puts "$procname $errMsg"
}
}
foreach ise_ch_id [array names channels] {
set ch_address $channels($ise_ch_id)
array_clear ch_descr
array set ch_descr [xmlrpc $url getDeviceDescription $ch_address]
if [info exist DEVICE_FUNCTIONS($dev_descr(TYPE))] {
catch {
rega "dom.GetObject($FUNCTIONMAP($DEVICE_FUNCTIONS($dev_descr(TYPE)))).Add($ise_ch_id)"
}
}
if [info exist CHANNEL_FUNCTIONS($ch_descr(TYPE))] {
catch {
rega "dom.GetObject($FUNCTIONMAP($CHANNEL_FUNCTIONS($ch_descr(TYPE)))).Add($ise_ch_id)"
}
}
set procname "conf_channel_$ch_descr(TYPE)"
if {[llength [info procs $procname]]} {
if [catch { $procname $url $ise_ch_id ch_descr } errMsg] {
puts "$procname $errMsg"
}
}
if {[llength [info procs "conf_value_$ch_descr(TYPE).*"]]} {
array_clear values_descr
array set values_descr [xmlrpc $url getParamsetDescription $ch_address "VALUES"]
foreach value [array names values_descr] {
set procname "conf_value_$ch_descr(TYPE).$value"
if {[llength [info procs $procname]]} {
array set value_descr $values_descr($value)
if [catch { $procname $url $ise_ch_id ch_descr value_descr } errMsg] {
puts "$procname $errMsg"
}
}
}
}
}
rega "dom.GetObject($ise_dev_id).AddMetaData(\"AUTOCONF\");"
}
}
} errorMsg ]} {
puts "Error: $errorMsg"
}
release_lock $LOCKFILE