1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JarvisPHP\Plugins\Wemo_plugin; |
4
|
|
|
|
5
|
|
|
use JarvisPHP\Core\JarvisSession; |
6
|
|
|
use JarvisPHP\Core\JarvisPHP; |
7
|
|
|
use JarvisPHP\Core\JarvisLanguage; |
8
|
|
|
use JarvisPHP\Core\JarvisTTS; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* A IFTTT Plugin for my Wemo Insight Switch |
12
|
|
|
* @author Stefano Bianchini |
13
|
|
|
* @website http://www.stefanobianchini.net |
14
|
|
|
*/ |
15
|
|
|
class Wemo_plugin implements \JarvisPHP\Core\JarvisPluginInterface{ |
16
|
|
|
/** |
17
|
|
|
* Priority of plugin |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
var $priority = 2; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* the behaviour of plugin |
24
|
|
|
* @param string $command |
25
|
|
|
*/ |
26
|
|
|
function answer($command) { |
|
|
|
|
27
|
|
|
$answer = ''; |
|
|
|
|
28
|
|
|
JarvisPHP::getLogger()->debug('Answering to command: "'.$command.'"'); |
29
|
|
|
|
30
|
|
|
$_IFTTT_MAKER_KEY = ''; |
31
|
|
|
$_IFTTT_MAKER_EVENT = ''; |
32
|
|
|
|
33
|
|
|
//Load API key from json config |
34
|
|
|
if(file_exists('Plugins/Wemo_plugin/api-key.json')) { |
35
|
|
|
//Create your own api key and put it in api-key.json |
36
|
|
|
$json_config = json_decode(file_get_contents('Plugins/Wemo_plugin/api-key.json')); |
37
|
|
|
$_IFTTT_MAKER_KEY = $json_config->ifttt_key; |
38
|
|
|
$_IFTTT_MAKER_EVENT = $json_config->ifttt_event; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
//Toggle switch via curl |
42
|
|
|
$ch = curl_init(); |
43
|
|
|
curl_setopt($ch, CURLOPT_URL, 'https://maker.ifttt.com/trigger/'.$_IFTTT_MAKER_EVENT.'/with/key/'.$_IFTTT_MAKER_KEY); |
44
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0'); |
45
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
46
|
|
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
47
|
|
|
curl_setopt($ch, CURLOPT_FAILONERROR, true); |
48
|
|
|
curl_setopt($ch, CURLOPT_VERBOSE, TRUE); |
49
|
|
|
curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTPS); |
50
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
51
|
|
|
$headers = array(); |
52
|
|
|
$headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'; |
53
|
|
|
$headers[] = 'Accept-Encoding: gzip, deflate'; |
54
|
|
|
$headers[] = 'Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3'; |
55
|
|
|
$headers[] = 'Cache-Control: no-cache'; |
56
|
|
|
$headers[] = 'Host: maker.ifttt.com'; |
57
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
58
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, array()); |
59
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
60
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
61
|
|
|
$result = curl_exec($ch); |
62
|
|
|
|
63
|
|
|
if(!$result) { |
64
|
|
|
JarvisPHP::getLogger()->error('Curl error: '.curl_error($ch)); |
65
|
|
|
$answer = JarvisLanguage::translate('command_not_sent',get_called_class()); |
66
|
|
|
} |
67
|
|
|
else { |
68
|
|
|
$answer = JarvisLanguage::translate('command_sent_to_light_switch',get_called_class()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
curl_close($ch); |
72
|
|
|
|
73
|
|
|
JarvisTTS::speak($answer); |
74
|
|
|
$response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true); |
75
|
|
|
$response->send(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get plugin's priority |
80
|
|
|
* @return int |
81
|
|
|
*/ |
82
|
|
|
function getPriority() { |
|
|
|
|
83
|
|
|
return $this->priority; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Is it the right plugin for the command? |
88
|
|
|
* @param string $command |
89
|
|
|
* @return boolean |
90
|
|
|
*/ |
91
|
|
|
function isLikely($command) { |
|
|
|
|
92
|
|
|
return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Does the plugin need a session? |
97
|
|
|
* @return boolean |
98
|
|
|
*/ |
99
|
|
|
function hasSession() { |
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.