1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JarvisPHP\Plugins\Weather_plugin; |
4
|
|
|
|
5
|
|
|
use JarvisPHP\Core\JarvisSession; |
6
|
|
|
use JarvisPHP\Core\JarvisPHP; |
7
|
|
|
use JarvisPHP\Core\JarvisLanguage; |
8
|
|
|
use JarvisPHP\Core\JarvisTTS; |
9
|
|
|
//OpenWeatherMap |
10
|
|
|
use Cmfcmf\OpenWeatherMap; |
11
|
|
|
use Cmfcmf\OpenWeatherMap\Exception as OWMException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A Weather plugin for today / tomorrow forecast |
15
|
|
|
* @author Stefano Bianchini |
16
|
|
|
* @website http://www.stefanobianchini.net |
17
|
|
|
*/ |
18
|
|
|
class Weather_plugin implements \JarvisPHP\Core\JarvisPluginInterface{ |
19
|
|
|
/** |
20
|
|
|
* Priority of plugin |
21
|
|
|
* @var int |
22
|
|
|
*/ |
23
|
|
|
var $priority = 2; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* the behaviour of plugin |
27
|
|
|
* @param string $command |
28
|
|
|
*/ |
29
|
|
|
function answer($command) { |
|
|
|
|
30
|
|
|
$answer = ''; |
|
|
|
|
31
|
|
|
JarvisPHP::getLogger()->debug('Answering to command: "'.$command.'"'); |
32
|
|
|
|
33
|
|
|
$owm = new OpenWeatherMap(); |
34
|
|
|
|
35
|
|
|
$tomorrow = new \DateTime(); |
36
|
|
|
$tomorrow->modify('+1 day'); |
37
|
|
|
|
38
|
|
|
$_OPENWEATHERMAP_API_KEY = ''; |
39
|
|
|
|
40
|
|
|
//Load API key from json config |
41
|
|
|
if(file_exists('Plugins/Weather_plugin/api-key.json')) { |
42
|
|
|
//Create your own api key and put it in api-key.json |
43
|
|
|
$json_config = json_decode(file_get_contents('Plugins/Weather_plugin/api-key.json')); |
44
|
|
|
$_OPENWEATHERMAP_API_KEY = $json_config->openweathermap_key; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
$forecast = $owm->getWeatherForecast('Rimini', 'metric', _LANGUAGE, $_OPENWEATHERMAP_API_KEY,2); |
49
|
|
|
} catch(OWMException $e) { |
50
|
|
|
JarvisPHP::getLogger()->error('OpenWeatherMap exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'); |
51
|
|
|
$answer = JarvisLanguage::translate('weather_error',get_called_class()); |
|
|
|
|
52
|
|
|
} catch(\Exception $e) { |
53
|
|
|
JarvisPHP::getLogger()->error('General exception: ' . $e->getMessage() . ' (Code ' . $e->getCode() . ').'); |
54
|
|
|
$answer = JarvisLanguage::translate('weather_error',get_called_class()); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if(preg_match(JarvisLanguage::translate('preg_match_today',get_called_class()), $command)) { |
58
|
|
|
$answer = JarvisLanguage::translate('forecast_for_today',get_called_class()); |
59
|
|
View Code Duplication |
foreach ($forecast as $weather) { |
|
|
|
|
60
|
|
|
if($weather->time->day->format('d.m.Y')==date('d.m.Y')) { |
61
|
|
|
$answer.=JarvisLanguage::translate('from',get_called_class()) . " " .$weather->time->from->format('H:i') . " ". JarvisLanguage::translate('to',get_called_class()) . " " . $weather->time->to->format('H:i').": "; |
62
|
|
|
$answer.=$weather->weather->description." ".sprintf(JarvisLanguage::translate('temperature',get_called_class()),trim(str_replace('°C','',$weather->temperature))); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} else if(preg_match(JarvisLanguage::translate('preg_match_tomorrow',get_called_class()), $command)) { |
66
|
|
|
$answer = JarvisLanguage::translate('forecast_for_tomorrow',get_called_class()); |
67
|
|
View Code Duplication |
foreach ($forecast as $weather) { |
|
|
|
|
68
|
|
|
if($weather->time->day->format('d.m.Y')==$tomorrow->format('d.m.Y')) { |
69
|
|
|
$answer.=JarvisLanguage::translate('from',get_called_class()) . " " .$weather->time->from->format('H:i') . " ". JarvisLanguage::translate('to',get_called_class()) . " " . $weather->time->to->format('H:i').": "; |
70
|
|
|
$answer.=$weather->weather->description." ".sprintf(JarvisLanguage::translate('temperature',get_called_class()),trim(str_replace('°C','',$weather->temperature))); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} else { |
74
|
|
|
$answer = JarvisLanguage::translate('not_understand_weather_day',get_called_class()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
|
79
|
|
|
JarvisTTS::speak($answer); |
80
|
|
|
$response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true); |
81
|
|
|
$response->send(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get plugin's priority |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
|
|
function getPriority() { |
|
|
|
|
89
|
|
|
return $this->priority; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Is it the right plugin for the command? |
94
|
|
|
* @param string $command |
95
|
|
|
* @return boolean |
96
|
|
|
*/ |
97
|
|
|
function isLikely($command) { |
|
|
|
|
98
|
|
|
return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command); |
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Does the plugin need a session? |
103
|
|
|
* @return boolean |
104
|
|
|
*/ |
105
|
|
|
function hasSession() { |
|
|
|
|
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.