Commit 9986903a by banice

command创建成

parent 9289eba3
......@@ -20,6 +20,7 @@ class AccessController extends Controller
public function accessToken(AccessToken $accessToken)
{
$iotClient = new IotClient($accessToken);
if ($iotClient->request() === true) {
......
......@@ -11,14 +11,42 @@ namespace App\Http\Controllers\device;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Lib\iot\command\ChargerCommand;
use Lib\iot\command\Send;
class ChargerController extends Controller
{
public function charger(Request $request)
/**
* @param Request $request
* @param ChargerCommand $chargerCommand
* @throws \Exception
*/
public function charger(Request $request, ChargerCommand $chargerCommand)
{
$chargerCommand->setOrderId(123);
$chargerCommand->setTime(12);
$chargerCommand->setPortLogic(1);
$chargerCommand->setServiceId('Transmission');
$chargerCommand->setMethod('Cmd_Res');
$send = new Send($chargerCommand);
$send->setCallBackUrl(route('deviceChargerReceiver'));
$send->setDeviceId('d245d3fd-26e1-4818-95b9-37e6af3cfcf7');
if ($send->send() === true) {
dd('success');
dd(1);
}
}
public function receiver(Request $request)
{
Log::debug($request->all());
}
}
\ No newline at end of file
......@@ -35,16 +35,23 @@ class AccessToken implements Request
public function __construct()
{
$this->appId = env('APP_ID');
$this->appSecret = env('APP_SECRET');
$this->appId = env('IOT_APP_ID');
$this->appSecret = env('IOT_APP_SECRET');
}
public function isJson(): bool
{
return false;
}
public function parseResponse(ResponseInterface $response)
{
$body = json_decode($response->getBody(), true);
$this->accessToken = $body['accessToken'];
$expiresLn = $body['expiresLn'];
$expiresLn = $body['expiresIn'];
$this->refreshToken = $body['refreshToken'];
Cache::put(self::ACCESS_CACHE_KEY, $this->accessToken, $expiresLn / 60);
Cache::put(self::REFRESH_ACCESS_CACHE_KEY, $this->refreshToken, 28 * 24 * 60);
......@@ -54,6 +61,9 @@ class AccessToken implements Request
public static function getAccessTokenCache()
{
// Cache::forget(self::ACCESS_CACHE_KEY);
// Cache::forget(self::REFRESH_ACCESS_CACHE_KEY);
if (Cache::has(self::ACCESS_CACHE_KEY) === true) {
return Cache::get(self::ACCESS_CACHE_KEY);
......
......@@ -43,14 +43,11 @@ class IotClient
$client = new Client();
Log::debug(storage_path('cert/key.pem'));
$request = [
'form_params' => $this->request->getFormData(),
'verify' => false,
'cert' => [
config('cert.cert_path'),
config('iot.cert_path'),
'IoM@1234',
]
];
......@@ -58,7 +55,20 @@ class IotClient
$request['headers'] = $this->headers;
}
if ($this->request->isJson() === false) {
$request['form_params'] = $this->request->getFormData();
$this->response = $client->request($this->request->getMethod(), config('iot.host') . $this->request->getUrl(), $request);
} else {
$request['json'] = $this->request->getFormData();
Log::debug($request);
$this->response = $client->request($this->request->getMethod(), config('iot.host') . $this->request->getUrl(), $request);
}
Log::debug($request);
Log::debug($this->response->getBody());
return $this->response->getStatusCode() === 200;
......
......@@ -22,5 +22,6 @@ interface Request
public function parseResponse(ResponseInterface $response);
public function isJson(): bool;
}
\ No newline at end of file
......@@ -31,5 +31,69 @@ class ChargerCommand extends Command
}
/**
* @return int
*/
public function getType(): int
{
return $this->type;
}
/**
* @param int $type
*/
public function setType(int $type): void
{
$this->type = $type;
}
/**
* @return int
*/
public function getPortLogic(): int
{
return $this->portLogic;
}
/**
* @param int $portLogic
*/
public function setPortLogic(int $portLogic): void
{
$this->portLogic = $portLogic;
}
/**
* @return int
*/
public function getOrderId(): int
{
return $this->orderId;
}
/**
* @param int $orderId
*/
public function setOrderId(int $orderId): void
{
$this->orderId = $orderId;
}
/**
* @return int
*/
public function getTime(): int
{
return $this->time;
}
/**
* @param int $time
*/
public function setTime(int $time): void
{
$this->time = $time;
}
}
\ No newline at end of file
......@@ -51,6 +51,9 @@ class Send implements Request
*/
public function __construct(Command $command)
{
$this->appId = env('IOT_APP_ID');
$this->appKey = env('IOT_APP_ID');
$this->commandInstance = $command;
$body = $this->commandInstance->getMessage();
......@@ -62,7 +65,7 @@ class Send implements Request
$this->command = [
'serviceId' => $this->commandInstance->getServiceId(),
'method' => $this->commandInstance->getMethod(),
'paras' => json_encode($message),
// 'paras' => json_encode(['downData' => $message]),
];
} else {
......@@ -71,14 +74,15 @@ class Send implements Request
}
}
public function send()
public function send(): bool
{
$iotClient = new IotClient($this);
$iotClient->setHeaders([
'app_key' => env('IOT_APP_KEY'),
'Authorization' => 'Bearer' . AccessToken::getAccessTokenCache(),
'app_key' => $this->appKey,
'Authorization' => 'Bearer ' . AccessToken::getAccessTokenCache(),
// 'Authorization' => 'Bearer ' . '60fe212de43e9615cf342d0e354441c',
'Content-Type' => 'application/json'
]);
......@@ -88,8 +92,13 @@ class Send implements Request
Log::debug($response->getBody());
return true;
}
return false;
}
public function getUrl(): string
......@@ -98,6 +107,12 @@ class Send implements Request
}
public function isJson(): bool
{
return true;
}
public function getMethod(): string
{
return 'POST';
......@@ -108,12 +123,12 @@ class Send implements Request
public function getFormData(): array
{
return [
'appId' => env('IOT_APP_ID'),
'deviceId' => $this->deviceId,
'command' => json_encode($this->command),
'callbackUrl' => $this->callBackUrl,
'expireTime' => $this->expireTime,
'maxRetransmit' => $this->maxRetransmit,
// 'appId' => $this->appId,
'deviceId' => $this->deviceId,
'command' => $this->command,
// 'callbackUrl' => $this->callBackUrl,
// 'expireTime' => $this->expireTime,
// 'maxRetransmit' => $this->maxRetransmit,
];
}
......@@ -123,5 +138,165 @@ class Send implements Request
// TODO: Implement parseResponse() method.
}
/**
* @return string
*/
public function getAccessToken(): string
{
return $this->accessToken;
}
/**
* @param string $accessToken
*/
public function setAccessToken(string $accessToken): void
{
$this->accessToken = $accessToken;
}
/**
* @return string
*/
public function getAppKey(): string
{
return $this->appKey;
}
/**
* @param string $appKey
*/
public function setAppKey(string $appKey): void
{
$this->appKey = $appKey;
}
/**
* @return string
*/
public function getContentType(): string
{
return $this->contentType;
}
/**
* @param string $contentType
*/
public function setContentType(string $contentType): void
{
$this->contentType = $contentType;
}
/**
* @return string
*/
public function getAppId(): string
{
return $this->appId;
}
/**
* @param string $appId
*/
public function setAppId(string $appId): void
{
$this->appId = $appId;
}
/**
* @return string
*/
public function getDeviceId(): string
{
return $this->deviceId;
}
/**
* @param string $deviceId
*/
public function setDeviceId(string $deviceId): void
{
$this->deviceId = $deviceId;
}
/**
* @return array|string
*/
public function getCommand()
{
return $this->command;
}
/**
* @param array|string $command
*/
public function setCommand($command): void
{
$this->command = $command;
}
/**
* @return Command
*/
public function getCommandInstance(): Command
{
return $this->commandInstance;
}
/**
* @param Command $commandInstance
*/
public function setCommandInstance(Command $commandInstance): void
{
$this->commandInstance = $commandInstance;
}
/**
* @return string
*/
public function getCallBackUrl(): string
{
return $this->callBackUrl;
}
/**
* @param string $callBackUrl
*/
public function setCallBackUrl(string $callBackUrl): void
{
$this->callBackUrl = $callBackUrl;
}
/**
* @return int
*/
public function getExpireTime(): int
{
return $this->expireTime;
}
/**
* @param int $expireTime
*/
public function setExpireTime(int $expireTime): void
{
$this->expireTime = $expireTime;
}
/**
* @return int
*/
public function getMaxRetransmit(): int
{
return $this->maxRetransmit;
}
/**
* @param int $maxRetransmit
*/
public function setMaxRetransmit(int $maxRetransmit): void
{
$this->maxRetransmit = $maxRetransmit;
}
}
\ No newline at end of file
......@@ -22,3 +22,4 @@ $router->get('access', 'access\AccessController@accessToken');
$router->post('device/power/on', 'device\ChargerController@charger');
$router->post('device/power/on/receiver', ['uses' => 'device\ChargerController@receiver', 'as' => 'deviceChargerReceiver']);
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