PHP_SMART_HOME_V3/app/models/DeviceManager.php

97 lines
2.7 KiB
PHP
Raw Normal View History

2019-08-23 11:39:42 +00:00
<?php
class DeviceManager{
public static $devices;
2020-04-21 17:44:30 +00:00
static function getAllDevices () {
2019-08-24 11:13:04 +00:00
return Db::loadAll ("SELECT * FROM devices WHERE approved != ?", Array(2));
2019-08-23 11:39:42 +00:00
}
2020-04-21 17:44:30 +00:00
static function getAllDevicesInRoom ($roomId = "") {
2019-08-24 11:13:04 +00:00
return Db::loadAll ("SELECT * FROM devices WHERE room_id = ? AND approved != ?", Array($roomId, 2));
2019-08-23 11:39:42 +00:00
}
2020-04-21 17:44:30 +00:00
static function getOtherDevices(){
2019-08-23 11:39:42 +00:00
return Db::loadAll ("SELECT * FROM devices WHERE room_id IS NULL ");
}
2020-04-21 17:44:30 +00:00
static function getDeviceByToken($deviceToken) {
2019-08-23 11:39:42 +00:00
return Db::loadOne("SELECT * FROM devices WHERE token = ?", array($deviceToken));
}
2020-04-21 17:44:30 +00:00
static function getDeviceByMac($deviceMac) {
2020-03-13 13:45:12 +00:00
return Db::loadOne("SELECT * FROM devices WHERE mac = ?", array($deviceMac));
}
2020-04-21 17:44:30 +00:00
static function getDeviceById($deviceId) {
2019-08-23 11:39:42 +00:00
return Db::loadOne("SELECT * FROM devices WHERE device_id = ?", array($deviceId));
}
2020-04-21 17:44:30 +00:00
public static function create ($name, $token) {
2019-11-27 16:05:49 +00:00
$defaultRoom = RoomManager::getDefaultRoomId();
2019-08-23 11:39:42 +00:00
$device = array (
'name' => $name,
'token' => $token,
2019-11-27 16:05:49 +00:00
'room_id' => $defaultRoom,
2019-08-23 11:39:42 +00:00
);
try {
Db::add ('devices', $device);
return Db::loadOne("SELECT device_id FROM devices WHERE token = ?", array($token))['device_id'];
2019-08-23 11:39:42 +00:00
} catch(PDOException $error) {
echo $error->getMessage();
die();
}
}
2020-04-21 17:44:30 +00:00
public static function edit ($deviceId, $values = []) {
2019-08-23 11:39:42 +00:00
try {
Db::edit ('devices', $values, 'WHERE device_id = ?', array($deviceId));
} catch(PDOException $error) {
echo $error->getMessage();
die();
}
}
2020-04-21 17:44:30 +00:00
public static function editByToken ($token, $values = []) {
2020-03-11 11:22:44 +00:00
try {
2020-03-12 20:34:12 +00:00
Db::edit ('devices', $values, 'WHERE token = ?', array($token));
2020-03-11 11:22:44 +00:00
} catch(PDOException $error) {
echo $error->getMessage();
die();
}
}
2019-08-23 11:39:42 +00:00
/**
* [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]
*/
2020-04-21 17:44:30 +00:00
public static function assignRoom ($roomId, $deviceId) {
2019-08-23 11:39:42 +00:00
$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í]
*/
2020-04-21 17:44:30 +00:00
public static function delete ($deviceId) {
2019-08-23 11:39:42 +00:00
Db::command ('DELETE FROM devices WHERE device_id=?', array ($deviceId));
}
2020-04-21 17:44:30 +00:00
public static function registeret ($deviceToken) {
2019-08-23 11:39:42 +00:00
return (count(Db::loadAll ("SELECT * FROM devices WHERE token=?", array($deviceToken))) == 1 ? true : false);
}
2020-04-21 17:44:30 +00:00
public static function approved ($deviceToken) {
2019-08-23 11:39:42 +00:00
return (count(Db::loadAll ("SELECT * FROM devices WHERE token=? AND approved = ?", array($deviceToken, 1))) == 1 ? true : false);
}
}
?>