You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

109 lines
2.7 KiB

<?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;
/**
* @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, 'is_bind' => SpeakerDevic::IS_BIND_YES])->get()->toArray();
}
/**
* 绑定
* @param $dev_name
* @param $store_id
* @return SpeakerDevic|null
* @throws \Throwable
*/
public function bindByStoreId($dev_name, $store_id)
{
$sd = null;
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 = $market_id;
// $sd->bind_time = time();
// $sd->saveOrFail();
$sd = SpeakerDevic::query()->updateOrCreate(
['store_id' => $store_id, 'device_name' => $dev_name],
['market_id' => $market_id, 'bind_time' => time(), 'is_bind' => SpeakerDevic::IS_BIND_YES]
);
} catch (Exception $e) {
$this->log->event(LogLabel::DEVICE_LOG, ['msg' => '绑定设备异常:'.$e->getMessage()]);
}
return $sd;
}
/**
* 解绑
* @param $bind_id
* @return int
*/
public function unbindById($bind_id)
{
return SpeakerDevic::query()->where(['id' => $bind_id])->update(['is_bind' => SpeakerDevic::IS_BIND_NO]);
}
/**
* 发布语音消息
* @param $store_id
* @param $msg
*/
public function pubMsgToStoreByDevName($dev_names, $msg)
{
foreach ($dev_names as $key => $dev_name) {
$this->IOTService->pub($dev_name['device_name'], $msg);
}
return true;
}
/**
* 当前设备是否已经被绑定
* @param $dev_name
* @return bool
*/
protected function checkDeviceEnable($dev_name)
{
return SpeakerDevic::query()->where(['device_name' => $dev_name, 'is_bind' => SpeakerDevic::IS_BIND_YES])->exists();
}
}