2
0
mirror of https://github.com/fhem/fhem-mirror.git synced 2025-01-31 12:49:34 +00:00

CULsim.pl: CUL and SCC simulator for STACKABLE.pm

git-svn-id: https://svn.fhem.de/fhem/trunk@13834 2b470e98-0d58-463d-a4d8-8e2adae1ed80
This commit is contained in:
rudolfkoenig 2017-03-28 15:44:28 +00:00
parent 916c7c4ba2
commit cbdfb65be1

106
fhem/contrib/CULsim.pl Normal file
View File

@ -0,0 +1,106 @@
#!/usr/bin/perl
# Used for SCC testing.
use strict;
use warnings;
use IO::Socket;
my $port = "12345";
my $serverSock = IO::Socket::INET->new(
Listen => 5,
LocalAddr => 'localhost',
LocalPort => $port,
Proto => 'tcp',
ReuseAddr => 1
);
die "Can't open server port: $!" if(!$serverSock);
print "Opened port $port\n";
my %selectlist;
$selectlist{$serverSock->fileno()} = $serverSock;
my $cnt=0;
for(;;) {
my ($rout,$rin) = ('','');
map { vec($rin, $_, 1) = 1; } keys %selectlist;
my $nfound = select($rout=$rin, undef, undef, 60);
die "select error: $!" if($nfound < 0);
if($nfound == 0) { # timeout
$cnt++;
my $msg = "";
if($cnt % 3 == 0) {
$msg = "T123400A62D04";
} elsif($cnt % 3 == 1) {
$msg = "*T123400A62D04";
} else {
$msg = "**T123400A62D04";
}
foreach my $fd (keys %selectlist) {
if($fd != $serverSock->fileno()) {
my $h = $selectlist{$fd};
print "$h->{addr}:$h->{port}: snd >$msg<\n";
syswrite($h->{sock}, $msg."\n");
}
}
}
foreach my $fd (keys %selectlist) {
next if(!vec($rout, $fd, 1));
my $h = $selectlist{$fd};
if($fd == $serverSock->fileno()) {
my @clientinfo = $h->accept();
if(!@clientinfo) {
print "Accept failed: $!\n";
} else {
my ($port, $iaddr) = sockaddr_in($clientinfo[1]);
my %hash = ( port => $port,
addr => inet_ntoa($iaddr),
sock => $clientinfo[0],
partial => "");
print "$hash{addr}:$hash{port}: Connect\n";
$selectlist{$clientinfo[0]->fileno()} = \%hash;
}
next;
}
my $buf;
if(sysread($h->{sock}, $buf, 256) <= 0) {
print "$h->{addr}:$h->{port}: left us\n";
delete $selectlist{$fd};
next;
}
$buf = $h->{partial} . $buf;
while($buf =~ m/\n/) {
my ($cmd, $rest) = split("\n", $buf, 2);
print "$h->{addr}:$h->{port}: rcv >$cmd<\n";
my $stars;
$cmd =~ m/^(\**)(.*)$/;
$stars = $1; $cmd = $2;
my $msg = "";
if($cmd eq "V") { $msg = "V 1.6".length($stars)." CUL868";
} elsif($cmd eq "T01") { $msg = "0000";
} elsif($cmd eq "?") { $msg = "? (? is unknown) Use one of t u x";
} elsif($cmd eq "t") { $msg = sprintf("%08X", (time()%86400)*125);
}
if($msg) {
print "$h->{addr}:$h->{port}: =>$stars$msg<\n";
syswrite($h->{sock}, $stars . $msg."\n")
}
$buf = $rest;
}
$h->{partial} = $buf;
}
}