物联网:七天构建一个闭环的物联网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,可实现远程开关信号的下发,更多功能欢迎探索,有更多的功能性需求,欢迎入群讨论或者参与进来

)

相关推荐
白搞电子7 小时前
ZeroClaw 是什么?与 ESP-Claw 的区别及文件整理用途示例
windows·物联网
南京码讯光电技术有限公司8 小时前
工业物联网关选型要看哪些参数?12 个硬指标与工业路由器配置对照
物联网
LiuYanG8 小时前
WebSocket 推送上线之后才发现的三个坑:连接集并发、坏连接与推送降级
物联网
Spatial3Ddddd9 小时前
数字孪生接IoT做仓储物流地基是什么
大数据·人工智能·物联网
夏虫不可与之言冰12 小时前
第三课 点亮 LED 与定时器非阻塞闪烁
c语言·c++·vscode·单片机·嵌入式硬件·物联网
Chery114012 小时前
低功耗无线传感器节点设计与射频芯片选型指南:BLE、Sub-1GHz与LoRa方案对比
物联网·射频工程
注塑机数据采集联网13 小时前
注塑机数据采集网关读取注塑机工艺/SPC数据计算原理
大数据·网络·数据仓库·物联网·智能路由器·制造
Chery114013 小时前
ESP32-C3FH8X技术解析:8MB Flash低功耗Wi-Fi与Bluetooth LE物联网SoC
单片机·物联网
笨笨饿15 小时前
#142_VS Code.map文件插件的用法
git·python·stm32·单片机·嵌入式硬件·物联网·vim
lhh_qrsly15 小时前
OTA 加密完整方案(嵌入式 IoT 常用)
开发语言·物联网·php