-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotify_downtime_api.php
More file actions
144 lines (91 loc) · 3.69 KB
/
notify_downtime_api.php
File metadata and controls
144 lines (91 loc) · 3.69 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
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
135
136
137
138
139
140
141
142
143
144
<?php
include("connect.php");
include("send_firebase_notifications.php");
$link=Connection();
$response = "";
//Make sure that it is a POST request.
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0){
throw new Exception('Request method must be POST!');
}
//Make sure that the content type of the POST request has been set to application/json
$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if(strcasecmp($contentType, 'application/json') != 0){
throw new Exception('Content type must be: application/json');
}
# Get JSON as a string
$json_str = file_get_contents('php://input');
# Get as an object
$json_obj = json_decode($json_str,false);
//If json_decode failed, the JSON is invalid.
if(!is_array($json_obj)){
throw new Exception('Received content contained invalid JSON!');
}
//print_r($json_obj);
//Saving records from bot to the DB
foreach($json_obj as $record) {
// print_r($item);
print $record->machineNumber;
echo " | ";
print $record->Timestamp;
echo "<br>";
$saveMachineDownQuery = "INSERT INTO `machine_down_notifications` (`machineNumber`, `timestamp`) VALUES ('".$record->machineNumber."','".$record->Timestamp."')";
$result = mysqli_query($link,$saveMachineDownQuery) or die(mysqli_error($link));
if ($result==1){
$response = "Successfully saved";
}else{
echo $result;
}
}
//Get all notifications that are "not_sent" and send tham
$unsentNotificationQuery = "SELECT id,machineNumber,timestamp FROM `machine_down_notifications` WHERE machine_down_notifications.status='not_sent' ";
$unsentNotifications = mysqli_query($link,$unsentNotificationQuery) or die(mysqli_error($link));
if($unsentNotifications === FALSE) {
die(mysqli_error($link)); // TODO: better error handling
}
while($row = mysqli_fetch_array($unsentNotifications)) {
$id = $row["id"];
$machineNumber = $row["machineNumber"];
$timestamp = $row["timestamp"];
echo "<br>";
echo "ID :".$id;
echo " | ";
echo "Machine number :".$machineNumber;
echo " | ";
echo "Timestamp :".$timestamp;
echo "<br>";
$getFireBaseTokenQuery = "SELECT token FROM `androidtokens` WHERE androidtokens.tabid = (SELECT tabid FROM tabmachineallocation Where tabmachineallocation.machineNumber='".$machineNumber."')";
echo $getFireBaseTokenQuery;
echo "<br>";
$tokens = array();
$tokenResult = mysqli_query($link,$getFireBaseTokenQuery) or die(mysqli_error($link));
while($row = mysqli_fetch_array($tokenResult)) {
$tokens[] = $row["token"];
}
echo "Firebase token : ";
print_r($tokens);
echo "<br>";
$message = array("notificationId" => $id,"machineNumber" => $machineNumber ,"notificationType" => "downtime","timestamp" => $timestamp);
echo "Message to be sent : ";
print_r($message);
echo "<br>";
$message_status = send_notification($tokens, $message);
echo $message_status;
echo "<br>";
//Sample reply :{"multicast_id":6726022849045016561,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1516170517011919%6e665f98f9fd7ecd"}]}
//Update notification status
//Only for testing
$firebaseResponse = json_decode($message_status);
$firebaseSuccessValue = $firebaseResponse->{'success'};
if($firebaseSuccessValue==1){
$updateNotificationStatusQuery = "UPDATE `machine_down_notifications` SET machine_down_notifications.status= 'sent' WHERE machine_down_notifications.id= '".$id."' " ;
}
echo $updateNotificationStatusQuery."<br>";
$updateNotificationStatusResult = mysqli_query($link,$updateNotificationStatusQuery) or die(mysqli_error($link));
if ($updateNotificationStatusResult==1){
echo "Successfully updated";
}else{
echo $updateNextPlanQueryResult;
}
}
mysqli_close($link);
?>