blib/lib/Geo/Local/Server.pm

filter: Statementstmt 81.2% 52 / 64 Branchbran 42.8% 12 / 28 Conditioncond 45.4% 5 / 11 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 100% 14 / 14 Podpod 100% 9 / 9 totaltotal 70.9% 93 / 131 SCAR 34.7
CC
19
Cov
67%
configfile
19.8
lonlathae
16.4
envname
3.1
CRAP 32.0
↑ Summary
1 package Geo::Local::Server;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use base qw{Package::New};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Config::IniFiles qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Path::Class qw{file};
Subroutine BEGIN: called CC=1 SCAR=0.0
7 #runtime use Win32 if Windows
8 #runtime use Sys::Config unless Windows
9  
10 3 our $VERSION = '0.09';
11  
12 =head1 NAME
13  
14 Geo::Local::Server - Returns the configured coordinates of the local server
15  
16 =head1 SYNOPSIS
17  
18 use Geo::Local::Server;
19 my $gls=Geo::Local::Server->new;
20 my ($lat, $lon)=$gls->latlon;
21  
22 =head1 DESCRIPTION
23  
24 Reads coordinates from either the user environment variable COORDINATES_WGS84_LON_LAT_HAE or the file /etc/local.coordinates or C:\Windows\local.coordinates.
25  
26 =head1 USAGE
27  
28 =head2 Scripts
29  
30 Typical use is with the provided scripts
31  
32 is_nighttime && power-outlet WeMo on host mylamp
33 is_daytime && power-outlet WeMo off host mylamp
34 echo "power-outlet WeMo on host mylamp" | at `sunset_time`
35 echo "power-outlet WeMo off host mylamp" | at `sunrise_time`
36  
37 =head2 One Liner
38  
39 $ perl -MGeo::Local::Server -e 'printf "Lat: %s, Lon: %s\n", Geo::Local::Server->new->latlon'
40 Lat: 38.780276, Lon: -77.386706
41  
42 =head1 METHODS
43  
44 =head2 latlon, latlong
45  
46 Returns a list of latitude, longitude
47  
48 =cut
49  
50 2 ▶sub latlon {(shift->lonlathae)[1,0]};
Subroutine latlon: called CC=1 SCAR=0.0
Pod documented
51  
52 2 ▶sub latlong {(shift->lonlathae)[1,0]};
Subroutine latlong: called CC=1 SCAR=0.0
Pod documented
53  
54 =head2 lat
55  
56 Returns the latitude.
57  
58 =cut
59  
60 2 ▶sub lat {(shift->lonlathae)[1]};
Subroutine lat: called CC=1 SCAR=0.0
Pod documented
61  
62 =head2 lon
63  
64 Returns the longitude
65  
66 =cut
67  
68 2 ▶sub lon {(shift->lonlathae)[0]};
Subroutine lon: called CC=1 SCAR=0.0
Pod documented
69  
70 =head2 hae
71  
72 Returns the configured height of above the ellipsoid
73  
74 =cut
75  
76 2 ▶sub hae {(shift->lonlathae)[2]};
Subroutine hae: called CC=1 SCAR=0.0
Pod documented
77  
78 =head2 lonlathae
79  
80 Returns a list of longitude, latitude and height above the ellipsoid
81  
82 =cut
83  
84 sub lonlathae {
85 10 ▶ my $self=shift;
Subroutine lonlathae: called CC=11 SCAR=28.0
Pod documented
86 10 ▶ unless ($self->{"lonlathae"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"lonlathae"}) 2 8 10
87 2 my $coordinates="";
88 2 my $envname=$self->envname;
89 2 my $file="";
90 2 ▶ if ($envname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($envname) 2 0 2
91 2 ▶ $coordinates=$ENV{$envname} || "";
Condition100%2/2
$ENV{$envname} || ''
l!l
11
MC/DC100%1/1
$ENV{$envname} || ''
$ENV{$envname}
Truth table100%2/2
A
A= $ENV{$envname}
Aresult
11
00
92 2 $coordinates=~s/^\s*//; #trim white space from beginning
93 2 $coordinates=~s/\s*$//; #trim white space from end
94 }
95 2 ▶ if ($coordinates) {
Branch: 2 branches
BranchTrueFalseTotal
if ($coordinates) { } 1 1 2
elsif (defined($file = $self->configfile) and -r $file and defined $self->ci and $self->ci->SectionExists('wgs84')) { } 1 0 1
96 #First Step Pull from environment which can be configured by user
97 1 my ($lon, $lat, $hae)=split(/\s+/, $coordinates);
98 1 $self->{"lonlathae"}=[$lon, $lat, $hae];
99 ▶ } elsif (defined($file=$self->configfile) and -r $file and defined($self->ci) and $self->ci->SectionExists("wgs84")) {
Condition33%3/9
defined($file = $self->configfile) and -r $file and defined $self->ci and $self->ci->SectionExists("wgs84")
!ll&&!rl&&r
001
defined($file = $self->configfile) and -r $file and defined $self->ci
!ll&&!rl&&r
001
defined($file = $self->configfile) and -r $file
!ll&&!rl&&r
001
MC/DC0%0/4
defined($file = $self->configfile) and -r $file and defined $self->ci and $self->ci->SectionExists("wgs84")
defined($file = $self->configfile) -r $file defined $self->ci $self->ci->SectionExists("wgs84")
Truth table20%1/5
A and B and C and D
A= defined($file = $self->configfile)B= -r $fileC= defined $self->ciD= $self->ci->SectionExists("wgs84")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
100 #We assign the config filename inside the elsif to not load runtime requires if we hit the ENV above.
101 #Second Step Pull from file system which can be configured by system
102 1 my $lat=$self->ci->val(wgs84 => "latitude");
103 1 my $lon=$self->ci->val(wgs84 => "longitude");
104 1 my $hae=$self->ci->val(wgs84 => "hae");
105 1 $self->{"lonlathae"}=[$lon, $lat, $hae];
106 } else {
107 #TODO: GeoIP
108 #TODO: gpsd
109 #TODO: some kind of memory block transfer
110 0 my $error=qq{Error: None of the following supported coordinate standards are configured.\n\n};
111 0 ▶ $error.=$envname ? qq{ - Environment variable "$envname" is not set\n} : qq{ - Environment variable is disabled\n};
Branch: 1 branch
BranchTrueFalseTotal
$envname ? : 0 0 0
112 0 ▶ $error.=$file ? qq{ - Config file "$file" is not readable.\n} : qq{ - Config file is disabled\n};
Branch: 1 branch
BranchTrueFalseTotal
$file ? : 0 0 0
113 0 die("$error ");
114 }
115 }
116 10 return @{$self->{"lonlathae"}};
117 }
118  
119 =head1 PROPERTIES
120  
121 =head2 envname
122  
123 Sets and returns the name of the environment variable.
124  
125 my $var=$gls->envname; #default COORDINATES_WGS84_LON_LAT_HAE
126 $gls->envname(""); #disable environment lookup
127 $gls->envname(undef); #reset to default
128  
129 =cut
130  
131 sub envname {
132 2 ▶ my $self=shift;
Subroutine envname: called CC=3 SCAR=11.4
Pod documented
133 2 ▶ $self->{"envname"}=shift if $_;
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 2 2
134 2 ▶ $self->{"envname"}="COORDINATES_WGS84_LON_LAT_HAE" unless defined $self->{"envname"};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"envname"} 2 0 2
135 2 return $self->{"envname"};
136 }
137  
138 =head2 configfile
139  
140 Sets and returns the location of the local.coordinates filename.
141  
142 my $var=$gls->configfile; #default /etc/local.coordinates or C:\Windows\local.coordinates
143 $gls->configfile(""); #disable file-based lookup
144 $gls->configfile(undef); #reset to default
145  
146 =cut
147  
148 sub configfile {
149 6 ▶ my $self=shift;
Subroutine configfile: called CC=6 SCAR=29.9
Pod documented
150 6 ▶ $self->{"configfile"}=shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 6 6
151 6 ▶ unless (defined $self->{"configfile"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"configfile"}) 0 6 6
152 0 my $file="local.coordinates";
153 0 my $path="/etc"; #default is unix-like systems
154 0 ▶ if ($^O eq "MSWin32") {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
155 0 eval("use Win32");
156 0 ▶ $path=eval("Win32::GetFolderPath(Win32::CSIDL_WINDOWS)") unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
157 } else {
158 0 eval("use Sys::Path");
159 0 ▶ $path=eval("Sys::Path->sysconfdir") unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
160 }
161 0 $self->{"configfile"}=file($path => $file); #isa Path::Class::File
162 }
163 6 return $self->{"configfile"};
164 }
165  
166 =head1 CONFIGURATION
167  
168 =head2 File
169  
170 I recommend building and installing an RPM from the included SPEC file which installs /etc/local.coorindates.
171  
172 rpmbuild -ta Geo-Local-Server-?.??.tar.gz
173  
174 Outerwise copy the example etc/local.coorindates file to either /etc/ or C:\Windows\ and then update the coordinates to match your server location.
175  
176 =head2 Environment
177  
178 I recommend building and installing an RPM with the included SPEC file which installs /etc/profile.d/local.coordinates.sh which correctly sets the COORDINATES_WGS84_LON_LAT_HAE environment variable.
179  
180 Otherwise you can export the COORDINATES_WGS84_LON_LAT_HAE variable or add it to your .bashrc file.
181  
182 export COORDINATES_WGS84_LON_LAT_HAE="-77.386706 38.780276 63"
183  
184 =head2 Format
185  
186 The /etc/local.coordinates file is an INI file. The [wgs84] section is required for this package to function.
187  
188 [main]
189 version=1
190  
191 [wgs84]
192 latitude=38.780276
193 longitude=-77.386706
194 hae=63
195  
196 =head1 OBJECT ACCESSORS
197  
198 =head2 ci
199  
200 Returns the L<Config::IniFiles> object so that you can read additional information from the INI file.
201  
202 my $config=$gls->ci; #isa Config::IniFiles
203  
204 Example
205  
206 my $version=$gls->ci->val("main", "version");
207  
208 =cut
209  
210 sub ci {
211 5 ▶ my $self=shift;
Subroutine ci: called CC=2 SCAR=6.9
Pod documented
212 5 my $file=$self->configfile; #support for objects that can stringify paths.
213 $self->{'ci'}=Config::IniFiles->new(-file=>"$file")
214 5 ▶ unless ref($self->{'ci'}) eq "Config::IniFiles";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"ci"} eq "Config::IniFiles" 1 4 5
215 5 return $self->{'ci'};
216 }
217  
218 =head1 BUGS
219  
220 Please log on RT and send an email to the author.
221  
222 =head1 SUPPORT
223  
224 DavisNetworks.com supports all Perl applications including this package.
225  
226 =head1 AUTHOR
227  
228 Michael R. Davis
229 CPAN ID: MRDVT
230 Satellite Tracking of People, LLC
231 mdavis@stopllc.com
232 http://www.stopllc.com/
233  
234 =head1 COPYRIGHT
235  
236 This program is free software licensed under the...
237  
238 The BSD License
239  
240 The full text of the license can be found in the LICENSE file included with this module.
241  
242 =head1 SEE ALSO
243  
244 L<DateTime::Event::Sunrise>, L<Power::Outlet>, L<Geo::Point>, L<URL::geo>
245  
246 =cut
247  
248 3 1;
↑ Summary