PHP_SMART_HOME_V3/app/models/managers/SubDeviceManager.php

75 lines
2.1 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));
}
2019-08-23 11:39:42 +00:00
}