script/ekahau-example.pl untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 35 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 225 CC 5 SCAR 21.0
CC
5
Cov
0%
get_floor
20.0
min
6.0
BEGIN
2.0
CRAP 8.2
1 #!/usr/local/bin/perl
2  
3 # Test line.
4  
5 use warnings;
6 use strict;
7  
8 use Ekahau;
9 use Getopt::Std;
10  
11 use constant DEFAULT_DEVICE_CHECK => 10;
12 use constant DEFAULT_NUM_GUESSES => 5;
13 use constant DEFAULT_GUESS_THRESHOLD => .10;
14  
15 our %opt;
16 our %ekopts;
17 getopts("h:p:L:g:G:c:",\%opt)
18 or die "Usage: $0 [-h hostname] [-p port] [-L licensefile]\n";
19 if ($opt{h}) { $ekopts{PeerAddr} = $opt{h} };
20 if ($opt{p}) { $ekopts{PeerPort} = $opt{p} };
21 if ($opt{L} || $ENV{EKAHAU_SDK_LICENSE}) { $ekopts{LicenseFile} = $opt{L} || $ENV{EKAHAU_SDK_LICENSE} };
22  
23 our $num_guesses = $opt{g} || DEFAULT_NUM_GUESSES;
24 our $guess_threshold = $opt{G} || DEFAULT_GUESS_THRESHOLD;
25 our $device_check_timeout = $opt{c} || DEFAULT_DEVICE_CHECK;
26 $ekopts{Timeout} = $device_check_timeout;
27  
28 our $loc_params = {
29 };
30  
31 # 'EPE.WLAN_SCAN_INTERVAL' => 100,
32 # 'EPE.WLAN_SCAN_MODE' => 2,
33 # 'EPE.SNAP_TO_RAIL' => 'true',
34 # 'EPE.EXPECTED_ERROR' => 'true',
35 # 'EPE.POSITIONING_MODE' => '2',
36 # 'EPE.LOCATION_UPDATE_INTERVAL' => 10_000,
37 # 'EPE.NUMBER_OF_AREAS' => 5 };
38
39  
40 my $ek = Ekahau->new(%ekopts)
41 or die "Couldn't create Ekahau object: $!\n";
42  
43 our %lastloc;
44 our %floor;
45 our %macaddr;
46 our $last_devlist_scan = 0;
47  
48 $SIG{ALRM} = sub { warn "ALRM at ",time,"!\n"; };
49 $SIG{TERM} = $SIG{QUIT} = $SIG{INT} = $SIG{PIPE} = sub { die "ektest.pl PID $$ killed by signal $_[0]\n"; };
50  
51 $| = 1;
52  
53 while (1)
54 {
55 if ((time - $last_devlist_scan) >= $device_check_timeout)
56 {
57 print "DEBUG Getting device list\n";
58 printf "IMOK %s AT %ld TIMEOUT %ld\n",
59 'ekahau', time, $device_check_timeout*3;
60 my $dl = $ek->get_device_list()
61 or die "Couldn't get device list: $ek->{err}\n";
62
63 foreach my $dev (grep { !$macaddr{$_} } @$dl)
64 {
65 my $prop = $ek->get_device_properties($dev)
66 or die "Couldn't get properties for '$dev': $ek->{err}\n";
67 $macaddr{$dev}=$prop->get_prop('NETWORK.MAC');
68 $ek->start_location_track($loc_params, $dev);
69 $ek->start_area_track($loc_params, $dev);
70 }
71 $last_devlist_scan = time;
72 # warn "DEBUG set alarm($device_check_timeout) at ",time,"\n";
73 # alarm($device_check_timeout);
74 }
75  
76 warn "DEBUG Waiting for next location update\n"
77 if ($ENV{VERBOSE});
78  
79 my $loc = $ek->next_track(Timeout => $device_check_timeout);
80 next unless $loc;
81 my $dev = $loc->{args}[0];
82 warn "DEBUG Got response $loc->{cmd}\n"
83 if ($ENV{VERBOSE});
84  
85 if ($loc->type eq 'AreaEstimate')
86 {
87 next unless $macaddr{$dev};
88  
89 my $where = $loc->get_prop('name');
90 if (!$where or $where eq 'null') { $where = 'Unknown' }
91 my($coord,$relcoord)=("","");
92  
93 $where = join(".",get_floor($ek,$loc->get_prop('contextId')),
94 $where);
95  
96 if (defined($lastloc{$dev}{x}) and defined($lastloc{$dev}{y}))
97 {
98 $coord = " COORDINATES $lastloc{$dev}{x},$lastloc{$dev}{y}";
99 my $poly = $loc->get_prop('polygon');
100 if ($poly and $poly ne 'null')
101 {
102 my($all_x,$all_y) = split(/\&/,$poly);
103 my $upper_y = min(split(/;/,$all_y));
104 my $leftmost_x = min(split(/;/,$all_y));
105 $relcoord = sprintf(" RELCOORD %.2f,%.2f",
106 ($lastloc{$dev}{x}-$leftmost_x),
107 ($lastloc{$dev}{y}-$upper_y));
108 }
109 }
110 my $ormaybe = "";
111 my @alternate = $loc->get_all;
112 shift @alternate;
113 foreach my $room (@alternate)
114 {
115 if ($room->get_prop('probability') >= $guess_threshold)
116 {
117 $ormaybe .= " ORMAYBE_FROM " .
118 join(".",get_floor($ek,$room->get_prop('contextId')),
119 $room->get_prop('name'));
120 $ormaybe .= " ORMAYBE_CONFIDENCE ".$room->get_prop('probability');
121 }
122 }
123 print "ISEE Ekahau.$macaddr{$dev} FROM $where$coord$relcoord AT ",time," CONFIDENCE ",$loc->{params}{probability},"$ormaybe\n";
124 }
125 elsif ($loc->type eq 'LocationEstimate')
126 {
127 $lastloc{$dev}{x} = $loc->get_prop('latestX');
128 $lastloc{$dev}{y} = $loc->get_prop('latestY');
129 }
130 elsif ($loc->error)
131 {
132 my $errstr = "errorMessage=".$loc->error_msg.", errorCode=".$loc->error_code.", errorLevel=".$loc->error_level;
133 warn "Handling error: $errstr\n"
134 if ($ENV{VERBOSE});
135 if ($loc->error_fatal)
136 {
137 print "DEBUG Ekahau client encountered a fatal error, and is restarting. See you in a jiffy! $errstr\n";
138 die "Fatal error encountered: $errstr\n";
139 }
140  
141 if ($macaddr{$dev})
142 {
143 print "DEBUG Device $macaddr{$dev} ($dev) is gone ($errstr).\n";
144 delete $macaddr{$dev};
145 }
146 }
147 else
148 {
149 print "DEBUG Unrecognized response $loc->{cmd}\n";
150 }
151 }
152  
153 {
154 my %floor;
155 sub get_floor
156 {
157 my($ek,$ctx_id)=@_;
158 die "Usage: get_floor(ekahau_obj, context_id)"
159 unless ($ek and $ctx_id);
160 if (!$floor{$ctx_id})
161 {
162 if (my $ctx = $ek->get_location_context($ctx_id))
163 {
164 $floor{$ctx_id}=$ctx->get_prop('address');
165 $floor{$ctx_id} =~ s|/|.|g;
166 }
167 }
168 $floor{$ctx_id};
169 }
170 }
171  
172 sub min
173 {
174 my $min = shift;
175 while(@_)
176 {
177 my $v = shift;
178 if ($v < $min)
179 {
180 $min = $v;
181 }
182 }
183 $min;
184 }