New Endpoint API

This commit is contained in:
JonatanRek 2020-06-28 16:45:05 +02:00
parent 7954ae49b9
commit 78c29482a6
3 changed files with 167 additions and 2 deletions

View File

@ -30,10 +30,13 @@ $router->post('/api/widgets/{widgetId}/check', 'WidgetApi@check');
$router->post('/api/widgets/{widgetId}/detail', 'WidgetApi@detail'); $router->post('/api/widgets/{widgetId}/detail', 'WidgetApi@detail');
//Google Home - API
$router->any('/api/HA/auth', 'Oauth'); $router->any('/api/HA/auth', 'Oauth');
$router->any('/api/HA', 'GoogleHomeApi@response'); $router->any('/api/HA', 'GoogleHomeApi@response');
//Endpoints API
$router->post('/api/endpoint', 'EndpointsApi@default');
// examples // examples
$router->any('/api/example', 'ExampleApi@example'); $router->any('/api/example', 'ExampleApi@example');
$router->any('/example', 'ExampleController@index'); $router->any('/example', 'ExampleController@index');

133
app/api/EndpointsApi.php Normal file
View File

@ -0,0 +1,133 @@
<?php
class EndpointsApi extends ApiController{
public function default(){
// $this->requireAuth();
$obj = $this->input;
//variables Definition
$command = "null";
//Log
$logManager = new LogManager();
$apiLogManager = new LogManager('../logs//api/'. date("Y-m-d").'.log');
//Token Checks
if ($obj['token'] == null || !isset($obj['token'])) {
$this->response([
'state' => 'unsuccess',
'errorMSG' => "Missing Value Token in JSON payload",
], 401);
}
//Vstupní Checky
if (!DeviceManager::registeret($obj['token'])) {
//Notification data setup
$notificationMng = new NotificationManager;
$notificationData = [
'title' => 'Info',
'body' => 'New device Detected Found',
'icon' => BASEDIR . '/app/templates/images/icon-192x192.png',
];
//Subdevice Registration
$deviceId = DeviceManager::create($obj['token'], $obj['token']);
foreach ($obj['values'] as $key => $value) {
if (!SubDeviceManager::getSubDeviceByMaster($deviceId, $key)) {
SubDeviceManager::create($deviceId, $key, UNITS[$key]);
}
}
//Notification for newly added Device
if ($notificationData != []) {
$subscribers = $notificationMng::getSubscription();
foreach ($subscribers as $key => $subscriber) {
$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordType::INFO);
$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
}
}
$logManager->write("[API] Registering Device", LogRecordType::INFO);
$this->response([
'state' => 'unsuccess',
'errorMSG' => "Device not registeret",
], 401);
}
if (!DeviceManager::approved($obj['token'])) {
$this->response([
'state' => 'unsuccess',
'errorMSG' => "Unaproved Device",
], 401);
}
//Diagnostic/Log Data Save
if (isset($obj['settings'])){
$data = ['mac' => $obj['settings']["network"]["mac"], 'ip_address' => $obj['settings']["network"]["ip"]];
if (array_key_exists("firmware_hash", $obj['settings'])) {
$data['firmware_hash'] = $obj['settings']["firmware_hash"];
}
DeviceManager::editByToken($obj['token'], $data);
$this->response([
'state' => 'succes',
'command' => $command,
]);
}
// Issuing command
if ($command == "null"){
$device = DeviceManager::getDeviceByToken($obj['token']);
$deviceId = $device['device_id'];
$deviceCommand = $device["command"];
if ($deviceCommand != '' && $deviceCommand != null && $deviceCommand != "null")
{
$command = $deviceCommand;
$data = [
'command'=>'null'
];
DeviceManager::editByToken($obj['token'], $data);
$logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordType::INFO);
}
}
if (isset($obj['values'])) {
//zapis
} else {
//Vypis
$device = DeviceManager::getDeviceByToken($obj['token']);
$deviceId = $device['device_id'];
if (count(SubDeviceManager::getAllSubDevices($deviceId)) == 0) {
SubDeviceManager::create($deviceId, 'on/off', UNITS[$key]);
//RecordManager::create($deviceId, 'on/off', 0);
}
$subDevicesData = SubDeviceManager::getAllSubDevices($deviceId);
$subDeviceLastReordValue = [];
foreach ($subDevicesData as $key => $subDeviceData) {
$subDeviceId = $subDeviceData['subdevice_id'];
$subDeviceLastReord = RecordManager::getLastRecord($subDeviceId);
$subDeviceLastReordValue[] = [$subDeviceData['type'] => $subDeviceLastReord['value']];
if ($subDeviceLastReord['execuded'] == 0){
$logManager->write("[API] subDevice_ID ".$subDeviceId . " executed comand with value " . json_encode($subDeviceLastReordValue) ." executed " . $subDeviceLastReord['execuded'], LogRecordType::INFO);
RecordManager::setExecuted($subDeviceLastReord['record_id']);
}
}
$this->response(['device' => [
'hostname' => $device['name'],
'ipAddress' => $device['ip_address'],
'subnet' => $device['subnet'],
'gateway' => $device['gateway'],
],
'state' => 'succes',
'value' => $subDeviceLastReordValue,
'command' => $command]);
}
// this method returns response as json
}
}

View File

@ -72,7 +72,7 @@ class GoogleHomeDeviceTypes {
const YogurtMaker = 'action.devices.types.YOGURTMAKER';*/ const YogurtMaker = 'action.devices.types.YOGURTMAKER';*/
private static $actionWordBook = [ private static $actionWordBook = [
'control-light' => 'action.devices.types.OUTLET', 'control-light' => 'action.devices.types.LIGHT',
'control-socket' => 'action.devices.types.OUTLET', 'control-socket' => 'action.devices.types.OUTLET',
'control-temp' => 'action.devices.types.THERMOSTAT', 'control-temp' => 'action.devices.types.THERMOSTAT',
'control-media' => 'action.devices.types.REMOTECONTROL', 'control-media' => 'action.devices.types.REMOTECONTROL',
@ -103,6 +103,35 @@ class GoogleHomeDeviceTypes {
'levelStepSize' => 2, 'levelStepSize' => 2,
'commandOnlyVolume' => false, 'commandOnlyVolume' => false,
], ],
'media_status'=> [
'transportControlSupportedCommands' => [
"NEXT",
"PREVIOUS",
"PAUSE",
"STOP",
"RESUME",
],
],
'media_apps' => [
[
"key" => "kodi",
"names" => [
"name_synonym" => [
"Kodi",
],
"lang" => "en"
]
],
],
'media_inputs' => [
"key" => "pc",
"names" => [
"name_synonym" => [
"PC",
],
"lang" => "en"
]
]
]; ];
static function getAction($deviceType){ static function getAction($deviceType){