示例#1
0
        $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) {
    echo sprintf('Exception Error: %s', $ex->getMessage());
    throw $ex;
}