Commit 38f269ab1fc6bf89a0d8cd9d1ef8a8cb57dcf3c1

Authored by 徐泉
1 parent 7326522f
Exists in master

物联代码提交

cloud/mypulsar/src/main/java/com/example/mypulsar/MyRunnerableInt.java
... ... @@ -8,7 +8,6 @@ import com.example.mypulsar.mq.AESBase64Utils;
8 8 import com.example.mypulsar.mq.MessageHandlerTask;
9 9 import com.example.mypulsar.mq.MqConsumer;
10 10 import com.example.mypulsar.mqtt.MqttConsumer;
11   -import com.example.mypulsar.task.SchduledTasks;
12 11 import com.example.mypulsar.utils.ControlUtils;
13 12 import com.example.mypulsar.utils.JsonUtils;
14 13 import com.example.mypulsar.utils.PulsarConsumerPoolFactory;
... ... @@ -25,7 +24,6 @@ import org.springframework.util.CollectionUtils;
25 24 import org.springframework.util.StringUtils;
26 25  
27 26 import java.text.SimpleDateFormat;
28   -import java.util.ArrayList;
29 27 import java.util.Date;
30 28 import java.util.List;
31 29 import java.util.function.BiConsumer;
... ... @@ -76,7 +74,7 @@ public class MyRunnerableInt implements ApplicationRunner {
76 74 //通过 AES (ECB 模式)对 accessKey 的中间 16 位代码进行解密
77 75 String data = AESBase64Utils.decrypt(vo.getData(), accessKey.substring(8, 24));
78 76 TuYaReceiverBean tuYaReceiverBean = JSON.parseObject(data, TuYaReceiverBean.class);
79   - log.info("接收消息,消息内容:"+ JSON.toJSONString(tuYaReceiverBean));
  77 +// log.info("接收消息,消息内容:"+ JSON.toJSONString(tuYaReceiverBean));
80 78 //处理数据上报
81 79 detealData(tuYaReceiverBean);
82 80 });
... ... @@ -128,8 +126,7 @@ public class MyRunnerableInt implements ApplicationRunner {
128 126 log.info("设备: {},已掉线",deviceName);
129 127 deviceBean.setDev_type(1);
130 128 deviceBean.setDev_status(0);
131   - updateDevStatus(deviceBean);
132   - updateStatus(devId, "0");
  129 + updateStatus(deviceBean.getDevId(), "0");
133 130 break;
134 131 case "online"://设备上线
135 132 log.info("设备: {},已上线",deviceName);
... ... @@ -155,13 +152,7 @@ public class MyRunnerableInt implements ApplicationRunner {
155 152 }
156 153  
157 154 private void updateStatus(String devId, String isConnection) {
158   -// String clintId = deviceDao.selectAttendance(devId);
159   -// if (clintId.equals(devId)) {
160 155 deviceDao.updateStatus(devId, isConnection, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
161   -// } else {
162   -// deviceDao.insert(devId, "-2", isConnection);
163   -// }
164   -
165 156 }
166 157  
167 158 private void updateDevStatus(DeviceBean deviceBean) {
... ... @@ -173,8 +164,7 @@ public class MyRunnerableInt implements ApplicationRunner {
173 164 if (deviceBean.getDataId() == null) deviceBean.setDataId("");
174 165 if (deviceBean.getCode() == null) deviceBean.setCode("");
175 166 if (deviceBean.getProductKey() == null) deviceBean.setProductKey("");
176   - log("设备上线,更新历史记录: "+JSON.toJSONString(deviceBean));
177   - int index = deviceDao.addDevice(deviceBean.getDataId(), deviceBean.getDevId(), deviceBean.getProductKey(), deviceBean.getCode(), deviceBean.getValue()
  167 + deviceDao.addDevice(deviceBean.getDataId(), deviceBean.getDevId(), deviceBean.getProductKey(), deviceBean.getCode(), deviceBean.getValue()
178 168 , deviceBean.getBizCode(), deviceBean.getTime(), deviceBean.getName(), deviceBean.getDpId(), deviceBean.getUid(), deviceBean.getDev_status(), deviceBean.getDev_type());
179 169 updateStatus(deviceBean.getDevId(), "1");
180 170 }
... ... @@ -206,16 +196,11 @@ public class MyRunnerableInt implements ApplicationRunner {
206 196 switch (code) {
207 197 case "va_temperature"://温度
208 198 deviceBean.setValue(Integer.parseInt(value) / 100 + "");
209   - log.info("温度:" + deviceBean.getValue());
210   - //存储 对应设备对应的温度记录
211   - ControlUtils.tempDevices.put(deviceBean.getDevId(),Integer.valueOf(deviceBean.getValue()));
212 199 break;
213 200 case "va_humidity"://湿度
214 201 deviceBean.setValue(Integer.parseInt(value) / 100 + "");
215   - log.info("湿度:" + deviceBean.getValue());
216 202 break;
217 203 case "pir"://人体感应
218   - log.info("人体感应:" + value);
219 204 if(value.equals("pir")){
220 205 deviceBean.setValue("有人");
221 206 }else{
... ... @@ -232,18 +217,18 @@ public class MyRunnerableInt implements ApplicationRunner {
232 217 case "battery_value"://电池电量值
233 218 break;
234 219 case "battery"://门磁电池电量值
235   - log("电池电量值:" + value);
  220 + log.info("电池电量值:" + value);
236 221 break;
237 222 case "battery_state"://电池电量状态 {“range”:[“low”,“middle”,“high”]}
238   - log("电池电量状态:" + value);
  223 + log.info("电池电量状态:" + value);
239 224 break;
240 225 case "doorcontact_state"://门磁状态
241 226 value = (value.equals("true") ? "开门" : "关门");
242   - log("门磁状态:" + (value.equals("true") ? "开门" : "关门"));
  227 + log.info("门磁状态:" + (value.equals("true") ? "开门" : "关门"));
243 228 break;
244 229 case "switch"://门磁状态
245 230 value = (value.equals("true") ? "开门" : "关门");
246   - log("开关状态:" + (value.equals("true") ? "开门" : "关门"));
  231 + log.info("开关状态:" + (value.equals("true") ? "开门" : "关门"));
247 232 break;
248 233 case "cur_voltage"://当前电压
249 234 if(value.equals("0")){
... ... @@ -251,7 +236,7 @@ public class MyRunnerableInt implements ApplicationRunner {
251 236 }else{
252 237 value = Integer.parseInt(value) / 10 + "";
253 238 }
254   - log(deviceBean.getDevId() + " 当前电压:" + value +"v");
  239 + log.info(deviceBean.getDevId() + " 当前电压:" + value +"v");
255 240 break;
256 241 case "cur_power"://当前功率
257 242 if(value.equals("0")){
... ... @@ -259,7 +244,7 @@ public class MyRunnerableInt implements ApplicationRunner {
259 244 }else{
260 245 value = Integer.parseInt(value) / 10+"";
261 246 }
262   - log(deviceBean.getDevId() + " 当前功率:" + value + "W");
  247 + log.info(deviceBean.getDevId() + " 当前功率:" + value + "W");
263 248 break;
264 249 case "cur_current"://当前电流
265 250 if(value.equals("0")){
... ... @@ -267,7 +252,7 @@ public class MyRunnerableInt implements ApplicationRunner {
267 252 }else{
268 253 value = Integer.parseInt(value) / 10+"";
269 254 }
270   - log(deviceBean.getDevId() + " 当前电流:" + value + "A");
  255 + log.info(deviceBean.getDevId() + " 当前电流:" + value + "A");
271 256 break;
272 257 case "smoke_sensor_state"://烟雾报警
273 258 break;
... ... @@ -310,80 +295,9 @@ public class MyRunnerableInt implements ApplicationRunner {
310 295 //获取人感设备控制
311 296 List<CalDevContrl> calDevContrls = deviceDao.getCalDevContrlWidthDevId(deviceBean.getDevId());
312 297 if (CollectionUtils.isEmpty(calDevContrls)) {
313   - log.info("未关联人感控制设备, 设备ID: "+ deviceBean.getDevId());
  298 + log.error("未关联人感控制设备, 设备ID: "+ deviceBean.getDevId());
314 299 return;
315 300 }
316 301 ControlUtils.getInstance(deviceDao).conTemper(calDevContrls, deviceBean);
317 302 }
318   -
319   - /**
320   - * 设置更新上报时间
321   - * @param clintId
322   - */
323   - private void setClintTime(String clintId){
324   - //获取人感设备
325   - List<CalDevContrl> calDevContrls = deviceDao.getCalDevContrlWidthDevId(clintId);
326   - if(!CollectionUtils.isEmpty(calDevContrls)){
327   - CalDevContrl calDevContrl = calDevContrls.get(0);
328   - String assDevice = calDevContrl.getAssDevice();
329   - //获取人感关联红外遥控器
330   - CalDevBeContrl hwClint = getCalDevBeCon(assDevice);
331   - if(hwClint==null) {
332   - log.info("未查询到控制红外遥控器");
333   - return;
334   - }
335   - //红外遥控设备id
336   - String conDevId = hwClint.getConDevId();
337   - SchduledTasks.closeDevMap.put(conDevId,new Date());
338   - }
339   - }
340   -
341   - private CalDevBeContrl getCalDevBeCon(String assDevice) {
342   - String[] assDeviceIds = assDevice.split(",");
343   - List<CalDevBeContrl> devBeContrlList = new ArrayList<>();
344   - for (int i = 0; i < assDeviceIds.length; i++) {
345   - String deviceId = assDeviceIds[i];
346   - //获取温感设备
347   - List<CalDevContrl> calDevContrl = deviceDao.getTempCalWitdDevid(deviceId);
348   - if(!CollectionUtils.isEmpty(calDevContrl)){
349   - for (int j = 0; j < calDevContrl.size(); j++) {
350   - //温感设备
351   - CalDevContrl calDevContrl1 = calDevContrl.get(j);
352   - if (!calDevContrl1.getDevBeId().equals("0")) {
353   - String[] calDevBeIdStr = calDevContrl1.getDevBeId().split(",");
354   - if (calDevBeIdStr != null) {
355   - for (int k = 0; k < calDevBeIdStr.length; k++) {
356   - //红外设备
357   - CalDevBeContrl calDevBeContrl = deviceDao.getCalDevBeControl(calDevBeIdStr[k]);
358   - devBeContrlList.add(calDevBeContrl);
359   - }
360   - }
361   - }
362   - }
363   - }
364   - }
365   - if(devBeContrlList.size()>0){
366   - return devBeContrlList.get(0);
367   - }
368   - return null;
369   - }
370   -
371   - /**
372   - * 根据温度变化控制设备
373   - *
374   - * @param deviceBean
375   - */
376   - private void calOpenOrCloseDevWithtemper(DeviceBean deviceBean) {
377   -
378   - ControlUtils.tempDevices.put(deviceBean.getDevId(), Integer.valueOf(deviceBean.getValue()));
379   -
380   -// List<CalDevContrl> calDevContrls = deviceDao.getCalDevContrlWidthDevId(deviceBean.getDevId());
381   -// if (null != calDevContrls && calDevContrls.size() > 0)
382   -// ControlUtils.getInstance(deviceDao).conTemper(calDevContrls, deviceBean);
383   -
384   - }
385   -
386   - private void log(String content) {
387   - System.out.println("接收到数据:" + content);
388   - }
389 303 }
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/bean/AirCommand.java 0 → 100644
... ... @@ -0,0 +1,29 @@
  1 +package com.example.mypulsar.bean;
  2 +
  3 +/**
  4 + * Created with IntelliJ IDEA.
  5 + *
  6 + * @Auther: xuquan
  7 + * @Date: 2021/04/28 17:15
  8 + * @Description:
  9 + */
  10 +public class AirCommand {
  11 + private Integer key;
  12 + private String value;
  13 +
  14 + public Integer getKey() {
  15 + return key;
  16 + }
  17 +
  18 + public void setKey(Integer key) {
  19 + this.key = key;
  20 + }
  21 +
  22 + public String getValue() {
  23 + return value;
  24 + }
  25 +
  26 + public void setValue(String value) {
  27 + this.value = value;
  28 + }
  29 +}
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/bean/AirLinkMode.java 0 → 100644
... ... @@ -0,0 +1,32 @@
  1 +package com.example.mypulsar.bean;
  2 +
  3 +import java.util.List;
  4 +
  5 +/**
  6 + * Created with IntelliJ IDEA.
  7 + *
  8 + * @Auther: xuquan
  9 + * @Date: 2021/04/28 17:13
  10 + * @Description:
  11 + */
  12 +public class AirLinkMode {
  13 +
  14 + private Integer deviceType;
  15 + private List<AirCommand> KVs;
  16 +
  17 + public Integer getDeviceType() {
  18 + return deviceType;
  19 + }
  20 +
  21 + public void setDeviceType(Integer deviceType) {
  22 + this.deviceType = deviceType;
  23 + }
  24 +
  25 + public List<AirCommand> getKVs() {
  26 + return KVs;
  27 + }
  28 +
  29 + public void setKVs(List<AirCommand> KVs) {
  30 + this.KVs = KVs;
  31 + }
  32 +}
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/bean/Wl_Attendace.java
1 1 package com.example.mypulsar.bean;
2 2  
3   -import afu.org.checkerframework.checker.igj.qual.I;
4   -
5 3 import java.io.Serializable;
6 4  
7 5 public class Wl_Attendace implements Serializable {
... ... @@ -12,6 +10,8 @@ public class Wl_Attendace implements Serializable {
12 10  
13 11 private String name;
14 12  
  13 + private Integer isConnection;
  14 +
15 15 public Integer getSchool_id() {
16 16 return school_id;
17 17 }
... ... @@ -36,6 +36,14 @@ public class Wl_Attendace implements Serializable {
36 36 this.name = name;
37 37 }
38 38  
  39 + public Integer getIsConnection() {
  40 + return isConnection;
  41 + }
  42 +
  43 + public void setIsConnection(Integer isConnection) {
  44 + this.isConnection = isConnection;
  45 + }
  46 +
39 47 @Override
40 48 public String toString() {
41 49 return "Wl_Attendace{" +
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/bean/Wl_LinkModeBean.java
... ... @@ -8,6 +8,7 @@ public class Wl_LinkModeBean {
8 8  
9 9 private String Field;
10 10  
  11 + private String DevicePara;
11 12  
12 13 public String getModeName() {
13 14 return ModeName;
... ... @@ -33,6 +34,14 @@ public class Wl_LinkModeBean {
33 34 Field = field;
34 35 }
35 36  
  37 + public String getDevicePara() {
  38 + return DevicePara;
  39 + }
  40 +
  41 + public void setDevicePara(String devicePara) {
  42 + DevicePara = devicePara;
  43 + }
  44 +
36 45 @Override
37 46 public String toString() {
38 47 return "Wl_LinkModeBean{" +
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/dao/DeviceDao.java
... ... @@ -32,15 +32,8 @@ public interface DeviceDao {
32 32 @Select("select * from Iot_Device where devId = #{devId}")
33 33 List<DeviceBean> getDevice(@Param("devId") String devId);
34 34  
35   - //获取联动控制模板
36   - @Select("select * from CalDevContrl ")
37   - List<CalDevContrl> getCalDevContrl();
38   -
39   - @Select("select top 1 * from WL_Attendance where clint_id = #{clint_id} and isConnection =1 and clint_type =10")
40   - Wl_Attendace getAttendace(@Param("clint_id") String clint_id);
41   -
42   - @Select("select * from CalDevContrl where Devid = #{Devid} and ModelType =10 and Status = 1")
43   - List<CalDevContrl> getCalDevContrlWidthDevId(@Param("Devid") String devId);
  35 + @Select("select top 1 value from Iot_Device where devId = #{devId} and code = 'va_temperature' ORDER BY time desc")
  36 + Integer getDeviceTemp(@Param("devId") String devId);
44 37  
45 38 @Select("select * from CalDevBeContrl where State = 1 and Id = #{Id}")
46 39 CalDevBeContrl getCalDevBeControl(@Param("Id") String Id);
... ... @@ -48,31 +41,41 @@ public interface DeviceDao {
48 41 @Select("select top 1 * from CalDevBeContrl where State = 1 and ConDevId = #{devId}")
49 42 CalDevBeContrl getHWDevControl(@Param("devId") String devId);
50 43  
51   - @Select("select * from CalDevContrl where Devid = #{Devid} and Status = 1 and ModelType = 9")
52   - List<CalDevContrl> getTempCalWitdDevid(@Param("Devid") String devId);
53   -
54   - @Select("select top 1* from CalDevContrl where DevBeId = #{beId} and Status = 1 and ModelType = 9")
55   - CalDevContrl getTempDevice(@Param("beId") Integer beId);
  44 + @Select("select top 1 * from WL_Attendance where clint_id = #{clint_id} and clint_type =10")
  45 + Wl_Attendace getAttendace(@Param("clint_id") String clint_id);
56 46  
57   - @Select("select clint_id\n" +
58   - "from WL_Attendance where school_id = 1085 and clint_type = 12 and state = 1")
59   - List<String> getHWDEvices(int i);
  47 + /***
  48 + * @Description: 获取人感
  49 + * @Param: [devId]
  50 + */
  51 + @Select("select * from CalDevContrl where Devid = #{Devid} and Status = 1 and ModelType =10")
  52 + List<CalDevContrl> getCalDevContrlWidthDevId(@Param("Devid") String devId);
  53 +
  54 + /***
  55 + * @Description: 获取温感
  56 + * @Param: [devId]
  57 + */
  58 + @Select({"select * from CalDevContrl where Devid = #{Devid} and Status = 1 and ModelType = 9"})
  59 + List<CalDevContrl> getTempCalWitdDevid(@Param("Devid") String devId);
60 60  
  61 + /**
  62 + * 获取红外遥控控制命令
  63 + * @param ConDevId
  64 + * @return
  65 + */
61 66 @Select("select TOP(1) ConValue from CalDevBeContrl where State = 1 and ConDevId = #{ConDevId}")
62 67 String getConBeValue(@Param("ConDevId") String ConDevId);
63 68  
64   -
65   - @Select("select Top(1) InUserTime from CalDevContrl where Status = 1")
66   - String getInuserTime();
67   -
68   - @Select("select clint_id from WL_Attendance where State = 1 and school_id = 1085 and clint_type = 12")
  69 + /**
  70 + * 获取所有红外遥控器
  71 + * @return
  72 + */
  73 + @Select("select clint_id,school_id,name,isConnection from WL_Attendance where State = 1 and school_id = 1085 and clint_type = 12")
69 74 List<Wl_Attendace> getHWIds();
70 75  
71   -
72 76 @Select("select * from WL_Attendance where state = 1 and school_id = 1085")
73 77 List<Wl_Attendace> getWL_AttendanceIds();
74 78  
75   -
76 79 @Select("select Top(1)name from WL_Attendance where State = 1 and school_id = 1085 and clint_type = 12 and clint_id =#{clint_id} order by intime desc")
77 80 String getWLName(@Param("clint_id") String clint_id);
78 81  
... ... @@ -82,11 +85,13 @@ public interface DeviceDao {
82 85  
83 86 List<Wl_LinkModeBean> getWL_LinkMode(@Param("SchoolId") String SchoolId);
84 87  
85   - IotDevice getIotDeviceRecord(@Param("devId") String devId, @Param("value") String value,@Param("openTime") String openTime);
  88 + IotDevice getIotDeviceAfterRecord(@Param("devId") String devId, @Param("value") String value,@Param("openTime") String openTime);
86 89  
87   - List<Wl_Attendace> getHWAtts();
  90 + IotDevice getIotDeviceRecord(@Param("devId") String devId, @Param("value") String value);
88 91  
  92 + List<Wl_Attendace> getHWAtts();
89 93  
  94 + CalDevContrl getTempDevice(@Param("beId") String beId);
90 95  
91 96  
92 97 }
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/task/SchduledTasks.java
... ... @@ -5,9 +5,8 @@ import com.alibaba.fastjson.JSONObject;
5 5 import com.example.mypulsar.bean.*;
6 6 import com.example.mypulsar.campusDao.CampusDao;
7 7 import com.example.mypulsar.dao.DeviceDao;
  8 +import com.example.mypulsar.utils.DateFormatUtil;
8 9 import com.example.mypulsar.utils.HttpUtil;
9   -import com.example.mypulsar.utils.JsonUtils;
10   -import lombok.Data;
11 10 import lombok.extern.slf4j.Slf4j;
12 11 import org.dom4j.Document;
13 12 import org.dom4j.DocumentException;
... ... @@ -36,16 +35,20 @@ public class SchduledTasks {
36 35  
37 36 public static Map<String, Date> conDevMap = new HashMap<>();//存储被控制的设备
38 37  
39   - public static Map<String, Date> closeDevMap = new HashMap<>();
40   -
41   - private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
42   -
43 38 private String closeModel = "{\"mode\":\"0\",\"power\":\"0\",\"remote_id\":\"%s\",\"remote_index\":\"11272\",\"temp\":\"26\",\"wind\":\"0\"}";
44 39  
45 40 boolean isClose = false;
46 41 boolean outClose= false;
47 42  
48   - @Scheduled(fixedRate = 60000)
  43 +
  44 + /***
  45 + * @Description: 每间隔2分钟关闭一次到点设备无人状态下设备
  46 + * @Param: []
  47 + * @return: void
  48 + * @Author: xuquan
  49 + * @Date: 2021/4/29
  50 + */
  51 + @Scheduled(fixedRate = 300*1000)
49 52 public void closeAttend() {
50 53 log.info(".......定时任务执行设备关闭.......,有在线遥控设备数量" + conDevMap.size());
51 54 Iterator<Map.Entry<String, Date>> it = conDevMap.entrySet().iterator();
... ... @@ -58,17 +61,19 @@ public class SchduledTasks {
58 61 //获取红外绑定的遥控器ID
59 62 String conValue = getRemoteId(deviceDao.getConBeValue(conBeDeviceId));
60 63 Date openTime = entry.getValue();
61   - log.info("在线红外设备:" + name + "---在线时间:" + simpleDateFormat.format(openTime)+" 当前时间:"+ simpleDateFormat.format(new Date()));
  64 + //空调开启时间
  65 + String openTimeStr = DateFormatUtil.dateFormatString(openTime,"yyyy-MM-dd HH:mm:ss");
  66 + String nowTimeStr = DateFormatUtil.dateFormatString(new Date(),"yyyy-MM-dd HH:mm:ss");
  67 + log.info("在线红外设备:" + name + "---在线时间:" + openTimeStr +" 当前时间:"+ nowTimeStr);
62 68 //获取红外设备
63 69 CalDevBeContrl hwDevice = deviceDao.getHWDevControl(conBeDeviceId);
64 70 if(hwDevice !=null){
65   - Integer beId = hwDevice.getId();
  71 + String beId = hwDevice.getId() + "";
66 72 //获取温感设备
67 73 CalDevContrl calDevContrl = deviceDao.getTempDevice(beId);
68 74 //无人状态直接关闭
69   - boolean isOk = convertDeviceRecord(calDevContrl,openTime);
  75 + boolean isOk = convertDeviceRecord(calDevContrl,openTimeStr);
70 76 if(isOk){
71   - log.info("开始执行关闭命令, remote_id: {}",conValue);
72 77 String response = HttpUtil.addAirCode(String.format(closeModel, conValue), conBeDeviceId, name);
73 78 try{
74 79 // 将字符串转为XML
... ... @@ -80,7 +85,7 @@ public class SchduledTasks {
80 85 log.info("请求结果:" + title);
81 86 if(title.equals("200")){
82 87 log.info("关闭成功:设备 :{},遥控ID: {}",name,conValue);
83   - conDevMap.remove(conBeDeviceId);
  88 + it.remove();
84 89 }
85 90 } catch (DocumentException e) {
86 91 e.printStackTrace();
... ... @@ -91,141 +96,47 @@ public class SchduledTasks {
91 96 }
92 97 }
93 98 }
94   - if (!outClose){
95   - initWlModeOut();
96   - }
97   - }
98   -
99   -
100   - /**
101   - * 解析上报记录,判断空间是否是无人状态。
102   - * 条件:第一次上报无人后,五分钟内没有上报有人记录,则为无人
103   - * @param calDevContrl
104   - * @param openTime
105   - * @return
106   - */
107   - private boolean convertDeviceRecord(CalDevContrl calDevContrl,Date openTime){
108   - if(calDevContrl != null){
109   - List<String> list = new ArrayList();
110   - Map map = new HashMap();
111   - //温感设备ID
112   - String tempDevId =calDevContrl.getDevid();
113   - //空调开启时间
114   - String openTimeStr = simpleDateFormat.format(openTime);
115   - //温感关联的人感设备
116   - String assDevice = calDevContrl.getAssDevice();
117   - String[] assDeviceIds = assDevice.split(",");
118   - for (int i = 0; i < assDeviceIds.length; i++) {
119   - String deviceId = assDeviceIds[i];
120   - //过滤温感设备ID
121   - if (!deviceId.equals(tempDevId)) {
122   - list.add(deviceId);
123   - Wl_Attendace attendance = deviceDao.getAttendace(deviceId);
124   - if(attendance==null){
125   - map.put(deviceId,"true");
126   - continue;
127   - }
128   - //取得空调开启时间,离最近一次上报无人记录
129   - IotDevice iotDevice = deviceDao.getIotDeviceRecord(deviceId, "无人", openTimeStr);
130   - log.info("空调开启时间最近一次,无人记录,时间:{}, 设备信息:{}",openTime,JSON.toJSON(iotDevice));
131   - if(iotDevice != null){
132   - //五分钟内没有上报有人记录
133   - String afterTime5 = getDateAddMinute(iotDevice.getTime(),5);
134   - iotDevice = deviceDao.getIotDeviceRecord(deviceId,"有人",afterTime5);
135   - log.info("5分钟之后的有人记录,时间:{}, 设备信息:{}",afterTime5,JSON.toJSON(iotDevice));
136   - if(iotDevice ==null){
137   - map.put(deviceId,"true");
138   - }else{
139   - map.put(deviceId,"false");
140   - }
141   - }else{
142   - map.put(deviceId,"false");
143   - }
144   - }
145   - }
146   - log.info("设备信息:{}, 上报记录: {}",JSON.toJSON(list),JSON.toJSON(map));
147   - //false 无人 true 有人,当前后人感都为无人下,返回true
148   - if(!CollectionUtils.isEmpty(list) && !map.isEmpty()){
149   - if(map.get(list.get(0)).equals("true")&& map.get(list.get(1)).equals("true")){
150   - return true;
151   - }
152   - }
153   - }
154   - return false;
155 99 }
156 100  
157   - /**
158   - * 日期叠加计算,以月为单位:整数
159   - * @param date 起始时间
160   - * @return
161   - */
162   - public static String getDateAddMinute(String date,int length) {
163   - Date date1 = stringFormatDate(date,"yyyy-MM-dd HH:mm:ss");
164   - Calendar cal = Calendar.getInstance();
165   - cal.setTime(date1);//设置起时间
166   - cal.add(Calendar.MINUTE,length);
167   - String strDate = dateFormatString(cal.getTime(),"yyyy-MM-dd HH:mm:ss");
168   - return strDate;
169   - }
170 101  
171   - /**
172   - * String 转 Date : 指定格式:pattern
173   - * @param date
174   - * @param pattern
175   - * @return
  102 + /***
  103 + * @Description: 每间每隔1分钟执行一次到点关闭
  104 + * @Param: []
  105 + * @return: void
  106 + * @Author: xuquan
  107 + * @Date: 2021/4/29
176 108 */
177   - public static Date stringFormatDate(String date, String pattern) {
178   - SimpleDateFormat simpleDateFormat = null;
179   - if (date != null) {
180   - try{
181   - simpleDateFormat = new SimpleDateFormat(pattern);
182   - Date retultDate = simpleDateFormat.parse(date);
183   - return retultDate;
184   - }catch (ParseException e){
185   - e.printStackTrace();
186   - }
  109 + @Scheduled(fixedDelay = 60*1000)
  110 + public void closeDevice(){
  111 + if (!outClose){
  112 + initWlModeOut();
187 113 }
188   - return null;
189 114 }
190 115  
191   - /**
192   - * Date转 String : 指定格式:pattern
193   - * @param date
194   - * @param pattern
195   - * @return
  116 + /***
  117 + * @Description: 在06:00-- 22:00之外,每隔一个小时执行一次计划之外关闭
  118 + * @Param: []
  119 + * @return: void
  120 + * @Author: xuquan
  121 + * @Date: 2021/4/29
196 122 */
197   - public static String dateFormatString(Date date,String pattern) {
198   - SimpleDateFormat df = null;
199   - String returnValue = null;
200   - if (date != null) {
201   - df = new SimpleDateFormat(pattern);
202   - returnValue = df.format(date);
203   - }
204   - return returnValue;
205   - }
206   -
207   -
208   - @Scheduled(cron = "0 0/30 * * * ?")
  123 + @Scheduled(cron = "0 0 * * * ?")
209 124 public void closeAttendOut() {
210 125 int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
211   - if (!isClose && hour < 23 && hour > 5){
  126 + if (!isClose && hour > 22 && hour < 6){
212 127 initWlMode();
213 128 }
214 129 }
215 130  
216   - public static void main(String[] args) {
217   - System.out.println(Calendar.getInstance().get(Calendar.HOUR_OF_DAY));
218   - }
219   -
220 131 /**
221 132 * 根据物联模板判断是否关闭
222 133 */
223 134 public void initWlModeOut() {
224 135 try {
225   - isClose = true;
  136 + outClose = true;
226 137 //获取有效的模板
227 138 List<Wl_LinkModeBean> wl_linkModes = deviceDao.getWL_LinkMode("1085");
228   - if (null != wl_linkModes) {
  139 + if (!CollectionUtils.isEmpty(wl_linkModes)) {
229 140 for (int i = 0; i < wl_linkModes.size(); i++) {
230 141 Wl_LinkModeBean wl_linkModeBean = wl_linkModes.get(i);
231 142 String field = wl_linkModeBean.getField();//场地id
... ... @@ -239,39 +150,39 @@ public class SchduledTasks {
239 150 String weekStr = jsonObject.getString("Week");
240 151 String[] weeks = weekStr.split(",");
241 152 String endTime = jsonObject.getString("EndTime");
242   - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
243   - SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm:ss");
244 153 int indexWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
245 154 for (int k = 0; k < weeks.length; k++) {
246 155 int week = Integer.parseInt(weeks[k]);
247 156 if (week == indexWeek) {
248   - try {
249   - //截至时间
250   - Date endDate = simpleDateFormat.parse(endTime);
251   - Date endDate2 = getDateByTime(3,endDate);
252   - long entTime = endDate.getTime();
253   - long entTime2 = endDate2.getTime();
254   - //当前时间
255   - Date date = simpleDateFormat1.parse(new Date().getHours() + ":" + new Date().getMinutes() + ":" + new Date().getSeconds());
256   - long indexTime = date.getTime();
257   - //当前时间等于结束时间触发一次指令
258   - if (indexTime > entTime && indexTime < entTime2) {
259   - //关闭所有设备
260   - for (int l = 0; l < fields.length; l++) {
261   - String fieldId = fields[l];
262   - //获取红外遥控器
263   - List<Wl_Attendace> clintIds = deviceDao.getHWAttsWithRoomId(fieldId);
264   - closeAllDevice(clintIds);
265   - }
  157 + //截至时间
  158 + String endDateStr = DateFormatUtil.dateFormatString(new Date(),"yyyy-MM-dd");
  159 + endDateStr = endDateStr +" "+ endTime;
  160 + Date endDate = DateFormatUtil.stringFormatDate(endDateStr,"yyyy-MM-dd HH:mm");
  161 + long endDateTime = endDate.getTime();
  162 + //截至时间后2分钟
  163 + Date after2Date = getDateByTime(3,endDate);
  164 + long after2DateTime = after2Date.getTime();
  165 + //当前时间
  166 + Date now = new Date();
  167 + String nowStr = DateFormatUtil.dateFormatString(now,"yyyy-MM-dd HH:mm");
  168 + Date dateNow = DateFormatUtil.stringFormatDate(nowStr,"yyyy-MM-dd HH:mm");
  169 + long nowTime = dateNow.getTime();
  170 + //当前时间等于结束时间触发一次指令
  171 + if (nowTime > endDateTime && nowTime < after2DateTime) {
  172 + log.info("计划结束---,开始执行关闭空调任务-----");
  173 + //关闭所有设备
  174 + for (int l = 0; l < fields.length; l++) {
  175 + String fieldId = fields[l];
  176 + //获取红外遥控器
  177 + List<Wl_Attendace> clintIds = deviceDao.getHWAttsWithRoomId(fieldId);
  178 + closeAllDevice(clintIds);
266 179 }
267   - } catch (ParseException e) {
268   - log.info("关闭设备异常: ",e);
269 180 }
270 181 }
271 182 }
272 183 }
273 184 }
274   - } catch (JSONException e) {
  185 + } catch (Exception e) {
275 186 log.info("关闭设备异常: ",e);
276 187 }
277 188 }
... ... @@ -279,13 +190,13 @@ public class SchduledTasks {
279 190 } catch (Exception e) {
280 191 log.info("关闭设备异常: ",e);
281 192 }
282   - isClose = false;//保证条件一直在执行
  193 + outClose = false;//保证条件一直在执行
283 194 }
284 195  
285 196 public static Date getDateByTime(int length, Date date){
286 197 Calendar cal = Calendar.getInstance();
287 198 cal.setTime(date);//设置起时间
288   - cal.add(Calendar.MINUTE,length);//把日期往后增加
  199 + cal.add(Calendar.MINUTE,length);//把日期往后增加3分钟
289 200 return cal.getTime();
290 201 }
291 202  
... ... @@ -338,7 +249,6 @@ public class SchduledTasks {
338 249 long starTime = startDate.getTime();
339 250 long indexTime = date.getTime();
340 251 long entTime = endDate.getTime();
341   -
342 252 if (indexTime < starTime || indexTime > entTime) {//在时间之外
343 253 //关闭所有设备
344 254 for (int l = 0; l < fields.length; l++) {
... ... @@ -362,6 +272,9 @@ public class SchduledTasks {
362 272 } else {
363 273 //获取红外遥控器
364 274 List<Wl_Attendace> clintIds = deviceDao.getHWIds();
  275 + if(!CollectionUtils.isEmpty(clintIds)){
  276 + return;
  277 + }
365 278 closeAllDevice(clintIds);
366 279 }
367 280 } catch (Exception e) {
... ... @@ -416,4 +329,101 @@ public class SchduledTasks {
416 329 }
417 330  
418 331  
  332 + /**
  333 + * 解析上报记录,判断空间是否是无人状态。
  334 + * 条件:第一次上报无人后,五分钟内没有上报有人记录,则为无人
  335 + * @param calDevContrl
  336 + * @param openTimeStr
  337 + * @return
  338 + */
  339 + private boolean convertDeviceRecord(CalDevContrl calDevContrl,String openTimeStr){
  340 + if(calDevContrl != null){
  341 + List<String> list = new ArrayList();
  342 + Map<String,Integer> map = new HashMap();
  343 + //温感设备ID
  344 + String tempDevId =calDevContrl.getDevid();
  345 + //温感关联的人感设备
  346 + String assDevice = calDevContrl.getAssDevice();
  347 + String[] assDeviceIds = assDevice.split(",");
  348 + for (int i = 0; i < assDeviceIds.length; i++) {
  349 + String deviceId = assDeviceIds[i];
  350 + //过滤温感设备ID
  351 + if (!deviceId.equals(tempDevId)) {
  352 + list.add(deviceId);
  353 + //获取
  354 + Wl_Attendace attendance = deviceDao.getAttendace(deviceId);
  355 + if(attendance==null){
  356 + log.warn("设备ID["+deviceId+"],未绑定。");
  357 + map.put(deviceId,1);
  358 + continue;
  359 + }
  360 + String deviceName = attendance.getName();
  361 + Integer isConnection = attendance.getIsConnection();
  362 + if(isConnection.intValue()!=1){
  363 + log.warn("设备["+deviceName+"],离线跳过。");
  364 + map.put(deviceId,1);
  365 + continue;
  366 + }
  367 + //取设备最近一次上报无人记录时间,
  368 + IotDevice front = deviceDao.getIotDeviceRecord(deviceId, "无人");
  369 + String noTime = front.getTime();
  370 + //比较两者时间先后,无人时间 < 空调开启时间,则取最近10分钟是否有人记录上报
  371 + if(DateFormatUtil.timeCompare(noTime,openTimeStr) == -1){
  372 + log.info("------- 最近无人时间小于空调开启时间 ------");
  373 +// String afterTime = getDateAddMinute(noTime,5);
  374 + log.info("{},最近无人时间:{},空调开启时间:{},10分钟后时间:{}",deviceName,noTime,openTimeStr,noTime);
  375 + IotDevice iotDevice = deviceDao.getIotDeviceAfterRecord(deviceId, "有人", noTime);
  376 + log.info("10分钟后有人,记录信息:{}",JSON.toJSON(iotDevice));
  377 + if(iotDevice == null){
  378 + map.put(deviceId,1);
  379 + }else{
  380 + map.put(deviceId,2);
  381 + }
  382 + }else{//比较两者时间先后,无人时间 > 空调开启时间,则取空调开启后最近的无人记录上报
  383 + log.info("------- 最近无人时间大于空调开启时间 ------");
  384 + log.info("{}, 开启时间:{}",deviceName, openTimeStr);
  385 + IotDevice iotDevice = deviceDao.getIotDeviceAfterRecord(deviceId, "无人", openTimeStr);
  386 + log.info("空调开启后,最近无人记录,记录信息:{}",JSON.toJSON(iotDevice));
  387 + if(iotDevice != null) {
  388 + noTime = iotDevice.getTime();
  389 +// String afterTime = getDateAddMinute(noTime, 5);
  390 + log.info("{},最近无人时间: {}",deviceName,noTime);
  391 + IotDevice after = deviceDao.getIotDeviceAfterRecord(deviceId, "有人", noTime);
  392 + log.info("10分钟后有人,记录信息:{}",JSON.toJSON(after));
  393 + if (after == null) {
  394 + map.put(deviceId, 1);
  395 + } else {
  396 + map.put(deviceId, 2);
  397 + }
  398 + }else {
  399 + map.put(deviceId, 2);
  400 + }
  401 + }
  402 + }
  403 + }
  404 + log.info("上报记录:"+JSON.toJSON(map));
  405 + //false 无人 true 有人,当前后人感都为无人下,返回true
  406 + if(!CollectionUtils.isEmpty(list) && !map.isEmpty()){
  407 + if(map.get(list.get(0)).intValue()== 1 && map.get(list.get(1)).intValue()==1){
  408 + return true;
  409 + }
  410 + }
  411 + }
  412 + return false;
  413 + }
  414 +
  415 + /**
  416 + * 日期叠加计算,以月为单位:整数
  417 + * @param date 起始时间
  418 + * @return
  419 + */
  420 + public static String getDateAddMinute(String date,int length) {
  421 + Date date1 = DateFormatUtil.stringFormatDate(date,"yyyy-MM-dd HH:mm:ss");
  422 + Calendar cal = Calendar.getInstance();
  423 + cal.setTime(date1);//设置起时间
  424 + cal.add(Calendar.MINUTE,length);
  425 + String strDate = DateFormatUtil.dateFormatString(cal.getTime(),"yyyy-MM-dd HH:mm:ss");
  426 + return strDate;
  427 + }
  428 +
419 429 }
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/utils/ControlUtils.java
1 1 package com.example.mypulsar.utils;
2 2  
3 3 import com.alibaba.fastjson.JSON;
4   -import com.example.mypulsar.bean.CalDevBeContrl;
5   -import com.example.mypulsar.bean.CalDevContrl;
6   -import com.example.mypulsar.bean.CloseClintBean;
7   -import com.example.mypulsar.bean.DeviceBean;
  4 +import com.alibaba.fastjson.TypeReference;
  5 +import com.example.mypulsar.bean.*;
8 6 import com.example.mypulsar.dao.DeviceDao;
9 7 import com.example.mypulsar.task.SchduledTasks;
10   -import io.micrometer.core.instrument.util.JsonUtils;
11 8 import lombok.extern.slf4j.Slf4j;
12 9 import org.springframework.boot.configurationprocessor.json.JSONArray;
13 10 import org.springframework.boot.configurationprocessor.json.JSONException;
14 11 import org.springframework.boot.configurationprocessor.json.JSONObject;
15 12 import org.springframework.util.CollectionUtils;
16 13 import org.springframework.util.StringUtils;
17   -import springfox.documentation.spring.web.json.Json;
18 14  
19 15 import java.text.ParseException;
20 16 import java.text.SimpleDateFormat;
21 17 import java.util.*;
  18 +import java.util.stream.Collectors;
22 19  
23 20 @Slf4j
24 21 public class ControlUtils {
... ... @@ -63,7 +60,7 @@ public class ControlUtils {
63 60 try {
64 61 //联动的时间范围,[{"Week":"5","StartTime":"14:02","EndTime":"14:06"}]
65 62 if(StringUtils.isEmpty(cal.getInUserTime())){
66   - log.info("人感控制设备ID: {},未设置联动计划。",cal.getDevid());
  63 + log.error("人感控制设备ID: {},未设置联动计划。",cal.getDevid());
67 64 continue;
68 65 }
69 66 String inUseTime =cal.getInUserTime();
... ... @@ -79,7 +76,6 @@ public class ControlUtils {
79 76 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
80 77 SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm:ss");
81 78 int indexWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
82   - log.info("周计划日:{},当前日期:周:{}",weekStr,indexWeek);
83 79 for (int j = 0; j < weeks.length; j++) {
84 80 int week = Integer.parseInt(weeks[j]);
85 81 if (week == indexWeek) {
... ... @@ -105,125 +101,61 @@ public class ControlUtils {
105 101 }
106 102 }
107 103 } catch (JSONException e) {
108   - e.printStackTrace();
  104 + log.error("解析联动计划失败。",e);
109 105 } catch (ParseException e) {
110   - e.printStackTrace();
  106 + log.error("解析联动计划失败。",e);
111 107 }
112 108 }
113 109 }
114 110  
115   - /**
116   - * @param cal 人体感应器
117   - * @param deviceBean 控制设备
118   - */
119 111 private void calCompre(CalDevContrl cal, DeviceBean deviceBean) {
120 112 if (deviceBean.getValue().equals("无人")){
121 113 return;
122 114 }
123   - log.info("温度是否达标:,关联设备:{}",cal.getAssDevice());
124   -// //温度符合要求
125   - boolean isok = false;
126   - //人感设备对应的关联联动设备
  115 + Integer tempear = null;
127 116 if (!StringUtils.isEmpty(cal.getAssDevice())) {
128 117 String[] devices = cal.getAssDevice().split(",");
129 118 for (int i = 0; i < devices.length; i++) {
130 119 String device = devices[i];
131   - //取出湿度传感器设备
132   - List<CalDevContrl> calDevContrls = deviceDao.getTempCalWitdDevid(device);
133   - if(!CollectionUtils.isEmpty(calDevContrls)){
134   - for (int j = 0; j < calDevContrls.size(); j++) {
135   - String tempDevId = calDevContrls.get(j).getDevid();
136   - if (tempDevices.containsKey(tempDevId)) {
137   - //温度记录了
138   - int tempear = tempDevices.get(tempDevId);
139   - log.info("有温度记录:"+tempear);
140   - isok = tempIsOk(tempear, calDevContrls.get(j));
141   - } else {
142   - //沒有记录溫度按照28度比较
143   - log.info("有温度记录:28");
144   - isok =tempIsOk(28, calDevContrls.get(j));
145   - }
146   - }
147   - }
148   - }
149   - if(isok){
150   - startDevice(cal,deviceBean.getValue());
151   - }
152   - }
153   - }
154   -
155   - /**
156   - * 计算温度是否达标
157   - *
158   - * @param assDevice
159   - */
160   - private synchronized boolean calTemp(String assDevice) {
161   - if (!StringUtils.isEmpty(assDevice)) {
162   - String[] devices = assDevice.split(",");
163   - for (int i = 0; i < devices.length; i++) {
164   - String device = devices[i];
165   - //湿度传感器设备
166 120 List<CalDevContrl> calDevContrls = deviceDao.getTempCalWitdDevid(device);
167   - if(!CollectionUtils.isEmpty(calDevContrls)){
168   - for (int j = 0; j < calDevContrls.size(); j++) {
169   - String tempDevId = calDevContrls.get(j).getDevid();
170   - if (tempDevices.containsKey(tempDevId)) {
171   - //温度记录了
172   - int tempear = tempDevices.get(tempDevId);
173   - log.info("有温度记录:"+tempear);
174   - return tempIsOk(tempear, calDevContrls.get(j));
175   - } else {
176   - //沒有记录溫度按照28度比较
177   - log.info("有温度记录:28");
178   - return tempIsOk(28, calDevContrls.get(j));
179   - }
  121 + if (!CollectionUtils.isEmpty(calDevContrls)) {
  122 + String tempDevId = calDevContrls.get(0).getDevid();
  123 + tempear = deviceDao.getDeviceTemp(tempDevId);
  124 + if (tempear != null) {
  125 + tempIsOk(tempear.intValue(), calDevContrls.get(0), cal);
  126 + log.info("有温度上报记录:{}", tempear);
  127 + return;
  128 + }else{
  129 + log.info("无温度上报记录:按默认26");
  130 + tempIsOk(26, calDevContrls.get(0), cal);
  131 + return;
180 132 }
181 133 }
182 134 }
183 135 }
184   - return false;
185 136 }
186 137  
187 138 /**
188   - * 温度比较
  139 + * 温度比较, 根据条件开启
189 140 * @param tempear
190   - * @param calDevContrl
  141 + * @param tempDev
191 142 * @return
192 143 */
193   - private boolean tempIsOk(int tempear, CalDevContrl calDevContrl) {
194   - log.info("温度比较,温感设备信息:"+ JSON.toJSONString(calDevContrl));
195   - if(calDevContrl.getComPre() ==1){
196   - if (tempear <= Integer.parseInt(calDevContrl.getThreValue())) {
197   - log.info("温度比较: 实际温度:{},小于制热临界值:{},准备开启制热。",tempear,calDevContrl.getThreValue());
198   - return true;
  144 + private void tempIsOk(int tempear, CalDevContrl tempDev,CalDevContrl preDev) {
  145 + //制热 comPre=1 ;threValue 临界值
  146 + if(tempDev.getComPre() ==1){
  147 + if (tempear <= Integer.parseInt(tempDev.getThreValue())) {
  148 + log.info("温度比较: 实际温度:{}, 小于制热临界值:{}, 准备开启制热。",tempear,tempDev.getThreValue());
  149 + startDevice(preDev,1);
199 150 }
200 151 }
201   - if(calDevContrl.getComPre() ==3){
202   - if (tempear >= Integer.parseInt(calDevContrl.getThreValue())) {
203   - log.info("温度比较: 实际温度:{},大于制制冷临界值:{},准备开启制冷。",tempear,calDevContrl.getThreValue());
204   - return true;
  152 + //制冷 comPre=3 threValue 临界值
  153 + if(tempDev.getComPre() ==3){
  154 + if (tempear >= Integer.parseInt(tempDev.getThreValue())) {
  155 + log.info("温度比较: 实际温度:{}, 大于制制冷临界值:{},准备开启制冷。",tempear,tempDev.getThreValue());
  156 + startDevice(preDev,0);
205 157 }
206 158 }
207   - return false;
208   - }
209   -
210   - /**
211   - * 移除设备
212   - *
213   - * @param calDevContrl
214   - */
215   - private void removeSet(CalDevContrl calDevContrl) {
216   -
217   - Iterator<CalDevContrl> calDevContrlIterator = deviceSet.iterator();
218   -
219   - while (calDevContrlIterator.hasNext()) {
220   -
221   - CalDevContrl calDevContrl1 = calDevContrlIterator.next();
222   - if (calDevContrl1.getDevid().equals(calDevContrl.getDevid()) && calDevContrl.getComPre() == calDevContrl1.getComPre()) {
223   - calDevContrlIterator.remove();
224   - }
225   - }
226   -
227 159 }
228 160  
229 161 /**
... ... @@ -231,8 +163,7 @@ public class ControlUtils {
231 163 *
232 164 * @param cal
233 165 */
234   - private void startDevice(CalDevContrl cal,String value) {
235   -// deviceSet.add(cal);
  166 + private void startDevice(CalDevContrl cal,int flag) {
236 167 //设备类型 10:人感;9温感
237 168 int modelType = cal.getModelType();
238 169 //人感设备关联的遥控设备
... ... @@ -240,56 +171,161 @@ public class ControlUtils {
240 171 //取得红外遥控设备
241 172 List<CalDevBeContrl> calDevBeContrlList = getCalDevBeCon(assDevice);
242 173 if(CollectionUtils.isEmpty(calDevBeContrlList)){
243   - log.info("未查询到控制红外遥控器");
  174 + log.error("未查询到控制红外遥控器");
244 175 return;
245 176 }
  177 + //遍历红外遥控
246 178 for (int i = 0; i < calDevBeContrlList.size(); i++) {
247 179 CalDevBeContrl calDevBeContrl = calDevBeContrlList.get(i);
248 180 //红外遥控设备id
249 181 String conDevId = calDevBeContrl.getConDevId();
250 182 String conCodes = calDevBeContrl.getConCode();
  183 + //获取有效的模板
  184 + List<Wl_LinkModeBean> linkModes = deviceDao.getWL_LinkMode("1085");
  185 + Wl_LinkModeBean wl_linkModeBean = null;
  186 + if(!CollectionUtils.isEmpty(linkModes)){
  187 + wl_linkModeBean= linkModes.get(0);
  188 + }
251 189 //遥控设备,执行命令
252   - String conValue = calDevBeContrl.getConValue();
253   - String[] conCodeStr = conCodes.split(",");
  190 + String conValue = convert(wl_linkModeBean,calDevBeContrl.getConValue(),flag);
  191 + if(StringUtils.isEmpty(conValue)){
  192 + log.error("开启指令解析失败");
  193 + return;
  194 + }
254 195 //设备名
255 196 String name = deviceDao.getWLName(conDevId);
256   - for (int j = 0; j < conCodeStr.length; j++) {
257   - String conCode = conCodeStr[j];
258   - switch (modelType) {
259   - case 1://开关
260   - HttpUtil.controlDev(conDevId, conCode, conValue);
261   - break;
262   - case 2://插座
263   - HttpUtil.controlDev(conDevId, conCode, conValue);
264   - break;
265   - case 3://排插
266   - HttpUtil.controlDev(conDevId, conCode, conValue);
267   - break;
268   - case 9://温度传感器
269   - break;
270   - case 10://人体感应触发空调开关
271   - log.info("设备类型:"+modelType+"设备集合"+ JSON.toJSONString(SchduledTasks.conDevMap));
272   - if (!SchduledTasks.conDevMap.containsKey(conDevId)&&SchduledTasks.conDevMap.get(conDevId)==null) {
273   - //设备已开,更新时间
274   - SchduledTasks.conDevMap.put(conDevId,new Date());
275   - HttpUtil.addAirCode(conValue, conDevId, name);
276   - }
277   - case 12://万能遥控器
278   - log.info("设备类型:"+modelType);
279   - if (!SchduledTasks.conDevMap.containsKey(conDevId)&&SchduledTasks.conDevMap.get(conDevId)==null) {//设备已开,更新时间
280   - SchduledTasks.conDevMap.put(conDevId,new Date());
281   - HttpUtil.addAirCode(conValue, conDevId, name);
282   - }
283   - break;
  197 + switch (modelType) {
  198 + case 1://开关
  199 + sendDeviceCommand(conCodes,conDevId,conValue);
  200 + break;
  201 + case 2://插座
  202 + sendDeviceCommand(conCodes,conDevId,conValue);
  203 + break;
  204 + case 3://排插
  205 + sendDeviceCommand(conCodes,conDevId,conValue);
  206 + break;
  207 + case 9://温度传感器
  208 + break;
  209 + case 10://人体感应触发空调开关
  210 + log.info("设备类型:"+modelType+"设备集合"+ JSON.toJSONString(SchduledTasks.conDevMap));
  211 + if (!SchduledTasks.conDevMap.containsKey(conDevId) && SchduledTasks.conDevMap.get(conDevId)==null) {
  212 + //设备已开,更新时间
  213 + SchduledTasks.conDevMap.put(conDevId,new Date());
  214 + HttpUtil.addAirCode(conValue, conDevId, name);
  215 + }
  216 + case 12://万能遥控器
  217 + log.info("设备类型:"+modelType);
  218 + if (!SchduledTasks.conDevMap.containsKey(conDevId) && SchduledTasks.conDevMap.get(conDevId)==null) {
  219 + SchduledTasks.conDevMap.put(conDevId,new Date());
  220 + HttpUtil.addAirCode(conValue, conDevId, name);
  221 + }
  222 + break;
  223 + }
  224 +
  225 + }
  226 + }
  227 +
  228 + /***
  229 + * @Description: 发送 开关、插座、排插命令
  230 + * @Param: [conCodes, conDevId, conValue]
  231 + * @return: void
  232 + * @Author: xuquan
  233 + * @Date: 2021/4/28
  234 + */
  235 + private void sendDeviceCommand(String conCodes,String conDevId,String conValue){
  236 + String[] conCodeStr = conCodes.split(",");
  237 + for (int j = 0; j < conCodeStr.length; j++) {
  238 + String conCode = conCodeStr[j];
  239 + HttpUtil.controlDev(conDevId, conCode, conValue);
  240 + }
  241 + }
  242 +
  243 + private static String convert(Wl_LinkModeBean wl_linkModeBean,String conValue,int flag){
  244 + if(wl_linkModeBean == null){
  245 + return "";
  246 + }
  247 + TuYaAirCondition condition = JsonUtils.nonDefaultMapper().fromJson(conValue,TuYaAirCondition.class);
  248 + //联动设置
  249 + String para = wl_linkModeBean.getDevicePara();
  250 + List<AirLinkMode> list = JSON.parseObject(para, new TypeReference<List<AirLinkMode>>(){});
  251 + if(CollectionUtils.isEmpty(list)) {
  252 + return "";
  253 + }
  254 + for(AirLinkMode mode : list){
  255 + if(mode.getDeviceType().intValue() ==9){
  256 + List<AirCommand> commands = mode.getKVs();
  257 + for(AirCommand command : commands){
  258 + int key = command.getKey().intValue();
  259 + String temp = command.getValue();
  260 + //制冷
  261 + if(flag ==0 && key == flag){
  262 + condition.setMode("0");
  263 + condition.setPower("1");
  264 + condition.setTemp(temp);
  265 + condition.setWind("0");
  266 + continue;
  267 + }
  268 + //制热
  269 + if(flag ==1 && key == flag){
  270 + condition.setMode("1");
  271 + condition.setPower("1");
  272 + //制热默认22
  273 + condition.setTemp("22");
  274 + condition.setWind("0");
  275 + }
284 276 }
285 277 }
286 278 }
  279 + return JsonUtils.nonDefaultMapper().toJson(condition);
287 280 }
288 281  
289   - /**
290   - * 获取红外遥控控制的设备
291   - * @param assDevice
292   - * @return
  282 +// private static String convert2(String s,String conValue,int flag) throws JSONException {
  283 +// TuYaAirCondition condition = JsonUtils.nonDefaultMapper().fromJson(conValue,TuYaAirCondition.class);
  284 +// List<AirLinkMode> list = JSON.parseObject(s, new TypeReference<List<AirLinkMode>>(){});
  285 +// if(CollectionUtils.isEmpty(list)) {
  286 +// return "";
  287 +// }
  288 +// for(AirLinkMode mode : list){
  289 +// if(mode.getDeviceType().intValue() ==9){
  290 +// List<AirCommand> commands = mode.getKVs();
  291 +// for(AirCommand command : commands){
  292 +// int key = command.getKey().intValue();
  293 +// String temp = command.getValue();
  294 +// //制冷
  295 +// if(flag ==0 && key == flag){
  296 +// condition.setMode("1");
  297 +// condition.setPower("1");
  298 +// condition.setTemp(temp);
  299 +// condition.setWind("0");
  300 +// continue;
  301 +// }
  302 +// //制热
  303 +// if(flag ==1 && key == flag){
  304 +// condition.setMode("2");
  305 +// condition.setPower("1");
  306 +// condition.setTemp(temp);
  307 +// condition.setWind("0");
  308 +// }
  309 +// }
  310 +// }
  311 +// }
  312 +// return JsonUtils.nonDefaultMapper().toJson(condition);
  313 +// }
  314 +//
  315 +// public static void main(String[] args) throws JSONException {
  316 +// String s = "[{\"DeviceId\":1,\"DeviceName\":\"温湿度传感器\",\"DeviceType\":9,\"KVs\":[{\"Key\":0,\"Value\":\"20\",\"Text\":\"空调制冷开\"},{\"Key\":1,\"Value\":\"12\",\"Text\":\"空调制热开\"}]},{\"DeviceId\":2,\"DeviceName\":\"人体感应器\",\"DeviceType\":10,\"KVs\":[{\"Key\":0,\"Value\":\"有人\",\"Text\":\"空调开\"},{\"Key\":1,\"Value\":\"无人\",\"Text\":\"空调关\"}]}]";
  317 +// List<AirLinkMode> list = JsonUtils.nonDefaultMapper().fromJson(s,List.class);
  318 +// String s2 = "{\"mode\":2,\"power\":\"1\",\"remote_id\":\"6c1556ca44a7e5d2336nrr\",\"remote_index\":\"11272\",\"temp\":\"26\",\"wind\":\"0\"}";
  319 +// //获取有效的模板
  320 +// System.out.println(convert2(s,s2,1));
  321 +// }
  322 +
  323 + /***
  324 + * @Description: 获取红外设备含多台
  325 + * @Param: [assDevice]
  326 + * @return: java.util.List<com.example.mypulsar.bean.CalDevBeContrl>
  327 + * @Author: xuquan
  328 + * @Date: 2021/4/28
293 329 */
294 330 private List<CalDevBeContrl> getCalDevBeCon(String assDevice) {
295 331 String[] assDeviceIds = assDevice.split(",");
... ... @@ -315,7 +351,13 @@ public class ControlUtils {
315 351 }
316 352 }
317 353 }
318   - return devBeContrlList;
  354 + if(devBeContrlList.size()>0){
  355 + //去重重复数据
  356 + List<CalDevBeContrl> hwList = devBeContrlList.stream().collect(Collectors.collectingAndThen(
  357 + Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(CalDevBeContrl::getConDevId))), ArrayList::new));
  358 + return hwList;
  359 + }
  360 + return null;
319 361 }
320 362  
321 363  
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/utils/DateFormatUtil.java 0 → 100644
... ... @@ -0,0 +1,168 @@
  1 +package com.example.mypulsar.utils;
  2 +
  3 +
  4 +import java.text.ParseException;
  5 +import java.text.SimpleDateFormat;
  6 +import java.util.Calendar;
  7 +import java.util.Date;
  8 +
  9 +/**
  10 + * 日期公共计算工具类
  11 + * @Author: xuquan
  12 + * @Date: 2020/8/2 10:24
  13 + */
  14 +public class DateFormatUtil {
  15 +
  16 + private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  17 +
  18 +
  19 + /**
  20 + * 日期叠加计算,以年为单位:整数
  21 + * @param length 叠加长度
  22 + * @param date 起始时间
  23 + * @return
  24 + */
  25 + public static Date getDateByYearLength(int length, Date date) {
  26 + Calendar cal = Calendar.getInstance();
  27 + cal.setTime(date);//设置起时间
  28 + cal.add(Calendar.YEAR, length);//增加一年
  29 + return cal.getTime();
  30 + }
  31 +
  32 + /**
  33 + * 日期叠加计算,以月为单位:整数
  34 + * @param length 叠加长度
  35 + * @param date 起始时间
  36 + * @return
  37 + */
  38 + public static Date getDateByMonthLength(int length, Date date) {
  39 + Calendar cal = Calendar.getInstance();
  40 + cal.setTime(date);//设置起时间
  41 + cal.add(Calendar.MONTH, length);//增加一月
  42 + return cal.getTime();
  43 + }
  44 +
  45 + /**
  46 + * 日期叠加计算,以月为单位:整数
  47 + * @param date 起始时间
  48 + * @return
  49 + */
  50 + public static String getDateAdd(String date) {
  51 + Date date1 = stringFormatDate(date,"yyyy-MM-dd HH:mm:ss");
  52 + Calendar cal = Calendar.getInstance();
  53 + cal.setTime(date1);//设置起时间
  54 + cal.add(Calendar.SECOND, 2);
  55 + String strDate = dateFormatString(cal.getTime(),"yyyy-MM-dd HH:mm:ss");
  56 + return strDate;
  57 + }
  58 +
  59 + /**
  60 + * 日期叠加计算,以天为单位:整数
  61 + * @param date 起始时间
  62 + * @return
  63 + */
  64 + public static String getDateByTime(String date){
  65 + Date date1 = stringFormatDate(date,"yyyy-MM-dd HH:mm:ss");
  66 + Calendar cal = Calendar.getInstance();
  67 + cal.setTime(date1);//设置起时间
  68 + cal.add(Calendar.DATE,1);//把日期往后增加
  69 + String strDate = dateFormatString(cal.getTime(),"yyyy-MM-dd HH:mm:ss");
  70 + return strDate;
  71 + }
  72 +
  73 + /**
  74 + * Date转 String : 指定格式:pattern
  75 + * @param date
  76 + * @param pattern
  77 + * @return
  78 + */
  79 + public static String dateFormatString(Date date,String pattern) {
  80 + SimpleDateFormat df = null;
  81 + String returnValue = null;
  82 + if (date != null) {
  83 + df = new SimpleDateFormat(pattern);
  84 + returnValue = df.format(date);
  85 + }
  86 + return returnValue;
  87 + }
  88 +
  89 + /**
  90 + * String 转 Date : 指定格式:pattern
  91 + * @param date
  92 + * @param pattern
  93 + * @return
  94 + */
  95 + public static Date stringFormatDate(String date, String pattern) {
  96 + SimpleDateFormat simpleDateFormat = null;
  97 + if (date != null) {
  98 + try{
  99 + simpleDateFormat = new SimpleDateFormat(pattern);
  100 + Date retultDate = simpleDateFormat.parse(date);
  101 + return retultDate;
  102 + }catch (ParseException e){
  103 + e.printStackTrace();
  104 + }
  105 + }
  106 + return null;
  107 + }
  108 +
  109 + /**
  110 + * 输出yyyy年MM月dd日 格式时间
  111 + * @param date
  112 + * @param pattern
  113 + * @return
  114 + */
  115 + public static String formatDate(String date, String pattern) {
  116 + SimpleDateFormat sf = new SimpleDateFormat(pattern);
  117 + String resultTime ="";
  118 + try{
  119 + Date datetime = simpleDateFormat.parse(date);
  120 + resultTime = sf.format(datetime);
  121 + }catch (ParseException e){
  122 + e.printStackTrace();
  123 + }
  124 + return resultTime;
  125 + }
  126 + /**
  127 + * 比较两时间大小
  128 + * @param t1
  129 + * @param t2
  130 + * @return
  131 + */
  132 + public static int timeCompare(String t1,String t2){
  133 + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
  134 + Calendar c1=Calendar.getInstance();
  135 + Calendar c2=Calendar.getInstance();
  136 + try {
  137 + c1.setTime(formatter.parse(t1));
  138 + c2.setTime(formatter.parse(t2));
  139 + } catch (ParseException e) {
  140 + e.printStackTrace();
  141 + }
  142 + int result=c1.compareTo(c2);
  143 + return result;
  144 + }
  145 +
  146 + private static byte[] lock = new byte[0];
  147 + // 位数,默认是8位
  148 + private final static long strNum = 100000000;
  149 +
  150 + /**
  151 + * 创建以时间为唯一UUID
  152 + * @return
  153 + */
  154 + public static String createTimeId() {
  155 + long r = 0;
  156 + synchronized (lock) {
  157 + r = (long) ((Math.random() + 1) * strNum);
  158 + }
  159 + return System.currentTimeMillis() + String.valueOf(r).substring(1);
  160 + }
  161 +
  162 + public static void main(String[] args) {
  163 + System.out.println(timeCompare("2021-04-29 23:32","2021-04-29 23:38"));
  164 + System.out.println(timeCompare("2021-04-29 23:38","2021-04-29 23:38"));
  165 + System.out.println(timeCompare("2021-04-29 23:42","2021-04-29 23:38"));
  166 + }
  167 +
  168 +}
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/utils/HttpUtil.java
... ... @@ -7,22 +7,17 @@ import com.example.mypulsar.task.SchduledTasks;
7 7 import lombok.extern.slf4j.Slf4j;
8 8 import org.springframework.boot.configurationprocessor.json.JSONException;
9 9 import org.springframework.boot.configurationprocessor.json.JSONObject;
10   -import org.springframework.core.io.FileSystemResource;
11   -import org.springframework.http.HttpEntity;
12   -import org.springframework.http.HttpHeaders;
13   -import org.springframework.http.MediaType;
14   -import org.springframework.http.ResponseEntity;
15   -import org.springframework.util.LinkedMultiValueMap;
16   -import org.springframework.util.MultiValueMap;
17 10 import org.springframework.web.client.RestTemplate;
18 11  
19 12 import java.io.BufferedReader;
20 13 import java.io.DataOutputStream;
21   -import java.io.File;
22 14 import java.io.InputStreamReader;
23 15 import java.net.HttpURLConnection;
24 16 import java.net.URL;
25   -import java.util.*;
  17 +import java.util.ArrayList;
  18 +import java.util.HashMap;
  19 +import java.util.List;
  20 +import java.util.Map;
26 21  
27 22 /**
28 23 * http 工具类
... ... @@ -141,24 +136,33 @@ public class HttpUtil {
141 136 * @param conDevName 设备名称
142 137 * @return
143 138 */
144   - public static String addAirCode(String conValue, String conDevId, String conDevName) {
  139 + public static String addAirCode(String conValue, String conDevId, String conDevName) {
145 140 try {
146 141 String url = String.format("http://120.26.116.253:5555/TuYa/%s/addAirCode", conDevId);
147   -// String url = String.format("http://120.26.116.253:5555/TuYa/%s/airConditioners", conDevId);
148 142 RestTemplate restTemplate = new RestTemplate();
149 143 TuYaAirCondition tuYaAirCondition = new TuYaAirCondition();
150   - String power = "";
151 144 try {
152 145 JSONObject jsonObject = new JSONObject(conValue);
153   - power = jsonObject.getString("power");
154   - tuYaAirCondition.setMode(jsonObject.getString("mode"));
  146 + //开关
  147 + String power = jsonObject.getString("power");
  148 + //模式
  149 + String mode = jsonObject.getString("mode");
  150 + //风速
  151 + String wind = jsonObject.getString("wind");
  152 + //温度
  153 + String temp = jsonObject.getString("temp");
  154 + //遥控器码值
  155 + String remoteIndex =jsonObject.getString("remote_index");
  156 + //遥控器ID
  157 + String remoteId = jsonObject.getString("remote_id");
  158 + tuYaAirCondition.setMode(mode);
155 159 tuYaAirCondition.setPower(power);
156   - tuYaAirCondition.setRemote_id(jsonObject.getString("remote_id"));
157   - tuYaAirCondition.setWind(jsonObject.getString("wind"));
158   - tuYaAirCondition.setTemp(jsonObject.getString("temp"));
159   - tuYaAirCondition.setRemote_index(jsonObject.getString("remote_index"));
  160 + tuYaAirCondition.setRemote_id(remoteId);
  161 + tuYaAirCondition.setWind(wind);
  162 + tuYaAirCondition.setTemp(temp);
  163 + tuYaAirCondition.setRemote_index(remoteIndex);
160 164 } catch (JSONException e) {
161   - log.info("控制失败:失败信息",e);
  165 + log.error("解析指令失败:",e);
162 166 }
163 167 log.info("开始发送遥控命令至设备:请求地址: {},设备名称: {},设备ID: {},请求参数: {}",url,conDevName,conDevId,tuYaAirCondition.toString());
164 168 //轮询2次下发指令,防止偶然性第一次没启动
... ... @@ -170,11 +174,11 @@ public class HttpUtil {
170 174 log.info("返回结果:{}",response);
171 175 return response;
172 176 }catch (Exception e){
173   - log.info("请求红外控制指令失败:{}",e);
  177 + log.error("请求红外控制指令失败:{}",e);
174 178 if(SchduledTasks.conDevMap.containsKey(conDevId)){
175 179 SchduledTasks.conDevMap.remove(conDevId);
176 180 }
177 181 }
178   - return null;
  182 + return "";
179 183 }
180 184 }
... ...
cloud/mypulsar/src/main/resources/iotmapper/iotmapper.xml
... ... @@ -7,7 +7,7 @@
7 7 </select>-->
8 8 <select id="getHWAttsWithRoomId" parameterType="java.lang.String"
9 9 resultType="com.example.mypulsar.bean.Wl_Attendace">
10   - select clint_id from WL_Attendance where belongAreaId = ${belongAreaId} and state = 1 and clint_type = 12;
  10 + select clint_id from WL_Attendance where belongAreaId = ${belongAreaId} and state = 1 and clint_type = 12
11 11 </select>
12 12  
13 13 <!-- 插入遥控器数据-->
... ... @@ -20,16 +20,25 @@
20 20 <!-- 新建联动模板-->
21 21  
22 22 <select id="getWL_LinkMode" parameterType="java.lang.String" resultType="com.example.mypulsar.bean.Wl_LinkModeBean">
23   - select * from WL_LinkageMode where SchoolId = ${SchoolId} and State = 1;
  23 + select * from WL_LinkageMode where SchoolId = ${SchoolId} and State = 1
  24 + </select>
  25 +
  26 + <select id="getIotDeviceAfterRecord" resultType="com.example.mypulsar.bean.IotDevice">
  27 + select top 1 devId,code,value,time,dev_status from Iot_Device where devId = #{devId} and [value] = #{value}
  28 + and dev_status = 1 and code = 'pir' and time > #{openTime} ORDER BY time asc
24 29 </select>
25 30  
26 31 <select id="getIotDeviceRecord" resultType="com.example.mypulsar.bean.IotDevice">
27 32 select top 1 devId,code,value,time,dev_status from Iot_Device where devId = #{devId} and [value] = #{value}
28   - and dev_status = 1 and code = 'pir' and dev_type =3 and time > #{openTime} ORDER BY time asc
  33 + and dev_status = 1 and code = 'pir' ORDER BY time desc
29 34 </select>
30 35  
31 36 <select id="getHWAtts" parameterType="java.lang.String" resultType="com.example.mypulsar.bean.Wl_Attendace">
32   - select * from WL_Attendance where state = 1 and clint_type = 12 and school_id = 1085;
  37 + select * from WL_Attendance where state = 1 and clint_type = 12 and school_id = 1085
  38 + </select>
  39 +
  40 + <select id="getTempDevice" parameterType="java.lang.String" resultType="com.example.mypulsar.bean.CalDevContrl">
  41 + select top 1 * from CalDevContrl where Status = 1 and ModelType = 9 and DevBeId like '%'+ #{beId} +'%' order by CreateTime desc
33 42 </select>
34 43  
35 44 </mapper>
36 45 \ No newline at end of file
... ...