Example #1
0
 /**
  * testConnectSocketException method
  *
  * @expectedException \Att\M2X\MQTT\Error\SocketException
  * @expectedExceptionMessage Connection refused
  *
  * @return void
  */
 public function testConnectSocketException()
 {
     $client = new MQTTClient('foobar', array('host' => '0.0.0.0'));
     $client->connect();
 }
Example #2
0
 /**
  * Create or update a stream resource
  *
  * @param MQTTClient $client
  * @param Resource $parent
  * @param string $name
  * @param array $data
  * @return Stream
  */
 public static function createStream(MQTTClient $client, Resource $parent, $name, $data)
 {
     $path = str_replace(':parent_path', $parent->path(), static::$path) . '/' . $name;
     $response = $client->put($path, $data);
     return new self($client, $parent, $response->json());
 }
Example #3
0
<?php

include '../vendor/autoload.php';
use Att\M2X\MQTT\MQTTClient;
use Att\M2X\MQTT\Error\M2XException;
$apiKey = getenv("API_KEY");
$deviceId = getenv("DEVICE");
$m2x = new MQTTClient($apiKey);
$m2x->connect();
# Get the device
$device = $m2x->device($deviceId);
# Create the streams if they don't exist yet
$device->updateStream('load_1m');
$device->updateStream('load_5m');
$device->updateStream('load_15m');
while (true) {
    list($load_1m, $load_5m, $load_15m) = sys_getloadavg();
    $now = date('c');
    $values = array('load_1m' => array(array('value' => $load_1m, 'timestamp' => $now)), 'load_5m' => array(array('value' => $load_5m, 'timestamp' => $now)), 'load_15m' => array(array('value' => $load_15m, 'timestamp' => $now)));
    try {
        $device->postUpdates($values);
    } catch (M2XException $ex) {
        echo 'Error: ' . $ex->getMessage();
        echo $ex->response->raw;
        break;
    }
    sleep(10);
}
Example #4
0
        print sprintf("SAY: %s\n\r", $command->data['message']);
        $command->process();
    } else {
        $reason = 'The "message" param is required in data';
        $command->reject(compact('reason'));
    }
}
function process_report_command($command)
{
    $public_ip = getHostByName(php_uname('n'));
    $pid = (string) getmypid();
    print sprintf("REPORT: public_ip: %s, pid: %s\n\r", $public_ip, $pid);
    $command->process(compact('public_ip', 'pid'));
}
try {
    $client = new MQTTClient($apiKey, array('clientId' => 'PHP Test Client', 'host' => gethostbyname($hostname)));
    $client->connect();
    echo "Connected to the broker\n\r";
    $device = $client->device($deviceId);
    //Check for unacknowledged commands
    $commands = $device->commands(array('status' => 'pending', 'limit' => 100));
    foreach ($commands as $command) {
        $command->refresh();
        process_command($command);
    }
    //Listen for incoming commands
    while ($command = $device->receiveCommand()) {
        process_command($command);
    }
    $client->disconnect();
} catch (Exception $ex) {