<commit_before><?php
/**
 * CONNECT MQTT CLIENT
 *
 * @package MajorDoMo
 * @author <NAME> <<EMAIL>> http://smartliving.ru/
 * @version 1.3
 */
chdir(dirname(__FILE__) . '/../');

include_once("./config.php");
include_once("./lib/loader.php");

include_once("./load_settings.php");

$started_time = time();
$max_run_time = 2 * 60 * 60; // do restart in 2 hours

set_time_limit(0);

include_once(DIR_MODULES . 'connect/connect.class.php');

$menu_sent_time = 0;
$devices_sent_time = 0;

include_once(ROOT . "3rdparty/phpmqtt/phpMQTT.php");

$saved_devices_data = array();

const CONNECT_HOST = 'connect.smartliving.ru';

setGlobal((str_replace('.php', '', basename(__FILE__))).'Run', time(), 1);
$cycleVarName='ThisComputer.'.str_replace('.php', '', basename(__FILE__)).'Run';

while (1) {
    $connect = new connect();
    $connect->getConfig();

    if (!$connect->config['CONNECT_SYNC']) {
        echo "Connect sync turned off.";
        exit;
    }

    $sqlQuery = "SELECT * FROM commands";
    $commands = SQLSelect($sqlQuery);
    $total = count($commands);

    for ($i = 0; $i < $total; $i++) {
        $cmd_values[$commands[$i]['ID']] = $commands[$i]['CUR_VALUE'];
        $cmd_titles[$commands[$i]['ID']] = $commands[$i]['RENDER_TITLE'];
        $cmd_data[$commands[$i]['ID']] = $commands[$i]['RENDER_DATA'];
    }

    $username = strtolower($connect->config['CONNECT_USERNAME']);
    $password = $connect->config['CONNECT_PASSWORD'];

    $host = CONNECT_HOST;
    if ($connect->config['CONNECT_INSECURE']) {
        $port = '1883';
        $ca_file = NULL;
    } else {
        $port = '8883';
        $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";
    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";
        $topics = array();
        for ($i = 0; $i < $total; $i++) {
            $path = trim($query_list[$i]);
            echo date('H:i:s') . " Path: $path\n";
            $topics[$path] = array("qos" => 0, "function" => "procmsg");
        }
        foreach ($topics as $k => $v) {
            echo date('H:i:s') . " Subscribing to: $k\n";
        }
        $mqtt_client->subscribe($topics, 0);
        $ping_timestamp = 0;
        while ($mqtt_client->proc()) {
            $currentMillis = round(microtime(true) * 10000);
            if ($currentMillis - $previousMillis > 10000) {
                $previousMillis = $currentMillis;
                $checked_time=time();
                //setGlobal((str_replace('.php', '', basename(__FILE__))) . 'Run', time(), 1);
                SQLExec("REPLACE INTO cached_values (KEYWORD, DATAVALUE, EXPIRE)  VALUES ('MJD:$cycleVarName', '$checked_time','".date('Y-m-d H:i:s',$checked_time+60)."')");
                if (file_exists('./reboot') || IsSet($_GET['onetime'])) {
                    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'];
                        $connect->sendDeviceProperty($property_data['DATANAME'], $property_data['DATAVALUE']);
                    }
                }
                $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";
                    $connect->sendAllDevices();
                }
            }
            /*
            if (time() - $menu_sent_time > 60 * 60) {
                $menu_sent_time = time();
                send_all_menu();
            }
            */
            if ((time() - $started_time) > $max_run_time) {
                echo "Exit cycle CONNECT... (reconnecting)";<commit_msg>Update cycle_connect.php<commit_after><?php
/**
 * CONNECT MQTT CLIENT
 *
 * @package MajorDoMo
 * @author <NAME> <<EMAIL>> http://smartliving.ru/
 * @version 1.3
 */
chdir(dirname(__FILE__) . '/../');

include_once("./config.php");
include_once("./lib/loader.php");

include_once("./load_settings.php");

$started_time = time();
$max_run_time = 2 * 60 * 60; // do restart in 2 hours

set_time_limit(0);

include_once(DIR_MODULES . 'connect/connect.class.php');

$menu_sent_time = 0;
$devices_sent_time = 0;

include_once(ROOT . "3rdparty/phpmqtt/phpMQTT.php");

$saved_devices_data = array();

const CONNECT_HOST = 'connect.smartliving.ru';

setGlobal((str_replace('.php', '', basename(__FILE__))).'Run', time(), 1);
$cycleVarName='ThisComputer.'.str_replace('.php', '', basename(__FILE__)).'Run';

while (1) {
    $connect = new connect();
    $connect->getConfig();

    if (!$connect->config['CONNECT_SYNC']) {
        echo "Connect sync turned off.";
        exit;
    }

    $sqlQuery = "SELECT * FROM commands";
    $commands = SQLSelect($sqlQuery);
    $total = count($commands);

    for ($i = 0; $i < $total; $i++) {
        $cmd_values[$commands[$i]['ID']] = $commands[$i]['CUR_VALUE'];
        $cmd_titles[$commands[$i]['ID']] = $commands[$i]['RENDER_TITLE'];
        $cmd_data[$commands[$i]['ID']] = $commands[$i]['RENDER_DATA'];
    }

    $username = strtolower($connect->config['CONNECT_USERNAME']);
    $password = $connect->config['CONNECT_PASSWORD'];

    $host = CONNECT_HOST;
    if ($connect->config['CONNECT_INSECURE']) {
        $port = '1883';
        $ca_file = NULL;
    } else {
        $port = '8883';
        $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";
    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";
        $topics = array();
        for ($i = 0; $i < $total; $i++) {
            $path = trim($query_list[$i]);
            echo date('H:i:s') . " Path: $path\n";
            $topics[$path] = array("qos" => 0, "function" => "procmsg");
        }
        foreach ($topics as $k => $v) {
            echo date('H:i:s') . " Subscribing to: $k\n";
        }
        $mqtt_client->subscribe($topics, 0);
        $ping_timestamp = 0;
        while ($mqtt_client->proc()) {
            $currentMillis = round(microtime(true) * 10000);
            if ($currentMillis - $previousMillis > 10000) {
                $previousMillis = $currentMillis;
                $checked_time=time();
                //setGlobal((str_replace('.php', '', basename(__FILE__))) . 'Run', time(), 1);
                saveToCache("MJD:$cycleVarName", $checked_time);
                if (file_exists('./reboot') || IsSet($_GET['onetime'])) {
                    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'];
                        $connect->sendDeviceProperty($property_data['DATANAME'], $property_data['DATAVALUE']);
                    }
                }
                $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";
                    $connect->sendAllDevices();
                }
            }
            /*
            if (time() - $menu_sent_time > 60 * 60) {
                $menu_sent_time = time();
                send_all_menu();
            }
            */
            if ((time() - $started_time) > $max_run_time) {
                echo "Exit cycle CONNECT... (reconnecting)";
{'fraction_non_alphanumeric': 0.18204461771713235, 'fraction_numerical': 0.010396361273554255, 'mean_word_length': 2.4203703703703705, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7359281', 'n_tokens_mistral': 3059, 'n_tokens_neox': 2915, 'n_words': 506}