Newer
Older
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
<?php
namespace SmartData;
require_once( __DIR__ . '/Exception.php');
require_once( __DIR__ . '/Packer.php');
require_once( __DIR__ . '/Unit.php');
use SmartData\Utility\Pack;
use SmartData\Utility\Unpack;
class Series
{
public $version;
private $unit;
public $x;
public $y;
public $z;
public $r;
public $t0;
public $t1;
public $dev;
public $signature;
public function __construct($v, $u, $x,$y,$z, $r, $t0, $t1, $dev, $signature=0, $workflow=0)
{
// Conditions for a valid series.
//TODO: Check the maximum value comparison
//if ((gmp_cmp(gmp_init($t0), gmp_init($t1)) > 0) or ($r < 0)){
if (($t0 > $t1) or ($t0 < 0) or ($t1 < 0) or ($r < 0)){
throw new Exception\BadRequestException("Invalid series");
}
$tmp_v = $v;
if(is_numeric($tmp_v) && is_int($tmp_v+0)){
// nothing
} else {
$tmp_v = explode(".", $tmp_v);
$tmp_v = ($tmp_v[0]<<4) + $tmp_v[1];
}
$this->version = $tmp_v;
$this->unit = ($u instanceof Unit) ? $u : Unit::interpret($u);
$this->x = $x;
$this->y = $y;
$this->z = $z;
$this->r = $r;
$this->t0 = $t0;
$this->t1 = $t1;
$this->dev = $dev;
$this->signature = $signature;
$this->workflow = $workflow;
}
public function unit()
{
return $this->unit;
}
public function contains_point($x, $y, $z)
{
return ( sqrt(pow($this->x - $x, 2) + pow($this->y - $y, 2) + pow($this->z - $z, 2)) <= $this->r );
}
public function contains_time($time)
{
return ( $this->t0 <= $time && $time <= $this->t1 );
}
public function __get($property)
{
if (property_exists($this, $property)) {
switch ($property) {
case 'unit':
if($this->unit instanceof Unit)
return $this->unit->cod;
else
return $this->unit;
default:
return $this->$property;
}
}
}
//public function create()
//{
// return API::create($this->pack());
//}
//public function attach()
//{
// return API::attach($this->pack());
//}
//public function get()
//{
// return API::get($this->pack());
//}
public function pack(array $suppress = array())
{
$bin = '';
if(!in_array("version", $suppress))
$bin .= Pack::uInt8($this->version);
if(!in_array("unit", $suppress))
$bin .= Pack::uInt32($this->unit->cod);
if(!in_array("x", $suppress))
$bin .= Pack::int32($this->x);
if(!in_array("y", $suppress))
$bin .= Pack::int32($this->y);
if(!in_array("z", $suppress))
$bin .= Pack::int32($this->z);
if(!in_array("r", $suppress))
$bin .= Pack::int32($this->r);
if(!in_array("t0", $suppress))
$bin .= Pack::uInt64($this->t0);
if(!in_array("t1", $suppress))
$bin .= Pack::uInt64($this->t1);
if(!in_array("dev", $suppress))
$bin .= Pack::uint32($this->dev);
return $bin;
}
public static function unpack(& $bin, array $compl = array())
{
$ver = $compl['version'] ?? Unpack::uInt8($bin, true);
if(array_key_exists('unit', $compl))
$u = Unit::interpret($compl['unit']);
else
$u = Unit::unpack($bin);
$x = $compl['x'] ?? Unpack::int32($bin, true);
$y = $compl['y'] ?? Unpack::int32($bin, true);
$z = $compl['z'] ?? Unpack::int32($bin, true);
$r = $compl['r'] ?? Unpack::int32($bin, true);
$t0 = $compl['t0'] ?? Unpack::uInt64($bin, true);
if($t0<0)
$t0 = 0;
$t1 = $compl['t1'] ?? Unpack::uInt64($bin, true);
if($t1<0)
$t1 = PHP_INT_MAX;
$d = $compl['dev'] ?? Unpack::uint32($bin, true);
return new self($ver, $u, $x, $y, $z, $r, $t0, $t1, $d);
}
public function toArray()
{
$json = array();
$json['version'] = $this->version;
$json['unit'] = $this->unit->cod;
$json['x'] = $this->x;
$json['y'] = $this->y;
$json['z'] = $this->z;
$json['r'] = $this->r;
$json['t0'] = $this->t0;
$json['t1'] = $this->t1;
$json['dev'] = $this->dev;
return $json;
}
public function toJson()
{
$json = json_encode($this->toArray());
$json = preg_replace('/:"([0-9]*)"/',':$1',$json);
//$json = preg_replace('/\"t0\":\"/', "\"t0\":", $json);
//$json = preg_replace('/\"t1\":\"/', "\"t1\":", $json);
return $json;
}
public static function fromJson(\stdClass $json)
{
return new self(
$json->version,
$json->unit,
$json->x,
$json->y,
$json->z,
$json->r,
$json->t0,
$json->t1,
$json->dev??0,
$json->signature??0,
$json->workflow??0
);
}
public function __toString()
{
$string = "{";
$string .= "v={$this->version}, ";
$string .= "u={$this->unit}, ";
$string .= "x={$this->x}, ";
$string .= "y={$this->y}, ";
$string .= "z={$this->z}, ";
$string .= "r={$this->r}, ";
$string .= "t0={$this->t0}, ";
$string .= "t1={$this->t1}, ";
$string .= "d={$this->dev}";
if($this->version == SmartData::MOBILE_VERSION) // Remove.. Now we have the classs "Tracker" for mobile version
$string .= ",sig={$this->signature}";
$string .= '}';
return $string;
}
}