2020-05-16 15:18:27 +00:00
|
|
|
<?php
|
|
|
|
class SubDeviceManager
|
|
|
|
{
|
|
|
|
public static $devices;
|
|
|
|
|
|
|
|
public static function getAllSubDevices($deviceId = null)
|
|
|
|
{
|
2020-12-07 19:21:27 +00:00
|
|
|
if ($deviceId == null) {
|
2020-05-16 15:18:27 +00:00
|
|
|
return Db::loadAll("SELECT * FROM subdevices");
|
|
|
|
}
|
|
|
|
return Db::loadAll("SELECT * FROM subdevices WHERE device_id = ?", array($deviceId));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getSubDeviceMaster($subDeviceId)
|
|
|
|
{
|
|
|
|
return Db::loadOne("SELECT * FROM devices WHERE device_id = (SELECT device_id FROM subdevices WHERE subdevice_id = ?)", array($subDeviceId));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getSubDeviceByMaster($deviceId, $subDeviceType = null)
|
|
|
|
{
|
|
|
|
if ($subDeviceType == null) {
|
|
|
|
return Db::loadOne("SELECT * FROM subdevices WHERE device_id = ?;", array($deviceId));
|
|
|
|
} else {
|
|
|
|
return Db::loadOne("SELECT * FROM subdevices WHERE device_id = ? AND type = ?;", array($deviceId, $subDeviceType));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-04 18:06:54 +00:00
|
|
|
public static function getSubDeviceByMasterAndType($deviceId, $subDeviceType = '')
|
2020-05-16 15:18:27 +00:00
|
|
|
{
|
2020-07-04 18:06:54 +00:00
|
|
|
if ($subDeviceType == '') {
|
2020-05-16 15:18:27 +00:00
|
|
|
return Db::loadOne("SELECT * FROM subdevices WHERE device_id = ?;", array($deviceId));
|
|
|
|
} else {
|
|
|
|
return Db::loadOne("SELECT * FROM subdevices WHERE device_id = ? AND type = ?;", array($deviceId, $subDeviceType));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getSubDevice($subDeviceId)
|
|
|
|
{
|
|
|
|
return Db::loadOne("SELECT * FROM subdevices WHERE subdevice_id = ?;", array($subDeviceId));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getSubDevicesTypeForMater($deviceId)
|
|
|
|
{
|
|
|
|
$parsedTypes = [];
|
|
|
|
$types = Db::loadAll("SELECT type FROM subdevices WHERE device_id = ?;", array($deviceId));
|
|
|
|
foreach ($types as $orderNum => $type) {
|
|
|
|
$parsedTypes[$orderNum] = $type['type'];
|
|
|
|
}
|
|
|
|
return $parsedTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
//check if dubdevice exist
|
|
|
|
|
|
|
|
public static function create($deviceId, $type, $unit)
|
|
|
|
{
|
|
|
|
$record = array(
|
|
|
|
'device_id' => $deviceId,
|
|
|
|
'type' => $type,
|
|
|
|
'unit' => $unit,
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
Db::add('subdevices', $record);
|
|
|
|
} catch (PDOException $error) {
|
|
|
|
echo $error->getMessage();
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function remove($subDeviceId)
|
|
|
|
{
|
|
|
|
RecordManager::cleanSubdeviceRecords($subDeviceId);
|
|
|
|
return Db::loadAll("DELETE FROM subdevices WHERE subdevice_id = ?", array($subDeviceId));
|
|
|
|
}
|
|
|
|
|
2020-12-07 19:21:27 +00:00
|
|
|
public static function getSubdevicesByRoomIds($roomIds = NULL)
|
|
|
|
{
|
|
|
|
if (empty($roomIds)) return NULL;
|
2020-05-16 15:18:27 +00:00
|
|
|
|
2020-07-20 10:46:46 +00:00
|
|
|
//TODO: @Patrik Check line 89
|
2020-05-16 15:18:27 +00:00
|
|
|
$rows = Db::loadAll("
|
2020-12-15 08:07:47 +00:00
|
|
|
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
|
2020-05-16 15:18:27 +00:00
|
|
|
JOIN devices d ON sd.device_id = d.device_id
|
|
|
|
JOIN records r ON r.subdevice_id = sd.subdevice_id
|
2020-12-07 19:21:27 +00:00
|
|
|
WHERE d.room_id IN (" . str_repeat("?,", count($roomIds) - 1) . "?)
|
2020-11-18 21:09:02 +00:00
|
|
|
/*AND value != '999'*/
|
2020-05-16 15:18:27 +00:00
|
|
|
AND r.record_id IN (
|
|
|
|
SELECT MAX(record_id)
|
|
|
|
FROM records
|
|
|
|
GROUP BY subdevice_id
|
2020-07-15 17:15:36 +00:00
|
|
|
)
|
2020-07-20 10:46:46 +00:00
|
|
|
GROUP BY subdevice_id
|
2020-10-15 16:55:16 +00:00
|
|
|
ORDER BY d.name DESC
|
2020-05-16 15:18:27 +00:00
|
|
|
", $roomIds);
|
|
|
|
|
|
|
|
$ret = [];
|
2020-12-07 19:21:27 +00:00
|
|
|
foreach ($rows as $row) {
|
2020-05-16 15:18:27 +00:00
|
|
|
$ret[$row['room_id']][] = $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
2020-12-07 19:21:27 +00:00
|
|
|
|
|
|
|
public static function getSubdeviceDetailById($subDeviceId){
|
|
|
|
if (empty($subDeviceId)) return NULL;
|
|
|
|
|
|
|
|
$rows = Db::loadOne("SELECT d.room_id, d.sleep_time, sd.subdevice_id, sd.type, sd.device_id FROM subdevices sd
|
|
|
|
JOIN devices d ON sd.device_id = d.device_id
|
|
|
|
WHERE sd.subdevice_id = ? ", [$subDeviceId]);
|
|
|
|
|
|
|
|
return $rows;
|
|
|
|
|
|
|
|
}
|
2020-05-16 15:18:27 +00:00
|
|
|
}
|