Compare commits
13 Commits
3.0
...
adfb564ccc
Author | SHA1 | Date | |
---|---|---|---|
|
adfb564ccc | ||
|
a3eaf16fd6 | ||
8b32ecc619 | |||
7ea4a90980 | |||
38b29a2a4a | |||
821c0926e8 | |||
3d3b073131 | |||
|
485709d61f | ||
|
1d44bd62e2 | ||
|
75dab33c9a | ||
2410441b88 | |||
|
1f49391b36 | ||
|
c17e3090a8 |
11
README.md
11
README.md
@@ -136,6 +136,17 @@ Distributed under the MIT License. See `LICENSE` for more information.
|
||||
Project Link: [https://github.com/GamerClassN7/Smart_Home/](https://github.com/GamerClassN7/Smart_Home/) <br> <br>
|
||||
<sup><sub>Tags (ignore): ESP32 ESP12 ESP08 ESP01 ESP Smart Home Automation System PWA PHP HTML JS DHT11 DHT22
|
||||
|
||||
## Code Statistics
|
||||
Type | Files | Lines
|
||||
-----|-------|-----------
|
||||
php | 2 | 2298 (89%)
|
||||
md | 1 | 171 (7%)
|
||||
js | 1 | 64 (2%)
|
||||
sh | 1 | 34 (1%)
|
||||
json | 1 | 10 (0.4%)
|
||||
-----|-------|-----------
|
||||
All | 6 | 2577
|
||||
|
||||
## Folder structure
|
||||
```
|
||||
- /app # app specific files
|
||||
|
@@ -24,37 +24,54 @@ $router->any('/plugins', 'Plugins');
|
||||
$router->any('/ajax', 'Ajax');
|
||||
$router->any('/oauth', 'Oauth');
|
||||
|
||||
//Vue APP
|
||||
//Vue APP - Authentication
|
||||
$router->post('/api/login', 'AuthApi@login');
|
||||
$router->post('/api/logout', 'AuthApi@logout');
|
||||
|
||||
//Vue APP - Rooms
|
||||
$router->get('/api/rooms', 'RoomsApi@default');
|
||||
$router->get('/api/rooms/{roomId}/update', 'RoomsApi@update');
|
||||
|
||||
$router->get('/api/devices', 'DevicesApi@default');
|
||||
|
||||
$router->get('/api/plugins', 'PluginsApi@default');
|
||||
|
||||
$router->get('/api/users', 'UsersApi@default');
|
||||
|
||||
//Vue APP - Server Info
|
||||
$router->get('/api/server', 'ServerApi@default');
|
||||
$router->get('/api/server/log', 'ServerApi@logStatus');
|
||||
|
||||
//Vue APP - Widgets
|
||||
$router->post('/api/widgets/{widgetId}/run', 'WidgetApi@run');
|
||||
$router->get('/api/widgets/{widgetId}/detail', 'WidgetApi@detail');
|
||||
$router->get('/api/widgets/{widgetId}/detail/{period}', 'WidgetApi@detail');
|
||||
|
||||
//Vue APP - Automations Endpoints
|
||||
$router->get('/api/automations', 'AutomationsApi@default');
|
||||
$router->get('/api/automations/{automationId}/detail', 'AutomationsApi@detail');
|
||||
$router->post('/api/automations/create', 'AutomationsApi@create');
|
||||
|
||||
|
||||
|
||||
//cron
|
||||
$router->post('/cron/clean', 'CronApi@clean');
|
||||
$router->post('/cron/fetch', 'CronApi@fetch');
|
||||
$router->post('/cron/automations', 'CronApi@automations');
|
||||
|
||||
|
||||
//Google Home - API
|
||||
$router->any('/api/HA/auth', 'Oauth');
|
||||
$router->any('/api/HA', 'GoogleHomeApi@response');
|
||||
|
||||
|
||||
//Endpoints API
|
||||
$router->post('/api/endpoint/', 'EndpointsApi@default');
|
||||
$router->any('/api/update/', 'UpdatesApi@default');
|
||||
$router->any('/api/users/status', 'UsersApi@status');
|
||||
$router->any('/api/users/subscribe', 'UsersApi@subscribe');
|
||||
|
||||
|
||||
// examples
|
||||
$router->any('/api/example', 'ExampleApi@example');
|
||||
$router->any('/example', 'ExampleController@index');
|
||||
|
34
app/api/AutomationsApi.php
Normal file
34
app/api/AutomationsApi.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
class AutomationsApi extends ApiController
|
||||
{
|
||||
|
||||
public function default()
|
||||
{
|
||||
//$this->requireAuth();
|
||||
$response = [];
|
||||
$response = AutomationManager::getAll(["automation_id","name","enabled"]);
|
||||
|
||||
$this->response($response);
|
||||
}
|
||||
|
||||
public function detail($automationId)
|
||||
{
|
||||
//$this->requireAuth();
|
||||
$response = [];
|
||||
$response = AutomationManager::getById($automationId, ["automation_id", "last_execution_time", "owner_id", "conditions", "tasks"]);
|
||||
|
||||
$this->response($response);
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$this->requireAuth();
|
||||
$obj = $this->input;
|
||||
|
||||
$response = [];
|
||||
$response = AutomationManager::create($obj['name'],$obj['days'], $obj['tasks'], $obj['conditions']);
|
||||
|
||||
$this->response(['value'=>'OK']);
|
||||
}
|
||||
}
|
@@ -20,8 +20,7 @@ class CronApi extends ApiController
|
||||
$this->response(['Value' => 'OK']);
|
||||
}
|
||||
|
||||
public function fetch()
|
||||
{
|
||||
public function fetch(){
|
||||
//Run Plugins
|
||||
$result = [];
|
||||
$dir = $_SERVER['DOCUMENT_ROOT'] . BASEDIR . 'app/plugins/';
|
||||
@@ -60,4 +59,9 @@ class CronApi extends ApiController
|
||||
//Print Result
|
||||
$this->response($result);
|
||||
}
|
||||
|
||||
public function automations(){
|
||||
AutomationManager:executeAll();
|
||||
$this->response(['Value' => 'OK']);
|
||||
}
|
||||
}
|
||||
|
@@ -78,7 +78,9 @@ class EndpointsApi extends ApiController{
|
||||
foreach ($obj['logs'] as $log) {
|
||||
$deviceLogManager = new LogManager('../logs/devices/'. date("Y-m-d").'.log');
|
||||
$deviceLogManager->setLevel(LOGLEVEL);
|
||||
$deviceLogManager->write("[Device Log Msg] Device_ID " . $device['device_id'] . "->" . $log, LogRecordTypes::ERROR);
|
||||
if ($log != 'HTTP_UPDATE_FAILD code-102 messageFile Not Found (404)'){
|
||||
$deviceLogManager->write("[Device Log Msg] Device_ID " . $device['device_id'] . "->" . $log, LogRecordTypes::ERROR);
|
||||
}
|
||||
unset($deviceLogManager);
|
||||
}
|
||||
$this->response([
|
||||
@@ -113,7 +115,7 @@ class EndpointsApi extends ApiController{
|
||||
}
|
||||
|
||||
$subDeviceLastReordValue[$key] = $value['value'];
|
||||
RecordManager::create($device['device_id'], $key, round($value['value'],3));
|
||||
RecordManager::create($device['device_id'], $key, round($value['value'],3), 'device');
|
||||
$logManager->write("[API] Device_ID " . $device['device_id'] . " writed value " . $key . ' ' . $value['value'], LogRecordTypes::INFO);
|
||||
|
||||
//notification
|
||||
|
@@ -54,6 +54,7 @@ class RoomsApi extends ApiController
|
||||
foreach ($roomsData as $roomKey => $roomData) {
|
||||
if ($roomData['device_count'] == 0) continue;
|
||||
$subDevicesSorted = isset($subDevicesData[$roomData['room_id']]) ? Utilities::sortArrayByKey($subDevicesData[$roomData['room_id']], 'connection_error', 'asc') : [];
|
||||
if (count($subDevicesSorted) == 0) continue;
|
||||
$response[] = [
|
||||
'room_id' => $roomData['room_id'],
|
||||
'name' => $roomData['name'],
|
||||
|
@@ -14,7 +14,7 @@ class WidgetApi extends ApiController
|
||||
$subDeviceData = SubDeviceManager::getSubDevice($subDeviceId);
|
||||
if ($subDeviceData['type'] == 'on/off') {
|
||||
$lastValue = RecordManager::getLastRecord($subDeviceData['subdevice_id'])['value'];
|
||||
RecordManager::create($subDeviceData['device_id'], 'on/off', (int) !$lastValue);
|
||||
RecordManager::create($subDeviceData['device_id'], 'on/off', (int) !$lastValue, "vue-app");
|
||||
$response = !$lastValue;
|
||||
} else {
|
||||
throw new Exception("Bad Request", 403);
|
||||
@@ -46,7 +46,7 @@ class WidgetApi extends ApiController
|
||||
$response = null;
|
||||
$subDeviceData = SubDeviceManager::getSubdeviceDetailById($subDeviceId);
|
||||
|
||||
|
||||
|
||||
//TODO: zeptat se @Patrik Je Graf Dobře Seřazený na DESC ?
|
||||
$events = RecordManager::getAllRecordForGraph($subDeviceId, $period, $groupBy[$period]);
|
||||
if ( count($events) == 0){
|
||||
@@ -114,7 +114,7 @@ class WidgetApi extends ApiController
|
||||
|
||||
];
|
||||
|
||||
//TODO: Make Cleaner
|
||||
//TODO: Make Cleaner
|
||||
if (isset(RANGES[$subDeviceData['type']])){
|
||||
$response['graph']['options']['scales']['yAxes'] = [[
|
||||
'ticks' => [
|
||||
@@ -124,7 +124,7 @@ class WidgetApi extends ApiController
|
||||
]
|
||||
]];
|
||||
}
|
||||
|
||||
|
||||
$this->response($response);
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
class GoogleHome {
|
||||
static function sync($requestId){
|
||||
class GoogleHome
|
||||
{
|
||||
static function sync($requestId)
|
||||
{
|
||||
$devices = [];
|
||||
$roomsData = RoomManager::getAllRooms();
|
||||
foreach ($roomsData as $roomKey => $roomData) {
|
||||
@@ -26,7 +28,7 @@ class GoogleHome {
|
||||
}
|
||||
}
|
||||
|
||||
if ($traids < 1){
|
||||
if ($traids < 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -53,428 +55,444 @@ class GoogleHome {
|
||||
$response = [
|
||||
'requestId' => $requestId,
|
||||
'payload' => [
|
||||
'agentUserId'=>'651351531531',
|
||||
'devices' => array_values( $devices ),
|
||||
'agentUserId' => '651351531531',
|
||||
'devices' => array_values($devices),
|
||||
],
|
||||
];
|
||||
|
||||
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
|
||||
$apiLogManager = new LogManager('../logs/google-home/' . date("Y-m-d") . '.log');
|
||||
$apiLogManager->setLevel(LOGLEVEL);
|
||||
$apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
|
||||
echo json_encode($response);
|
||||
}
|
||||
|
||||
static function query($requestId, $payload){
|
||||
static function query($requestId, $payload)
|
||||
{
|
||||
$devices = [];
|
||||
$num = 0;
|
||||
foreach ($payload['devices'] as $deviceId) {
|
||||
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceId['id']);
|
||||
|
||||
$tempDevice[$deviceId['id']] = [
|
||||
'online' => false,
|
||||
'status' => 'OFFLINE',
|
||||
];
|
||||
foreach ($subDevicesData as $key => $subDeviceData) {
|
||||
$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
|
||||
if ($lastRecord['execuded'] == 1){
|
||||
$tempDevice[$deviceId['id']]['online'] = true;
|
||||
$tempDevice[$deviceId['id']]['status'] = "SUCCESS";
|
||||
} else {
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($deviceId['id'], 6) as $key => $value) {
|
||||
if ($value['execuded'] == 1){
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
|
||||
if ($subDevicesData = SubDeviceManager::getAllSubDevices($deviceId['id'])) {
|
||||
foreach ($subDevicesData as $key => $subDeviceData) {
|
||||
$lastRecord = RecordManager::getLastRecord($subDeviceData['subdevice_id']);
|
||||
if ($lastRecord != false && $lastRecord['execuded'] == 1) {
|
||||
$tempDevice[$deviceId['id']]['online'] = true;
|
||||
$tempDevice[$deviceId['id']]['status'] = "SUCCESS";
|
||||
} else {
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($deviceId['id'], 6) as $key => $value) {
|
||||
if ($value['execuded'] == 1) {
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
}
|
||||
}
|
||||
if ($waiting < $executed) {
|
||||
$tempDevice[$deviceId['id']]['online'] = true;
|
||||
}
|
||||
}
|
||||
if ($waiting < $executed){
|
||||
$tempDevice[$deviceId['id']]['online'] = true;
|
||||
|
||||
switch ($subDeviceData['type']) {
|
||||
case 'temp_cont':
|
||||
$tempDevice[$deviceId['id']]['thermostatMode'] = 'off';
|
||||
if ($lastRecord['value'] != 0) {
|
||||
$tempDevice[$deviceId['id']]['thermostatMode'] = 'heat';
|
||||
}
|
||||
$tempDevice[$deviceId['id']]['thermostatTemperatureAmbient'] = $lastRecord['value'];
|
||||
$tempDevice[$deviceId['id']]['thermostatTemperatureSetpoint'] = $lastRecord['value'];
|
||||
break;
|
||||
case 'vol_cont':
|
||||
$tempDevice[$deviceId['id']]['currentVolume'] = $lastRecord['value'];
|
||||
break;
|
||||
case 'media_apps':
|
||||
$tempDevice[$deviceId['id']]['currentApplication'] = "kodi";
|
||||
break;
|
||||
case 'media_input':
|
||||
$tempDevice[$deviceId['id']]['currentInput'] = "pc";
|
||||
break;
|
||||
case 'media_status':
|
||||
$tempDevice[$deviceId['id']]['activityState'] = "ACTIVE";
|
||||
$tempDevice[$deviceId['id']]['playbackState'] = "PLAYING";
|
||||
break;
|
||||
case 'on/off':
|
||||
$tempDevice[$deviceId['id']]['on'] = ($lastRecord['value'] == 1 ? true : false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// $lastRecord = RecordManager::getLastRecord($deviceId['id']);
|
||||
// //var_dump($lastRecord);
|
||||
// if ($lastRecord['execuded'] == 1) {
|
||||
// $online = true;
|
||||
// $status = 'SUCCESS';
|
||||
// } else {
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($deviceId['id'], 6) as $key => $value) {
|
||||
if ($value['execuded'] == 1) {
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
}
|
||||
}
|
||||
|
||||
switch ($subDeviceData['type']) {
|
||||
case 'temp_cont':
|
||||
$tempDevice[$deviceId['id']]['thermostatMode'] = 'off';
|
||||
if ($lastRecord['value'] != 0) {
|
||||
$tempDevice[$deviceId['id']]['thermostatMode'] = 'heat';
|
||||
}
|
||||
$tempDevice[$deviceId['id']]['thermostatTemperatureAmbient'] = $lastRecord['value'];
|
||||
$tempDevice[$deviceId['id']]['thermostatTemperatureSetpoint'] = $lastRecord['value'];
|
||||
break;
|
||||
case 'vol_cont':
|
||||
$tempDevice[$deviceId['id']]['currentVolume'] = $lastRecord['value'];
|
||||
break;
|
||||
case 'media_apps':
|
||||
$tempDevice[$deviceId['id']]['currentApplication'] = "kodi";
|
||||
break;
|
||||
case 'media_input':
|
||||
$tempDevice[$deviceId['id']]['currentInput'] = "pc";
|
||||
break;
|
||||
case 'media_status':
|
||||
$tempDevice[$deviceId['id']]['activityState'] = "ACTIVE";
|
||||
$tempDevice[$deviceId['id']]['playbackState'] = "PLAYING";
|
||||
break;
|
||||
case 'on/off':
|
||||
$tempDevice[$deviceId['id']]['on'] = ($lastRecord['value'] == 1 ? true : false);
|
||||
break;
|
||||
if ($waiting < $executed) {
|
||||
$status = "PENDING";
|
||||
$online = true;
|
||||
}
|
||||
// }
|
||||
$devices = $tempDevice;
|
||||
if (count($devices)> 1){
|
||||
$devices[] = $tempDevice;
|
||||
}
|
||||
}
|
||||
$response = [
|
||||
'requestId' => $requestId,
|
||||
'payload' => [
|
||||
'devices' => $devices,
|
||||
],
|
||||
];
|
||||
$apiLogManager = new LogManager('../logs/google-home/' . date("Y-m-d") . '.log');
|
||||
$apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
|
||||
$apiLogManager->setLevel(LOGLEVEL);
|
||||
echo json_encode($response);
|
||||
}
|
||||
|
||||
static function execute($requestId, $payload)
|
||||
{
|
||||
$commands = [];
|
||||
foreach ($payload['commands'] as $key => $command) {
|
||||
foreach ($command['devices'] as $key2 => $device) {
|
||||
$executionCommand = $command['execution'][0];
|
||||
if (isset($command['execution'][$key])) {
|
||||
$executionCommand = $command['execution'][$key];
|
||||
}
|
||||
|
||||
$deviceType = GoogleHomeDeviceTypes::getType($executionCommand['command']);
|
||||
if ($subDeviceId = SubDeviceManager::getSubDeviceByMasterAndType($device['id'], $deviceType)) {
|
||||
$subDeviceId = $subDeviceId['subdevice_id'];
|
||||
switch ($executionCommand['command']) {
|
||||
case 'action.devices.commands.OnOff':
|
||||
$commands[] = self::executeSwitch($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.ThermostatTemperatureSetpoint':
|
||||
$commands[] = self::executeTermostatValue($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.ThermostatSetMode':
|
||||
$commands[] = self::executeTermostatMode($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.setVolume':
|
||||
$commands[] = self::executeVolume($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.appSelect':
|
||||
$commands[] = self::executeApp($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.SetInput':
|
||||
$commands[] = self::executeInput($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.mediaNext':
|
||||
$commands[] = self::executeMediaCont($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.mediaPrevious':
|
||||
$commands[] = self::executeMediaCont($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.mediaPause':
|
||||
$commands[] = self::executeMediaCont($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.mediaResume':
|
||||
$commands[] = self::executeMediaCont($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.mediaStop':
|
||||
$commands[] = self::executeMediaCont($subDeviceId, $executionCommand);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
$response = [
|
||||
'requestId' => $requestId,
|
||||
'payload' => [
|
||||
'commands' => $commands,
|
||||
],
|
||||
];
|
||||
|
||||
$apiLogManager = new LogManager('../logs/google-home/' . date("Y-m-d") . '.log');
|
||||
$apiLogManager->setLevel(LOGLEVEL);
|
||||
$apiLogManager->write("[API][EXECUTE][$requestId]\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
|
||||
|
||||
echo json_encode($response);
|
||||
}
|
||||
|
||||
static function executeSwitch($subDeviceId, $executionCommand)
|
||||
{
|
||||
$value = 0;
|
||||
$status = 'OFFLINE';
|
||||
$online = false;
|
||||
|
||||
if ($executionCommand['params']['on']) $value = 1;
|
||||
|
||||
RecordManager::createWithSubId($subDeviceId, $value, 'google');
|
||||
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
|
||||
if ($value['execuded'] == 1) {
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($lastRecord['execuded'] == 1){
|
||||
if ($waiting < $executed) {
|
||||
$status = "PENDING";
|
||||
$online = true;
|
||||
$status = 'SUCCESS';
|
||||
} else {
|
||||
}
|
||||
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'on' => $executionCommand['params']['on'],
|
||||
'online' => $online,
|
||||
],
|
||||
];
|
||||
return $commandTemp;
|
||||
}
|
||||
|
||||
static function executeTermostatValue($subDeviceId, $executionCommand)
|
||||
{
|
||||
$value = 0;
|
||||
$status = 'OFFLINE';
|
||||
$online = false;
|
||||
|
||||
if (isset($executionCommand['params']['thermostatTemperatureSetpoint'])) {
|
||||
$value = $executionCommand['params']['thermostatTemperatureSetpoint'];
|
||||
}
|
||||
|
||||
RecordManager::createWithSubId($subDeviceId, $value, 'google');
|
||||
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $lastValue) {
|
||||
if ($lastValue['execuded'] == 1) {
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($waiting < $executed) {
|
||||
$status = "PENDING";
|
||||
$online = true;;
|
||||
}
|
||||
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'thermostatMode' => 'heat',
|
||||
'thermostatTemperatureSetpoint' => $value,
|
||||
'thermostatTemperatureAmbient' => $value,
|
||||
'online' => $online,
|
||||
//ambient z dalšího zenzoru v roomu
|
||||
],
|
||||
];
|
||||
return $commandTemp;
|
||||
}
|
||||
|
||||
static function executeTermostatMode($subDeviceId, $executionCommand)
|
||||
{
|
||||
$mode = "off";
|
||||
$value = 0;
|
||||
$status = 'OFFLINE';
|
||||
$online = false;
|
||||
|
||||
if (isset($executionCommand['params']['thermostatMode']) && $executionCommand['params']['thermostatMode'] != 'off') {
|
||||
$mode = $executionCommand['params']['thermostatMode'];
|
||||
$value = RecordManager::getLastRecordNotNull($subDeviceId)['value'];
|
||||
}
|
||||
|
||||
RecordManager::createWithSubId($subDeviceId, $value, 'google');
|
||||
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
|
||||
if ($value['execuded'] == 1) {
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($waiting < $executed) {
|
||||
$status = "PENDING";
|
||||
$online = true;
|
||||
}
|
||||
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'thermostatMode' => $mode,
|
||||
'online' => $online,
|
||||
],
|
||||
];
|
||||
|
||||
return $commandTemp;
|
||||
}
|
||||
|
||||
static function executeVolume($subDeviceId, $executionCommand)
|
||||
{
|
||||
//echo $executionCommand['params']['volumeLevel'];
|
||||
$status = 'OFFLINE';
|
||||
$online = false;
|
||||
|
||||
$currentVolume = RecordManager::getLastRecord($subDeviceId)['value'];
|
||||
|
||||
if (isset($executionCommand['params']['volumeLevel'])) {
|
||||
RecordManager::createWithSubId($subDeviceId, $executionCommand['params']['volumeLevel']);
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($deviceId['id'], 6) as $key => $value) {
|
||||
if ($value['execuded'] == 1){
|
||||
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
|
||||
if ($value['execuded'] == 1) {
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
}
|
||||
}
|
||||
if ($waiting < $executed){
|
||||
if ($waiting < $executed) {
|
||||
$status = "PENDING";
|
||||
$online = true;
|
||||
$currentVolume = $executionCommand['params']['volumeLevel'];
|
||||
}
|
||||
}
|
||||
$devices = $tempDevice;
|
||||
/*if (count($devices)> 1){
|
||||
$devices[] = $tempDevice;
|
||||
}*/
|
||||
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'currentVolume' => $currentVolume,
|
||||
'online' => $online,
|
||||
],
|
||||
];
|
||||
|
||||
return $commandTemp;
|
||||
}
|
||||
$response = [
|
||||
'requestId' => $requestId,
|
||||
'payload' => [
|
||||
'devices' => $devices,
|
||||
],
|
||||
];
|
||||
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
|
||||
$apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
|
||||
$apiLogManager->setLevel(LOGLEVEL);
|
||||
echo json_encode($response);
|
||||
}
|
||||
|
||||
static function execute($requestId, $payload){
|
||||
$commands = [];
|
||||
foreach ($payload['commands'] as $key => $command) {
|
||||
foreach ($command['devices'] as $key2 => $device) {
|
||||
$executionCommand = $command['execution'][0];
|
||||
if (isset($command['execution'][$key])) {
|
||||
$executionCommand = $command['execution'][$key];
|
||||
static function executeApp($subDeviceId, $executionCommand)
|
||||
{
|
||||
//echo $executionCommand['params']['newApplication'];
|
||||
$status = 'OFFLINE';
|
||||
$online = false;
|
||||
|
||||
$currentApplication = RecordManager::getLastRecord($subDeviceId)['value'];
|
||||
|
||||
if (isset($executionCommand['params']['newApplication'])) {
|
||||
RecordManager::createWithSubId($subDeviceId, $executionCommand['params']['newApplication']);
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
|
||||
if ($value['execuded'] == 1) {
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
}
|
||||
}
|
||||
|
||||
$deviceType = GoogleHomeDeviceTypes::getType($executionCommand['command']);
|
||||
$subDeviceId = SubDeviceManager::getSubDeviceByMasterAndType($device['id'], $deviceType)['subdevice_id'];
|
||||
switch ($executionCommand['command']) {
|
||||
case 'action.devices.commands.OnOff':
|
||||
$commands[] = self::executeSwitch($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.ThermostatTemperatureSetpoint':
|
||||
$commands[] = self::executeTermostatValue($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.ThermostatSetMode':
|
||||
$commands[] = self::executeTermostatMode($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.setVolume':
|
||||
$commands[] = self::executeVolume($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.appSelect':
|
||||
$commands[] = self::executeApp($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.SetInput':
|
||||
$commands[] = self::executeInput($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.mediaNext':
|
||||
$commands[] = self::executeMediaCont($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.mediaPrevious':
|
||||
$commands[] = self::executeMediaCont($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.mediaPause':
|
||||
$commands[] = self::executeMediaCont($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.mediaResume':
|
||||
$commands[] = self::executeMediaCont($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
case 'action.devices.commands.mediaStop':
|
||||
$commands[] = self::executeMediaCont($subDeviceId, $executionCommand);
|
||||
break;
|
||||
|
||||
if ($waiting < $executed) {
|
||||
$status = "PENDING";
|
||||
$online = true;
|
||||
$currentApplication = $executionCommand['params']['newApplication'];
|
||||
}
|
||||
}
|
||||
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'currentApplication' => $currentApplication,
|
||||
'online' => $online,
|
||||
],
|
||||
];
|
||||
|
||||
return $commandTemp;
|
||||
}
|
||||
$response = [
|
||||
'requestId' => $requestId,
|
||||
'payload' => [
|
||||
'commands' => $commands,
|
||||
],
|
||||
];
|
||||
|
||||
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
|
||||
$apiLogManager->setLevel(LOGLEVEL);
|
||||
$apiLogManager->write("[API][EXECUTE][$requestId]\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
|
||||
static function executeInput($subDeviceId, $executionCommand)
|
||||
{
|
||||
//echo $executionCommand['params']['newInput'];
|
||||
$status = 'OFFLINE';
|
||||
$online = false;
|
||||
|
||||
echo json_encode($response);
|
||||
}
|
||||
$currentInput = RecordManager::getLastRecord($subDeviceId)['value'];
|
||||
|
||||
static function executeSwitch($subDeviceId, $executionCommand){
|
||||
$value = 0;
|
||||
$status = 'OFFLINE';
|
||||
$online = false;
|
||||
|
||||
if ($executionCommand['params']['on']) $value = 1;
|
||||
|
||||
RecordManager::createWithSubId($subDeviceId, $value);
|
||||
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
|
||||
if ($value['execuded'] == 1){
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
if (isset($executionCommand['params']['newInput'])) {
|
||||
RecordManager::createWithSubId($subDeviceId, $executionCommand['params']['newInput']);
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
|
||||
if ($value['execuded'] == 1) {
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
}
|
||||
}
|
||||
if ($waiting < $executed) {
|
||||
$status = "PENDING";
|
||||
$online = true;
|
||||
$currentInput = $executionCommand['params']['newInput'];
|
||||
}
|
||||
}
|
||||
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'currentInput' => $currentInput,
|
||||
'online' => $online,
|
||||
],
|
||||
];
|
||||
|
||||
return $commandTemp;
|
||||
}
|
||||
|
||||
if ($waiting < $executed){
|
||||
$status = "PENDING";
|
||||
static function executeMediaCont($subDeviceId, $executionCommand)
|
||||
{
|
||||
$status = 'SUCCESS';
|
||||
$online = true;
|
||||
}
|
||||
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'on' => $executionCommand['params']['on'],
|
||||
'online' => $online,
|
||||
],
|
||||
];
|
||||
return $commandTemp;
|
||||
}
|
||||
|
||||
static function executeTermostatValue($subDeviceId, $executionCommand){
|
||||
$value = 0;
|
||||
$status = 'OFFLINE';
|
||||
$online = false;
|
||||
|
||||
if (isset($executionCommand['params']['thermostatTemperatureSetpoint'])) {
|
||||
$value = $executionCommand['params']['thermostatTemperatureSetpoint'];
|
||||
}
|
||||
|
||||
RecordManager::createWithSubId($subDeviceId, $value);
|
||||
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $lastValue) {
|
||||
if ($lastValue['execuded'] == 1){
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($waiting < $executed){
|
||||
$status = "PENDING";
|
||||
$online = true;;
|
||||
}
|
||||
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'thermostatMode' => 'heat',
|
||||
'thermostatTemperatureSetpoint' => $value,
|
||||
'thermostatTemperatureAmbient' => $value,
|
||||
'online' => $online,
|
||||
//ambient z dalšího zenzoru v roomu
|
||||
],
|
||||
];
|
||||
return $commandTemp;
|
||||
}
|
||||
|
||||
static function executeTermostatMode($subDeviceId, $executionCommand){
|
||||
$mode = "off";
|
||||
$value = 0;
|
||||
$status = 'OFFLINE';
|
||||
$online = false;
|
||||
|
||||
if (isset($executionCommand['params']['thermostatMode']) && $executionCommand['params']['thermostatMode'] != 'off') {
|
||||
$mode = $executionCommand['params']['thermostatMode'];
|
||||
$value = RecordManager::getLastRecordNotNull($subDeviceId)['value'];
|
||||
}
|
||||
|
||||
RecordManager::createWithSubId($subDeviceId, $value);
|
||||
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
|
||||
if ($value['execuded'] == 1){
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($waiting < $executed){
|
||||
$status = "PENDING";
|
||||
$online = true;
|
||||
}
|
||||
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'thermostatMode' => $mode,
|
||||
'online' => $online,
|
||||
],
|
||||
];
|
||||
|
||||
return $commandTemp;
|
||||
}
|
||||
|
||||
static function executeVolume($subDeviceId, $executionCommand){
|
||||
//echo $executionCommand['params']['volumeLevel'];
|
||||
$status = 'OFFLINE';
|
||||
$online = false;
|
||||
|
||||
$currentVolume = RecordManager::getLastRecord($subDeviceId)['value'];
|
||||
|
||||
if (isset($executionCommand['params']['volumeLevel'])) {
|
||||
RecordManager::createWithSubId($subDeviceId, $executionCommand['params']['volumeLevel']);
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
|
||||
if ($value['execuded'] == 1){
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
}
|
||||
}
|
||||
if ($waiting < $executed){
|
||||
$status = "PENDING";
|
||||
$online = true;
|
||||
$currentVolume = $executionCommand['params']['volumeLevel'];
|
||||
}
|
||||
}
|
||||
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'currentVolume' => $currentVolume,
|
||||
'online' => $online,
|
||||
],
|
||||
];
|
||||
|
||||
return $commandTemp;
|
||||
}
|
||||
|
||||
static function executeApp($subDeviceId, $executionCommand){
|
||||
//echo $executionCommand['params']['newApplication'];
|
||||
$status = 'OFFLINE';
|
||||
$online = false;
|
||||
|
||||
$currentApplication = RecordManager::getLastRecord($subDeviceId)['value'];
|
||||
|
||||
if (isset($executionCommand['params']['newApplication'])) {
|
||||
RecordManager::createWithSubId($subDeviceId, $executionCommand['params']['newApplication']);
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
|
||||
if ($value['execuded'] == 1){
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
}
|
||||
}
|
||||
if ($waiting < $executed){
|
||||
$status = "PENDING";
|
||||
$online = true;
|
||||
$currentApplication = $executionCommand['params']['newApplication'];
|
||||
}
|
||||
}
|
||||
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'currentApplication' => $currentApplication,
|
||||
'online' => $online,
|
||||
],
|
||||
];
|
||||
|
||||
return $commandTemp;
|
||||
}
|
||||
|
||||
static function executeInput($subDeviceId, $executionCommand){
|
||||
//echo $executionCommand['params']['newInput'];
|
||||
$status = 'OFFLINE';
|
||||
$online = false;
|
||||
|
||||
$currentInput = RecordManager::getLastRecord($subDeviceId)['value'];
|
||||
|
||||
if (isset($executionCommand['params']['newInput'])) {
|
||||
RecordManager::createWithSubId($subDeviceId, $executionCommand['params']['newInput']);
|
||||
$executed = 0;
|
||||
$waiting = 0;
|
||||
foreach (RecordManager::getLastRecord($subDeviceId, 4) as $key => $value) {
|
||||
if ($value['execuded'] == 1){
|
||||
$executed++;
|
||||
} else {
|
||||
$waiting++;
|
||||
}
|
||||
}
|
||||
if ($waiting < $executed){
|
||||
$status = "PENDING";
|
||||
$online = true;
|
||||
$currentInput = $executionCommand['params']['newInput'];
|
||||
}
|
||||
}
|
||||
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'currentInput' => $currentInput,
|
||||
'online' => $online,
|
||||
],
|
||||
];
|
||||
|
||||
return $commandTemp;
|
||||
}
|
||||
|
||||
static function executeMediaCont($subDeviceId, $executionCommand){
|
||||
$status = 'SUCCESS';
|
||||
$online = true;
|
||||
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'online' => $online,
|
||||
],
|
||||
];
|
||||
|
||||
return $commandTemp;
|
||||
}
|
||||
$deviceId = SubDeviceManager::getSubDeviceMaster($subDeviceId)['device_id'];
|
||||
$commandTemp = [
|
||||
'ids' => [(string) $deviceId],
|
||||
'status' => $status,
|
||||
'states' => [
|
||||
'online' => $online,
|
||||
],
|
||||
];
|
||||
|
||||
return $commandTemp;
|
||||
}
|
||||
}
|
||||
|
@@ -8,9 +8,9 @@ class AutomationManager{
|
||||
}
|
||||
|
||||
public static function deactive($automationId) {
|
||||
$automation = Db::loadOne ("SELECT * FROM automation WHERE automation_id=?" , array ($automationId));
|
||||
$flipedValue = ($automation['active'] == 1 ? 0 : 1);
|
||||
return Db::command ('UPDATE automation SET active = ? WHERE automation_id=?', array ($flipedValue,$automationId));
|
||||
$automation = self::getById($automationId,["enabled"]);
|
||||
$flipedValue = ($automation['enabled'] == 1 ? 0 : 1);
|
||||
return Db::command ('UPDATE automation SET enabled = ? WHERE automation_id=?', array ($flipedValue,$automationId));
|
||||
}
|
||||
|
||||
public static function restart($automationId) {
|
||||
@@ -34,19 +34,24 @@ class AutomationManager{
|
||||
}
|
||||
} catch(PDOException $error) {
|
||||
echo $error->getMessage();
|
||||
return false;
|
||||
die();
|
||||
}
|
||||
}
|
||||
|
||||
public static function getAll(){
|
||||
return Db::loadAll ("SELECT * FROM automation");
|
||||
public static function getAll($collums = ['*']){
|
||||
return Db::loadAll ("SELECT ". implode("," , $collums)." FROM automation");
|
||||
}
|
||||
|
||||
public static function getById($automationId, $collums = ['*']){
|
||||
return Db::loadOne("SELECT ". implode("," , $collums)." FROM automation WHERE automation_id = ?", [$automationId]);
|
||||
|
||||
}
|
||||
|
||||
public static function executeAll(){
|
||||
global $logManager;
|
||||
|
||||
$automations = Db::loadAll ("SELECT * FROM automation");
|
||||
/*$automations = Db::loadAll ("SELECT * FROM automation");
|
||||
$dayNameNow = strtolower (date('D', time()));
|
||||
|
||||
foreach ($automations as $automation) {
|
||||
@@ -170,6 +175,6 @@ class AutomationManager{
|
||||
Db::edit('automation', array('locked' => 0), 'WHERE automation_id = ?', array($automation['automation_id']));
|
||||
}
|
||||
}
|
||||
}
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
@@ -2,16 +2,21 @@
|
||||
class RecordManager{
|
||||
public static $records;
|
||||
|
||||
public static function createWithSubId ($subDeviceId, $value) {
|
||||
public static function createWithSubId ($subDeviceId, $value, $origin = false) {
|
||||
try {
|
||||
$record = [
|
||||
'execuded' => 1,
|
||||
];
|
||||
|
||||
Db::edit ('records', $record, 'WHERE subdevice_id = ?', array ($subDeviceId));
|
||||
$record = array (
|
||||
'subdevice_id' => $subDeviceId,
|
||||
'value' => $value,
|
||||
);
|
||||
|
||||
if ($origin != false)
|
||||
$record['Origin'] = $origin;
|
||||
|
||||
return Db::add ('records', $record);
|
||||
} catch(PDOException $error) {
|
||||
echo $error->getMessage();
|
||||
@@ -19,7 +24,7 @@ class RecordManager{
|
||||
}
|
||||
}
|
||||
|
||||
public static function create ($deviceId, $type, $value) {
|
||||
public static function create ($deviceId, $type, $value, $origin = false) {
|
||||
$subDeviceId = Db::loadOne('SELECT * FROM subdevices WHERE device_id = ? AND type = ?;', array($deviceId, $type))['subdevice_id'];
|
||||
if ($subDeviceId == '') {
|
||||
return false;
|
||||
@@ -28,7 +33,7 @@ class RecordManager{
|
||||
//Ochrana proti duplicitním hodnotám zapisují se jen změny
|
||||
$lastRecord = self::getLastRecord($subDeviceId, 1);
|
||||
|
||||
if ($lastRecord['value'] == $value){
|
||||
if (isset($lastRecord['value']) && $lastRecord['value'] == $value){
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -42,6 +47,10 @@ class RecordManager{
|
||||
'subdevice_id' => $subDeviceId,
|
||||
'value' => $value,
|
||||
);
|
||||
|
||||
if ($origin != false)
|
||||
$record['Origin'] = $origin;
|
||||
|
||||
return Db::add ('records', $record);
|
||||
} catch(PDOException $error) {
|
||||
echo $error->getMessage();
|
||||
@@ -87,7 +96,7 @@ class RecordManager{
|
||||
$dateTime = $dateTime->modify($periodLocal);
|
||||
$dateTime = $dateTime->format('Y-m-d H:i:s');
|
||||
$groupBy = strtoupper($groupBy).'(time)';
|
||||
$sql = 'SELECT value, time, execuded FROM records
|
||||
$sql = 'SELECT value, time, execuded, origin FROM records
|
||||
WHERE
|
||||
subdevice_id = ?
|
||||
AND
|
||||
@@ -114,6 +123,6 @@ class RecordManager{
|
||||
public static function setHistory($subDeviceId){
|
||||
$history = SubDeviceManager::getSubDevice($subDeviceId)['history'];
|
||||
if ($history > 0) self::clean(-abs($history));
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@@ -14,7 +14,7 @@ class SettingsManager{
|
||||
}
|
||||
|
||||
public static function create ($name, $value, $type = '') {
|
||||
if (!self::getByName($name)){
|
||||
//if (!self::getByName($name)){
|
||||
$setting = array (
|
||||
'name' => $name,
|
||||
'value' => $value,
|
||||
@@ -26,7 +26,7 @@ class SettingsManager{
|
||||
echo $error->getMessage();
|
||||
die();
|
||||
}
|
||||
}
|
||||
//}
|
||||
}
|
||||
|
||||
public static function update ($name, $value, $type = '') {
|
||||
|
@@ -22,7 +22,7 @@ class AirQuality extends VirtualDeviceManager
|
||||
|
||||
$finalUrl = sprintf($this->api_uri, $this->city_sluig, $this->app_id);
|
||||
$json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true);
|
||||
RecordManager::create($deviceId, $this->subdevice_type, $json['data']['aqi']);
|
||||
RecordManager::create($deviceId, $this->subdevice_type, $json['data']['aqi'], 'plugin');
|
||||
} else {
|
||||
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name, 'senzore-virtual');
|
||||
DeviceManager::approved($this->virtual_device_name);
|
||||
|
@@ -30,7 +30,7 @@ class AirQuality extends VirtualDeviceManager
|
||||
|
||||
$finalUrl = sprintf($this->api_uri, $this->city_sluig, $this->app_id);
|
||||
$json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true);
|
||||
RecordManager::create($deviceId, $this->subdevice_type, $json['data']['aqi']);
|
||||
RecordManager::create($deviceId, $this->subdevice_type, $json['data']['aqi'], 'plugin');
|
||||
} else {
|
||||
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name, 'senzore-virtual');
|
||||
DeviceManager::approved($this->virtual_device_name);
|
||||
|
@@ -31,7 +31,7 @@ class CovidV2 extends VirtualDeviceManager
|
||||
$json = json_decode(Utilities::CallAPI('GET', $finalUrl, ''), true)['data'];
|
||||
|
||||
foreach ($dataItems as $dataItem) {
|
||||
RecordManager::create($deviceId, strtolower($dataItem), end($json)[$this->name_index[$dataItem]]);
|
||||
RecordManager::create($deviceId, strtolower($dataItem), end($json)[$this->name_index[$dataItem]], 'plugin');
|
||||
}
|
||||
} else {
|
||||
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name, strtolower($this->virtual_device_name));
|
||||
|
85
app/plugins/DameJidlo.php
Normal file
85
app/plugins/DameJidlo.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?php
|
||||
class DameJidlo extends VirtualDeviceManager
|
||||
{
|
||||
private $virtual_device_name = "Dáme Jídlo";
|
||||
private $device_type = "virtual-device";
|
||||
private $subdevice_type = "dame-jidlo";
|
||||
|
||||
public function deliveryTime($token)
|
||||
{
|
||||
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
|
||||
$ch = curl_init();
|
||||
|
||||
curl_setopt($ch, CURLOPT_URL, 'https://cz.fd-api.com/api/v5/tracking/active-orders?time_variation=Variation2');
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
|
||||
|
||||
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
|
||||
|
||||
$headers = array();
|
||||
$headers[] = 'Authority: cz.fd-api.com';
|
||||
$headers[] = 'Sec-Ch-Ua: \"Chromium\";v=\"88\", \"Google Chrome\";v=\"88\", \";Not A Brand\";v=\"99\"';
|
||||
$headers[] = 'Accept: application/json, text/plain, */*';
|
||||
$headers[] = 'Authorization: Bearer ' . $token;
|
||||
$headers[] = 'X-Pd-Language-Id: 2';
|
||||
$headers[] = 'Sec-Ch-Ua-Mobile: ?0';
|
||||
$headers[] = 'X-Fp-Api-Key: volo';
|
||||
$headers[] = 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36';
|
||||
$headers[] = 'Origin: https://www.damejidlo.cz';
|
||||
$headers[] = 'Sec-Fetch-Site: cross-site';
|
||||
$headers[] = 'Sec-Fetch-Mode: cors';
|
||||
$headers[] = 'Sec-Fetch-Dest: empty';
|
||||
$headers[] = 'Referer: https://www.damejidlo.cz/';
|
||||
$headers[] = 'Accept-Language: en-US,en;q=0.9,cs-CZ;q=0.8,cs;q=0.7';
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
|
||||
$result = curl_exec($ch);
|
||||
if (curl_errno($ch)) {
|
||||
echo 'Error:' . curl_error($ch);
|
||||
}
|
||||
curl_close($ch);
|
||||
|
||||
return (isset(json_decode($result, true)['data']['active_orders'][0]['delivery']['time']['eta']) ? json_decode($result, true)['data']['active_orders'][0]['delivery']['time']['eta'] : 0);
|
||||
}
|
||||
|
||||
function make()
|
||||
{
|
||||
//Register the settings
|
||||
$settingMng = new SettingsManager();
|
||||
if (!($settingField = $settingMng->getByName("damejidlo_token","damejidlo"))) {
|
||||
$settingMng->create("damejidlo_token", "", "damejidlo");
|
||||
} else {
|
||||
$bearer = $settingField['value'];
|
||||
}
|
||||
|
||||
try {
|
||||
if (DeviceManager::registeret($this->virtual_device_name)) {
|
||||
$deviceId = DeviceManager::getDeviceByToken($this->virtual_device_name)['device_id'];
|
||||
if (!$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, $this->subdevice_type)) {
|
||||
SubDeviceManager::create($deviceId, $this->subdevice_type, '');
|
||||
sleep(1);
|
||||
$subDevice = SubDeviceManager::getSubDeviceByMaster($deviceId, strtolower($this->subdevice_type));
|
||||
}
|
||||
|
||||
//if (!$this->fetchEnabled($deviceId,$subDevice['subdevice_id'])) die();
|
||||
|
||||
RecordManager::create($deviceId, $this->subdevice_type, $this->deliveryTime($bearer), 'plugin');
|
||||
} else {
|
||||
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name, 'senzore-virtual');
|
||||
DeviceManager::approved($this->virtual_device_name);
|
||||
}
|
||||
return 'sucessful';
|
||||
} catch (Exception $e) {
|
||||
return 'exception: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
function translate($value){
|
||||
if ($value == 0) {
|
||||
return " Delivered";
|
||||
} else if ($value <= 1) {
|
||||
return "less " . $value;
|
||||
}
|
||||
return '';
|
||||
}
|
||||
}
|
@@ -3,6 +3,19 @@ class DatabaseBackup
|
||||
{
|
||||
public function make()
|
||||
{
|
||||
//Register the settings
|
||||
$time = '00:00';
|
||||
$settingMng = new SettingsManager();
|
||||
if (!($settingField = $settingMng->getByName("backup_time","db_backup"))) {
|
||||
$settingMng->create("backup_time", $time, "db_backup");
|
||||
} else {
|
||||
$time = $settingField['value'];
|
||||
}
|
||||
|
||||
//Time to Backup ?
|
||||
if (date("H:i",time()) != $time)
|
||||
return 'pending';
|
||||
|
||||
try {
|
||||
$filenames = [];
|
||||
$backupWorker = new DatabaseBackup;
|
||||
@@ -19,6 +32,7 @@ class DatabaseBackup
|
||||
private function data()
|
||||
{
|
||||
$backupfile = $_SERVER['DOCUMENT_ROOT'] . BASEDIR . "/backup/" . DBNAME . '_data_' . date("Y-m-d", time()) . '.sql';
|
||||
if (file_exists($backupfile)) return null;
|
||||
$command = "mysqldump --skip-comments --no-create-info -h localhost -u " . DBUSER . " -p" . DBPASS . " " . DBNAME . " -r $backupfile 2>&1";
|
||||
$this->executeCommand($command);
|
||||
return $backupfile;
|
||||
@@ -27,6 +41,7 @@ class DatabaseBackup
|
||||
private function scheme()
|
||||
{
|
||||
$backupfile = $_SERVER['DOCUMENT_ROOT'] . BASEDIR . "/backup/" . DBNAME . '_scheme_' . date("Y-m-d", time()) . '.sql';
|
||||
if (file_exists($backupfile)) return null;
|
||||
$command = "mysqldump --skip-comments --no-data -h localhost -u " . DBUSER . " -p" . DBPASS . " " . DBNAME . " -r $backupfile 2>&1";
|
||||
$this->executeCommand($command);
|
||||
return $backupfile;
|
||||
|
@@ -29,7 +29,7 @@ class N7Day extends VirtualDeviceManager
|
||||
$datediff = $now - $your_date;
|
||||
$daysUntilN7Day = round($datediff / (60 * 60 * 24));
|
||||
|
||||
RecordManager::create($deviceId, $this->subdevice_type, $daysUntilN7Day);
|
||||
RecordManager::create($deviceId, $this->subdevice_type, $daysUntilN7Day, 'plugin');
|
||||
} else {
|
||||
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name, $this->device_type);
|
||||
DeviceManager::approved($this->virtual_device_name);
|
||||
|
@@ -55,7 +55,7 @@ class OpenWeatherMap extends VirtualDeviceManager
|
||||
}
|
||||
|
||||
|
||||
RecordManager::create($deviceId, $this->subdevice_type, $json['weather'][0]['id']);
|
||||
RecordManager::create($deviceId, $this->subdevice_type, $json['weather'][0]['id'], 'plugin');
|
||||
} else {
|
||||
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name, 'senzore-virtual');
|
||||
DeviceManager::approved($this->virtual_device_name);
|
||||
|
@@ -52,7 +52,7 @@ class UsaElection extends VirtualDeviceManager
|
||||
$OnePercent = ($Trump + $Biden + $Unknown) / 100;
|
||||
|
||||
foreach ($dataItems as $Category) {
|
||||
RecordManager::create($deviceId, strtolower($Category), round(($$Category / $OnePercent)));
|
||||
RecordManager::create($deviceId, strtolower($Category), round(($$Category / $OnePercent)), 'plugin');
|
||||
}
|
||||
} else {
|
||||
DeviceManager::create($this->virtual_device_name, $this->virtual_device_name, 'senzore-virtual');
|
||||
|
2
git-stats.ps1
Normal file
2
git-stats.ps1
Normal file
@@ -0,0 +1,2 @@
|
||||
$lines = (ls -r|sls '^\s*(#|$)' -a -n).Count
|
||||
write-host $lines" of code"
|
Reference in New Issue
Block a user