api tweeks

This commit is contained in:
GamerClassN7 2020-12-15 09:07:47 +01:00
parent ee0ebff76a
commit 39f44c8d03
3 changed files with 20 additions and 32 deletions

View File

@ -24,53 +24,43 @@ class RoomsApi extends ApiController
$type = $subDevicesData[$subDeviceKey][$key]['type'];
} else if (strpos(SubDeviceManager::getSubDeviceMaster($subDevicesData[$subDeviceKey][$key]['subdevice_id'])['type'], '-') !== false) {
$type = SubDeviceManager::getSubDeviceMaster($subDevicesData[$subDeviceKey][$key]['subdevice_id'])['type'];
} else {
continue;
}
//Connection Error Creation
$connectionError = true;
$LastRecordTime = new DateTime($subDevicesData[$subDeviceKey][$key]['time']);
$interval = $LastRecordTime->diff(new DateTime());
$lastSeen = ($interval->format('%h') * 60 + $interval->format('%i'));
if ($subDevicesData[$subDeviceKey][$key]['sleep_time'] == NULL || $subDevicesData[$subDeviceKey][$key]['sleep_time'] == 0 || $lastSeen < $subDevicesData[$subDeviceKey][$key]['sleep_time']) {
$connectionError = false;
}
$subDevicesData[$subDeviceKey][$key]['connection_error'] = $connectionError;
//Record Translation
$cammelCaseClass = "";
foreach (explode('-', $type) as $word) {
$cammelCaseClass .= ucfirst($word);
}
if (!class_exists($cammelCaseClass)) {
continue;
if (class_exists($cammelCaseClass)) {
$deviceClass = new $cammelCaseClass;
if (method_exists($deviceClass, 'translate')) {
$subDevicesData[$subDeviceKey][$key]['value'] = $deviceClass->translate($subDevicesData[$subDeviceKey][$key]['value']);
}
}
$deviceClass = new $cammelCaseClass;
if (!method_exists($deviceClass, 'translate')) {
continue;
}
$subDevicesData[$subDeviceKey][$key]['value'] = $deviceClass->translate($subDevicesData[$subDeviceKey][$key]['value']);
//Connection Error Creation
$niceTime = Utilities::ago($LastRecordTime);
$interval = $LastRecordTime->diff(new DateTime());
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$lastSeen = ($hours * 60 + $minutes);
if (
$lastSeen < $subDevicesData[$subDeviceKey][$key]['sleep_time'] ||
$subDevicesData[$subDeviceKey][$key]['type'] == "on/off" ||
$subDevicesData[$subDeviceKey][$key]['type'] == "door" ||
$subDevicesData[$subDeviceKey][$key]['type'] == "wather"
) {
$connectionError = false;
}
$subDevicesData[$subDeviceKey][$key]['connection_error'] = $connectionError
}
}
foreach ($roomsData as $roomKey => $roomData) {
if ($roomData['device_count'] == 0) continue;
$subDevicesSorted = isset($subDevicesData[$roomData['room_id']]) ? Utilities::sortArrayByKey($subDevicesData[$roomData['room_id']], 'connection_error', 'asc') : [];
$response[] = [
'room_id' => $roomData['room_id'],
'name' => $roomData['name'],
'widgets' => isset($subDevicesData[$roomData['room_id']]) ? $subDevicesData[$roomData['room_id']] : [],
'widgets' => $subDevicesSorted,
];
}
$this->response($response);
}

View File

@ -44,8 +44,6 @@ class WidgetApi extends ApiController
];
$response = null;
$connectionError = true;
$subDeviceData = SubDeviceManager::getSubdeviceDetailById($subDeviceId);

View File

@ -78,7 +78,7 @@ class SubDeviceManager
//TODO: @Patrik Check line 89
$rows = Db::loadAll("
SELECT d.room_id, d.sleep_time, sd.subdevice_id, sd.device_id, d.icon, d.name, sd.type, sd.unit, r.value FROM subdevices sd
SELECT d.room_id, d.sleep_time, sd.subdevice_id, sd.device_id, d.icon, d.name, sd.type, sd.unit, r.value, r.time FROM subdevices sd
JOIN devices d ON sd.device_id = d.device_id
JOIN records r ON r.subdevice_id = sd.subdevice_id
WHERE d.room_id IN (" . str_repeat("?,", count($roomIds) - 1) . "?)