Join the "Danske ZenseHome Brugere" Facebook group

Rene Olskjær har lavet en fin PWA til at køre på LAMP på en server - se på https://github.com/Rene-Olskjaer/zense.olskjaer.dk


Dokumentation fra Zensehome

  File Modified
ZIP Archive zense.olskjaer.dk-master.zip 21-03-2019 by Normann P. Nielsen
Microsoft Word Document Extern kommunikation med Zense enheder.docx 31-01-2019 by Normann P. Nielsen
PDF File Zensehome-PC-box-ASCII-API-UK-v1.8.pdf 11-01-2019 by Normann P. Nielsen
ZIP Archive Android App Source.zip 10-12-2018 by Normann P. Nielsen
PDF File Zense PC-boks Ascii interface v1.7.pdf 02-04-2016 by Normann P. Nielsen
PDF File Ekstern adgang til PC-boks med LAN.pdf 02-04-2016 by Normann P. Nielsen

expect - programmed dialogue with interactive programs

Metoden vi fra en Ubuntu sender info til vores ZenseHome PC Boks (som har ID 32010 og sidder på IP 10.0.0.110) over det lokale net.

For Ubuntu 18.04, add a global repo:

sudo add-apt-repository universe


Links

http://www.admin-magazine.com/Articles/Automating-with-Expect-Scripts (læs specielt Expect Caveats texten)

https://wiki.tcl-lang.org/page/Expect+Hints+and+Tips

http://stackoverflow.com/questions/7789710/expect-script-to-automate-telnet-login

https://www.pantz.org/software/expect/expect_examples_and_tips.html

https://www.mkssoftware.com/docs/man1/expect.1.asp

Expect Scripts

Grundscriptet

Dette er "grundscriptet, som søger for at logge ind, agere og logge ud - jeg er ikke 100% sikker på om sleeps faktisk er nødvendige....:

Husker man ikke at lave logout eller fejler scriptet inden, da man få output fra forrige run ved næste run, og i det hele taget lidt "underligheder" (sad)

Jeg oplever engang imellem ved brug af API at min box bare "dør" - eller at det tager længe før den vil æde en kommando igen...(sad)

#!/usr/bin/expect
spawn telnet 10.0.0.110 10001
expect "'^]'."
send ">>Login 32010<<\r"
expect ">>Login Ok<<"
sleep .5;

......
......
......

sleep .5;
send ">>Logout<<\r"
expect ">>Logout Ok<<"
exit

Brug:

exp_internal 1 i koden for at se hvad der egentlig sker hele velen igennem

log_user 0 for at supresse output

log_file <filename> for at logge til en fil


List alle forbrugsregistrende devices

getdevices.exp
#!/usr/bin/expect
spawn telnet 10.0.0.110 10001
expect "'^]'."
send ">>Login 32010<<\r"
expect ">>Login Ok<<"
sleep .5;
send ">>Get Devices<<\r"
sleep .5;
send ">>Logout<<\r"
expect ">>Logout Ok<<"
exit

Scriptets output

batman:Zensehome bnp$ ./getdevices.exp
spawn telnet 10.0.0.110 10001
Trying 10.0.0.110...
Connected to 10.0.0.110.
Escape character is '^]'.
>>Login 32010<<
>>Login Ok<<
>>Get Devices<<
>>Get Devices 18324, 10553, 24955, 19334, 16469, 16476, 17965, 17934, 17951, 27544, 24963, 24958, 24953, 10576, 10598, 24965, 24956, 50371, 24952, 24957, 10604, 10170, 6769, 24944, 24951, 27545, 27540, 27541, 24960, 24946, 24943, 24947, 24941, 24948, >>Logout<<
24949, 27538, 24945, 27542, 24954, 27546, 24720, 16478, 17933, 58739, 24942, 24959, 24718, 24950, 24717, 24719, 16682, 24716, 27539<<
>>Logout Ok<<
batman:Zensehome bnp$


Tænd et Device

Her tænder vi for 10604 som er en loftslampe hos mig.

turnon.exp
 #!/usr/bin/expect
spawn telnet 10.0.0.110 10001
expect "'^]'."
send ">>Login 32010<<\r"
expect ">>Login Ok<<"
sleep .5;
send ">>Set 10604 1<<"
sleep .5;
send ">>Logout<<\r"
expect ">>Logout Ok<<"
exit

Sluk et device

Her slukker vi for samme:

#!/usr/bin/expect
spawn telnet 10.0.0.110 10001
expect "'^]'."
send ">>Login 32010<<\r"
expect ">>Login Ok<<"
sleep .1;
send ">>Set 10604 0<<"
sleep .1;
send ">>Logout<<\r"
expect ">>Logout Ok<<"
exit


Error handling

November 2018 - Dette er en update til ovenstående, efter at have arbejde med scriptet - dette script har error handling og passende output.

#!/usr/bin/expect
log_user 0
spawn telnet 10.0.0.110 10001
expect {
     "'^]'" { send ">>Login 32010<<\r"; }
     "refused" { catch {exp_close}; send_user "Connection refused\n";exp_wait; exit }
     "No route" { catch {exp_close}; send_user "No Route to host\n";exp_wait; exit }
     "eof" { catch {exp_close}; send_user "EOF\n";exp_wait; exit }
}
sleep .1;
send ">>Set 10604 1<<";
sleep .1;
send ">>Logout<<\r";
sleep .1;
close;
send_user "Success\n";
exit

December 2018 - Dette er en toogle metode - dvs. der læses og skiftes status

#!/usr/bin/expect
log_user 0
spawn telnet 10.0.0.110 10001
expect {
     "'^]'" { send ">>Login 32010<<\r"; }
     "refused" { catch {exp_close}; send_user "Connection refused\n";exp_wait; exit }
     "No route" { catch {exp_close}; send_user "No Route to host\n";exp_wait; exit }
     "eof" { catch {exp_close}; send_user "EOF\n";exp_wait; exit }
}
sleep .1;
send ">>Get 10604<<\r";
sleep .1;
expect {
     "'^]'" { send ">>Login 32010<<\r"; }
     "refused" { catch {exp_close}; send_user "Connection refused\n";exp_wait; exit }
     "No route" { catch {exp_close}; send_user "No Route to host\n";exp_wait; exit }
     "eof" { catch {exp_close}; send_user "EOF\n";exp_wait; exit }
}
sleep .1;
send ">>Get 10604<<\r";
expect {
     ">>Get 100<<" { send ">>Set 10604 0<<\r"; exit }
     ">>Get 0<<" { send ">>Set 10604 1<<\r"; exit }
     ">>Get Timeout<<" { catch {exp_close}; send_user "Timeout for Get\n";exp_wait; exit }
}
sleep .1;
send ">>Logout<<\r";
sleep .1;
close;
send_user "Success\n";
exit