FTP -> GIT (SYNC)

This commit is contained in:
JonatanRek
2019-09-19 14:48:31 +02:00
parent ad3118799f
commit 8f709bfb30
85 changed files with 310 additions and 173 deletions

172
app/views/Ajax.php Normal file
View File

@@ -0,0 +1,172 @@
<?php
$files = scandir('app/class/');
$files = array_diff($files, array('.', '..'));
foreach($files as $file) {
include_once 'app/class/'. $file;
}
class Ajax extends Template
{
function __construct()
{
global $userManager;
global $lang;
if (!$userManager->isLogin()){
header('Location: ./');
}
$is_ajax = 'XMLHttpRequest' == ( $_SERVER['HTTP_X_REQUESTED_WITH'] ?? '' );
if (!$is_ajax){
header('Location: ./');
}
if (isset($_POST['subDevice_id'])){
$subDeviceId = $_POST['subDevice_id'];
if (isset($_POST['lastRecord'])){
echo RecordManager::getLastRecord($subDeviceId)['execuded'];
die();
}
$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
if ($subDeviceData['type'] == 'on/off'){
//TODO: Pridelat kontrolu změnit stav pouze pokud se poslední [executed] stav != novému
if (RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value'] == 0){
RecordManager::create($deviceId, 'on/off', 1);
echo 'ON';
}else{
RecordManager::create($deviceId, 'on/off', 0);
echo 'OFF';
}
}
} else if (isset($_POST['automation_id'])){
$automationId = $_POST['automation_id'];
if (isset($_POST['action']) && $_POST['action'] == 'delete') {
AutomationManager::remove($automationId);
}else {
AutomationManager::deactive($automationId);
}
} else if (isset($_POST['subDevice']) && isset($_POST['action']) && $_POST['action'] == "chart") {
//TODO lepe rozstrukturovat
$subDeviceId = $_POST['subDevice'];
$period = $_POST['period'];
$groupBy = $_POST['group'];
$subDevice = SubDeviceManager::getSubDevice($subDeviceId);
$records = RecordManager::getAllRecordForGraph($subDeviceId, $period, $groupBy);
$array = array_column($records, 'value');
if ($subDevice['type'] == 'light'){
foreach ($array as $key => $value) {
if ($value == 1 || $value == 0)
{
return;
}
if ($value > 810){
$array[$key] = 1;
} else {
$array[$key] = 0;
}
}
}
$data = json_encode($array);
$arrayTimeStamps = array_column($records, 'time');
foreach ($arrayTimeStamps as $key => $value) {
$arrayTimeStamps[$key] = (new DateTime($value))->format(TIMEFORMAT);
}
$labels = json_encode($arrayTimeStamps);
$range = RANGES[$subDevice['type']];
header('Content-Type: application/json');
$JSON = '{
"type": "line",
"data": {
"labels": ' . $labels . ',
"datasets": [{
"data": ' . $data . ',
"backgroundColor": "#7522bf",
"lineTension": 0,
"radius": 5
}]
},
"options": {
"legend": {
"display": false
},
"scales": {
"yAxes": [{
"ticks": {
"min": ' . $range['min'] . ',
"max": ' . $range['max'] . ',
"steps": ' . $range['scale'] . '
}
}]
},
"tooltips": {
"enabled": false
},
"hover": {
"mode": null
}
}
}';
echo $JSON;
} else if (isset($_POST['action']) && $_POST['action'] == "getState") {
//State Update
$roomsData = RoomManager::getAllRooms();
$subDevices = [];
foreach ($roomsData as $roomKey => $roomsData) {
$devicesData = DeviceManager::getAllDevicesInRoom($roomsData['room_id']);
foreach ($devicesData as $deviceKey => $deviceData) {
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
$parsedValue = round($lastRecord['value']);
//TODO: Předelat na switch snažší přidávání
/*Value Parsing*/
if ($subDeviceData['type'] == "on/off") {
$parsedValue = ($parsedValue == 1 ? 'ON' : 'OFF');
}
if ($subDeviceData['type'] == "light") {
$replacementTrue = 'Light';
$replacementFalse = 'Dark';
if ($parsedValue != 1){
//Analog Reading
$parsedValue = ($parsedValue <= 810 ? $replacementTrue : $replacementFalse);
} else {
//Digital Reading
$parsedValue = ($parsedValue == 0 ? $replacementTrue : $replacementFalse);
}
}
if ($subDeviceData['type'] == "door") {
$replacementTrue = 'Closed';
$replacementFalse = 'Opened';
$parsedValue = ($parsedValue == 1 ? $replacementTrue : $replacementFalse);
}
$subDevices[$subDeviceData['subdevice_id']] = [
'value' => $parsedValue .$subDeviceData['unit'],
'time' => $lastRecord['time'],
];
}
}
}
echo json_encode($subDevices);
die();
} else if (isset($_POST['scene_id'])) {
$sceneId = $_POST['scene_id'];
if (isset($_POST['action']) && $_POST['action'] == 'delete') {
SceneManager::delete($sceneId);
}else {
echo SceneManager::execScene($sceneId);
}
}
die();
}
}

