Adtional tweeks and fixes

This commit is contained in:
JonatanRek 2020-10-05 21:12:06 +02:00
parent 94d6576070
commit e3b7e866f0
6 changed files with 43 additions and 28 deletions

View File

@ -31,10 +31,10 @@ class EndpointsApi extends ApiController{
]; ];
//Subdevice Registration //Subdevice Registration
$deviceId = DeviceManager::create($obj['token'], $obj['token']); $device = DeviceManager::create($obj['token'], $obj['token']);
foreach ($obj['values'] as $key => $value) { foreach ($obj['values'] as $key => $value) {
if (!SubDeviceManager::getSubDeviceByMaster($deviceId, $key)) { if (!SubDeviceManager::getSubDeviceByMaster($device['device_id'], $key)) {
SubDeviceManager::create($deviceId, $key, UNITS[$key]); SubDeviceManager::create($device['device_id'], $key, UNITS[$key]);
} }
} }
@ -61,6 +61,8 @@ class EndpointsApi extends ApiController{
], 401); ], 401);
} }
$device = DeviceManager::getDeviceByToken($obj['token']);
//Diagnostic //Diagnostic
if (isset($obj['settings'])){ if (isset($obj['settings'])){
$data = ['mac' => $obj['settings']["network"]["mac"], 'ip_address' => $obj['settings']["network"]["ip"]]; $data = ['mac' => $obj['settings']["network"]["mac"], 'ip_address' => $obj['settings']["network"]["ip"]];
@ -77,17 +79,15 @@ class EndpointsApi extends ApiController{
//Log Data Save //Log Data Save
if (isset($obj['logs'])){ if (isset($obj['logs'])){
foreach ($obj['logs'] as $log) { foreach ($obj['logs'] as $log) {
$logManager = new LogManager('../logs/devices/'. date("Y-m-d").'.log'); $deviceLogManager = new LogManager('../logs/devices/'. date("Y-m-d").'.log');
$logManager->setLevel(LOGLEVEL); $deviceLogManager->setLevel(LOGLEVEL);
$logManager->write("[Device Log Msg] Device_ID " . $deviceId . "->" . $log, LogRecordTypes::ERROR); $deviceLogManager->write("[Device Log Msg] Device_ID " . $device['device_id'] . "->" . $log, LogRecordTypes::ERROR);
unset($logManager); unset($deviceLogManager);
} }
} }
// Issuing command // Issuing command
if ($command == "null"){ if ($command == "null"){
$device = DeviceManager::getDeviceByToken($obj['token']);
$deviceId = $device['device_id'];
$deviceCommand = $device["command"]; $deviceCommand = $device["command"];
if ($deviceCommand != '' && $deviceCommand != null && $deviceCommand != "null") if ($deviceCommand != '' && $deviceCommand != null && $deviceCommand != "null")
{ {
@ -96,24 +96,22 @@ class EndpointsApi extends ApiController{
'command'=>'null' 'command'=>'null'
]; ];
DeviceManager::editByToken($obj['token'], $data); DeviceManager::editByToken($obj['token'], $data);
$logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordTypes::INFO); $logManager->write("[API] Device_ID " . $device['device_id'] . " executing command " . $command, LogRecordTypes::INFO);
} }
} }
$jsonAnswer = []; $jsonAnswer = [];
$subDeviceLastReordValue = []; $subDeviceLastReordValue = [];
$device = DeviceManager::getDeviceByToken($obj['token']);
$deviceId = $device['device_id'];
if (isset($obj['values'])) { if (isset($obj['values'])) {
//ZAPIS //ZAPIS
foreach ($obj['values'] as $key => $value) { foreach ($obj['values'] as $key => $value) {
if (!SubDeviceManager::getSubDeviceByMaster($deviceId, $key)) { if (!SubDeviceManager::getSubDeviceByMaster($device['device_id'], $key)) {
SubDeviceManager::create($deviceId, $key, UNITS[$key]); SubDeviceManager::create($device['device_id'], $key, UNITS[$key]);
} }
$subDeviceLastReordValue[$key] = $value['value']; $subDeviceLastReordValue[$key] = $value['value'];
RecordManager::create($deviceId, $key, round($value['value'],3)); RecordManager::create($device['device_id'], $key, round($value['value'],3));
$logManager->write("[API] Device_ID " . $deviceId . " writed value " . $key . ' ' . $value['value'], LogRecordTypes::INFO); $logManager->write("[API] Device_ID " . $device['device_id'] . " writed value " . $key . ' ' . $value['value'], LogRecordTypes::INFO);
//notification //notification
if ($key == 'door' || $key == 'water') { if ($key == 'door' || $key == 'water') {
@ -151,17 +149,17 @@ class EndpointsApi extends ApiController{
//upravit format na setings-> netvork etc //upravit format na setings-> netvork etc
$subDevicesTypeList = SubDeviceManager::getSubDeviceSTypeForMater($deviceId); $subDevicesTypeList = SubDeviceManager::getSubDeviceSTypeForMater($device['device_id']);
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'];
} }
} else { } else {
if (count(SubDeviceManager::getAllSubDevices($deviceId)) == 0) { if (count(SubDeviceManager::getAllSubDevices($device['device_id'])) == 0) {
//SubDeviceManager::create($deviceId, 'on/off', UNITS[$key]); //SubDeviceManager::create($device['device_id'], 'on/off', UNITS[$key]);
//RecordManager::create($deviceId, 'on/off', 0); //RecordManager::create($device['device_id'], 'on/off', 0);
} }
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceId); $subDevicesData = SubDeviceManager::getAllSubDevices($device['device_id']);
foreach ($subDevicesData as $key => $subDeviceData) { foreach ($subDevicesData as $key => $subDeviceData) {
$subDeviceId = $subDeviceData['subdevice_id']; $subDeviceId = $subDeviceData['subdevice_id'];
@ -186,6 +184,7 @@ class EndpointsApi extends ApiController{
$this->response($jsonAnswer); $this->response($jsonAnswer);
// this method returns response as json // this method returns response as json
unset($logManager); //unset($logManager); //TODO: Opravit
die();
} }
} }

View File

@ -16,4 +16,22 @@ class UsersApi extends ApiController{
$this->response($response); $this->response($response);
} }
public function status(){
//$this->requireAuth();
$response = null;
$obj = $this->input;
$atHome = $obj['atHome'];
$user = UserManager::getUser($obj['user']);
$userAtHome = $user['at_home'];
$userId = $user['user_id'];
if (!empty($user)) {
if($userAtHome != $atHome){
UserManager::atHome($userId, $atHome);
}
}
$this->response(['value'=>'OK']);
}
} }

View File

@ -3,7 +3,7 @@ class RoomManager{
public static $rooms; public static $rooms;
static function getDefaultRoomId() { static function getDefaultRoomId() {
$defaultRoom = Db::loadOne("SELECT room_id FROM rooms WHERE 'default' = 1"); $defaultRoom = Db::loadOne("SELECT `room_id` FROM `rooms` WHERE `default` = 1");
return $defaultRoom['room_id']; return $defaultRoom['room_id'];
} }

View File

@ -3,7 +3,7 @@ class UserManager
{ {
public static function getUsers ($filtr = ['*']) { public static function getUsers ($filtr = ['*']) {
try { try {
$allUsers = Db::loadAll ("SELECT " . implode($filtr, ",") . " FROM users"); $allUsers = Db::loadAll ("SELECT " . implode(",", $filtr) . " FROM users");
return $allUsers; return $allUsers;
} catch(PDOException $error) { } catch(PDOException $error) {
echo $error->getMessage(); echo $error->getMessage();

View File

@ -16,9 +16,7 @@ class Spotify extends VirtualDeviceManager {
} }
public function callback(){ public function callback(){
$token = $_GET; var_dump($_REQUEST);
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
var_dump($actual_link);
(new SettingsManager)->create('spotify_token', $token); (new SettingsManager)->create('spotify_token', $token);
} }

View File

@ -3,7 +3,6 @@ class Setting extends Template
{ {
function __construct() function __construct()
{ {
$userManager = new UserManager(); $userManager = new UserManager();
$langMng = new LanguageManager('en'); $langMng = new LanguageManager('en');
@ -57,6 +56,7 @@ class Setting extends Template
$template->prepare('rooms', $rooms); $template->prepare('rooms', $rooms);
$template->render(); $template->render();
} }
} }