-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathDL-RAD.php
More file actions
105 lines (92 loc) · 3.08 KB
/
DL-RAD.php
File metadata and controls
105 lines (92 loc) · 3.08 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
<?php
/* https://www.decentlab.com/products/radar-distance-level-sensor-for-lorawan */
abstract class DecentlabDecoder
{
const PROTOCOL_VERSION = 2;
protected $sensors;
public function decode($payload = '')
{
$bytes = hex2bin($payload);
$parts = [];
$parts['version'] = ord($bytes[0]);
if ($parts['version'] != self::PROTOCOL_VERSION) {
$parts['error'] = sprintf("protocol version %u doesn't match v2", $parts['version']);
return $parts;
}
$parts['device_id'] = unpack('n', substr($bytes, 1))[1];
$flags = unpack('n', substr($bytes, 3))[1];
/* decode payload */
$k = 5;
foreach ($this->sensors as $sensor) {
if (($flags & 1) == 1) {
$x = [];
/* convert data to 16-bit integer array */
for ($j = 0; $j < $sensor['length']; $j++) {
array_push($x, unpack('n', substr($bytes, $k))[1]);
$k += 2;
}
/* decode sensor values */
foreach ($sensor['values'] as $value) {
if ($value['convert'] != NULL) {
$parts[$value['name'] . '_value'] = $value['convert']($x);
if ($value['unit'] != NULL) {
$parts[$value['name'] . '_unit'] = $value['unit'];
}
}
}
}
$flags >>= 1;
}
return $parts;
}
}
class DL_RAD_Decoder extends DecentlabDecoder {
public function __construct()
{
$this->sensors = [
[
'length' => 4,
'values' => [
[
'name' => 'distance',
'convert' => function ($x) { return $x[0]; },
'unit' => 'mm',
],
[
'name' => 'temperature',
'convert' => function ($x) { return ($x[1] - 32768) / 100; },
'unit' => '°C',
],
[
'name' => 'reliability',
'convert' => function ($x) { return ($x[2] - 32768) / 100; },
'unit' => 'dB',
],
[
'name' => 'status',
'convert' => function ($x) { return $x[3]; },
'unit' => NULL,
],
],
],
[
'length' => 1,
'values' => [
[
'name' => 'battery_voltage',
'convert' => function ($x) { return $x[0] / 1000; },
'unit' => 'V',
],
],
],
];
}
}
$decoder = new DL_RAD_Decoder();
$payloads = [
'02586b0003074c88b68a8c00000b93',
'02586b00020b93',
];
foreach($payloads as $payload) {
var_dump($decoder->decode($payload));
}