|
55 | 55 | if ( isset( $DATA["rid"] ) ){ $DATA = array( $DATA ); }
|
56 | 56 |
|
57 | 57 | foreach($DATA as $room){
|
| 58 | + $RoomName = str_replace(' ', '', $room['name']); |
58 | 59 | if( isset($room['rid'] ) ){
|
59 | 60 | $DEVICES = array();
|
60 | 61 |
|
61 | 62 | if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
|
62 | 63 | $file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
|
63 |
| - $data1 = "\n### ".$room['name']." \ndef on_message_".$room['name']."(client, userdata, msg):\n"; |
64 |
| - $data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$room['name']."\" + msg.payload.decode())\n"; |
| 64 | + $data1 = "\n### ".$RoomName." \ndef on_message_".$RoomName."(client, userdata, msg):\n"; |
| 65 | + $data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$RoomName."\" + msg.payload.decode())\n"; |
65 | 66 | $data3 = " r = requests.get('".LOCAL_URL."/api.php?fx=toggle&type=room&uid=".$room['rid']."&val=' + msg.payload.decode())\n";
|
66 | 67 | $data4 = " r.json()\n";
|
67 |
| - $data5 = " client.publish(\"".$MQTT_prefix."/".$room['name']."/".$room['rid']."/status\", msg.payload.decode()"; |
| 68 | + $data5 = " client.publish(\"".$MQTT_prefix."/".$RoomName."/".$room['rid']."/status\", msg.payload.decode()"; |
68 | 69 | $data6 = ", 0, True)\n";
|
69 |
| - $data7 = "\ndef on_message_".$room['name']."_Bright(client, userdata, msg):\n"; |
70 |
| - $data8 = " print (\"".$room['name']." Brightness \" + msg.payload.decode())\n"; |
| 70 | + $data7 = "\ndef on_message_".$RoomName."_Bright(client, userdata, msg):\n"; |
| 71 | + $data8 = " print (\"".$RoomName." Brightness \" + msg.payload.decode())\n"; |
71 | 72 | $data9 = " r = requests.get('".LOCAL_URL."/api.php?fx=dim&type=room&uid=".$room['rid']."&val=' + msg.payload.decode())\n";
|
72 | 73 | $data10 = " r.json()\n\n";
|
73 |
| - $data11 = " client.publish(\"".$MQTT_prefix."/".$room['name']."/".$room['rid']."/brightness\", msg.payload.decode()"; |
| 74 | + $data11 = " client.publish(\"".$MQTT_prefix."/".$RoomName."/".$room['rid']."/brightness\", msg.payload.decode()"; |
74 | 75 | $data12 = ", 0, True)\n";
|
75 | 76 |
|
76 | 77 | fwrite($file_handle, $data1);
|
|
115 | 116 | $roomDevices = 0;
|
116 | 117 | if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
|
117 | 118 | foreach($DEVICES as $device){
|
| 119 | + $DeviceName = str_replace(' ', '', $device['name']); |
118 | 120 | $file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
|
119 |
| - $data1 = "\n### ".$room['name']."-".$device['name']." \ndef on_message_".$room['name']."_".$device['name']."(client, userdata, msg):\n"; |
120 |
| - $data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$room['name']." ".$device['name']."\" + msg.payload.decode())\n"; |
| 121 | + $data1 = "\n### ".$RoomName."-".$DeviceName." \ndef on_message_".$RoomName."_".$DeviceName."(client, userdata, msg):\n"; |
| 122 | + $data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$RoomName." ".$DeviceName."\" + msg.payload.decode())\n"; |
121 | 123 | $data3 = " r = requests.get('".LOCAL_URL."/api.php?fx=toggle&type=device&uid=".$device['did']."&val=' + msg.payload.decode())\n";
|
122 | 124 | $data4 = " r.json()\n";
|
123 |
| - $data5 = " client.publish(\"".$MQTT_prefix."/".$room['name']."/".$device['name']."/".$device['did']."/status\", msg.payload.decode()"; |
| 125 | + $data5 = " client.publish(\"".$MQTT_prefix."/".$RoomName."/".$DeviceName."/".$device['did']."/status\", msg.payload.decode()"; |
124 | 126 | $data6 = ", 0, True)\n";
|
125 |
| - $data7 = "\ndef on_message_".$room['name']."_".$device['name']."_Bright(client, userdata, msg):\n"; |
126 |
| - $data8 = " print (\"".$room['name']." ".$device['name']." Brightness \" + msg.payload.decode())\n"; |
| 127 | + $data7 = "\ndef on_message_".$RoomName."_".$DeviceName."_Bright(client, userdata, msg):\n"; |
| 128 | + $data8 = " print (\"".$RoomName." ".$DeviceName." Brightness \" + msg.payload.decode())\n"; |
127 | 129 | $data9 = " r = requests.get('".LOCAL_URL."/api.php?fx=dim&type=device&uid=".$device['did']."&val=' + msg.payload.decode())\n";
|
128 | 130 | $data10 = " r.json()\n\n";
|
129 |
| - $data11 = " client.publish(\"".$MQTT_prefix."/".$room['name']."/".$device['name']."/".$device['did']."/brightness\", msg.payload.decode()"; |
| 131 | + $data11 = " client.publish(\"".$MQTT_prefix."/".$RoomName."/".$DeviceName."/".$device['did']."/brightness\", msg.payload.decode()"; |
130 | 132 | $data12 = ", 0, True)\n";
|
131 | 133 |
|
132 | 134 | fwrite($file_handle, $data1);
|
|
163 | 165 | $scenes = $sarray["scene"];
|
164 | 166 | if( is_array($scenes) ){
|
165 | 167 | foreach($scenes as $scene){
|
| 168 | + $SceneName = str_replace(' ', '', $scene['name']); |
166 | 169 | // for($x = 0; $x < sizeof($scenes); $x++){
|
167 | 170 | if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
|
168 | 171 | $file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
|
169 |
| - $data1 = "\n### ".$scene['name']." \ndef on_message_".$scene['name']."(client, userdata, msg):\n"; |
170 |
| - $data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$scene['name']."\" + msg.payload.decode())\n"; |
| 172 | + $data1 = "\n### ".$SceneName." \ndef on_message_".$SceneName."(client, userdata, msg):\n"; |
| 173 | + $data2 = " if (msg.payload.decode() == '0' or msg.payload.decode() == '1'):\n print (\"".$SceneName."\" + msg.payload.decode())\n"; |
171 | 174 | $data3 = " r = requests.get('".LOCAL_URL."/api.php?fx=scene&uid=".$scene['sid']."&type=' + msg.payload.decode())\n";
|
172 | 175 | $data4 = " r.json()\n";
|
173 |
| - $data5 = " client.publish(\"".$MQTT_prefix."/".$scene['name']."/".$scene['sid']."/status\", msg.payload.decode()"; |
| 176 | + $data5 = " client.publish(\"".$MQTT_prefix."/".$SceneName."/".$scene['sid']."/status\", msg.payload.decode()"; |
174 | 177 | $data6 = ", 0, True)\n";
|
175 | 178 |
|
176 | 179 | fwrite($file_handle, $data1);
|
|
202 | 205 | if ( isset( $DATA["rid"] ) ){ $DATA = array( $DATA ); }
|
203 | 206 |
|
204 | 207 | foreach($DATA as $room){
|
| 208 | + $RoomName = str_replace(' ', '', $room['name']); |
205 | 209 | if( isset($room['rid'] ) ){
|
206 | 210 | $DEVICES = array();
|
207 | 211 |
|
208 | 212 | if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
|
209 | 213 | foreach($DEVICES as $device){
|
210 | 214 | $file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
|
211 |
| - $data1 = "\n### ".$room['name']." Begin\n"; |
212 |
| - $data2 = "client.message_callback_add('".$MQTT_prefix."/".$room['name']."/".$room['rid']."/switch', on_message_".$room['name'].")\n"; |
213 |
| - $data3 = "client.message_callback_add('".$MQTT_prefix."/".$room['name']."/".$room['rid']."/brightness/set', on_message_".$room['name']."_Bright)\n"; |
| 215 | + $data1 = "\n### ".$RoomName." Begin\n"; |
| 216 | + $data2 = "client.message_callback_add('".$MQTT_prefix."/".$RoomName."/".$room['rid']."/switch', on_message_".$RoomName.")\n"; |
| 217 | + $data3 = "client.message_callback_add('".$MQTT_prefix."/".$RoomName."/".$room['rid']."/brightness/set', on_message_".$RoomName."_Bright)\n"; |
214 | 218 | fwrite($file_handle, $data1);
|
215 | 219 | fwrite($file_handle, $data2);
|
216 | 220 | fwrite($file_handle, $data3);
|
|
245 | 249 | $roomDevices = 0;
|
246 | 250 | if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
|
247 | 251 | foreach($DEVICES as $device){
|
| 252 | + $DeviceName = str_replace(' ', '', $device['name']); |
248 | 253 | $file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
|
249 |
| - $data1 = "\n### ".$room['name']."-".$device['name']." Begin\n"; |
250 |
| - $data2 = "client.message_callback_add('".$MQTT_prefix."/".$room['name']."/".$device['name']."/".$device['did']."/switch', on_message_".$room['name']."_".$device['name'].")\n"; |
251 |
| - $data3 = "client.message_callback_add('".$MQTT_prefix."/".$room['name']."/".$device['name']."/".$device['did']."/brightness/set', on_message_".$room['name']."_".$device['name']."_Bright)\n"; |
| 254 | + $data1 = "\n### ".$RoomName."-".$device['name']." Begin\n"; |
| 255 | + $data2 = "client.message_callback_add('".$MQTT_prefix."/".$RoomName."/".$DeviceName."/".$device['did']."/switch', on_message_".$RoomName."_".$DeviceName.")\n"; |
| 256 | + $data3 = "client.message_callback_add('".$MQTT_prefix."/".$RoomName."/".$DeviceName."/".$device['did']."/brightness/set', on_message_".$RoomName."_".$DeviceName."_Bright)\n"; |
252 | 257 | fwrite($file_handle, $data1);
|
253 | 258 | fwrite($file_handle, $data2);
|
254 | 259 | fwrite($file_handle, $data3);
|
|
277 | 282 | $scenes = $sarray["scene"];
|
278 | 283 | if( is_array($scenes) ){
|
279 | 284 | foreach($scenes as $scene){
|
| 285 | + $SceneName = str_replace(' ', '', $scene['name']); |
280 | 286 | // for($x = 0; $x < sizeof($scenes); $x++){
|
281 | 287 | if ($mqtt->connect(true, NULL, $MQTTusername, $MQTTpassword)) {
|
282 | 288 | $file_handle = fopen('mqtt_sub.py', 'a') or die('Error opening file.');
|
283 | 289 | $data1 = "\n### ".$scene['name']." Begin\n";
|
284 |
| - $data2 = "client.message_callback_add('".$MQTT_prefix."/".$scene['name']."/".$scene['sid']."/switch', on_message_".$scene['name'].")\n"; |
| 290 | + $data2 = "client.message_callback_add('".$MQTT_prefix."/".$SceneName."/".$scene['sid']."/switch', on_message_".$scene['name'].")\n"; |
285 | 291 | fwrite($file_handle, $data1);
|
286 | 292 | fwrite($file_handle, $data2);
|
287 | 293 | fclose($file_handle);
|
|
0 commit comments