MQTT 管理客户端
This commit is contained in:
commit
5eefef9b19
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
composer.lock
|
||||||
|
.idea/
|
||||||
|
vendor/
|
||||||
|
tests/
|
30
composer.json
Normal file
30
composer.json
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
{
|
||||||
|
"name": "maiyoule/mqttclient_author",
|
||||||
|
"type": "library",
|
||||||
|
"description": "MQTT管理模块操作库",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Shengli",
|
||||||
|
"email": "mothz@126.com",
|
||||||
|
"role": "developer"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"php": ">=8.0",
|
||||||
|
"guzzlehttp/guzzle": "~6.0",
|
||||||
|
"phpseclib/phpseclib": "~3.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^11.0"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"cn\\com\\maiyoule\\mqttclient\\": "src"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload-dev": {
|
||||||
|
"psr-4": {
|
||||||
|
"cn\\com\\maiyoule\\mqttclient\\test\\": "tests"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
20
src/IRequest.php
Normal file
20
src/IRequest.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace cn\com\maiyoule\mqttclient;
|
||||||
|
|
||||||
|
abstract class IRequest
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 请求路径
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
abstract public function path(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 载体
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
abstract public function body(): array;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
175
src/MQTTManager.php
Normal file
175
src/MQTTManager.php
Normal file
@ -0,0 +1,175 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace cn\com\maiyoule\mqttclient;
|
||||||
|
|
||||||
|
use cn\com\maiyoule\mqttclient\exception\ApiException;
|
||||||
|
use cn\com\maiyoule\mqttclient\response\BizResponse;
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\Exception\ClientException;
|
||||||
|
use phpseclib3\Crypt\RSA;
|
||||||
|
|
||||||
|
class MQTTManager
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* 私钥
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected string $privateKey = '';
|
||||||
|
/**
|
||||||
|
* 签名类型
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected string $type = 'rsa';
|
||||||
|
/**
|
||||||
|
* 应用ID
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected string $appId = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 接口地址
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected string $api = 'https://mqttauthor.maiyoule.com.cn';
|
||||||
|
/**
|
||||||
|
* 版本号
|
||||||
|
*/
|
||||||
|
public const VERSION = "1.0";
|
||||||
|
/**
|
||||||
|
* 是否为调试模式
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
protected bool $debug = false;
|
||||||
|
|
||||||
|
public function getPrivateKey(): string
|
||||||
|
{
|
||||||
|
return $this->privateKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setPrivateKey(string $privateKey): void
|
||||||
|
{
|
||||||
|
$this->privateKey = $privateKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getType(): string
|
||||||
|
{
|
||||||
|
return $this->type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setType(string $type): void
|
||||||
|
{
|
||||||
|
$this->type = $type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAppId(): string
|
||||||
|
{
|
||||||
|
return $this->appId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setAppId(string $appId): void
|
||||||
|
{
|
||||||
|
$this->appId = $appId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getApi(): string
|
||||||
|
{
|
||||||
|
return $this->api;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setApi(string $api): void
|
||||||
|
{
|
||||||
|
$this->api = $api;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isDebug(): bool
|
||||||
|
{
|
||||||
|
return $this->debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setDebug(bool $debug): void
|
||||||
|
{
|
||||||
|
$this->debug = $debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ?Client $client = null;
|
||||||
|
|
||||||
|
private function getHttpClient(): Client
|
||||||
|
{
|
||||||
|
if ($this->client == null) {
|
||||||
|
$this->client = new Client([
|
||||||
|
'base_uri' => $this->api,
|
||||||
|
'allow_redirects' => [
|
||||||
|
'max' => 2
|
||||||
|
],
|
||||||
|
'debug' => $this->debug,
|
||||||
|
'headers' => [
|
||||||
|
'user-agent' => 'MaiYouLeMQTTClientAuthor/' . self::VERSION,
|
||||||
|
'Accept' => 'application/json'
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
return $this->client;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private function sign(string $str)
|
||||||
|
{
|
||||||
|
$prikey = RSA::loadPrivateKey($this->privateKey);
|
||||||
|
$result = $prikey->sign($str);
|
||||||
|
return base64_encode($result);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param IRequest $request
|
||||||
|
* @return BizResponse
|
||||||
|
* @throws ApiException
|
||||||
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||||
|
*/
|
||||||
|
public function exec(IRequest $request): BizResponse
|
||||||
|
{
|
||||||
|
//生成签名
|
||||||
|
$url = sprintf('%s%s', $this->api, $request->path());
|
||||||
|
$urls = parse_url($url);
|
||||||
|
|
||||||
|
$path = $urls['path'];
|
||||||
|
$body = $request->body();
|
||||||
|
|
||||||
|
$body['appid'] = $this->getAppId();
|
||||||
|
$body['type'] = $this->getType();
|
||||||
|
|
||||||
|
ksort($body, SORT_NATURAL);
|
||||||
|
|
||||||
|
$waitItem = [];
|
||||||
|
foreach ($body as $key => $value) {
|
||||||
|
$waitItem[] = $key . '=' . urlencode($value);
|
||||||
|
}
|
||||||
|
$waitItem[] = urlencode($path);
|
||||||
|
$str = join('&', $waitItem);
|
||||||
|
$sign = $this->sign($str);
|
||||||
|
$body['sign'] = $sign;
|
||||||
|
$http = $this->getHttpClient();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$result = $http->request('POST', $path, [
|
||||||
|
'form_params' => $body
|
||||||
|
]);
|
||||||
|
$content = $result->getBody()->getContents();
|
||||||
|
if (empty($content)) {
|
||||||
|
//没有反馈内容
|
||||||
|
return new BizResponse();
|
||||||
|
}
|
||||||
|
$data = json_decode($content, true);
|
||||||
|
return new BizResponse($data['code'] ?? 0, $data['message'] ?? '', $data['data'] ?? []);
|
||||||
|
|
||||||
|
} catch (ClientException $e) {
|
||||||
|
$body = $e->getResponse()->getBody()->getContents();
|
||||||
|
$message = '';
|
||||||
|
if (!empty($body)) {
|
||||||
|
$data = json_decode($body, true);
|
||||||
|
$message = $data['message'] ?? '';
|
||||||
|
}
|
||||||
|
throw new ApiException($message, $e->getResponse()->getStatusCode());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
74
src/biz/AppUserCreateRequest.php
Normal file
74
src/biz/AppUserCreateRequest.php
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace cn\com\maiyoule\mqttclient\biz;
|
||||||
|
|
||||||
|
use cn\com\maiyoule\mqttclient\IRequest;
|
||||||
|
|
||||||
|
class AppUserCreateRequest extends IRequest
|
||||||
|
{
|
||||||
|
|
||||||
|
public function path(): string
|
||||||
|
{
|
||||||
|
return 'mqtt/user/create';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function body(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'password' => $this->getPassword(),
|
||||||
|
'fettle' => $this->getFettle(),
|
||||||
|
'role' => $this->getRole(),
|
||||||
|
'biz' => join(',', $this->getBiz())
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private array $biz = [];
|
||||||
|
|
||||||
|
public function getBiz(): array
|
||||||
|
{
|
||||||
|
return $this->biz;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setBiz(array $biz): void
|
||||||
|
{
|
||||||
|
$this->biz = $biz;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string $password = '';
|
||||||
|
private string $fettle = '';
|
||||||
|
private string $role = 'user';
|
||||||
|
|
||||||
|
|
||||||
|
public function getRole(): string
|
||||||
|
{
|
||||||
|
return $this->role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setRole(string $role): void
|
||||||
|
{
|
||||||
|
$this->role = $role;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function getPassword(): string
|
||||||
|
{
|
||||||
|
return $this->password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setPassword(string $password): void
|
||||||
|
{
|
||||||
|
$this->password = $password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFettle(): string
|
||||||
|
{
|
||||||
|
return $this->fettle;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setFettle(string $fettle): void
|
||||||
|
{
|
||||||
|
$this->fettle = $fettle;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
39
src/biz/AppUserDeleteRequest.php
Normal file
39
src/biz/AppUserDeleteRequest.php
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace cn\com\maiyoule\mqttclient\biz;
|
||||||
|
|
||||||
|
use cn\com\maiyoule\mqttclient\IRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除用户请求
|
||||||
|
*/
|
||||||
|
class AppUserDeleteRequest extends IRequest
|
||||||
|
{
|
||||||
|
|
||||||
|
public function path(): string
|
||||||
|
{
|
||||||
|
return 'mqtt/user/delete';
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
110
src/biz/AppUserUpdateRequest.php
Normal file
110
src/biz/AppUserUpdateRequest.php
Normal file
@ -0,0 +1,110 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace cn\com\maiyoule\mqttclient\biz;
|
||||||
|
|
||||||
|
use cn\com\maiyoule\mqttclient\IRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户修改请求
|
||||||
|
*/
|
||||||
|
class AppUserUpdateRequest extends IRequest
|
||||||
|
{
|
||||||
|
|
||||||
|
public function path(): string
|
||||||
|
{
|
||||||
|
return 'mqtt/user/update';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function body(): array
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'username' => $this->getUsername(),
|
||||||
|
'state' => $this->isState() ? 0 : 1,
|
||||||
|
'role' => $this->getRole()
|
||||||
|
];
|
||||||
|
|
||||||
|
if (!is_null($this->password)) {
|
||||||
|
$data['password'] = $this->password;
|
||||||
|
}
|
||||||
|
if (!is_null($this->fettle)) {
|
||||||
|
$data['fettle'] = $this->fettle;
|
||||||
|
}
|
||||||
|
if (!is_null($this->biz)) {
|
||||||
|
$data['biz'] = join(',', $this->biz);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ?array $biz = null;
|
||||||
|
|
||||||
|
public function getBiz(): array
|
||||||
|
{
|
||||||
|
return $this->biz;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setBiz(array $biz): void
|
||||||
|
{
|
||||||
|
$this->biz = $biz;
|
||||||
|
}
|
||||||
|
|
||||||
|
private string $username;
|
||||||
|
private ?string $password = null;
|
||||||
|
private bool $state = true;
|
||||||
|
private ?string $fettle = null;
|
||||||
|
|
||||||
|
private string $role = '';
|
||||||
|
|
||||||
|
public function getRole(): string
|
||||||
|
{
|
||||||
|
return $this->role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setRole(string $role): void
|
||||||
|
{
|
||||||
|
$this->role = $role;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function getUsername(): string
|
||||||
|
{
|
||||||
|
return $this->username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setUsername(string $username): void
|
||||||
|
{
|
||||||
|
$this->username = $username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPassword(): string
|
||||||
|
{
|
||||||
|
return $this->password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setPassword(string $password): void
|
||||||
|
{
|
||||||
|
$this->password = $password;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isState(): bool
|
||||||
|
{
|
||||||
|
return $this->state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setState(bool $state): void
|
||||||
|
{
|
||||||
|
$this->state = $state;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFettle(): string
|
||||||
|
{
|
||||||
|
return $this->fettle;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setFettle(string $fettle): void
|
||||||
|
{
|
||||||
|
$this->fettle = $fettle;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
14
src/exception/ApiException.php
Normal file
14
src/exception/ApiException.php
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace cn\com\maiyoule\mqttclient\exception;
|
||||||
|
|
||||||
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
class ApiException extends \Exception
|
||||||
|
{
|
||||||
|
#[Pure] public function __construct(string $message = "", int $code = 0, ?Throwable $previous = null)
|
||||||
|
{
|
||||||
|
parent::__construct($message, $code, $previous);
|
||||||
|
}
|
||||||
|
}
|
66
src/response/BizResponse.php
Normal file
66
src/response/BizResponse.php
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace cn\com\maiyoule\mqttclient\response;
|
||||||
|
|
||||||
|
class BizResponse
|
||||||
|
{
|
||||||
|
private int $code;
|
||||||
|
private string $message;
|
||||||
|
private $data;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $code
|
||||||
|
* @param string $message
|
||||||
|
* @param $data
|
||||||
|
*/
|
||||||
|
public function __construct(int $code = -1, string $message = '', $data = [])
|
||||||
|
{
|
||||||
|
$this->code = $code;
|
||||||
|
$this->message = $message;
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isSuccess(): bool
|
||||||
|
{
|
||||||
|
return $this->code == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCode(): int
|
||||||
|
{
|
||||||
|
return $this->code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setCode(int $code): void
|
||||||
|
{
|
||||||
|
$this->code = $code;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMessage(): string
|
||||||
|
{
|
||||||
|
return $this->message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setMessage(string $message): void
|
||||||
|
{
|
||||||
|
$this->message = $message;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getData()
|
||||||
|
{
|
||||||
|
return $this->data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param mixed $data
|
||||||
|
*/
|
||||||
|
public function setData($data): void
|
||||||
|
{
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user