-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
108 lines (92 loc) · 3.26 KB
/
index.php
File metadata and controls
108 lines (92 loc) · 3.26 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
106
107
108
<?php
require($_SERVER['DOCUMENT_ROOT'].'/api/thirdparty/pusher-php-server/lib/Pusher.php');
chdir('..');
include_once $_SERVER['DOCUMENT_ROOT']."/api/conf/database.php";
require $_SERVER['DOCUMENT_ROOT'].'/api/thirdparty/slim/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->get('/trafficLight/:id/', 'getTrafficLight');
$app->post('/trafficLight/:estat/', 'addTrafficLight');
$app->put('/trafficLight/:estat/', 'updateTrafficLight');
$app->delete('/trafficLight/:id/', 'deleteTrafficLight');
$app->post('/trafficLight/:id/message/', 'addTrafficLightMessage');
$app->run();
function getTrafficLight($id) {
$sql = "select * FROM ".DB_TAULA_TRAFFICLIGHT." ";
try {
$db = getConnection();
$stmt = $db->query($sql);
$trafficlight = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
//echo '{"trafficlight": ' . json_encode($trafficlight) . '}';
echo json_encode($trafficlight);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function addTrafficLight($estat) {
$request = \Slim\Slim::getInstance()->request();
//$trafficlight = json_decode($request->getBody());
$sql = "INSERT INTO ".DB_TAULA_TRAFFICLIGHT." (estat) VALUES (:estat)";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("estat", $estat);
$stmt->execute();
$trafficlight->id = $db->lastInsertId();
$db = null;
updateStatusPusher($trafficlight->id, $estat);
echo json_encode($trafficlight);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function updateTrafficLight($id) {
$request = \Slim\Slim::getInstance()->request();
$body = $request->getBody();
parse_str($body, $trafficlight);
$sql = "UPDATE ".DB_TAULA_TRAFFICLIGHT." SET estat=:estat WHERE id=:id";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("estat", $trafficlight['estat']);
$stmt->bindParam("id", $id);
$stmt->execute();
$db = null;
updateStatusPusher($id, $trafficlight['estat']);
echo json_encode($trafficlight);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function deleteTrafficLight($id) {
$sql = "DELETE FROM ".DB_TAULA_TRAFFICLIGHT." WHERE id=:id";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $id);
$stmt->execute();
$db = null;
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
function getConnection() {
$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
function addTrafficLightMessage($id) {
$request = \Slim\Slim::getInstance()->request();
$body = $request->getBody();
parse_str($body, $trafficlight);
sendMessagePusher($id, $trafficlight['text']);
}
function updateStatusPusher($id, $estat){
$pusher = new Pusher(PUSHER_COM_KEY, PUSHER_COM_SECRET, PUSHER_COM_APP_ID);
$pusher->trigger('trafficLight', 'change', array('id' => $id, 'estat'=>$estat) );
}
function sendMessagePusher($id, $text){
$pusher = new Pusher(PUSHER_COM_KEY, PUSHER_COM_SECRET, PUSHER_COM_APP_ID);
$pusher->trigger('trafficLight', 'message', array('id' => $id, 'text'=>$text) );
}