Skip to content

Commit

Permalink
Update cycle_connect.php
Browse files Browse the repository at this point in the history
Connect cycle hang fix
  • Loading branch information
sergejey committed Dec 28, 2020
1 parent 81f5b58 commit 8bba2f7
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion scripts/cycle_connect.php
Expand Up @@ -63,18 +63,20 @@
$ca_file = dirname(__FILE__) . '/../modules/connect/fullchain.pem';
}


$query = $username . '/incoming_urls,' . $username . '/menu_session,' . $username . '/reverse_requests';
$ping_topic = $username . '/ping';
$client_name = "MajorDoMo " . $username . " Connect";
$mqtt_client = new Bluerhinos\phpMQTT($host, $port, $client_name, $ca_file);

echo date('H:i:s') . " Connecting to $host:$port\n";
DebMes("Connecting to $host:$port",'connect');
if ($mqtt_client->connect(true, NULL, $username, $password)) {

$query_list = explode(',', $query);
$total = count($query_list);
echo date('H:i:s') . " Topics to watch: $query (Total: $total)\n";
setGlobal((str_replace('.php', '', basename(__FILE__))) . 'Run', time(), 1);
DebMes("CONNECTED.",'connect');
$topics = array();
for ($i = 0; $i < $total; $i++) {
$path = trim($query_list[$i]);
Expand All @@ -83,6 +85,7 @@
}
foreach ($topics as $k => $v) {
echo date('H:i:s') . " Subscribing to: $k\n";
DebMes("Subscribing to $k",'connect');
}
$mqtt_client->subscribe($topics, 0);
$ping_timestamp = 0;
Expand All @@ -97,20 +100,26 @@
exit;
}
}


if (!defined('DISABLE_SIMPLE_DEVICES')) {
//$saved_devices_data
$devices_data = checkOperationsQueue('connect_device_data');
foreach ($devices_data as $property_data) {
if (!isset($saved_devices_data[$property_data['DATANAME']]) || $saved_devices_data[$property_data['DATANAME']] != $property_data['DATAVALUE']) {
$saved_devices_data[$property_data['DATANAME']] = $property_data['DATAVALUE'];
//DebMes("Sending value of ".$property_data['DATANAME']. "(".$property_data['DATAVALUE'].")",'connect');
$connect->sendDeviceProperty($property_data['DATANAME'], $property_data['DATAVALUE']);
//DebMes("Sending value OK",'connect');
}
}
$sync_required = checkOperationsQueue('connect_sync_devices');
if ((time() - $devices_sent_time > 60 * 60) || is_array($sync_required[0])) {
$devices_sent_time = time();
echo date('Y-m-d H:i:s') . " Sending all devices\n";
//DebMes("Sending all devices",'connect');
$connect->sendAllDevices();
//DebMes("All devices sent OK",'connect');
}
}
/*
Expand All @@ -120,19 +129,27 @@
}
*/
if ((time() - $started_time) > $max_run_time) {
DebMes("Running too long, exit.",'connect');
echo "Exit cycle CONNECT... (reconnecting)";
$mqtt_client->close();
exit;
}
if ((time() - $ping_timestamp) > 60) {
//DebMes("Sending PING to MQTT",'connect');
$ping_timestamp = time();
set_time_limit(10);
$mqtt_client->publish($ping_topic, time());
set_time_limit(0);
//DebMes("Sending PING OK",'connect');
}
}
DebMes("Closing MQTT connection",'connect');
$mqtt_client->close();

} else {
echo date('Y-m-d H:i:s') . " Failed to connect ...\n";
setGlobal((str_replace('.php', '', basename(__FILE__))) . 'Run', time(), 1);
DebMes("Failed to connect to MQTT",'connect');
sleep(10);
continue;
}
Expand All @@ -141,32 +158,41 @@
function procmsg($topic, $msg)
{
echo date("Y-m-d H:i:s") . " Topic:{$topic} $msg\n";
//DebMes("Processing incoming topic: $topic",'connect');
if (preg_match('/menu_session/is', $topic)) {
echo date("Y-m-d H:i:s") . " Menu session\n";
$parent_id = $msg;
if ($parent_id == 0) {
global $menu_sent_time;
if ((time() - $menu_sent_time) > 30) {
$menu_sent_time = time();
//DebMes("Sending full menu.",'connect');
send_all_menu();
//DebMes("OK",'connect');
}
} else {
echo date('Y-m-d H:i:s') . " Sending menu element $parent_id\n";
//DebMes("Sending menu element: $parent_id",'connect');
update_menu_data($parent_id);
//DebMes("OK",'connect');
}
} elseif (preg_match('/incoming_urls/is', $topic)) {
$url = BASE_URL . $msg;
echo date("Y-m-d H:i:s") . " Incoming url: $url\n";
//DebMes("Incoming URL: $url",'connect');
getURLBackground($url, 0);
} elseif (preg_match('/reverse_urls/is', $topic)) {
//DebMes("Reverse URL: $msg",'connect');
$url = BASE_URL . '/ajax/connect.html?no_session=1&op=reverse_request&msg=' . urlencode($msg);
echo date("Y-m-d H:i:s") . " Incoming reverse url: $msg\n";
getURLBackground($url, 0);
} elseif (preg_match('/reverse_requests/is', $topic)) {
//DebMes("Reverse request: $msg",'connect');
$url = BASE_URL . '/ajax/connect.html?no_session=1&op=reverse_request_full&msg=' . urlencode($msg);
echo date("Y-m-d H:i:s") . " Incoming reverse request: $msg\n";
getURLBackground($url, 0);
}
//DebMes("Processing complete.",'connect');

}

Expand Down

0 comments on commit 8bba2f7

Please sign in to comment.