Thread Typisiert Perl in pack()? (3 answers)
Opened by rosti at 2017-01-24 10:35

rosti
 2017-01-25 07:26
#185925 #185925
User since
2011-03-19
3237 Artikel
BenutzerIn
[Homepage]
user image
Es sind 2 Zeilen, weil es in einer Schleife läuft ;)

Ansonsten ist der Fall klar und richtig: mit pack() hats gar nichts zu tun--

PS/Edit: Der ganze Code siehe untenstehend und das Modul
http://rolfrost.de/px1673.html

und alles zusammen funktioniert absolut sauber.

Mehr dazu demnächst auf der Homepage zu meinem neuen Projekt Hausautomatisierung ;)

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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';
}

Last edited: 2017-01-25 07:33:41 +0100 (CET)

View full thread Typisiert Perl in pack()?