Last active
September 21, 2023 07:15
-
-
Save vbargsten/e4d050557f4b621601bdb5ec745efec6 to your computer and use it in GitHub Desktop.
Homematic script to copy weekly programs from one device to multiple others
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string srcDevName = "WT-OGV-Wohnung"; | |
string destDevNames = "HKV-OGV-Wohnzimmer" # "\t" # "HKV-OGV-Küche" # "\t" # "HKV-OGV-Bad"; | |
string paramNames = "P1_\t" # "P2_\t" # "P3_"; | |
string memberStartStr = "<member><name>"; | |
string memberNameEndStr = "</name><value>"; | |
string memberEndStr = "</value></member>"; | |
string typeList = "boolean\ti4\tdouble"; | |
object dev = devices.Get(srcDevName); | |
if (!dev) { | |
WriteLine("dev" # " not found"); | |
quit; | |
} | |
string srcParamSet; | |
string destParamSet; | |
! srcParamSet = xmlrpc.GetParamset(dev.Interface(),dev.Address(),"MASTER"); | |
! srcParamSet = xmlrpc.GetParamset(dev.Interface(), dev.Address()#":1", "MASTER").Replace("<member>", "\n<member>").Replace("</struct>", "\n</struct>"); | |
! Get full parameter set as xml | |
srcParamSet = xmlrpc.GetParamset(dev.Interface(), dev.Address()#":1", "MASTER"); | |
integer idx_name_start; | |
integer idx_name_end; | |
string curName; | |
integer remainingLen; | |
while(true) { | |
! Find index of parameter data | |
idx_name_start = srcParamSet.Find(memberStartStr); | |
!WriteLine(idx_name_start); | |
if (idx_name_start < 0) { | |
! WriteLine(memberStartStr # " not found"); | |
break; | |
} | |
remainingLen = srcParamSet.Length(); | |
remainingLen = remainingLen - idx_name_start; | |
remainingLen = remainingLen - memberStartStr.Length(); | |
srcParamSet = srcParamSet.Substr(idx_name_start + memberStartStr.Length(), remainingLen); | |
idx_name_end = srcParamSet.Find(memberNameEndStr); | |
if (idx_name_end < 0) { | |
WriteLine("Missing end tags " # memberNameEndStr # " in:\n " # srcParamSet); | |
break; | |
} | |
curName = srcParamSet.Substr(0, idx_name_end); | |
remainingLen = srcParamSet.Length(); | |
remainingLen = remainingLen - idx_name_end; | |
remainingLen = remainingLen - memberNameEndStr.Length(); | |
srcParamSet = srcParamSet.Substr(idx_name_end + memberNameEndStr.Length(), remainingLen); | |
string checkName; | |
boolean selectParam = false; | |
foreach(checkName, paramNames) { | |
if (curName.Contains(checkName)) { | |
! WriteLine(curName); | |
selectParam = true; | |
break; | |
} | |
} | |
if (selectParam == false) { | |
continue; | |
} | |
selectParam = false; | |
string curType; | |
string curVal; | |
foreach(curType, typeList) { | |
if (srcParamSet.StartsWith("<" # curType # ">") == false) { | |
continue; | |
} | |
! WriteLine("type is " # curType); | |
integer idxValueEnd = srcParamSet.Find("</" # curType # ">" # memberEndStr); | |
if (idxValueEnd < 0) { | |
WriteLine("End tag </" # curType # ">" # memberEndStr # " not found"); | |
quit; | |
} | |
! WriteLine("idxValueEnd is " # idxValueEnd); | |
integer curValLen = idxValueEnd; | |
curValLen = curValLen - curType.Length(); | |
curValLen = curValLen - 2; | |
! WriteLine("curValLen is " # curValLen); | |
curVal = srcParamSet.Substr( curType.Length() + 2, curValLen ); | |
! WriteLine("curVal is " # curVal); | |
! Cut off the type, must be at least <x>y</x>=3+1+4 chars | |
remainingLen = srcParamSet.Length(); | |
remainingLen = remainingLen - idxValueEnd; | |
remainingLen = remainingLen - curType.Length(); | |
remainingLen = remainingLen - 3; | |
remainingLen = remainingLen - memberEndStr.Length(); | |
srcParamSet = srcParamSet.Substr(idxValueEnd + curType.Length() + 3 + memberEndStr.Length(), remainingLen); | |
selectParam = true; | |
break; | |
} | |
if (selectParam == false) { | |
continue; | |
} | |
string rpcData = " {"#curName#" {"#curType#" "#curVal#"}}"; | |
destParamSet = destParamSet + rpcData; | |
! WriteLine( rpcData ); | |
} | |
WriteLine( "Copying the following parameters: " # destParamSet ); | |
string destDevName; | |
foreach(destDevName, destDevNames) { | |
string stderr = ""; | |
string stdout = ""; | |
object oOBJ= channels.Get (destDevName#":1"); | |
if (!oOBJ) { | |
WriteLine("destination channel " # destDevName # ":1 not found"); | |
quit; | |
} | |
object oIFace= dom.GetObject (oOBJ.Interface () ); | |
if (!oIFace) { | |
WriteLine("destination interface " # oOBJ.Interface () # " not found"); | |
quit; | |
} | |
WriteLine(oOBJ); | |
string cmdTcl= ""; | |
cmdTcl = cmdTcl # "puts [xmlrpc "; | |
cmdTcl = cmdTcl # oIFace.InterfaceUrl(); | |
cmdTcl = cmdTcl # "/ putParamset [list string \\\"" # oOBJ.Address() # "\\\"] [list string \\\"MASTER\\\"] [list struct \\\""; | |
cmdTcl = cmdTcl # destParamSet # "\\\"]]"; | |
string cmd = "/bin/sh -c 'echo \"load tclrpc.so; " # cmdTcl # "\" |tclsh'"; | |
WriteLine("Executing the following command: " # cmd); | |
system.Exec(cmd,&stdout,&stderr); | |
WriteLine(stdout); | |
WriteLine(stderr); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment