Skip to content
This repository has been archived by the owner on Jan 5, 2021. It is now read-only.

Commit

Permalink
Fixed Mosquitto-PHP client
Browse files Browse the repository at this point in the history
  • Loading branch information
denzilferreira committed Jan 4, 2016
1 parent 1b9cef2 commit 36bc074
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions application/controllers/webservice.php
Expand Up @@ -388,14 +388,17 @@ public function publish() {
// 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 peer has certificate
$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

// Loop through devices and send message
foreach ($devices as $device) {
$client->connect($mqtt_conf['mqtt_server'], $mqtt_conf['mqtt_port']); //make connection
$client->publish($topic['study_id'] . "/" . $device . "/" . $topic['type'], $msg, 1, false);
$client->loop();
sleep(1);
}

$client->disconnect();

// Save ESM to history
$study_db = $this->_get_study_database($study_id);
Expand Down Expand Up @@ -493,15 +496,13 @@ public function study_config_updated() {

// Get all study devices
$study_devices = $this->Researcher_model->get_device_data($study_db, "", "", "", "", "4294967295");
//print_r($study_devices);

// 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 peer has certificate
$client->setCredentials($mqtt_conf['mqtt_username'], $mqtt_conf['mqtt_password']); //load study-specific user credentials so we can connect

// Loop through devices and send message
Expand All @@ -516,14 +517,4 @@ public function study_config_updated() {
exit();
}
}

// Debugging function
/*
public function test($table) {
$study_db = $this->_get_study_database(307);
$this->load->model("Aware_model");
$this->Aware_model->check_table($study_db, $table);
}
*/

}

0 comments on commit 36bc074

Please sign in to comment.