<?php

use Mosquitto\Client;
error_log($_POST['test'] . PHP_EOL, 3, "/var/log/php_errors.log");
$client = new Mosquitto\Client('PostListenerClient');
$client->connect('atliot.com', 1883);
$client->publish('office/uhfrfid/echain', 'publish from php');
$client->disconnect();
 public function study_config_updated()
 {
     $study_id = $this->input->post('study_id', true);
     if ($this->Researcher_model->check_study_privileges($study_id, $this->session->userdata('id')) || $this->session->userdata('manager')) {
         $config = $this->input->post('config', true);
         // Get study database
         $study_db = $this->_get_study_database($study_id);
         // Get all study devices
         $study_devices = $this->Researcher_model->get_device_data($study_db, "", "", "", "", "4294967295");
         // Get MQTT server details
         $mqtt_conf = $this->_get_mqtt_server_details($study_id);
         // Using Mosquitto-PHP client that we installed over PECL
         $client = new Mosquitto\Client("aware", true);
         $client->setTlsCertificates($this->config->item("public_keys") . "server.crt");
         //load server SSL certificate
         $client->setTlsOptions(Mosquitto\Client::SSL_VERIFY_PEER, "tlsv1.2", NULL);
         //make sure client is using our server certificate to connect
         $client->setCredentials($mqtt_conf['mqtt_username'], $mqtt_conf['mqtt_password']);
         //load study-specific user credentials so we can connect
         $client->connect($mqtt_conf['mqtt_server'], $mqtt_conf['mqtt_port']);
         //make connection
         // Loop through devices and send message
         foreach ($study_devices as $device) {
             if (array_key_exists("device_id", $device)) {
                 $client->publish($study_id . "/" . $device["device_id"] . "/configuration", $config, 1, false);
                 $client->loop();
                 sleep(1);
             }
         }
         $client->disconnect();
     } else {
         header('HTTP/1.0 401 Unauthorized');
         exit;
     }
 }
 public function remove_device()
 {
     if ($this->session->userdata('researcher')) {
         $study_id = $this->input->post('study_id', true);
         $device_id = $this->input->post('device_id', true);
         $study_db = $this->_get_study_database($study_id);
         //$this->Researcher_model->is_study_creator($study_id, $this->session->userdata('id'))
         if ($this->Researcher_model->device_in_study($study_db, $device_id) && $this->Researcher_model->check_study_privileges($study_id, $this->session->userdata('id'))) {
             //$this->output->set_output(json_encode(array("success" => true)));
             // Get study data table names
             $data_collected = $this->Researcher_model->get_study_tables($study_db);
             $study_tables = array();
             foreach ($data_collected as $key => $value) {
                 array_push($study_tables, $data_collected[$key]["TABLE_NAME"]);
             }
             // Delete device and its' data
             $success = $this->Researcher_model->remove_device($study_db, $study_tables, $device_id);
             // Get MQTT server details
             $mqtt_conf = $this->_get_mqtt_server_details($study_id);
             // Using Mosquitto-PHP client that we installed over PECL
             $client = new Mosquitto\Client("aware", true);
             $client->setTlsCertificates($this->config->item("public_keys") . "server.crt");
             //load server SSL certificate
             $client->setTlsOptions(Mosquitto\Client::SSL_VERIFY_PEER, "tlsv1.2", NULL);
             //make sure client is using our server certificate to connect
             $client->setCredentials($mqtt_conf['mqtt_username'], $mqtt_conf['mqtt_password']);
             //load study-specific user credentials so we can connect
             $client->connect($mqtt_conf['mqtt_server'], $mqtt_conf['mqtt_port'], 60);
             //make connection, keep alive 30 seconds
             $client->publish($study_id . "/" . $device_id . "/broadcasts", "ACTION_QUIT_STUDY", 1, false);
             $client->loop();
             sleep(1);
             $client->disconnect();
             $this->output->set_output(json_encode(array("success" => $success)));
         } else {
             header('HTTP/1.0 401 Unauthorized');
             exit;
         }
     } else {
         header('HTTP/1.0 401 Unauthorized');
         exit;
     }
 }