Esempio n. 1
0
function deviceMetadata()
{
    try {
        $m2x = new M2X(API_KEY);
        $device = $m2x->device(DEVICE_ID);
        $response = $device->metadata();
        echo "\n\rLoad metadata for device ";
        echo $response->raw();
        $metadata = json_decode($response->raw(), true);
        if (count($metadata) > 0) {
            $response = $device->metadataField($metadata[0]['key']);
            echo "\n\rLoad metadata field for device ";
            echo $response->raw();
        }
        $metadataObj = array("hi" => "hello");
        $response = $device->updateMetadata($metadataObj);
        if ($response->statusCode == 204) {
            echo "\n\rUpdate metadata for device is Successful.";
            $response = $device->updateMetadataField("hi", "test");
            if ($response->statusCode == 204) {
                echo "\n\rUpdate metadata field for device is successful.";
            } else {
                echo "\n\rUpdate metadata field for device Failed. Please try again.";
            }
        } else {
            echo "\n\rUpdate metadata for device Failed. Please try again.";
        }
    } catch (M2XException $ex) {
        echo 'Error: ' . $ex->getMessage();
        echo $ex->response->raw;
        break;
    }
}
Esempio n. 2
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;
    }
}
Esempio n. 3
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();
Esempio n. 4
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);
}