-
Notifications
You must be signed in to change notification settings - Fork 6
/
tidepoolsync.php
85 lines (84 loc) · 2.71 KB
/
tidepoolsync.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
include_once "tidepool.inc.php";
error_reporting(E_ALL);
function sortByDate($a, $b) {
return $a['date'] - $b['date'];
}
function RandomString($Length)
{
$characters = str_split('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',1);
$randstring = array();
for ($i = 0; $i < $Length; $i++) {
$randstring[] = $characters[rand(0, count($characters)-1)];
}
return implode($randstring);
}
rename("api/v1/dexcom.data" , "api/v1/data.old");
$file = file_get_contents("api/v1/data.old");
$file = str_replace('[','',$file);
$file = explode("]",$file);
$json = array();
$data = array();
foreach ($file as $point) {
$json = explode("},",$point);
foreach ($json as $value) {
$value = $value."}";
if(substr( $value, 0, 25 ) === '{"device":"xDrip-DexcomG5'){
$data[] = json_decode($value, true);
}
}
}
unset($file,$json);
$array = array();
foreach ($data as $entry) {
if($entry["type"] == "sgv"){
switch($entry["noise"]){
case 0:
$entry["noise"] = "NONE";
break;
case 1:
$entry["noise"] = "CLEAN";
break;
case 2:
$entry["noise"] = "LIGHT";
break;
case 3:
$entry["noise"] = "MEDIUM";
break;
case 4:
$entry["noise"] = "HEAVY";
break;
case 6:
$entry["noise"] = "MAX";
break;
default:
$entry["noise"] = "NOT_COMPUTED";
}
$array[$entry["dateString"].$entry["type"].$entry["sgv"]] = $entry;
}
if($entry["type"] == "mbg")
$array[$entry["dateString"].$entry["type"].$entry["mbg"]] = $entry;
}
unset ($data);
echo "Uploading to DB:<br>";
usort($array, 'sortByDate');
ConnectDB();
foreach ($array as $entry) {
if($entry["type"] == "sgv")
echo InsertValue($entry["dateString"], $entry["type"], $entry["sgv"] , '"payload":{"internalTime":"'.GetDeviceTime($entry["dateString"]).'","delta":'.$entry["delta"].',"noiseMode":"'.$entry["noise"].'","trend":"'. $entry["direction"].'","value":'.ConvertToMmol($entry["sgv"]).'}');
if($entry["type"] == "mbg")
echo InsertValue($entry["dateString"], $entry["type"], $entry["mbg"] , '"payload":{"subType":"manual","time":"'.GetUTCTime($entry["dateString"]).'","timezoneOffset":'.GetUtcOffset().',"type":"smbg","units":"mmol/L","value":'.ConvertToMmol($entry["mbg"]).'}');
}
DisconnectDB();
rename("api/v1/data.old", "api/v1/olddata/".RandomString(12).".".RandomString(4));
echo "<br>Uploading to Tidepool:<br>";
Login();
ConnectDB();
$results = RunQuery("SELECT * FROM `Data` WHERE `Uploaded` IS NULL ORDER BY `Data`.`DateString` ASC");
while ($row = mysqli_fetch_array($results)){
echo UploadBG($row["DateString"],$row["Type"],$row["Value"],$row["Payload"]);
ConfirmUpload($row["Payload"]);
}
DisconnectDB();
Logout();
?>