PHP_SMART_HOME_V3/app/models/managers/DeviceManager.php

109 lines
3.0 KiB
PHP
Raw Permalink Normal View History

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
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") {
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 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);
}
}
?>