-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.php
71 lines (60 loc) · 1.72 KB
/
read.php
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
<?php
/**
* Makhtar Diouf - Arduino project
* Process received data stored in json files: humidity, sound, temperature...etc
* $Id$
*/
class ArduiData {
public $data;
function __construct() {
$this->data = [];
}
public function getData($target) {
$this->processData($target);
}
protected function processData($file) {
$date = date("Y-m-d");
$dir = "output/" . $date . "/";
$jsDateTime = function ($arg) {
echo '[new Date(' . $arg[0] * 1000 . "), $arg[1] ],";
};
$target = $dir . $file . ".json";
$fp = fopen($target, "r");
if (!$fp) {
$_SESSION["error_msg"] = "Could not open $target";
return;
}
$output = "";
while ($line = fgets($fp)) {
$temp = json_decode($line);
if (!feof($fp) || (!empty($temp[0]))) {
$output .= $jsDateTime($temp);
}
}
echo $output;
fclose($fp);
}
public static function readLastData($file) {
try {
$date = date("Y-m-d");
$dir = "output/" . $date . "/";
$target = $dir . $file . ".json";
$fp = fopen($target, "r");
if (!$fp) {
$_SESSION["error_msg"] = "Could not open $target";
return;
}
$pos = -1;
$t = " ";
while ($t != "\n") {
fseek($fp, $pos, SEEK_END);
$t = fgetc($fp);
$pos += -1;
}
echo fgets($fp);
fclose($fp);
} catch (Exception $ex) {
$_SESSION["error_msg"] = "Could not open $target: " . $ex->getMessage();
}
}
}