############################################## package main; use strict; use warnings; use IO::Socket::INET; use Switch; sub pilight_Initialize($) { my ($hash) = @_; $hash->{SetFn} = "pilight_Set"; $hash->{DefFn} = "pilight_Define"; $hash->{AttrList} = "protocol housecode number systemcode unitcode id remote_ip remote_port useOldVersion rawCodeOn rawCodeOff follow-on-for-timer"; } # housecode == id und number == unitcode ################################### sub pilight_Set($@) { my ($hash, @a) = @_; my $rc = undef; return "no set value specified" if(int(@a) < 2); return "on off" if($a[1] eq "?"); my $command = $a[1]; my $c_timer= ""; if(defined($a[2])) { $c_timer=$a[2]; } Log 3, "pilight command: $command"; if($command eq "on") { $rc = commit($hash, 1); } elsif($command eq "on-for-timer") { InternalTimer(gettimeofday()+$c_timer, "pilight_on_timeout",$hash, 0); # on-for-timer is now a on. $rc = commit($hash, 1); } # elsif else { $rc = commit($hash, 0); } if ($rc) { $hash->{CHANGED}[0] = $command; $hash->{STATE} = $command; $hash->{READINGS}{state}{TIME} = TimeNow(); $hash->{READINGS}{state}{VAL} = $command; }; return undef; } ################################### sub pilight_on_timeout($) { my ($hash) = @_; my @a; $a[0]=$hash->{NAME}; $a[1]="off"; pilight_Set($hash,@a); return undef; } sub pilight_Define($$) { my ($hash, $def) = @_; my @a = split("[ \t][ \t]*", $def); my $u = "wrong syntax: define "; return $u if(int(@a) < 2); $hash->{protocol} = $a[2]; #for backward compartibility $hash->{housecode} = (int(@a) > 2) ? $a[3] : ''; $hash->{unitcode} = (int(@a) > 2) ? $a[4] : ''; return undef; } sub commit { my ($hash, $on) = @_; my $name = $hash->{NAME}; my $protocol = $hash->{protocol}; my $rawCodeOn = AttrVal($name, "rawCodeOn", $hash->{rawCodeOn}); my $rawCodeOff = AttrVal($name, "rawCodeOff", $hash->{rawCodeOff}); my $housecode = AttrVal($name, "id", AttrVal($name, "housecode", $hash->{housecode})); my $unit = AttrVal($name, "unitcode", $hash->{unitcode}); my $systemcode = AttrVal($name, "systemcode", '0'); my $param = $on ? "on" : "off"; my $remote_ip = AttrVal($name, "remote_ip", '127.0.0.1'); my $remote_port = AttrVal($name, "remote_port", '5000'); my $useOldVersion = AttrVal($name, "useOldVersion", undef); my ($socket,$client_socket); # flush after every write $| = 1; $socket = new IO::Socket::INET ( PeerHost => $remote_ip, PeerPort => $remote_port, Proto => 'tcp', ); if (!$socket) { Log 3, "pilight: ERROR. Can't open socket to pilight-daemon: $! See: https://github.com/andreas-fey/fhem-pilight/issues/3\n"; return undef }; my $data = $useOldVersion ? '{ "message": "client sender" }' : '{ "action": "identify" }'; $socket->send($data); $socket->recv($data,1024); $data =~ s/\n/ /g; if ( $data !~ /accept client/ && $data !~ /success/) # "accept client" < v6, { Log 3, "pilight: ERROR. No handshake with pilight-daemon. Received: >>>$data<<<\n"; return undef }; my $code = "{\"protocol\":[ \"$protocol\" ],"; if( $protocol eq 'raw') { switch( $param ) { case 'on' { $code = $code . "\"code\":\"$rawCodeOn\""} # on case 'off' { $code = $code . "\"code\":\"$rawCodeOff\""} #off } } else { switch( $protocol ) { case 'kaku_switch' { $code = $code . "\"id\":$housecode, \"unit\":$unit,\"$param\":1"} case 'quigg_switch' { $code = $code . "\"id\":$housecode, \"unit\":$unit,\"$param\":1"} case 'quigg_gt7000' { $code = $code . "\"id\":$housecode, \"unit\":$unit,\"$param\":1"} case 'quigg_gt1000' { $code = $code . "\"id\":$housecode, \"unit\":$unit,\"$param\":1"} case 'elro' { $code = $code . "\"systemcode\":$systemcode, \"unitcode\":$unit,\"$param\":1"} case 'elro_he' { $code = $code . "\"systemcode\":$systemcode, \"unitcode\":$unit,\"$param\":1"} case 'elro_hc' { $code = $code . "\"systemcode\":$systemcode, \"unitcode\":$unit,\"$param\":1"} case 'silvercrest' { $code = $code . "\"systemcode\":$systemcode, \"unitcode\":$unit,\"$param\":1"} case 'pollin' { $code = $code . "\"systemcode\":$systemcode, \"unitcode\":$unit,\"$param\":1"} case 'brennenstuhl' { $code = $code . "\"systemcode\":$systemcode, \"unitcode\":$unit,\"$param\":1"} case 'mumbi' { $code = $code . "\"systemcode\":$systemcode, \"unitcode\":$unit,\"$param\":1"} case 'impuls' { $code = $code . "\"systemcode\":$systemcode, \"programcode\":$unit,\"$param\":1"} case 'rsl366' { $code = $code . "\"systemcode\":$systemcode, \"programcode\":$unit,\"$param\":1"} case 'intertechno_old' { $code = $code . "\"id\":$systemcode, \"unit\":$unit,\"$param\":1"} case 'clarus_switch' { $code = $code . "\"id\":\"$systemcode\", \"unit\":$unit,\"$param\":1"} case 'rev1_switch' { $code = $code . "\"id\":\"$systemcode\", \"unit\":$unit,\"$param\":1"} case 'rev2_switch' { $code = $code . "\"id\":\"$systemcode\", \"unit\":$unit,\"$param\":1"} case 'rev3_switch' { $code = $code . "\"id\":\"$systemcode\", \"unit\":$unit,\"$param\":1"} } } $code = $code . '}'; $data = $useOldVersion ? "{ \"message\": \"send\", \"code\": $code}" : "{ \"action\": \"send\", \"code\": $code}"; Log 3, "pilight data: $data"; $socket->send($data); $socket->close(); return 1; } 1; =pod =begin html

pilight

=end html =cut