-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.php
134 lines (122 loc) · 4.5 KB
/
model.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
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
<?php
class PillsModel {
private $mysqli;
private $broker;
private $user;
private $redis;
private $timestoreAdminkey;
public function __construct($mysqli, $user, $host, $brokerUsername, $brokerPass, $redis, $timestoreAdminkey) {
$this->mysqli = $mysqli;
$this->broker = new SecureMqtt($host, $brokerUsername, $brokerPass);
$this->user = $user;
$this->redis = $redis;
$this->timestoreAdminkey = $timestoreAdminkey;
}
public function getAllData(){
$userId = $this->user;
if($result = $this->mysqli->query("SELECT * FROM Cells WHERE user_id='$userId'")){
$data = Array();
while($row = $result->fetch_array()){
$data[$row["cell_index"]]["time"] = $row["deadline"];
$data[$row["cell_index"]]["importance"] = $row["importance"];
$data[$row["cell_index"]]["snoozes"] = $row["snoozes"];
$data[$row["cell_index"]]["pills"] = Array();
}
if($result = $this->mysqli->query("SELECT * FROM Names_in_cells WHERE user_id='$userId'")) {
while($row = $result->fetch_array())
array_push($data[$row["cell_index"]]["pills"], $row["name"]);
}
//Getting schedule state
$allNames = Array();
$days = Array("mon", "tue", "wed", "thu", "fri", "sat", "sun");
$daytimes = Array("morn", "noon", "aft", "eve");
foreach($days as $day)
foreach($daytimes as $time)
array_push($allNames, "$day $time");
$userid = $this->user;
for($i = 0; $i < 28; $i++) {
$name = $allNames[$i];
$result = $this->mysqli->query("SELECT * FROM feeds WHERE userid = '$userid' AND name = '$name'");
$row = $result->fetch_array();
$id = $row['id'];
$lastvalue = $this->redis->hmget("feed:lastvalue:$id",array('time','value'));
$data[$i]["state"] = $lastvalue['value'];
}
return $data;
} else
return null;
}
public function getPillNames(){
$names = Array();
if($result = $this->mysqli->query("SELECT name FROM Pill_names")){
while($row = $result->fetch_array())
array_push($names, $row['name']);
}
return $names;
}
public function sendScheduleToBroker($json){
//there is a bug either in the broker or either in this PHP
//MQTT client library that you CAN NOT PUBLISH without a loop with some delays.
$sent = false;
for($i = 0; $i < 4; $i++) {
$this->broker->loop();
if(!$sent) {
$this->broker->publish("house/pill/schedule", $json, 1, 1);
$sent = true;
}
sleep(1);
}
}
public function updateCell($cellId, $snoozes = null, $deadline = null, $importance = null, $pillNames = null){
$userid = $this->user;
$result = $this->mysqli->query("SELECT * FROM Cells WHERE user_id=$userid AND cell_index=$cellId");
if($this->mysqli->error) var_dump($this->mysqli->error);
if($result->num_rows == 0)
return "Cell does not exist";
else {
$query = "UPDATE Cells SET ";
if($snoozes !== null) {
$query .= "snoozes=$snoozes";
if($deadline !== null || $importance !== null)
$query .= ",";
}
if($deadline !== null) {
$query .= " deadline=$deadline";
if($importance !== null)
$query .= ",";
}
if($importance !== null) $query .= " importance=$importance";
$query .= " WHERE user_id=$userid AND cell_index=$cellId";
$this->mysqli->query($query);
if($this->mysqli->error) var_dump($this->mysqli->error);
if($pillNames) {
$this->mysqli->query("DELETE FROM Names_in_cells WHERE user_id='$userid' AND cell_index='$cellId'");
if($this->mysqli->error) var_dump($this->mysqli->error);
foreach($pillNames as $name){
$rows = $this->mysqli->query("SELECT * FROM Pill_names WHERE name='$name'");
if($this->mysqli->error) var_dump($this->mysqli->error);
$rows2 = $this->mysqli->query("SELECT * FROM Names_in_cells WHERE user_id='$userid' AND cell_index='$cellId' AND name='$name'");
if($this->mysqli->error) var_dump($this->mysqli->error);
if(!$rows || $rows->num_rows == 0) {
$this->mysqli->query("INSERT INTO Pill_names VALUES('$name')");
if($this->mysqli->error) var_dump($this->mysqli->error);
}
if(!$rows2 || $rows2->num_rows == 0) {
$this->mysqli->query("INSERT INTO Names_in_cells VALUES('$userid', '$cellId', '$name')");
if($this->mysqli->error) var_dump($this->mysqli->error);
}
}
}
return "Update done";
}
}
}
class SecureMqtt extends Mosquitto\Client{
public function __construct($host, $username, $pass){
parent::__construct();
parent::setCredentials($username, $pass);
parent::setTlsCertificates(".");
parent::setTlsOptions(Mosquitto\Client::SSL_VERIFY_NONE, "tlsv1", NULL);
parent::connect($host, 8883);
}
}