-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifttt_link.php
162 lines (111 loc) · 4.06 KB
/
ifttt_link.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
include_once 'core.php';
autOnly();
if(isset($_GET['short_name']) && isset($_GET['action']))
{
$short_name = mysql_real_escape_string($_GET['short_name']);
$action = $_GET['action'] == 'on' ? 'on' : 'off';
switchRelay($short_name, $action, ' (с сайта)', true);
header('Location: index.php?'.$short_name.'_turned_'.$action.'=1&');
}
/* LEGACY
if(isset($_GET['id_device']) && isset($_GET['action']))
{
$unixtime = time();
$strtime = date('d.m.Y H:i:s', $unixtime);
switch($_GET['action'])
{
case 'on': $action = 'on'; $act = 1; $ru_action = 'вкл.'; break;
case 'off': $action = 'off'; $act = 0; $ru_action = 'выкл.'; break;
default: $error = 'wrong_action'; break;
}
$id_device = (int)$_GET['id_device'];
$q_device = mysql_query("SELECT * FROM `ewelink_devices` WHERE `id` = ".$id_device);
// собираем базу комнат
$q_rooms = mysql_query("SELECT * FROM `rooms`");
$_ROOMS = array();
while($_tmp = mysql_fetch_assoc($q_rooms))
{
$_ROOMS[$_tmp['id']] = $_tmp['name'];
}
if(mysql_num_rows($q_device) != 1)
{
$error = 'null_answer';
}
if(!empty($error))
{
header('Location: index.php?error='.$error);
exit;
}
$_DEVICE = mysql_fetch_assoc($q_device);
#1) Делаем запрос к IFTTT
#2) Записываем событие в свою БД
#3) Шлем сообщение в Telegram
#4) Редиректим назад
// запрос к ifttt
$link = 'https://maker.ifttt.com/trigger/ewelink_'.$_DEVICE['short_name'].'_'.$action.'/with/key/'.$_IFTTT['key'];
// die($link);
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHPscript for Smarthome by D13410N3/1.0');
$a = curl_exec($ch);
// echo $a;
// запись в БД
mysql_query("INSERT INTO `ewelink_events`(`id_device`, `time`, `action`) VALUES (".$_DEVICE['id'].", ".$unixtime.", ".$act.")");
// работа с tg
$message = '<b>'.$_DEVICE['full_name'].'</b> | '.$_ROOMS[$_DEVICE['id_room']].' <i>('.$strtime.')</i>'.PHP_EOL;
// определяем свет или просто подача питания
if($_DEVICE['type'] == 'light')
{
$message .= 'свет ';
}
else
{
$message .= 'устройство ';
}
$message .= '<i>'.$ru_action.'</i> (с сайта)';
if($act == 0)
{
$q_last = mysql_query("SELECT * FROM `ewelink_events` WHERE `id_device` = ".$_DEVICE['id']." AND `action` = 1 ORDER BY `id` DESC LIMIT 1");
// die(mysql_error());
if(mysql_num_rows($q_last) == 0)
{
$string_duration = 'Не найдено время включения';
}
else
{
$last_session = mysql_fetch_assoc($q_last);
$duration = $unixtime - $last_session['time'];
$days = floor($duration / 86400);
$hours = floor(($duration - $days * 86400) / 3600);
$minutes = floor(($duration - $days * 86400 - $hours * 3600) / 60);
$seconds = $duration - $days * 86400 - $hours * 3600 - $minutes * 60;
$string_duration = 'Продолжительность сессии: ';
if($days > 0)
{
$string_duration .= $days.' дн. ';
}
if($hours > 0)
{
$string_duration .= $hours.' ч. ';
}
if($minutes > 0)
{
$string_duration .= $minutes.' мин. ';
}
if($seconds > 0)
{
$string_duration .= $seconds.' сек.';
}
}
// контактен....ция строк
$message .= PHP_EOL;
$message .= $string_duration;
}
sendMessage($_CHAT['id'], $message, 'HTML');
// редиректим взад
header('Location: index.php?turned_'.$action.'=1&');
exit;
}
*/