Bether Log + Log Wia API

This commit is contained in:
JonatanRek 2020-03-16 14:52:36 +01:00
parent e14a0edb3c
commit f570556d85
2 changed files with 32 additions and 10 deletions

32
api.php
View File

@ -92,6 +92,7 @@ try {
$token = $obj['token']; $token = $obj['token'];
$values = null; $values = null;
$settings = null; $settings = null;
$deviceLogs = null;
$command = "null"; $command = "null";
if (isset($obj['values'])) { if (isset($obj['values'])) {
@ -102,6 +103,12 @@ if (isset($obj['settings'])) {
$settings = $obj['settings']; $settings = $obj['settings'];
} }
if (isset($obj['logs'])) {
$deviceLogs = $obj['logs'];
}
//Checks //Checks
if ($token == null || $token == "") { if ($token == null || $token == "") {
echo json_encode(array( echo json_encode(array(
@ -162,7 +169,7 @@ if (!DeviceManager::approved($token)) {
} }
// Diagnostic Data Write to DB // Diagnostic Data Write to DB
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"];
@ -175,20 +182,33 @@ if ($command == "null"){
$device = DeviceManager::getDeviceByToken($token); $device = DeviceManager::getDeviceByToken($token);
$deviceId = $device['device_id']; $deviceId = $device['device_id'];
$deviceCommand = $device["command"]; $deviceCommand = $device["command"];
if ($deviceCommand != '' || $deviceCommand != null) if ($deviceCommand != '' && $deviceCommand != null && $deviceCommand != "null")
{ {
$command = $deviceCommand; $command = $deviceCommand;
}
$data = [ $data = [
'command'=>'null' 'command'=>'null'
]; ];
DeviceManager::editByToken($token, $data); DeviceManager::editByToken($token, $data);
$logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordType::INFO); $logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordType::INFO);
} }
}
// Diagnostic Logs Write To log File
if ($deviceLogs != null && $deviceLogs != ""){
foreach ($deviceLogs as $log) {
$logManager->write("[Device Log Msg] Device_ID " . $deviceId . "->" . $log, LogRecordType::ERROR);
}
$jsonAnswer = [
'state' => 'succes',
'command' => $command,
];
echo json_encode($jsonAnswer, JSON_PRETTY_PRINT);
header($_SERVER["SERVER_PROTOCOL"]." 200 OK");
die();
}
// Subdevices first data! // Subdevices first data!
if ($values != null || $values != "") { if ($values != null && $values != "") {
//ZAPIS //ZAPIS
$device = DeviceManager::getDeviceByToken($token); $device = DeviceManager::getDeviceByToken($token);
@ -268,7 +288,7 @@ if ($values != null || $values != "") {
$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']);
} }

View File

@ -46,7 +46,9 @@ class LogManager
function write($value, $type = LogRecordType::ERROR){ function write($value, $type = LogRecordType::ERROR){
$record = "[".date("H:m:s")."][".$type."]" . $value . "\n"; $record = "[".date("H:m:s")."][".$type."]" . $value . "\n";
if (strlen($record) > 65 ) {
$record = Utilities::stringInsert($record,"\n",65); $record = Utilities::stringInsert($record,"\n",65);
}
fwrite($this->logFile, $record); fwrite($this->logFile, $record);
} }