1 | #!/usr/bin/perl |
---|
2 | #------------------------------------------------------------------------------- |
---|
3 | # This file is part of the FLARM¨-Radar Project. |
---|
4 | # |
---|
5 | # Copyright 2013 Netzschmiede GmbH (http://www.netzschmiede.ch) |
---|
6 | # |
---|
7 | # Licensed under the Apache License, Version 2.0 (the "License"); |
---|
8 | # you may not use this file except in compliance with the License. |
---|
9 | # You may obtain a copy of the License at |
---|
10 | # |
---|
11 | # http://www.apache.org/licenses/LICENSE-2.0 |
---|
12 | # |
---|
13 | # Unless required by applicable law or agreed to in writing, software |
---|
14 | # distributed under the License is distributed on an "AS IS" BASIS, |
---|
15 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
16 | # See the License for the specific language governing permissions and |
---|
17 | # limitations under the License. |
---|
18 | # |
---|
19 | # Project Website: www.flarmradar.ch |
---|
20 | # Email: info@flarmradar.ch |
---|
21 | #------------------------------------------------------------------------------- |
---|
22 | |
---|
23 | use strict; |
---|
24 | use warnings; |
---|
25 | use Getopt::Std; |
---|
26 | use File::Basename; |
---|
27 | use POSIX qw/strftime/; |
---|
28 | use LWP::UserAgent; |
---|
29 | |
---|
30 | my %config; |
---|
31 | my %options; |
---|
32 | my $ua; |
---|
33 | |
---|
34 | # default values |
---|
35 | my $cfile = "$ENV{'HOME'}/.flarmclient/client.config"; |
---|
36 | my $fifo = "$ENV{'HOME'}/.flarmclient/client.fifo"; |
---|
37 | |
---|
38 | # functions |
---|
39 | sub usage { |
---|
40 | print <<EOF; |
---|
41 | NAME |
---|
42 | $0 -- stream flarm data to server |
---|
43 | |
---|
44 | SYNOPSIS |
---|
45 | $0 [-c config_file] [-f data_file] [-h] |
---|
46 | |
---|
47 | DESCRIPTION |
---|
48 | The following options are available: |
---|
49 | |
---|
50 | -c Use the specified configuration file. Use the default configuration |
---|
51 | file as a starting point for customization. |
---|
52 | |
---|
53 | -f Read the data from the specified data file. This is mainly used for |
---|
54 | testing and development. |
---|
55 | |
---|
56 | -h Print this help. |
---|
57 | |
---|
58 | EOF |
---|
59 | exit 0; |
---|
60 | } |
---|
61 | |
---|
62 | sub cleanup { |
---|
63 | if (-e "$fifo") { |
---|
64 | unlink($fifo) || die("unable to remove $fifo: $!"); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | sub readconfig { |
---|
69 | open(CONF, "< $cfile") || die("failed to open config file for reading: $!"); |
---|
70 | while(my $line = <CONF>) { |
---|
71 | chomp($line); |
---|
72 | next if $line =~ /^\s*#/; |
---|
73 | next if $line =~ /^\s*$/; |
---|
74 | if ($line =~ /^\s*(\S*)\s*=\s*(\S*)\s*$/) { |
---|
75 | $config{$1} = $2; |
---|
76 | } |
---|
77 | } |
---|
78 | close(CONF); |
---|
79 | } |
---|
80 | |
---|
81 | # send the records to the server. We don't make a request for each record for |
---|
82 | # performance reasons. |
---|
83 | sub flush { |
---|
84 | my ($records, $url) = @_; |
---|
85 | my $date = strftime "%Y/%m/%d", localtime; |
---|
86 | my $resturl = $url . "/" . $date; |
---|
87 | my $request = HTTP::Request->new('PUT'); |
---|
88 | $request->url($resturl); |
---|
89 | $request->header('stationKey'=>$config{'key'}); |
---|
90 | $request->content($records); |
---|
91 | # run the request |
---|
92 | my $response = $ua->request($request); |
---|
93 | # analyze the response |
---|
94 | my $code = $response->code; |
---|
95 | $response->code == 200 || print("error processing records (code=" . $response->code . ") " . $records . "\n"); |
---|
96 | } |
---|
97 | |
---|
98 | # parse options |
---|
99 | getopts('c:f:h', \%options); |
---|
100 | |
---|
101 | # read config file |
---|
102 | if (defined($options{'c'})) { |
---|
103 | $cfile = $options{'c'}; |
---|
104 | } |
---|
105 | if (defined($options{'h'})) { |
---|
106 | usage(); |
---|
107 | } |
---|
108 | readconfig(); |
---|
109 | |
---|
110 | # validation: key must be present in config file |
---|
111 | die("no key found in config file (option: key)") unless defined($config{'key'}); |
---|
112 | |
---|
113 | # remove old leftovers |
---|
114 | cleanup(); |
---|
115 | |
---|
116 | # create pipe |
---|
117 | die("no fifo found in config file (option: fifo)") unless defined($fifo); |
---|
118 | if (! -d dirname($fifo)) { |
---|
119 | system("mkdir", "-p", dirname($fifo)) == 0 || die("failed to create fifo directory " . dirname($fifo) . ": $!") |
---|
120 | } |
---|
121 | system("mkfifo", $fifo) == 0 || die("failed to create fifo: $!"); |
---|
122 | |
---|
123 | # force a flush right away and after every write or print |
---|
124 | local $| = 1; |
---|
125 | |
---|
126 | # fork minicom and write to pipe |
---|
127 | defined( my $pid = fork() ) or die "can't fork: $!"; |
---|
128 | unless ($pid) { |
---|
129 | # we're the child |
---|
130 | open( STDIN, '/dev/null' ) or die "can't read /dev/null: $!"; |
---|
131 | |
---|
132 | if (defined($options{'f'})) { |
---|
133 | open(DATA, "< $options{'f'}") || die("failed to open data file $options{'f'}: $!"); |
---|
134 | open(FIFO, "> $fifo") || die("failed to open fifo for writing: $!"); |
---|
135 | while(my $line = <DATA>) { |
---|
136 | chomp($line); |
---|
137 | next if ($line =~ /^\s*$/); |
---|
138 | print FIFO $line, "\n" || die("failed to execute child command: $!"); |
---|
139 | } |
---|
140 | close(DATA); |
---|
141 | close(FIFO); |
---|
142 | } else { |
---|
143 | system("minicom", "-C", $fifo) == 0 || die("failed to start minicom: $!"); |
---|
144 | } |
---|
145 | exit 0; |
---|
146 | } |
---|
147 | |
---|
148 | # create UserAgent object |
---|
149 | $ua = new LWP::UserAgent; |
---|
150 | my $buf; |
---|
151 | # read data from pipe |
---|
152 | open(FIFO, "< $fifo") || die("failed to open fifo for reading: $!"); |
---|
153 | while(my $record = <FIFO>) { |
---|
154 | chomp($record); |
---|
155 | $buf = (defined($buf)) ? "$buf;$record" : $record; |
---|
156 | if ($record =~ /^\$GPGGA,/) { |
---|
157 | flush($buf, $config{'url'}); |
---|
158 | sleep(1); |
---|
159 | $buf = undef; |
---|
160 | } |
---|
161 | } |
---|
162 | close(FIFO); |
---|
163 | |
---|
164 | cleanup(); |
---|
165 | exit 0; |
---|
166 | |
---|