Ejemplo n.º 1
3
<?php

$client = new Mosquitto\Client();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect("localhost", 1883, 5);
$client->onLog('logger');
$client->subscribe('#', 1);
for ($i = 0; $i < 10; $i++) {
    $client->loop();
}
$client->unsubscribe('#');
for ($i = 0; $i < 10; $i++) {
    $client->loop();
}
function connect($r, $message)
{
    echo "I got code {$r} and message {$message}\n";
}
function subscribe()
{
    echo "Subscribed to a topic\n";
}
function unsubscribe()
{
    echo "Unsubscribed from a topic\n";
}
function message($message)
{
function on_disconnect()
{
    global $_HOST, $_PORT, $_USERNAME;
    // Display notice/confirmation of being disconnected.
    echo 'Client disconnected.' . PHP_EOL;
    echo 'Will attempt to reconnect in 5s.' . PHP_EOL;
    sleep(5);
    // Create a new $client object.
    echo 'Trying to reconnect...' . PHP_EOL;
    $client = new Mosquitto\Client();
    $client->connect($_HOST, $_PORT);
    // Bind callbacks
    $client->onConnect('on_connect');
    $client->onDisconnect('on_disconnect');
    $client->onSubscribe('on_subscribe');
    $client->onMessage('on_message');
    for (;;) {
        $client->loop();
    }
}
Ejemplo n.º 3
1
<?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();
Ejemplo n.º 4
1
 public function subscribe($topic, $qos)
 {
     $mid = parent::subscribe($topic, $qos);
     $this->pendingSubs[$mid] = $topic;
 }
Ejemplo n.º 5
1
<?php

$c = new Mosquitto\Client();
$c->onConnect(function ($code, $message) {
    echo "I'm connected\n";
});
$c->connect('localhost', 1883, 60);
$c->subscribe('#', 1);
$c->onMessage(function ($m) {
    var_dump($m);
});
$socket = $c->getSocket();
$base = new EventBase();
$ev = new Event($base, $socket, Event::READ | Event::PERSIST, 'cb', $base);
function cb($fd, $what, $arg)
{
    global $c;
    echo "Triggered\n";
    var_dump(func_get_args());
    $c->loop();
}
$ev->add();
$base->dispatch();
Ejemplo n.º 6
0
 public function init()
 {
     $c = new \Mosquitto\Client();
     if ($this->cafile && $this->certfile && $this->keyfile) {
         $c->setTlsCertificates($this->cafile, $this->certfile, $this->keyfile, $this->password);
     }
     $c->connect($this->host, $this->port, $this->keepalive);
     $this->object = $c;
 }
Ejemplo n.º 7
0
            $log->error("Cannot connect to redis at " . $redis_server['host'] . ", autentication failed");
            die('Check log\\n');
        }
    }
} else {
    $redis = false;
}
require "Modules/user/user_model.php";
$user = new User($mysqli, $redis, null);
require_once "Modules/feed/feed_model.php";
$feed = new Feed($mysqli, $redis, $feed_settings);
require_once "Modules/input/input_model.php";
$input = new Input($mysqli, $redis, $feed);
require_once "Modules/process/process_model.php";
$process = new Process($mysqli, $input, $feed, $user->get_timezone($mqtt_server['userid']));
$mqtt_client = new Mosquitto\Client();
$mqtt_client->setCredentials($mqtt_server['user'], $mqtt_server['password']);
$connected = false;
$mqtt_client->onConnect('connect');
$mqtt_client->onDisconnect('disconnect');
$mqtt_client->onSubscribe('subscribe');
$mqtt_client->onMessage('message');
$mqtt_client->connect($mqtt_server['host'], $mqtt_server['port'], 5);
$topic = $mqtt_server['basetopic'] . "/#";
echo "Subscribing to: " . $topic . "\n";
$log->warn("Subscribing to: " . $topic);
$mqtt_client->subscribe($topic, 2);
while (true) {
    $mqtt_client->loop();
}
function connect($r, $message)
Ejemplo n.º 8
0
}
class Message
{
    public $id;
    public $state = false;
    public $msg;
    public static function factory(Mosquitto\Message $msg, $state = false)
    {
        $message = new Message();
        $message->state = $state;
        $message->msg = $msg;
        $message->id = $msg->mid;
        return $message;
    }
}
$client = new Mosquitto\Client('client.terminal.onpublish', false);
$client->onMessage(function ($msg) {
    print_r(array('receive', $msg));
    MQ::addReceive($msg);
});
$client->onPublish(function ($mid) {
    MQ::confirm($mid);
    print_r(array('comfirm publish', MQ::$publish[$mid]));
});
$client->onConnect(function ($rc, $msg) {
    print_r(array('rc' => $rc, 'message' => $msg));
});
$client->connect('localhost', 1883, 60);
sleep(1);
$client->subscribe('/test/publish', 1);
$msg = Message::factory(new Mosquitto\Message());
Ejemplo n.º 9
0
<?php

$client = new Mosquitto\Client();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->setWill('/hello', "Client died :-(", 1, 0);
$client->connect("localhost", 1883, 5);
$client->subscribe('/#', 1);
$client->loopForever();
function connect($r)
{
    echo "I got code {$r}\n";
}
function subscribe()
{
    echo "Subscribed to a topic\n";
}
function message($message)
{
    printf("Got a message on topic %s with payload:\n%s\n", $message->topic, $message->payload);
}
function disconnect()
{
    echo "Disconnected cleanly\n";
}
Ejemplo n.º 10
0
 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;
     }
 }
Ejemplo n.º 11
0
 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;
     }
 }
Ejemplo n.º 12
-1
<?php

$client = new Mosquitto\Client();
$client->onConnect('connect');
$client->onDisconnect('disconnect');
$client->onSubscribe('subscribe');
$client->onMessage('message');
$client->connect("localhost", 1883, 5);
$client->subscribe('/#', 1);
while (true) {
    $client->loop();
    $mid = $client->publish('/hello', "Hello from PHP at " . date('Y-m-d H:i:s'), 1, 0);
    echo "Sent message ID: {$mid}\n";
    $client->loop();
    sleep(2);
}
$client->disconnect();
unset($client);
function connect($r)
{
    echo "I got code {$r}\n";
}
function subscribe()
{
    echo "Subscribed to a topic\n";
}
function message($message)
{
    printf("Got a message ID %d on topic %s with payload:\n%s\n\n", $message->mid, $message->topic, $message->payload);
}
function disconnect()