Commit 05955bc6 by banice

lumen准备升级到5.6

parent 433d7c84
This diff is collapsed. Click to expand it.
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/7/29
* Time: 下午3:54
*/
namespace Lib\aliyun;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Support\Facades\Log;
use Lib\aliyun\api\BaseApi;
use Lib\aliyun\sign\Sign;
use function MongoDB\BSON\toJSON;
use Psr\Http\Message\ResponseInterface;
class Client
{
/**
* @var BaseApi
*/
private $api;
/**
* @var \GuzzleHttp\Client
*/
private $client;
/**
* @var ResponseInterface
*/
private $response;
/**
* @var bool
*/
private $result = false;
/**
* @var string
*
*/
private $errorMessage = '';
/**
* @var string
*/
private $requestId;
/**
* @var array
*/
private $responseData;
/**
* aliyun iot uri
*/
// const URI = 'https://iot.aliyuncs.com/';
const URI = 'https://iot.cn-shanghai.aliyuncs.com/';
public function __construct()
{
$this->client = new \GuzzleHttp\Client();
}
/**
* @param string $method
* @return bool
*/
public function request($method = 'GET')
{
$sign = new Sign($this->api);
$signature = $sign->sign($method);
if ($this->api instanceof BaseApi) {
$this->api->setSignature($signature);
}
$request = $this->api->getRequest();
$request['Signature'] = $signature;
$str = '';
foreach ($request as $key => $value) {
$str .= '&' . $key . '=' . urlencode($value);
}
$str = trim($str, '&');
$this->response = $this->client->get(self::URI . '?' . $str);
$data = json_decode($this->response->getBody(), true);
Log::debug($data);
if ($this->response->getStatusCode() === 200) {
if ($data['Success'] === true) {
$this->result = true;
$this->requestId = $data['RequestId'];
$this->responseData = $data;
if (isset($data['Data'])) {
$this->api->parseResponse($data);
}
} else {
$this->errorMessage = $data['ErrorMessage'];
$this->result = false;
}
}
return $this->result;
}
/**
* @return BaseApi
*/
public function getApi(): BaseApi
{
return $this->api;
}
/**
* @param BaseApi $api
*/
public function setApi(BaseApi $api): void
{
$this->api = $api;
}
/**
* @return \GuzzleHttp\Client
*/
public function getClient(): \GuzzleHttp\Client
{
return $this->client;
}
/**
* @param \GuzzleHttp\Client $client
*/
public function setClient(\GuzzleHttp\Client $client): void
{
$this->client = $client;
}
/**
* @return ResponseInterface
*/
public function getResponse(): ResponseInterface
{
return $this->response;
}
/**
* @return bool
*/
public function isResult(): bool
{
return $this->result;
}
/**
* @param bool $result
*/
public function setResult(bool $result): void
{
$this->result = $result;
}
/**
* @return string
*/
public function getErrorMessage(): string
{
return $this->errorMessage;
}
/**
* @param string $errorMessage
*/
public function setErrorMessage(string $errorMessage): void
{
$this->errorMessage = $errorMessage;
}
/**
* @return string
*/
public function getRequestId(): string
{
return $this->requestId;
}
/**
* @param string $requestId
*/
public function setRequestId(string $requestId): void
{
$this->requestId = $requestId;
}
/**
* @return array
*/
public function getResponseData(): array
{
return $this->responseData;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/7/29
* Time: 下午3:51
*/
namespace Lib\aliyun;
class Config
{
const FORMAT = 'json';
const Version = '2018-01-20';
const AccessKeyId = 'LTAItZVbkVaVXifu';
const SignatureMethod = 'HMAC-SHA1';
const SignatureVersion = '1.0';
const RegionId = 'cn-shanghai';
// LTAIRUkuhDA4poTj ysh accessId
//1WE7UvMuA0QZIwl8JjekTXB9ccxkpy ysh secret
const ACCESS_SECRET = 'Irko4313cUEbb1RYXGWEz6s6VgNETG';
// const ACCESS_SECRET = 'Irko4313cUEbb1RYXGWEz6s6VgNETG';
const ACCESS_KEY = 'LTAItZVbkVaVXifu';
// const ACCESS_KEY='LTAIRUkuhDA4poTj';
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/7/29
* Time: 下午3:56
*/
namespace Lib\aliyun\api;
use Lib\aliyun\Config;
abstract class BaseApi
{
/**
* @var string
*/
protected $action = '';
/**
* @var string
*/
protected $format = Config::FORMAT;
/**
* @var string
*/
protected $version = Config::Version;
/**
* @var string
*/
protected $accessKeyId = Config::AccessKeyId;
/**
* @var string
*/
protected $signatureMethod = Config::SignatureMethod;
/**
* @var string
*/
protected $signatureVersion = Config::SignatureVersion;
/**
* @var string
*/
protected $regionId = Config::RegionId;
/**
* @var false|string
*/
protected $timestamp = '';
/**
* @var string
*/
protected $signatureNonce = '';
/**
* @var string
*/
protected $signature = '';
/**
* @var array
*/
protected $request = [];
/**
* @var string
*/
protected $errorMessage;
/**
* @return array
*/
abstract public function getRequest(): array;
/**
* @param array $data
*/
abstract public function parseResponse(array $data): void;
/**
* BaseApi constructor.
*/
public function __construct()
{
$this->timestamp = date('Y-m-d\TH:i:s\Z', time() - 60 * 60 * 8);
$this->signatureNonce = md5(uniqid(mt_rand(), true));
}
/**
* @return array
*/
protected function getCommonRequest()
{
return [
'Action' => $this->action,
'Format' => $this->format,
'Version' => $this->version,
'AccessKeyId' => $this->accessKeyId,
'SignatureMethod' => $this->signatureMethod,
'Timestamp' => $this->timestamp,
'SignatureVersion' => $this->signatureVersion,
'SignatureNonce' => $this->signatureNonce,
'RegionId' => $this->regionId,
];
}
/**
* @return string
*/
public function getFormat(): string
{
return $this->format;
}
/**
* @param string $format
*/
public function setFormat(string $format): void
{
$this->format = $format;
}
/**
* @return string
*/
public function getVersion(): string
{
return $this->version;
}
/**
* @param string $version
*/
public function setVersion(string $version): void
{
$this->version = $version;
}
/**
* @return string
*/
public function getAccessKeyId(): string
{
return $this->accessKeyId;
}
/**
* @param string $accessKeyId
*/
public function setAccessKeyId(string $accessKeyId): void
{
$this->accessKeyId = $accessKeyId;
}
/**
* @return string
*/
public function getSignatureMethod(): string
{
return $this->signatureMethod;
}
/**
* @param string $signatureMethod
*/
public function setSignatureMethod(string $signatureMethod): void
{
$this->signatureMethod = $signatureMethod;
}
/**
* @return string
*/
public function getSignatureVersion(): string
{
return $this->signatureVersion;
}
/**
* @param string $signatureVersion
*/
public function setSignatureVersion(string $signatureVersion): void
{
$this->signatureVersion = $signatureVersion;
}
/**
* @return string
*/
public function getRegionId(): string
{
return $this->regionId;
}
/**
* @param string $regionId
*/
public function setRegionId(string $regionId): void
{
$this->regionId = $regionId;
}
/**
* @return false|string
*/
public function getTimestamp()
{
return $this->timestamp;
}
/**
* @param false|string $timestamp
*/
public function setTimestamp($timestamp): void
{
$this->timestamp = $timestamp;
}
/**
* @return string
*/
public function getSignatureNonce(): string
{
return $this->signatureNonce;
}
/**
* @param string $signatureNonce
*/
public function setSignatureNonce(string $signatureNonce): void
{
$this->signatureNonce = $signatureNonce;
}
/**
* @return string
*/
public function getSignature(): string
{
return $this->signature;
}
/**
* @param string $signature
*/
public function setSignature(string $signature): void
{
$this->signature = $signature;
}
/**
* @return string
*/
public function getAction(): string
{
return $this->action;
}
/**
* @param string $action
*/
public function setAction(string $action): void
{
$this->action = $action;
}
/**
* @return string
*/
public function getErrorMessage(): string
{
return $this->errorMessage;
}
/**
* @param string $errorMessage
*/
public function setErrorMessage(string $errorMessage): void
{
$this->errorMessage = $errorMessage;
}
/**
* @param array $request
*/
public function setRequest(array $request): void
{
$this->request = $request;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/7/29
* Time: 下午3:55
*/
namespace Lib\aliyun\api;
interface IApi
{
public function getRequest(): array;
public function parseResponse():void ;
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/7/29
* Time: 下午3:54
*/
namespace Lib\aliyun\api;
class PublishToTopic extends BaseApi
{
protected $action = 'Pub';
/**
* @var string
*/
private $productKey = '';
/**
* @var string
*/
private $messageContent = '';
/**
* @var string
*/
private $topicFullName = '';
/**
* @var string
*/
private $messageId;
/**
* @var int
*/
private $qos = 0;
public function __construct()
{
parent::__construct();
}
/**
* @return array
* @throws \Exception
*/
public function getRequest(): array
{
$parameters = get_object_vars($this);
// foreach ($parameters as $parameter => $value) {
// if ($parameter != 'signature') {
//
// }
// }
$this->request = [
'Action' => $this->action,
'Format' => $this->format,
'Version' => $this->version,
'AccessKeyId' => $this->accessKeyId,
'SignatureMethod' => $this->signatureMethod,
'Timestamp' => $this->timestamp,
'SignatureVersion' => $this->signatureVersion,
'SignatureNonce' => $this->signatureNonce,
'RegionId' => $this->regionId,
'ProductKey' => $this->productKey,
'MessageContent' => $this->messageContent,
'TopicFullName' => $this->topicFullName,
'Qos' => $this->qos,
];
ksort($this->request);
return $this->request;
}
public function parseResponse(array $data): void
{
if ($data['Success'] === true) {
$this->messageId = $data['MessageId'];
}
}
/**
* @return string
*/
public function getProductKey(): string
{
return $this->productKey;
}
/**
* @param string $productKey
*/
public function setProductKey(string $productKey): void
{
$this->productKey = $productKey;
}
/**
* @return string
*/
public function getMessageContent(): string
{
return $this->messageContent;
}
/**
* @param string $messageContent
*/
public function setMessageContent(string $messageContent): void
{
$messageContent = base64_encode($messageContent);
$this->messageContent = $messageContent;
}
/**
* @return string
*/
public function getTopicFullName(): string
{
return $this->topicFullName;
}
/**
* @param string $topicFullName
*/
public function setTopicFullName(string $topicFullName): void
{
$this->topicFullName = $topicFullName;
}
/**
* @return int
*/
public function getQos(): int
{
return $this->qos;
}
/**
* @param int $qos
*/
public function setQos(int $qos): void
{
$this->qos = $qos;
}
/**
* @return string
*/
public function getMessageId(): string
{
return $this->messageId;
}
/**
* @param string $messageId
*/
public function setMessageId(string $messageId): void
{
$this->messageId = $messageId;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/1
* Time: 上午10:28
*/
namespace Lib\aliyun\api\device;
use Lib\aliyun\api\BaseApi;
class GetDevicesStatus extends BaseApi
{
/**
* @var string
*/
protected $action = 'GetDeviceStatus';
/**
* @var string
*/
private $iotId;
/**
* @var string
*/
private $productKey;
/**
* @var string
*/
private $deviceName;
/**
* @var string
*/
private $status;
/**
* @return array
*/
public function getRequest(): array
{
if (isset($this->iotId)) {
$tmp = [
'IotId' => $this->iotId,
];
} else {
$tmp = [
'ProductKey' => $this->productKey,
'DeviceName' => $this->deviceName,
];
}
return $tmp + $this->getCommonRequest();
}
public function parseResponse(array $data): void
{
if ($data['Success'] === true) {
switch ($data['Data']['Status']) {
case 'ONLINE':
$this->status = 1;
break;
case 'OFFLINE':
$this->status = 0;
break;
case 'UNACTIVE':
$this->status = 2;
break;
case 'DISABLE':
$this->status = 3;
break;
default:
break;
}
}
}
/**
* @return string
*/
public function getAction(): string
{
return $this->action;
}
/**
* @param string $action
*/
public function setAction(string $action): void
{
$this->action = $action;
}
/**
* @return string
*/
public function getIotId(): string
{
return $this->iotId;
}
/**
* @param string $iotId
*/
public function setIotId(string $iotId): void
{
$this->iotId = $iotId;
}
/**
* @return string
*/
public function getProductKey(): string
{
return $this->productKey;
}
/**
* @param string $productKey
*/
public function setProductKey(string $productKey): void
{
$this->productKey = $productKey;
}
/**
* @return string
*/
public function getDeviceName(): string
{
return $this->deviceName;
}
/**
* @param string $deviceName
*/
public function setDeviceName(string $deviceName): void
{
$this->deviceName = $deviceName;
}
/**
* @return string
*/
public function getStatus(): string
{
return $this->status;
}
/**
* @param string $status
*/
public function setStatus(string $status): void
{
$this->status = $status;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/7
* Time: 上午10:05
*/
namespace Lib\aliyun\api\device;
use Lib\aliyun\api\BaseApi;
class QueryDevice extends BaseApi
{
/**
* @var string
*/
protected $action = 'QueryDevice';
/**
* @var string
*/
private $productKey;
/**
* @var int
*/
private $pageSize = 10;
/**
* @var int
*/
private $currentPage = 1;
public function getRequest(): array
{
return $this->getCommonRequest() + [
'ProductKey' => $this->productKey,
'PageSize' => $this->pageSize,
'CurrentPage' => $this->currentPage,
];
// TODO: Implement getRequest() method.
}
public function parseResponse(array $data): void
{
}
/**
* @return string
*/
public function getAction(): string
{
return $this->action;
}
/**
* @param string $action
*/
public function setAction(string $action): void
{
$this->action = $action;
}
/**
* @return string
*/
public function getProductKey(): string
{
return $this->productKey;
}
/**
* @param string $productKey
*/
public function setProductKey(string $productKey): void
{
$this->productKey = $productKey;
}
/**
* @return int
*/
public function getPageSize(): int
{
return $this->pageSize;
}
/**
* @param int $pageSize
*/
public function setPageSize(int $pageSize): void
{
$this->pageSize = $pageSize;
}
/**
* @return int
*/
public function getCurrentPage(): int
{
return $this->currentPage;
}
/**
* @param int $currentPage
*/
public function setCurrentPage(int $currentPage): void
{
$this->currentPage = $currentPage;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/7
* Time: 上午9:52
*/
namespace Lib\aliyun\api\device;
use Lib\aliyun\api\BaseApi;
class QueryDeviceDetail extends BaseApi
{
/**
* @var string
*/
protected $action = 'QueryDeviceDetail';
/**
* @var string
*/
private $iotId;
/**
* @var string
*/
private $productKey;
/**
* @var string
*/
private $deviceName;
/**
* @var string
*/
private $productName;
/**
* @var string
*/
private $deviceSecret;
/**
* @var string
*/
private $gmtGreate;
/**
* @var string
*/
private $gmtActive;
/**
* @var
*/
private $gmtOnline;
/**
* @var string
*/
private $status;
public function getRequest(): array
{
return $this->getCommonRequest() + [
'IotId' => $this->iotId,
'ProductKey' => $this->productKey,
'DeviceName' => $this->deviceName,
];
}
public function parseResponse(array $data): void
{
$data = $data['Data'];
$this->productName = $data['ProductKey'];
$this->deviceSecret = $data['DeviceSecret'];
$this->iotId = $data['IotId'];
$this->gmtGreate = $data['GmtGreate'];
$this->gmtActive = $data['GmtActive'];
$this->gmtOnline = $data['GmtOnline'];
$this->status = $data['Status'];
}
/**
* @return string
*/
public function getAction(): string
{
return $this->action;
}
/**
* @param string $action
*/
public function setAction(string $action): void
{
$this->action = $action;
}
/**
* @return string
*/
public function getIotId(): string
{
return $this->iotId;
}
/**
* @param string $iotId
*/
public function setIotId(string $iotId): void
{
$this->iotId = $iotId;
}
/**
* @return string
*/
public function getProductKey(): string
{
return $this->productKey;
}
/**
* @param string $productKey
*/
public function setProductKey(string $productKey): void
{
$this->productKey = $productKey;
}
/**
* @return string
*/
public function getDeviceName(): string
{
return $this->deviceName;
}
/**
* @param string $deviceName
*/
public function setDeviceName(string $deviceName): void
{
$this->deviceName = $deviceName;
}
/**
* @return string
*/
public function getProductName(): string
{
return $this->productName;
}
/**
* @param string $productName
*/
public function setProductName(string $productName): void
{
$this->productName = $productName;
}
/**
* @return string
*/
public function getDeviceSecret(): string
{
return $this->deviceSecret;
}
/**
* @param string $deviceSecret
*/
public function setDeviceSecret(string $deviceSecret): void
{
$this->deviceSecret = $deviceSecret;
}
/**
* @return string
*/
public function getGmtGreate(): string
{
return $this->gmtGreate;
}
/**
* @param string $gmtGreate
*/
public function setGmtGreate(string $gmtGreate): void
{
$this->gmtGreate = $gmtGreate;
}
/**
* @return string
*/
public function getGmtActive(): string
{
return $this->gmtActive;
}
/**
* @param string $gmtActive
*/
public function setGmtActive(string $gmtActive): void
{
$this->gmtActive = $gmtActive;
}
/**
* @return mixed
*/
public function getGmtOnline()
{
return $this->gmtOnline;
}
/**
* @param mixed $gmtOnline
*/
public function setGmtOnline($gmtOnline): void
{
$this->gmtOnline = $gmtOnline;
}
/**
* @return string
*/
public function getStatus(): string
{
return $this->status;
}
/**
* @param string $status
*/
public function setStatus(string $status): void
{
$this->status = $status;
}
}
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/7
* Time: 上午9:36
*/
namespace Lib\aliyun\api\device;
use Lib\aliyun\api\BaseApi;
class RegisterDevice extends BaseApi
{
/**
* @var string
*/
protected $action = 'RegisterDevice';
/**
* @var string
*/
private $productKey;
/**
* @var string
*/
private $deviceName;
/**
* @var array
*/
private $deviceData;
/**
* @var string
*/
private $deviceSecret;
/**
* @var string
*/
private $deviceIotId;
/**
* @return array
*/
public function getRequest(): array
{
return $this->getCommonRequest() + ['ProductKey' => $this->productKey, 'DeviceName' => $this->deviceName];
}
/**
* @param array $data
*/
public function parseResponse(array $data): void
{
$this->deviceData = $data['Data'];
$this->deviceSecret = $this->deviceData['DeviceSecret'];
$this->deviceIotId = $this->deviceData['IotId'];
}
/**
* @return string
*/
public function getAction(): string
{
return $this->action;
}
/**
* @param string $action
*/
public function setAction(string $action): void
{
$this->action = $action;
}
/**
* @return string
*/
public function getProductKey(): string
{
return $this->productKey;
}
/**
* @param string $productKey
*/
public function setProductKey(string $productKey): void
{
$this->productKey = $productKey;
}
/**
* @return string
*/
public function getDeviceName(): string
{
return $this->deviceName;
}
/**
* @param string $deviceName
*/
public function setDeviceName(string $deviceName): void
{
$this->deviceName = $deviceName;
}
/**
* @return array
*/
public function getDeviceData(): array
{
return $this->deviceData;
}
/**
* @param array $deviceData
*/
public function setDeviceData(array $deviceData): void
{
$this->deviceData = $deviceData;
}
/**
* @return string
*/
public function getDeviceSecret(): string
{
return $this->deviceSecret;
}
/**
* @param string $deviceSecret
*/
public function setDeviceSecret(string $deviceSecret): void
{
$this->deviceSecret = $deviceSecret;
}
/**
* @return string
*/
public function getDeviceIotId(): string
{
return $this->deviceIotId;
}
/**
* @param string $deviceIotId
*/
public function setDeviceIotId(string $deviceIotId): void
{
$this->deviceIotId = $deviceIotId;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/7
* Time: 下午3:45
*/
namespace Lib\aliyun\api\product;
use Lib\aliyun\api\BaseApi;
/**
* Class CreateProduct
* @package Lib\aliyun\api\product
*/
class CreateProduct extends BaseApi
{
/**
* @var string
*/
protected $action = 'CreateProduct';
/**
* @var string
*/
private $name;
/**
* @var int
*/
private $nodeType;
/**
* @var int
*/
private $catId;
/**
* @var string
*/
private $desc;
/**
* @var string
*/
private $productName;
/**
* @var string
*/
private $productKey;
/**
* @var string
*/
private $productDesc;
/**
* @var string
*/
private $createUserId;
/**
* @var string
*/
private $fromSource;
public function getRequest(): array
{
$return = [
'Name' => $this->name,
'NodeType' => $this->nodeType,
'CatId' => $this->catId,
'Desc' => $this->desc,
];
$return = array_filter($return, function ($value) {
return isset($value);
});
return $this->getCommonRequest() + $return;
}
public function parseResponse(array $data): void
{
$data = $data['ProductInfo'];
$this->productKey = $data['ProductKey'];
$this->productDesc = $data['ProductDesc'];
$this->fromSource = $data['FromSource'];
}
/**
* @return string
*/
public function getAction(): string
{
return $this->action;
}
/**
* @param string $action
*/
public function setAction(string $action): void
{
$this->action = $action;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @return int
*/
public function getNodeType(): int
{
return $this->nodeType;
}
/**
* @param int $nodeType
*/
public function setNodeType(int $nodeType): void
{
$this->nodeType = $nodeType;
}
/**
* @return int
*/
public function getCatId(): int
{
return $this->catId;
}
/**
* @param int $catId
*/
public function setCatId(int $catId): void
{
$this->catId = $catId;
}
/**
* @return string
*/
public function getDesc(): string
{
return $this->desc;
}
/**
* @param string $desc
*/
public function setDesc(string $desc): void
{
$this->desc = $desc;
}
/**
* @return string
*/
public function getProductName(): string
{
return $this->productName;
}
/**
* @param string $productName
*/
public function setProductName(string $productName): void
{
$this->productName = $productName;
}
/**
* @return string
*/
public function getProductKey(): string
{
return $this->productKey;
}
/**
* @param string $productKey
*/
public function setProductKey(string $productKey): void
{
$this->productKey = $productKey;
}
/**
* @return string
*/
public function getProductDesc(): string
{
return $this->productDesc;
}
/**
* @param string $productDesc
*/
public function setProductDesc(string $productDesc): void
{
$this->productDesc = $productDesc;
}
/**
* @return string
*/
public function getCreateUserId(): string
{
return $this->createUserId;
}
/**
* @param string $createUserId
*/
public function setCreateUserId(string $createUserId): void
{
$this->createUserId = $createUserId;
}
/**
* @return string
*/
public function getFromSource(): string
{
return $this->fromSource;
}
/**
* @param string $fromSource
*/
public function setFromSource(string $fromSource): void
{
$this->fromSource = $fromSource;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/8/7
* Time: 下午3:53
*/
namespace Lib\aliyun\api\product;
class QueryProduct
{
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/7/30
* Time: 上午9:56
*/
namespace Lib\aliyun\sign;
use Illuminate\Support\Facades\Log;
use Lib\aliyun\api\BaseApi;
use Lib\aliyun\Config;
class Sign
{
/**
* @var BaseApi
*/
private $api;
/**
* @var string
*/
private $sign;
public function __construct(BaseApi $api)
{
$this->api = $api;
}
/**
* @param string $method
* @return string
*/
public function sign(string $method = 'GET')
{
$request = $this->api->getRequest();
ksort($request);
$str = '';
foreach ($request as $key => $value) {
$str .= '&' . $this->charTransfer(urlencode($key)) . '=' . $this->charTransfer(urlencode($value));
}
$str = trim($str, '&');
$str = urlencode($str);
$str = $method . '&%2F&' . $str;
$hash = hash_hmac('sha1', $str, Config::ACCESS_SECRET . '&', true);
$this->sign = base64_encode($hash);
return $this->sign;
}
/**
* @param string $res
* @return string
*/
private function charTransfer(string $res): string
{
// $str = str_replace(['+', '*', '%7E'], ['%20', '%2A', '~'], $str);
$res = preg_replace('/\+/', '%20', $res);
$res = preg_replace('/\*/', '%2A', $res);
$res = preg_replace('/%7E/', '~', $res);
return $res;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/20
* Time: 下午3:02
*/
namespace Lib\custom;
use Dingo\Api\Contract\Transformer\Adapter;
use Dingo\Api\Http\Request;
use Dingo\Api\Transformer\Binding;
use Lib\wx\pay\Order;
class MyCustomTransformer implements Adapter
{
private $returnCode = 200;
private $resultCode = 'SUCCESS';
private $errorMessage = '';
private $data = [];
public function __construct(int $returnCode = 200, string $resultCode = 'SUCCESS')
{
$this->returnCode = $returnCode;
$this->resultCode = $resultCode;
}
public function transform($response, $transformer, Binding $binding, Request $request)
{
$data = [];
if ($response instanceof Order) {
$order = $response->getParameters();
$data = [
'appId' => $order['appid'],
'timeStamp' => strval(time()),
'nonceStr' => str_random(32),
'package' => 'prepay_id=' . $order['prepay_id'],
'signType' => config('wx.sign_type'),
];
}
return array_filter([
'return_code' => $this->returnCode,
'result_code' => $this->resultCode,
'data' => $data,
], function ($value) {
return filled($value);
});
}
}
\ No newline at end of file
......@@ -9,7 +9,8 @@
namespace Lib\wx;
use Lib\wx\pay\Order;
use Illuminate\Support\Facades\Log;
use Lib\wx\pay\UnifiedOrder;
use Spatie\ArrayToXml\ArrayToXml;
class Client
......@@ -24,18 +25,28 @@ class Client
* @throws \Exception
* @return bool
*/
public function request(Order $order, string $url): bool
public function request(UnifiedOrder $order, string $url): bool
{
$client = new \GuzzleHttp\Client();
$content = $order->getOrderRequest();
$sign = Tool::sign($content);
$content['sign'] = $sign;
$body = [];
foreach ($content as $key => $value) {
$body[$key] = ['_cdate' => $value];
if ($key != 'total_fee') {
$body[$key] = ['_cdata' => $value];
} else {
$body[$key] = $value;
}
}
$body = ArrayToXml::convert($body);
$response = $client->post($url, [
......@@ -46,6 +57,7 @@ class Client
if ($response->getStatusCode() === 200) {
$this->response = Tool::xmlToArray($response->getBody()->getContents());
if ($this->response['return_code'] === 'SUCCESS' && Tool::validateSign($this->response) && $this->response['result_code'] === 'SUCCESS') {
return true;
}
......
......@@ -9,10 +9,43 @@
namespace Lib\wx\pay;
interface Order
class Order
{
private $parameters = [
'appid' => '',
'mch_id' => '',
'device_info' => '',
'nonce_str' => '',
'sign' => '',
'result_code' => '',
'err_code' => '',
'err_code_des' => '',
'trade_type' => '',
'prepay_id' => '',
'code_url' => ''
];
public function parseResponse(array $response)
{
foreach ($response as $key => $value) {
if (isset($this->parameters[$key])) {
$this->parameters[$key] = $value;
}
}
}
/**
* @return array
*/
public function getParameters(): array
{
return $this->parameters;
}
public function getOrderRequest(): array;
}
\ No newline at end of file
......@@ -6,18 +6,15 @@
* Time: 下午1:07
*/
namespace Lib\wx;
namespace Lib\wx\pay;
use Lib\wx\pay\Order;
use Mockery\Exception;
/**
* 微信支付
* Class Pay
* @package Lib\wx
*/
class UnifiedOrder implements Order
class UnifiedOrder
{
/**
......@@ -66,9 +63,9 @@ class UnifiedOrder implements Order
*/
public function __construct()
{
$this->requestParameters['appid'] = env('WX_APP_ID');
$this->requestParameters['appid'] = config('wx.app_id');
$this->requestParameters['mch_id'] = env('WX_MCH_ID');
$this->requestParameters['mch_id'] = config('wx.mch_id');
// $urlGenerator = app('Dingo\Api\Routing\UrlGenerator');
//
......@@ -88,7 +85,7 @@ class UnifiedOrder implements Order
public function getOrderRequest(): array
{
$this->requestParameters = array_filter($this->requestParameters, function ($value, $key) {
return $this->requestParameters = array_filter($this->requestParameters, function ($value, $key) {
return filled($value);
......
......@@ -10,6 +10,9 @@ namespace App\Http\Controllers\v1;
use App\Http\Controllers\Controller;
use App\model\UserModel;
use Illuminate\Support\Facades\DB;
use Lib\custom\MyCustomTransformer;
class TestController extends Controller
{
......@@ -17,6 +20,8 @@ class TestController extends Controller
public function test()
{
return $this->response->collection(UserModel::all(), new MyCustomTransformer());
return '123';
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/17
* Time: 下午8:35
*/
namespace App\Http\Controllers\v1\replace;
use App\Http\Controllers\Controller;
use App\model\BoxModel;
use Illuminate\Http\Request;
class ReplaceController extends Controller
{
public function replace(Request $request)
{
// $validator = Validator::make();
//todo 验证舱门的有效性
//todo 舱门唯一id
$boxId = $request->get('box_id');
//todo 检测是否预约 ,舱门状态
BoxModel::where('id',$boxId)->first(['box_code','battery_code','reserve_status'])->toArray();
//todo 下发打开舱门的命令
}
}
\ No newline at end of file
......@@ -75,18 +75,23 @@ class CodeController extends Controller
abort(500, $request);
}
if ($userModel->where('openid', $openId)->update([
'session_key' => $sessionKey,
'telephone' => $phoneNumber,
'pure_phone' => $purePhoneNumber,
'country_code' => $countryCode,
]) === 0) {
$userModel = UserModel::where('telephone', $purePhoneNumber)->first();
if ($userModel !== null) {
$userModel->session_key = $sessionKey;
$userModel->openid = $openId;
$userModel->save();
} else {
$userModel = new UserModel();
if ($userModel->addWxUser($wxUser) === false) {
abort(500, 'add new user fail');
}
}
return $this->response->array([
'return_code' => 200,
......
......@@ -43,7 +43,7 @@ class RegisterController extends Controller
abort(400, $validator->errors()->first());
}
$telephone = $request->get('phone');
$telephone = $request->get('telephone');
$code = $request->get('code');
$sms->setPhone($telephone);
......@@ -84,7 +84,7 @@ class RegisterController extends Controller
$validator = Validator::make($request->all(), [
'phone' => 'required|digits:11|exists:guests,telephone',
'telephone' => 'required|digits:11|exists:guests,telephone',
'code' => 'required|digits:4',
]);
......@@ -92,7 +92,7 @@ class RegisterController extends Controller
if ($validator->fails() === true) {
abort(400, $validator->errors()->first());
}
$phone = $request->get('phone');
$phone = $request->get('telephone');
$code = $request->get('code');
$sms->setPhone($phone);
......@@ -105,6 +105,8 @@ class RegisterController extends Controller
$token = Auth::login($userModel);
Log::debug($token);
return $this->response->array($this->getResponseCode() + ['data' => ['token' => $token]]);
......
......@@ -11,6 +11,12 @@ namespace App\Http\Controllers\v1\wx\pay;
use App\Http\Controllers\Controller;
use App\model\PayGroupModel;
use Illuminate\Http\Request;
use Lib\custom\MyCustomTransformer;
use Lib\wx\Client;
use Lib\wx\pay\Order;
use Lib\wx\pay\UnifiedOrder;
use Validator;
class CardController extends Controller
{
......@@ -35,6 +41,60 @@ class CardController extends Controller
'items' => $payGroup
],
]);
}
//购买换电卡
/**
* @param Request $request
* @param UnifiedOrder $unifiedOrder
* @return \Dingo\Api\Http\Response
* @throws \Exception
*/
public function pay(Request $request, UnifiedOrder $unifiedOrder)
{
$validator = Validator::make($request->all(), [
'amount' => 'required',
'day' => 'required',
'card_id' => 'required',
]);
if ($validator->fails() === true) {
abort(400, $validator->errors()->first());
}
//todo 验证金额日期id是否一致
$day = $request->get('day');
$amount = $request->get('amount');
$notifyUrl = '';
$openid = 'own8r5AR8dvJqy8GQM_zNX9weR8Y';
$unifiedOrder->setParameters([
'body' => '购买充电卡',
'out_trade_no' => time() . rand(100000, 999999),
'total_fee' => $amount,
'spbill_create_ip' => $request->getClientIp(),
'notify_url' => $notifyUrl,
'trade_type' => 'JSAPI',
'openid' => $openid,
]);
$client = new Client();
if ($client->request($unifiedOrder, config('wx.unified_order_url') === true)) {
$response = $client->getResponse();
$order = new Order();
$order->parseResponse($response);
return $this->response->item($order, new MyCustomTransformer());
} else {
abort(500, 'request order fail');
}
}
......
......@@ -12,10 +12,13 @@ namespace App\Http\Controllers\v1\wx\pay;
use App\Http\Controllers\Controller;
use App\model\SystemModel;
use Illuminate\Http\Request;
use Lib\custom\MyCustomTransformer;
use Lib\wx\Client;
use Lib\wx\login\Login;
use Lib\wx\UnifiedOrder;
use Lib\wx\pay\Order;
use Lib\wx\pay\UnifiedOrder;
use Validator;
use Log;
class DepositController extends Controller
{
......@@ -55,22 +58,23 @@ class DepositController extends Controller
$validator = Validator::make($request->all(), [
'amount' => 'required|int',
'code' => 'required'
// 'code' => 'required'
]);
$code = $request->get('code');
$login = new Login($code);
if ($login->request() === true) {
$openid = $login->getOpenId();
//fixme 存储用户的sessionId
}
// $code = $request->get('code');
//
// $login = new Login($code);
// if ($login->request() === true) {
// $openid = $login->getOpenId();
// //fixme 存储用户的sessionId
// }
$openid = 'own8r5AR8dvJqy8GQM_zNX9weR8Y';
//todo 验证押金金额
$amount = $request->get('amount');
$amount *= 100;
$notifyUrl = '';
$notifyUrl = 'http://';
$unifiedOrder->setParameters([
'body' => '支付押金',
......@@ -86,6 +90,15 @@ class DepositController extends Controller
if ($client->request($unifiedOrder, config('wx.unified_order_url')) === true) {
$response = $client->getResponse();
$order = new Order();
$order->parseResponse($response);
return $this->response->item($order, new MyCustomTransformer());
} else {
dd('error');
}
......
......@@ -11,11 +11,23 @@ namespace App\Http\Controllers\v1\wx\pay;
use App\Http\Controllers\Controller;
use App\model\PayGroupModel;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Lib\custom\MyCustomTransformer;
use Lib\wx\Client;
use Lib\wx\pay\Order;
use Lib\wx\pay\UnifiedOrder;
use Validator;
/**
* 充值
* Class TopUpController
* @package App\Http\Controllers\v1\wx\pay
*/
class TopUpController extends Controller
{
public function get()
{
$date = date('Y-m-d H:i:s');
......@@ -39,4 +51,80 @@ class TopUpController extends Controller
]);
}
/**
* 余额充值
* @param Request $request
* @param UnifiedOrder $unifiedOrder
* @return \Dingo\Api\Http\Response
* @throws \Exception
*/
public function pay(Request $request, UnifiedOrder $unifiedOrder)
{
$validator = Validator::make($request->all(), [
'amount' => 'required',
'code' => 'required'
]);
if ($validator->fails() === true) {
abort(400, $validator->errors()->first());
}
//todo code 获取openid和更新session id 验证金额
$amount = $request->get('amount');
$openid = 'own8r5AR8dvJqy8GQM_zNX9weR8Y';
$notifyUrl = '';
$unifiedOrder->setParameters([
'body' => '余额充值',
'out_trade_no' => time() . rand(100000, 999999),
'total_fee' => $amount,
'spbill_create_ip' => $request->getClientIp(),
'notify_url' => $notifyUrl,
'trade_type' => 'JSAPI',
'openid' => $openid,
]);
$client = new Client();
if ($client->request($unifiedOrder, config('wx.unified_order_url') === true)) {
$response = $client->getResponse();
$order = new Order();
$order->parseResponse($response);
return $this->response->item($order, new MyCustomTransformer());
} else {
abort(500, 'request order fail');
}
}
}
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/17
* Time: 下午8:40
*/
namespace App\model;
use Illuminate\Database\Eloquent\Model;
class BoxModel extends Model
{
protected $table = 'box_rooms';
}
\ No newline at end of file
......@@ -24,11 +24,16 @@ $app = new Laravel\Lumen\Application(
);
$app->configure('logging');
$app->configure('auth');
$app->configure('logging');
$app->configure('dysms');
$app->configure('wx');
$app->configure('mqtt');
$app->configure('redis_key');
$app->withFacades();
......@@ -87,6 +92,7 @@ $app->routeMiddleware([
// 'auth' => App\Http\Middleware\Authenticate::class,
// ]);
/*
|--------------------------------------------------------------------------
| Register Service Providers
......@@ -123,11 +129,17 @@ $app['Dingo\Api\Auth\Auth']->extend('oauth', function ($app) {
return new Dingo\Api\Auth\Provider\JWT($app['Tymon\JWTAuth\JWTAuth']);
});
app('Dingo\Api\Transformer\Factory')->setAdapter(function ($app) {
//app('Dingo\Api\Transformer\Factory')->setAdapter(function ($app) {
//
//
// return new Dingo\Api\Transformer\Adapter\Fractal(new League\Fractal\Manager, 'include', ',');
//
//});
app('Dingo\Api\Transformer\Factory')->setAdapter(function ($app) {
return new \Lib\custom\MyCustomTransformer();
return new Dingo\Api\Transformer\Adapter\Fractal(new League\Fractal\Manager, 'include', ',');
});
$app['Dingo\Api\Exception\Handler']->setErrorFormat([
......
{
"name": "laravel/lumen",
"description": "The Laravel Lumen Framework.",
"keywords": ["framework", "laravel", "lumen"],
"keywords": [
"framework",
"laravel",
"lumen"
],
"license": "MIT",
"type": "project",
"require": {
......@@ -23,7 +27,8 @@
"autoload": {
"psr-4": {
"App\\": "app/",
"Lib\\": "Lib/"
"Lib\\": "Lib/",
"Src\\": "src/"
}
},
"autoload-dev": {
......
......@@ -70,22 +70,6 @@ return [
'timezone' => env('DB_TIMEZONE', '+08:00'),
'strict' => env('DB_STRICT_MODE', false),
],
'old_cdz' => [
'driver' => 'mysql',
'host' => env('PRE_DB_HOST'),
'port' => env('PRE_DB_PORT', 3306),
'database' => env('PRE_DB_DATABASE'),
'username' => env('PRE_DB_USERNAME'),
'password' => env('PRE_DB_PASSWORD'),
'charset' => env('DB_CHARSET', 'utf8mb4'),
'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'),
'prefix' => env('DB_PREFIX', ''),
'timezone' => env('DB_TIMEZONE', '+08:00'),
'strict' => env('DB_STRICT_MODE', false),
],
'pgsql' => [
'driver' => 'pgsql',
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/27
* Time: 下午5:19
*/
return [
'hdg' => [
'product_key' => env('MQTT_PRODUCT_KEY'),
'format' => 'json',
'version' => '2018-01-20',
'access_key_id' => env('MQTT_ACCESS_KEY_ID', 'LTAItZVbkVaVXifu'),
'sign_method' => 'HMAC-SHA1',
'sign_version' => '1.0',
'region_id' => 'cn-shanghai',
'access_secret' => env('MQTT_ACCESS_SECRET', 'Irko4313cUEbb1RYXGWEz6s6VgNETG'),
'access_key' => env('MQTT_ACCESS_KEY', 'LTAItZVbkVaVXifu'),
]
];
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/27
* Time: 上午11:23
*/
return [
'AC_ALARM' => 'hdg:ac:alarm:hash'
];
......@@ -26,5 +26,6 @@ return [
'lang' => env('WX_LANG'),
'app_key' => env('WX_APP_KEY'),
'unified_order_url' => 'https://api.mch.weixin.qq.com/pay/unifiedorder',
'sign_type' => 'MD5',
];
\ No newline at end of file
<?php
$data = [
'err_alarm_type' => [0, 0, 0, 0, 0, 0],
'sensor_bean_count' => 3,
'sensor_bean' => [
[
'humidity' => 1,
'temperature' => 1,
'sensor_state' => 1,
'retain' => 0,
],
[
'humidity' => 1,
'temperature' => 1,
'sensor_state' => 1,
'retain' => 0,
],
[
'humidity' => 1,
'temperature' => 1,
'sensor_state' => 1,
'retain' => 0,
]
],
'sensor_smoke_count' => 1,
'sensor_smoke' => [
[
'white_smoke' => 1,
'black_smoke' => 1,
'device_state' => 1,
'retain' => 0,
]
]
];
$data = [
'ntc_count' => 3,
'ntc' => [
40, 50, 60
],
'humidity_count' => 3,
'humidity' => [2, 3, 4],
'while_smoke' => 0,
'black_smoke' => 0,
];
$data = [
'box_id' => 1,
'site' => '利尔达',
'user' => 'admin',
'password' => 'admin',
];
$data = [
'hardware_version' => '1.0',
'software_version' => '1.0',
'reserve' => ''
];
$data = [
'door_count' => 10,
'door' => [
'box_num' => 1,
'battery_num' => '2312313',
'err_lock' => 0,
'err_lock_bat' => 0,
'lock_state' => 1,
'battery_state' => 1,
'door_num' => 1
]
];
echo json_encode($data);
?>
\ No newline at end of file
......@@ -12,6 +12,7 @@
*/
$router->get('/', function () use ($router) {
return $router->app->version();
});
......@@ -22,11 +23,7 @@ $api = app('Dingo\Api\Routing\Router');
$api->version('v1', ['namespace' => 'App\Http\Controllers\v1', 'prefix' => 'api/wx', 'middleware' => ['addResultCode']], function ($api) {
$api->post('test', function () {
dd(1);
});
$api->get('test', 'TestController@test');
$api->get('code', 'wx\auth\CodeController@login');
......@@ -42,10 +39,16 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\v1', 'prefix' => 'api/
$api->get('card/items', 'wx\pay\CardController@get');
//
$api->get('deposit/order', 'wx\pay\DepositController@pay');
$api->group(['prefix' => '', 'middleware' => 'auth:api'], function ($api) {
$api->get('tickets', 'wx\pay\ActivityController@getTicket');
//
});
});
......
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/26
* Time: 下午2:03
*/
namespace src\box;
use App\Http\model\DoorModel;
use src\box\door\Door;
class Box
{
use SearchModel;
private $id = 0;
private $code = '';
private $type = 1;
public function __construct(int $id = 0)
{
$this->id = $id;
}
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* @return string
*/
public function getCode(): string
{
return $this->code;
}
/**
* @param string $code
*/
public function setCode(string $code): void
{
$this->code = $code;
}
/**
* @param int $doorCount
*/
public function initDoor(int $doorCount = 16): void
{
$i = 1;
while ($doorCount--) {
$door = new Door();
$door->setName($i++);
$door->setBoxId($this->id);
$door->setBoxType($this->type);
$doorModel = new DoorModel();
$doorModel->add($door);
unset($door);
unset($doorModel);
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/26
* Time: 下午2:06
*/
namespace src\box;
use App\Http\model\BoxModel;
trait SearchModel
{
/**
* @var BoxModel
*/
protected $boxModel;
/**
* @param string $code
* @param array $parameters
* @return bool
*/
public function searchByCode(string $code, array $parameters = []): bool
{
$this->boxModel = BoxModel::where([['code', $code], ['box_type', 0]])->first($parameters);
return $this->boxModel === null ? false : true;
}
/**
* @param string $code
* @param int $num
* @param array $parameters
* @return bool
*/
public function searchByCodeNum(string $code, int $num, array $parameters = []): bool
{
$this->boxModel = BoxModel::where([['code', $code], ['box_type', $num]])->first($parameters);
return $this->boxModel === null ? false : true;
}
/**
* @return BoxModel
*/
public function getBoxModel(): BoxModel
{
return $this->boxModel;
}
/**
* @param BoxModel $boxModel
*/
public function setBoxModel(BoxModel $boxModel): void
{
$this->boxModel = $boxModel;
}
/**
* @param string $parameter
* @return mixed
*/
public function getValue(string $parameter)
{
return $this->boxModel->$parameter;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/27
* Time: 上午10:52
*/
namespace src\box\ac;
use Illuminate\Support\Facades\Redis;
class Alarm
{
private $boxId;
/**
* @var array 湿度
*/
private $humidity = [];
/**
* @var array 温度
*/
private $temperature = [];
private $sensorState = [];
/**
* @var array 保留
*/
private $retain = [];
/**
* @var int 白烟
*/
private $whiteSmoke = 0;
/**
* @var int 黑烟
*/
private $blackSmoke = 0;
/**
* @var int 设备状态
*/
private $deviceState = 0;
/**
* @var int 预留
*/
private $acRetain = 0;
/**
* 更新所有报警数据至redis
*/
public function updateAllToRedis()
{
$redis = app('Redis');
$update = [];
$i = 1;
foreach ([
'humidity' => $this->humidity,
'temperature' => $this->temperature,
'sensor_state' => $this->sensorState,
'retain' => $this->retain,
] as $key => $items) {
$i = 1;
foreach ($items as $item) {
$update[$key . '_' . $i++] = $item;
}
}
$update['white_smoke'] = $this->whiteSmoke;
$update['black_smoke'] = $this->blackSmoke;
$update['device_state'] = $this->deviceState;
$update['retain'] = $this->acRetain;
if ($redis instanceof \Redis) {
$redis->hMset(config('redis_key.AC_ALARM') . ':' . $this->boxId, $update);
}
}
/**
* @return array
*/
public function getHumidity(): array
{
return $this->humidity;
}
/**
* @param array $humidity
*/
public function setHumidity(array $humidity): void
{
$this->humidity = $humidity;
}
/**
* @return array
*/
public function getTemperature(): array
{
return $this->temperature;
}
/**
* @param array $temperature
*/
public function setTemperature(array $temperature): void
{
$this->temperature = $temperature;
}
/**
* @return array
*/
public function getSensorState(): array
{
return $this->sensorState;
}
/**
* @param array $sensorState
*/
public function setSensorState(array $sensorState): void
{
$this->sensorState = $sensorState;
}
/**
* @return array
*/
public function getRetain(): array
{
return $this->retain;
}
/**
* @param array $retain
*/
public function setRetain(array $retain): void
{
$this->retain = $retain;
}
/**
* @return int
*/
public function getWhiteSmoke(): int
{
return $this->whiteSmoke;
}
/**
* @param int $whiteSmoke
*/
public function setWhiteSmoke(int $whiteSmoke): void
{
$this->whiteSmoke = $whiteSmoke;
}
/**
* @return int
*/
public function getBlackSmoke(): int
{
return $this->blackSmoke;
}
/**
* @param int $blackSmoke
*/
public function setBlackSmoke(int $blackSmoke): void
{
$this->blackSmoke = $blackSmoke;
}
/**
* @return int
*/
public function getDeviceState(): int
{
return $this->deviceState;
}
/**
* @param int $deviceState
*/
public function setDeviceState(int $deviceState): void
{
$this->deviceState = $deviceState;
}
/**
* @return mixed
*/
public function getBoxId()
{
return $this->boxId;
}
/**
* @param mixed $boxId
*/
public function setBoxId($boxId): void
{
$this->boxId = $boxId;
}
/**
* @return int
*/
public function getAcRetain(): int
{
return $this->acRetain;
}
/**
* @param int $acRetain
*/
public function setAcRetain(int $acRetain): void
{
$this->acRetain = $acRetain;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/26
* Time: 下午3:20
*/
namespace src\box\door;
class Door
{
/**
* @var int
*/
private $id = 0;
/**
* @var int
*/
private $name = 1;
/**
* @var int
*/
private $boxId = 0;
/**
* @var int
*/
private $boxType = 1;
/**
* @return int
*/
public function getId(): int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* @return int
*/
public function getName(): int
{
return $this->name;
}
/**
* @param int $name
*/
public function setName(int $name): void
{
$this->name = $name;
}
/**
* @return int
*/
public function getBoxId(): int
{
return $this->boxId;
}
/**
* @param int $boxId
*/
public function setBoxId(int $boxId): void
{
$this->boxId = $boxId;
}
/**
* @return int
*/
public function getBoxType(): int
{
return $this->boxType;
}
/**
* @param int $boxType
*/
public function setBoxType(int $boxType): void
{
$this->boxType = $boxType;
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: banice
* Date: 2018/9/27
* Time: 下午5:13
*/
namespace src\box\door;
use Illuminate\Support\Facades\Log;
use Lib\aliyun\api\PublishToTopic;
use Lib\aliyun\Client;
trait Open
{
public function controlDoor(int $boxNum, int $doorNum, string $orderId, string $deviceName, string $topic = 'get', int $operation = 1)
{
$publishToTopic = new PublishToTopic();
$publishToTopic->setMessageContent(json_encode([
'command' => config('command.open_door'),
'doors' => [
'box_num' => $boxNum,
'door_num' => $doorNum,
'order_id' => $orderId,
'operation' => $operation
]
]));
$publishToTopic->setProductKey('a1pZNA3yDdm');
$topicFullName = '/' . env('MQTT_PRODUCT_KEY') . '/' . $deviceName . '/' . $topic;
$publishToTopic->setTopicFullName($topicFullName);
$client = new Client();
$client->setApi($publishToTopic);
if ($client->request() === true) {
Log::debug('success');
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment