PHP_SMART_HOME_V3/app/models/managers/SubDeviceManager.php

98 lines
2.7 KiB
PHP
Raw Normal View History

2019-08-23 11:39:42 +00:00
<?php
class SubDeviceManager
{
2019-08-24 11:13:04 +00:00
public static $devices;
2020-04-30 17:40:34 +00:00
public static function getAllSubDevices($deviceId = null)
2019-08-24 11:13:04 +00:00
{
2020-04-30 17:40:34 +00:00
if ($deviceId == null){
return Db::loadAll("SELECT * FROM subdevices");
}
2019-08-24 11:13:04 +00:00
return Db::loadAll("SELECT * FROM subdevices WHERE device_id = ?", array($deviceId));
}
2020-04-21 17:44:30 +00:00
public static function getSubDeviceMaster($subDeviceId)
2019-08-24 11:13:04 +00:00
{
return Db::loadOne("SELECT * FROM devices WHERE device_id = (SELECT device_id FROM subdevices WHERE subdevice_id = ?)", array($subDeviceId));
}
2020-04-21 17:44:30 +00:00
public static function getSubDeviceByMaster($deviceId, $subDeviceType = null)
2019-08-24 11:13:04 +00:00
{
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-04-21 17:44:30 +00:00
public static function getSubDeviceByMasterAndType($deviceId, $subDeviceType = null)
2019-08-24 11:13:04 +00:00
{
if (!empty($subDeviceType)) {
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-04-21 17:44:30 +00:00
public static function getSubDevice($subDeviceId)
2019-08-24 11:13:04 +00:00
{
return Db::loadOne("SELECT * FROM subdevices WHERE subdevice_id = ?;", array($subDeviceId));
}
2020-04-21 17:44:30 +00:00
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;
}
2019-08-24 11:13:04 +00:00
//check if dubdevice exist
2020-04-21 17:44:30 +00:00
public static function create($deviceId, $type, $unit)
2019-08-24 11:13:04 +00:00
{
$record = array(
'device_id' => $deviceId,
'type' => $type,
'unit' => $unit,
);
try {
Db::add('subdevices', $record);
} catch (PDOException $error) {
echo $error->getMessage();
die();
}
}
2020-04-21 17:44:30 +00:00
public static function remove($subDeviceId)
2019-08-24 11:13:04 +00:00
{
RecordManager::cleanSubdeviceRecords($subDeviceId);
return Db::loadAll("DELETE FROM subdevices WHERE subdevice_id = ?", array($subDeviceId));
}
2020-05-15 18:59:12 +00:00
public static function getSubdevicesByRoomIds($roomIds = NULL) {
if(empty($roomIds)) return NULL;
$rows = Db::loadAll("
SELECT d.room_id, sd.subdevice_id, sd.device_id, d.name, sd.type, sd.unit, r.value 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)."?)
AND r.record_id IN (
SELECT MAX(record_id)
FROM records
GROUP BY subdevice_id
)
", $roomIds);
$ret = [];
foreach($rows as $row){
$ret[$row['room_id']][] = $row;
}
return $ret;
}
2019-08-23 11:39:42 +00:00
}