PHP code example of creatcode / think-iotmonitor

1. Go to this page and download the library: Download creatcode/think-iotmonitor library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

creatcode / think-iotmonitor example snippets




return [
    // 组件总开关。
    'enable' => true,

    'traffic' => [
        // 是否启用 TCP 流量统计。
        'enable' => false,

        // 内存统计数据写入 Redis 的间隔,单位秒。
        'flush_interval' => 5,

        // 分钟统计数据的保留时间,单位秒。
        'retention_seconds' => 86400,
    ],

    'db' => [
        // 数据库连接探活间隔,单位秒。
        'db_ping_interval' => 30,

        // Redis 连接探活间隔,单位秒。
        'redis_ping_interval' => 15,
    ],

    'protocol' => [
        // 是否校验 Modbus RTU CRC。
        'rtu_crc_check' => false,

        // 特殊报文长度配置,key 为报文标识,value 为完整报文长度。
        'extra_packets' => [
            'imei' => 19,
            'ping' => 4,
        ],
    ],

    'overview' => [
        // 是否启用监控总览。
        'enable' => false,

        // 需要统计的 Redis 队列名称。
        'queues' => [
            'login_command',
            'dev_link',
            'dev_alarm',
        ],
    ],
];

use CreatCode\ThinkIotMonitor\Monitor\WorkermanMonitorBootstrap;
use CreatCode\ThinkIotMonitor\Runtime\Runtime;

$worker->onWorkerStart = function () {
    Runtime::boot();
    WorkermanMonitorBootstrap::boot();
};

use CreatCode\ThinkIotMonitor\Protocol\ModbusTcpProtocol;

$worker->protocol = ModbusTcpProtocol::class;

use CreatCode\ThinkIotMonitor\RedisManager;

// 读操作:连接异常时自动重连并重试一次。
$value = RedisManager::hGet('key', 'field');

// 安全写入:发生异常时返回默认值。
RedisManager::safeWrite('zAdd', ['Key', time(), 'member'], 0);

// 安全管道操作。
RedisManager::safePipeline(function ($redis) {
    $redis->incr('counter');
});

use CreatCode\ThinkIotMonitor\DbManager;

$rows = DbManager::call(function () {
    return \think\facade\Db::name('device')->select();
});

use CreatCode\ThinkIotMonitor\TrafficReader;

$data = (new TrafficReader())->buildTrafficData(60);

use CreatCode\ThinkIotMonitor\Monitor\OverviewReader;

$data = (new OverviewReader())->build(60);

use CreatCode\ThinkIotMonitor\ManagerHelper;

// 读取配置,支持点号路径。
ManagerHelper::config('traffic.flush_interval', 5);
ManagerHelper::boolConfig('traffic.enable', false);
ManagerHelper::dbConfig('redis_ping_interval', 15);
ManagerHelper::pluginConfig();

// 状态判断。
ManagerHelper::pluginEnabled();
ManagerHelper::trafficEnabled();

// 工具方法。
ManagerHelper::deviceActiveTimeKey();
ManagerHelper::log('monitor.log', 'message');
text
/iotmonitor.php
text
/iotmonitor.php/overview?minutes=60
text
app/Base.php
app/base.php
text
IOTMONITOR_BOOTSTRAP=/absolute/path/to/bootstrap.php
text
/iotmonitor.php
/iotmonitor.php/overview