-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.php
146 lines (117 loc) · 3.97 KB
/
tasks.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
<?php
/*
*Project: eYSIP_2015_IoT-Connected-valves-for-irrigation-of-greenhouse
*Team members: Jayant Solanki, Kevin D'Souza
*File name: tasks.php
*Author: Jayant Solanki
*Runs continously in cli mode , executing the scheduled tasks based upon start and stop time
*
*/
include 'iotdb.php';
require(__DIR__ . '/spMQTT.class.php');
date_default_timezone_set('Asia/Kolkata');//setting IST
spMQTTDebug::Enable();
mysql_select_db($dbname) or die(mysql_error());
$query="SELECT * FROM tasks";
//echo $query;
$results=mysql_query($query);
if (mysql_num_rows($results) > 0)
{
echo "</br>Executing tasks</br>";
while($row = mysql_fetch_assoc($results))
{
$id=$row['id'];
$start=$row['start'];
$stop=$row['stop'];
$action=$row['action'];
$currenttime=date('Hi');
//echo $currenttime;
if($start!=NULL)
{
if($currenttime>=$start and $currenttime<$stop and $action==1)
{
$task="SELECT * FROM devices"; //starting every valves,,// problem here why is it not selecting dvices from a particular group????
$result=mysql_query($task);
if (mysql_num_rows($result) > 0)
{
while($rows = mysql_fetch_assoc($result))
{
$macid=$rows['macid'];
command($macid,$action); //switch
//echo "Switch OFF"; //update button status
}
}
$query = "UPDATE tasks SET action ='0' WHERE id='$id'";
$action=0;//doing this because dont want to again fetch from table
//setting start NUll so that it wont check
echo "</br>".$query;
if(!mysql_query($query,mysql_connect($dbhost, $dbuser, $dbpass)))
echo "INSERT failed: $query<br/>".mysql_error()."<br/><br/>";
else
echo "</br>Task upadated to stop</br>";
$query="UPDATE devices SET action='1'"; //this is for updating running status off devices,//problem here
echo "</br>".$query;
if(!mysql_query($query,mysql_connect($dbhost, $dbuser, $dbpass)))
echo "INSERT failed: $query<br/>".mysql_error()."<br/><br/>";
}
}
if($stop!=NULL)
{
if($currenttime==0000)
$currenttime=2400; //2400 is same as 1200am or 0000
if($currenttime>=$stop and $action==0)
{
$task="SELECT * FROM devices"; //stopping every valves
$result=mysql_query($query);
if (mysql_num_rows($result) > 0)
{
while($rows = mysql_fetch_assoc($results))
{
$macid=$rows['macid'];
command($macid,$action); //switch
//echo "Switch ON"; //update button status
}
}
$query = "UPDATE tasks SET action ='1' WHERE id='$id'"; //resetting to 1 for next day execution
echo $query;
if(!mysql_query($query,mysql_connect($dbhost, $dbuser, $dbpass)))
echo "INSERT failed: $query<br/>".mysql_error()."<br/><br/>";
else
echo "</br>Task upadated for next day</br>";
$query="UPDATE devices SET action='0'"; //this is for updating running status off devices
echo "</br>".$query;
if(!mysql_query($query,mysql_connect($dbhost, $dbuser, $dbpass)))
echo "INSERT failed: $query<br/>".mysql_error()."<br/><br/>";
}
}
}
echo "Done executing";
}
else
{
echo "No scheduled tasks exist";
}
/*
*
* Function Name: command($macid,$action)
* Input: $ macid for macid, and $action for defining 0/1 for OFF/ON commands
* Output: publish ON/OFF commands to esp device.
* each msg has macid, which will enable the script to generate a macid based topic(esp/macid)
* Logic: msg format is 0, 1, 2, for OFF, ON and battery status.
*
*
*/
function command($macid,$action) //for sending mqtt commands
{
//$mqtt->setAuth('sskaje', '123123');
$mqtt = new spMQTT('tcp://127.0.0.1:1883/');
$connected = $mqtt->connect();
if (!$connected) {
die(" Mosca MQTT Server is Offline\n");
}
$mqtt->ping();
$msg = str_repeat($action, 1);
//echo "</br>esp/valve/".$macid;
$mqtt->publish('esp/valve/'.$macid, $msg, 0, 1, 0, 1);
echo "</br>Success";
}