-
Notifications
You must be signed in to change notification settings - Fork 4
/
mysensors-if.pl
108 lines (97 loc) · 2.89 KB
/
mysensors-if.pl
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/perl -w
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
# Author: epierre
use warnings;
use strict;
use POSIX qw(strftime);
use Device::SerialPort;
use IO::Handle;
use DateTime;
use Scalar::Util qw(looks_like_number);
use DBI;
use Config::Simple;
# Initialization strings
my $conf = ".conf-mysensors";
my $ccnt;
my $cfg;
my ($count, $string, $radioId, $value);
my $Config=&read_conf($conf);
my $port = $Config->{"Internal.usb_port"};
my $gw_ip = $Config->{"Internal.gw_ip"};
my $gw_port = $Config->{"Internal.gw_port"};
my $dbh;
my %sensor_tab;
# USB port opening
my $ob = &connect_usb($port);
$ob->close || warn "close failed";;
$ob = &connect_usb($port);
my $sleep = 5;
print "Sleeping $sleep second to let arduino get ready...\n";
sleep $sleep;
# Now parse output
my @vals;
open(FIC,">>log-gw.txt")||die $!;
print FIC "Starting\n";
FIC->autoflush(1);
while(1) {
$ccnt++;
$ob->lastline("\n");
($count, $string) = $ob->read(255);
#print "$ccnt $string\n\n";
@vals = split("\n", $string);
foreach (@vals) {
$_=~ s/\t/\=/;
$_=~ s/\r//;
$_=~ s/\n//;
my ($radioId, $childId,$messageType,$subType,$payload) = split(";", $_);
if (! $childId) {$childId="0";}
if (! $messageType) {$messageType="0";}
if (! $subType) {$subType="0";}
if (! $payload) {$payload="0";}
next unless ($radioId);
next if (! looks_like_number $radioId);
#$value = 0 unless ($value);
#$value = $value/1000 if $radioId =~ /I/g;
my $dt = DateTime->now;
my $date=join ' ', $dt->ymd, $dt->hms;
print "$date $radioId $childId $messageType $subType $payload\n";
if ($radioId==1) {
print FIC "$date $radioId $childId $messageType $subType $payload\n";
}
`curl -s "http://$gw_ip:$gw_port/message/$radioId/$childId/$messageType/$subType/$payload" &`;
}
sleep(1);
}
close(FIC);
$ob->write_drain;
$ob->close;
undef $ob;
sub connect_usb {
my $port=$_[0];
Device::SerialPort->new($port, 1) || die "Can't open $port: $ +!";
$ob = Device::SerialPort->new($port, 1) || die "Can't open $port: $ +!";
$ob->databits(8);
$ob->baudrate(115200);
$ob->parity("none");
$ob->stopbits(1);
$ob->buffers( 4096, 4096 );
$ob->write_settings();
return $ob;
}
sub read_conf {
my $conf=$_[0];
$cfg = new Config::Simple($conf) or die Config::Simple->error();;
$cfg->autosave(1);
# getting the values as a hash:
my %Config = $cfg->vars();
return \%Config;
#$user = $cfg->param("mysql.user")
#$cfg->param("User", "tom");
#$cfg->delete('mysql.user');
}
sub save_conf {
$cfg=$_[0];
$cfg->save();
}