This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connecttag.php
133 lines (112 loc) · 3.12 KB
/
connecttag.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
<?php require 'common.php'; ?>
<?php
function connectTag($eTagData){
require 'common.php';
//get json data from arduino
//recieves data in form of
/*{
"uid": (UID),
"pattern": (PATTERN),
"controluid": (CONTROLUID),
"state": (STATE),
"category": (CATEGORY),
"raw_cooked": (RAW_COOKED),
"fridge_freezer": (FRIDGE_FREEZER)
} where () is replaced with value */
$raw_data = $eTagData;
//$raw_data = $_POST['tagData'];
$tag_data = json_decode($raw_data, true);
$query = "
SELECT uid
FROM panels
WHERE alpha_uid = :alpha_uid
";
$query_params = array(
':alpha_uid' => $tag_data['controluid']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
$data = $stmt->fetchAll();
$controlId = $data[0]['uid'];
$query = "
SELECT uid
FROM tags
WHERE uid = :uid
";
$query_params = array(
':uid' => $tag_data['uid']
);
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
}
include_once 'getexpirydate.php';
$query_params = array(
':uid' => $tag_data['uid'],
':pattern' => $tag_data['pattern'],
':controluid' => $controlId,
':state' => $tag_data['state'],
':last_activation_date' => time(),
':category' => $tag_data['category'],
':raw_cooked' => $tag_data['raw_cooked'],
':fridge_freezer' => $tag_data['fridge_freezer'],
':expiry_date' => getExpiryDate($tag_data['category'], $tag_data['raw_cooked'], $tag_data['fridge_freezer'])
);
//no match, add tag as new entry
if ($stmt->rowCount() == 0){
$query = "
INSERT INTO tags
(uid, pattern, controluid, state, last_activation_date, category, raw_cooked, fridge_freezer, expiry_date)
VALUES
(:uid, :pattern, :controluid, :state, :last_activation_date, :category, :raw_cooked, :fridge_freezer, :expiry_date)
";
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query: " . $ex->getMessage());
return false;
}
return true;
} else {//found match, update existing tag information
$query = "
UPDATE tags
SET pattern = :pattern,
controluid = :controluid,
state = :state,
last_activation_date = :last_activation_date,
category = :category,
raw_cooked = :raw_cooked,
fridge_freezer = :fridge_freezer,
expiry_date = :expiry_date
WHERE uid = :uid
";
try
{
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
return false;
die("Failed to run query: " . $ex->getMessage());
}
return true;
}
}
?>