rooms api
This commit is contained in:
parent
02ba4e5d6f
commit
c3d8a211ed
@ -4,33 +4,23 @@ class RoomsApi extends ApiController{
|
|||||||
|
|
||||||
public function default(){
|
public function default(){
|
||||||
//$this->requireAuth();
|
//$this->requireAuth();
|
||||||
$rooms = [];
|
$response = [];
|
||||||
$roomsData = RoomManager::getAllRooms();
|
$roomIds = [];
|
||||||
foreach ($roomsData as $roomKey => $roomData) {
|
$roomsData = RoomManager::getRoomsDefault();
|
||||||
$widgets = [];
|
|
||||||
$devicesData = DeviceManager::getAllDevicesInRoom($roomData['room_id']);
|
foreach ($roomsData as $roomKey => $room) {
|
||||||
foreach ($devicesData as $deviceKey => $deviceData) {
|
$roomIds[] = $room['room_id'];
|
||||||
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceData['device_id']);
|
|
||||||
foreach ($subDevicesData as $subDeviceKey => $subDeviceData) {
|
|
||||||
$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
|
|
||||||
$widgets[] = [
|
|
||||||
'subdevice_id' => $subDeviceData['subdevice_id'],
|
|
||||||
'device_id' => $deviceData['device_id'],
|
|
||||||
'name' => $deviceData['name'],
|
|
||||||
'type' => $subDeviceData['type'],
|
|
||||||
'icon' => $deviceData['icon'],
|
|
||||||
'value' => $lastRecord['value'],
|
|
||||||
'unit' => $subDeviceData['unit'],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$rooms[] = [
|
$subDevicesData = SubDeviceManager::getSubdevicesByRoomIds($roomIds);
|
||||||
|
|
||||||
|
foreach ($roomsData as $roomKey => $roomData) {
|
||||||
|
$response[] = [
|
||||||
'room_id' => $roomData['room_id'],
|
'room_id' => $roomData['room_id'],
|
||||||
'name' => $roomData['name'],
|
'name' => $roomData['name'],
|
||||||
'widgets' => $widgets,
|
'widgets' => isset($subDevicesData[$roomData['room_id']]) ? $subDevicesData[$roomData['room_id']] : [],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
$this->response($rooms);
|
$this->response($response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,11 @@ class RoomManager{
|
|||||||
return $allRoom;
|
return $allRoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function getRoomsDefault () {
|
||||||
|
$allRoom = Db::loadAll ("SELECT room_id, name FROM rooms");
|
||||||
|
return $allRoom;
|
||||||
|
}
|
||||||
|
|
||||||
public static function create ($name) {
|
public static function create ($name) {
|
||||||
$room = array (
|
$room = array (
|
||||||
'name' => $name,
|
'name' => $name,
|
||||||
|
@ -71,4 +71,27 @@ class SubDeviceManager
|
|||||||
RecordManager::cleanSubdeviceRecords($subDeviceId);
|
RecordManager::cleanSubdeviceRecords($subDeviceId);
|
||||||
return Db::loadAll("DELETE FROM subdevices WHERE subdevice_id = ?", array($subDeviceId));
|
return Db::loadAll("DELETE FROM subdevices WHERE subdevice_id = ?", array($subDeviceId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function getSubdevicesByRoomIds($roomIds = NULL) {
|
||||||
|
if(empty($roomIds)) return NULL;
|
||||||
|
|
||||||
|
$rows = Db::loadAll("
|
||||||
|
SELECT d.room_id, sd.subdevice_id, sd.device_id, d.name, sd.type, sd.unit, r.value FROM subdevices sd
|
||||||
|
JOIN devices d ON sd.device_id = d.device_id
|
||||||
|
JOIN records r ON r.subdevice_id = sd.subdevice_id
|
||||||
|
WHERE d.room_id IN (".str_repeat("?,", count($roomIds)-1)."?)
|
||||||
|
AND r.record_id IN (
|
||||||
|
SELECT MAX(record_id)
|
||||||
|
FROM records
|
||||||
|
GROUP BY subdevice_id
|
||||||
|
)
|
||||||
|
", $roomIds);
|
||||||
|
|
||||||
|
$ret = [];
|
||||||
|
foreach($rows as $row){
|
||||||
|
$ret[$row['room_id']][] = $row;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user