1 | #!/usr/bin/perl -w
|
---|
2 | use warnings;
|
---|
3 | use strict;
|
---|
4 |
|
---|
5 | use JSON::XS qw{};
|
---|
6 | use IO::Socket::INET6 qw{};
|
---|
7 |
|
---|
8 | package gpsd
|
---|
9 | {
|
---|
10 | local ($gpsd::hasgps, $gpsd::self) = (1);
|
---|
11 | my ($json, $socket) = (JSON::XS->new, IO::Socket::INET6->new(PeerAddr => '127.0.0.1',PeerPort => '2947'));
|
---|
12 | sub new
|
---|
13 | {
|
---|
14 | my $class = shift;
|
---|
15 | $gpsd::self = {};
|
---|
16 | bless($gpsd::self, $class);
|
---|
17 | return $gpsd::self;
|
---|
18 | }
|
---|
19 |
|
---|
20 | sub name
|
---|
21 | {
|
---|
22 | return "GPSD";
|
---|
23 | }
|
---|
24 |
|
---|
25 |
|
---|
26 | sub doupstart
|
---|
27 | {
|
---|
28 | return 1;
|
---|
29 | }
|
---|
30 |
|
---|
31 | sub upstart
|
---|
32 | {
|
---|
33 | print "Detecting if GPSD is running...\n";
|
---|
34 | if (!defined($socket))
|
---|
35 | {
|
---|
36 | $gpsd::hasgps = 0;
|
---|
37 | }
|
---|
38 | else
|
---|
39 | {
|
---|
40 | $gpsd::hasgps = 1;
|
---|
41 | $socket->send("?WATCH={\"enable\":true};\n");
|
---|
42 | my $line;
|
---|
43 | $line = $socket->getline;
|
---|
44 | $line = $socket->getline;
|
---|
45 | $line = $socket->getline;
|
---|
46 | }
|
---|
47 | if ($gpsd::hasgps)
|
---|
48 | {
|
---|
49 | print "GPSD detected... Enabling Location awareness...\n";
|
---|
50 | undef($main::settings{'gps'});
|
---|
51 | $main::settings{'gps'} = $gpsd::self;
|
---|
52 | print "GPS configured.\nLocation awareness enabled.\n";
|
---|
53 |
|
---|
54 | }
|
---|
55 | else
|
---|
56 | {
|
---|
57 | print "GPSD not running, Not enabling GPSD location awareness.\n";
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | sub location
|
---|
62 | {
|
---|
63 | $socket->send("?POLL;\n");
|
---|
64 | my $line;
|
---|
65 | $line = $socket->getline;
|
---|
66 |
|
---|
67 | if (defined($line))
|
---|
68 | {
|
---|
69 | chomp $line;
|
---|
70 | my %data = %{ $json->decode($line)};
|
---|
71 | my %tpv = %{$data{'tpv'}[0]};
|
---|
72 | my ($lat, $lon) = ($tpv{'lat'}, $tpv{'lon'});
|
---|
73 | return "Lat: $lat, Lon: $lon";
|
---|
74 | }
|
---|
75 | else
|
---|
76 | {
|
---|
77 | return "GPSD Didn't respond. Is it running?";
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | sub locsv
|
---|
82 | {
|
---|
83 | $socket->send("?POLL;\n");
|
---|
84 | my $line;
|
---|
85 | $line = $socket->getline;
|
---|
86 |
|
---|
87 | if (defined($line))
|
---|
88 | {
|
---|
89 | chomp $line;
|
---|
90 | my %data = %{ $json->decode($line)};
|
---|
91 | my %tpv = %{$data{'tpv'}[0]};
|
---|
92 | my ($lat, $lon) = ($tpv{'lat'}, $tpv{'lon'});
|
---|
93 | return "$lat, $lon";
|
---|
94 | }
|
---|
95 | else
|
---|
96 | {
|
---|
97 | return "0,0";
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | sub hasgps
|
---|
102 | {
|
---|
103 | return $gpsd::hasgps;
|
---|
104 | }
|
---|
105 |
|
---|
106 | sub defaultsettings
|
---|
107 | {
|
---|
108 | return undef;
|
---|
109 | }
|
---|
110 | 1; #Yay Packages having to end with a 1!
|
---|
111 | }
|
---|