OTA Tweeks
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
<?php
|
||||
class EndpointsApi extends ApiController{
|
||||
|
||||
public function default(){
|
||||
// $this->requireAuth();
|
||||
$obj = $this->input;
|
||||
@@ -72,116 +71,114 @@ class EndpointsApi extends ApiController{
|
||||
$this->response([
|
||||
'state' => 'succes',
|
||||
'command' => $command,
|
||||
]);
|
||||
}
|
||||
|
||||
// Issuing command
|
||||
if ($command == "null"){
|
||||
$device = DeviceManager::getDeviceByToken($obj['token']);
|
||||
$deviceId = $device['device_id'];
|
||||
$deviceCommand = $device["command"];
|
||||
if ($deviceCommand != '' && $deviceCommand != null && $deviceCommand != "null")
|
||||
{
|
||||
$command = $deviceCommand;
|
||||
$data = [
|
||||
'command'=>'null'
|
||||
];
|
||||
DeviceManager::editByToken($obj['token'], $data);
|
||||
$logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordType::INFO);
|
||||
}
|
||||
}
|
||||
|
||||
$jsonAnswer = [];
|
||||
$subDeviceLastReordValue = [];
|
||||
$device = DeviceManager::getDeviceByToken($obj['token']);
|
||||
$deviceId = $device['device_id'];
|
||||
|
||||
if (isset($obj['values'])) {
|
||||
//ZAPIS
|
||||
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);
|
||||
|
||||
//notification
|
||||
if ($key == 'door' || $key == 'water') {
|
||||
$notificationMng = new NotificationManager;
|
||||
$notificationData = [];
|
||||
|
||||
switch ($key) {
|
||||
case 'door':
|
||||
$notificationData = [
|
||||
'title' => 'Info',
|
||||
'body' => 'Someone just open up '.$device['name'],
|
||||
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
|
||||
];
|
||||
|
||||
break;
|
||||
case 'water':
|
||||
$notificationData = [
|
||||
'title' => 'Alert',
|
||||
'body' => 'Wather leak detected by '.$device['name'],
|
||||
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
|
||||
];
|
||||
break;
|
||||
}
|
||||
if (DEBUGMOD) $notificationData['body'] .= ' value='.$value['value'];
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//upravit format na setings-> netvork etc
|
||||
|
||||
$subDevicesTypeList = SubDeviceManager::getSubDeviceSTypeForMater($deviceId);
|
||||
if (!in_array($subDevicesTypeList, ['on/off', 'door', 'water'])) {
|
||||
$jsonAnswer['device']['sleepTime'] = $device['sleep_time'];
|
||||
}
|
||||
} else {
|
||||
if (count(SubDeviceManager::getAllSubDevices($deviceId)) == 0) {
|
||||
SubDeviceManager::create($deviceId, 'on/off', UNITS[$key]);
|
||||
//RecordManager::create($deviceId, 'on/off', 0);
|
||||
}
|
||||
|
||||
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceId);
|
||||
|
||||
foreach ($subDevicesData as $key => $subDeviceData) {
|
||||
$subDeviceId = $subDeviceData['subdevice_id'];
|
||||
$subDeviceLastReord = RecordManager::getLastRecord($subDeviceId);
|
||||
$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']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$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
|
||||
|
||||
], 200);
|
||||
}
|
||||
|
||||
private function sendFile($path)
|
||||
{
|
||||
// Issuing command
|
||||
if ($command == "null"){
|
||||
$device = DeviceManager::getDeviceByToken($obj['token']);
|
||||
$deviceId = $device['device_id'];
|
||||
$deviceCommand = $device["command"];
|
||||
if ($deviceCommand != '' && $deviceCommand != null && $deviceCommand != "null")
|
||||
{
|
||||
$command = $deviceCommand;
|
||||
$data = [
|
||||
'command'=>'null'
|
||||
];
|
||||
DeviceManager::editByToken($obj['token'], $data);
|
||||
$logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordType::INFO);
|
||||
}
|
||||
}
|
||||
|
||||
$jsonAnswer = [];
|
||||
$subDeviceLastReordValue = [];
|
||||
$device = DeviceManager::getDeviceByToken($obj['token']);
|
||||
$deviceId = $device['device_id'];
|
||||
|
||||
if (isset($obj['values'])) {
|
||||
//ZAPIS
|
||||
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);
|
||||
|
||||
//notification
|
||||
if ($key == 'door' || $key == 'water') {
|
||||
$notificationMng = new NotificationManager;
|
||||
$notificationData = [];
|
||||
|
||||
switch ($key) {
|
||||
case 'door':
|
||||
$notificationData = [
|
||||
'title' => 'Info',
|
||||
'body' => 'Someone just open up '.$device['name'],
|
||||
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
|
||||
];
|
||||
|
||||
break;
|
||||
case 'water':
|
||||
$notificationData = [
|
||||
'title' => 'Alert',
|
||||
'body' => 'Wather leak detected by '.$device['name'],
|
||||
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
|
||||
];
|
||||
break;
|
||||
}
|
||||
if (DEBUGMOD) $notificationData['body'] .= ' value='.$value['value'];
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//upravit format na setings-> netvork etc
|
||||
|
||||
$subDevicesTypeList = SubDeviceManager::getSubDeviceSTypeForMater($deviceId);
|
||||
if (!in_array($subDevicesTypeList, ['on/off', 'door', 'water'])) {
|
||||
$jsonAnswer['device']['sleepTime'] = $device['sleep_time'];
|
||||
}
|
||||
} else {
|
||||
if (count(SubDeviceManager::getAllSubDevices($deviceId)) == 0) {
|
||||
SubDeviceManager::create($deviceId, 'on/off', UNITS[$key]);
|
||||
//RecordManager::create($deviceId, 'on/off', 0);
|
||||
}
|
||||
|
||||
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceId);
|
||||
|
||||
foreach ($subDevicesData as $key => $subDeviceData) {
|
||||
$subDeviceId = $subDeviceData['subdevice_id'];
|
||||
$subDeviceLastReord = RecordManager::getLastRecord($subDeviceId);
|
||||
$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']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$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));
|
||||
@@ -215,7 +212,7 @@ class EndpointsApi extends ApiController{
|
||||
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);
|
||||
sendFile($localBinary);
|
||||
//get device data
|
||||
$device = DeviceManager::getDeviceByMac($macAddress);
|
||||
$deviceName = $device['name'];
|
||||
@@ -238,7 +235,7 @@ class EndpointsApi extends ApiController{
|
||||
}
|
||||
}
|
||||
} else {
|
||||
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
|
||||
header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified', true, 304);
|
||||
}
|
||||
} else {
|
||||
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
|
||||
|
Reference in New Issue
Block a user