2020-05-16 15:18:27 +00:00
|
|
|
<?php
|
|
|
|
class DeviceManager{
|
|
|
|
public static $devices;
|
|
|
|
|
|
|
|
static function getAllDevices () {
|
2020-11-18 20:40:13 +00:00
|
|
|
return Db::loadAll ("SELECT * FROM devices
|
2020-09-15 14:38:11 +00:00
|
|
|
WHERE approved != ?", Array(2));
|
2020-05-16 15:18:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 20:40:13 +00:00
|
|
|
|
|
|
|
|
2020-05-16 15:18:27 +00:00
|
|
|
static function getAllDevicesInRoom ($roomId = "") {
|
|
|
|
return Db::loadAll ("SELECT * FROM devices WHERE room_id = ? AND approved != ?", Array($roomId, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
static function getOtherDevices(){
|
|
|
|
return Db::loadAll ("SELECT * FROM devices WHERE room_id IS NULL ");
|
|
|
|
}
|
|
|
|
|
|
|
|
static function getDeviceByToken($deviceToken) {
|
|
|
|
return Db::loadOne("SELECT * FROM devices WHERE token = ?", array($deviceToken));
|
|
|
|
}
|
|
|
|
|
|
|
|
static function getDeviceByMac($deviceMac) {
|
|
|
|
return Db::loadOne("SELECT * FROM devices WHERE mac = ?", array($deviceMac));
|
|
|
|
}
|
|
|
|
|
|
|
|
static function getDeviceById($deviceId) {
|
|
|
|
return Db::loadOne("SELECT * FROM devices WHERE device_id = ?", array($deviceId));
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:24:10 +00:00
|
|
|
static function getAllDevicesSorted ($sort, $sortType = "ASC") {
|
2020-09-15 14:38:11 +00:00
|
|
|
return Db::loadAll ("SELECT devices.* FROM devices
|
|
|
|
LEFT JOIN rooms ON (devices.room_id = rooms.room_id)
|
|
|
|
WHERE devices.approved != ? ORDER BY $sort $sortType", Array(2));
|
2020-09-09 18:24:10 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:59:45 +00:00
|
|
|
public static function create ($name, $token, $type = "") {
|
2020-05-16 15:18:27 +00:00
|
|
|
$defaultRoom = RoomManager::getDefaultRoomId();
|
|
|
|
$device = array (
|
|
|
|
'name' => $name,
|
|
|
|
'token' => $token,
|
|
|
|
'room_id' => $defaultRoom,
|
|
|
|
);
|
2020-10-22 15:59:45 +00:00
|
|
|
if (!empty($type)) {
|
|
|
|
$device['type'] = $type;
|
|
|
|
}
|
2020-05-16 15:18:27 +00:00
|
|
|
try {
|
|
|
|
Db::add ('devices', $device);
|
|
|
|
return Db::loadOne("SELECT device_id FROM devices WHERE token = ?", array($token))['device_id'];
|
|
|
|
} catch(PDOException $error) {
|
|
|
|
echo $error->getMessage();
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function edit ($deviceId, $values = []) {
|
|
|
|
try {
|
|
|
|
Db::edit ('devices', $values, 'WHERE device_id = ?', array($deviceId));
|
|
|
|
} catch(PDOException $error) {
|
|
|
|
echo $error->getMessage();
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function editByToken ($token, $values = []) {
|
|
|
|
try {
|
|
|
|
Db::edit ('devices', $values, 'WHERE token = ?', array($token));
|
|
|
|
} catch(PDOException $error) {
|
|
|
|
echo $error->getMessage();
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [assignRoom Přiřazení zařízení do třídy]
|
|
|
|
* @param [type] $roomId [číslo místnosti do kter se má zařízení přiřadit]
|
|
|
|
* @param [type] $deviceId [Číslo zařízení které chcete přiřadit do místnosti]
|
|
|
|
*/
|
|
|
|
public static function assignRoom ($roomId, $deviceId) {
|
|
|
|
$device = array (
|
|
|
|
'room_id' => $roomId,
|
|
|
|
);
|
|
|
|
try {
|
|
|
|
Db::edit ('devices', $device, 'WHERE device_id = ?', array($deviceId));
|
|
|
|
} catch(PDOException $error) {
|
|
|
|
echo $error->getMessage();
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [delete Smazání zařízení]
|
|
|
|
* @param [type] $deviceId [Id zařízení ke smazání]
|
|
|
|
*/
|
|
|
|
public static function delete ($deviceId) {
|
|
|
|
Db::command ('DELETE FROM devices WHERE device_id=?', array ($deviceId));
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function registeret ($deviceToken) {
|
|
|
|
return (count(Db::loadAll ("SELECT * FROM devices WHERE token=?", array($deviceToken))) == 1 ? true : false);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function approved ($deviceToken) {
|
|
|
|
return (count(Db::loadAll ("SELECT * FROM devices WHERE token=? AND approved = ?", array($deviceToken, 1))) == 1 ? true : false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
?>
|