PHP_SMART_HOME_V3/app/api/WidgetApi.php

65 lines
1.6 KiB
PHP
Raw Normal View History

2020-05-24 17:35:19 +00:00
<?php
class WidgetApi extends ApiController{
2020-05-24 17:37:29 +00:00
public function run($subDeviceId){
2020-05-24 17:35:19 +00:00
//$this->requireAuth();
$response = null;
$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
if ($subDeviceData['type'] == 'on/off'){
$lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value'];
RecordManager::create($subDeviceData['device_id'], 'on/off', !$lastValue);
$response = !$lastValue;
}
2020-05-26 19:01:03 +00:00
$this->response(['value' => $response]);
2020-05-24 17:35:19 +00:00
}
2020-05-26 19:42:39 +00:00
public function check($subDeviceId){
//$this->requireAuth();
$response = null;
$lastRecord = RecordManager::getLastRecord($subDeviceId);
$response = [
'executet' => $lastRecord['execuded'],
'value' => $lastRecord['value'],
];
$this->response($response);
}
2020-05-26 19:47:36 +00:00
public function detail($subDeviceId){
//$this->requireAuth();
$response = null;
2020-05-27 19:37:35 +00:00
$connectionError = true;
$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
$deviceData = DeviceManager::getDeviceById($deviceId);
$events = RecordManager::getLastRecord($subDeviceId, 5);
$LastRecordTime = new DateTime($$events[4]['time']);
$niceTime = Utilities::ago($LastRecordTime);
$interval = $LastRecordTime->diff(new DateTime());
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$lastSeen = ($hours * 60 + $minutes);
if (
$lastSeen < $deviceData['sleep_time'] ||
$subDeviceData['type'] == "on/off" ||
$subDeviceData['type'] == "door"
) {
$connectionError = false;
}
$response = [
'records'=> $events,
'comError' => $connectionError,
'lastConnectionTime' => (empty($niceTime) ? "00:00" : $niceTime),
];
2020-05-26 19:47:36 +00:00
$this->response($response);
}
2020-05-24 17:35:19 +00:00
}