65 lines
2.1 KiB
PHP
65 lines
2.1 KiB
PHP
<?php
|
|
// +----------------------------------------------------------------------
|
|
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
|
// +----------------------------------------------------------------------
|
|
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
|
// +----------------------------------------------------------------------
|
|
// | Author: ywxapp<admin@ywxapp.cn>
|
|
// +----------------------------------------------------------------------
|
|
declare (strict_types = 1);
|
|
|
|
namespace addon\mqttbroker\controller\api;
|
|
|
|
use think\facade\Db;
|
|
use ywxapp\controller\ApiController;
|
|
use addon\mqttbroker\model\Message;
|
|
|
|
/**
|
|
* Mqtt 类
|
|
*
|
|
* @author ywxapp <admin@ywxapp.cn>
|
|
*/
|
|
class Mqtt extends ApiController
|
|
{
|
|
protected $noNeedLogin = ['*'];
|
|
protected $noNeedVerify = ['*'];
|
|
|
|
/**
|
|
* 获取 Broker 运行状态
|
|
* GET /mqttbroker/api/status
|
|
*/
|
|
public function status()
|
|
{
|
|
$data = [
|
|
'online' => Db::name('mqttbroker_connection')->where('status', 1)->count(),
|
|
'total' => Db::name('mqttbroker_connection')->count(),
|
|
'messages' => Message::count(),
|
|
'topics' => Db::name('mqttbroker_topic')->count(),
|
|
];
|
|
$this->result->success($data);
|
|
}
|
|
|
|
/**
|
|
* 发布消息
|
|
* POST /mqttbroker/api/publish {topic, payload, qos}
|
|
*
|
|
* 零外部依赖:写入出站队列(DB),由常驻 Broker 进程消费后经内部路由投递,
|
|
* 不再依赖 workerman/mqtt 客户端连接本地 Broker。
|
|
*/
|
|
public function publish()
|
|
{
|
|
$topic = input('topic', '');
|
|
$payload = input('payload', '');
|
|
$qos = (int) input('qos', 0);
|
|
if (!$topic) {
|
|
$this->result->error('topic required');
|
|
}
|
|
$ok = \addon\mqttbroker\service\Store::enqueueManual($topic, $payload, $qos, 0, 'api');
|
|
if ($ok) {
|
|
$this->result->success('已加入发布队列,Broker 将投递给在线订阅者');
|
|
} else {
|
|
$this->result->error('发布失败:无法写入出站队列(请检查数据库)');
|
|
}
|
|
}
|
|
}
|