61
app/views/Automation.php Normal file
View File

@@ -0,0 +1,61 @@
<?php
$files = scandir('app/class/');
$files = array_diff($files, array('.', '..'));
foreach($files as $file) {
include_once 'app/class/'. $file;
}
class Automation extends Template
{
function __construct()
{
global $userManager;
global $lang;
if (!$userManager->isLogin()){
header('Location: ./login');
}
$automations = [];
$automationsData = AutomationManager::getAll();
foreach ($automationsData as $automationKey => $automationData) {
$doSomething = [];
foreach (json_decode($automationData['do_something']) as $deviceId => $subDeviceState) {
$subDeviceMasterDeviceData = DeviceManager::getDeviceById($deviceId);
$doSomething[$deviceId] = [
'name' => $subDeviceMasterDeviceData['name'],
'state' => $subDeviceState,
];
}
$automations[$automationData['automation_id']] = [
'name' => '',
'onDays' => json_decode($automationData['on_days']),
'ifSomething' => $automationData['if_something'],
'doSomething' => $doSomething,
'active' => $automationData['active'],
];
}
$approvedSubDevices = [];
$allDevicesData = DeviceManager::getAllDevices();
foreach ($allDevicesData as $deviceKey => $deviceValue) {
if (!$deviceValue['approved']) continue;
$allSubDevicesData = SubDeviceManager::getAllSubDevices($deviceValue['device_id']);
foreach ($allSubDevicesData as $subDeviceKey => $subDeviceValue) {
$approvedSubDevices[$subDeviceValue['subdevice_id']] = [
'name' => $allDevicesData[$deviceKey]['name'] . $allDevicesData[$deviceKey]['device_id'],
'type' => $subDeviceValue['type'],
'masterDevice' => $subDeviceValue['device_id'],
];
}
}
$template = new Template('automation');
$template->prepare('title', 'Automation');
$template->prepare('lang', $lang);
$template->prepare('automations', $automations);
$template->prepare('subDevices', $approvedSubDevices);
$template->render();
}
}

90
app/views/Dashboard.php Normal file
View File

@@ -0,0 +1,90 @@
<?php
class Dashboard extends Template
{
function __construct()
{
global $userManager;
global $lang;
if (!$userManager->isLogin()){
header('Location: ./login');
}
$template = new Template('dashboard');
$dashboard = [];
$dashboardData = DashboardManager::getAllDashboards($userManager->getUserData('user_id'));
foreach ($dashboardData as $dashboardItemKey => $dashboardItemValue) {
$subDeviceData = SubDeviceManager::getSubDevice($dashboardItemValue['subdevice_id']);
$deviceData = SubDeviceManager::getSubDeviceMaster($dashboardItemValue['subdevice_id']);
$lastRecord = RecordManager::getLastRecord($dashboardItemValue['subdevice_id']);
$parsedValue = $lastRecord['value'];
//TODO: Opravit aby to bylo stejné parsování jako na HOME
if ($subDeviceData['type'] == "on/off") {
$parsedValue = ($parsedValue == 1 ? 'ON' : 'OFF');
}
if ($subDeviceData['type'] == "light") {
$parsedValue = ($parsedValue == 1 ? 'Light' : 'Dark');
}
$dashboard[$dashboardItemValue['dashboard_id']] = [
'icon' => $deviceData['icon'],
'id' => $subDeviceData['subdevice_id'],
'masterId' => $deviceData['device_id'],
'name' => $deviceData['name'],
'type' => $subDeviceData['type'],
'unit' => $subDeviceData['unit'],
'lastRecord' => [
'value' => $parsedValue,
],
];
}
$approvedSubDevices = [];
$allDevicesData = DeviceManager::getAllDevices();
foreach ($allDevicesData as $deviceKey => $deviceValue) {
if (!$deviceValue['approved']) continue;
$allSubDevicesData = SubDeviceManager::getAllSubDevices($deviceValue['device_id']);
foreach ($allSubDevicesData as $subDeviceKey => $subDeviceValue) {
$approvedSubDevices[$subDeviceValue['subdevice_id']] = [
'name' => $allDevicesData[$deviceKey]['name'],
'type' => $subDeviceValue['type'],
];
}
}
if (isset($_POST['deviceId'])){
$deviceData = DeviceManager::getDeviceById($_POST['deviceId']);
$subDevices = [];
$subDevicesData = SubDeviceManager::getAllSubDevices($_POST['deviceId']);
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
$subDevices[$subDeviceData['subdevice_id']] = [
'type' => $subDeviceData['type'],
'unit' => $subDeviceData['unit'],
];
}
$device = [
'id' => $deviceData['device_id'],
'name' => $deviceData['name'],
'token' => $deviceData['token'],
'icon' => $deviceData['icon'],
'subDevices' => $subDevices,
];
$template->prepare('deviceData', $device);
}
$template->prepare('title', 'Nástěnka');
$template->prepare('lang', $lang);
$template->prepare('dashboard', $dashboard);
$template->prepare('subDevices', $approvedSubDevices);
$template->render();
}
}

