Home Refactor/Optimalization #1

This commit is contained in:
JonatanRek 2019-10-10 14:38:10 +02:00
parent 2b84afcba2
commit c6c3b5831a
2 changed files with 67 additions and 65 deletions

View File

@ -43,12 +43,12 @@ class Utilities
} }
/** /**
* [generateGraphJson description] * [generateGraphJson description]
* @param string $type [line/bar] * @param string $type [line/bar]
* @param array $data [description] * @param array $data [description]
* @param array $options [description] * @param array $options [description]
* @return [type] [description] * @return [type] [description]
*/ */
function generateGraphJson(string $type = 'line', array $data = [], array $options = []){ function generateGraphJson(string $type = 'line', array $data = [], array $options = []){
$array = [ $array = [
@ -91,4 +91,20 @@ class Utilities
]; ];
return json_encode($array, JSON_PRETTY_PRINT); return json_encode($array, JSON_PRETTY_PRINT);
} }
function ago( $datetime )
{
$interval = date_create('now')->diff( $datetime );
$suffix = ( $interval->invert ? ' ago' : '' );
if ( $v = $interval->y >= 1 ) return self::pluralize( $interval->m, 'month' ) . $suffix;
if ( $v = $interval->d >= 1 ) return self::pluralize( $interval->d, 'day' ) . $suffix;
if ( $v = $interval->h >= 1 ) return self::pluralize( $interval->h, 'hour' ) . $suffix;
if ( $v = $interval->i >= 1 ) return self::pluralize( $interval->i, 'minute' ) . $suffix;
return self::pluralize( $interval->s, 'second' ) . $suffix;
}
function pluralize( $count, $text )
{
return $count . ( ( $count == 1 ) ? ( " $text" ) : ( " ${text}s" ) );
}
} }

View File

@ -32,6 +32,7 @@ class Home extends Template
} }
$template->prepare('usersAtHome', $usersAtHome); $template->prepare('usersAtHome', $usersAtHome);
$roomsItems = []; $roomsItems = [];
$roomsData = RoomManager::getAllRooms(); $roomsData = RoomManager::getAllRooms();
foreach ($roomsData as $roomKey => $roomsData) { foreach ($roomsData as $roomKey => $roomsData) {
@ -44,69 +45,71 @@ class Home extends Template
$events = RecordManager::getLastRecord($subDeviceData['subdevice_id'], 5); $events = RecordManager::getLastRecord($subDeviceData['subdevice_id'], 5);
$connectionError = false; $connectionError = true;
$parsedValue = ""; $parsedValue = "";
$niceTime = ""; $niceTime = "";
if (sizeof($events) > 1) { if (sizeof($events) > 1) {
//TODO: skontrolovat zdali se jedná o poslední (opravdu nejaktuálnější) záznam
$lastRecord = $events[0]; $lastRecord = $events[0];
$lastValue = round($lastRecord['value']);
$parsedValue = round($lastRecord['value']); $parsedValue = $lastValue;
/*Value Parsing*/ /*Value Parsing*/
if ($subDeviceData['type'] == "on/off") { //Last Value Parsing
$parsedValue = ($parsedValue == 1 ? 'ON' : 'OFF'); switch ($subDeviceData['type']) {
} case 'on/off':
$replacementTrue = 'On';
$replacementFalse = 'Off';
break;
if ($subDeviceData['type'] == "door") { case 'door':
$replacementTrue = 'Closed'; $replacementTrue = 'Closed';
$replacementFalse = 'Opened'; $replacementFalse = 'Open';
foreach ($events as $key => $value) { break;
$events[$key]['value'] = ($value['value'] == 1 ? $replacementTrue : $replacementFalse);
}
$parsedValue = ($parsedValue == 1 ? $replacementTrue : $replacementFalse);
}
if ($subDeviceData['type'] == "light") { case 'light':
$replacementTrue = 'Light'; $replacementTrue = 'Light';
$replacementFalse = 'Dark'; $replacementFalse = 'Dark';
foreach ($events as $key => $value) { break;
if ($parsedValue != 1){
//Analog Reading case 'water':
$events[$key]['value'] = ($value['value'] <= 810 ? $replacementTrue : $replacementFalse); $replacementTrue = 'Wet';
} else { $replacementFalse = 'Dry';
//Digital Reading break;
$events[$key]['value'] = ($value['value'] == 0 ? $replacementTrue : $replacementFalse);
} default:
$replacementTrue = '';
$replacementFalse = '';
break;
}
if ($replacementTrue != '' && $replacementFalse != '') {
//parsing last values
$parsedValue = $replacementFalse;
if ($lastValue == 1) {
$parsedValue = $replacementTrue;
} }
if ($parsedValue != 1){
//Analog Reading //parsing last events values
$parsedValue = ($parsedValue <= 810 ? $replacementTrue : $replacementFalse); foreach ($events as $key => $value) {
} else { $events[$key]['value'] = $replacementFalse;
//Digital Reading if ($value == 1) {
$parsedValue = ($parsedValue == 0 ? $replacementTrue : $replacementFalse); $events[$key]['value'] = $replacementTrue;
}
} }
} }
$date2 = new DateTime($lastRecord['time']); $LastRecordTime = new DateTime($lastRecord['time']);
$niceTime = Utilities::ago($LastRecordTime);
$niceTime = $this->ago($date2); $interval = $LastRecordTime->diff(new DateTime());
$startDate = date_create($lastRecord['time']);
$interval = $startDate->diff(new DateTime());
$hours = $interval->format('%h'); $hours = $interval->format('%h');
$minutes = $interval->format('%i'); $minutes = $interval->format('%i');
$lastSeen = ($hours * 60 + $minutes); $lastSeen = ($hours * 60 + $minutes);
if ($lastSeen > $deviceData['sleep_time'] && $subDeviceData['type'] != "on/off") { if ($lastSeen < $deviceData['sleep_time'] || $subDeviceData['type'] == "on/off") {
$connectionError = true; $connectionError = false;
} }
} else {
$connectionError = true;
} }
$subDevices[$subDeviceData['subdevice_id']] = [ $subDevices[$subDeviceData['subdevice_id']] = [
@ -123,12 +126,11 @@ class Home extends Template
} }
$permissionArray = json_decode($deviceData['permission']); $permissionArray = json_decode($deviceData['permission']);
$userIsDeviceAdmin = false; $userIsDeviceAdmin = false;
if($permissionArray[1] == 3) { if($permissionArray[1] == 3) {
$userIsDeviceAdmin = true; $userIsDeviceAdmin = true;
} else if ($permissionArray[0] == 3) { } else if ($permissionArray[0] == 3) {
if ( $deviceData['owner'] == $userManager->getUserData('user_id')) { if ( $deviceData['owner'] == $_SESSION['user']['id']) {
$userIsDeviceAdmin = true; $userIsDeviceAdmin = true;
} }
} }
@ -162,20 +164,4 @@ class Home extends Template
$template->render(); $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" ) );
}
} }