package com.ruoyi.quartz.task; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.common.constant.IotConstants; import com.ruoyi.common.core.domain.onenet.Datapoint; import com.ruoyi.common.utils.DateUtils; import com.ruoyi.common.utils.http.HttpUtils; import com.ruoyi.common.utils.onenet.Token; import com.ruoyi.device.domain.AsDevice; import com.ruoyi.device.domain.AsWateringRecord; import com.ruoyi.device.mapper.AsDeviceMapper; import com.ruoyi.device.mapper.AsWateringRecordMapper; import com.ruoyi.common.core.domain.onenet.Data; import com.ruoyi.common.core.domain.onenet.DataPointRes; import com.ruoyi.common.core.domain.onenet.Datastream; import com.ruoyi.quartz.domain.WateringLog; import lombok.SneakyThrows; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.List; /** * 定时获取浇花器日志 * * @author qiuzhenzhao */ @Component("logTask") public class IotLogTask { private static final Logger logger = LoggerFactory.getLogger(IotLogTask.class); @Value(value = "${watering.iotUrl}") private String iotUrl; @Value(value = "${watering.productId}") private String productId; @Resource private AsWateringRecordMapper asWateringRecordMapper; @Resource private AsDeviceMapper asDeviceMapper; @SneakyThrows public void getDeviceLog() { logger.info("定时任务--获取浇花器日志"); /**1.获取到所有的设备列表*/ List asDevices = asDeviceMapper.selectAsDeviceList(null); for (AsDevice device : asDevices) { /**1.请求url获取到浇花器日志*/ String param = "device_name=" + device.getMac() + "&product_id=" + productId; String sendUrl = iotUrl+ IotConstants.ADDS_HISTORY_DATAPOINTS + "?"+param; String token = Token.getToken(); logger.info("IOT获取到Authorization:【{}】",token); String result = HttpUtils.sendGetWithToken(sendUrl, null, token); logger.info("IOT返回的结果【{}】",result); /**2.处理返回的参数*/ DataPointRes dataPointRes = JSONObject.parseObject(result, DataPointRes.class); Data data = dataPointRes.getData(); List datastreams = data.getDatastreams(); for (Datastream datastream: datastreams) { if(datastream.getId().equals("jj")){ List datapoints = datastream.getDatapoints(); for (Object obj:datapoints) { // String s = JSON.toJSONString(obj); // Datapoint datapoint = JSONObject.parseObject(s, Datapoint.class); // DatapointValue value = datapoint.getValue(); // DatapointValue.Ds ds = value.getDs();//定时 // boolean kaiguan3 = ds.getKaiguan();//定时开关 // int js_sec1 = ds.getJs_sec();//喷洒时间 // int start_hour = ds.getStart_hour();//启动小时 // int start_min = ds.getStart_min();//启动分钟 // // DatapointValue.Mc mc = value.getMc();//脉冲模式 // int jg_sec = mc.getJg_sec();//间隔时间 // int js_sec = mc.getJs_sec();//启动时间 // boolean kaiguan2 = mc.getKaiguan();//开关 // // // DatapointValue.Set set = value.getSet();//设置 // boolean kaiguan1 = set.getKaiguan();//开关 // int xp_min = set.getXp_min();//息屏分钟 // // DatapointValue.Tr tr = value.getTr();//土壤 // int end_sd = tr.getEnd_sd();//结束湿度 // boolean kaiguan = tr.getKaiguan();//开关 // int start_sd = tr.getStart_sd();//启动湿度 // // int turan_show = value.getTuran_show();//土壤当前湿度 // int jiaoshui_qiangdu = value.getJiaoshui_qiangdu();//浇水强度 String s ="{\"water_time\":\"2023-12-06\",\"start_mode\":\"1\",\"spraying_time\":60,\"mc\":{\"jg_sec\":10,\"js_sec\":5,\"kaiguan\":false},\"jiaoshui_qiangdu\":3,\"start_moisture\":10,\"end_moisture\":10}\n"; WateringLog wateringLog = JSONObject.parseObject(s, WateringLog.class); /**3.将DatapointValue对象转成*/ AsWateringRecord build = AsWateringRecord.builder() .deviceId(device.getDeviceId()) .waterTime(DateUtils.dateTime(DateUtils.YYYY_MM_DD, wateringLog.getWater_time())) .startMode(wateringLog.getStart_mode()) .sprayingTime(Integer.parseInt(wateringLog.getSpraying_time())) .pulseMode(wateringLog.getMc().getKaiguan() + "") .pulseModeParam(wateringLog.getMc().toString()) .waterIntensity(wateringLog.getJiaoshui_qiangdu()) .startMoisture(wateringLog.getStart_moisture()) .endMoisture(wateringLog.getEnd_moisture()) .userName(device.getUserName()) .build(); build.setCreateTime(DateUtils.getNowDate()); int i = asWateringRecordMapper.insertAsWateringRecord(build); /**3.根据最新时间保存数据*/ } } } } } }