# $Id$ ############################################################################## # # 66_ECMD.pm # Copyright by Dr. Boris Neubert # e-mail: omega at online dot de # # This file is part of fhem. # # Fhem is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # Fhem is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with fhem. If not, see . # ############################################################################## package main; =for comment General rule: ECMD handles raw data, i.e. data that might contain control and non-printable characters. User input for raw data, e.g. setting attributes, and display of raw data is perl-encoded. Perl-encoded raw data in logs is not enclosed in double quotes. A carriage return/line feed (characters 13 and 10) is encoded as \r\n and logged as \r\n (\010\012) Decoding is handled by dq(). Encoding is handled by cq(). changes as of 27 Nov 2016: - if split is used, the strings at which the messages are split are still part of the messages - no default attributes for requestSeparator and responseSeparator - input of raw data as perl-encoded string (for setting attributes) - be more verbose and explicit at loglevel 5 - documentation corrected and amended =cut use strict; use warnings; use Time::HiRes qw(gettimeofday); use DevIo; sub ECMD_Attr($@); sub ECMD_Clear($); #sub ECMD_Parse($$$$$); sub ECMD_Read($); sub ECMD_ReadAnswer($$); sub ECMD_Ready($); sub ECMD_Write($$$); use vars qw {%attr %defs}; ##################################### sub ECMD_Initialize($) { my ($hash) = @_; # Provider $hash->{WriteFn} = "ECMD_Write"; $hash->{ReadFn} = "ECMD_Read"; $hash->{Clients} = ":ECMDDevice:"; # Consumer $hash->{DefFn} = "ECMD_Define"; $hash->{UndefFn} = "ECMD_Undef"; $hash->{ReadyFn} = "ECMD_Ready"; $hash->{GetFn} = "ECMD_Get"; $hash->{SetFn} = "ECMD_Set"; $hash->{AttrFn} = "ECMD_Attr"; $hash->{AttrList}= "classdefs split logTraffic:0,1,2,3,4,5 timeout partial requestSeparator responseSeparator autoReopen stop:0,1"; } ##################################### sub ECMD_Define($$) { my ($hash, $def) = @_; my @a = split("[ \t]+", $def); my $name = $a[0]; my $protocol = $a[2]; if(@a < 4 || @a > 4 || (($protocol ne "telnet") && ($protocol ne "serial"))) { my $msg = "wrong syntax: define ECMD telnet or define ECMD serial "; Log 2, $msg; return $msg; } $hash->{fhem}{".requestSeparator"}= undef; $hash->{fhem}{".responseSeparator"}= undef; $hash->{fhem}{".split"}= undef; DevIo_CloseDev($hash); $hash->{Protocol}= $protocol; my $devicename= $a[3]; $hash->{DeviceName} = $devicename; my $ret = DevIo_OpenDev($hash, 0, undef); return $ret; } ##################################### sub ECMD_Undef($$) { my ($hash, $arg) = @_; my $name = $hash->{NAME}; # deleting port for clients foreach my $d (sort keys %defs) { if(defined($defs{$d}) && defined($defs{$d}{IODev}) && $defs{$d}{IODev} == $hash) { my $lev = ($reread_active ? 4 : 2); Log3 $hash, $lev, "deleting port for $d"; delete $defs{$d}{IODev}; } } DevIo_CloseDev($hash); return undef; } ##################################### sub ECMD_DoInit($) { my $hash = shift; my $name = $hash->{NAME}; my $msg = undef; ECMD_Clear($hash); $hash->{STATE} = "Initialized" if(!$hash->{STATE}); return undef; } ##################################### sub oq($) { my ($s)= @_; return join("", map { sprintf("\\%03o", ord($_)) } split("", $s)); } sub dq($) { my ($s)= @_; return defined($s) ? ( $s eq "" ? "empty string" : escapeLogLine($s) . " (" . oq($s) . ")" ) : ""; } sub cq($) { my ($s)= @_; $s =~ s/\\(\d)(\d)(\d)/chr($1*64+$2*8+$3)/eg; $s =~ s/\\a/\a/g; $s =~ s/\\e/\e/g; $s =~ s/\\f/\f/g; $s =~ s/\\n/\n/g; $s =~ s/\\r/\r/g; $s =~ s/\\t/\t/g; $s =~ s/\\\\/\\/g; return $s; } ##################################### sub ECMD_Log($$$) { my ($hash, $loglevel, $logmsg)= @_; my $name= $hash->{NAME}; $loglevel= AttrVal($name, "logTraffic", undef) unless(defined($loglevel)); return unless(defined($loglevel)); Log3 $hash, $loglevel, "$name: $logmsg"; } ##################################### sub ECMD_Ready($) { my ($hash) = @_; return DevIo_OpenDev($hash, 1, "ECMD_DoInit") if($hash->{STATE} eq "disconnected"); # This is relevant for windows/USB only my $po = $hash->{USBDev}; my ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags); if($po) { ($BlockingFlags, $InBytes, $OutBytes, $ErrorFlags) = $po->status; } return ($InBytes && $InBytes>0); } ##################################### sub ECMD_isStopped($) { my $dev = shift; # name or hash $dev = $dev->{NAME} if(defined($dev) && ref($dev) eq "HASH"); my $isDisabled = AttrVal($dev, "stop", 0); } ##################################### sub ECMD_SimpleRead($) { my $hash = shift; return undef if ECMD_isStopped($hash); my $answer= DevIo_SimpleRead($hash); ECMD_Log $hash, undef, "read " . dq($answer); return $answer; } sub ECMD_SimpleWrite($$) { my ($hash, $msg) = @_; return undef if ECMD_isStopped($hash); ECMD_Log $hash, undef, "write " . dq($msg); DevIo_SimpleWrite($hash, $msg, 0); } sub ECMD_SimpleExpect($$$) { my ($hash, $msg, $expect) = @_; my $name= $hash->{NAME}; return undef if ECMD_isStopped($name); my $timeout= AttrVal($name, "timeout", 3.0); my $partialTimeout= AttrVal($name, "partial", 0.0); ECMD_Log $hash, undef, "write " . dq($msg) . ", expect $expect"; my $answer= DevIo_Expect($hash, $msg, $timeout ); #Debug "$name: Expect got \"" . escapeLogLine($answer) . "\"."; # complete partial answers if($partialTimeout> 0) { my $t0= gettimeofday(); while(!defined($answer) || ($answer !~ /^$expect$/)) { #Debug "$name: waiting for a match..."; my $a= DevIo_SimpleReadWithTimeout($hash, $partialTimeout); # we deliberately use partialTimeout here! #Debug "$name: SimpleReadWithTimeout got \"" . escapeLogLine($a) . "\"."; if(defined($a)) { $answer= ( defined($answer) ? $answer . $a : $a ); } #Debug "$name: SimpleExpect has now answer \"" . escapeLogLine($answer) . "\"."; last if(gettimeofday()-$t0> $partialTimeout); } } if(defined($answer)) { ECMD_Log $hash, undef, "read " . dq($answer); if($answer !~ m/^$expect$/) { ECMD_Log $hash, 1, "unexpected answer " . dq($answer) . " received (wrote " . dq($msg) . ", expected $expect)"; } } else { ECMD_Log $hash, 1, "no answer received (wrote " . dq($msg) . ", expected $expect)"; } return $answer; } ##################################### sub ECMD_SetState($$$$) { my ($hash, $tim, $vt, $val) = @_; return undef; } ##################################### sub ECMD_Clear($) { my $hash = shift; return undef if ECMD_isStopped($hash); # Clear the pipe DevIo_TimeoutRead($hash, 0.1); } ##################################### sub ECMD_Get($@) { my ($hash, @a) = @_; return "get needs at least one parameter" if(@a < 2); my $name = $a[0]; my $cmd= $a[1]; my $arg = ($a[2] ? $a[2] : ""); my @args= @a; shift @args; shift @args; my ($answer, $err); return "No get $cmd for dummies" if(IsDummy($name)); if($cmd eq "raw") { return "get raw needs an argument" if(@a< 3); my $ecmd= join " ", @args; $ecmd= AnalyzePerlCommand(undef, $ecmd); # poor man's error catching... if($ecmd =~ "^Bareword \"") { $answer= $ecmd; } else { $answer= ECMD_SimpleExpect($hash, $ecmd, ".*"); } } else { return "Unknown argument $cmd, choose one of raw"; } $hash->{READINGS}{$cmd}{VAL} = $answer; $hash->{READINGS}{$cmd}{TIME} = TimeNow(); return "$name $cmd => $answer"; } ##################################### sub ECMD_EvalClassDef($$$) { my ($hash, $classname, $filename)=@_; my $name= $hash->{NAME}; # refuse overwriting existing definitions if(defined($hash->{fhem}{classDefs}{$classname})) { my $err= "$name: class $classname is already defined."; Log3 $hash, 1, $err; return $err; } # try and open the class definition file if(!open(CLASSDEF, $filename)) { my $err= "$name: cannot open file $filename for class $classname."; Log3 $hash, 1, $err; return $err; } my @classdef= ; close(CLASSDEF); # add the class definition Log3 $hash, 5, "$name: adding new class $classname from file $filename"; $hash->{fhem}{classDefs}{$classname}{filename}= $filename; # format of the class definition: # params parameters for device definition # get cmd {} defines a get command # get params parameters for get command # get expect regex expected regex for get command # get postproc { } postprocessor for get command # set cmd {} defines a set command # set expect regex expected regex for set command # set params parameters for get command # set postproc { } postprocessor for set command # all lines are optional # # eaxmple class definition 1: # get adc cmd {"adc get %channel"} # get adc params channel # # eaxmple class definition 1: # params btnup btnstop btndown # set up cmd {"io set ddr 2 ff\nio set port 2 1%btnup\nwait 1000\nio set port 2 00"} # set stop cmd {"io set ddr 2 ff\nio set port 2 1%btnstop\nwait 1000\nio set port 2 00"} # set down cmd {"io set ddr 2 ff\nio set port 2 1%btndown\nwait 1000\nio set port 2 00"} my $cont= ""; foreach my $line (@classdef) { # kill trailing newline chomp $line; # kill comments and blank lines $line=~ s/\#.*$//; $line=~ s/\s+$//; $line= $cont . $line; if($line=~ s/\\$//) { $cont= $line; undef $line; } next unless($line); $cont= ""; Log3 $hash, 5, "$name: evaluating >$line<"; # split line into command and definition my ($cmd, $def)= split("[ \t]+", $line, 2); #if($cmd eq "nonl") { # Log3 $hash, 5, "$name: no newline"; # $hash->{fhem}{classDefs}{$classname}{nonl}= 1; #} # # params # if($cmd eq "params") { Log3 $hash, 5, "$name: parameters are $def"; $hash->{fhem}{classDefs}{$classname}{params}= $def; # # state # } elsif($cmd eq "state") { Log3 $hash, 5, "$name: state is determined as $def"; $hash->{fhem}{classDefs}{$classname}{state}= $def; # # reading # } elsif($cmd eq "reading") { my ($readingname, $spec, $arg)= split("[ \t]+", $def, 3); # # match # if($spec eq "match") { if($arg !~ m/^"(.*)"$/s) { Log3 $hash, 1, "$name: match for reading $readingname is not enclosed in double quotes."; next; } $arg = $1; Log3 $hash, 5, "$name: reading $readingname will match $arg"; $hash->{fhem}{classDefs}{$classname}{readings}{$readingname}{match}= $arg; # # postproc # } elsif($spec eq "postproc") { if($arg !~ m/^{.*}$/s) { Log3 $hash, 1, "$name: postproc command for reading $readingname is not a perl command."; next; } $arg =~ s/^(\\\n|[ \t])*//; # Strip space or \\n at the beginning $arg =~ s/[ \t]*$//; Log3 $hash, 5, "$name: reading $readingname postprocessor defined as $arg"; $hash->{fhem}{classDefs}{$classname}{readings}{$readingname}{postproc}= $arg; # # anything else # } else { Log3 $hash, 1, "$name: illegal spec $spec for reading $readingname for class $classname in file $filename."; } # # set, get # } elsif($cmd eq "set" || $cmd eq "get") { my ($cmdname, $spec, $arg)= split("[ \t]+", $def, 3); if($spec eq "params") { if($cmd eq "set") { Log3 $hash, 5, "$name: set $cmdname has parameters $arg"; $hash->{fhem}{classDefs}{$classname}{sets}{$cmdname}{params}= $arg; } elsif($cmd eq "get") { Log3 $hash, 5, "$name: get $cmdname has parameters $arg"; $hash->{fhem}{classDefs}{$classname}{gets}{$cmdname}{params}= $arg; } } elsif($spec eq "cmd") { if($arg !~ m/^{.*}$/s) { Log3 $hash, 1, "$name: command for $cmd $cmdname is not a perl command."; next; } $arg =~ s/^(\\\n|[ \t])*//; # Strip space or \\n at the beginning $arg =~ s/[ \t]*$//; if($cmd eq "set") { Log3 $hash, 5, "$name: set $cmdname command defined as $arg"; $hash->{fhem}{classDefs}{$classname}{sets}{$cmdname}{cmd}= $arg; } elsif($cmd eq "get") { Log3 $hash, 5, "$name: get $cmdname command defined as $arg"; $hash->{fhem}{classDefs}{$classname}{gets}{$cmdname}{cmd}= $arg; } } elsif($spec eq "postproc") { if($arg !~ m/^{.*}$/s) { Log3 $hash, 1, "$name: postproc command for $cmd $cmdname is not a perl command."; next; } $arg =~ s/^(\\\n|[ \t])*//; # Strip space or \\n at the beginning $arg =~ s/[ \t]*$//; if($cmd eq "set") { Log3 $hash, 5, "$name: set $cmdname postprocessor defined as $arg"; $hash->{fhem}{classDefs}{$classname}{sets}{$cmdname}{postproc}= $arg; } elsif($cmd eq "get") { Log3 $hash, 5, "$name: get $cmdname postprocessor defined as $arg"; $hash->{fhem}{classDefs}{$classname}{gets}{$cmdname}{postproc}= $arg; } } elsif($spec eq "expect") { if($arg !~ m/^"(.*)"$/s) { Log3 $hash, 1, "$name: expect for $cmd $cmdname is not enclosed in double quotes."; next; } $arg = $1; if($cmd eq "set") { Log3 $hash, 5, "$name: set $cmdname expects $arg"; $hash->{fhem}{classDefs}{$classname}{sets}{$cmdname}{expect}= $arg; } elsif($cmd eq "get") { Log3 $hash, 5, "$name: get $cmdname expects $arg"; $hash->{fhem}{classDefs}{$classname}{gets}{$cmdname}{expect}= $arg; } } else { Log3 $hash, 1, "$name: illegal spec $spec for $cmd $cmdname for class $classname in file $filename."; } } else { Log3 $hash, 1, "$name: illegal tag $cmd for class $classname in file $filename."; } } # store class definitions in attribute $attr{$name}{classdefs}= ""; my @a; foreach my $c (keys %{$hash->{fhem}{classDefs}}) { push @a, "$c=$hash->{fhem}{classDefs}{$c}{filename}"; } $attr{$name}{"classdefs"}= join(":", @a); return undef; } ##################################### sub ECMD_Attr($@) { my @a = @_; my $hash= $defs{$a[1]}; my $name= $hash->{NAME}; if($a[0] eq "set") { if($a[2] eq "classdefs") { my @classdefs= split(/:/,$a[3]); delete $hash->{fhem}{classDefs}; foreach my $classdef (@classdefs) { my ($classname,$filename)= split(/=/,$classdef,2); ECMD_EvalClassDef($hash, $classname, $filename); } } elsif($a[2] eq "requestSeparator") { my $c= cq($a[3]); $hash->{fhem}{".requestSeparator"}= $c; Log3 $hash, 5, "$name: requestSeparator set to " . dq($c); } elsif($a[2] eq "responseSeparator") { my $c= cq($a[3]); $hash->{fhem}{".responseSeparator"}= $c; Log3 $hash, 5, "$name: responseSeparator set to " . dq($c); } elsif($a[2] eq "split") { my $c= cq($a[3]); $hash->{fhem}{".split"}= $c; Log3 $hash, 5, "$name: split set to " . dq($c); } } elsif($a[0] eq "del") { if($a[2] eq "requestSeparator") { $hash->{fhem}{".requestSeparator"}= undef; Log3 $hash, 5, "$name: requestSeparator deleted"; } elsif($a[2] eq "responseSeparator") { $hash->{fhem}{".responseSeparator"}= undef; Log3 $hash, 5, "$name: responseSeparator deleted"; } elsif($a[2] eq "split") { $hash->{fhem}{".split"}= undef; Log3 $hash, 5, "$name: split deleted"; } } return undef; } ##################################### sub ECMD_Reopen($) { my ($hash) = @_; return undef if ECMD_isStopped($hash); DevIo_CloseDev($hash); DevIo_OpenDev($hash, 1, undef); return undef; } ##################################### sub ECMD_Set($@) { my ($hash, @a) = @_; my $name = $a[0]; # usage check #my $usage= "Usage: set $name classdef OR set $name reopen"; my $usage= "Unknown argument $a[1], choose one of reopen classdef"; if((@a == 2) && ($a[1] eq "reopen")) { return ECMD_Reopen($hash); } return $usage if(@a != 4); return $usage if($a[1] ne "classdef"); # from the definition my $classname= $a[2]; my $filename= $a[3]; return ECMD_EvalClassDef($hash, $classname, $filename); } ##################################### # called from the global loop, when the select for hash->{FD} reports data sub ECMD_Read($) { my ($hash) = @_; return undef unless($hash->{STATE} eq "opened"); # avoid reading from closed device my $buf = ECMD_SimpleRead($hash); return unless(defined($buf)); return if($buf eq ""); ECMD_Log $hash, 5, "Spontaneously received " . dq($buf); Dispatch($hash, $buf, undef); # dispatch result to ECMDDevices } ##################################### sub ECMD_Write($$$) { my ($hash,$msg,$expect) = @_; my $name= $hash->{NAME}; my $lastWrite= defined($hash->{fhem}{".lastWrite"}) ? $hash->{fhem}{".lastWrite"} : 0; my $now= gettimeofday(); my $autoReopen= AttrVal($name, "autoReopen", undef); if(defined($autoReopen)) { my ($timeout,$delay)= split(',',$autoReopen); ECMD_Reopen($hash) if($now>$lastWrite+$timeout); sleep($delay); } $hash->{fhem}{".lastWrite"}= $now; my $answer; my $ret= ""; my $requestSeparator= $hash->{fhem}{".requestSeparator"}; my $responseSeparator= $hash->{fhem}{".responseSeparator"}; my @ecmds; if(defined($requestSeparator)) { @ecmds= split $requestSeparator, $msg; } else { push @ecmds, $msg; } ECMD_Log $hash, 5, "command split into " . ($#ecmds+1) . " parts, requestSeparator is " . dq($requestSeparator) if($#ecmds>0); foreach my $ecmd (@ecmds) { ECMD_Log $hash, 5, "sending command " . dq($ecmd); my $msg .= $ecmd; if(defined($expect)) { $answer= ECMD_SimpleExpect($hash, $msg, $expect); $answer= "" unless(defined($answer)); ECMD_Log $hash, 5, "received answer " . dq($answer); $answer.= $responseSeparator if(defined($responseSeparator) && ($#ecmds>0)); $ret.= $answer; } else { ECMD_SimpleWrite($hash, $msg); } } return $ret; } ##################################### 1; =pod =item device =item summary configurable request/response-like communication (physical device) =item summary_DE konfigurierbare Frage/Antwort-Kommunikation (physisches Gerät) =begin html

