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

View File

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

View File

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

View File

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