Weather API Start

This commit is contained in:
Václav Španinger 2020-08-25 15:12:25 +02:00
parent 7f42d49634
commit 0efbda0583
4 changed files with 101 additions and 35 deletions

View File

@ -31,10 +31,11 @@ $router->get('/api/users', 'UsersApi@default');
$router->get('/api/server', 'ServerApi@default'); $router->get('/api/server', 'ServerApi@default');
$router->get('/api/server/log', 'ServerApi@logStatus'); $router->get('/api/server/log', 'ServerApi@logStatus');
$router->post('/api/widgets/{widgetId}/run', 'WidgetApi@run'); $router->post('/api/widgets/{widgetId}/run', 'WidgetApi@run');
$router->post('/api/widgets/{widgetId}/detail', 'WidgetApi@detail'); $router->get('/api/widgets/{widgetId}/detail', 'WidgetApi@detail');
//cron //cron
$router->post('/cron/clean', 'CronApi@clean'); $router->post('/cron/clean', 'CronApi@clean');
#$router->post('/cron/fetch', 'CronApi@fetch');
//Google Home - API //Google Home - API
$router->any('/api/HA/auth', 'Oauth'); $router->any('/api/HA/auth', 'Oauth');

View File

@ -1,8 +1,14 @@
<?php <?php
class CronApi extends ApiController { class CronApi extends ApiController {
public function clean(){ public function clean(){
$logKeeper = new LogMaintainer(); $logKeeper = new LogMaintainer();
$logKeeper->purge(LOGTIMOUT); $logKeeper->purge(LOGTIMOUT);
$this->response(['Value' => 'OK']); $this->response(['Value' => 'OK']);
} }
public function fetch(){
echo (new VirtualDeviceManager)->fetch('');
$this->response(['Value' => 'OK']);
}
} }

View File

@ -1,10 +1,12 @@
<?php <?php
/** /**
* *
*/ */
class Utilities class Utilities
{ {
static function cleanString($text) { static function cleanString($text)
{
$utf8 = array( $utf8 = array(
'/[áàâãªä]/u' => 'a', '/[áàâãªä]/u' => 'a',
'/[ÁÀÂÃÄ]/u' => 'A', '/[ÁÀÂÃÄ]/u' => 'A',
@ -50,7 +52,8 @@ class Utilities
* @return [type] [description] * @return [type] [description]
*/ */
static function generateGraphJson(string $type = 'line', array $data = [], array $options = []){ static function generateGraphJson(string $type = 'line', array $data = [], array $options = [])
{
$array = [ $array = [
'type' => $type, 'type' => $type,
'data' => [ 'data' => [
@ -110,7 +113,8 @@ class Utilities
return $count . (($count == 1) ? (" $text") : (" ${text}s")); return $count . (($count == 1) ? (" $text") : (" ${text}s"));
} }
static function checkOperator($value1, $operator, $value2) { static function checkOperator($value1, $operator, $value2)
{
switch ($operator) { switch ($operator) {
case '<': // Less than case '<': // Less than
return $value1 < $value2; return $value1 < $value2;
@ -141,4 +145,37 @@ class Utilities
return FALSE; return FALSE;
} // end switch } // end switch
} }
static function CallAPI($method, $url, $data = false)
{
$curl = curl_init();
switch ($method) {
case "POST":
curl_setopt($curl, CURLOPT_POST, 1);
if ($data)
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
break;
case "PUT":
curl_setopt($curl, CURLOPT_PUT, 1);
break;
default:
if ($data)
$url = sprintf("%s?%s", $url, http_build_query($data));
}
// Optional Authentication:
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
} }

View File

@ -0,0 +1,22 @@
<?php
class VirtualDeviceManager
{
function fetch($url)
{
$json = json_decode(Utilities::CallAPI('GET', 'api.openweathermap.org/data/2.5/weather?q=prague&appid=1ee609f2fcf8048e84f1d2fb1d1d72b5', ''), true);
if (DeviceManager::registeret('1ee609f2fcf8048e84f1d2fb1d1d72b5')) {
$deviceId = DeviceManager::getDeviceByToken('1ee609f2fcf8048e84f1d2fb1d1d72b5')['device_id'];
if (!SubDeviceManager::getSubDeviceByMaster($deviceId, 'weather-nice')) {
SubDeviceManager::create($deviceId, 'weather-nice', '');
}
var_dump($json['weather'][0]);
RecordManager::create($deviceId, 'weather-nice', $json['weather'][0]['main']);
} else {
$deviceId = DeviceManager::create('1ee609f2fcf8048e84f1d2fb1d1d72b5', '1ee609f2fcf8048e84f1d2fb1d1d72b5')['device_id'];
}
}
}