define <name> ECMDDevice [<classname> [<parameter1> [<parameter2> [<parameter3> ... ]]]]
<classname>
.
Normally, the logical ECMDDevice is attached to the latest previously defined physical ECMD device
for I/O. Use the IODev
attribute of the logical ECMDDevice to attach to any
physical ECMD device, e.g. attr myRelais2 IODev myAVRNETIO
. In such a case the correct
reference to the class cannot be made at the time of definition of the device. Thus, you need to
omit the <classname> and <parameter> references in the definition of the device and use the
class
attribute instead.
Examples:
define myADC ECMDDevice ADC
define myRelais1 ECMDDevice relais 8
define myRelais2 ECMDDevice
attr myRelais2 IODev myAVRNETIO
attr myRelais2 class relais 8
set <name> <commandname> [<parameter1> [<parameter2> [<parameter3> ... ]]]
<commandname>
definition in
the class definition.set <commandname>
is invoked the perl special in curly brackets from the command definition
is evaluated and the result is sent to the physical ECMD device.
set myRelais1 on
set myDisplay text This\x20text\x20has\x20blanks!
get <name> <commandname> [<parameter1> [<parameter2> [<parameter3> ... ]]]
<commandname>
definition in
the class definition.get <commandname>
is invoked the perl special in curly brackets from the command definition
is evaluated and the result is sent to the physical ECMD device. The response from the physical ECMD device is returned
and the state of the logical ECMD device is updated accordingly.
get myADC value 3
attr myRelais2 class relais 8
./etc/fhem/ADC.classdef
looks as follows:
get value cmd {"adc get %channel\n"}
get value params channel
get value expect "\d+\n"
get value postproc { s/^(\d+)\n$/$1/;; $_ }
define AVRNETIO ECMD telnet 192.168.0.91:2701 # define the physical device
attr AVRNETIO classdefs ADC=/etc/fhem/ADC.classdef # define the device class ADC
define myADC ECDMDevice ADC # define the logical device myADC with device class ADC
get myADC value 1 # retrieve the value of analog/digital converter number 1
get value
has one named parameter
channel
. In the example the literal 1
is given and thus %channel
is replaced by 1
to yield "adc get 1\n"
after macro substitution. Perl
evaluates this to a literal string which is send as a plain ethersex command to the AVR-NET-IO. The
board returns something like 024\n
for the current value of analog/digital converter number 1. The postprocessor keeps only the digits.
/etc/fhem/relais.classdef
looks as follows:
params pinmask
set on cmd {"io set ddr 2 ff\n\000ioset port 2 0%pinmask\n\000wait 1000\n\000io set port 2 00\n"}
set on expect ".*"
set on postproc {s/^OK\nOK\nOK\nOK\n$/success/; "$_" eq "success" ? "ok" : "error"; }
define AVRNETIO ECMD telnet 192.168.0.91:2701 # define the physical device
attr AVRNETIO classdefs relais=/etc/fhem/relais.classdef # define the device class relais
attr AVRNETIO requestSeparator \000
define myRelais ECMDDevice 8 # define the logical device myRelais with pin mask 8
set myRelais on # execute the "on" command
%pinmask
is replaced by 8
to yield
"io set ddr 2 ff\n\000io set port 2 08\n\000wait 1000\n\000io set port 2 00\n\000"
after macro substitution. Perl
evaluates this to a literal string. This string is split into lines (with trailing newline characters)
- io set ddr 2 ff\n
- ioset port 2 08\n
- wait 1000\n
- io set port 2 00\n
These lines are sent as a plain ethersex commands to the AVR-NET-IO one by one. After
each line the answer from the physical device is read back. They are concatenated and returned
for further processing by the postproc
command.
For any of the four plain ethersex commands, the AVR-NET-IO returns the string OK\n
. They are
concatenated. The postprocessor takes the result from $_
,
substitutes it by the string success
if it is OK\nOK\nOK\nOK\n
, and then either
returns the string ok
or the string error
. If the responseSeparator was set to \000,
the result string would be OK\n\000OK\n\000OK\n\000OK\n\000
instead of OK\nOK\nOK\nOK\n
.
/etc/fhem/DummyServer.classdef
looks as follows:
reading foo match "\d+\n"
reading foo postproc { s/^(\d+).*$/$1/;; $_ }
define myDummyServer ECMD telnet localhost:9999 # define the physical device
set myDummyServer classdef DummyServer /etc/fhem/DummyServer.classdef # define the device class DummyServer
define myDummyClient ECDMDevice DummyServer # define a logical device with device class DummyServer
On a Unix command line, run netcat -l 9999
. This makes netcat listening on port 9999. Data received on that port are printed on stdout. Data input from stdin is sent to the other end of an incoming connection.
Start FHEM.
Then enter the number 4711 at the stdin of the running netcat server.
FHEM sees 4711\n
coming in from the netcat dummy server. The incoming string matches the regular expression of the foo
reading. The postprocessor is used to strip any trailing garbage from the digits. The result 4711 is used to update the foo
reading.