PHP_SMART_HOME_V3/app/views/Device.php

121 lines
3.4 KiB
PHP
Raw Permalink Normal View History

2020-08-31 19:23:23 +00:00
<?php
class Device extends Template
{
2020-11-18 20:40:13 +00:00
function __construct($sortBy = null, $sortType = null)
{
2021-01-05 20:35:12 +00:00
// //Notification data setup
// $notificationMng = new NotificationManager;
// $notificationData = [
// 'title' => 'Info',
// 'body' => 'New device Detected Found',
2021-01-06 18:23:12 +00:00
// 'icon' => 'http://dev.steelants.cz/projekty/simplehome-client/img/icons/favicon-16x16.png',
2021-01-05 20:35:12 +00:00
// ];
// //Notification for newly added Device
// if ($notificationData != []) {
// $subscribers = $notificationMng::getSubscription();
// foreach ($subscribers as $key => $subscriber) {
// $notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
// }
// }
2020-11-18 20:40:13 +00:00
$userManager = new UserManager();
$deviceManager = new DeviceManager();
$subDeviceManager = new SubDeviceManager();
$recordManager = new RecordManager();
$roomManager = new RoomManager();
$langMng = new LanguageManager('en');
if (!$userManager->isLogin()) {
2020-10-25 23:01:30 +00:00
header('Location: ' . BASEURL . 'login');
2020-08-31 19:23:23 +00:00
}
2020-11-18 20:40:13 +00:00
$template = new Template('device');
$template->prepare('title', $langMng->get("m_devices"));
2020-08-31 19:23:23 +00:00
2020-11-18 20:40:13 +00:00
$sortWordBook = [
"id" => "device_id",
"name" => "name",
"room" => "room_id",
"ip" => "ip_address",
"mac" => "mac",
"token" => "token",
"signal" => "signal",
"firmware" => "firmware_hash",
"icon" => "icon",
"history" => "history",
2020-11-18 20:40:13 +00:00
];
2020-09-09 18:24:10 +00:00
2020-11-18 20:40:13 +00:00
$sortIcons = [
"ASC" => "&#xf0de",
"DESC" => "&#xf0dd",
];
2020-09-09 18:24:10 +00:00
2020-11-18 20:40:13 +00:00
$nextSort = [
"ASC" => "DESC",
"DESC" => "ASC",
];
2020-09-09 18:24:10 +00:00
2020-11-18 20:40:13 +00:00
$devices = $deviceManager->getAllDevices();
if (empty($sortBy) && empty($sortType)) {
$sortBy = "id";
$sortType = "DESC";
2020-09-09 18:24:10 +00:00
}
2020-11-18 20:40:13 +00:00
$template->prepare('sortIcon', [$sortBy => $sortIcons[$sortType]]);
2020-08-31 19:23:23 +00:00
foreach ($devices as $key => $device) {
2020-11-18 20:40:13 +00:00
//Signal Stenght
$subdevice = $subDeviceManager->getSubDeviceByMasterAndType($device['device_id'], "wifi");
$subdeviceLocal = $subDeviceManager->getSubDeviceByMaster($device['device_id']);
2021-01-05 20:35:12 +00:00
if (!empty($subdeviceLocal)) {
$devices[$key]['history'] = (!empty($subdeviceLocal['history']) ? $subdeviceLocal['history'] : 0);
} else {
2021-01-05 12:01:06 +00:00
$devices[$key]['history'] = "null";
}
2020-11-18 20:40:13 +00:00
$devices[$key]['signal'] = "";
if (!empty($subdevice['subdevice_id'])) {
$record = $recordManager->getLastRecord($subdevice['subdevice_id']);
2020-11-18 20:40:13 +00:00
if (!empty($record)) {
$devices[$key]['signal'] = $record['value'] . " " . $subdevice['unit'];
}
}
2020-11-18 20:40:13 +00:00
//Firmware Status
2020-11-18 20:57:58 +00:00
if (empty($devices[$key]['mac'])) {
$devices[$key]['firmware_hash'] = "";
continue;
}
2020-11-18 20:40:13 +00:00
$localBinary = "../updater/" . str_replace(':', '', $device['mac']) . ".bin";
$devices[$key]['firmware_hash'] = "";
if (file_exists($localBinary)) {
$hash = md5_file($localBinary);
2020-08-31 19:23:23 +00:00
if ($hash == $device['firmware_hash']) {
$devices[$key]['firmware_hash'] = "true";
} else {
$devices[$key]['firmware_hash'] = "need";
}
} else {
$devices[$key]['firmware_hash'] = "false";
}
2020-09-09 18:24:10 +00:00
}
2020-11-18 20:40:13 +00:00
$devices = Utilities::sortArrayByKey($devices, $sortWordBook[$sortBy], strtolower($sortType));
2020-08-31 19:23:23 +00:00
2020-09-07 09:33:24 +00:00
$rooms = $roomManager->getAllRooms();
2020-11-18 20:40:13 +00:00
$template->prepare('baseUrl', BASEURL);
$template->prepare('baseDir', BASEDIR);
$template->prepare('debugMod', DEBUGMOD);
$template->prepare('logToLiveTime', LOGTIMOUT);
$template->prepare('rooms', $rooms);
$template->prepare('sortType', $nextSort[$sortType]);
$template->prepare('devices', $devices);
$template->prepare('langMng', $langMng);
2020-08-31 19:23:23 +00:00
2020-11-18 20:40:13 +00:00
$template->render();
2020-08-31 19:23:23 +00:00
}
}