Browse Source

fix updater

dev
unknown 1 year ago
parent
commit
f4ee4d5a7c
5 changed files with 56 additions and 10 deletions
  1. +1
    -0
      .gitignore
  2. +5
    -3
      _FIRMWARE/firmwares/Shelly1/Shelly1_v2/Shelly1_v2/Shelly1_v2.ino
  3. +4
    -2
      api.php
  4. +1
    -0
      test.php
  5. +45
    -5
      update.php

+ 1
- 0
.gitignore View File

@ -7,3 +7,4 @@ _nemazat/css/main.css
_nemazat/css/font-awesome.min.css
app/logs/*.log
.vscode/sftp.json
app/updater/*.bin

+ 5
- 3
_FIRMWARE/firmwares/Shelly1/Shelly1_v2/Shelly1_v2/Shelly1_v2.ino View File

@ -54,6 +54,8 @@ void setup() {
attachInterrupt(digitalPinToInterrupt(SWITCH), handleInterrupt, CHANGE);
//wifi
if (ssid != "") {
WiFi.disconnect();
WiFi.softAPdisconnect(true);
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pasw);
@ -81,7 +83,7 @@ void setup() {
);
auto ret = ESPhttpUpdate.update(client, host2, url2);
auto ret = ESPhttpUpdate.update(client, host2, 80, url2);
delay(500);
switch(ret) {
case HTTP_UPDATE_FAILED:
@ -105,11 +107,11 @@ void setup() {
break;
}
delay(500);
Serial.println("Update proběhl!");
Serial.println(WiFi.localIP());
jsonContent = {};
jsonContent["token"] = apiToken;
jsonContent["values"]["on/off"]["value"] = (String)state;
jsonContent["settings"]["network"]["ip"] = WiFi.localIP().toString();
jsonContent["settings"]["network"]["mac"] = WiFi.macAddress();
sendDataToWeb();
return;
}

+ 4
- 2
api.php View File

@ -38,7 +38,9 @@ Db::connect (DBHOST, DBUSER, DBPASS, DBNAME);
//Read API data
$json = file_get_contents('php://input');
$obj = json_decode($json, true);
$logManager->write("[API] Rest API request body -> decodet to json \n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
if (defined(DEBUGMOD) && DEBUGMOD == 1) {
$logManager->write("[API] Rest API request body -> decodet to json \n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
}
//zabespecit proti Ddosu
if (isset($obj['user']) && $obj['user'] != ''){
@ -48,7 +50,7 @@ if (isset($obj['user']) && $obj['user'] != ''){
$userId = $user['user_id'];
$atHome = $obj['atHome'];
UserManager::atHome($userId, $atHome);
$logManager->write("[Record] user " . $userId . "changet his home state to " . $atHome . RECORDTIMOUT , LogRecordType::WARNING);
$logManager->write("[Record] user " . $userId . " changet his home state to " . $atHome . " " . RECORDTIMOUT , LogRecordType::INFO);
echo 'Saved: ' . $atHome;
header("HTTP/1.1 200 OK");
die();

+ 1
- 0
test.php View File

@ -1,6 +1,7 @@
<pre>
<?php
var_dump($_POST);
var_dump(file_exists("./app/updater/" . "3C71BF22FDCF" . ".bin"));
?>
</pre>

+ 45
- 5
update.php View File

@ -1,7 +1,31 @@
<?PHP
/** Includes **/
include_once('./config.php');
//Autoloader
$files = scandir('./app/class/');
$files = array_diff($files, array(
'.',
'..',
'app',
'ChartJS.php',
'ChartJS_Line.php',
'ChartManager.php',
'DashboardManager.php',
'Partial.php',
'Form.php',
'Route.php',
'Template.php',
'Ajax.php',
));
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])) {
@ -11,7 +35,7 @@ function check_header($name, $value = false)
return false;
}
return true;
}
}*/
function sendFile($path)
{
@ -23,7 +47,8 @@ function sendFile($path)
readfile($path);
}
if (!check_header('HTTP_USER_AGENT', 'ESP8266-http-Update')) {
/*if (!check_header('HTTP_USER_AGENT', 'ESP8266-http-Update')) {
header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden', true, 403);
echo "only for ESP8266 updater!\n";
exit();
@ -41,7 +66,22 @@ if (
header($_SERVER["SERVER_PROTOCOL"] . ' 403 Forbidden', true, 403);
echo "only for ESP8266 updater! (header)\n";
exit();
}
}*/
$localBinary = "./app/updater/" . $_SERVER['HTTP_X_ESP8266_STA_MAC'] . ".bin";
sendFile($localBinary);
$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);
if (file_exists($localBinary)) {
$logManager->write("[Update] version PHP: " . md5_file($localBinary), LogRecordType::INFO);
if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) {
sendFile($localBinary);
} else {
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
}
} else {
header("HTTP/1.1 404 Not Found");
}
die();

Loading…
Cancel
Save