struct.php
4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
require_once 'model/map.php';
require_once 'model/access_point.php';
require_once 'model/location.php';
require_once 'model/rssi_record.php';
class Loc {
public $_x;
public $_y;
}
class HistogramFingerprint
{
public $_location;
public $_mesurement; //array of HistoValue
}
class Bean
{
public $_value;
public $_occurence;
}
class HistogramValue
{
public $_macAddr;
public $_value; //RSSIHistogram = vector<Bean>
public function __construct($mac, $meas, $isMeas){
if($isMeas) {
$this->_macAddr=$mac;
$map = $meas;
ksort($map);
foreach($map as $first=>$second)
{
$val = new Bean();
$val->_value = $first;
$val->_occurence = $second;
$this->_value = $val;
}
}
else{
$this->_macAddr=$mac;
$this->_value = $meas;
}
}
}
class structFunctions {
public static function add_bean($histo,$b)
{
$i =0;
foreach($histo as $histoBean)
{
if($histoBean->_value == $b->_value)
{
$histoBean->_occurence += $b->_occurence;
return $histo;
}
$i++;
}
$histo[$i] = $b;
return $histo;
}
public static function merge_histo($dest,$other)
{
foreach($b as $other)
{
add_bean($dest,$b);
}
return $dest;
}
public static function add_value($sample,$v)
{
$i =0;
foreach($sample as $histoVal)
{
if($histoVal->_macAddr == $v->_macAddr)
{
merge_histo($histoVal->_value,$v->_value);
return $sample;
}
$i++;
}
$sample[$i] = $v;
return $sample;
}
public static function probability_histo($s1,$s2)
{
$sum_s1 = 0;
$sum_s2 = 0;
$p = 0;
foreach($s1 as $bean){
$sum_s1+=$bean->_occurence;
}
foreach ($s2 as $bean) {
$sum_s2+=$bean->_occurence;
}
foreach($s1 as $bean){
$v = $b->_value;
$i=0;
foreach($s2 as $b2)
{
if($b2->_value == $v)
{
break;
}
$i++;
}
if($i != count($s2)) {
$p+=min($b->_occurence/$sum_s1,$s2[$i]/$sum_s2);
}
}
return $p;
}
public static function probability($hf,$dev_measurement){
$p = 1;
foreach($hf as $v){
$i=0;
foreach($dev_measurement as $hv)
{
if($hv->_macAddr == $v->_macAddr)
{
break;
}
$i++;
}
if($i != count($dev_measurement)) {
$p*=probability_histo($v->_value,$dev_measurement[$i]->_value);
}
else {
$p=0;
}
if($p==0)
{
break;
}
}
return $p;
}
public static function locate($current,$db){
$p =0;
$l;
foreach($db as $f)
{
$tmpPos = probability($current,$f);
if($p <$tmpPos)
{
$p=$tmpPos;
$l = $f->_location;
}
}
return $l;
}
public static function load_histogram_db()
{
// ----------------------------------------------------------------------------------------- //
// ------------------------- Translation from DB to our Stuctures -------------------------- //
// ----------------------------------------------------------------------------------------- //
include 'bootstrap.php';
$loc = $entityManager->getRepository('rssi_record')->createQueryBuilder('rs')->select('rs.location')->groupBy('rs.location')->getQuery()->getResult();
foreach($loc as $l) {
$ap = $entityManager->getRepository('rssi_record')->createQueryBuilder('rs')
->select('rs.access_point')
->groupBy('rs.location', 'rs.access_point')
->getQuery()
->getResult();
$myHistoFinger = new HistogramFingerprint();
foreach($ap as $a) {
$myHistoVal = new HistogramValue();
$beans = $entityManager->getRepository('rssi_record')->findBy(array('location' => $l.getId(), 'access_point' => $a.getId()));
foreach($beans as $b) {
$myBean = new Bean();
$myBean->_value = $b.getValue();
$myBean->_occurence = $b.getOccurences();
array_push($myHistoVal->_value,$myBean);
}
$myHistoVal->_macAddr = $a.getMac_addr();
array_push($myHistoFinger->_mesurement,$myHistoVal);
}
$tmpPos = $entityManager->getRepository('location')->findOneBy(array('id' => $l.getLocation()));
$myLocation = new Location();
$myLocation->_x=$tmpPos.getX();
$myLocation->_y=$tmpPos.getY();
$myHistoFinger->_location = $myLocation;
array_push($db, $myHistoFinger);
}
return $loc;
}
}
?>