use lib qw( d:/home/netsh100633/files/fwlib d:/home/netsh100633/files/fwlib/factory d:/home/netsh100633/html/cgi-bin ); use strict; use warnings; use JSON; use HTTPRequest; use PX1674; use FastEAV; require dd; my $main = bless{ comport => 'COM1', host => 'rolfrost', file => 'd:/tmp/dongle.cmp', debug => 1 },'main'; $main->request(); ########################################################################### # Status für Geräte vom Webservice abholen sub request{ my $self = shift; my $dal = FastEAV->new( file => $self->{file}, auto => 0 ) or die $@; my $host = $self->{host}; my $r = HTTPRequest->common( host => $host, uri => '/hausomat.html?getstate=1;json=1', method => 'GET' ) or die "Keine Verbindung"; my $json = JSON->new; my $slice = $json->decode($r->response_body); my %objects = (); foreach my $device(@$slice){ next unless $device->{devid}; my ($addr, $id) = split "-", $device->{devid}; my $state = $device->{type} eq 'PWM' ? $self->pwm($device->{state}, $device->{period}) : $device->{state}; push @{$objects{$addr}}, {state => $state, id => $id}; } foreach my $address(keys %objects){ my $px = Win32::SerialPort::PX1674->new( comport => $self->{comport}, address => hex($address), debug => $self->{debug} ); my $comp = $dal->checkout($address) || {}; foreach my $hunt(@{$objects{$address}}){ $comp->{$hunt->{id}} ||= ''; $px->switch($hunt->{state}, $hunt->{id}) if $comp->{$hunt->{id}} ne $hunt->{state}; $comp->{$hunt->{id}} = $hunt->{state}; $dal->checkin($address, %$comp); } $dal->write; } } # PulsWeitenModulation sub pwm{ my $self = shift; my $power = shift; my $period = shift; return 'On' if $power == 1; return 'Off' if $power == 0; my $nr = do "LfdNr.pm"; return ($nr % $period +1)/$period <= $power ? 'On' : 'Off'; }