update php scripts
This commit is contained in:
@@ -1,11 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once 'wunderground.php';
|
// require_once 'wunderground.php';
|
||||||
|
require_once 'openweathermap.php';
|
||||||
|
|
||||||
define('DEFAULT_WAKE', '6:00');
|
define('DEFAULT_WAKE', '6:00');
|
||||||
define('DEFAULT_SLEEP', '22:00');
|
define('DEFAULT_SLEEP', '22:00');
|
||||||
|
|
||||||
define('TWILIGHT_FILE', 'static/twilight_%d.csv');
|
define('TWILIGHT_FILE', 'static/twilight_%d.csv');
|
||||||
define('STATION', 'IUITGEES3');
|
define('STATION', 'INOORDHO104');
|
||||||
|
define('CITY', 2745978);
|
||||||
|
|
||||||
function getHour($sTime = null) {
|
function getHour($sTime = null) {
|
||||||
if (!is_numeric($sTime)) {
|
if (!is_numeric($sTime)) {
|
||||||
@@ -44,9 +46,11 @@ function getSleep(&$aTwilight = null) {
|
|||||||
return strtotime($sWake = isset($aTwilight) ? $aTwilight[3] : DEFAULT_WAKE);
|
return strtotime($sWake = isset($aTwilight) ? $aTwilight[3] : DEFAULT_WAKE);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getTemperature($sStation = STATION) {
|
function getTemperature($sStation = STATION, $iCity = CITY) {
|
||||||
$aData = wunderground('conditions', sprintf('pws:%s', STATION));
|
// $aData = wunderground('conditions', sprintf('pws:%s', STATION));
|
||||||
return isset($aData['current_observation']['temp_c']) ? $aData['current_observation']['temp_c'] : null;
|
// return isset($aData['current_observation']['temp_c']) ? $aData['current_observation']['temp_c'] : null;
|
||||||
|
$aData = openweathermap(CITY);
|
||||||
|
return isset($aData['main']['temp']) ? floatval($aData['main']['temp']) - 273.15 : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function command($sCommand) {
|
function command($sCommand) {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#!/usr/bin/php
|
#!/usr/bin/php
|
||||||
<?php
|
<?php
|
||||||
#ja
|
|
||||||
require_once 'functions.php';
|
require_once 'functions.php';
|
||||||
require_once 'daemon.php';
|
require_once 'daemon.php';
|
||||||
|
|
||||||
|
|||||||
69
openweathermap.php
Executable file
69
openweathermap.php
Executable file
@@ -0,0 +1,69 @@
|
|||||||
|
<?php
|
||||||
|
define('KEY', 'e8f868de4eb21a7c6a877f8197cc3ed3');
|
||||||
|
define('LIMIT_MINUTE', 10);
|
||||||
|
define('LIMIT_DAY', 500);
|
||||||
|
define('LIMIT_FILE', '/opt/inverter/data/openweathermap.json');
|
||||||
|
|
||||||
|
function openweathermap($iCity, $bDebug = false) {
|
||||||
|
/* Get current date values */
|
||||||
|
$iMinute = date('i');
|
||||||
|
$iDay = date('z');
|
||||||
|
|
||||||
|
if (file_exists(LIMIT_FILE)) {
|
||||||
|
/* Read number of calls used */
|
||||||
|
$sJSON = file_get_contents(LIMIT_FILE);
|
||||||
|
$aJSON = json_decode($sJSON, true);
|
||||||
|
$aCount = array(
|
||||||
|
'minute' => $iMinute != $aJSON['minute'][0] ? 0 : $aJSON['minute'][1],
|
||||||
|
'day' => $iDay != $aJSON['day'][0] ? 0 : $aJSON['day'][1]);
|
||||||
|
} else {
|
||||||
|
/* Initialise to zero */
|
||||||
|
$aCount = array(
|
||||||
|
'minute' => 0,
|
||||||
|
'day' => 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Check call limits */
|
||||||
|
$iWait = 0;
|
||||||
|
if ($aCount['minute'] >= LIMIT_MINUTE) {
|
||||||
|
$iWait = 60 - date('s');
|
||||||
|
if ($bDebug === true) {
|
||||||
|
printf("Minute limit (%d) reached, wait %d seconds\n", LIMIT_MINUTE, $iWait);
|
||||||
|
}
|
||||||
|
$aCount['minute'] = 0;
|
||||||
|
} else if ($aCount['day'] >= LIMIT_DAY) {
|
||||||
|
$iWait = strtotime('00:00 + 1 day') - time();
|
||||||
|
if ($bDebug === true) {
|
||||||
|
printf("Daily limit (%d) reached, wait %d seconds\n", LIMIT_DAY, $iWait);
|
||||||
|
}
|
||||||
|
$aCount['day'] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Prevent from exceeding call limits */
|
||||||
|
if ($iWait > 0) {
|
||||||
|
//die("Try again later!\n");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Update call counts */
|
||||||
|
++$aCount['minute'];
|
||||||
|
++$aCount['day'];
|
||||||
|
|
||||||
|
/* Report number of calls used */
|
||||||
|
if ($bDebug === true) {
|
||||||
|
printf("Used %d/%d minutely and %d/%d daily calls\n", $aCount['minute'], LIMIT_MINUTE, $aCount['day'], LIMIT_DAY);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Write number of calls used to file */
|
||||||
|
$aJSON = array(
|
||||||
|
'minute' => array($iMinute, $aCount['minute']),
|
||||||
|
'day' => array($iDay, $aCount['day']));
|
||||||
|
file_put_contents(LIMIT_FILE, json_encode($aJSON));
|
||||||
|
|
||||||
|
/* Perform actual call */
|
||||||
|
$sUrl = sprintf('https://api.openweathermap.org/data/2.5/weather?id=%d&appid=%s', $iCity, KEY);
|
||||||
|
$sUrl = sprintf('https://api.openweathermap.org/data/2.5/weather?id=%d&appid=%s', $iCity, KEY);
|
||||||
|
// $sUrl = 'https://samples.openweathermap.org/data/2.5/weather?q=Uitgeeddst&appid=5fc7ebf9168bfbe9745920438e3b1';
|
||||||
|
$sJSON = file_get_contents($sUrl);
|
||||||
|
return json_decode($sJSON, true);
|
||||||
|
}
|
||||||
12
weather.php
12
weather.php
@@ -1,7 +1,11 @@
|
|||||||
#!/usr/bin/php
|
#!/usr/bin/php
|
||||||
<?php
|
<?php
|
||||||
require_once 'wunderground.php';
|
// require_once 'wunderground.php';
|
||||||
|
require_once 'openweathermap.php';
|
||||||
|
|
||||||
define('STATION', 'INOORDHO104');
|
// define('STATION', 'INHASSUM4');
|
||||||
$aData = wunderground('conditions', sprintf('pws:%s', STATION));
|
define('CITY', 2745978);
|
||||||
echo $fTemperature = isset($aData['current_observation']['temp_c']) ? $aData['current_observation']['temp_c'] : null;
|
|
||||||
|
// $aData = wunderground('conditions', sprintf('pws:%s', STATION));
|
||||||
|
$aData = openweathermap(CITY);
|
||||||
|
echo $fTemperature = isset($aData['main']['temp']) ? floatval($aData['main']['temp']) - 273.15 : null;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
define('KEY', '2556854ea765c351');
|
define('KEY', '5fc7ebf9168bfbe9745920438e3b17bf');
|
||||||
define('LIMIT_MINUTE', 10);
|
define('LIMIT_MINUTE', 10);
|
||||||
define('LIMIT_DAY', 500);
|
define('LIMIT_DAY', 500);
|
||||||
define('LIMIT_FILE', '/opt/inverter/data/wunderground.json');
|
define('LIMIT_FILE', '/opt/inverter/data/wunderground.json');
|
||||||
|
|||||||
Reference in New Issue
Block a user