Firmware Updater (Backend + GUI)
This commit is contained in:
parent
0d076eb67c
commit
44c4c84292
1
.gitignore
vendored
1
.gitignore
vendored
@ -6,5 +6,6 @@ _nemazat/css/main.css.map
|
||||
_nemazat/css/main.css
|
||||
_nemazat/css/font-awesome.min.css
|
||||
app/logs/*.log
|
||||
.vscode/
|
||||
.vscode/sftp.json
|
||||
app/updater/*.bin
|
||||
|
34
api.php
34
api.php
@ -38,8 +38,9 @@ Db::connect (DBHOST, DBUSER, DBPASS, DBNAME);
|
||||
//Read API data
|
||||
$json = file_get_contents('php://input');
|
||||
$obj = json_decode($json, true);
|
||||
|
||||
if (defined(DEBUGMOD) && DEBUGMOD == 1) {
|
||||
$logManager->write("[API] Rest API request body -> decodet to json \n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
||||
$logManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
|
||||
}
|
||||
|
||||
//zabespecit proti Ddosu
|
||||
@ -52,7 +53,7 @@ if (isset($obj['user']) && $obj['user'] != ''){
|
||||
UserManager::atHome($userId, $atHome);
|
||||
$logManager->write("[Record] user " . $userId . " changet his home state to " . $atHome . " " . RECORDTIMOUT , LogRecordType::INFO);
|
||||
echo 'Saved: ' . $atHome;
|
||||
header("HTTP/1.1 200 OK");
|
||||
header($_SERVER["SERVER_PROTOCOL"]." 200 OK");
|
||||
die();
|
||||
}
|
||||
}
|
||||
@ -64,7 +65,7 @@ if (DEBUGMOD != 1) {
|
||||
'state' => 'unsuccess',
|
||||
'errorMSG' => "Using API from your IP insnt alowed!",
|
||||
));
|
||||
header("HTTP/1.1 401 Unauthorized");
|
||||
header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
|
||||
$logManager->write("[API] acces denied from " . $_SERVER['REMOTE_ADDR'], LogRecordType::WARNING);
|
||||
exit();
|
||||
}
|
||||
@ -90,18 +91,23 @@ try {
|
||||
//Variables
|
||||
$token = $obj['token'];
|
||||
$values = null;
|
||||
$settings = null;
|
||||
|
||||
if (isset($obj['values'])) {
|
||||
$values = $obj['values'];
|
||||
}
|
||||
|
||||
if (isset($obj['settings'])) {
|
||||
$settings = $obj['settings'];
|
||||
}
|
||||
|
||||
//Checks
|
||||
if ($token == null || $token == "") {
|
||||
echo json_encode(array(
|
||||
'state' => 'unsuccess',
|
||||
'errorMSG' => "Missing Value Token in JSON payload",
|
||||
));
|
||||
header("HTTP/1.1 401 Unauthorized");
|
||||
header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
|
||||
die();
|
||||
}
|
||||
|
||||
@ -123,7 +129,7 @@ if (!DeviceManager::registeret($token)) {
|
||||
if ($notificationData != []) {
|
||||
$subscribers = $notificationMng::getSubscription();
|
||||
foreach ($subscribers as $key => $subscriber) {
|
||||
$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ");
|
||||
$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordType::INFO);
|
||||
$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
|
||||
}
|
||||
}
|
||||
@ -132,11 +138,11 @@ if (!DeviceManager::registeret($token)) {
|
||||
//Notification for newly added Device
|
||||
$subscribers = $notificationMng::getSubscription();
|
||||
foreach ($subscribers as $key => $subscriber) {
|
||||
$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ");
|
||||
$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordType::INFO);
|
||||
$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
|
||||
}
|
||||
|
||||
header("HTTP/1.1 401 Unauthorized");
|
||||
header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
|
||||
echo json_encode(array(
|
||||
'state' => 'unsuccess',
|
||||
'errorMSG' => "Device not registeret",
|
||||
@ -146,7 +152,7 @@ if (!DeviceManager::registeret($token)) {
|
||||
}
|
||||
|
||||
if (!DeviceManager::approved($token)) {
|
||||
header("HTTP/1.1 401 Unauthorized");
|
||||
header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
|
||||
echo json_encode(array(
|
||||
'state' => 'unsuccess',
|
||||
'errorMSG' => "Unaproved Device",
|
||||
@ -154,6 +160,11 @@ if (!DeviceManager::approved($token)) {
|
||||
exit();
|
||||
}
|
||||
|
||||
// Diagnostic Data Write to DB
|
||||
if (isset($settings)){
|
||||
DeviceManager::editByToken($token, ['mac' => $settings["network"]["mac"], 'ip_address' => $settings["network"]["ip"]]);
|
||||
}
|
||||
|
||||
// Subdevices first data!
|
||||
if ($values != null || $values != "") {
|
||||
|
||||
@ -193,7 +204,7 @@ if ($values != null || $values != "") {
|
||||
if ($notificationData != []) {
|
||||
$subscribers = $notificationMng::getSubscription();
|
||||
foreach ($subscribers as $key => $subscriber) {
|
||||
$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ");
|
||||
$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordType::INFO);
|
||||
$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
|
||||
}
|
||||
}
|
||||
@ -217,10 +228,9 @@ if ($values != null || $values != "") {
|
||||
$jsonAnswer['device']['sleepTime'] = $device['sleep_time'];
|
||||
}
|
||||
echo json_encode($jsonAnswer);
|
||||
header("HTTP/1.1 200 OK");
|
||||
header($_SERVER["SERVER_PROTOCOL"]." 200 OK");
|
||||
} else {
|
||||
//Vypis
|
||||
//TODO: doděla uložení výpisu jinými slovy zda li byl comman vykonán
|
||||
$device = DeviceManager::getDeviceByToken($token);
|
||||
$deviceId = $device['device_id'];
|
||||
|
||||
@ -248,7 +258,7 @@ if ($values != null || $values != "") {
|
||||
'state' => 'succes',
|
||||
'value' => $subDeviceLastReordValue
|
||||
));
|
||||
header("HTTP/1.1 200 OK");
|
||||
header($_SERVER["SERVER_PROTOCOL"]." 200 OK");
|
||||
}
|
||||
|
||||
unset($logManager);
|
||||
|
@ -47,6 +47,15 @@ class DeviceManager{
|
||||
}
|
||||
}
|
||||
|
||||
public function editByToken ($token, $values = []) {
|
||||
try {
|
||||
Db::edit ('devices', $values, 'WHERE token = ?', array($deviceId));
|
||||
} catch(PDOException $error) {
|
||||
echo $error->getMessage();
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* [assignRoom Přiřazení zařízení do třídy]
|
||||
* @param [type] $roomId [číslo místnosti do kter se má zařízení přiřadit]
|
||||
|
@ -67,6 +67,10 @@
|
||||
<div class="label">Type:</div>
|
||||
<input class="input" type="text" name="deviceToken" value="<?php echo $DEVICE['type']; ?>" disabled>
|
||||
</div>
|
||||
<div class="field">
|
||||
<div class="label">Mac Address:</div>
|
||||
<input class="input" type="text" name="deviceToken" value="<?php echo $DEVICE['mac']; ?>" disabled>
|
||||
</div>
|
||||
<div class="field">
|
||||
<div class="label">IP:</div>
|
||||
<input class="input" type="text" name="deviceIp" value="<?php echo $DEVICE['ip']; ?>" minlength="7" maxlength="15" size="15" pattern="^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$" disabled>
|
||||
@ -85,6 +89,13 @@
|
||||
<div class="label">DNS:</div>
|
||||
<input class="input" type="text" name="deviceDns" value="<?php echo $DEVICE['dns']; ?>" minlength="7" maxlength="15" size="15" pattern="^((\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$" disabled>
|
||||
</div>
|
||||
|
||||
<h4 class="mb-4"><?php $LANGMNG->echo('t_deviceVersion'); ?></h4>
|
||||
<div class="field">
|
||||
<div class="label"><?php $LANGMNG->echo('l_uploadFirmware'); ?></div>
|
||||
<input class="input" type="file" name="deviceFirmware" value="">
|
||||
</div>
|
||||
|
||||
<?php if ($DEVICE['userIsAdmin']) { ?>
|
||||
<?php if (!in_array($SUBDEVICE['type'], ['on/off', 'door', 'water'])) { ?>
|
||||
<div class="field">
|
||||
|
@ -161,6 +161,7 @@ class Home extends Template
|
||||
'room' => $deviceData['room_id'],
|
||||
'token' => $deviceData['token'],
|
||||
'type' => $deviceData['type'],
|
||||
'mac' => $deviceData['mac'],
|
||||
'ip' => $deviceData['ip_address'],
|
||||
'subnet' => $deviceData['subnet'],
|
||||
'gateway' => $deviceData['gateway'],
|
||||
|
64
update.php
64
update.php
@ -23,19 +23,8 @@ foreach($files as $file) {
|
||||
include './app/class/'. $file;
|
||||
}
|
||||
$logManager = new LogManager();
|
||||
header('Content-type: text/plain; charset=utf8', true);
|
||||
|
||||
/*
|
||||
function check_header($name, $value = false)
|
||||
{
|
||||
if (!isset($_SERVER[$name])) {
|
||||
return false;
|
||||
}
|
||||
if ($value && $_SERVER[$name] != $value) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}*/
|
||||
header('Content-type: text/plain; charset=utf8', true);
|
||||
|
||||
function sendFile($path)
|
||||
{
|
||||
@ -47,41 +36,36 @@ function sendFile($path)
|
||||
readfile($path);
|
||||
}
|
||||
|
||||
|
||||
/*if (!check_header('HTTP_USER_AGENT', 'ESP8266-http-Update')) {
|
||||
header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden', true, 403);
|
||||
echo "only for ESP8266 updater!\n";
|
||||
exit();
|
||||
}
|
||||
|
||||
if (
|
||||
!check_header('HTTP_X_ESP8266_STA_MAC') ||
|
||||
!check_header('HTTP_X_ESP8266_AP_MAC') ||
|
||||
!check_header('HTTP_X_ESP8266_FREE_SPACE') ||
|
||||
!check_header('HTTP_X_ESP8266_SKETCH_SIZE') ||
|
||||
!check_header('HTTP_X_ESP8266_SKETCH_MD5') ||
|
||||
!check_header('HTTP_X_ESP8266_CHIP_SIZE') ||
|
||||
!check_header('HTTP_X_ESP8266_SDK_VERSION')
|
||||
) {
|
||||
header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden', true, 403);
|
||||
echo "only for ESP8266 updater! (header)\n";
|
||||
exit();
|
||||
}*/
|
||||
|
||||
|
||||
|
||||
|
||||
$localBinary = "./app/updater/" . str_replace(':', '', $_SERVER['HTTP_X_ESP8266_STA_MAC']) . ".bin";
|
||||
$logManager->write("[Update] url: " . $localBinary, LogRecordType::INFO);
|
||||
$logManager->write("[Update] version: " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordType::INFO);
|
||||
$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("[Update] version PHP: " . md5_file($localBinary), LogRecordType::INFO);
|
||||
$logManager->write("[Updater] version PHP: " . md5_file($localBinary), LogRecordType::INFO);
|
||||
if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) {
|
||||
sendFile($localBinary);
|
||||
|
||||
//notification
|
||||
$notificationMng = new NotificationManager;
|
||||
$notificationData = [
|
||||
'title' => 'Info',
|
||||
'body' => 'Someone device 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);
|
||||
$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
|
||||
}
|
||||
} else {
|
||||
header("HTTP/1.1 404 Not Found");
|
||||
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
|
||||
}
|
||||
header($_SERVER["SERVER_PROTOCOL"].' 500 no version for ESP MAC', true, 500);
|
||||
die();
|
||||
|
Loading…
Reference in New Issue
Block a user