mirror of
https://github.com/fhem/fhem-mirror.git
synced 2025-01-31 12:49:34 +00:00
add ws2000 modul and standalone reader
git-svn-id: https://svn.fhem.de/fhem/trunk@173 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
parent
c10e7c1d8e
commit
84e0bc71fa
505
fhem/FHEM/87_WS2000.pm
Normal file
505
fhem/FHEM/87_WS2000.pm
Normal file
@ -0,0 +1,505 @@
|
||||
package main;
|
||||
###########################
|
||||
# 87_ws2000.pm
|
||||
# Modul for FHEM
|
||||
#
|
||||
# contributed by thomas dressler 2008
|
||||
# $Id: 87_WS2000.pm,v 1.1 2008-05-10 21:10:29 tdressler Exp $
|
||||
###########################
|
||||
use strict;
|
||||
use Switch;
|
||||
use warnings;
|
||||
|
||||
#prototypes to make komodo happy
|
||||
use vars qw{%attr %defs};
|
||||
sub Log($$);
|
||||
|
||||
####################################
|
||||
# WS2000_Initialize
|
||||
# Implements Initialize function
|
||||
#
|
||||
sub WS2000_Initialize($)
|
||||
{
|
||||
my ($hash) = @_;
|
||||
|
||||
# Provider
|
||||
#$hash->{WriteFn} = "ws2000_Write";
|
||||
# $hash->{Clients} = ":WS2000Rain:WS2000Wind:WS2000Indoor:WS2000Lux:WS2000Pyro:WS2000Temp:WS2000TempHum";
|
||||
|
||||
# Consumer
|
||||
$hash->{DefFn} = "WS2000_Define";
|
||||
$hash->{UndefFn} = "WS2000_Undef";
|
||||
$hash->{GetFn} = "WS2000_Get";
|
||||
$hash->{SetFn} = "WS2000_Set";
|
||||
#$hash->{ParseFn} = "WS2000_Parse";
|
||||
$hash->{ReadFn} ="WS2000_Read";
|
||||
$hash->{ListFn} ="WS2000_List";
|
||||
$hash->{AttrList}= "model:WS2000 rain altitude loglevel:0,1,2,3,4,5";
|
||||
}
|
||||
|
||||
#####################################
|
||||
# WS2000_Define
|
||||
# Implements DefFn function
|
||||
#
|
||||
sub
|
||||
WS2000_Define($$)
|
||||
{
|
||||
my ($hash, $def) = @_;
|
||||
my @a = split("[ \t][ \t]*", $def);
|
||||
delete $hash->{po};
|
||||
delete $hash->{socket};
|
||||
delete $hash->{FD};
|
||||
my $quiet=0;
|
||||
my $name=$hash->{NAME};
|
||||
my $PortName = $a[2];
|
||||
my $PortObj;
|
||||
$attr{$a[0]}{savefirst} = 1;
|
||||
|
||||
if($PortName eq "none") {
|
||||
Log 1, "WS2000 device is none, commands will be echoed only";
|
||||
return undef;
|
||||
}
|
||||
|
||||
Log 4, "WS2000 opening device $PortName";
|
||||
|
||||
#switch serial/socket device
|
||||
if ($PortName=~/^\/dev|^COM/) {
|
||||
#normal devices (/dev), on windows COMx
|
||||
my $OS=$^O;
|
||||
if ($OS eq 'MSWin32') {
|
||||
eval ("use Win32::SerialPort;");
|
||||
if ($@) {
|
||||
$hash->{STATE} = "error using Modul Win32::SerialPort";
|
||||
Log 1,"Error using Device::SerialPort";
|
||||
return "Can't use Win32::SerialPort $@\n";
|
||||
}
|
||||
$PortObj = new Win32::SerialPort ($PortName, $quiet);
|
||||
if ($PortObj) {
|
||||
$hash->{STATE} = "error opening device";
|
||||
Log 1,"Error opening Serial Device $PortName";
|
||||
return "Can't open PortName: $^E\n";
|
||||
}
|
||||
} else {
|
||||
eval ("use Device::SerialPort;");
|
||||
if ($@) {
|
||||
$hash->{STATE} = "error using Modul Device::SerialPort";
|
||||
Log 1,"Error using Device::SerialPort";
|
||||
return "Can't Device::SerialPort $@\n";
|
||||
}
|
||||
$PortObj = new Device::SerialPort ($PortName, $quiet);
|
||||
if ($PortObj) {
|
||||
$hash->{STATE} = "error opening device";
|
||||
Log 1,"Error opening Serial Device $PortName";
|
||||
return "Can't open PortName: $^E\n";
|
||||
}
|
||||
}
|
||||
#Parameter 19200,8,2,Odd,None
|
||||
$PortObj->baudrate(19200);
|
||||
$PortObj->databits(8);
|
||||
$PortObj->parity("odd");
|
||||
$PortObj->stopbits(2);
|
||||
$PortObj->handshake("none");
|
||||
if (! $PortObj->write_settings) {
|
||||
undef $PortObj;
|
||||
return "Serial write Settings failed!\n";
|
||||
}
|
||||
$hash->{po}=$PortObj;
|
||||
$hash->{FD}=$PortObj->FILENO;
|
||||
$hash->{socket}=0;
|
||||
|
||||
}elsif($PortName=~/([\w.]+):(\d{1,5})/){
|
||||
#Sockets(hostname:port)
|
||||
my $host=$1;
|
||||
my $port=$2;
|
||||
my $xport=IO::Socket::INET->new(PeerAddr=>$host,
|
||||
PeerPort=>$port,
|
||||
timeout=>1,
|
||||
blocking=>0
|
||||
);
|
||||
if (!$xport) {
|
||||
$hash->{STATE} = "error opening device";
|
||||
Log 1,"Error opening Connection to $PortName";
|
||||
return "Can't Connect to $PortName -> $@ ( $!)\n";
|
||||
}
|
||||
$xport->autoflush(1);
|
||||
$hash->{FD}=$xport->fileno;
|
||||
$hash->{socket}=$xport;
|
||||
|
||||
|
||||
}else{
|
||||
$hash->{STATE} = "$PortName is no device and not implemented";
|
||||
Log 1,"$PortName is no device and not implemented";
|
||||
return "$PortName is no device and not implemented\n";
|
||||
}
|
||||
Log 4, "$name connected to device $PortName";
|
||||
$hash->{STATE} = "open";
|
||||
$hash->{DeviceName}=$PortName;
|
||||
return undef;
|
||||
}
|
||||
|
||||
#####################################
|
||||
# WS2000_Undef
|
||||
# implements UnDef-Function
|
||||
#
|
||||
sub
|
||||
WS2000_Undef($$)
|
||||
{
|
||||
my ($hash, $arg) = @_;
|
||||
my $name = $hash->{NAME};
|
||||
delete $hash->{FD};
|
||||
$hash->{STATE}='close';
|
||||
if ($hash->{socket}) {
|
||||
$hash->{socket}->shutdown(2);
|
||||
$hash->{socket}->close();
|
||||
}elsif ($hash->{po}) {
|
||||
$hash->{po}->close();
|
||||
}
|
||||
Log 5, "$name shutdown complete";
|
||||
return undef;
|
||||
}
|
||||
|
||||
#####################################
|
||||
# WS2000_Set
|
||||
# implement SetFn
|
||||
# currently nothing to set
|
||||
#
|
||||
sub
|
||||
WS2000_Set($@)
|
||||
{
|
||||
my ($hash, @a) = @_;
|
||||
|
||||
my $msg;
|
||||
my $name=$a[0];
|
||||
my $reading= $a[1];
|
||||
$msg="$name => No Set function ($reading) implemented";
|
||||
Log 1,$msg;
|
||||
return $msg;
|
||||
|
||||
}
|
||||
|
||||
#####################################
|
||||
# WS2000_Get
|
||||
# implement GetFn
|
||||
#
|
||||
sub
|
||||
WS2000_Get($@)
|
||||
{
|
||||
|
||||
my ($hash, @a) = @_;
|
||||
my $u1 = "Usage: get <name> [TH0..TH7, T0..T7, I0..I7, R0..R7, W0..W7, L0..L7, P0..P7, LAST, RAW]\n" .
|
||||
"get <name> list\n";
|
||||
|
||||
|
||||
return $u1 if(int(@a) != 2);
|
||||
|
||||
my $name= $a[0];
|
||||
my $reading= $a[1];
|
||||
my $msg;
|
||||
my $retval;
|
||||
my $time;
|
||||
my $sensor=$hash->{READINGS};
|
||||
if ($reading =~/list/i) {
|
||||
$msg='';
|
||||
foreach my $s (keys %$sensor) {
|
||||
next if !$s;
|
||||
$msg.="ID:$s, Last Update ".$sensor->{$s}{TIME}."\n";
|
||||
}
|
||||
}else {
|
||||
if(!defined($sensor->{$reading})) {
|
||||
$msg="Sensor ($reading)not defined, try 'get <n<me> list'";
|
||||
}else {
|
||||
$retval=$sensor->{$reading}{VAL};
|
||||
$time=$sensor->{$reading}{TIME};
|
||||
$retval=unpack("H*",$retval) if ($reading eq 'RAW');
|
||||
$msg= "$name $reading ($time) => $retval";
|
||||
}
|
||||
}
|
||||
return $msg;
|
||||
}
|
||||
|
||||
#####################################
|
||||
# WS2000_Write
|
||||
# currently dummy
|
||||
#
|
||||
sub
|
||||
WS2000_Write($$)
|
||||
{
|
||||
my ($hash,$msg) = @_;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#####################################
|
||||
# WS2000_Read
|
||||
# Implements ReadFn, called from global select
|
||||
#
|
||||
sub
|
||||
WS2000_Read($$) {
|
||||
|
||||
my ($hash) = @_;
|
||||
|
||||
my $STX=2;
|
||||
my $ETX=3;
|
||||
my $retval='';
|
||||
my $out=undef;
|
||||
my $byte;
|
||||
my $name=$hash->{NAME};
|
||||
my $xport=$hash->{socket};
|
||||
my $PortObj=$hash->{po};
|
||||
my $message=$hash->{PARTIAL}||'';
|
||||
my $status=$hash->{STEP};
|
||||
#read data(1 byte), because fhem select flagged data available
|
||||
if ($xport) {
|
||||
$xport->read($out,1);
|
||||
}elsif($PortObj) {
|
||||
$out = $PortObj->read(1);
|
||||
}
|
||||
return if(!defined($out) || length($out) == 0) ;
|
||||
Log 5, "$name => WS2000/RAW: " . unpack('H*',$out);
|
||||
|
||||
#check for frame: STX TYP W1 W2 W3 W4 W5 ETX
|
||||
$byte=ord($out);
|
||||
if($byte eq $STX) {
|
||||
#Log 4, "M232: return value \'" . $retval . "\'";
|
||||
$status= "STX";
|
||||
$message=$out;
|
||||
Log 5, "WS2000 STX received";
|
||||
} elsif($byte eq $ETX) {
|
||||
$status= "ETX";
|
||||
$message .=$out;
|
||||
Log 5, "WS2000 ETX received";
|
||||
} elsif ($status eq "STX"){
|
||||
$message .=$out;
|
||||
}
|
||||
$hash->{STEP}=$status;
|
||||
$hash->{PARTIAL}=$message;
|
||||
if($status eq "ETX") {
|
||||
WS2000_Parse($hash,$message);
|
||||
}
|
||||
}
|
||||
#####################################
|
||||
# WS2000_Parse
|
||||
# decodes complete frame
|
||||
# called directly from WS2000_Read
|
||||
sub
|
||||
WS2000_Parse($$) {
|
||||
my ($hash,$msg) = @_;
|
||||
my ($stx,$typ,$w1,$w2,$w3,$w4,$w5,$etx)=map {$_ & 0x7F} unpack("U*",$msg);
|
||||
|
||||
my ($sensor,$daten1,$einheit1,$daten2,$einheit2,$daten3,$einheit3,$result,$shortname,$val, $unit);
|
||||
my $group = ($typ & 0x70)/16 ;#/slash for komodo syntax checker!
|
||||
my $snr = $typ % 16;
|
||||
my $tm=TimeNow();
|
||||
my $name=$hash->{NAME};
|
||||
my $factor=$attr{$name}{rain}||366;
|
||||
my $altitude=$attr{$name}{altitude}||0;
|
||||
|
||||
#duplicate check (repeater?)
|
||||
my $prevmsg=$hash->{READINGS}{RAW}{VAL}||'';
|
||||
my $prevtime=$hash->{READINGS}{RAW}{TIME}||0;
|
||||
if (($prevmsg eq $msg) && ((time() - $prevtime) <3)) {
|
||||
Log 4,"$name check: Duplicate detected";
|
||||
return undef;
|
||||
}
|
||||
my $rawtext="Typ=$typ,W1=$w1,W2=$w2,W3=$w3,W4=$w4,W5=$w5";
|
||||
Log 4, "$name parsing: $rawtext";
|
||||
|
||||
#break into sensor specs
|
||||
switch ( $group ){
|
||||
case 7 {
|
||||
$sensor = "Fernbedienung";
|
||||
$shortname='FB';
|
||||
$einheit1='(CODE)';
|
||||
$daten1 = $w1 * 10000 + $w2 * 1000 + $w3 * 100 + $w4 * 10 + $w5;
|
||||
$result = $shortname . " => D=" . $daten1 . $einheit1;
|
||||
}
|
||||
case 0 {
|
||||
if ($snr < 8) {
|
||||
$sensor = "Temperatursensor V1.1(" . $snr . ")";
|
||||
}else{
|
||||
$snr -= 8;
|
||||
$sensor = "Temperatursensor V1.2(" .$snr. ")";
|
||||
}
|
||||
if ($w1 >= 64) {
|
||||
$daten1 = ((255 - $w1 - $w2) / 10) * (-1);
|
||||
}else{
|
||||
$daten1 = (($w1 * 128 + $w2) / 10);
|
||||
}
|
||||
$shortname='TX'.$snr;
|
||||
$einheit1 = " C";
|
||||
$result = $shortname . " => T=" . $daten1 . $einheit1;
|
||||
|
||||
}
|
||||
case 1 {
|
||||
if ($snr <8) {
|
||||
$sensor = "Temperatursensor mit Feuchte V1.1(" . $snr . ")";
|
||||
}else{
|
||||
$snr -= 8;
|
||||
$sensor = "Temperatursensor mit Feuchte V1.2(" . $snr . ")";
|
||||
}
|
||||
if ($w1 >= 64) {
|
||||
$daten1 = ((255 - $w1 - $w2) / 10) * (-1);
|
||||
}else{
|
||||
$daten1 = (($w1 * 128 + $w2) / 10);
|
||||
}
|
||||
$shortname='TH'.$snr;
|
||||
$einheit1 = " C";
|
||||
$daten2 = $w3;
|
||||
$daten3 = 0;
|
||||
$einheit2 = " %";
|
||||
|
||||
$result = $shortname . " => T=" . $daten1 . $einheit1 . ", H=" . $daten2 .$einheit2;
|
||||
|
||||
|
||||
}
|
||||
case 2 {
|
||||
if ( $snr < 8 ) {
|
||||
$sensor = "Regensensor V1.1(" . $snr . ")";
|
||||
}else{
|
||||
$snr -= 8;
|
||||
$sensor = "Regensensor V1.2(" . $snr . ")"
|
||||
}
|
||||
$shortname='R'.$snr;
|
||||
$daten1 = ($w1 * 128 + $w2);
|
||||
$einheit1= ' Imp';
|
||||
my $prev=$hash->{READINGS}{$shortname}{VAL};
|
||||
if ($prev && $prev=~/C=(\d+)/i) {
|
||||
$prev=$1;
|
||||
}else {
|
||||
$prev=0;
|
||||
}
|
||||
my $diff=$daten1-$prev;
|
||||
$daten2= $diff * $factor/1000;
|
||||
$einheit2 = " l/m²";
|
||||
$result = $shortname . " => M=".$daten2. $einheit2."(". $diff . $einheit1 ." x Faktor $factor), C=$daten1, P=$prev" ;
|
||||
|
||||
}
|
||||
case 3 {
|
||||
if ($snr < 8) {
|
||||
$sensor = "Windsensor V1.1(" . $snr . ")";
|
||||
}else{
|
||||
$snr -= 8;
|
||||
$sensor = "Windsensor V1.2(" . $snr . ")";
|
||||
}
|
||||
switch( $w3) {
|
||||
case 0 { $daten3 = 0;}
|
||||
case 1 { $daten3 = 22.5;}
|
||||
case 2 { $daten3 = 45;}
|
||||
case 3 { $daten3 = 67.5;}
|
||||
}
|
||||
$einheit3 = " +/-";
|
||||
$daten1 = ($w1 * 128 + $w2) / 10;
|
||||
$daten2 = $w4 * 128 + $w5;
|
||||
$einheit1 = " km/h";
|
||||
$einheit2 = " Grad";
|
||||
$shortname='W'.$snr;
|
||||
my @wr=("N","NNO","NO","ONO","O","OSO","SO","SSO","S","SSW","SW","WSW","W","WNW","NW","NNW");
|
||||
my @bf=(0,0.7,5.4,11.9,19.4,38.7,49.8,61.7,74.6,88.9,102.4,117.4);
|
||||
my @bfn=("Windstille","leiser Zug","leichte Brise","schwache Brise","maessige Brise","frische Brise",
|
||||
"starker Wind","steifer Wind","stürmischer Wind","Sturm","schweer Sturm","orkanartiger Sturm","Orkan");
|
||||
my $i=1;
|
||||
foreach (1..$#bf) {
|
||||
if ($daten1<$bf[$i]) {
|
||||
last;
|
||||
}
|
||||
$i++;
|
||||
}
|
||||
$i--;
|
||||
#windrichtung
|
||||
my $w=int($daten2/22.5+0.5);
|
||||
if ($w ==16) {$w=0;}
|
||||
$result = $shortname . " => S=" . $daten1 . $einheit1 . ", B=$i($bfn[$i]),D=" . $daten2 . $einheit2 . "($wr[$w]) $einheit3 $daten3";
|
||||
|
||||
}
|
||||
case 4 {
|
||||
if ($snr < 8) {
|
||||
$sensor = "Innensensor V1.1(" . $snr . ")";
|
||||
}else{
|
||||
$snr -= 8;
|
||||
$sensor = "Innensensor V1.2(" . $snr . ")";
|
||||
}
|
||||
if ($w1 >= 64) {
|
||||
$daten1 = ((255 - $w1 - $w2) / 10) * (-1);
|
||||
}else{
|
||||
$daten1 = (($w1 * 128 + $w2) / 10);
|
||||
}
|
||||
$shortname='I'.$snr;
|
||||
$daten2 = $w3;
|
||||
$daten3 = $w4 * 128 + $w5;
|
||||
$einheit1 = " C";
|
||||
$einheit2 = " %";
|
||||
$einheit3 = " hPa";
|
||||
$result = $shortname . " => T=" . $daten1 . $einheit1 . ", H=" . $daten2 . $einheit2 . ", D=" . $daten3 . $einheit3;
|
||||
|
||||
}
|
||||
case 5 {
|
||||
$snr -= 8 if $snr>7;; #only V1.2 sensors exists
|
||||
$sensor = "Helligkeitssensor V1.2(" . $snr . ")";
|
||||
$shortname='L'.$snr;
|
||||
switch ($w3) {
|
||||
case 0 {$daten1 = 1;}
|
||||
case 1 {$daten1 = 10;}
|
||||
case 2 {$daten1 = 100;}
|
||||
case 3 {$daten1 = 1000;}
|
||||
}
|
||||
$daten1 = $daten1 * ($w1 * 128 + $w2);
|
||||
$einheit1 = "Lux";
|
||||
$result = $shortname . " => L=" . $daten1 . $einheit1;
|
||||
|
||||
}
|
||||
case 6 {
|
||||
#Sensor has been never produced, but maybe there are personal implementations
|
||||
$snr -= 8 if $snr>7;
|
||||
$sensor = "Pyranometer V1.2(" . $snr . ")";
|
||||
$shortname='P'.$snr;
|
||||
switch ($w3) {
|
||||
case 0 {$daten1 = 1;}
|
||||
case 1 {$daten1 = 10;}
|
||||
case 2 {$daten1 = 100;}
|
||||
case 3 {$daten1 = 1000;}
|
||||
}
|
||||
$daten1 = $daten1 * ($w1 * 128 + $w2);
|
||||
$einheit1 = " W/m²";
|
||||
$result = $shortname . " => P=" . $daten1 . $einheit1;
|
||||
|
||||
}
|
||||
else {
|
||||
$shortname="U";
|
||||
$sensor = "unknown";
|
||||
$daten1 = $typ;
|
||||
$result = "(Group:" . $group . "/Typ:" . $typ . ")";
|
||||
Log 1, "$name => Unknown sensor detected". $result
|
||||
}#switch else
|
||||
|
||||
}#switch
|
||||
|
||||
#store result
|
||||
Log 4, $name." result:".$result;
|
||||
$rawtext='RAW => '.$rawtext;
|
||||
$hash->{READINGS}{LAST}{VAL}=$result;
|
||||
$hash->{READINGS}{LAST}{TIME}=$tm;
|
||||
$hash->{READINGS}{RAW}{TIME}=time();
|
||||
$hash->{READINGS}{RAW}{VAL}=$msg;
|
||||
$hash->{READINGS}{$shortname}{VAL}=$result;
|
||||
$hash->{READINGS}{$shortname}{TIME}=$tm;
|
||||
$hash->{STATE}=$result;
|
||||
$hash->{CHANGED}[0] = $result;
|
||||
$hash->{CHANGETIME}[0]=$tm;
|
||||
$hash->{CHANGED}[1] = $rawtext;
|
||||
$hash->{CHANGETIME}[1]=$tm;
|
||||
#notify system
|
||||
DoTrigger($name, undef);
|
||||
return $result;
|
||||
}
|
||||
#####################################
|
||||
sub
|
||||
WS2000_List($$)
|
||||
{
|
||||
my ($hash,$msg) = @_;
|
||||
$msg=WS2000_Get($hash,$hash->{NAME},'list');
|
||||
return $msg;
|
||||
}
|
||||
|
||||
|
||||
1;
|
353
fhem/contrib/ws2000_reader.pl
Normal file
353
fhem/contrib/ws2000_reader.pl
Normal file
@ -0,0 +1,353 @@
|
||||
#!perl
|
||||
#
|
||||
# read_ws2000 from device (elv art-Nr. 390-61) and provides socket service (like XPORT),
|
||||
# or act as client for this service
|
||||
# licence GPL2
|
||||
# Thomas Dressler 2008
|
||||
# $Id: ws2000_reader.pl,v 1.1 2008-05-10 21:10:38 tdressler Exp $
|
||||
|
||||
use Switch;
|
||||
use strict;
|
||||
use IO::Socket;
|
||||
use IO::Select;
|
||||
|
||||
our $PortObj;
|
||||
my $server;
|
||||
our $xport;
|
||||
our $sel;
|
||||
our $clients;
|
||||
our %sockets;
|
||||
my ($datum,$data,$lastdate,$lastdata);
|
||||
$SIG{INT}=$SIG{TERM}=\&signalhandler;
|
||||
$|=1;
|
||||
|
||||
PortOpen($ARGV[0]);
|
||||
|
||||
my $ServerPort=$ARGV[1];
|
||||
if ($ServerPort) {
|
||||
$server=IO::Socket::INET->new(LocalPort=>$ServerPort,
|
||||
ReuseAddr=>1,
|
||||
Listen=>10,
|
||||
timeout=>1,
|
||||
blocking=>0
|
||||
);
|
||||
die "ServerStart-Error: $@ \n" if !$server;
|
||||
$clients=IO::Select->new($server);
|
||||
}
|
||||
|
||||
|
||||
MAINLOOP:
|
||||
for(;;) {
|
||||
if($server) {
|
||||
#only for server mode
|
||||
my %message=undef;
|
||||
my $fh;
|
||||
while(my @ready=$clients->can_read(1)) {
|
||||
foreach $fh (@ready) {
|
||||
if ($fh==$server) {
|
||||
#new connection waiting
|
||||
my $new=$server->accept;
|
||||
$new->autoflush(1);
|
||||
$new->blocking(0);
|
||||
$new->timeout(1);
|
||||
$clients->add($new);
|
||||
my $host=$new->peerhost;
|
||||
$sockets{$new}=$host;
|
||||
print "\nNew Client:".$host."\n";
|
||||
#}else{
|
||||
# my $out=undef;
|
||||
# $fh->read($out,1);
|
||||
# $message{$fh}=$out;
|
||||
}
|
||||
}
|
||||
}
|
||||
#check living connections
|
||||
foreach my $fh($clients->handles) {
|
||||
next if $fh==$server;
|
||||
next if ($fh->peerhost);
|
||||
print "Terminate ".$sockets{$fh}. " by Check1\n";
|
||||
Client_disconnect($fh);
|
||||
}
|
||||
|
||||
}
|
||||
$data=get_data();
|
||||
next if ! $data;
|
||||
$datum = time();
|
||||
next if ($data eq $lastdata) && (($datum - $lastdate) < 30);
|
||||
my $result=decode_data($data);
|
||||
print scalar localtime().":".$result."\n";
|
||||
$lastdata = $data;
|
||||
$lastdate = $datum;
|
||||
}
|
||||
PortClose();
|
||||
exit 0;
|
||||
#--------SUBs -----------------------------
|
||||
sub Clientdisconnect{
|
||||
my $fh=shift;
|
||||
print "\n Client ".$sockets{$fh}." disconnected!\n";
|
||||
$clients->remove($fh);
|
||||
delete $sockets{$fh};
|
||||
$fh->shutdown(2);
|
||||
$fh->close;
|
||||
}
|
||||
sub PortOpen{
|
||||
my $PortName=shift;
|
||||
my $quiet=shift ||undef;
|
||||
|
||||
if ($PortName=~/^\/dev|^COM/) {
|
||||
#normal devices (/dev)
|
||||
my $OS=$^O;
|
||||
if ($OS eq 'MSWin32') {
|
||||
eval ("use Win32::SerialPort;");
|
||||
die "$@\n" if ($@);
|
||||
$PortObj = new Win32::SerialPort ($PortName, $quiet)
|
||||
|| die "Can't open $PortName: $^E\n"; # $quiet is optional
|
||||
} else {
|
||||
eval ("use Device::SerialPort;");
|
||||
die "$@\n" if ($@);
|
||||
$PortObj = new Device::SerialPort ($PortName, $quiet)
|
||||
|| die "Can't open $PortName: $^E\n"; # $quiet is optional
|
||||
|
||||
}
|
||||
#Parameter 19200,8,2,Odd,None
|
||||
$PortObj->baudrate(19200);
|
||||
$PortObj->databits(8);
|
||||
$PortObj->parity("odd");
|
||||
$PortObj->stopbits(2);
|
||||
$PortObj->handshake("none");
|
||||
if (! $PortObj->write_settings) {
|
||||
undef $PortObj;
|
||||
die "Write Settings failed!\n";
|
||||
}
|
||||
$sel=IO::Select->new($PortObj->{FD} );
|
||||
}elsif($PortName=~/([\w.]+):(\d{1,5})/){
|
||||
#Sockets(hostname:port)
|
||||
my $host=$1;
|
||||
my $port=$2;
|
||||
$xport=IO::Socket::INET->new(PeerAddr=>$host,
|
||||
PeerPort=>$port,
|
||||
timeout=>1,
|
||||
blocking=>0
|
||||
);
|
||||
die "Cannot connect to $PortName -> $@ ( $!) \n" if ! $xport;
|
||||
$xport->autoflush(1);
|
||||
$sel=IO::Select->new($xport);
|
||||
}else{
|
||||
die "$PortName is no device and not implemented!\n";
|
||||
}
|
||||
}
|
||||
sub PortClose {
|
||||
$PortObj->close if ($PortObj);
|
||||
if ($xport) {
|
||||
$clients->remove($xport) if $clients;
|
||||
$xport->shutdown(2);
|
||||
$xport->close;
|
||||
}
|
||||
if ($clients) {
|
||||
foreach my $socket($clients->handles) {
|
||||
Clientdisconnect($socket);
|
||||
}
|
||||
}
|
||||
}
|
||||
sub signalhandler {
|
||||
my $signal=shift;
|
||||
PortClose();
|
||||
print "\nTerminated by Signal $signal!\n";
|
||||
exit;
|
||||
}
|
||||
|
||||
sub get_data {
|
||||
|
||||
my $STX=2;
|
||||
my $ETX=3;
|
||||
my $retval='';
|
||||
my $status='';
|
||||
my $out=undef;
|
||||
my $message;
|
||||
my $byte;
|
||||
for(;;) {
|
||||
#sleep 1 if(!defined($out) || length($out) == 0);
|
||||
$out=undef;
|
||||
if ($xport) {
|
||||
|
||||
#my @readable=$select->can_read(1);
|
||||
#next if $#readable<0;
|
||||
|
||||
|
||||
#my $fh;
|
||||
#foreach $fh (@readable) {
|
||||
|
||||
next if ! $sel->can_read(1);
|
||||
$xport->read($out,1);
|
||||
#if ($xport->eof) {
|
||||
# print "Xport eof\n";
|
||||
# print "Server disconnected, terminate!\n";
|
||||
# PortClose();
|
||||
# exit 1;
|
||||
|
||||
#}
|
||||
|
||||
|
||||
|
||||
}elsif($PortObj) {
|
||||
$out = $PortObj->read(1);
|
||||
}
|
||||
next if(!defined($out) || length($out) == 0) ;
|
||||
|
||||
|
||||
$byte=ord($out);
|
||||
if($byte eq $STX) {
|
||||
#Log 4, "M232: return value \'" . $retval . "\'";
|
||||
$status= "STX";
|
||||
$message=$out;
|
||||
} elsif($byte eq $ETX) {
|
||||
$status= "ETX";
|
||||
$message .=$out;
|
||||
} elsif ($status eq "STX"){
|
||||
$byte=$byte & 0x7F;
|
||||
$retval .= chr($byte);
|
||||
$message .=$out;
|
||||
}
|
||||
last if($status eq "ETX");
|
||||
|
||||
}
|
||||
if ($server) {
|
||||
foreach my $client($clients->can_write(1)) {
|
||||
next if $client == $server;
|
||||
if (!$client->send($message)){
|
||||
Clientdisconnect($client);
|
||||
next;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return $retval;
|
||||
}
|
||||
|
||||
|
||||
sub decode_data {
|
||||
my ($sensor,$daten1,$einheit1,$daten2,$einheit2,$daten3,$einheit3,$result);
|
||||
my $data=shift;
|
||||
my ($typ,$w1,$w2,$w3,$w4,$w5)=unpack("U*",$data);
|
||||
my $group = ($typ & 0x70)/16 ;#/slash for komodo syntax checker!
|
||||
my $snr = $typ % 16;
|
||||
|
||||
|
||||
switch ( $group ){
|
||||
case 7 {
|
||||
$sensor = "Fernbedienung";
|
||||
$daten1 = $w1 * 10000 + $w2 * 1000 + $w3 * 100 + $w4 * 10 + $w5;
|
||||
$result = $sensor . "=" . $daten1 . $einheit1;
|
||||
}
|
||||
case 0 {
|
||||
if ($snr < 8) {
|
||||
$sensor = "Temperatursensor V1.1(" . $snr . ")";
|
||||
}else{
|
||||
$sensor = "Temperatursensor V1.2(" .($snr - 8). ")";
|
||||
}
|
||||
if ($w1 >= 64) {
|
||||
$daten1 = ((255 - $w1 - $w2) / 10) * (-1);
|
||||
}else{
|
||||
$daten1 = (($w1 * 128 + $w2) / 10);
|
||||
}
|
||||
$einheit1 = " °C";
|
||||
$result = $sensor . "=" . $daten1 . $einheit1;
|
||||
}
|
||||
case 1 {
|
||||
if ($snr <8) {
|
||||
$sensor = "Temperatursensor mit Feuchte V1.1(" . $snr . ")";
|
||||
}else{
|
||||
$sensor = "Temperatursensor mit Feuchte V1.2(" . ($snr - 8) . ")";
|
||||
}
|
||||
if ($w1 >= 64) {
|
||||
$daten1 = ((255 - $w1 - $w2) / 10) * (-1);
|
||||
}else{
|
||||
$daten1 = (($w1 * 128 + $w2) / 10);
|
||||
}
|
||||
|
||||
$daten2 = $w3;
|
||||
$daten3 = 0;
|
||||
$einheit1 = " °C";
|
||||
$einheit2 = " %";
|
||||
|
||||
$result = $sensor . "=" . $daten1 . $einheit1 . " " . $daten2 .$einheit2;
|
||||
}
|
||||
case 2 {
|
||||
if ( $snr < 8 ) {
|
||||
$sensor = "Regensensor V1.1(" . $snr . ")";
|
||||
}else{
|
||||
$sensor = "Regensensor V1.2(" . ($snr - 8) . ")"
|
||||
}
|
||||
$daten1 = ($w1 * 128 + $w2) * 0.36;
|
||||
$einheit1 = " l/m²";
|
||||
$result = $sensor . "=" . $daten1 . $einheit1;
|
||||
}
|
||||
case 3 {
|
||||
if ($snr < 8) {
|
||||
$sensor = "Windsensor V1.1(" . $snr . ")";
|
||||
}else{
|
||||
$sensor = "Windsensor V1.2(" & ($snr - 8) . ")";
|
||||
}
|
||||
switch( $w3) {
|
||||
case 0 { $einheit3 = "±0 °";}
|
||||
case 1 { $einheit3 = "± 22,5 °";}
|
||||
case 2 { $einheit3 = "± 45 °";}
|
||||
case 3 { $einheit3 = "± 67,5 °";}
|
||||
}
|
||||
$daten1 = ($w1 * 128 + $w2) / 10;
|
||||
$daten2 = $w4 * 128 + $w5;
|
||||
$einheit1 = " km/h";
|
||||
$einheit2 = " °";
|
||||
$result = $sensor . "=" . $daten1 . $einheit1 . " " . $daten2 . $einheit2 . " " . $einheit3;
|
||||
}
|
||||
case 4 {
|
||||
if ($snr < 8) {
|
||||
$sensor = "Innensensor V1.1(" . $snr . ")";
|
||||
}else{
|
||||
$sensor = "Innensensor V1.2(" . ($snr - 8) . ")";
|
||||
}
|
||||
if ($w1 >= 64) {
|
||||
$daten1 = ((255 - $w1 - $w2) / 10) * (-1);
|
||||
}else{
|
||||
$daten1 = (($w1 * 128 + $w2) / 10);
|
||||
}
|
||||
$daten2 = $w3;
|
||||
$daten3 = $w4 * 128 + $w5;
|
||||
$einheit1 = " °C";
|
||||
$einheit2 = " %";
|
||||
$einheit3 = " hPa";
|
||||
$result = $sensor . "=" . $daten1 . $einheit1 . " " . $daten2 . $einheit2 . " " . $daten3 . $einheit3;
|
||||
}
|
||||
case 5 {
|
||||
$sensor = "Helligkeitssensor V1.2(" . ($snr - 8) . ")";
|
||||
switch ($w3) {
|
||||
case 0 {$daten1 = 1;}
|
||||
case 1 {$daten1 = 10;}
|
||||
case 2 {$daten1 = 100;}
|
||||
case 3 {$daten1 = 1000;}
|
||||
}
|
||||
$daten1 = $daten1 * ($w1 * 128 + $w2);
|
||||
$einheit1 = "Lux";
|
||||
$result = $sensor . "=" . $daten1 . $einheit1;
|
||||
}
|
||||
case 6 {
|
||||
$sensor = "Pyranometer V1.2(" . ($snr - 8) . ")";
|
||||
switch ($w3) {
|
||||
case 0 {$daten1 = 1;}
|
||||
case 1 {$daten1 = 10;}
|
||||
case 2 {$daten1 = 100;}
|
||||
case 3 {$daten1 = 1000;}
|
||||
}
|
||||
$daten1 = $daten1 * ($w1 * 128 + $w2);
|
||||
$einheit1 = " W/m²";
|
||||
$result = $sensor . "=" . $daten1 . $einheit1;
|
||||
}
|
||||
else {
|
||||
$sensor = "Störung";
|
||||
$daten1 = $typ;
|
||||
$result = $sensor . "(Group:" . $group . "/Typ:" . $typ . ")";
|
||||
}#switch else
|
||||
|
||||
}#switch
|
||||
return $result;
|
||||
}
|
Loading…
Reference in New Issue
Block a user