ECMD

    Any physical device with request/response-like communication capabilities over a serial line or TCP connection can be defined as ECMD device. A practical example of such a device is the AVR microcontroller board AVR-NET-IO from Pollin with ECMD-enabled Ethersex firmware. The original NetServer firmware from Pollin works as well. There is a plenitude of use cases.

    A physical ECMD device can host any number of logical ECMD devices. Logical devices are defined as ECMDDevices in fhem. ADC 0 to 3 and I/O port 0 to 3 of the above mentioned board are examples of such logical devices. ADC 0 to 3 all belong to the same device class ADC (analog/digital converter). I/O port 0 to 3 belong to the device class I/O port. By means of extension boards you can make your physical device drive as many logical devices as you can imagine, e.g. IR receivers, LC displays, RF receivers/transmitters, 1-wire devices, etc.

    Defining one fhem module for any device class would create an unmanageable number of modules. Thus, an abstraction layer is used. You create a device class on the fly and assign it to a logical ECMD device. The class definition names the parameters of the logical device, e.g. a placeholder for the number of the ADC or port, as well as the get and set capabilities. Worked examples are to be found in the documentation of the ECMDDevice device.

    Note: this module requires the Device::SerialPort or Win32::SerialPort module if the module is connected via serial Port or USB.

    Character coding

    ECMD is suited to process any character including non-printable and control characters. User input for raw data, e.g. for setting attributes, and the display of raw data, e.g. in the log, is perl-encoded according to the following table (ooo stands for a three-digit octal number):
    characteroctalcode
    Bell007\a
    Backspace008\008
    Escape033\e
    Formfeed014\f
    Newline012\n
    Return015\r
    Tab011\t
    backslash134\134 or \\
    anyooo\ooo

    In user input, use \134 for backslash to avoid conflicts with the way FHEM handles continuation lines.

    Define

      define <name> ECMD telnet <IPAddress:Port>

      or

      define <name> ECMD serial <SerialDevice>[<@BaudRate>]

      Defines a physical ECMD device. The keywords telnet or serial are fixed.

      Examples:
        define AVRNETIO ECMD telnet 192.168.0.91:2701
        define AVRNETIO ECMD serial /dev/ttyS0
        define AVRNETIO ECMD serial /dev/ttyUSB0@38400

    Set

      set <name> classdef <classname> <filename>

      Creates a new device class <classname> for logical devices. The class definition is in the file <filename>. You must create the device class before you create a logical device that adheres to that definition.

      Example:
        set AVRNETIO classdef /etc/fhem/ADC.classdef

      set <name> reopen

      Closes and reopens the device. Could be handy if connection is lost and cannot be reestablished automatically.

    Get

      get <name> raw <command>

      Sends the command <command> to the physical ECMD device <name> and reads the response. In the likely case that the command needs to be terminated by a newline character, you have to resort to a <perl special>.

      Example:
        get AVRNETIO raw { "ip\n" }


    Attributes

    • classdefs
      A colon-separated list of <classname>=<filename>. The list is automatically updated if a class definition is added. You can directly set the attribute. Example: attr myECMD classdefs ADC=/etc/fhem/ADC.classdef:GPIO=/etc/fhem/AllInOne.classdef
    • split <separator>
      Some devices send several readings in one transmission. The split attribute defines the separator to split such transmissions into separate messages. The regular expression for matching a reading is then applied to each message in turn. After splitting, the separator is still part of the single messages. Separator can be a single- or multi-character string, e.g. \n or \r\n. Example: attr myECMD split \n splits foo 12\nbar off\n into foo 12\n and bar off\n.
    • logTraffic <loglevel>
      Enables logging of sent and received datagrams with the given loglevel. Control characters in the logged datagrams are escaped, i.e. a double backslash is shown for a single backslash, \n is shown for a line feed character, etc.
    • timeout <seconds>
      Time in seconds to wait for a response from the physical ECMD device before FHEM assumes that something has gone wrong. The default is 3 seconds if this attribute is not set.
    • partial <seconds>
      Some physical ECMD devices split responses into several transmissions. If the partial attribute is set, this behavior is accounted for as follows: (a) If a response is expected for a get or set command, FHEM collects transmissions from the physical ECMD device until either the response matches the expected response (reading ... match ... in the class definition) or the time in seconds given with the partial attribute has expired. (b) If a spontaneous transmission does not match the regular expression for any reading, the transmission is recorded and prepended to the next transmission. If the line is quiet for longer than the time in seconds given with the partial attribute, the recorded transmission is discarded. Use regular expressions that produce exact matches of the complete response (after combining partials and splitting).
    • requestSeparator <separator>
      A single command from FHEM to the device might need to be broken down into several requests. A command string is split at all occurrences of the request separator. The request separator itself is removed from the command string and thus is not part of the request. The default is to have no request separator. Use a request separator that does not occur in the actual request.
    • responseSeparator <separator>
      In order to identify the single responses from the device for each part of the command broken down by request separators, a response separator can be appended to the response to each single request. The response separator is only appended to commands split by means of a request separator. The default is to have no response separator, i.e. responses are simply concatenated. Use a response separator that does not occur in the actual response.
    • autoReopen <timeout>,<delay>
      If this attribute is set, the device is automatically reopened if no bytes were written for <timeout> seconds or more. After reopening FHEM waits <delay> seconds before writing to the device. Use the delay with care because it stalls FHEM completely.
    • stop
      Disables read/write access to the device if set to 1. No data is written to the physical ECMD device. A read request always returns an undefined result. This attribute can be used to temporarily disable a device that is not available.
    • verbose


    Separators

    When to use the split and partial attributes?

    Set the partial attribute in combination with reading ... match ... in the class definition, if you receive datagrams with responses which are broken into several transmissions, like resp followed by onse\r\n.

    Set the split attribute if you receive several responses in one transmission, like reply1\r\nreply2\r\n.

    When to use the requestSeparator and responseSeparator attributes?

    Set the requestSeparator attribute, if you want to send several requests in one command, with one transmission per request. The strings sent to the device for set and get commands as defined in the class definition are broken down into several request/response interactions with the physical device. The request separator is not sent to the physical device.

    Set the responseSeparator attribute to separate the responses received for a command broken down into several requests by means of a request separator. This is useful for easier postprocessing.

    Example: you want to send the requests request1 and request2 in one command. The physical device would respond with response1 and response2 respectively for each of the requests. You set the request separator to \000 and the response separator to \001 and you define the command as request1\000request2\000. The command is broken down into request1 and request2. request1 is sent to the physical device and response1 is received, followed by sending request2 and receiving response2. The final result is response1\001response2\001.

    You can think of this feature as of a macro. Splitting and partial matching is still done per single request/response within the macro.

    Datagram monitoring and matching

    Data to and from the physical device is processed as is. In particular, if you need to send a line feed you have to explicitely send a \n control character. On the other hand, control characters like line feeds are not stripped from the data received. This needs to be considered when defining a class definition.

    For debugging purposes, especially when designing a class definition, it is advisable to turn traffic logging on. Use attr myECMD logTraffic 3 to log all data to and from the physical device at level 3.

    Datagrams and attribute values are logged with non-printable and control characters encoded as here followed by the octal representation in parantheses. Example: #!foo\r\n (\043\041\146\157\157\015\012).

    Data received from the physical device is processed as it comes in chunks. If for some reason a datagram from the device is split in transit, pattern matching and processing will most likely fail. You can use the partial attribute to make FHEM collect and recombine the chunks.

    Connection error handling

    This modules handles unexpected disconnects of devices as follows (on Windows only for TCP connections):

    Disconnects are detected if and only if data from the device in reply to data sent to the device cannot be received with at most two attempts. FHEM waits at most 3 seconds (or the time specified in the timeout attribute, see Attributes). After the first failed attempt, the connection to the device is closed and reopened again. The state of the device is failed. Then the data is sent again to the device. If still no reply is received, the state of the device is disconnected, otherwise opened. You will have to fix the problem and then use set myECMD reopen to reconnect to the device.

    Please design your class definitions in such a way that the double sending of data does not bite you in any case.

    Class definition

    The class definition for a logical ECMD device class is contained in a text file. The text file is made up of single lines. Empty lines and text beginning with # (hash) are ignored. Therefore make sure not to use hashes in commands.
    The following commands are recognized in the device class definition:

    • params <parameter1> [<parameter2> [<parameter3> ... ]]

      Declares the names of the named parameters that must be present in the definition of the logical ECMD device.

    • state <reading>

      Normally, the state reading is set to the latest command or reading name followed by the value, if any. This command sets the state reading to the value of the named reading if and only if the reading is updated.

    • set <commandname> cmd { <perl special> }
      get <commandname> cmd { <perl special> }

      Declares a new set or get command <commandname>. If the user invokes the set or get command <commandname>, the string that results from the execution of the <perl special> is sent to the physical device.

      A request separator (see Attributes) can be used to split the command into chunks. This is required for sending multiple Ethersex commands for one command in the class definition. The result string for the command is the concatenation of all responses received from the physical device, optionally with response separators (see Attributes) in between.

    • set <commandname> expect "<regex>"
      get <commandname> expect "<regex>"

      Declares what FHEM expects to receive after the execution of the get or set command <commandname>. <regex> is a Perl regular expression. The double quotes around the regular expression are mandatory and they are not part of the regular expression itself. <regex> must match the entire reply, as in m/^<regex>$/. Particularly, broken connections can only be detected if something is expected (see Connection error handling).

    • set <commandname> postproc { <perl special> }
      get <commandname> postproc { <perl special> }

      Declares a postprocessor for the command <commandname>. The data received from the physical device in reply to the get or set command <commandname> is processed by the Perl code <perl command>. The perl code operates on $_. Make sure to return the result in $_ as well. The result of the perl command is shown as the result of the get or set command.

    • set <commandname> params <parameter1> [<parameter2> [<parameter3> ... ]]
      get <commandname> params <parameter1> [<parameter2> [<parameter3> ... ]]

      Declares the names of the named parameters that must be present in the set or get command <commandname>. Be careful not to use a parameter name that is already used in the device definition (see params above).

    • reading <reading> match "<regex>"

      Declares a new reading named <reading>. A spontaneous data transmission from the physical device that matches the Perl regular expression <regex> is evaluated to become the value of the named reading. All ECMDDevice devices belonging to the ECMD device with readings with matching regular expressions will receive an update of the said readings. <regex> must match the entire reply, as in m/^<regex>$/.

    • reading <reading> postproc { <perl special> }

      Declares a postprocessor for the reading <reading>. The data received for the named reading is processed by the Perl code <perl command>. This works analogously to the postproc spec for set and get commands.

    The perl specials in the definitions above can contain macros:

    • The macro %NAME will expand to the device name.
    • The macro %TYPE will expand to the device type.
    • The macro %<parameter> will expand to the current value of the named parameter. This can be either a parameter from the device definition or a parameter from the set or get command.
    • The macro substitution occurs before perl evaluates the expression. It is a plain text substitution. Be careful not to use parameters with overlapping names like %pin and %pin1.
    • If in doubt what happens, run the commands with loglevel 5 and inspect the log file.


    The rules outlined in the documentation of perl specials for the <perl command> in the postprocessor definitions apply. Note: Beware of undesired side effects from e.g. doubling of semicolons!
=end html =cut