-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqttWrapper.php
164 lines (144 loc) · 3.27 KB
/
mqttWrapper.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
163
164
<?php
include("resources/phpMQTT.php");
error_reporting(0);
$myServer = "not set";
$myPort = "not set";
$myUserName = "not set";
$myPassword = "not set";
$myIdentifier = "PHP MQTT Client";
if (validParam("server")) { $myServer = $_GET["server"]; }
if (validParam("port")) { $myPort = $_GET["port"]; }
if (validParam("userName")) { $myUserName = $_GET["userName"]; }
if (validParam("password")) { $myPassword = $_GET["password"]; }
if (validParam("action"))
{
switch($_GET["action"])
{
case "getTopic": getTopic(); break;
case "pubMess": pubMess(); break;
default:
displayJson("invalid action", "", ""); break;
}
}
function getMQTT()
{
global $myServer, $myPort, $myUserName, $myPassword, $myIdentifier;
if(($myServer == "not set" || $myPort == "not set") ||
($myUserName != "not set" && $myPassword == "not set" ))
{
// invalid details
return null;
}
else if($myUserName == "not set" && $myPassword == "not set" )
{
// connected with no ACL
return new phpMQTT($myServer, $myPort, $myIdentifier, "", "");
}
else
{
// connected with ACL
return new phpMQTT($myServer, $myPort, $myIdentifier,
$myUserName, $myPassword);
}
}
function validParam($paramName)
{
if(isset($_GET[$paramName]) && strlen(trim($_GET[$paramName])) > 0)
{
return true;
}
else
{
return false;
}
}
function getTopic()
{
if (validParam("topic"))
{
$myTopic = $_GET["topic"];
if(($mqtt = getMQTT()) == null)
{
displayJson("invalid connection details", $myTopic, "");
return -1;
}
else
{
// connected to MQTT
}
if ($mqtt->connect())
{
$topics[$myTopic] = array("qos"=>0, "function"=>"procmsg");
$mqtt->subscribe($topics);
if($mqtt->proc() == 0)
{
displayJson("no message", $myTopic, "");
}
else
{
//displayJson("unknown error", $myTopic, "");
}
$mqtt->close();
}
else
{
displayJson("failed to connect", $myTopic, "");
}
}
else
{
displayJson("invalid topic", "", "");
}
}
function procmsg($topic,$message)
{
displayJson("ok", $topic, $message);
}
function displayJson($status, $topic, $message)
{
global $myServer, $myPort, $myUserName, $myPassword, $myIdentifier;
$message = array ( "status" => $status,
"topic" => $topic, "message" => $message,
"server" => $myServer, "port"=>$myPort,
"userName"=>$myUserName, "password"=>$myPassword);
echo json_encode($message);
}
function pubMess()
{
$myTopic = $_GET["topic"];
$myMessage = $_GET["message"];
$myRetain = $_GET["retain"];
if($myRetain == "true") $myRetain = 1;
else $myRetain = 0;
$myQos = 0;
if (!validParam("message") & !validParam("topic"))
{
displayJson("invalid message & topic", "", "");
}
else if (!validParam("message"))
{
displayJson("invalid message", $myTopic, "");
}
else if (!validParam("topic"))
{
displayJson("invalid topic", "", $myTopic);
}
else
{
if(($mqtt = getMQTT()) == null)
{
displayJson("invalid connection details", $myTopic, $myMessage);
return -1;
}
if ($mqtt->connect())
{
$mqtt->publish($myTopic,$myMessage,$myQos,$myRetain);
displayJson("ok", $myTopic, $myMessage);
}
else
{
displayJson("failed to connect", $myTopic, $myMessage);
}
}
}
?>