2019-08-23 11:39:42 +00:00
|
|
|
<?php
|
|
|
|
class SubDeviceManager
|
|
|
|
{
|
2019-08-24 11:13:04 +00:00
|
|
|
public static $devices;
|
|
|
|
|
2020-04-21 17:44:30 +00:00
|
|
|
public static function getAllSubDevices($deviceId)
|
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)
|
2019-10-09 08:56:46 +00:00
|
|
|
{
|
|
|
|
$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
|
|
|
}
|