物联网:七天构建一个闭环的物联网DEMO-写一个API

基于DDD 思想,将业务逻辑再分层封装,开放 interface 给 Controller 层调用, 而非原始的 MVC 三层直接相互调用,这样有利于将数据处理逻辑与业务逻辑分层。因此这里提炼了一个 Manager:

复制代码
package com.ruoyi.project.business.controlcenter.iot.manager;

import com.ruoyi.project.business.controlcenter.iot.constant.CommonConstant;
import com.ruoyi.project.business.controlcenter.iot.mqtt.manager.MqttSendManager;
import com.ruoyi.project.business.controlcenter.iot.util.MqttParamUtil;
import com.ruoyi.project.business.controlcenter.iot.util.TopicUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class IotManager {

    @Resource
    private MqttSendManager mqttSendManager;

//    @Resource
//    private IotUserDeviceService userDeviceService;

    public void operateIndex(Integer index, String mac, Integer onOff) {
        String params = MqttParamUtil.getParams(index, mac, onOff, "pin");
        String topic = TopicUtil.extractTopic();
        mqttSendManager.sendToMqtt(topic, CommonConstant.QOS.ONLY_ONCE, params);
    }

    public void blink(Integer index, String mac, Integer onOff) {
        String params = MqttParamUtil.getParams(index, mac, onOff, "blink");
        String topic = TopicUtil.extractTopic();
        mqttSendManager.sendToMqtt(topic, CommonConstant.QOS.ONLY_ONCE, params);
    }

    public void blinkPwm(Integer index, String mac, Integer pmw) {
        String params = MqttParamUtil.getBlinkPwmParams(index, mac, pmw);
        String topic = TopicUtil.extractTopic();
        mqttSendManager.sendToMqtt(topic, CommonConstant.QOS.ONLY_ONCE, params);
    }

//    public void pwm(Integer index, String mac, Integer pmw) {
//        String params = MqttParamUtil.getPwmParams(index, mac, pmw);
//        String topic = TopicUtil.extractTopic();
//        mqttSendManager.sendToMqtt(topic, CommonConstant.QOS.ONLY_ONCE, params);
//    }
//
//    public void pwmOff(Integer index, String mac) {
//        String params = MqttParamUtil.getPwmOffParams(index, mac);
//        String topic = TopicUtil.extractTopic();
//        mqttSendManager.sendToMqtt(topic, CommonConstant.QOS.ONLY_ONCE, params);
//    }

    /**
     *
     * @param mac
     * @param reqType
     * @param seconds 执行时间
     */
//    public void deviceBleOperate(String mac, String reqType, Integer seconds) {
//        String params = MqttParamUtil.getBleAdvParam(mac, reqType, seconds);
//        String topic = TopicUtil.extractTopic();
//        mqttSendManager.sendToMqtt(topic, CommonConstant.QOS.ONLY_ONCE, params);
//    }


//    public void saveDeviceStorage(String ssid, String pwd, Long userDeviceId, Long userId) {
//        IotUserDevice device = userDeviceService.findById(userDeviceId, userId);
//        if(device == null) {
//            throw new ServiceException(ErrorCodeConstants.UNKNOWN_USER_DEVICE);
//        }
//        String params = MqttParamUtil.getSaveWifiParams(ssid, pwd, device.getMac());
//        String topic = TopicUtil.extractTopic();
//        mqttSendManager.sendToMqtt(topic, CommonConstant.QOS.ONLY_ONCE, params);
//    }


//    public void deviceBleCancel(String mac) {
//        String params = MqttParamUtil.getBleAdvCancelParam(mac);
//        String topic = TopicUtil.extractTopic();
//        mqttSendManager.sendToMqtt(topic, CommonConstant.QOS.ONLY_ONCE, params);
//    }
}

以上方法中, 被注释的部分是基于 esp32 的能力定制的接口, 我们可以主要先看未注释的几个方法:

复制代码
operateIndex : 向指定的引脚号写入指定的 value, 
参数:mac 是硬件设备固定的唯一识别码, index 是引脚号,onOff 分别对应 1 / 0 。

我们再看看 Controller 中的调用就比较简单了:

复制代码
package com.ruoyi.project.business.controlcenter;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.model.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.controlcenter.iot.manager.IotManager;
import com.ruoyi.project.business.controlcenter.request.IotRequest;
import com.ruoyi.project.business.gpiobiz.request.ListControlGpioBizRequest;
import com.ruoyi.project.business.gpiobiz.response.ControlGpioBizResponse;
import com.ruoyi.project.business.gpiobiz.service.IControlGpioBizService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 设备引脚业务Controller
 * 
 * @author ${author}
 * @date 2024-07-25
 */
@Slf4j
@RestController
@RequestMapping("/business/house")
public class HouseControlController extends BaseController {
    @Autowired
    private IotManager iotManager;
    @Autowired
    private IControlGpioBizService controlGpioBizService;

    /**
     * 查询家庭已支持远程控制的线路
     */
    @GetMapping("/list")
    public TableDataInfo list(ListControlGpioBizRequest request) {
        startPage();
        Long tenantId = getTenantId();
        List<ControlGpioBizResponse> list = controlGpioBizService.selectListWithMac(request, tenantId);
        return getDataTable(list);
    }

    /**
     * 开指定的 gipo
     */
    /**
     * turn
     * @param req
     * @return
     */
    @PostMapping("/turn")
    public AjaxResult turn(@RequestBody IotRequest req) {
        iotManager.operateIndex(req.getIndex(), req.getMac(), req.getOnOff());
        return AjaxResult.success("ok");
    }
}

通过 /turn 接口就能实现对指定设备的开关操作。如果有更多的操作需要, 可以在 esp32 中实现后, 再定制相应的 api 。

欢迎交流并三连。

(

开源地址:

wowiot: 基于ruoyi二开的一款开源智能硬件管理平台, 当前可支持硬件管理,并且支持 esp32 定制的api,可实现远程开关信号的下发,更多功能欢迎探索,有更多的功能性需求,欢迎入群讨论或者参与进来

)

相关推荐
老梁agent1 天前
MCP 协议实战:用标准化方式让 Agent 调用工业工具
物联网·agent·mcp
老梁agent4 天前
一个 Agent 不够用?工业场景下的多 Agent 路由模式实战
物联网·agent
老梁agent5 天前
从 0 到 22 篇:工业 Agent 的六大设计原则
物联网·agent
老梁agent7 天前
Agent 如何看懂时序数据?时间序列查询的 Tool 设计模式
物联网·agent
Inhand陈工13 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
大鱼>13 天前
大语言模型+物联网:LLM理解物理世界
物联网·struts·语言模型·多模态·aiot
果丁智能13 天前
物联网智能锁赋能集中式住宿:身份核验与远程权限管控的全链路技术实践
大数据·人工智能·物联网·智能家居
国产化创客13 天前
ESP32 CameraWebServer 原生摄像头项目全解析
物联网·开源·嵌入式·实时音视频·智能硬件
谁似人间西林客13 天前
数据智能怎么赋能工业制造?物联网场景落地方法解析
物联网·制造
InHand云飞小白13 天前
无人值守站点网络困境?工业级路由器IR315破解连接难题
网络·物联网·4g·工业路由器·4g路由器·iiot·蜂窝路由器