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/log', 'ServerApi@logStatus');
$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
$router->post('/cron/clean', 'CronApi@clean');
#$router->post('/cron/fetch', 'CronApi@fetch');
//Google Home - API
$router->any('/api/HA/auth', 'Oauth');

View File

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

View File

@ -1,10 +1,12 @@
<?php
/**
*
*/
*
*/
class Utilities
{
static function cleanString($text) {
static function cleanString($text)
{
$utf8 = array(
'/[áàâãªä]/u' => 'a',
'/[ÁÀÂÃÄ]/u' => 'A',
@ -36,7 +38,7 @@ class Utilities
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);
return $str;
@ -50,7 +52,8 @@ class Utilities
* @return [type] [description]
*/
static function generateGraphJson(string $type = 'line', array $data = [], array $options = []){
static function generateGraphJson(string $type = 'line', array $data = [], array $options = [])
{
$array = [
'type' => $type,
'data' => [
@ -94,23 +97,24 @@ class Utilities
return json_encode($array, JSON_PRETTY_PRINT);
}
static function ago( $datetime )
static function ago($datetime)
{
$interval = date_create('now')->diff( $datetime );
$suffix = ( $interval->invert ? ' ago' : '' );
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->h >= 1 ) return self::pluralize( $interval->h, 'hour' ) . $suffix;
if ( $v = $interval->i >= 1 ) return self::pluralize( $interval->i, 'minute' ) . $suffix;
return self::pluralize( $interval->s, 'second' ) . $suffix;
$interval = date_create('now')->diff($datetime);
$suffix = ($interval->invert ? ' ago' : '');
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->h >= 1) return self::pluralize($interval->h, 'hour') . $suffix;
if ($v = $interval->i >= 1) return self::pluralize($interval->i, 'minute') . $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) {
case '<': // Less than
return $value1 < $value2;
@ -141,4 +145,37 @@ class Utilities
return FALSE;
} // 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'];
}
}
}