modified: software/database/sqlite.php
modified: software/device-api.php Temperature registration added
This commit is contained in:
1 parent
34593f7ea4
commit
13e6cf1032
2 files changed
+30
-44
No files matched your search
+23
-4
@@ -71,10 +71,29 @@ $router->post('/api/config', function($req) use($db) {
|
||||
});
|
||||
|
||||
// Device reports temperature back on this endpoint.
|
||||
$router->post('/api/report', function($req) {
|
||||
echo 'body:' . var_dump($req->getBody()) . "\n";
|
||||
DumpHTTPRequestToFile::execute("/tmp/out.txt");
|
||||
http_response_code(200);
|
||||
$router->post('/api/report', function($req) use($db) {
|
||||
$msg = $req->getJSONBody();
|
||||
|
||||
if(!valid($msg,'id', '/^[0-9]+$/') ||
|
||||
!valid($msg,'temperature', '/^[0-9]+\.*[0-9]*$/') ||
|
||||
!valid($msg,'humidity', '/^[0-9]+\.*[0-9]*$/')
|
||||
) {
|
||||
echo '{"error_message": "Field verification failed."}';
|
||||
http_response_code(400);
|
||||
}
|
||||
else if($db->getDeviceByID($msg['id']) == null) {
|
||||
echo '{"error_message": "ID '.$msg['id'].' not found"}';
|
||||
http_response_code(400);
|
||||
}
|
||||
else {
|
||||
$data = [
|
||||
"temperature" => $msg['temperature'],
|
||||
"humidity" => $msg['humidity'],
|
||||
"read_at" => date('Y-m-d H:i:s')
|
||||
];
|
||||
$db->updateDevice($msg['id'], $data);
|
||||
http_response_code(200);
|
||||
}
|
||||
});
|
||||
|
||||
$router->get('/api/update', function() {
|
||||
|
||||
Reference in new issue
Block a user