170
app/views/Home.php Normal file
View File

@@ -0,0 +1,170 @@
<?php
class Home extends Template
{
function __construct()
{
global $userManager;
global $lang;
if (!$userManager->isLogin()){
header('Location: ./login');
}
$template = new Template('home');
//users instantialize
$users = UserManager::getUsers();
$template->prepare('users', $users);
//Users at home Info
$usersAtHome = '';
$i = 0;
foreach ($users as $user) {
$i++;
if ($user['at_home'] == 'true') {
$usersAtHome .= $user['username'];
if ($usersAtHome != "" && isset($users[$i + 1])){
$usersAtHome .= ', ';
}
}
}
$template->prepare('usersAtHome', $usersAtHome);
$roomsItems = [];
$roomsData = RoomManager::getAllRooms();
foreach ($roomsData as $roomKey => $roomsData) {
$devices = [];
$devicesData = DeviceManager::getAllDevicesInRoom($roomsData['room_id']);
foreach ($devicesData as $deviceKey => $deviceData) {
$subDevices = [];
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
$events = RecordManager::getLastRecord($subDeviceData['subdevice_id'], 5);
//TODO: skontrolovat zdali se jedná o poslední (opravdu nejaktuálnější) záznam
$lastRecord = $events[0];
$parsedValue = round($lastRecord['value']);
/*Value Parsing*/
if ($subDeviceData['type'] == "on/off") {
$parsedValue = ($parsedValue == 1 ? 'ON' : 'OFF');
}
if ($subDeviceData['type'] == "door") {
$replacementTrue = 'Closed';
$replacementFalse = 'Opened';
foreach ($events as $key => $value) {
$events[$key]['value'] = ($value['value'] == 1 ? $replacementTrue : $replacementFalse);
}
$parsedValue = ($parsedValue == 1 ? $replacementTrue : $replacementFalse);
}
if ($subDeviceData['type'] == "light") {
$replacementTrue = 'Light';
$replacementFalse = 'Dark';
foreach ($events as $key => $value) {
if ($parsedValue != 1){
//Analog Reading
$events[$key]['value'] = ($value['value'] <= 810 ? $replacementTrue : $replacementFalse);
} else {
//Digital Reading
$events[$key]['value'] = ($value['value'] == 0 ? $replacementTrue : $replacementFalse);
}
}
if ($parsedValue != 1){
//Analog Reading
$parsedValue = ($parsedValue <= 810 ? $replacementTrue : $replacementFalse);
} else {
//Digital Reading
$parsedValue = ($parsedValue == 0 ? $replacementTrue : $replacementFalse);
}
}
$date2 = new DateTime($lastRecord['time']);
$niceTime = $this->ago($date2);
$connectionError = false;
$startDate = date_create($lastRecord['time']);
$interval = $startDate->diff(new DateTime());
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$lastSeen = ($hours * 60 + $minutes);
if ($lastSeen > $deviceData['sleep_time'] && $subDeviceData['type'] != "on/off") {
$connectionError = true;
}
$subDevices[$subDeviceData['subdevice_id']] = [
'events'=> $events,
'type' => $subDeviceData['type'],
'unit' => $subDeviceData['unit'],
'comError' => $connectionError,
'lastRecort' => [
'value' => $parsedValue,
'time' => $lastRecord['time'],
'niceTime' => $niceTime,
],
];
}
$permissionArray = json_decode($deviceData['permission']);
$userIsDeviceAdmin = false;
if($permissionArray[1] == 3) {
$userIsDeviceAdmin = true;
} else if ($permissionArray[0] == 3) {
if ( $deviceData['owner'] == $userManager->getUserData('user_id')) {
$userIsDeviceAdmin = true;
}
}
$devices[$deviceData['device_id']] = [
'name' => $deviceData['name'],
'icon' => $deviceData['icon'],
'room' => $deviceData['room_id'],
'token' => $deviceData['token'],
'sleepTime' => $deviceData['sleep_time'],
'approved' => $deviceData['approved'],
'permission' => $permissionArray,
'owner' => $deviceData['owner'],
'userIsAdmin' => $userIsDeviceAdmin,
'subDevices' => $subDevices,
];
}
$roomsItems[$roomsData['room_id']] = [
'name' => $roomsData['name'],
'deviceCount' => $roomsData['device_count'],
'devices' => $devices,
];
}
$rooms = RoomManager::getAllRooms();
$template->prepare('rooms', $rooms);
$template->prepare('title', 'Home');
$template->prepare('lang', $lang);
$template->prepare('data', $roomsItems);
$template->render();
}
function ago( $datetime )
{
$interval = date_create('now')->diff( $datetime );
$suffix = ( $interval->invert ? ' ago' : '' );
if ( $v = $interval->y >= 1 ) return $this->pluralize( $interval->m, 'month' ) . $suffix;
if ( $v = $interval->d >= 1 ) return $this->pluralize( $interval->d, 'day' ) . $suffix;
if ( $v = $interval->h >= 1 ) return $this->pluralize( $interval->h, 'hour' ) . $suffix;
if ( $v = $interval->i >= 1 ) return $this->pluralize( $interval->i, 'minute' ) . $suffix;
return $this->pluralize( $interval->s, 'second' ) . $suffix;
}
function pluralize( $count, $text )
{
return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}
}

