add update

This commit is contained in:
Haitem 2020-07-14 18:24:21 +02:00
parent 7a3beea274
commit 7f5d348d07
2 changed files with 90 additions and 41 deletions

View File

@ -15,16 +15,13 @@ $router->any('/setting', 'Setting');
$router->any('/ajax', 'Ajax');
$router->any('/oauth', 'Oauth');
//Vue APP
$router->post('/api/login', 'AuthApi@login');
$router->post('/api/logout', 'AuthApi@logout');
$router->get('/api/rooms', 'RoomsApi@default');
$router->get('/api/rooms/{roomId}/update', 'RoomsApi@update');
$router->get('/api/devices', 'DevicesApi@default');
$router->get('/api/users', 'UsersApi@default');
$router->post('/api/widgets/{widgetId}/run', 'WidgetApi@run');
$router->post('/api/widgets/{widgetId}/check', 'WidgetApi@check');
$router->post('/api/widgets/{widgetId}/detail', 'WidgetApi@detail');
@ -35,7 +32,9 @@ $router->any('/api/HA/auth', 'Oauth');
$router->any('/api/HA', 'GoogleHomeApi@response');
//Endpoints API
$router->post('/api/endpoint', 'EndpointsApi@default');
$router->post('/api/endpoint/', 'EndpointsApi@default');
$router->post('/api/update/', 'EndpointsApi@update');
$router->post('/api/users/status', 'UsersApi@status');
// examples
$router->any('/api/example', 'ExampleApi@example');

View File

@ -18,6 +18,7 @@ class EndpointsApi extends ApiController{
'state' => 'unsuccess',
'errorMSG' => "Missing Value Token in JSON payload",
], 401);
die();
}
//Vstupní Checky
@ -90,16 +91,18 @@ class EndpointsApi extends ApiController{
}
}
if (isset($obj['values'])) {
//zapis
$jsonAnswer = [];
$subDeviceLastReordValue = [];
$device = DeviceManager::getDeviceByToken($obj['token']);
$deviceId = $device['device_id'];
if (isset($obj['values'])) {
//ZAPIS
$device = DeviceManager::getDeviceByToken($obj['token']);
$deviceId = $device['device_id'];
foreach ($obj['values'] as $key => $value) {
if (!SubDeviceManager::getSubDeviceByMaster($deviceId, $key)) {
SubDeviceManager::create($deviceId, $key, UNITS[$key]);
}
$subDeviceLastReordValue[$key] = $value['value'];
RecordManager::create($deviceId, $key, round($value['value'],3));
$logManager->write("[API] Device_ID " . $deviceId . " writed value " . $key . ' ' . $value['value'], LogRecordType::INFO);
@ -136,63 +139,110 @@ class EndpointsApi extends ApiController{
}
}
$hostname = strtolower($device['name']);
$hostname = str_replace(' ', '_', $hostname);
//upravit format na setings-> netvork etc
$jsonAnswer = [
'device' => [
'hostname' => $hostname,
'ipAddress' => $device['ip_address'],
'subnet' => $device['subnet'],
'gateway' => $device['gateway'],
],
'state' => 'succes',
'command' => $command,
];
$subDevicesTypeList = SubDeviceManager::getSubDeviceSTypeForMater($deviceId);
if (!in_array($subDevicesTypeList, ['on/off', 'door', 'water'])) {
$jsonAnswer['device']['sleepTime'] = $device['sleep_time'];
}
$this->response($jsonAnswer);
} else {
//Vypis
$device = DeviceManager::getDeviceByToken($obj['token']);
$deviceId = $device['device_id'];
if (count(SubDeviceManager::getAllSubDevices($deviceId)) == 0) {
SubDeviceManager::create($deviceId, 'on/off', UNITS[$key]);
//RecordManager::create($deviceId, 'on/off', 0);
}
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceId);
$subDeviceLastReordValue = [];
foreach ($subDevicesData as $key => $subDeviceData) {
$subDeviceId = $subDeviceData['subdevice_id'];
$subDeviceLastReord = RecordManager::getLastRecord($subDeviceId);
$subDeviceLastReordValue[] = [$subDeviceData['type'] => $subDeviceLastReord['value']];
$subDeviceLastReordValue[$subDeviceData['type']] = $subDeviceLastReord['value'];
if ($subDeviceLastReord['execuded'] == 0){
$logManager->write("[API] subDevice_ID ".$subDeviceId . " executed comand with value " . json_encode($subDeviceLastReordValue) ." executed " . $subDeviceLastReord['execuded'], LogRecordType::INFO);
RecordManager::setExecuted($subDeviceLastReord['record_id']);
}
}
$this->response(['device' => [
'hostname' => $device['name'],
'ipAddress' => $device['ip_address'],
'subnet' => $device['subnet'],
'gateway' => $device['gateway'],
],
'state' => 'succes',
'value' => $subDeviceLastReordValue,
'command' => $command]);
}
$hostname = "";
$hostname = strtolower($device['name']);
$hostname = str_replace(' ', '_', $hostname);
$jsonAnswer['device']['hostname'] = $hostname;
$jsonAnswer['state'] = 'succes';
$jsonAnswer['values'] = $subDeviceLastReordValue;
$jsonAnswer['command'] = $command;
$this->response($jsonAnswer);
// this method returns response as json
}
private function sendFile($path)
{
header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK', true, 200);
header('Content-Type: application/octet-stream', true);
header('Content-Disposition: attachment; filename=' . basename($path));
header('Content-Length: ' . filesize($path), true);
header('x-MD5: ' . md5_file($path), true);
readfile($path);
}
public function update() {
$logManager = new LogManager();
header('Content-type: text/plain; charset=utf8', true);
//Filtrování IP adress
if (DEBUGMOD != 1) {
if (!in_array($_SERVER['REMOTE_ADDR'], HOMEIP)) {
echo json_encode(array(
'state' => 'unsuccess',
'errorMSG' => "Using API from your IP insnt alowed!",
));
header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
$logManager->write("[Updater] acces denied from " . $_SERVER['REMOTE_ADDR'], LogRecordType::WARNING);
exit();
}
}
$macAddress = $_SERVER['HTTP_X_ESP8266_STA_MAC'];
$localBinary = "./app/updater/" . str_replace(':', '', $macAddress) . ".bin";
$logManager->write("[Updater] url: " . $localBinary, LogRecordType::INFO);
$logManager->write("[Updater] version: " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordType::INFO);
if (file_exists($localBinary)) {
$logManager->write("[Updater] version PHP: \n" . md5_file($localBinary), LogRecordType::INFO);
if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) {
$this->sendFile($localBinary);
//get device data
$device = DeviceManager::getDeviceByMac($macAddress);
$deviceName = $device['name'];
$deviceId = $device['device_id'];
//logfile write
$logManager->write("[Device] device_ID " . $deviceId . " was just updated to new version", LogRecordType::WARNING);
$logManager->write("[Device] version hash: \n" . md5_file($localBinary), LogRecordType::INFO);
//notification
$notificationMng = new NotificationManager;
$notificationData = [
'title' => 'Info',
'body' => $deviceName.' was just updated to new version',
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
];
if ($notificationData != []) {
$subscribers = $notificationMng->getSubscription();
foreach ($subscribers as $key => $subscriber) {
$logManager->write("[NOTIFICATION] SENDING TO " . $subscriber['id'] . " ", LogRecordType::INFO);
$answer = $notificationMng->sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
}
}
} else {
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
}
} else {
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
}
die();
}
}