Commit 9986903a by banice

command创建成

parent 9289eba3
...@@ -20,6 +20,7 @@ class AccessController extends Controller ...@@ -20,6 +20,7 @@ class AccessController extends Controller
public function accessToken(AccessToken $accessToken) public function accessToken(AccessToken $accessToken)
{ {
$iotClient = new IotClient($accessToken); $iotClient = new IotClient($accessToken);
if ($iotClient->request() === true) { if ($iotClient->request() === true) {
......
...@@ -11,14 +11,42 @@ namespace App\Http\Controllers\device; ...@@ -11,14 +11,42 @@ namespace App\Http\Controllers\device;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Lib\iot\command\ChargerCommand;
use Lib\iot\command\Send;
class ChargerController extends Controller 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 ...@@ -35,16 +35,23 @@ class AccessToken implements Request
public function __construct() public function __construct()
{ {
$this->appId = env('APP_ID'); $this->appId = env('IOT_APP_ID');
$this->appSecret = env('APP_SECRET'); $this->appSecret = env('IOT_APP_SECRET');
} }
public function isJson(): bool
{
return false;
}
public function parseResponse(ResponseInterface $response) public function parseResponse(ResponseInterface $response)
{ {
$body = json_decode($response->getBody(), true); $body = json_decode($response->getBody(), true);
$this->accessToken = $body['accessToken']; $this->accessToken = $body['accessToken'];
$expiresLn = $body['expiresLn']; $expiresLn = $body['expiresIn'];
$this->refreshToken = $body['refreshToken']; $this->refreshToken = $body['refreshToken'];
Cache::put(self::ACCESS_CACHE_KEY, $this->accessToken, $expiresLn / 60); Cache::put(self::ACCESS_CACHE_KEY, $this->accessToken, $expiresLn / 60);
Cache::put(self::REFRESH_ACCESS_CACHE_KEY, $this->refreshToken, 28 * 24 * 60); Cache::put(self::REFRESH_ACCESS_CACHE_KEY, $this->refreshToken, 28 * 24 * 60);
...@@ -54,6 +61,9 @@ class AccessToken implements Request ...@@ -54,6 +61,9 @@ class AccessToken implements Request
public static function getAccessTokenCache() 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) { if (Cache::has(self::ACCESS_CACHE_KEY) === true) {
return Cache::get(self::ACCESS_CACHE_KEY); return Cache::get(self::ACCESS_CACHE_KEY);
...@@ -184,7 +194,7 @@ class AccessToken implements Request ...@@ -184,7 +194,7 @@ class AccessToken implements Request
public function getFormData(): array public function getFormData(): array
{ {
$this->formData = [ $this->formData = [
'appId' => $this->appId, 'appId' => $this->appId,
'secret' => $this->appSecret, 'secret' => $this->appSecret,
]; ];
......
...@@ -43,14 +43,11 @@ class IotClient ...@@ -43,14 +43,11 @@ class IotClient
$client = new Client(); $client = new Client();
Log::debug(storage_path('cert/key.pem'));
$request = [ $request = [
'form_params' => $this->request->getFormData(),
'verify' => false, 'verify' => false,
'cert' => [ 'cert' => [
config('cert.cert_path'), config('iot.cert_path'),
'IoM@1234', 'IoM@1234',
] ]
]; ];
...@@ -58,7 +55,20 @@ class IotClient ...@@ -58,7 +55,20 @@ class IotClient
$request['headers'] = $this->headers; $request['headers'] = $this->headers;
} }
$this->response = $client->request($this->request->getMethod(), config('iot.host') . $this->request->getUrl(), $request);
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; return $this->response->getStatusCode() === 200;
......
...@@ -47,8 +47,8 @@ class RefreshToken implements Request ...@@ -47,8 +47,8 @@ class RefreshToken implements Request
return [ return [
'appId' => env('IOT_APP_ID'), 'appId' => env('IOT_APP_ID'),
'secret' => env('IOT_APP_SECRET'), 'secret' => env('IOT_APP_SECRET'),
'refreshToken' => $this->refreshToken 'refreshToken' => $this->refreshToken
]; ];
......
...@@ -22,5 +22,6 @@ interface Request ...@@ -22,5 +22,6 @@ interface Request
public function parseResponse(ResponseInterface $response); public function parseResponse(ResponseInterface $response);
public function isJson(): bool;
} }
\ No newline at end of file
...@@ -31,5 +31,69 @@ class ChargerCommand extends Command ...@@ -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 ...@@ -51,6 +51,9 @@ class Send implements Request
*/ */
public function __construct(Command $command) public function __construct(Command $command)
{ {
$this->appId = env('IOT_APP_ID');
$this->appKey = env('IOT_APP_ID');
$this->commandInstance = $command; $this->commandInstance = $command;
$body = $this->commandInstance->getMessage(); $body = $this->commandInstance->getMessage();
...@@ -61,8 +64,8 @@ class Send implements Request ...@@ -61,8 +64,8 @@ class Send implements Request
if ($this->commandInstance->getEncrypt() === '04') { if ($this->commandInstance->getEncrypt() === '04') {
$this->command = [ $this->command = [
'serviceId' => $this->commandInstance->getServiceId(), 'serviceId' => $this->commandInstance->getServiceId(),
'method' => $this->commandInstance->getMethod(), 'method' => $this->commandInstance->getMethod(),
'paras' => json_encode($message), // 'paras' => json_encode(['downData' => $message]),
]; ];
} else { } else {
...@@ -71,15 +74,16 @@ class Send implements Request ...@@ -71,15 +74,16 @@ class Send implements Request
} }
} }
public function send() public function send(): bool
{ {
$iotClient = new IotClient($this); $iotClient = new IotClient($this);
$iotClient->setHeaders([ $iotClient->setHeaders([
'app_key' => env('IOT_APP_KEY'), 'app_key' => $this->appKey,
'Authorization' => 'Bearer' . AccessToken::getAccessTokenCache(), 'Authorization' => 'Bearer ' . AccessToken::getAccessTokenCache(),
'Content-Type' => 'application/json' // 'Authorization' => 'Bearer ' . '60fe212de43e9615cf342d0e354441c',
'Content-Type' => 'application/json'
]); ]);
if ($iotClient->request() === true) { if ($iotClient->request() === true) {
...@@ -88,8 +92,13 @@ class Send implements Request ...@@ -88,8 +92,13 @@ class Send implements Request
Log::debug($response->getBody()); Log::debug($response->getBody());
return true;
} }
return false;
} }
public function getUrl(): string public function getUrl(): string
...@@ -98,6 +107,12 @@ class Send implements Request ...@@ -98,6 +107,12 @@ class Send implements Request
} }
public function isJson(): bool
{
return true;
}
public function getMethod(): string public function getMethod(): string
{ {
return 'POST'; return 'POST';
...@@ -108,12 +123,12 @@ class Send implements Request ...@@ -108,12 +123,12 @@ class Send implements Request
public function getFormData(): array public function getFormData(): array
{ {
return [ return [
'appId' => env('IOT_APP_ID'), // 'appId' => $this->appId,
'deviceId' => $this->deviceId, 'deviceId' => $this->deviceId,
'command' => json_encode($this->command), 'command' => $this->command,
'callbackUrl' => $this->callBackUrl, // 'callbackUrl' => $this->callBackUrl,
'expireTime' => $this->expireTime, // 'expireTime' => $this->expireTime,
'maxRetransmit' => $this->maxRetransmit, // 'maxRetransmit' => $this->maxRetransmit,
]; ];
} }
...@@ -123,5 +138,165 @@ class Send implements Request ...@@ -123,5 +138,165 @@ class Send implements Request
// TODO: Implement parseResponse() method. // 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'); ...@@ -22,3 +22,4 @@ $router->get('access', 'access\AccessController@accessToken');
$router->post('device/power/on', 'device\ChargerController@charger'); $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