34
app/views/Log.php Normal file
View File

@@ -0,0 +1,34 @@
<?php
class Log extends Template
{
function __construct()
{
global $userManager;
global $lang;
if (!$userManager->isLogin()){
header('Location: ./login');
}
$template = new Template('log');
$template->prepare('title', 'Log');
$result = array();
$cdir = scandir('./app/logs/');
foreach ($cdir as $key => $value)
{
if (!in_array($value,array(".","..")))
{
$result[$value] = $value;
}
}
$template->prepare('logsFiles', $result);
$template->prepare('lang', $lang);
$template->render();
}
}

19
app/views/Login.php Normal file
View File

@@ -0,0 +1,19 @@
<?php
class Login extends Template
{
function __construct()
{
global $userManager;
global $lang;
if ($userManager->isLogin()){
header('Location: ./');
}
$template = new Template('login');
$template->prepare('title', 'Home');
$template->prepare('lang', $lang);
$template->render();
}
}

11
app/views/Logout.php Normal file
View File

@@ -0,0 +1,11 @@
<?php
class Logout extends Template
{
function __construct()
{
global $userManager;
$userManager->logout();
header('Location: /vasek/home/login', TRUE);
die();
}
}

85
app/views/Rooms.php Normal file
View File

@@ -0,0 +1,85 @@
<?php
class Rooms extends Template
{
function __construct()
{
global $userManager;
global $lang;
if (!$userManager->isLogin()){
header('Location: ./login');
}
$template = new Template('rooms');
$template->prepare('title', 'Rooms');
$template->prepare('lang', $lang);
$roomsItems = [];
$roomsData = RoomManager::getAllRooms();
foreach ($roomsData as $roomKey => $roomsData) {
$devicesData = DeviceManager::getAllDevicesInRoom($roomsData['room_id']);
$roomReading = [];
if ($roomsData['device_count'] == 0) {
continue;
}
$roomReadingCache = [];
$roomControlsCache = [];
foreach ($devicesData as $deviceKey => $deviceData) {
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
$subDeviceType = $subDeviceData['type'];
$subDeviceUnit = $subDeviceData['unit'];
$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
if (in_array($subDeviceType, ['on/off','battery','door'])) {
if ($subDeviceType == 'on/off') {
$roomControlsCache[$subDeviceKey] = [
'type' => $subDeviceType,
'name' => $deviceData['name'],
'icon' => $deviceData['icon'],
'value' => $lastRecord['value'],
];
}
continue;
}
if (array_key_exists($subDeviceType, $roomReadingCache)) {
$roomReadingCache[$subDeviceType] = [
"value" => $roomReadingCache[$subDeviceType]['value'] + $lastRecord['value'],
"count" => $roomReadingCache[$subDeviceType]['count'] + 1,
"unit" => $subDeviceUnit,
];
} else {
$roomReadingCache[$subDeviceType] = [
"value" => $lastRecord['value'],
"count" => 1,
"unit" => $subDeviceUnit,
];
}
}
}
// parsing
foreach ($roomReadingCache as $type => $value) {
$roomReading[$type] = $value["value"] / $value["count"];
$roomReading[$type] .= @($value['unit']);
}
$roomsItems[$roomsData['room_id']] = [
'name' => $roomsData['name'],
'reading' => $roomReading,
'controls' => $roomControlsCache,
'deviceCount' => $roomsData['device_count'],
];
}
$template->prepare('rooms', $roomsItems);
$template->render();
}
}

