summaryrefslogtreecommitdiffstats
path: root/radiator-plc/mt300n-v2/usr/bin/mqtt_if.pl
blob: f81b6e08fc8a0257c9e04a71ac8eff90af58173a (plain)
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
#!/usr/bin/env perl

use strict;

use Device::SerialPort;
use Net::MQTT::Simple;

sub chomp_harder($) {
    my $ret = shift;
    my $irs = $/;
    $/ = "\n";
    chomp $ret;
    $/ = "\r";
    chomp $ret;
    $/ = $irs;
    return $ret;
}

sub get_line($$) {
    my ( $port, $mqtt ) = @_;

    my $ret = "";

    while (1) {
        my ( $count, $byte ) = $port->read(1);
        $mqtt->tick();

        if ( $count > 0 ) {
            $ret .= $byte;
            return chomp_harder($ret) if $byte eq "\n";
        }
        else {
            $ret = chomp_harder($ret);
            return $ret;
        }
    }
}

my $plc_port = "/dev/ttyS1";
my $plc = new Device::SerialPort($plc_port) || die "can't open $plc_port\n";

$plc->baudrate(38400);
$plc->parity("none");
$plc->databits(8);
$plc->stopbits(1);
$plc->stty_icanon(0);

$plc->read_char_time(0);
$plc->read_const_time(1000);

my $mqtt = Net::MQTT::Simple->new("10.32.139.1");

sub mqtt_msg($$) {
    my ( $topic, $message ) = @_;

    $plc->write("MQTT $topic $message\r\n");
}

$mqtt->subscribe( 'cmnd/+/var1', \&mqtt_msg );
$mqtt->subscribe( 'cmnd/+/var2', \&mqtt_msg );
$mqtt->subscribe( 'cmnd/+/var3', \&mqtt_msg );

while (1) {
    my $line = get_line( $plc, $mqtt );
    next unless length($line) > 0;

    if ( $line =~ /MQTT\s+([^\s]+)\s+(.+)/ ) {
        $mqtt->publish( $1, $2 );
    }

    if ( $line =~ /MQTTR\s+([^\s]+)\s+(.+)/ ) {
        $mqtt->retain( $1, $2 );
    }

}