| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Ekahau::Events; | ||
| 7 | use Getopt::Std; | ||
| 8 | |||
| 9 | use constant DEFAULT_DEVICE_CHECK => 10; | ||
| 10 | use constant DEFAULT_NUM_GUESSES => 5; | ||
| 11 | use constant DEFAULT_GUESS_THRESHOLD => .10; | ||
| 12 | |||
| 13 | our %opt; | ||
| 14 | our %ekopts; | ||
| 15 | getopts("h:p:L:g:G:c:",\%opt) | ||
| 16 | or die "Usage: $0 [-h hostname] [-p port] [-L licensefile]\n"; | ||
| 17 | if ($opt{h}) { $ekopts{PeerAddr} = $opt{h} }; | ||
| 18 | if ($opt{p}) { $ekopts{PeerPort} = $opt{p} }; | ||
| 19 | if ($opt{L} || $ENV{EKAHAU_SDK_LICENSE}) { $ekopts{LicenseFile} = $opt{L} || $ENV{EKAHAU_SDK_LICENSE} }; | ||
| 20 | |||
| 21 | our $num_guesses = $opt{g} || DEFAULT_NUM_GUESSES; | ||
| 22 | our $guess_threshold = $opt{G} || DEFAULT_GUESS_THRESHOLD; | ||
| 23 | our $device_check_timeout = $opt{c} || DEFAULT_DEVICE_CHECK; | ||
| 24 | |||
| 25 | $ekopts{Timeout} = $device_check_timeout; | ||
| 26 | |||
| 27 | my $ek = Ekahau::Events->new(%ekopts) | ||
| 28 | or die "Couldn't create Ekahau object: $!\n"; | ||
| 29 | |||
| 30 | our %lastloc; | ||
| 31 | our %floor; | ||
| 32 | our %macaddr; | ||
| 33 | our $last_devlist_scan = 0; | ||
| 34 | |||
| 35 | $SIG{ALRM} = sub { warn "ALRM at ",time,"!\n"; }; | ||
| 36 | |||
| 37 | $| = 1; | ||
| 38 | |||
| 39 | $ek->register_handler('','DEVICE_LIST',\&update_devices); | ||
| 40 | $ek->register_handler('','DEVICE_PROPERTIES',\&update_device_properties); | ||
| 41 | $ek->register_handler('','AREA_ESTIMATE',\&update_areas); | ||
| 42 | $ek->register_handler('','LOCATION_ESTIMATE',\&update_locations); | ||
| 43 | $ek->register_handler('','CONTEXT',\&update_contexts); | ||
| 44 | $ek->register_handler('','',\&other_events); | ||
| 45 | |||
| 46 | while(1) | ||
| 47 | { | ||
| 48 | if ((time - $last_devlist_scan) >= $device_check_timeout) | ||
| 49 | { | ||
| 50 | print "DEBUG Getting device list\n"; | ||
| 51 | printf "IMOK %s AT %ld TIMEOUT %ld\n", | ||
| 52 | 'ekahau', time, $device_check_timeout*3; | ||
| 53 | # $need_devlist = 0; | ||
| 54 | my $dl = $ek->request_device_list() | ||
| 55 | or die "Couldn't get device list: $ek->{err}\n"; | ||
| 56 | $last_devlist_scan = time; | ||
| 57 | } | ||
| 58 | if ($ek->can_read($device_check_timeout)) | ||
| 59 | { | ||
| 60 | $ek->dispatch; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | sub update_devices | ||
| 65 | { | ||
| 66 | my($resp)=@_; | ||
| 67 | |||
| 68 | foreach my $dev (grep { !$macaddr{$_} } keys %{$resp->{params}}) | ||
| 69 | { | ||
| 70 | my $prop = $ek->request_device_properties($dev) | ||
| 71 | or die "Couldn't get properties for '$dev'\n"; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | sub update_device_properties | ||
| 76 | { | ||
| 77 | my($prop)=@_; | ||
| 78 | |||
| 79 | my $dev = $prop->{args}[0]; | ||
| 80 | $macaddr{$dev}=$prop->{params}{'NETWORK.MAC'}; | ||
| 81 | $ek->start_location_track($dev); | ||
| 82 | $ek->start_area_track({ 'EPE.NUMBER_OF_AREAS' => $num_guesses }, $dev); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub update_areas | ||
| 86 | { | ||
| 87 | my($loc)=@_; | ||
| 88 | my $dev = $loc->{args}[0]; | ||
| 89 | my $floor = $floor{$loc->{params}{contextId}}; | ||
| 90 | |||
| 91 | if (!$macaddr{$dev}) | ||
| 92 | { | ||
| 93 | # Skip unknown devices | ||
| 94 | return; | ||
| 95 | } | ||
| 96 | if (!$floor) | ||
| 97 | { | ||
| 98 | # Request the context, and we'll display the location after we get it. | ||
| 99 | $ek->request_location_context($loc->{params}{contextId}); | ||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | my $where = $loc->{params}{name}; | ||
| 104 | if ($where eq 'null') { $where = 'Unknown' } | ||
| 105 | my($coord,$relcoord)=("",""); | ||
| 106 | |||
| 107 | |||
| 108 | $where = $floor.".".$where; | ||
| 109 | |||
| 110 | if (defined($lastloc{$dev}{x}) and defined($lastloc{$dev}{y})) | ||
| 111 | { | ||
| 112 | $coord = " COORDINATES $lastloc{$dev}{x},$lastloc{$dev}{y}"; | ||
| 113 | if ($loc->{params}{polygon} and $loc->{params}{polygon} ne 'null') | ||
| 114 | { | ||
| 115 | my($all_x,$all_y) = split(/\&/,$loc->{params}{polygon}); | ||
| 116 | my $upper_y = min(split(/;/,$all_y)); | ||
| 117 | my $leftmost_x = min(split(/;/,$all_y)); | ||
| 118 | $relcoord = sprintf(" RELCOORD %.2f,%.2f", | ||
| 119 | ($lastloc{$dev}{x}-$leftmost_x), | ||
| 120 | ($lastloc{$dev}{y}-$upper_y)); | ||
| 121 | } | ||
| 122 | } | ||
| 123 | my $ormaybe = ""; | ||
| 124 | foreach my $i (1..$#{$loc->{params}{AREA}}) | ||
| 125 | { | ||
| 126 | if (my $room = $loc->{params}{AREA}[$i]) | ||
| 127 | { | ||
| 128 | if ($room->{probability} >= $guess_threshold) | ||
| 129 | { | ||
| 130 | my $or_floor = $floor{$room->{contextId}}; | ||
| 131 | if (!$or_floor) | ||
| 132 | { | ||
| 133 | # Request the context, and we'll display the location after we get it. | ||
| 134 | $ek->request_location_context($loc->{params}{contextId}); | ||
| 135 | next; | ||
| 136 | } | ||
| 137 | |||
| 138 | $ormaybe .= " ORMAYBE_FROM " . $or_floor.".".$room->{name} . | ||
| 139 | " ORMAYBE_CONFIDENCE ".$room->{probability}; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | print "ISEE Ekahau.$macaddr{$dev} FROM $where$coord$relcoord AT ",time," CONFIDENCE ",$loc->{params}{probability},"$ormaybe\n"; | ||
| 144 | } | ||
| 145 | |||
| 146 | sub update_locations | ||
| 147 | { | ||
| 148 | my($loc)=@_; | ||
| 149 | my $dev = $loc->{args}[0]; | ||
| 150 | |||
| 151 | $lastloc{$dev}{x} = $loc->{params}{latestX}; | ||
| 152 | $lastloc{$dev}{y} = $loc->{params}{latestY}; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub update_contexts | ||
| 156 | { | ||
| 157 | my($ctx)=@_; | ||
| 158 | my $ctx_id = $ctx->{args}[0]; | ||
| 159 | |||
| 160 | $floor{$ctx_id}=$ctx->{params}{address}; | ||
| 161 | $floor{$ctx_id} =~ s|/|.|g; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub other_events | ||
| 165 | { | ||
| 166 | my($loc) = @_; | ||
| 167 | my $dev = $loc->{args}[0]; | ||
| 168 | |||
| 169 | if ($loc->{cmd} =~ /_(?:PROBLEM|FAILED)$/) | ||
| 170 | { | ||
| 171 | warn "Handling error: error='$loc->{cmd}', errorCode='$loc->{params}{errorCode}', errorLevel='$loc->{params}{errorLevel}'\n" | ||
| 172 | if ($ENV{VERBOSE}); | ||
| 173 | if (!defined($loc->{params}{errorLevel}) or $loc->{params}{errorLevel} >= 3) | ||
| 174 | { | ||
| 175 | print "DEBUG Ekahau client encountered a fatal error, and is restarting. See you in a jiffy! errorCode=$loc->{params}{errorCode}, errorLevel=$loc->{params}{errorLevel}\n"; | ||
| 176 | die "Fatal error encountered: $loc->{params}{errorCode}, errorLevel=$loc->{params}{errorLevel}.\n"; | ||
| 177 | } | ||
| 178 | |||
| 179 | if ($macaddr{$dev}) | ||
| 180 | { | ||
| 181 | print "DEBUG Device $macaddr{$dev} ($dev) is gone ($loc->{cmd}).\n"; | ||
| 182 | delete $macaddr{$dev}; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | sub min | ||
| 188 | { | ||
| 189 | my $min = shift; | ||
| 190 | while(@_) | ||
| 191 | { | ||
| 192 | my $v = shift; | ||
| 193 | if ($v < $min) | ||
| 194 | { | ||
| 195 | $min = $v; | ||
| 196 | } | ||
| 197 | } | ||
| 198 | $min; | ||
| 199 | } |