Compare commits
28 Commits
430bc08d1e
...
1.2.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 9607f69e24 | |||
| 6d3709da51 | |||
| 1456b21f34 | |||
| 7343267d3c | |||
| 430af8aede | |||
| 9028a8c59d | |||
| daefbe884c | |||
| 9ad385e044 | |||
| 9b926634cf | |||
| 320739411a | |||
| cffc90dfc9 | |||
| 5b84406652 | |||
| 3c5020b80b | |||
| e50eed84a2 | |||
| 41da8f6aee | |||
| d9cf1f12b4 | |||
| d37f452946 | |||
| ef5eddbc8d | |||
| 9939ee4c8b | |||
| 2ff864b8c8 | |||
| b3a5c320c1 | |||
| c1547e191c | |||
| 90f683d655 | |||
| 8b076e52a9 | |||
| 8d17cb73ab | |||
| 2100f37ffa | |||
| 48720a5912 | |||
| 6e5ec4f056 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@ composer.lock
|
||||
.idea/
|
||||
vendor/
|
||||
tests/prikey.pem
|
||||
demo.php
|
||||
|
||||
16
README.md
16
README.md
@@ -1,11 +1,13 @@
|
||||
### 安装
|
||||
|
||||
```shell
|
||||
composer require maiyoule/mqttclient_author
|
||||
```
|
||||
|
||||
### 配置
|
||||
|
||||
```php
|
||||
$manager = new MQTTManager();
|
||||
$manager = new AuthorServerClient();
|
||||
//设置APPID
|
||||
$manager->setAppId('');
|
||||
//设置私钥
|
||||
@@ -14,6 +16,8 @@ $manager->setPrivateKey('');
|
||||
|
||||
### 使用
|
||||
|
||||
#### 创建
|
||||
|
||||
```php
|
||||
//创建MQTT用户
|
||||
$request = new AppUserCreateRequest();
|
||||
@@ -33,3 +37,13 @@ $data = $biz->getData();
|
||||
//....
|
||||
print_r($data);
|
||||
```
|
||||
|
||||
#### 注销登录
|
||||
```php
|
||||
$request=new \cn\com\maiyoule\mqttclient\biz\AppUserLogoutRequest();
|
||||
$request->setUsername('xxxxx');
|
||||
|
||||
$biz=$manager->exec($request);
|
||||
|
||||
echo sprintf('注销结果:%b',$biz->isSuccess())
|
||||
```
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"name": "maiyoule/mqttclient_author",
|
||||
"type": "library",
|
||||
"description": "MQTT管理模块操作库",
|
||||
"version": "1.0.1",
|
||||
"version": "1.2.2",
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
@@ -13,8 +13,10 @@
|
||||
],
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"guzzlehttp/guzzle": "~6.0",
|
||||
"phpseclib/phpseclib": "~3.0"
|
||||
"guzzlehttp/guzzle": "^7.0",
|
||||
"phpseclib/phpseclib": "~3.0",
|
||||
"workerman/mqtt": "^2.2",
|
||||
"psr/log": "^3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^11.0"
|
||||
|
||||
282
src/AuthorAppMQTTClient.php
Normal file
282
src/AuthorAppMQTTClient.php
Normal file
@@ -0,0 +1,282 @@
|
||||
<?php
|
||||
|
||||
namespace cn\com\maiyoule\mqttclient;
|
||||
|
||||
use cn\com\maiyoule\mqttclient\biz\client\SendMQTTMessage;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Workerman\Mqtt\Client;
|
||||
use Workerman\Worker;
|
||||
|
||||
/**
|
||||
* MQTT 客户端封装,用于主管理员
|
||||
*/
|
||||
class AuthorAppMQTTClient
|
||||
{
|
||||
private static ?self $instance = null;
|
||||
|
||||
public static function getInstance(): self
|
||||
{
|
||||
if (self::$instance == null) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
private string $appid = '';
|
||||
private string $server = 'mqtt://mqttauthor.maiyoule.com.cn';
|
||||
private int $port = 1883;
|
||||
private string $clientId = '';
|
||||
private string $username = '';
|
||||
private string $password = '';
|
||||
|
||||
|
||||
private ?Client $client = null;
|
||||
|
||||
public function getServer(): string
|
||||
{
|
||||
return $this->server;
|
||||
}
|
||||
|
||||
public function setServer(string $server): AuthorAppMQTTClient
|
||||
{
|
||||
$this->server = $server;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPort(): int
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
public function setPort(int $port): AuthorAppMQTTClient
|
||||
{
|
||||
$this->port = $port;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getClientId(): string
|
||||
{
|
||||
return $this->clientId;
|
||||
}
|
||||
|
||||
public function setClientId(string $clientId): AuthorAppMQTTClient
|
||||
{
|
||||
$this->clientId = $clientId;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getUsername(): string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function setUsername(string $username): AuthorAppMQTTClient
|
||||
{
|
||||
$this->username = $username;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPassword(): string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
public function setPassword(string $password): AuthorAppMQTTClient
|
||||
{
|
||||
$this->password = $password;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
public function getAppid(): string
|
||||
{
|
||||
return $this->appid;
|
||||
}
|
||||
|
||||
public function setAppid(string $appid): AuthorAppMQTTClient
|
||||
{
|
||||
$this->appid = $appid;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
private LoggerInterface $logger;
|
||||
|
||||
public function getLogger(): LoggerInterface
|
||||
{
|
||||
return $this->logger;
|
||||
}
|
||||
|
||||
public function setLogger(LoggerInterface $logger): void
|
||||
{
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function __construct()
|
||||
{
|
||||
if (class_exists('Worker')) {
|
||||
throw new \Exception('没有找到Workman');
|
||||
}
|
||||
}
|
||||
|
||||
private ?Worker $worker;
|
||||
|
||||
/**
|
||||
* 启动服务
|
||||
* @return bool
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function start(): bool
|
||||
{
|
||||
$this->worker = new Worker();
|
||||
|
||||
$this->worker->onWorkerStart = function () {
|
||||
$this->logger->info('Worker 启动');
|
||||
$this->__startWorker();
|
||||
};
|
||||
$this->worker->onMessage = function ($conn, $data) {
|
||||
$this->logger->debug($data);
|
||||
};
|
||||
$this->worker->onWorkerStop = function () {
|
||||
$this->logger->info('Worker 停止');
|
||||
$this->__stopWorker();
|
||||
};
|
||||
$this->worker->onError = function () {
|
||||
$this->logger->error('Worker启动遇到错误');
|
||||
};
|
||||
if (!Worker::isRunning()) {
|
||||
Worker::runAll();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止
|
||||
* @return void
|
||||
*/
|
||||
public function stop(): void
|
||||
{
|
||||
if (is_null($this->worker)) {
|
||||
return;
|
||||
}
|
||||
$this->worker->stop();
|
||||
$this->worker = null;
|
||||
}
|
||||
|
||||
private function __stopWorker(): void
|
||||
{
|
||||
if (is_null($this->client)) {
|
||||
return;
|
||||
}
|
||||
$topic = sprintf('biz/%s/#', $this->getAppid());
|
||||
|
||||
$this->client->unsubscribe($topic);
|
||||
//清理订阅
|
||||
$this->client->disconnect();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function __startWorker(): void
|
||||
{
|
||||
//参考 https://www.workerman.net/doc/workerman/components/workerman-mqtt.html
|
||||
$option = [
|
||||
'keepalive' => 29,
|
||||
'client_id' => $this->getClientId(),
|
||||
'protocol_name' => 'MQTT',
|
||||
'protocol_level' => 5,
|
||||
'clean_session' => true,
|
||||
'reconnect_period' => 2,
|
||||
'connect_timeout' => 10,
|
||||
'username' => $this->getUsername(),
|
||||
'password' => $this->getPassword(),
|
||||
'resubscribe' => true,
|
||||
'debug' => false
|
||||
];
|
||||
$address = sprintf('mqtt://%s:%d', $this->getServer(), $this->getPort());
|
||||
$this->logger->debug('建立MQTT连接到' . $address);
|
||||
|
||||
$this->client = new Client($address, $option);
|
||||
//连接回调
|
||||
$this->client->onConnect = function (Client $connection) {
|
||||
$this->logger->debug('MQTT 连接成功');
|
||||
$topic = sprintf('biz/%s/#', $this->getAppid());
|
||||
$connection->subscribe($topic, ['qos' => 0], function (\Exception|null $exception, array $granted) {
|
||||
|
||||
if (!is_null($exception)) {
|
||||
$this->logger->error($exception->getMessage());
|
||||
}
|
||||
$this->logger->debug('主题订阅:' . json_encode($granted));
|
||||
});
|
||||
|
||||
$this->callEvent(self::EVENT_CONNECT, $connection);
|
||||
};
|
||||
$this->client->onClose = function () {
|
||||
$this->logger->debug('MQTT 连接断开');
|
||||
$this->callEvent(self::EVENT_CLOSE);
|
||||
};
|
||||
$this->client->onMessage = function (string $topic, string $data, Client $client) {
|
||||
//收到消息
|
||||
$this->logger->debug(sprintf('MQTT 收到消息 Topic:%s Data:%s', $topic, $data));
|
||||
$this->callEvent(self::EVENT_MESSAGE, $topic, $data, $client);
|
||||
};
|
||||
//遇到错误
|
||||
$this->client->onError = function (\Exception $err) {
|
||||
$this->logger->error('MQTT 遇到错误' . $err->getMessage());
|
||||
$this->callEvent(self::EVENT_ERROR, $err);
|
||||
};
|
||||
$this->client->connect();
|
||||
|
||||
}
|
||||
|
||||
const EVENT_CONNECT = 'connect';
|
||||
const EVENT_CLOSE = 'close';
|
||||
|
||||
const EVENT_MESSAGE = 'message';
|
||||
const EVENT_ERROR = 'error';
|
||||
|
||||
private array $mapEvent = [];
|
||||
|
||||
public function registerEvent(string $event, \Closure $callback): AuthorAppMQTTClient
|
||||
{
|
||||
$this->mapEvent[$event][] = $callback;
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function callEvent(string $event, ...$args): void
|
||||
{
|
||||
if (isset($this->mapEvent[$event])) {
|
||||
foreach ($this->mapEvent[$event] as $callback) {
|
||||
call_user_func_array($callback, $args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息到目标客户端
|
||||
* @param SendMQTTMessage $message
|
||||
* @return bool
|
||||
*/
|
||||
public function sendToTarget(SendMQTTMessage $message): bool
|
||||
{
|
||||
//组装主题
|
||||
$topic = sprintf('biz/%s/%s/%s/downstream', $this->getAppid(), $message->getBiz(), $message->getTargetId());
|
||||
if (is_null($this->client)) {
|
||||
return false;
|
||||
}
|
||||
$options = [
|
||||
'qos' => $message->getQoS(),
|
||||
'retain' => $message->isRetain(),
|
||||
'dup' => false
|
||||
];
|
||||
$this->client->publish($topic, $message->getBiz(), $options);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -8,7 +8,10 @@ use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use phpseclib3\Crypt\RSA;
|
||||
|
||||
class MQTTManager
|
||||
/**
|
||||
* MQTT服务端接口
|
||||
*/
|
||||
class AuthorServerClient
|
||||
{
|
||||
/**
|
||||
* 私钥
|
||||
@@ -159,7 +162,7 @@ class MQTTManager
|
||||
return new BizResponse();
|
||||
}
|
||||
$data = json_decode($content, true);
|
||||
return new BizResponse($data['code'] ?? 0, $data['message'] ?? '', $data['data'] ?? []);
|
||||
return new BizResponse($data['code'] ?? 0, $data['message'] ?? '', $data['data'] ?? [], $data['err'] ?? '');
|
||||
|
||||
} catch (ClientException $e) {
|
||||
$body = $e->getResponse()->getBody()->getContents();
|
||||
37
src/biz/AppUpdateCallbackRequest.php
Normal file
37
src/biz/AppUpdateCallbackRequest.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace cn\com\maiyoule\mqttclient\biz;
|
||||
|
||||
use cn\com\maiyoule\mqttclient\IRequest;
|
||||
|
||||
class AppUpdateCallbackRequest extends IRequest
|
||||
{
|
||||
|
||||
public function path(): string
|
||||
{
|
||||
return 'app/callback/update';
|
||||
}
|
||||
|
||||
public function body(): array
|
||||
{
|
||||
return [
|
||||
'url' => $this->getUrl()
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
private string $url;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUrl(): string
|
||||
{
|
||||
return $this->url;
|
||||
}
|
||||
|
||||
public function setUrl(string $url): void
|
||||
{
|
||||
$this->url = $url;
|
||||
}
|
||||
}
|
||||
@@ -18,22 +18,60 @@ class AppUserCreateRequest extends IRequest
|
||||
'password' => $this->getPassword(),
|
||||
'fettle' => $this->getFettle(),
|
||||
'role' => $this->getRole(),
|
||||
'biz' => join(',', $this->getBiz())
|
||||
'publish' => join(',', $this->getPublish()),
|
||||
'subscribe' => join(',', $this->getSubscribe()),
|
||||
'expire' => $this->expireAt
|
||||
];
|
||||
}
|
||||
|
||||
private array $biz = [];
|
||||
private array $subscribe = [];
|
||||
|
||||
public function getBiz(): array
|
||||
public function getSubscribe(): array
|
||||
{
|
||||
return $this->biz;
|
||||
return $this->subscribe;
|
||||
}
|
||||
|
||||
public function setBiz(array $biz): void
|
||||
public function setSubscribe(array|string $subscribe): void
|
||||
{
|
||||
$this->biz = $biz;
|
||||
if (is_string($subscribe)) {
|
||||
$subscribe = [$subscribe];
|
||||
}
|
||||
$this->subscribe = $subscribe;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var string 过期时间
|
||||
*/
|
||||
private string $expireAt = '';
|
||||
|
||||
public function getExpireAt(): string
|
||||
{
|
||||
return $this->expireAt;
|
||||
}
|
||||
|
||||
public function setExpireAt(string $expireAt): void
|
||||
{
|
||||
$this->expireAt = $expireAt;
|
||||
}
|
||||
|
||||
|
||||
private array $publish = [];
|
||||
|
||||
public function getPublish(): array
|
||||
{
|
||||
return $this->publish;
|
||||
}
|
||||
|
||||
public function setPublish(array|string $publish): void
|
||||
{
|
||||
if (is_string($publish)) {
|
||||
$publish = [$publish];
|
||||
}
|
||||
|
||||
$this->publish = $publish;
|
||||
}
|
||||
|
||||
|
||||
private string $password = '';
|
||||
private string $fettle = '';
|
||||
private string $role = 'user';
|
||||
|
||||
36
src/biz/AppUserLogoutRequest.php
Normal file
36
src/biz/AppUserLogoutRequest.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace cn\com\maiyoule\mqttclient\biz;
|
||||
|
||||
use cn\com\maiyoule\mqttclient\IRequest;
|
||||
|
||||
class AppUserLogoutRequest extends IRequest
|
||||
{
|
||||
|
||||
public function path(): string
|
||||
{
|
||||
|
||||
return 'mqtt/user/kick';
|
||||
}
|
||||
|
||||
public function body(): array
|
||||
{
|
||||
return [
|
||||
'username' => $this->getUsername()
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
private string $username;
|
||||
|
||||
public function getUsername(): string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function setUsername(string $username): void
|
||||
{
|
||||
$this->username = $username;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -29,23 +29,37 @@ class AppUserUpdateRequest extends IRequest
|
||||
if (!is_null($this->fettle)) {
|
||||
$data['fettle'] = $this->fettle;
|
||||
}
|
||||
if (!is_null($this->biz)) {
|
||||
$data['biz'] = join(',', $this->biz);
|
||||
if (!empty($this->publish)) {
|
||||
$data['publish'] = join(',', $this->getPublish());
|
||||
}
|
||||
if (!empty($this->subscribe)) {
|
||||
$data['subscribe'] = join(',', $this->getSubscribe());
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
private ?array $biz = null;
|
||||
private array $subscribe = [];
|
||||
|
||||
public function getBiz(): array
|
||||
public function getSubscribe(): array
|
||||
{
|
||||
return $this->biz;
|
||||
return $this->subscribe;
|
||||
}
|
||||
|
||||
public function setBiz(array $biz): void
|
||||
public function setSubscribe(array|string $subscribe): void
|
||||
{
|
||||
$this->biz = $biz;
|
||||
if (is_string($subscribe)) {
|
||||
$subscribe = [$subscribe];
|
||||
}
|
||||
$this->subscribe = $subscribe;
|
||||
}
|
||||
|
||||
|
||||
private array $publish = [];
|
||||
|
||||
public function getPublish(): array
|
||||
{
|
||||
return $this->publish;
|
||||
}
|
||||
|
||||
private string $username;
|
||||
|
||||
34
src/biz/AppUsersRequest.php
Normal file
34
src/biz/AppUsersRequest.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace cn\com\maiyoule\mqttclient\biz;
|
||||
|
||||
use cn\com\maiyoule\mqttclient\IRequest;
|
||||
|
||||
class AppUsersRequest extends IRequest
|
||||
{
|
||||
|
||||
public function path(): string
|
||||
{
|
||||
return 'mqtt/users';
|
||||
}
|
||||
|
||||
private string $role;
|
||||
|
||||
public function getRole(): string
|
||||
{
|
||||
return $this->role;
|
||||
}
|
||||
|
||||
public function setRole(string $role): void
|
||||
{
|
||||
$this->role = $role;
|
||||
}
|
||||
|
||||
|
||||
public function body(): array
|
||||
{
|
||||
return [
|
||||
'role' => $this->role,
|
||||
];
|
||||
}
|
||||
}
|
||||
91
src/biz/client/SendMQTTMessage.php
Normal file
91
src/biz/client/SendMQTTMessage.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace cn\com\maiyoule\mqttclient\biz\client;
|
||||
|
||||
class SendMQTTMessage
|
||||
{
|
||||
/**
|
||||
* @var string 业务标识符
|
||||
*/
|
||||
private string $biz;
|
||||
/**
|
||||
* @var string 目标客户端ID
|
||||
*/
|
||||
private string $targetId;
|
||||
|
||||
private string $message;
|
||||
|
||||
private int $qoS = 0;
|
||||
|
||||
private bool $retain = false;
|
||||
|
||||
private function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public static function create(string $biz, string $targetId, string $message, int $qos = 0, bool $retain = false): SendMQTTMessage
|
||||
{
|
||||
$instance = new self();
|
||||
$instance->setBiz($biz);
|
||||
$instance->setTargetId($targetId);
|
||||
$instance->setMessage($message);
|
||||
$instance->setQoS($qos);
|
||||
$instance->setRetain($retain);
|
||||
return $instance;
|
||||
}
|
||||
|
||||
const QOS_0 = 0;
|
||||
const QOS_1 = 1;
|
||||
const QOS_2 = 2;
|
||||
|
||||
public function isRetain(): bool
|
||||
{
|
||||
return $this->retain;
|
||||
}
|
||||
|
||||
public function setRetain(bool $retain): void
|
||||
{
|
||||
$this->retain = $retain;
|
||||
}
|
||||
|
||||
public function getQoS(): int
|
||||
{
|
||||
return $this->qoS;
|
||||
}
|
||||
|
||||
public function setQoS(int $qoS): void
|
||||
{
|
||||
$this->qoS = $qoS;
|
||||
}
|
||||
|
||||
public function getMessage(): string
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
public function setMessage(string $message): void
|
||||
{
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
public function getTargetId(): string
|
||||
{
|
||||
return $this->targetId;
|
||||
}
|
||||
|
||||
public function setTargetId(string $targetId): void
|
||||
{
|
||||
$this->targetId = $targetId;
|
||||
}
|
||||
|
||||
public function getBiz(): string
|
||||
{
|
||||
return $this->biz;
|
||||
}
|
||||
|
||||
public function setBiz(string $biz): void
|
||||
{
|
||||
$this->biz = $biz;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,19 +6,22 @@ class BizResponse
|
||||
{
|
||||
private int $code;
|
||||
private string $message;
|
||||
private $data;
|
||||
private mixed $data;
|
||||
|
||||
private string $errCode;
|
||||
|
||||
/**
|
||||
* @param int $code
|
||||
* @param string $message
|
||||
* @param $data
|
||||
* @param array $data
|
||||
* @param string $errCode
|
||||
*/
|
||||
public function __construct(int $code = -1, string $message = '', $data = [])
|
||||
public function __construct(int $code = -1, string $message = '', mixed $data = [], string $errCode = '')
|
||||
{
|
||||
$this->code = $code;
|
||||
$this->message = $message;
|
||||
$this->data = $data;
|
||||
$this->errCode = $errCode;
|
||||
}
|
||||
|
||||
public function isSuccess(): bool
|
||||
@@ -49,7 +52,7 @@ class BizResponse
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getData()
|
||||
public function getData(): mixed
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
@@ -62,5 +65,15 @@ class BizResponse
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
public function getErrCode(): string
|
||||
{
|
||||
return $this->errCode;
|
||||
}
|
||||
|
||||
public function setErrCode(string $errCode): void
|
||||
{
|
||||
$this->errCode = $errCode;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -2,38 +2,49 @@
|
||||
|
||||
namespace cn\com\maiyoule\mqttclient\test;
|
||||
|
||||
use cn\com\maiyoule\mqttclient\biz\AppUpdateCallbackRequest;
|
||||
use cn\com\maiyoule\mqttclient\biz\AppUserCreateRequest;
|
||||
use cn\com\maiyoule\mqttclient\biz\AppUserDeleteRequest;
|
||||
use cn\com\maiyoule\mqttclient\biz\AppUserUpdateRequest;
|
||||
use cn\com\maiyoule\mqttclient\exception\ApiException;
|
||||
use cn\com\maiyoule\mqttclient\MQTTManager;
|
||||
use cn\com\maiyoule\mqttclient\AuthorServerClient;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use PHPUnit\Framework\Assert;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AppManagerTest extends TestCase
|
||||
{
|
||||
private MQTTManager $manager;
|
||||
private AuthorServerClient $manager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->manager = new MQTTManager();
|
||||
$this->manager->setAppId('6MMVTLW66D');
|
||||
$this->manager = new AuthorServerClient();
|
||||
$this->manager->setAppId('6N6QPV2FYD');
|
||||
$this->manager->setPrivateKey(file_get_contents(__DIR__ . '/prikey.pem'));
|
||||
$this->manager->setApi('http://localhost:8000/api/');
|
||||
$this->manager->setApi('https://mqttauthor_dev.maiyoule.com.cn/api/');
|
||||
$this->manager->setDebug(true);
|
||||
}
|
||||
|
||||
|
||||
public function testAdmin()
|
||||
{
|
||||
$request = new AppUserCreateRequest();
|
||||
$request->setPassword('123');
|
||||
$request->setRole('admin');
|
||||
$request->setBiz('ch');
|
||||
$biz = $this->manager->exec($request);
|
||||
$this->assertTrue($biz->isSuccess(), $biz->getMessage());
|
||||
}
|
||||
|
||||
|
||||
public function testRunUser()
|
||||
{
|
||||
|
||||
|
||||
try {
|
||||
$request = new AppUserCreateRequest();
|
||||
$request->setPassword('111');
|
||||
$request->setFettle('');
|
||||
$request->setRole('admin');
|
||||
$request->setBiz(['ch','ws']);
|
||||
$request->setBiz(['ch', 'ws']);
|
||||
|
||||
$biz = $this->manager->exec($request);
|
||||
if (!$biz->isSuccess()) {
|
||||
@@ -71,4 +82,16 @@ class AppManagerTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function testUpdateAppUrl()
|
||||
{
|
||||
$request = new AppUpdateCallbackRequest();
|
||||
$request->setUrl('http://202.200.18.46:8000/api.php');
|
||||
try {
|
||||
$biz = $this->manager->exec($request);
|
||||
$this->assertTrue($biz->isSuccess(), $biz->getMessage());
|
||||
} catch (GuzzleException|ApiException $e) {
|
||||
$this->fail($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
39
tests/AuthorAppMQTTClientTest.php
Normal file
39
tests/AuthorAppMQTTClientTest.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace cn\com\maiyoule\mqttclient\test;
|
||||
|
||||
use cn\com\maiyoule\mqttclient\AuthorAppMQTTClient;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AuthorAppMQTTClientTest extends TestCase
|
||||
{
|
||||
private AuthorAppMQTTClient $client;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
$this->client->stop();
|
||||
}
|
||||
|
||||
|
||||
public function testInit()
|
||||
{
|
||||
$this->client = AuthorAppMQTTClient::getInstance();
|
||||
$this->client->setServer('202.200.18.22');
|
||||
$this->client->setAppid('6N6QPV2FYD');
|
||||
$this->client
|
||||
->setClientId('6NEC8SKNK5')
|
||||
->setUsername('6N6QPV2FYD')
|
||||
->setPassword('123')
|
||||
->registerEvent(AuthorAppMQTTClient::EVENT_CONNECT, function ($connection) {
|
||||
print_r('连接成功');
|
||||
});
|
||||
$result = $this->client->start();
|
||||
|
||||
self::assertTrue($result, '启动失败');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user