line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WWW::PlaceEngine; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
23978
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
42
|
|
4
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
53
|
|
5
|
1
|
|
|
1
|
|
1117
|
use Readonly; |
|
1
|
|
|
|
|
2770
|
|
|
1
|
|
|
|
|
53
|
|
6
|
1
|
|
|
1
|
|
1027
|
use JSON; |
|
1
|
|
|
|
|
17948
|
|
|
1
|
|
|
|
|
7
|
|
7
|
1
|
|
|
1
|
|
1355
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
84334
|
|
|
1
|
|
|
|
|
413
|
|
8
|
|
|
|
|
|
|
$VERSION = '0.03'; |
9
|
|
|
|
|
|
|
$JSON::QuotApos = 1; |
10
|
|
|
|
|
|
|
$JSON::UTF8 = 1; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Readonly my $API_HOST => 'http://www.placeengine.com/api'; |
13
|
|
|
|
|
|
|
Readonly my $RTAG_DAEMON => 'http://localhost:5448'; |
14
|
|
|
|
|
|
|
Readonly my $APKEY_DEFAULT => 'jVX7qKlFTUUcbNOBAnVX7XFhSUm6FyFzapE5oyv4Y.D6asJrrI5w3dUKsBgrpXe8eDIYrAff3WWKtXxnx.SX7OztpPf7SrrcJK-c0fyZKYSNVn-Gp3Kqb-4-VajcTxlFKt12r44C6oK5OPh7UsWLvt-xB3J.TuPHj0ptHJtuGAn1xc.ZA-4R3LBOQyYUsyphZACHrMvKQ1dAlPZPdyiwxpQfFczAZ4AljisHF5eFvjfYk6y5YUNsaT-TOqNCG22UyLTKL4t0bk.43YJU0M2cbdf07TWmDkQOy5JP9NmX1Ea8vbCZTM.DgEqPrsmrOaI9mmvEVppeCxASBz48ON.shw__,cGVybA__,44OR44O844Or44OX44Ot44Kw44Op44Og'; |
15
|
|
|
|
|
|
|
Readonly my $TESTED_RTAGD => 'w070606'; |
16
|
|
|
|
|
|
|
Readonly my $AGENT_DEFAULT => "WWW::PlaceEngine/$VERSION"; |
17
|
|
|
|
|
|
|
Readonly my $ERR_NOT_OCCUR => 0; |
18
|
|
|
|
|
|
|
Readonly my $ERR_WIFI_OFF => 1; |
19
|
|
|
|
|
|
|
Readonly my $ERR_NO_AP => 2; |
20
|
|
|
|
|
|
|
Readonly my $ERR_WIFI_DENY => 4; |
21
|
|
|
|
|
|
|
Readonly my $ERR_WIFI_TO => 5; |
22
|
|
|
|
|
|
|
Readonly my $ERR_NO_APPKEY => 6; |
23
|
|
|
|
|
|
|
Readonly my $ERR_NO_RTAGD => 7; |
24
|
|
|
|
|
|
|
Readonly my $ERR_NO_HOST => 8; |
25
|
|
|
|
|
|
|
Readonly my $ERR_RTAGD_OLD => 9; |
26
|
|
|
|
|
|
|
Readonly my $ERR_NO_LOCAL => 10; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Readonly my $ERROR_TABLE => { |
29
|
|
|
|
|
|
|
$ERR_NOT_OCCUR => '', |
30
|
|
|
|
|
|
|
$ERR_WIFI_OFF => 'WiFi device is maybe turned off.', |
31
|
|
|
|
|
|
|
$ERR_NO_AP => 'No APs are found or getting WiFi information is denyed.', |
32
|
|
|
|
|
|
|
$ERR_WIFI_DENY => 'Getting WiFi information is denyed.', |
33
|
|
|
|
|
|
|
$ERR_WIFI_TO => 'Getting WiFi information is timeout.', |
34
|
|
|
|
|
|
|
$ERR_NO_APPKEY => 'Application key is wrong or not found.', |
35
|
|
|
|
|
|
|
$ERR_NO_RTAGD => 'PlaceEngine client not found or cannot accessible.', |
36
|
|
|
|
|
|
|
$ERR_NO_HOST => 'PlaceEngine API host cannot accessible.', |
37
|
|
|
|
|
|
|
$ERR_RTAGD_OLD => "PlaceEngine client's version is old. At least $TESTED_RTAGD version is need", |
38
|
|
|
|
|
|
|
$ERR_NO_LOCAL => 'No APs are found in local DB.', |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
############################################################################## |
42
|
|
|
|
|
|
|
# CONSTRCUTOR |
43
|
|
|
|
|
|
|
############################################################################## |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub new { |
46
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
47
|
0
|
|
|
|
|
|
my %opt = @_; |
48
|
0
|
|
|
|
|
|
bless { |
49
|
|
|
|
|
|
|
host => $API_HOST , |
50
|
|
|
|
|
|
|
rtagd => $RTAG_DAEMON , |
51
|
|
|
|
|
|
|
appkey => $APKEY_DEFAULT , |
52
|
|
|
|
|
|
|
errcode => $ERR_NOT_OCCUR , |
53
|
|
|
|
|
|
|
err => '' , |
54
|
|
|
|
|
|
|
# overwrite |
55
|
|
|
|
|
|
|
%opt, |
56
|
|
|
|
|
|
|
}, $class; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
############################################################################## |
60
|
|
|
|
|
|
|
# ACCESSOR |
61
|
|
|
|
|
|
|
############################################################################## |
62
|
|
|
|
|
|
|
BEGIN{ |
63
|
1
|
|
|
1
|
|
4
|
for my $name (qw/ua host rtagd appkey err errcode rtag t numap version/) |
64
|
|
|
|
|
|
|
{ |
65
|
10
|
0
|
|
0
|
1
|
1861
|
eval qq{ |
|
0
|
0
|
|
0
|
1
|
|
|
|
0
|
0
|
|
0
|
1
|
|
|
|
0
|
0
|
|
0
|
1
|
|
|
|
0
|
0
|
|
0
|
1
|
|
|
|
0
|
0
|
|
0
|
1
|
|
|
|
0
|
0
|
|
0
|
1
|
|
|
|
0
|
0
|
|
0
|
1
|
|
|
|
0
|
0
|
|
0
|
1
|
|
|
|
0
|
0
|
|
0
|
0
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
sub $name { \$_[0]->{$name} = \$_[1] if(defined \$_[1]); \$_[0]->{$name} } |
67
|
|
|
|
|
|
|
}; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
############################################################################## |
72
|
|
|
|
|
|
|
# METHODS |
73
|
|
|
|
|
|
|
############################################################################## |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub get_location { |
76
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
77
|
0
|
0
|
|
|
|
|
$self->check_rtagd or return; |
78
|
0
|
0
|
|
|
|
|
$self->get_rtag or return; |
79
|
0
|
|
|
|
|
|
$self->decode_rtag(); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub get_local_location { |
83
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
84
|
0
|
|
0
|
|
|
|
my $time = shift || $self->t || time; |
85
|
0
|
|
|
|
|
|
my $param = '/locdb?t=' . $time; |
86
|
0
|
|
|
|
|
|
$param .= '&appk=' . $self->appkey; |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
0
|
|
|
|
my $ua = $self->ua || $self->ua(LWP::UserAgent->new(agent=>$AGENT_DEFAULT)); |
89
|
0
|
|
|
|
|
|
my $res = $ua->get($self->rtagd . $param); |
90
|
0
|
0
|
|
|
|
|
return $self->set_err($ERR_NO_RTAGD) if !$res->is_success; |
91
|
|
|
|
|
|
|
|
92
|
0
|
|
|
|
|
|
my $cont = $res->content; |
93
|
0
|
|
|
|
|
|
my ($long,$lat,$result,$opt) = @{jsonToObj($cont)}; |
|
0
|
|
|
|
|
|
|
94
|
0
|
|
|
|
|
|
for my $name (qw/long lat result/) { |
95
|
0
|
|
|
|
|
|
$opt->{$name} = eval qq{\$$name}; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
0
|
0
|
|
|
|
|
if ($result < 0) { |
|
|
0
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
return $self->set_err($result * -1); |
100
|
|
|
|
|
|
|
} elsif ($result == 0) { |
101
|
0
|
0
|
0
|
|
|
|
return $self->set_err($ERR_NO_LOCAL) if (($opt->{lat} == 0.0) && ($opt->{long} == 0.0)); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
return $opt; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub check_rtagd { |
108
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
109
|
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
|
return $self->version if $self->version; |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
0
|
|
|
|
my $ua = $self->ua || $self->ua(LWP::UserAgent->new(agent=>$AGENT_DEFAULT)); |
113
|
0
|
|
|
|
|
|
my $origto = $ua->timeout; |
114
|
0
|
|
|
|
|
|
$ua->timeout(30); |
115
|
0
|
|
|
|
|
|
my $res = $ua->get($self->rtagd . '/ackjs?t=' . time); |
116
|
0
|
|
|
|
|
|
$ua->timeout($origto); |
117
|
0
|
0
|
|
|
|
|
return $self->set_err($ERR_NO_RTAGD) if !$res->is_success; |
118
|
|
|
|
|
|
|
|
119
|
0
|
|
|
|
|
|
my ($version) = $res->content =~ /^ackRTAG\("(.*)"\);$/; |
120
|
|
|
|
|
|
|
|
121
|
0
|
0
|
|
|
|
|
return $self->set_err($ERR_RTAGD_OLD) if ($version lt $TESTED_RTAGD); |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
$self->version($version); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
sub get_rtag { |
127
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
128
|
0
|
|
|
|
|
|
$self->set_err; |
129
|
0
|
|
|
|
|
|
$self->numap(0); |
130
|
0
|
|
|
|
|
|
$self->rtag(''); |
131
|
0
|
|
|
|
|
|
$self->t(time); |
132
|
0
|
0
|
|
|
|
|
return $self->set_err($ERR_NO_APPKEY) if !$self->appkey; |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
0
|
|
|
|
my $ua = $self->ua || $self->ua(LWP::UserAgent->new(agent=>$AGENT_DEFAULT)); |
135
|
0
|
|
|
|
|
|
my $origto = $ua->timeout; |
136
|
0
|
|
|
|
|
|
$ua->timeout(30); |
137
|
0
|
|
|
|
|
|
my $res = $ua->get($self->rtagd . '/rtagjs?t=' . $self->t . '&appk=' . $self->appkey); |
138
|
0
|
|
|
|
|
|
$ua->timeout($origto); |
139
|
0
|
0
|
|
|
|
|
return $self->set_err($ERR_NO_RTAGD) if !$res->is_success; |
140
|
|
|
|
|
|
|
|
141
|
0
|
|
|
|
|
|
my ($rtag,$numap,$time) = $res->content =~ /^recvRTAG\("(.*)",(.*),(.*)\);$/; |
142
|
|
|
|
|
|
|
|
143
|
0
|
0
|
|
|
|
|
if ($numap < 0) { |
|
|
0
|
|
|
|
|
|
144
|
0
|
|
|
|
|
|
return $self->set_err($numap * -1); |
145
|
|
|
|
|
|
|
} elsif ($numap == 0) { |
146
|
0
|
|
|
|
|
|
return $self->set_err($ERR_NO_AP); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
0
|
|
|
|
|
|
$self->numap($numap); |
150
|
0
|
|
|
|
|
|
$self->t($time); |
151
|
0
|
|
|
|
|
|
$self->rtag($rtag); |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
sub decode_rtag { |
155
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
156
|
0
|
|
0
|
|
|
|
my $rtag = shift || $self->rtag || ''; |
157
|
0
|
|
0
|
|
|
|
my $time = shift || $self->t || time; |
158
|
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
my $param = '/loc?rtag=' . $rtag . '&t=' . $time; |
160
|
0
|
|
|
|
|
|
$param .= '&appk=' . $self->appkey .'&fmt=json'; |
161
|
|
|
|
|
|
|
|
162
|
0
|
|
0
|
|
|
|
my $ua = $self->ua || $self->ua(LWP::UserAgent->new(agent=>$AGENT_DEFAULT)); |
163
|
0
|
|
|
|
|
|
my $res = $ua->get($self->host . $param); |
164
|
0
|
0
|
|
|
|
|
return $self->set_err($ERR_NO_HOST) if !$res->is_success; |
165
|
|
|
|
|
|
|
|
166
|
0
|
|
|
|
|
|
my $cont = $res->content; |
167
|
0
|
|
|
|
|
|
my ($long,$lat,$range,$opt) = @{jsonToObj($cont)}; |
|
0
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
for my $name (qw/long lat range/) { |
169
|
0
|
|
|
|
|
|
$opt->{$name} = eval qq{\$$name}; |
170
|
|
|
|
|
|
|
} |
171
|
|
|
|
|
|
|
|
172
|
0
|
0
|
|
|
|
|
if ($opt->{range} == -113) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
173
|
0
|
|
|
|
|
|
return $self->set_err($opt->{range},'Request format is illegal.'); |
174
|
|
|
|
|
|
|
} elsif ($opt->{range} <= -100) { |
175
|
0
|
|
|
|
|
|
return $self->set_err($opt->{range},'AP has no location information.'); |
176
|
|
|
|
|
|
|
} elsif ($opt->{range} <= 0) { |
177
|
0
|
|
|
|
|
|
return $self->set_err($opt->{range},'No APs are found.'); |
178
|
|
|
|
|
|
|
} |
179
|
0
|
|
|
|
|
|
return $opt; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
############################################################################## |
183
|
|
|
|
|
|
|
# ERROR |
184
|
|
|
|
|
|
|
############################################################################## |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub set_err { |
187
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
188
|
0
|
|
|
|
|
|
my ($errcode,$err) = @_; |
189
|
|
|
|
|
|
|
|
190
|
0
|
|
0
|
|
|
|
$self->errcode($errcode || 0); |
191
|
0
|
|
0
|
|
|
|
$self->err( $err || $ERROR_TABLE->{$errcode} || 'Unkown error occured.'); |
192
|
|
|
|
|
|
|
|
193
|
0
|
|
|
|
|
|
return; |
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
1; |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
__END__ |