fix updater and other things
This commit is contained in:
		
							
								
								
									
										
											BIN
										
									
								
								_FIRMWARE/firmwares/Sonoff_Basic/Arduino_ide_setting.png
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								_FIRMWARE/firmwares/Sonoff_Basic/Arduino_ide_setting.png
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 19 KiB  | 
@@ -98,20 +98,19 @@ void setup() {
 | 
				
			|||||||
              Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
 | 
					              Serial.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
 | 
				
			||||||
              Serial.println();
 | 
					              Serial.println();
 | 
				
			||||||
              Serial.println();
 | 
					              Serial.println();
 | 
				
			||||||
              Serial.println();
 | 
					 | 
				
			||||||
              break;
 | 
					              break;
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
          case HTTP_UPDATE_NO_UPDATES:
 | 
					          case HTTP_UPDATE_NO_UPDATES:
 | 
				
			||||||
              Serial.println("HTTP_UPDATE_NO_UPDATES");
 | 
					              Serial.println("HTTP_UPDATE_NO_UPDATES");
 | 
				
			||||||
              Serial.println();
 | 
					              Serial.println();
 | 
				
			||||||
              Serial.println();
 | 
					              Serial.println();
 | 
				
			||||||
 | 
					              Serial.println();
 | 
				
			||||||
              break;
 | 
					              break;
 | 
				
			||||||
  
 | 
					  
 | 
				
			||||||
          case HTTP_UPDATE_OK:
 | 
					          case HTTP_UPDATE_OK:
 | 
				
			||||||
              Serial.println("HTTP_UPDATE_OK");
 | 
					              Serial.println("HTTP_UPDATE_OK");
 | 
				
			||||||
              Serial.println();
 | 
					              Serial.println();
 | 
				
			||||||
              Serial.println();
 | 
					              Serial.println();
 | 
				
			||||||
              Serial.println();
 | 
					 | 
				
			||||||
              break;
 | 
					              break;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      delay(500);
 | 
					      delay(500);
 | 
				
			||||||
@@ -214,7 +213,6 @@ void loadDataFromWeb() {
 | 
				
			|||||||
  } else if (requestState == "succes") {
 | 
					  } else if (requestState == "succes") {
 | 
				
			||||||
    unsuccessfulRounds = 0;
 | 
					    unsuccessfulRounds = 0;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					 | 
				
			||||||
  WiFi.hostname(hostName);
 | 
					  WiFi.hostname(hostName);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										29
									
								
								api.php
									
									
									
									
									
								
							
							
						
						
									
										29
									
								
								api.php
									
									
									
									
									
								
							@@ -125,7 +125,7 @@ if (!DeviceManager::registeret($token)) {
 | 
				
			|||||||
		if (!SubDeviceManager::getSubDeviceByMaster($deviceId, $key)) {
 | 
							if (!SubDeviceManager::getSubDeviceByMaster($deviceId, $key)) {
 | 
				
			||||||
			SubDeviceManager::create($deviceId, $key, UNITS[$key]);
 | 
								SubDeviceManager::create($deviceId, $key, UNITS[$key]);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		
 | 
					
 | 
				
			||||||
		if ($notificationData != []) {
 | 
							if ($notificationData != []) {
 | 
				
			||||||
			$subscribers = $notificationMng::getSubscription();
 | 
								$subscribers = $notificationMng::getSubscription();
 | 
				
			||||||
			foreach ($subscribers as $key => $subscriber) {
 | 
								foreach ($subscribers as $key => $subscriber) {
 | 
				
			||||||
@@ -134,14 +134,14 @@ if (!DeviceManager::registeret($token)) {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	//Notification for newly added Device
 | 
						//Notification for newly added Device
 | 
				
			||||||
	$subscribers = $notificationMng::getSubscription();
 | 
						$subscribers = $notificationMng::getSubscription();
 | 
				
			||||||
	foreach ($subscribers as $key => $subscriber) {
 | 
						foreach ($subscribers as $key => $subscriber) {
 | 
				
			||||||
		$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordType::INFO);
 | 
							$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordType::INFO);
 | 
				
			||||||
		$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
 | 
							$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
 | 
						header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
 | 
				
			||||||
	echo json_encode(array(
 | 
						echo json_encode(array(
 | 
				
			||||||
		'state' => 'unsuccess',
 | 
							'state' => 'unsuccess',
 | 
				
			||||||
@@ -164,14 +164,14 @@ if (!DeviceManager::approved($token)) {
 | 
				
			|||||||
if ($settings != null || $settings != ""){
 | 
					if ($settings != null || $settings != ""){
 | 
				
			||||||
	$data = ['mac' => $settings["network"]["mac"], 'ip_address' => $settings["network"]["ip"]];
 | 
						$data = ['mac' => $settings["network"]["mac"], 'ip_address' => $settings["network"]["ip"]];
 | 
				
			||||||
	if (array_key_exists("firmware_hash", $settings)) {
 | 
						if (array_key_exists("firmware_hash", $settings)) {
 | 
				
			||||||
		$data .= ['firmware_hash'=>$settings["firmware_hash"]];
 | 
							$data['firmware_hash'] = $settings["firmware_hash"];
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	DeviceManager::editByToken($token, $data);
 | 
						DeviceManager::editByToken($token, $data);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Subdevices first data!
 | 
					// Subdevices first data!
 | 
				
			||||||
if ($values != null || $values != "") {
 | 
					if ($values != null || $values != "") {
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	//ZAPIS
 | 
						//ZAPIS
 | 
				
			||||||
	$device = DeviceManager::getDeviceByToken($token);
 | 
						$device = DeviceManager::getDeviceByToken($token);
 | 
				
			||||||
	$deviceId = $device['device_id'];
 | 
						$deviceId = $device['device_id'];
 | 
				
			||||||
@@ -181,12 +181,12 @@ if ($values != null || $values != "") {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		RecordManager::create($deviceId, $key, round($value['value'],3));
 | 
							RecordManager::create($deviceId, $key, round($value['value'],3));
 | 
				
			||||||
		$logManager->write("[API] Device_ID " . $deviceId . " writed value " . $key . ' ' . $value['value'], LogRecordType::INFO);
 | 
							$logManager->write("[API] Device_ID " . $deviceId . " writed value " . $key . ' ' . $value['value'], LogRecordType::INFO);
 | 
				
			||||||
		
 | 
					
 | 
				
			||||||
		//notification
 | 
							//notification
 | 
				
			||||||
		if ($key == 'door' || $key == 'water') {
 | 
							if ($key == 'door' || $key == 'water') {
 | 
				
			||||||
			$notificationMng = new NotificationManager;
 | 
								$notificationMng = new NotificationManager;
 | 
				
			||||||
			$notificationData = [];
 | 
								$notificationData = [];
 | 
				
			||||||
			
 | 
					
 | 
				
			||||||
			switch ($key) {
 | 
								switch ($key) {
 | 
				
			||||||
				case 'door':
 | 
									case 'door':
 | 
				
			||||||
					$notificationData = [
 | 
										$notificationData = [
 | 
				
			||||||
@@ -194,7 +194,7 @@ if ($values != null || $values != "") {
 | 
				
			|||||||
						'body' => 'Someone just open up '.$device['name'],
 | 
											'body' => 'Someone just open up '.$device['name'],
 | 
				
			||||||
						'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
 | 
											'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
 | 
				
			||||||
					];
 | 
										];
 | 
				
			||||||
					
 | 
					
 | 
				
			||||||
				break;
 | 
									break;
 | 
				
			||||||
				case 'water':
 | 
									case 'water':
 | 
				
			||||||
					$notificationData = [
 | 
										$notificationData = [
 | 
				
			||||||
@@ -214,9 +214,10 @@ if ($values != null || $values != "") {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	$hostname = strtolower($device['name']);
 | 
						$hostname = strtolower($device['name']);
 | 
				
			||||||
	$hostname = str_replace(' ', '_', $hostname);
 | 
						$hostname = str_replace(' ', '_', $hostname);
 | 
				
			||||||
 | 
						//upravit format na setings-> netvork etc
 | 
				
			||||||
	$jsonAnswer = [
 | 
						$jsonAnswer = [
 | 
				
			||||||
		'device' => [
 | 
							'device' => [
 | 
				
			||||||
			'hostname' => $hostname,
 | 
								'hostname' => $hostname,
 | 
				
			||||||
@@ -226,7 +227,7 @@ if ($values != null || $values != "") {
 | 
				
			|||||||
		],
 | 
							],
 | 
				
			||||||
		'state' => 'succes',
 | 
							'state' => 'succes',
 | 
				
			||||||
	];
 | 
						];
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	$subDevicesTypeList = SubDeviceManager::getSubDeviceSTypeForMater($deviceId);
 | 
						$subDevicesTypeList = SubDeviceManager::getSubDeviceSTypeForMater($deviceId);
 | 
				
			||||||
	if (!in_array($subDevicesTypeList, ['on/off', 'door', 'water'])) {
 | 
						if (!in_array($subDevicesTypeList, ['on/off', 'door', 'water'])) {
 | 
				
			||||||
		$jsonAnswer['device']['sleepTime'] = $device['sleep_time'];
 | 
							$jsonAnswer['device']['sleepTime'] = $device['sleep_time'];
 | 
				
			||||||
@@ -237,21 +238,21 @@ if ($values != null || $values != "") {
 | 
				
			|||||||
	//Vypis
 | 
						//Vypis
 | 
				
			||||||
	$device = DeviceManager::getDeviceByToken($token);
 | 
						$device = DeviceManager::getDeviceByToken($token);
 | 
				
			||||||
	$deviceId = $device['device_id'];
 | 
						$deviceId = $device['device_id'];
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	if (count(SubDeviceManager::getAllSubDevices($deviceId)) == 0) {
 | 
						if (count(SubDeviceManager::getAllSubDevices($deviceId)) == 0) {
 | 
				
			||||||
		SubDeviceManager::create($deviceId, 'on/off', UNITS[$key]);
 | 
							SubDeviceManager::create($deviceId, 'on/off', UNITS[$key]);
 | 
				
			||||||
		//RecordManager::create($deviceId, 'on/off', 0);
 | 
							//RecordManager::create($deviceId, 'on/off', 0);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	$subDeviceId = SubDeviceManager::getAllSubDevices($deviceId)[0]['subdevice_id'];
 | 
						$subDeviceId = SubDeviceManager::getAllSubDevices($deviceId)[0]['subdevice_id'];
 | 
				
			||||||
	$subDeviceLastReord = RecordManager::getLastRecord($subDeviceId);
 | 
						$subDeviceLastReord = RecordManager::getLastRecord($subDeviceId);
 | 
				
			||||||
	$subDeviceLastReordValue = $subDeviceLastReord['value'];
 | 
						$subDeviceLastReordValue = $subDeviceLastReord['value'];
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	if ($subDeviceLastReord['execuded'] == 0){
 | 
						if ($subDeviceLastReord['execuded'] == 0){
 | 
				
			||||||
		$logManager->write("[API] subDevice id ".$subDeviceId . " executed comand with value " .$subDeviceLastReordValue . " record id " . $subDeviceLastReord['record_id'] . " executed " . $subDeviceLastReord['execuded']);
 | 
							$logManager->write("[API] subDevice id ".$subDeviceId . " executed comand with value " .$subDeviceLastReordValue . " record id " . $subDeviceLastReord['record_id'] . " executed " . $subDeviceLastReord['execuded']);
 | 
				
			||||||
		RecordManager::setExecuted($subDeviceLastReord['record_id']);
 | 
							RecordManager::setExecuted($subDeviceLastReord['record_id']);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	
 | 
					
 | 
				
			||||||
	echo json_encode(array(
 | 
						echo json_encode(array(
 | 
				
			||||||
		'device' => [
 | 
							'device' => [
 | 
				
			||||||
			'hostname' => $device['name'],
 | 
								'hostname' => $device['name'],
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -49,7 +49,7 @@ class DeviceManager{
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	public function editByToken ($token, $values = []) {
 | 
						public function editByToken ($token, $values = []) {
 | 
				
			||||||
		try {
 | 
							try {
 | 
				
			||||||
			Db::edit ('devices', $values, 'WHERE token = ?', array($deviceId));
 | 
								Db::edit ('devices', $values, 'WHERE token = ?', array($token));
 | 
				
			||||||
		} catch(PDOException $error) {
 | 
							} catch(PDOException $error) {
 | 
				
			||||||
			echo $error->getMessage();
 | 
								echo $error->getMessage();
 | 
				
			||||||
			die();
 | 
								die();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,8 +16,8 @@ class NotificationManager
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	function getSubscription(){
 | 
						function getSubscription () {
 | 
				
			||||||
		return Db::loadAll('SELECT * FROM notifications;', array());
 | 
							return Db::loadAll ("SELECT * FROM notifications");
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	function sendSimpleNotification(string $serverKey, string $to, array $data, bool $timeStamp = false){
 | 
						function sendSimpleNotification(string $serverKey, string $to, array $data, bool $timeStamp = false){
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,8 +7,9 @@ if (isset($_POST) && !empty($_POST)){
 | 
				
			|||||||
		$sleepTime = 0;
 | 
							$sleepTime = 0;
 | 
				
			||||||
		if (isset($_FILES['deviceFirmware']) && isset($_FILES['deviceFirmware']['tmp_name']) && $_FILES['deviceFirmware']['tmp_name'] != "") {
 | 
							if (isset($_FILES['deviceFirmware']) && isset($_FILES['deviceFirmware']['tmp_name']) && $_FILES['deviceFirmware']['tmp_name'] != "") {
 | 
				
			||||||
			$file = $_FILES['deviceFirmware'];
 | 
								$file = $_FILES['deviceFirmware'];
 | 
				
			||||||
			$fileName = (isset ($_POST['deviceMac']) ? str_replace(":", "", $_POST['deviceMac']) . ".bin" : "");
 | 
								$deviceMac = DeviceManager::getDeviceById($deviceId)['mac'];
 | 
				
			||||||
			if (file_exists("./app/updater/" . $fileName)) {
 | 
								$fileName = (isset ($deviceMac) && $deviceMac != "" ? str_replace(":", "", $deviceMac) . ".bin" : "");
 | 
				
			||||||
 | 
								if ($fileName != "" && file_exists("./app/updater/" . $fileName)) {
 | 
				
			||||||
				unlink("./app/updater/" . $fileName);
 | 
									unlink("./app/updater/" . $fileName);
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			if ($fileName != "") {
 | 
								if ($fileName != "") {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										41
									
								
								update.php
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								update.php
									
									
									
									
									
								
							@@ -22,6 +22,10 @@ $files = array_diff($files, array(
 | 
				
			|||||||
foreach($files as $file) {
 | 
					foreach($files as $file) {
 | 
				
			||||||
	include './app/class/'.  $file;
 | 
						include './app/class/'.  $file;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					//DB Conector
 | 
				
			||||||
 | 
					Db::connect (DBHOST, DBUSER, DBPASS, DBNAME);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
$logManager = new LogManager();
 | 
					$logManager = new LogManager();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
header('Content-type: text/plain; charset=utf8', true);
 | 
					header('Content-type: text/plain; charset=utf8', true);
 | 
				
			||||||
@@ -52,33 +56,28 @@ function sendFile($path)
 | 
				
			|||||||
$localBinary = "./app/updater/" . str_replace(':', '', $_SERVER['HTTP_X_ESP8266_STA_MAC']) . ".bin";
 | 
					$localBinary = "./app/updater/" . str_replace(':', '', $_SERVER['HTTP_X_ESP8266_STA_MAC']) . ".bin";
 | 
				
			||||||
$logManager->write("[Updater] url: " . $localBinary, LogRecordType::INFO);
 | 
					$logManager->write("[Updater] url: " . $localBinary, LogRecordType::INFO);
 | 
				
			||||||
$logManager->write("[Updater] version: " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordType::INFO);
 | 
					$logManager->write("[Updater] version: " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordType::INFO);
 | 
				
			||||||
 | 
					 | 
				
			||||||
if (file_exists($localBinary)) {
 | 
					if (file_exists($localBinary)) {
 | 
				
			||||||
	$logManager->write("[Updater] 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)) {
 | 
						if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) {
 | 
				
			||||||
		sendFile($localBinary);
 | 
							sendFile($localBinary);
 | 
				
			||||||
 | 
					 | 
				
			||||||
		//notification
 | 
							//notification
 | 
				
			||||||
			$notificationMng = new NotificationManager;
 | 
							$notificationMng = new NotificationManager;
 | 
				
			||||||
			$notificationData = [
 | 
							$notificationData = [
 | 
				
			||||||
				'title' => 'Info',
 | 
								'title' => 'Info',
 | 
				
			||||||
				'body' => 'Someone device was just updated to new version',
 | 
								'body' => 'Someone device was just updated to new version',
 | 
				
			||||||
				'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
 | 
								'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
 | 
				
			||||||
			];
 | 
							];
 | 
				
			||||||
		
 | 
							if ($notificationData != []) {
 | 
				
			||||||
			if ($notificationData != []) {
 | 
								$subscribers = $notificationMng->getSubscription();
 | 
				
			||||||
				$subscribers = $notificationMng::getSubscription();
 | 
								foreach ($subscribers as $key => $subscriber) {
 | 
				
			||||||
				foreach ($subscribers as $key => $subscriber) {
 | 
									$logManager->write("[NOTIFICATION] SENDING TO " . $subscriber['id'] . " ", LogRecordType::INFO);
 | 
				
			||||||
					$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordType::INFO);
 | 
									$answer = $notificationMng->sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
 | 
				
			||||||
					$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
 | 
					 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
 | 
								header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	header($_SERVER["SERVER_PROTOCOL"].' 500 no version for ESP MAC', true, 500);
 | 
					} else {
 | 
				
			||||||
	die();
 | 
						header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
 | 
				
			||||||
	
 | 
					}
 | 
				
			||||||
 | 
					die();
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user