Example #1
0
 /**
  * testGet method
  *
  * @return void
  */
 public function testGet()
 {
     $m2x = new M2X('abc123');
     $m2x->request = $this->getMockBuilder('Att\\M2X\\HttpRequest')->setMethods(array('header'))->getMock();
     $m2x->request->expects($this->at(0))->method('header')->with($this->equalTo('X-M2X-KEY'), $this->equalTo('abc123'));
     $m2x->get('/status');
 }
Example #2
0
function distributionMetadata()
{
    try {
        $m2x = new M2X(API_KEY);
        $distribution = $m2x->distribution(DISTRIBUTION_ID);
        $response = $distribution->metadata();
        echo $response->raw();
        echo "\n\rLoad metadata for distribution status {$response->statusCode}";
        $metadata = json_decode($response->raw(), true);
        if (count($metadata) > 0) {
            $response = $distribution->metadataField($metadata[0]['key']);
            echo "\n\rLoad metadata field for distribution ";
            echo $response->raw();
        }
        $metadataObj = array("hi" => "distribution");
        $response = $distribution->updateMetadata($metadataObj);
        if ($response->statusCode == 204) {
            echo "\n\rUpdate metadata for distribution is successful.";
            $response = $distribution->updateMetadataField("hi", "distributions");
            if ($response->statusCode == 204) {
                echo "\n\rUpdate metadata field for distribution is successful.";
            } else {
                echo "\n\rUpdate metadata field for distribution Failed. Please try again.";
            }
        } else {
            echo "\n\rUpdate metadata for distribution Failed. Please try again.";
        }
    } catch (M2XException $ex) {
        echo 'Error: ' . $ex->getMessage();
        echo $ex->response->raw;
        break;
    }
}
Example #3
0
 /**
  * Create or update a stream resource
  *
  * @param M2X $client
  * @param Resource $parent
  * @param string $name
  * @param array $data
  * @return Stream
  */
 public static function createStream(M2X $client, Resource $parent, $name, $data)
 {
     $path = str_replace(':parent_path', $parent->path(), static::$path) . '/' . $name;
     $response = $client->put($path, $data);
     if ($response->statusCode == 204) {
         return self::getStream($client, $parent, $name);
     }
     return new self($client, $parent, $response->json());
 }
Example #4
0
function deviceCommands()
{
    try {
        $m2x = new M2X(API_KEY);
        $device = $m2x->device(DEVICE_ID);
        // Retrieve the list of recent commands sent to target device.
        $commands = $device->commands(array('status' => 'pending', 'limit' => 2));
        foreach ($commands as $command) {
            // Get details of a received commands for the device.
            $responseCommand = $device->command($command->id);
            if ($responseCommand->name == 'SAY_COMMAND') {
                echo "\n\rMark the given command as processed....\n\r";
                $device->process($responseCommand);
            } else {
                echo "\n\rMark the given command as rejected....\n\r";
                $device->reject($responseCommand);
            }
        }
    } catch (M2XException $ex) {
        echo 'Error: ' . $ex->getMessage();
        echo $ex->response->raw;
        break;
    }
}
Example #5
0
<?php

include '../vendor/autoload.php';
use Att\M2X\M2X;
use Att\M2X\Error\M2XException;
$api_key = "<API KEY HERE>";
$device_id = "<DEVICE>";
$stream = "<STREAM NAME>";
$m2x = new M2X($api_key);
try {
    // View Device
    $device = $m2x->device($device_id);
    // Create/Update Stream
    $data = array("type" => "numeric", "unit" => array("label" => "Celsius"));
    $stream = $device->updateStream($stream, $data);
    // List Streams
    $streams = $device->streams();
    // Get Details From Existing Stream
    $stream = $device->stream($stream);
    // Read Values From Existing Stream
    $values = $stream->values();
    // Post Multiple Values To Stream
    $stream->postValues(array(array("value" => 456), array("value" => 789), array("value" => 123.145)));
    // Read Location Information
    $info = $device->location();
    // Update Location Information
    $device->updateLocation(array("name" => "Seattle", "latitude" => 47.6097, "longitude" => 122.3331));
    // Delete Location History
    $device->deleteLocationHistory(array("from" => "2016-08-09T15:15:00.000Z", "end" => "2016-08-31T22:15:00.000Z"));
} catch (M2XException $ex) {
    echo $ex->getMessage();
Example #6
0
<?php

include '../vendor/autoload.php';
use Att\M2X\M2X;
use Att\M2X\Error\M2XException;
$apiKey = getenv("API_KEY");
$deviceId = getenv("DEVICE");
$m2x = new M2X($apiKey);
# 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);
}