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',
@ -36,21 +38,22 @@ class Utilities
return preg_replace(array_keys($utf8), array_values($utf8), $text); return preg_replace(array_keys($utf8), array_values($utf8), $text);
} }
static function stringInsert($str,$insertstr,$pos) static function stringInsert($str, $insertstr, $pos)
{ {
$str = substr($str, 0, $pos) . $insertstr . substr($str, $pos); $str = substr($str, 0, $pos) . $insertstr . substr($str, $pos);
return $str; return $str;
} }
/** /**
* [generateGraphJson description] * [generateGraphJson description]
* @param string $type [line/bar] * @param string $type [line/bar]
* @param array $data [description] * @param array $data [description]
* @param array $options [description] * @param array $options [description]
* @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' => [
@ -94,51 +97,85 @@ class Utilities
return json_encode($array, JSON_PRETTY_PRINT); return json_encode($array, JSON_PRETTY_PRINT);
} }
static function ago( $datetime ) static function ago($datetime)
{ {
$interval = date_create('now')->diff( $datetime ); $interval = date_create('now')->diff($datetime);
$suffix = ( $interval->invert ? ' ago' : '' ); $suffix = ($interval->invert ? ' ago' : '');
if ( $v = $interval->y >= 1 ) return self::pluralize( $interval->m, 'month' ) . $suffix; if ($v = $interval->y >= 1) return self::pluralize($interval->m, 'month') . $suffix;
if ( $v = $interval->d >= 1 ) return self::pluralize( $interval->d, 'day' ) . $suffix; if ($v = $interval->d >= 1) return self::pluralize($interval->d, 'day') . $suffix;
if ( $v = $interval->h >= 1 ) return self::pluralize( $interval->h, 'hour' ) . $suffix; if ($v = $interval->h >= 1) return self::pluralize($interval->h, 'hour') . $suffix;
if ( $v = $interval->i >= 1 ) return self::pluralize( $interval->i, 'minute' ) . $suffix; if ($v = $interval->i >= 1) return self::pluralize($interval->i, 'minute') . $suffix;
return self::pluralize( $interval->s, 'second' ) . $suffix; return self::pluralize($interval->s, 'second') . $suffix;
} }
static function pluralize( $count, $text ) static function pluralize($count, $text)
{ {
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;
case '<=': // Less than or equal to case '<=': // Less than or equal to
return $value1 <= $value2; return $value1 <= $value2;
case '>': // Greater than case '>': // Greater than
return $value1 > $value2; return $value1 > $value2;
case '>=': // Greater than or equal to case '>=': // Greater than or equal to
return $value1 >= $value2; return $value1 >= $value2;
case '==': // Equal case '==': // Equal
return ($value1 == $value2); return ($value1 == $value2);
case '===': // Identical case '===': // Identical
return $value1 === $value2; return $value1 === $value2;
case '!==': // Not Identical case '!==': // Not Identical
return $value1 !== $value2; return $value1 !== $value2;
case '!=': // Not equal case '!=': // Not equal
case '<>': // Not equal case '<>': // Not equal
return $value1 != $value2; return $value1 != $value2;
case '||': // Or case '||': // Or
case 'or': // Or case 'or': // Or
return $value1 || $value2; return $value1 || $value2;
case '&&': // And case '&&': // And
case 'and': // And case 'and': // And
return $value1 && $value2; return $value1 && $value2;
case 'xor': // Or case 'xor': // Or
return $value1 xor $value2; return $value1 xor $value2;
default: default:
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'];
}
}
}