13 changed files with 323 additions and 84 deletions
-
37app/Amqp/Consumer/DevicOrderConsumer.php
-
24app/Constants/LogLabel.php
-
73app/Controller/DeviceController.php
-
10app/Model/Order.php
-
10app/Model/OrderMain.php
-
9app/Model/Store.php
-
88app/Service/DeviceServiceImp.php
-
3app/Service/DeviceServiceInterFace.php
-
54app/Service/IOTAliService.php
-
8app/Service/IOTServiceInterface.php
-
89app/TaskWorker/AliIotTask.php
-
1config/autoload/dependencies.php
-
1config/routes.php
@ -0,0 +1,24 @@ |
|||
<?php |
|||
|
|||
declare(strict_types=1); |
|||
|
|||
namespace App\Constants; |
|||
|
|||
use Hyperf\Constants\AbstractConstants; |
|||
use Hyperf\Constants\Annotation\Constants; |
|||
|
|||
/** |
|||
* @Constants |
|||
*/ |
|||
class LogLabel extends AbstractConstants |
|||
{ |
|||
/** |
|||
* @Message("Ssdb Log Label") |
|||
*/ |
|||
const SSDB_LOG = 'ssdb_log'; |
|||
|
|||
/** |
|||
* @Message("Device Speaker Log Label") |
|||
*/ |
|||
const DEVICE_LOG = 'device_log'; |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace App\Model; |
|||
|
|||
|
|||
class Order extends Model |
|||
{ |
|||
protected $table = 'ims_cjdc_order'; |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
<?php |
|||
|
|||
|
|||
namespace App\Model; |
|||
|
|||
|
|||
class OrderMain extends Model |
|||
{ |
|||
protected $table = 'ims_cjdc_order_main'; |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
<?php |
|||
|
|||
namespace App\Model; |
|||
|
|||
class Store extends Model |
|||
{ |
|||
protected $table = 'ims_cjdc_store'; |
|||
|
|||
} |
|||
@ -1,29 +1,99 @@ |
|||
<?php |
|||
|
|||
namespace App\Service; |
|||
|
|||
use App\Commons\Log; |
|||
use App\Constants\LogLabel; |
|||
use App\Model\SpeakerDevic; |
|||
use App\Model\Store; |
|||
use Hyperf\Di\Annotation\Inject; |
|||
use Hyperf\Utils\ApplicationContext; |
|||
use App\TaskWorker\AliIotTask; |
|||
|
|||
class DeviceServiceImp implements DeviceServiceInterFace |
|||
{ |
|||
|
|||
/** |
|||
* @Inject |
|||
* @var Log |
|||
*/ |
|||
protected $log; |
|||
|
|||
public function getListByStoreId($store_id){ |
|||
/** |
|||
* @Inject |
|||
* @var IOTServiceInterface |
|||
*/ |
|||
protected $IOTService; |
|||
|
|||
/** |
|||
* 获取绑定列表 |
|||
* @param $store_id |
|||
* @return \Hyperf\Database\Model\Builder[]|\Hyperf\Database\Model\Collection |
|||
*/ |
|||
public function getListByStoreId($store_id) |
|||
{ |
|||
return SpeakerDevic::query()->where(['store_id' => $store_id])->get(); |
|||
} |
|||
public function bindByStoreId($dev_name,$store_id){ |
|||
|
|||
/** |
|||
* 绑定 |
|||
* @param $dev_name |
|||
* @param $store_id |
|||
* @return SpeakerDevic|null |
|||
* @throws \Throwable |
|||
*/ |
|||
public function bindByStoreId($dev_name, $store_id) |
|||
{ |
|||
$sd = null; |
|||
try{ |
|||
$sd = new SpeakerDevic; |
|||
|
|||
if ($this->checkDeviceEnable($dev_name)) { |
|||
return $sd; |
|||
} |
|||
|
|||
try { |
|||
|
|||
// 获取市场ID
|
|||
$market_id = Store::query()->where(['id' => $store_id])->value('market_id'); |
|||
|
|||
$sd = new SpeakerDevic; |
|||
$sd->store_id = $store_id; |
|||
$sd->device_name = $dev_name; |
|||
$sd->market_id = 1; |
|||
$sd->market_id = $market_id; |
|||
$sd->bind_time = time(); |
|||
$sd->saveOrFail(); |
|||
}catch(Exception $e){ |
|||
//var_dump($e);
|
|||
} catch (Exception $e) { |
|||
$this->log->event(LogLabel::DEVICE_LOG, ['msg' => '绑定设备异常:'.$e->getMessage()]); |
|||
} |
|||
return $sd; |
|||
} |
|||
public function pubMsgByStoreIdAndDevName($dev_name,$store_id,$msg){ |
|||
|
|||
/** |
|||
* 解绑 |
|||
* @param $bind_id |
|||
* @return int |
|||
*/ |
|||
public function unbindById($bind_id) |
|||
{ |
|||
return SpeakerDevic::destroy($bind_id); |
|||
} |
|||
|
|||
/** |
|||
* 发布语音消息 |
|||
* @param $store_id |
|||
* @param $msg |
|||
*/ |
|||
public function pubMsgToStoreByDevName($dev_name, $msg) |
|||
{ |
|||
return $this->IOTService->pub($dev_name, $msg); |
|||
} |
|||
|
|||
/** |
|||
* 当前设备是否已经被绑定 |
|||
* @param $dev_name |
|||
* @return bool |
|||
*/ |
|||
protected function checkDeviceEnable($dev_name) |
|||
{ |
|||
return SpeakerDevic::query()->where(['device_name' => $dev_name])->exists(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,54 @@ |
|||
<?php |
|||
|
|||
namespace App\Service; |
|||
|
|||
use AlibabaCloud\Client\AlibabaCloud; |
|||
use AlibabaCloud\Client\Exception\ClientException; |
|||
use AlibabaCloud\Client\Exception\ServerException; |
|||
use App\Commons\Log; |
|||
use App\Constants\LogLabel; |
|||
use Hyperf\Di\Annotation\Inject; |
|||
|
|||
class IOTAliService implements IOTServiceInterface |
|||
{ |
|||
|
|||
/** |
|||
* @Inject |
|||
* @var Log |
|||
*/ |
|||
protected $log; |
|||
|
|||
public function pub($device_name, $msg) |
|||
{ |
|||
AlibabaCloud::accessKeyClient(env('ALI_IOT_KEY'), env('ALI_IOT_SECRET')) |
|||
->regionId(env('ALI_IOT_REGION')) |
|||
->asDefaultClient(); |
|||
|
|||
try { |
|||
AlibabaCloud::rpc() |
|||
->product('Iot') |
|||
->version('2018-01-20') |
|||
->action('Pub') |
|||
->method('POST') |
|||
->host(env('ALI_IOT_HOST')) |
|||
->options([ |
|||
'query' => [ |
|||
'RegionId' => "cn-shanghai", |
|||
'TopicFullName' => "/a1ZSurIJmO0/".$device_name."/user/get", |
|||
'MessageContent' => base64_encode($msg), |
|||
'ProductKey' => env('ALI_IOT_PRODUCT_KEY'), |
|||
], |
|||
]) |
|||
->request(); |
|||
} catch (ClientException $e) { |
|||
$this->log->event(LogLabel::DEVICE_LOG, ['msg' => 'ClientException发布失败:'.$e->getErrorMessage()]); |
|||
return false; |
|||
} catch (ServerException $e) { |
|||
$this->log->event(LogLabel::DEVICE_LOG, ['msg' => 'ServerException发布失败:'.$e->getErrorMessage()]); |
|||
return false; |
|||
} |
|||
|
|||
return true; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
<?php |
|||
|
|||
namespace App\Service; |
|||
|
|||
interface IOTServiceInterface |
|||
{ |
|||
public function pub($device_name, $msg); |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue