PHP_SMART_HOME_V3/app/class/DeviceManager.php

97 lines
2.6 KiB
PHP
Raw Permalink Normal View History

2019-08-23 11:39:42 +00:00
<?php
class DeviceManager{
public static $devices;
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
}
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
}
function getOtherDevices(){
return Db::loadAll ("SELECT * FROM devices WHERE room_id IS NULL ");
}
function getDeviceByToken($deviceToken) {
return Db::loadOne("SELECT * FROM devices WHERE token = ?", array($deviceToken));
}
2020-03-13 13:45:12 +00:00
function getDeviceByMac($deviceMac) {
return Db::loadOne("SELECT * FROM devices WHERE mac = ?", array($deviceMac));
}
2019-08-23 11:39:42 +00:00
function getDeviceById($deviceId) {
return Db::loadOne("SELECT * FROM devices WHERE device_id = ?", array($deviceId));
}
public 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();
}
}
public function edit ($deviceId, $values = []) {
try {
Db::edit ('devices', $values, 'WHERE device_id = ?', array($deviceId));
} catch(PDOException $error) {
echo $error->getMessage();
die();
}
}
2020-03-11 11:22:44 +00:00
public function editByToken ($token, $values = []) {
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]
*/
public 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 function delete ($deviceId) {
Db::command ('DELETE FROM devices WHERE device_id=?', array ($deviceId));
}
public function registeret ($deviceToken) {
return (count(Db::loadAll ("SELECT * FROM devices WHERE token=?", array($deviceToken))) == 1 ? true : false);
}
public function approved ($deviceToken) {
return (count(Db::loadAll ("SELECT * FROM devices WHERE token=? AND approved = ?", array($deviceToken, 1))) == 1 ? true : false);
}
}
?>