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

)

相关推荐
薛慕昭18 小时前
《从硬件到云端:STC8H ADC数据采集与华为物联网平台对接全解析》
服务器·物联网
zskj_zhyl18 小时前
银发科技:AI健康小屋如何破解老龄化困局
人工智能·科技·物联网
Blossom.11819 小时前
量子计算在密码学中的应用与挑战:重塑信息安全的未来
人工智能·深度学习·物联网·算法·密码学·量子计算·量子安全
龙大大L1 天前
第五章:5.3 ESP32物联网应用:阿里云IoT平台与腾讯云IoT平台的数据上传与远程控制
java·物联网·struts·esp32
移远通信1 天前
QuecPython+GNSS:实现快速定位
物联网·gnss·quecpython
小叮当⇔2 天前
IOT项目——物联网 GPS
物联网
程序边界2 天前
DeepSeek在物联网设备中的应用:通过轻量化模型实现本地化数据分析
物联网·struts·数据分析
网易独家音乐人Mike Zhou2 天前
【Linux应用】交叉编译环境配置,以及最简单粗暴的环境移植(直接从目标板上复制)
linux·stm32·mcu·物联网·嵌入式·iot
塔能物联运维2 天前
解析塔能科技:绿色低碳智慧节能一站式破局之匙
大数据·人工智能·物联网
无脑学c++2 天前
STM32串口重定向:MDK与GCC重定向需重写的不同函数
stm32·单片机·物联网