82
app/views/Scene.php Normal file
View File

@@ -0,0 +1,82 @@
<?php
class Scene extends Template
{
function __construct()
{
global $userManager;
global $lang;
if (!$userManager->isLogin()){
header('Location: ./');
}
$template = new Template('scene');
$template->prepare('title', 'Scény');
$template->prepare('lang', $lang);
$scenes = [];
foreach (SceneManager::getAllScenes() as $sceneId => $sceneData) {
$doSomething = [];
foreach (json_decode($sceneData['do_something']) as $subdeviceId => $subDeviceState) {
$subDeviceMasterDeviceData = SubDeviceManager::getSubDeviceMaster($subdeviceId);
$doSomething[$subdeviceId] = [
'name' => $subDeviceMasterDeviceData['name'],
'state' => $subDeviceState,
];
}
$scenes[$sceneData['scene_id']] = [
"name" => $sceneData['name'],
"icon" => $sceneData['icon'],
"doSomething" => $doSomething,
];
}
$template->prepare('scenes', $scenes);
$approvedSubDevices = [];
$allDevicesData = DeviceManager::getAllDevices();
foreach ($allDevicesData as $deviceKey => $deviceValue) {
if (!$deviceValue['approved']) continue;
$allSubDevicesData = SubDeviceManager::getAllSubDevices($deviceValue['device_id']);
foreach ($allSubDevicesData as $subDeviceKey => $subDeviceValue) {
if ($subDeviceValue['type'] != 'on/off') continue;
$approvedSubDevices[$subDeviceValue['subdevice_id']] = [
'name' => $allDevicesData[$deviceKey]['name'],
];
}
}
$template->prepare('subDevices', $approvedSubDevices);
$approvedSubDevices = [];
$allDevicesData = DeviceManager::getAllDevices();
foreach ($allDevicesData as $deviceKey => $deviceValue) {
if (!$deviceValue['approved']) continue;
$allSubDevicesData = SubDeviceManager::getAllSubDevices($deviceValue['device_id']);
foreach ($allSubDevicesData as $subDeviceKey => $subDeviceValue) {
if ($subDeviceValue['type'] != 'on/off') continue;
$approvedSubDevices[$subDeviceValue['subdevice_id']] = [
'name' => $allDevicesData[$deviceKey]['name'],
];
}
}
$template->prepare('subDevices', $approvedSubDevices);
if (isset($_POST['devices'])){
$devices = $_POST['devices'];
$devicesOBJ = [];
foreach ($devices as $deviceId) {
$deviceData = DeviceManager::getDeviceById($deviceId);
$subdeviceData = SubDeviceManager::getSubDeviceByMaster($deviceId, 'on/off');
$devicesOBJ[$deviceId] = [
'name' => $deviceData['name'],
'setableSubDevices' => $subdeviceData['subdevice_id'],
];
}
$template->prepare('setStateFormDevices', $devicesOBJ);
$template->prepare('sceneName', $_POST['sceneName']);
$template->prepare('sceneIcon', $_POST['sceneIcon']);
}
$template->render();
}
}

31
app/views/Setting.php Normal file
View File

@@ -0,0 +1,31 @@
<?php
class Setting extends Template
{
function __construct()
{
global $userManager;
global $lang;
if (!$userManager->isLogin()){
header('Location: ./login');
}
$automations = [];
$automationsData = AutomationManager::getAll();
foreach ($automationsData as $automationKey => $automationData) {
$automations[$automationData['automation_id']] = [
'name' => '',
'onDays' => $automationData['on_days'],
'ifSomething' => $automationData['if_something'],
'doSomething' => $automationData['do_something'],
];
}
$template = new Template('setting');
$template->prepare('title', 'Automation');
$template->prepare('lang', $lang);
$template->prepare('automations', $automations);
$template->render();
}
}