// +---------------------------------------------------------------------- declare(strict_types=1); namespace addon\iot\controller\backend; use think\facade\Db; use think\facade\View; use ywxapp\controller\BackendBase; use addon\iot\service\DeviceService; use addon\iot\service\DeviceLogService; use addon\iot\service\SensorConfigService; /** * 智能设备控制器(绑定表,预登记 SN → App 扫码绑定激活) */ class Devices extends BackendBase { /** * 设备管理页(layui.table 前端拉取) */ public function device() { return View::fetch('admin/device'); } /** * 设备列表(layui.table 分页 JSON,支持关键词过滤) */ public function deviceTable() { $kw = trim((string) input('get.kw', '')); $page = max(1, (int) input('get.page', 1)); $limit = (int) input('get.limit', 20); if ($limit < 1 || $limit > 100) { $limit = 20; } $q = Db::name('iot_device'); if ($kw !== '') { $q->where(function ($query) use ($kw) { $query->whereLike('sn', "%{$kw}%") ->whereOr('title', 'like', "%{$kw}%") ->whereOr('uuid', 'like', "%{$kw}%"); }); } $count = (int) (clone $q)->count(); $rows = $q->order('id', 'desc')->page($page, $limit)->select()->toArray(); foreach ($rows as &$d) { $d['online'] = DeviceService::isOnline((string) $d['uuid']); $d['member_account'] = (int) $d['member_id'] > 0 ? DeviceService::memberAccount((int) $d['member_id']) : ''; unset($d['secret']); } unset($d); $this->result->success(['list' => $rows, 'count' => $count]); } /** * 登记/编辑设备(华为式:新增=预登记SN,自动生成配对码+激活令牌,绑定激活由设备/App 完成) */ public function saveDevice() { $id = (int) input('post.id', 0); $sn = trim((string) input('post.sn', '')); $title = trim((string) input('post.title', '')); $model = trim((string) input('post.model', '')); $room = trim((string) input('post.room', '')); $status = (int) input('post.status', 1); $pairCode = trim((string) input('post.pair_code', '')); if ($title === '') { $this->result->error('设备名称不能为空'); } try { if ($id > 0) { $update = [ 'title' => $title, 'model' => $model, 'room' => $room, 'status' => $status ? 1 : 0, 'update_at' => time(), ]; if ($pairCode !== '') { $update['pair_code'] = $pairCode; } Db::name('iot_device')->where('id', $id)->update($update); } else { $res = DeviceService::register($sn, $title, $model, $room, $pairCode); $this->result->success($res, '登记成功'); } } catch (\Throwable $e) { $this->result->error('保存失败:' . $e->getMessage()); } $this->result->success('保存成功(预登记设备由设备激活/App 扫码绑定)'); } /** * 重置设备密钥(返回新明文,前端展示一次) */ public function resetDeviceSecret() { $uuid = trim((string) input('post.uuid', '')); try { $res = DeviceService::resetSecret($uuid); } catch (\Throwable $e) { $this->result->error($e->getMessage()); } $this->result->success($res); } /** * 重新生成激活令牌(仅未激活设备,按 SN 定位;已激活设备如需换凭证走 resetDeviceSecret) */ public function resetActivateToken() { $sn = trim((string) input('post.sn', '')); try { $res = DeviceService::resetActivateToken($sn); } catch (\Throwable $e) { $this->result->error($e->getMessage()); } $this->result->success($res); } /** * 删除设备(含 MQTT账号/ACL/影子/订阅/离线队列) */ public function deleteDevice() { $uuid = trim((string) input('post.uuid', '')); DeviceService::remove($uuid); $this->result->success('已删除(含MQTT账号/ACL/影子/订阅)'); } /** * 读取设备传感器配置(配置真源在服务端:后台编辑 → retained 下发设备 + App 共用同一份) */ public function deviceConfig() { $uuid = trim((string) input('get.uuid', '')); if ($uuid === '' || !DeviceService::findByUuid($uuid)) { $this->result->error('设备不存在或未激活(未分配 uuid)'); } $cfg = SensorConfigService::get($uuid); $cfg['uuid'] = $uuid; $this->result->success($cfg); } /** * 保存设备传感器配置(保存即生效:立即入 retained 下发队列,设备在线秒级生效、离线上线即生效) * param: uuid(必填), sensors(JSON 数组:{index,alias,probes,upper,lower,gap,enabled}) * 位置序号键统一 `index`(旧键 `i` 仍兼容) */ public function saveDeviceConfig() { $uuid = trim((string) input('post.uuid', '')); if ($uuid === '' || !DeviceService::findByUuid($uuid)) { $this->result->error('设备不存在或未激活(未分配 uuid)'); } $sensors = input('post.sensors', ''); if (is_string($sensors)) { $sensors = json_decode($sensors, true); } if (!is_array($sensors)) { $this->result->error('sensors 参数须为 JSON 数组'); } try { $cfg = SensorConfigService::save($uuid, $sensors); } catch (\Throwable $e) { $this->result->error($e->getMessage()); } $cfg['uuid'] = $uuid; $this->result->success($cfg, '配置已保存并下发(设备离线则上线后自动生效)'); } /** * 设备日志(JSON,按设备 uuid 分页) */ public function deviceLogs() { $uuid = trim((string) input('get.uuid', '')); $page = max(1, (int) input('get.page', 1)); $size = min(100, max(1, (int) input('get.size', 20))); $data = $uuid !== '' ? DeviceLogService::page($uuid, $page, $size) : ['list' => [], 'total' => 0]; $this->result->success($data); } }