diff --git a/cloud/mypulsar/src/main/java/com/example/mypulsar/MyRunnerableInt.java b/cloud/mypulsar/src/main/java/com/example/mypulsar/MyRunnerableInt.java index 6e7ae4d..43d592c 100644 --- a/cloud/mypulsar/src/main/java/com/example/mypulsar/MyRunnerableInt.java +++ b/cloud/mypulsar/src/main/java/com/example/mypulsar/MyRunnerableInt.java @@ -8,7 +8,6 @@ import com.example.mypulsar.mq.AESBase64Utils; import com.example.mypulsar.mq.MessageHandlerTask; import com.example.mypulsar.mq.MqConsumer; import com.example.mypulsar.mqtt.MqttConsumer; -import com.example.mypulsar.task.SchduledTasks; import com.example.mypulsar.utils.ControlUtils; import com.example.mypulsar.utils.JsonUtils; import com.example.mypulsar.utils.PulsarConsumerPoolFactory; @@ -25,7 +24,6 @@ import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.function.BiConsumer; @@ -76,7 +74,7 @@ public class MyRunnerableInt implements ApplicationRunner { //通过 AES (ECB 模式)对 accessKey 的中间 16 位代码进行解密 String data = AESBase64Utils.decrypt(vo.getData(), accessKey.substring(8, 24)); TuYaReceiverBean tuYaReceiverBean = JSON.parseObject(data, TuYaReceiverBean.class); - log.info("接收消息,消息内容:"+ JSON.toJSONString(tuYaReceiverBean)); +// log.info("接收消息,消息内容:"+ JSON.toJSONString(tuYaReceiverBean)); //处理数据上报 detealData(tuYaReceiverBean); }); @@ -128,8 +126,7 @@ public class MyRunnerableInt implements ApplicationRunner { log.info("设备: {},已掉线",deviceName); deviceBean.setDev_type(1); deviceBean.setDev_status(0); - updateDevStatus(deviceBean); - updateStatus(devId, "0"); + updateStatus(deviceBean.getDevId(), "0"); break; case "online"://设备上线 log.info("设备: {},已上线",deviceName); @@ -155,13 +152,7 @@ public class MyRunnerableInt implements ApplicationRunner { } private void updateStatus(String devId, String isConnection) { -// String clintId = deviceDao.selectAttendance(devId); -// if (clintId.equals(devId)) { deviceDao.updateStatus(devId, isConnection, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); -// } else { -// deviceDao.insert(devId, "-2", isConnection); -// } - } private void updateDevStatus(DeviceBean deviceBean) { @@ -173,8 +164,7 @@ public class MyRunnerableInt implements ApplicationRunner { if (deviceBean.getDataId() == null) deviceBean.setDataId(""); if (deviceBean.getCode() == null) deviceBean.setCode(""); if (deviceBean.getProductKey() == null) deviceBean.setProductKey(""); - log("设备上线,更新历史记录: "+JSON.toJSONString(deviceBean)); - int index = deviceDao.addDevice(deviceBean.getDataId(), deviceBean.getDevId(), deviceBean.getProductKey(), deviceBean.getCode(), deviceBean.getValue() + deviceDao.addDevice(deviceBean.getDataId(), deviceBean.getDevId(), deviceBean.getProductKey(), deviceBean.getCode(), deviceBean.getValue() , deviceBean.getBizCode(), deviceBean.getTime(), deviceBean.getName(), deviceBean.getDpId(), deviceBean.getUid(), deviceBean.getDev_status(), deviceBean.getDev_type()); updateStatus(deviceBean.getDevId(), "1"); } @@ -206,16 +196,11 @@ public class MyRunnerableInt implements ApplicationRunner { switch (code) { case "va_temperature"://温度 deviceBean.setValue(Integer.parseInt(value) / 100 + ""); - log.info("温度:" + deviceBean.getValue()); - //存储 对应设备对应的温度记录 - ControlUtils.tempDevices.put(deviceBean.getDevId(),Integer.valueOf(deviceBean.getValue())); break; case "va_humidity"://湿度 deviceBean.setValue(Integer.parseInt(value) / 100 + ""); - log.info("湿度:" + deviceBean.getValue()); break; case "pir"://人体感应 - log.info("人体感应:" + value); if(value.equals("pir")){ deviceBean.setValue("有人"); }else{ @@ -232,18 +217,18 @@ public class MyRunnerableInt implements ApplicationRunner { case "battery_value"://电池电量值 break; case "battery"://门磁电池电量值 - log("电池电量值:" + value); + log.info("电池电量值:" + value); break; case "battery_state"://电池电量状态 {“range”:[“low”,“middle”,“high”]} - log("电池电量状态:" + value); + log.info("电池电量状态:" + value); break; case "doorcontact_state"://门磁状态 value = (value.equals("true") ? "开门" : "关门"); - log("门磁状态:" + (value.equals("true") ? "开门" : "关门")); + log.info("门磁状态:" + (value.equals("true") ? "开门" : "关门")); break; case "switch"://门磁状态 value = (value.equals("true") ? "开门" : "关门"); - log("开关状态:" + (value.equals("true") ? "开门" : "关门")); + log.info("开关状态:" + (value.equals("true") ? "开门" : "关门")); break; case "cur_voltage"://当前电压 if(value.equals("0")){ @@ -251,7 +236,7 @@ public class MyRunnerableInt implements ApplicationRunner { }else{ value = Integer.parseInt(value) / 10 + ""; } - log(deviceBean.getDevId() + " 当前电压:" + value +"v"); + log.info(deviceBean.getDevId() + " 当前电压:" + value +"v"); break; case "cur_power"://当前功率 if(value.equals("0")){ @@ -259,7 +244,7 @@ public class MyRunnerableInt implements ApplicationRunner { }else{ value = Integer.parseInt(value) / 10+""; } - log(deviceBean.getDevId() + " 当前功率:" + value + "W"); + log.info(deviceBean.getDevId() + " 当前功率:" + value + "W"); break; case "cur_current"://当前电流 if(value.equals("0")){ @@ -267,7 +252,7 @@ public class MyRunnerableInt implements ApplicationRunner { }else{ value = Integer.parseInt(value) / 10+""; } - log(deviceBean.getDevId() + " 当前电流:" + value + "A"); + log.info(deviceBean.getDevId() + " 当前电流:" + value + "A"); break; case "smoke_sensor_state"://烟雾报警 break; @@ -310,80 +295,9 @@ public class MyRunnerableInt implements ApplicationRunner { //获取人感设备控制 List calDevContrls = deviceDao.getCalDevContrlWidthDevId(deviceBean.getDevId()); if (CollectionUtils.isEmpty(calDevContrls)) { - log.info("未关联人感控制设备, 设备ID: "+ deviceBean.getDevId()); + log.error("未关联人感控制设备, 设备ID: "+ deviceBean.getDevId()); return; } ControlUtils.getInstance(deviceDao).conTemper(calDevContrls, deviceBean); } - - /** - * 设置更新上报时间 - * @param clintId - */ - private void setClintTime(String clintId){ - //获取人感设备 - List calDevContrls = deviceDao.getCalDevContrlWidthDevId(clintId); - if(!CollectionUtils.isEmpty(calDevContrls)){ - CalDevContrl calDevContrl = calDevContrls.get(0); - String assDevice = calDevContrl.getAssDevice(); - //获取人感关联红外遥控器 - CalDevBeContrl hwClint = getCalDevBeCon(assDevice); - if(hwClint==null) { - log.info("未查询到控制红外遥控器"); - return; - } - //红外遥控设备id - String conDevId = hwClint.getConDevId(); - SchduledTasks.closeDevMap.put(conDevId,new Date()); - } - } - - private CalDevBeContrl getCalDevBeCon(String assDevice) { - String[] assDeviceIds = assDevice.split(","); - List devBeContrlList = new ArrayList<>(); - for (int i = 0; i < assDeviceIds.length; i++) { - String deviceId = assDeviceIds[i]; - //获取温感设备 - List calDevContrl = deviceDao.getTempCalWitdDevid(deviceId); - if(!CollectionUtils.isEmpty(calDevContrl)){ - for (int j = 0; j < calDevContrl.size(); j++) { - //温感设备 - CalDevContrl calDevContrl1 = calDevContrl.get(j); - if (!calDevContrl1.getDevBeId().equals("0")) { - String[] calDevBeIdStr = calDevContrl1.getDevBeId().split(","); - if (calDevBeIdStr != null) { - for (int k = 0; k < calDevBeIdStr.length; k++) { - //红外设备 - CalDevBeContrl calDevBeContrl = deviceDao.getCalDevBeControl(calDevBeIdStr[k]); - devBeContrlList.add(calDevBeContrl); - } - } - } - } - } - } - if(devBeContrlList.size()>0){ - return devBeContrlList.get(0); - } - return null; - } - - /** - * 根据温度变化控制设备 - * - * @param deviceBean - */ - private void calOpenOrCloseDevWithtemper(DeviceBean deviceBean) { - - ControlUtils.tempDevices.put(deviceBean.getDevId(), Integer.valueOf(deviceBean.getValue())); - -// List calDevContrls = deviceDao.getCalDevContrlWidthDevId(deviceBean.getDevId()); -// if (null != calDevContrls && calDevContrls.size() > 0) -// ControlUtils.getInstance(deviceDao).conTemper(calDevContrls, deviceBean); - - } - - private void log(String content) { - System.out.println("接收到数据:" + content); - } } diff --git a/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/AirCommand.java b/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/AirCommand.java new file mode 100644 index 0000000..60955cc --- /dev/null +++ b/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/AirCommand.java @@ -0,0 +1,29 @@ +package com.example.mypulsar.bean; + +/** + * Created with IntelliJ IDEA. + * + * @Auther: xuquan + * @Date: 2021/04/28 17:15 + * @Description: + */ +public class AirCommand { + private Integer key; + private String value; + + public Integer getKey() { + return key; + } + + public void setKey(Integer key) { + this.key = key; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/AirLinkMode.java b/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/AirLinkMode.java new file mode 100644 index 0000000..018cf4b --- /dev/null +++ b/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/AirLinkMode.java @@ -0,0 +1,32 @@ +package com.example.mypulsar.bean; + +import java.util.List; + +/** + * Created with IntelliJ IDEA. + * + * @Auther: xuquan + * @Date: 2021/04/28 17:13 + * @Description: + */ +public class AirLinkMode { + + private Integer deviceType; + private List KVs; + + public Integer getDeviceType() { + return deviceType; + } + + public void setDeviceType(Integer deviceType) { + this.deviceType = deviceType; + } + + public List getKVs() { + return KVs; + } + + public void setKVs(List KVs) { + this.KVs = KVs; + } +} diff --git a/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/Wl_Attendace.java b/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/Wl_Attendace.java index 646237a..0bfc15e 100644 --- a/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/Wl_Attendace.java +++ b/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/Wl_Attendace.java @@ -1,7 +1,5 @@ package com.example.mypulsar.bean; -import afu.org.checkerframework.checker.igj.qual.I; - import java.io.Serializable; public class Wl_Attendace implements Serializable { @@ -12,6 +10,8 @@ public class Wl_Attendace implements Serializable { private String name; + private Integer isConnection; + public Integer getSchool_id() { return school_id; } @@ -36,6 +36,14 @@ public class Wl_Attendace implements Serializable { this.name = name; } + public Integer getIsConnection() { + return isConnection; + } + + public void setIsConnection(Integer isConnection) { + this.isConnection = isConnection; + } + @Override public String toString() { return "Wl_Attendace{" + diff --git a/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/Wl_LinkModeBean.java b/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/Wl_LinkModeBean.java index 34c3e05..b961196 100644 --- a/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/Wl_LinkModeBean.java +++ b/cloud/mypulsar/src/main/java/com/example/mypulsar/bean/Wl_LinkModeBean.java @@ -8,6 +8,7 @@ public class Wl_LinkModeBean { private String Field; + private String DevicePara; public String getModeName() { return ModeName; @@ -33,6 +34,14 @@ public class Wl_LinkModeBean { Field = field; } + public String getDevicePara() { + return DevicePara; + } + + public void setDevicePara(String devicePara) { + DevicePara = devicePara; + } + @Override public String toString() { return "Wl_LinkModeBean{" + diff --git a/cloud/mypulsar/src/main/java/com/example/mypulsar/dao/DeviceDao.java b/cloud/mypulsar/src/main/java/com/example/mypulsar/dao/DeviceDao.java index 763611e..d45e974 100644 --- a/cloud/mypulsar/src/main/java/com/example/mypulsar/dao/DeviceDao.java +++ b/cloud/mypulsar/src/main/java/com/example/mypulsar/dao/DeviceDao.java @@ -32,15 +32,8 @@ public interface DeviceDao { @Select("select * from Iot_Device where devId = #{devId}") List getDevice(@Param("devId") String devId); - //获取联动控制模板 - @Select("select * from CalDevContrl ") - List getCalDevContrl(); - - @Select("select top 1 * from WL_Attendance where clint_id = #{clint_id} and isConnection =1 and clint_type =10") - Wl_Attendace getAttendace(@Param("clint_id") String clint_id); - - @Select("select * from CalDevContrl where Devid = #{Devid} and ModelType =10 and Status = 1") - List getCalDevContrlWidthDevId(@Param("Devid") String devId); + @Select("select top 1 value from Iot_Device where devId = #{devId} and code = 'va_temperature' ORDER BY time desc") + Integer getDeviceTemp(@Param("devId") String devId); @Select("select * from CalDevBeContrl where State = 1 and Id = #{Id}") CalDevBeContrl getCalDevBeControl(@Param("Id") String Id); @@ -48,31 +41,41 @@ public interface DeviceDao { @Select("select top 1 * from CalDevBeContrl where State = 1 and ConDevId = #{devId}") CalDevBeContrl getHWDevControl(@Param("devId") String devId); - @Select("select * from CalDevContrl where Devid = #{Devid} and Status = 1 and ModelType = 9") - List getTempCalWitdDevid(@Param("Devid") String devId); - - @Select("select top 1* from CalDevContrl where DevBeId = #{beId} and Status = 1 and ModelType = 9") - CalDevContrl getTempDevice(@Param("beId") Integer beId); + @Select("select top 1 * from WL_Attendance where clint_id = #{clint_id} and clint_type =10") + Wl_Attendace getAttendace(@Param("clint_id") String clint_id); - @Select("select clint_id\n" + - "from WL_Attendance where school_id = 1085 and clint_type = 12 and state = 1") - List getHWDEvices(int i); + /*** + * @Description: 获取人感 + * @Param: [devId] + */ + @Select("select * from CalDevContrl where Devid = #{Devid} and Status = 1 and ModelType =10") + List getCalDevContrlWidthDevId(@Param("Devid") String devId); + + /*** + * @Description: 获取温感 + * @Param: [devId] + */ + @Select({"select * from CalDevContrl where Devid = #{Devid} and Status = 1 and ModelType = 9"}) + List getTempCalWitdDevid(@Param("Devid") String devId); + /** + * 获取红外遥控控制命令 + * @param ConDevId + * @return + */ @Select("select TOP(1) ConValue from CalDevBeContrl where State = 1 and ConDevId = #{ConDevId}") String getConBeValue(@Param("ConDevId") String ConDevId); - - @Select("select Top(1) InUserTime from CalDevContrl where Status = 1") - String getInuserTime(); - - @Select("select clint_id from WL_Attendance where State = 1 and school_id = 1085 and clint_type = 12") + /** + * 获取所有红外遥控器 + * @return + */ + @Select("select clint_id,school_id,name,isConnection from WL_Attendance where State = 1 and school_id = 1085 and clint_type = 12") List getHWIds(); - @Select("select * from WL_Attendance where state = 1 and school_id = 1085") List getWL_AttendanceIds(); - @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") String getWLName(@Param("clint_id") String clint_id); @@ -82,11 +85,13 @@ public interface DeviceDao { List getWL_LinkMode(@Param("SchoolId") String SchoolId); - IotDevice getIotDeviceRecord(@Param("devId") String devId, @Param("value") String value,@Param("openTime") String openTime); + IotDevice getIotDeviceAfterRecord(@Param("devId") String devId, @Param("value") String value,@Param("openTime") String openTime); - List getHWAtts(); + IotDevice getIotDeviceRecord(@Param("devId") String devId, @Param("value") String value); + List getHWAtts(); + CalDevContrl getTempDevice(@Param("beId") String beId); } diff --git a/cloud/mypulsar/src/main/java/com/example/mypulsar/task/SchduledTasks.java b/cloud/mypulsar/src/main/java/com/example/mypulsar/task/SchduledTasks.java index efb23c0..d106041 100644 --- a/cloud/mypulsar/src/main/java/com/example/mypulsar/task/SchduledTasks.java +++ b/cloud/mypulsar/src/main/java/com/example/mypulsar/task/SchduledTasks.java @@ -5,9 +5,8 @@ import com.alibaba.fastjson.JSONObject; import com.example.mypulsar.bean.*; import com.example.mypulsar.campusDao.CampusDao; import com.example.mypulsar.dao.DeviceDao; +import com.example.mypulsar.utils.DateFormatUtil; import com.example.mypulsar.utils.HttpUtil; -import com.example.mypulsar.utils.JsonUtils; -import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.dom4j.Document; import org.dom4j.DocumentException; @@ -36,16 +35,20 @@ public class SchduledTasks { public static Map conDevMap = new HashMap<>();//存储被控制的设备 - public static Map closeDevMap = new HashMap<>(); - - private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - private String closeModel = "{\"mode\":\"0\",\"power\":\"0\",\"remote_id\":\"%s\",\"remote_index\":\"11272\",\"temp\":\"26\",\"wind\":\"0\"}"; boolean isClose = false; boolean outClose= false; - @Scheduled(fixedRate = 60000) + + /*** + * @Description: 每间隔2分钟关闭一次到点设备无人状态下设备 + * @Param: [] + * @return: void + * @Author: xuquan + * @Date: 2021/4/29 + */ + @Scheduled(fixedRate = 300*1000) public void closeAttend() { log.info(".......定时任务执行设备关闭.......,有在线遥控设备数量" + conDevMap.size()); Iterator> it = conDevMap.entrySet().iterator(); @@ -58,17 +61,19 @@ public class SchduledTasks { //获取红外绑定的遥控器ID String conValue = getRemoteId(deviceDao.getConBeValue(conBeDeviceId)); Date openTime = entry.getValue(); - log.info("在线红外设备:" + name + "---在线时间:" + simpleDateFormat.format(openTime)+" 当前时间:"+ simpleDateFormat.format(new Date())); + //空调开启时间 + String openTimeStr = DateFormatUtil.dateFormatString(openTime,"yyyy-MM-dd HH:mm:ss"); + String nowTimeStr = DateFormatUtil.dateFormatString(new Date(),"yyyy-MM-dd HH:mm:ss"); + log.info("在线红外设备:" + name + "---在线时间:" + openTimeStr +" 当前时间:"+ nowTimeStr); //获取红外设备 CalDevBeContrl hwDevice = deviceDao.getHWDevControl(conBeDeviceId); if(hwDevice !=null){ - Integer beId = hwDevice.getId(); + String beId = hwDevice.getId() + ""; //获取温感设备 CalDevContrl calDevContrl = deviceDao.getTempDevice(beId); //无人状态直接关闭 - boolean isOk = convertDeviceRecord(calDevContrl,openTime); + boolean isOk = convertDeviceRecord(calDevContrl,openTimeStr); if(isOk){ - log.info("开始执行关闭命令, remote_id: {}",conValue); String response = HttpUtil.addAirCode(String.format(closeModel, conValue), conBeDeviceId, name); try{ // 将字符串转为XML @@ -80,7 +85,7 @@ public class SchduledTasks { log.info("请求结果:" + title); if(title.equals("200")){ log.info("关闭成功:设备 :{},遥控ID: {}",name,conValue); - conDevMap.remove(conBeDeviceId); + it.remove(); } } catch (DocumentException e) { e.printStackTrace(); @@ -91,141 +96,47 @@ public class SchduledTasks { } } } - if (!outClose){ - initWlModeOut(); - } - } - - - /** - * 解析上报记录,判断空间是否是无人状态。 - * 条件:第一次上报无人后,五分钟内没有上报有人记录,则为无人 - * @param calDevContrl - * @param openTime - * @return - */ - private boolean convertDeviceRecord(CalDevContrl calDevContrl,Date openTime){ - if(calDevContrl != null){ - List list = new ArrayList(); - Map map = new HashMap(); - //温感设备ID - String tempDevId =calDevContrl.getDevid(); - //空调开启时间 - String openTimeStr = simpleDateFormat.format(openTime); - //温感关联的人感设备 - String assDevice = calDevContrl.getAssDevice(); - String[] assDeviceIds = assDevice.split(","); - for (int i = 0; i < assDeviceIds.length; i++) { - String deviceId = assDeviceIds[i]; - //过滤温感设备ID - if (!deviceId.equals(tempDevId)) { - list.add(deviceId); - Wl_Attendace attendance = deviceDao.getAttendace(deviceId); - if(attendance==null){ - map.put(deviceId,"true"); - continue; - } - //取得空调开启时间,离最近一次上报无人记录 - IotDevice iotDevice = deviceDao.getIotDeviceRecord(deviceId, "无人", openTimeStr); - log.info("空调开启时间最近一次,无人记录,时间:{}, 设备信息:{}",openTime,JSON.toJSON(iotDevice)); - if(iotDevice != null){ - //五分钟内没有上报有人记录 - String afterTime5 = getDateAddMinute(iotDevice.getTime(),5); - iotDevice = deviceDao.getIotDeviceRecord(deviceId,"有人",afterTime5); - log.info("5分钟之后的有人记录,时间:{}, 设备信息:{}",afterTime5,JSON.toJSON(iotDevice)); - if(iotDevice ==null){ - map.put(deviceId,"true"); - }else{ - map.put(deviceId,"false"); - } - }else{ - map.put(deviceId,"false"); - } - } - } - log.info("设备信息:{}, 上报记录: {}",JSON.toJSON(list),JSON.toJSON(map)); - //false 无人 true 有人,当前后人感都为无人下,返回true - if(!CollectionUtils.isEmpty(list) && !map.isEmpty()){ - if(map.get(list.get(0)).equals("true")&& map.get(list.get(1)).equals("true")){ - return true; - } - } - } - return false; } - /** - * 日期叠加计算,以月为单位:整数 - * @param date 起始时间 - * @return - */ - public static String getDateAddMinute(String date,int length) { - Date date1 = stringFormatDate(date,"yyyy-MM-dd HH:mm:ss"); - Calendar cal = Calendar.getInstance(); - cal.setTime(date1);//设置起时间 - cal.add(Calendar.MINUTE,length); - String strDate = dateFormatString(cal.getTime(),"yyyy-MM-dd HH:mm:ss"); - return strDate; - } - /** - * String 转 Date : 指定格式:pattern - * @param date - * @param pattern - * @return + /*** + * @Description: 每间每隔1分钟执行一次到点关闭 + * @Param: [] + * @return: void + * @Author: xuquan + * @Date: 2021/4/29 */ - public static Date stringFormatDate(String date, String pattern) { - SimpleDateFormat simpleDateFormat = null; - if (date != null) { - try{ - simpleDateFormat = new SimpleDateFormat(pattern); - Date retultDate = simpleDateFormat.parse(date); - return retultDate; - }catch (ParseException e){ - e.printStackTrace(); - } + @Scheduled(fixedDelay = 60*1000) + public void closeDevice(){ + if (!outClose){ + initWlModeOut(); } - return null; } - /** - * Date转 String : 指定格式:pattern - * @param date - * @param pattern - * @return + /*** + * @Description: 在06:00-- 22:00之外,每隔一个小时执行一次计划之外关闭 + * @Param: [] + * @return: void + * @Author: xuquan + * @Date: 2021/4/29 */ - public static String dateFormatString(Date date,String pattern) { - SimpleDateFormat df = null; - String returnValue = null; - if (date != null) { - df = new SimpleDateFormat(pattern); - returnValue = df.format(date); - } - return returnValue; - } - - - @Scheduled(cron = "0 0/30 * * * ?") + @Scheduled(cron = "0 0 * * * ?") public void closeAttendOut() { int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY); - if (!isClose && hour < 23 && hour > 5){ + if (!isClose && hour > 22 && hour < 6){ initWlMode(); } } - public static void main(String[] args) { - System.out.println(Calendar.getInstance().get(Calendar.HOUR_OF_DAY)); - } - /** * 根据物联模板判断是否关闭 */ public void initWlModeOut() { try { - isClose = true; + outClose = true; //获取有效的模板 List wl_linkModes = deviceDao.getWL_LinkMode("1085"); - if (null != wl_linkModes) { + if (!CollectionUtils.isEmpty(wl_linkModes)) { for (int i = 0; i < wl_linkModes.size(); i++) { Wl_LinkModeBean wl_linkModeBean = wl_linkModes.get(i); String field = wl_linkModeBean.getField();//场地id @@ -239,39 +150,39 @@ public class SchduledTasks { String weekStr = jsonObject.getString("Week"); String[] weeks = weekStr.split(","); String endTime = jsonObject.getString("EndTime"); - SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); - SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm:ss"); int indexWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1; for (int k = 0; k < weeks.length; k++) { int week = Integer.parseInt(weeks[k]); if (week == indexWeek) { - try { - //截至时间 - Date endDate = simpleDateFormat.parse(endTime); - Date endDate2 = getDateByTime(3,endDate); - long entTime = endDate.getTime(); - long entTime2 = endDate2.getTime(); - //当前时间 - Date date = simpleDateFormat1.parse(new Date().getHours() + ":" + new Date().getMinutes() + ":" + new Date().getSeconds()); - long indexTime = date.getTime(); - //当前时间等于结束时间触发一次指令 - if (indexTime > entTime && indexTime < entTime2) { - //关闭所有设备 - for (int l = 0; l < fields.length; l++) { - String fieldId = fields[l]; - //获取红外遥控器 - List clintIds = deviceDao.getHWAttsWithRoomId(fieldId); - closeAllDevice(clintIds); - } + //截至时间 + String endDateStr = DateFormatUtil.dateFormatString(new Date(),"yyyy-MM-dd"); + endDateStr = endDateStr +" "+ endTime; + Date endDate = DateFormatUtil.stringFormatDate(endDateStr,"yyyy-MM-dd HH:mm"); + long endDateTime = endDate.getTime(); + //截至时间后2分钟 + Date after2Date = getDateByTime(3,endDate); + long after2DateTime = after2Date.getTime(); + //当前时间 + Date now = new Date(); + String nowStr = DateFormatUtil.dateFormatString(now,"yyyy-MM-dd HH:mm"); + Date dateNow = DateFormatUtil.stringFormatDate(nowStr,"yyyy-MM-dd HH:mm"); + long nowTime = dateNow.getTime(); + //当前时间等于结束时间触发一次指令 + if (nowTime > endDateTime && nowTime < after2DateTime) { + log.info("计划结束---,开始执行关闭空调任务-----"); + //关闭所有设备 + for (int l = 0; l < fields.length; l++) { + String fieldId = fields[l]; + //获取红外遥控器 + List clintIds = deviceDao.getHWAttsWithRoomId(fieldId); + closeAllDevice(clintIds); } - } catch (ParseException e) { - log.info("关闭设备异常: ",e); } } } } } - } catch (JSONException e) { + } catch (Exception e) { log.info("关闭设备异常: ",e); } } @@ -279,13 +190,13 @@ public class SchduledTasks { } catch (Exception e) { log.info("关闭设备异常: ",e); } - isClose = false;//保证条件一直在执行 + outClose = false;//保证条件一直在执行 } public static Date getDateByTime(int length, Date date){ Calendar cal = Calendar.getInstance(); cal.setTime(date);//设置起时间 - cal.add(Calendar.MINUTE,length);//把日期往后增加 + cal.add(Calendar.MINUTE,length);//把日期往后增加3分钟 return cal.getTime(); } @@ -338,7 +249,6 @@ public class SchduledTasks { long starTime = startDate.getTime(); long indexTime = date.getTime(); long entTime = endDate.getTime(); - if (indexTime < starTime || indexTime > entTime) {//在时间之外 //关闭所有设备 for (int l = 0; l < fields.length; l++) { @@ -362,6 +272,9 @@ public class SchduledTasks { } else { //获取红外遥控器 List clintIds = deviceDao.getHWIds(); + if(!CollectionUtils.isEmpty(clintIds)){ + return; + } closeAllDevice(clintIds); } } catch (Exception e) { @@ -416,4 +329,101 @@ public class SchduledTasks { } + /** + * 解析上报记录,判断空间是否是无人状态。 + * 条件:第一次上报无人后,五分钟内没有上报有人记录,则为无人 + * @param calDevContrl + * @param openTimeStr + * @return + */ + private boolean convertDeviceRecord(CalDevContrl calDevContrl,String openTimeStr){ + if(calDevContrl != null){ + List list = new ArrayList(); + Map map = new HashMap(); + //温感设备ID + String tempDevId =calDevContrl.getDevid(); + //温感关联的人感设备 + String assDevice = calDevContrl.getAssDevice(); + String[] assDeviceIds = assDevice.split(","); + for (int i = 0; i < assDeviceIds.length; i++) { + String deviceId = assDeviceIds[i]; + //过滤温感设备ID + if (!deviceId.equals(tempDevId)) { + list.add(deviceId); + //获取 + Wl_Attendace attendance = deviceDao.getAttendace(deviceId); + if(attendance==null){ + log.warn("设备ID["+deviceId+"],未绑定。"); + map.put(deviceId,1); + continue; + } + String deviceName = attendance.getName(); + Integer isConnection = attendance.getIsConnection(); + if(isConnection.intValue()!=1){ + log.warn("设备["+deviceName+"],离线跳过。"); + map.put(deviceId,1); + continue; + } + //取设备最近一次上报无人记录时间, + IotDevice front = deviceDao.getIotDeviceRecord(deviceId, "无人"); + String noTime = front.getTime(); + //比较两者时间先后,无人时间 < 空调开启时间,则取最近10分钟是否有人记录上报 + if(DateFormatUtil.timeCompare(noTime,openTimeStr) == -1){ + log.info("------- 最近无人时间小于空调开启时间 ------"); +// String afterTime = getDateAddMinute(noTime,5); + log.info("{},最近无人时间:{},空调开启时间:{},10分钟后时间:{}",deviceName,noTime,openTimeStr,noTime); + IotDevice iotDevice = deviceDao.getIotDeviceAfterRecord(deviceId, "有人", noTime); + log.info("10分钟后有人,记录信息:{}",JSON.toJSON(iotDevice)); + if(iotDevice == null){ + map.put(deviceId,1); + }else{ + map.put(deviceId,2); + } + }else{//比较两者时间先后,无人时间 > 空调开启时间,则取空调开启后最近的无人记录上报 + log.info("------- 最近无人时间大于空调开启时间 ------"); + log.info("{}, 开启时间:{}",deviceName, openTimeStr); + IotDevice iotDevice = deviceDao.getIotDeviceAfterRecord(deviceId, "无人", openTimeStr); + log.info("空调开启后,最近无人记录,记录信息:{}",JSON.toJSON(iotDevice)); + if(iotDevice != null) { + noTime = iotDevice.getTime(); +// String afterTime = getDateAddMinute(noTime, 5); + log.info("{},最近无人时间: {}",deviceName,noTime); + IotDevice after = deviceDao.getIotDeviceAfterRecord(deviceId, "有人", noTime); + log.info("10分钟后有人,记录信息:{}",JSON.toJSON(after)); + if (after == null) { + map.put(deviceId, 1); + } else { + map.put(deviceId, 2); + } + }else { + map.put(deviceId, 2); + } + } + } + } + log.info("上报记录:"+JSON.toJSON(map)); + //false 无人 true 有人,当前后人感都为无人下,返回true + if(!CollectionUtils.isEmpty(list) && !map.isEmpty()){ + if(map.get(list.get(0)).intValue()== 1 && map.get(list.get(1)).intValue()==1){ + return true; + } + } + } + return false; + } + + /** + * 日期叠加计算,以月为单位:整数 + * @param date 起始时间 + * @return + */ + public static String getDateAddMinute(String date,int length) { + Date date1 = DateFormatUtil.stringFormatDate(date,"yyyy-MM-dd HH:mm:ss"); + Calendar cal = Calendar.getInstance(); + cal.setTime(date1);//设置起时间 + cal.add(Calendar.MINUTE,length); + String strDate = DateFormatUtil.dateFormatString(cal.getTime(),"yyyy-MM-dd HH:mm:ss"); + return strDate; + } + } diff --git a/cloud/mypulsar/src/main/java/com/example/mypulsar/utils/ControlUtils.java b/cloud/mypulsar/src/main/java/com/example/mypulsar/utils/ControlUtils.java index 9cbdbf7..87dd9c7 100644 --- a/cloud/mypulsar/src/main/java/com/example/mypulsar/utils/ControlUtils.java +++ b/cloud/mypulsar/src/main/java/com/example/mypulsar/utils/ControlUtils.java @@ -1,24 +1,21 @@ package com.example.mypulsar.utils; import com.alibaba.fastjson.JSON; -import com.example.mypulsar.bean.CalDevBeContrl; -import com.example.mypulsar.bean.CalDevContrl; -import com.example.mypulsar.bean.CloseClintBean; -import com.example.mypulsar.bean.DeviceBean; +import com.alibaba.fastjson.TypeReference; +import com.example.mypulsar.bean.*; import com.example.mypulsar.dao.DeviceDao; import com.example.mypulsar.task.SchduledTasks; -import io.micrometer.core.instrument.util.JsonUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.configurationprocessor.json.JSONArray; import org.springframework.boot.configurationprocessor.json.JSONException; import org.springframework.boot.configurationprocessor.json.JSONObject; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; -import springfox.documentation.spring.web.json.Json; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; +import java.util.stream.Collectors; @Slf4j public class ControlUtils { @@ -63,7 +60,7 @@ public class ControlUtils { try { //联动的时间范围,[{"Week":"5","StartTime":"14:02","EndTime":"14:06"}] if(StringUtils.isEmpty(cal.getInUserTime())){ - log.info("人感控制设备ID: {},未设置联动计划。",cal.getDevid()); + log.error("人感控制设备ID: {},未设置联动计划。",cal.getDevid()); continue; } String inUseTime =cal.getInUserTime(); @@ -79,7 +76,6 @@ public class ControlUtils { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"); SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm:ss"); int indexWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1; - log.info("周计划日:{},当前日期:周:{}",weekStr,indexWeek); for (int j = 0; j < weeks.length; j++) { int week = Integer.parseInt(weeks[j]); if (week == indexWeek) { @@ -105,125 +101,61 @@ public class ControlUtils { } } } catch (JSONException e) { - e.printStackTrace(); + log.error("解析联动计划失败。",e); } catch (ParseException e) { - e.printStackTrace(); + log.error("解析联动计划失败。",e); } } } - /** - * @param cal 人体感应器 - * @param deviceBean 控制设备 - */ private void calCompre(CalDevContrl cal, DeviceBean deviceBean) { if (deviceBean.getValue().equals("无人")){ return; } - log.info("温度是否达标:,关联设备:{}",cal.getAssDevice()); -// //温度符合要求 - boolean isok = false; - //人感设备对应的关联联动设备 + Integer tempear = null; if (!StringUtils.isEmpty(cal.getAssDevice())) { String[] devices = cal.getAssDevice().split(","); for (int i = 0; i < devices.length; i++) { String device = devices[i]; - //取出湿度传感器设备 - List calDevContrls = deviceDao.getTempCalWitdDevid(device); - if(!CollectionUtils.isEmpty(calDevContrls)){ - for (int j = 0; j < calDevContrls.size(); j++) { - String tempDevId = calDevContrls.get(j).getDevid(); - if (tempDevices.containsKey(tempDevId)) { - //温度记录了 - int tempear = tempDevices.get(tempDevId); - log.info("有温度记录:"+tempear); - isok = tempIsOk(tempear, calDevContrls.get(j)); - } else { - //沒有记录溫度按照28度比较 - log.info("有温度记录:28"); - isok =tempIsOk(28, calDevContrls.get(j)); - } - } - } - } - if(isok){ - startDevice(cal,deviceBean.getValue()); - } - } - } - - /** - * 计算温度是否达标 - * - * @param assDevice - */ - private synchronized boolean calTemp(String assDevice) { - if (!StringUtils.isEmpty(assDevice)) { - String[] devices = assDevice.split(","); - for (int i = 0; i < devices.length; i++) { - String device = devices[i]; - //湿度传感器设备 List calDevContrls = deviceDao.getTempCalWitdDevid(device); - if(!CollectionUtils.isEmpty(calDevContrls)){ - for (int j = 0; j < calDevContrls.size(); j++) { - String tempDevId = calDevContrls.get(j).getDevid(); - if (tempDevices.containsKey(tempDevId)) { - //温度记录了 - int tempear = tempDevices.get(tempDevId); - log.info("有温度记录:"+tempear); - return tempIsOk(tempear, calDevContrls.get(j)); - } else { - //沒有记录溫度按照28度比较 - log.info("有温度记录:28"); - return tempIsOk(28, calDevContrls.get(j)); - } + if (!CollectionUtils.isEmpty(calDevContrls)) { + String tempDevId = calDevContrls.get(0).getDevid(); + tempear = deviceDao.getDeviceTemp(tempDevId); + if (tempear != null) { + tempIsOk(tempear.intValue(), calDevContrls.get(0), cal); + log.info("有温度上报记录:{}", tempear); + return; + }else{ + log.info("无温度上报记录:按默认26"); + tempIsOk(26, calDevContrls.get(0), cal); + return; } } } } - return false; } /** - * 温度比较 + * 温度比较, 根据条件开启 * @param tempear - * @param calDevContrl + * @param tempDev * @return */ - private boolean tempIsOk(int tempear, CalDevContrl calDevContrl) { - log.info("温度比较,温感设备信息:"+ JSON.toJSONString(calDevContrl)); - if(calDevContrl.getComPre() ==1){ - if (tempear <= Integer.parseInt(calDevContrl.getThreValue())) { - log.info("温度比较: 实际温度:{},小于制热临界值:{},准备开启制热。",tempear,calDevContrl.getThreValue()); - return true; + private void tempIsOk(int tempear, CalDevContrl tempDev,CalDevContrl preDev) { + //制热 comPre=1 ;threValue 临界值 + if(tempDev.getComPre() ==1){ + if (tempear <= Integer.parseInt(tempDev.getThreValue())) { + log.info("温度比较: 实际温度:{}, 小于制热临界值:{}, 准备开启制热。",tempear,tempDev.getThreValue()); + startDevice(preDev,1); } } - if(calDevContrl.getComPre() ==3){ - if (tempear >= Integer.parseInt(calDevContrl.getThreValue())) { - log.info("温度比较: 实际温度:{},大于制制冷临界值:{},准备开启制冷。",tempear,calDevContrl.getThreValue()); - return true; + //制冷 comPre=3 threValue 临界值 + if(tempDev.getComPre() ==3){ + if (tempear >= Integer.parseInt(tempDev.getThreValue())) { + log.info("温度比较: 实际温度:{}, 大于制制冷临界值:{},准备开启制冷。",tempear,tempDev.getThreValue()); + startDevice(preDev,0); } } - return false; - } - - /** - * 移除设备 - * - * @param calDevContrl - */ - private void removeSet(CalDevContrl calDevContrl) { - - Iterator calDevContrlIterator = deviceSet.iterator(); - - while (calDevContrlIterator.hasNext()) { - - CalDevContrl calDevContrl1 = calDevContrlIterator.next(); - if (calDevContrl1.getDevid().equals(calDevContrl.getDevid()) && calDevContrl.getComPre() == calDevContrl1.getComPre()) { - calDevContrlIterator.remove(); - } - } - } /** @@ -231,8 +163,7 @@ public class ControlUtils { * * @param cal */ - private void startDevice(CalDevContrl cal,String value) { -// deviceSet.add(cal); + private void startDevice(CalDevContrl cal,int flag) { //设备类型 10:人感;9温感 int modelType = cal.getModelType(); //人感设备关联的遥控设备 @@ -240,56 +171,161 @@ public class ControlUtils { //取得红外遥控设备 List calDevBeContrlList = getCalDevBeCon(assDevice); if(CollectionUtils.isEmpty(calDevBeContrlList)){ - log.info("未查询到控制红外遥控器"); + log.error("未查询到控制红外遥控器"); return; } + //遍历红外遥控 for (int i = 0; i < calDevBeContrlList.size(); i++) { CalDevBeContrl calDevBeContrl = calDevBeContrlList.get(i); //红外遥控设备id String conDevId = calDevBeContrl.getConDevId(); String conCodes = calDevBeContrl.getConCode(); + //获取有效的模板 + List linkModes = deviceDao.getWL_LinkMode("1085"); + Wl_LinkModeBean wl_linkModeBean = null; + if(!CollectionUtils.isEmpty(linkModes)){ + wl_linkModeBean= linkModes.get(0); + } //遥控设备,执行命令 - String conValue = calDevBeContrl.getConValue(); - String[] conCodeStr = conCodes.split(","); + String conValue = convert(wl_linkModeBean,calDevBeContrl.getConValue(),flag); + if(StringUtils.isEmpty(conValue)){ + log.error("开启指令解析失败"); + return; + } //设备名 String name = deviceDao.getWLName(conDevId); - for (int j = 0; j < conCodeStr.length; j++) { - String conCode = conCodeStr[j]; - switch (modelType) { - case 1://开关 - HttpUtil.controlDev(conDevId, conCode, conValue); - break; - case 2://插座 - HttpUtil.controlDev(conDevId, conCode, conValue); - break; - case 3://排插 - HttpUtil.controlDev(conDevId, conCode, conValue); - break; - case 9://温度传感器 - break; - case 10://人体感应触发空调开关 - log.info("设备类型:"+modelType+"设备集合"+ JSON.toJSONString(SchduledTasks.conDevMap)); - if (!SchduledTasks.conDevMap.containsKey(conDevId)&&SchduledTasks.conDevMap.get(conDevId)==null) { - //设备已开,更新时间 - SchduledTasks.conDevMap.put(conDevId,new Date()); - HttpUtil.addAirCode(conValue, conDevId, name); - } - case 12://万能遥控器 - log.info("设备类型:"+modelType); - if (!SchduledTasks.conDevMap.containsKey(conDevId)&&SchduledTasks.conDevMap.get(conDevId)==null) {//设备已开,更新时间 - SchduledTasks.conDevMap.put(conDevId,new Date()); - HttpUtil.addAirCode(conValue, conDevId, name); - } - break; + switch (modelType) { + case 1://开关 + sendDeviceCommand(conCodes,conDevId,conValue); + break; + case 2://插座 + sendDeviceCommand(conCodes,conDevId,conValue); + break; + case 3://排插 + sendDeviceCommand(conCodes,conDevId,conValue); + break; + case 9://温度传感器 + break; + case 10://人体感应触发空调开关 + log.info("设备类型:"+modelType+"设备集合"+ JSON.toJSONString(SchduledTasks.conDevMap)); + if (!SchduledTasks.conDevMap.containsKey(conDevId) && SchduledTasks.conDevMap.get(conDevId)==null) { + //设备已开,更新时间 + SchduledTasks.conDevMap.put(conDevId,new Date()); + HttpUtil.addAirCode(conValue, conDevId, name); + } + case 12://万能遥控器 + log.info("设备类型:"+modelType); + if (!SchduledTasks.conDevMap.containsKey(conDevId) && SchduledTasks.conDevMap.get(conDevId)==null) { + SchduledTasks.conDevMap.put(conDevId,new Date()); + HttpUtil.addAirCode(conValue, conDevId, name); + } + break; + } + + } + } + + /*** + * @Description: 发送 开关、插座、排插命令 + * @Param: [conCodes, conDevId, conValue] + * @return: void + * @Author: xuquan + * @Date: 2021/4/28 + */ + private void sendDeviceCommand(String conCodes,String conDevId,String conValue){ + String[] conCodeStr = conCodes.split(","); + for (int j = 0; j < conCodeStr.length; j++) { + String conCode = conCodeStr[j]; + HttpUtil.controlDev(conDevId, conCode, conValue); + } + } + + private static String convert(Wl_LinkModeBean wl_linkModeBean,String conValue,int flag){ + if(wl_linkModeBean == null){ + return ""; + } + TuYaAirCondition condition = JsonUtils.nonDefaultMapper().fromJson(conValue,TuYaAirCondition.class); + //联动设置 + String para = wl_linkModeBean.getDevicePara(); + List list = JSON.parseObject(para, new TypeReference>(){}); + if(CollectionUtils.isEmpty(list)) { + return ""; + } + for(AirLinkMode mode : list){ + if(mode.getDeviceType().intValue() ==9){ + List commands = mode.getKVs(); + for(AirCommand command : commands){ + int key = command.getKey().intValue(); + String temp = command.getValue(); + //制冷 + if(flag ==0 && key == flag){ + condition.setMode("0"); + condition.setPower("1"); + condition.setTemp(temp); + condition.setWind("0"); + continue; + } + //制热 + if(flag ==1 && key == flag){ + condition.setMode("1"); + condition.setPower("1"); + //制热默认22 + condition.setTemp("22"); + condition.setWind("0"); + } } } } + return JsonUtils.nonDefaultMapper().toJson(condition); } - /** - * 获取红外遥控控制的设备 - * @param assDevice - * @return +// private static String convert2(String s,String conValue,int flag) throws JSONException { +// TuYaAirCondition condition = JsonUtils.nonDefaultMapper().fromJson(conValue,TuYaAirCondition.class); +// List list = JSON.parseObject(s, new TypeReference>(){}); +// if(CollectionUtils.isEmpty(list)) { +// return ""; +// } +// for(AirLinkMode mode : list){ +// if(mode.getDeviceType().intValue() ==9){ +// List commands = mode.getKVs(); +// for(AirCommand command : commands){ +// int key = command.getKey().intValue(); +// String temp = command.getValue(); +// //制冷 +// if(flag ==0 && key == flag){ +// condition.setMode("1"); +// condition.setPower("1"); +// condition.setTemp(temp); +// condition.setWind("0"); +// continue; +// } +// //制热 +// if(flag ==1 && key == flag){ +// condition.setMode("2"); +// condition.setPower("1"); +// condition.setTemp(temp); +// condition.setWind("0"); +// } +// } +// } +// } +// return JsonUtils.nonDefaultMapper().toJson(condition); +// } +// +// public static void main(String[] args) throws JSONException { +// 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\":\"空调关\"}]}]"; +// List list = JsonUtils.nonDefaultMapper().fromJson(s,List.class); +// String s2 = "{\"mode\":2,\"power\":\"1\",\"remote_id\":\"6c1556ca44a7e5d2336nrr\",\"remote_index\":\"11272\",\"temp\":\"26\",\"wind\":\"0\"}"; +// //获取有效的模板 +// System.out.println(convert2(s,s2,1)); +// } + + /*** + * @Description: 获取红外设备含多台 + * @Param: [assDevice] + * @return: java.util.List + * @Author: xuquan + * @Date: 2021/4/28 */ private List getCalDevBeCon(String assDevice) { String[] assDeviceIds = assDevice.split(","); @@ -315,7 +351,13 @@ public class ControlUtils { } } } - return devBeContrlList; + if(devBeContrlList.size()>0){ + //去重重复数据 + List hwList = devBeContrlList.stream().collect(Collectors.collectingAndThen( + Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(CalDevBeContrl::getConDevId))), ArrayList::new)); + return hwList; + } + return null; } diff --git a/cloud/mypulsar/src/main/java/com/example/mypulsar/utils/DateFormatUtil.java b/cloud/mypulsar/src/main/java/com/example/mypulsar/utils/DateFormatUtil.java new file mode 100644 index 0000000..ca80ef5 --- /dev/null +++ b/cloud/mypulsar/src/main/java/com/example/mypulsar/utils/DateFormatUtil.java @@ -0,0 +1,168 @@ +package com.example.mypulsar.utils; + + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +/** + * 日期公共计算工具类 + * @Author: xuquan + * @Date: 2020/8/2 10:24 + */ +public class DateFormatUtil { + + private static final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); + + + /** + * 日期叠加计算,以年为单位:整数 + * @param length 叠加长度 + * @param date 起始时间 + * @return + */ + public static Date getDateByYearLength(int length, Date date) { + Calendar cal = Calendar.getInstance(); + cal.setTime(date);//设置起时间 + cal.add(Calendar.YEAR, length);//增加一年 + return cal.getTime(); + } + + /** + * 日期叠加计算,以月为单位:整数 + * @param length 叠加长度 + * @param date 起始时间 + * @return + */ + public static Date getDateByMonthLength(int length, Date date) { + Calendar cal = Calendar.getInstance(); + cal.setTime(date);//设置起时间 + cal.add(Calendar.MONTH, length);//增加一月 + return cal.getTime(); + } + + /** + * 日期叠加计算,以月为单位:整数 + * @param date 起始时间 + * @return + */ + public static String getDateAdd(String date) { + Date date1 = stringFormatDate(date,"yyyy-MM-dd HH:mm:ss"); + Calendar cal = Calendar.getInstance(); + cal.setTime(date1);//设置起时间 + cal.add(Calendar.SECOND, 2); + String strDate = dateFormatString(cal.getTime(),"yyyy-MM-dd HH:mm:ss"); + return strDate; + } + + /** + * 日期叠加计算,以天为单位:整数 + * @param date 起始时间 + * @return + */ + public static String getDateByTime(String date){ + Date date1 = stringFormatDate(date,"yyyy-MM-dd HH:mm:ss"); + Calendar cal = Calendar.getInstance(); + cal.setTime(date1);//设置起时间 + cal.add(Calendar.DATE,1);//把日期往后增加 + String strDate = dateFormatString(cal.getTime(),"yyyy-MM-dd HH:mm:ss"); + return strDate; + } + + /** + * Date转 String : 指定格式:pattern + * @param date + * @param pattern + * @return + */ + public static String dateFormatString(Date date,String pattern) { + SimpleDateFormat df = null; + String returnValue = null; + if (date != null) { + df = new SimpleDateFormat(pattern); + returnValue = df.format(date); + } + return returnValue; + } + + /** + * String 转 Date : 指定格式:pattern + * @param date + * @param pattern + * @return + */ + public static Date stringFormatDate(String date, String pattern) { + SimpleDateFormat simpleDateFormat = null; + if (date != null) { + try{ + simpleDateFormat = new SimpleDateFormat(pattern); + Date retultDate = simpleDateFormat.parse(date); + return retultDate; + }catch (ParseException e){ + e.printStackTrace(); + } + } + return null; + } + + /** + * 输出yyyy年MM月dd日 格式时间 + * @param date + * @param pattern + * @return + */ + public static String formatDate(String date, String pattern) { + SimpleDateFormat sf = new SimpleDateFormat(pattern); + String resultTime =""; + try{ + Date datetime = simpleDateFormat.parse(date); + resultTime = sf.format(datetime); + }catch (ParseException e){ + e.printStackTrace(); + } + return resultTime; + } + /** + * 比较两时间大小 + * @param t1 + * @param t2 + * @return + */ + public static int timeCompare(String t1,String t2){ + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + Calendar c1=Calendar.getInstance(); + Calendar c2=Calendar.getInstance(); + try { + c1.setTime(formatter.parse(t1)); + c2.setTime(formatter.parse(t2)); + } catch (ParseException e) { + e.printStackTrace(); + } + int result=c1.compareTo(c2); + return result; + } + + private static byte[] lock = new byte[0]; + // 位数,默认是8位 + private final static long strNum = 100000000; + + /** + * 创建以时间为唯一UUID + * @return + */ + public static String createTimeId() { + long r = 0; + synchronized (lock) { + r = (long) ((Math.random() + 1) * strNum); + } + return System.currentTimeMillis() + String.valueOf(r).substring(1); + } + + public static void main(String[] args) { + System.out.println(timeCompare("2021-04-29 23:32","2021-04-29 23:38")); + System.out.println(timeCompare("2021-04-29 23:38","2021-04-29 23:38")); + System.out.println(timeCompare("2021-04-29 23:42","2021-04-29 23:38")); + } + +} diff --git a/cloud/mypulsar/src/main/java/com/example/mypulsar/utils/HttpUtil.java b/cloud/mypulsar/src/main/java/com/example/mypulsar/utils/HttpUtil.java index 542c4d7..625a52f 100644 --- a/cloud/mypulsar/src/main/java/com/example/mypulsar/utils/HttpUtil.java +++ b/cloud/mypulsar/src/main/java/com/example/mypulsar/utils/HttpUtil.java @@ -7,22 +7,17 @@ import com.example.mypulsar.task.SchduledTasks; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.configurationprocessor.json.JSONException; import org.springframework.boot.configurationprocessor.json.JSONObject; -import org.springframework.core.io.FileSystemResource; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import java.io.BufferedReader; import java.io.DataOutputStream; -import java.io.File; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * http 工具类 @@ -141,24 +136,33 @@ public class HttpUtil { * @param conDevName 设备名称 * @return */ - public static String addAirCode(String conValue, String conDevId, String conDevName) { + public static String addAirCode(String conValue, String conDevId, String conDevName) { try { String url = String.format("http://120.26.116.253:5555/TuYa/%s/addAirCode", conDevId); -// String url = String.format("http://120.26.116.253:5555/TuYa/%s/airConditioners", conDevId); RestTemplate restTemplate = new RestTemplate(); TuYaAirCondition tuYaAirCondition = new TuYaAirCondition(); - String power = ""; try { JSONObject jsonObject = new JSONObject(conValue); - power = jsonObject.getString("power"); - tuYaAirCondition.setMode(jsonObject.getString("mode")); + //开关 + String power = jsonObject.getString("power"); + //模式 + String mode = jsonObject.getString("mode"); + //风速 + String wind = jsonObject.getString("wind"); + //温度 + String temp = jsonObject.getString("temp"); + //遥控器码值 + String remoteIndex =jsonObject.getString("remote_index"); + //遥控器ID + String remoteId = jsonObject.getString("remote_id"); + tuYaAirCondition.setMode(mode); tuYaAirCondition.setPower(power); - tuYaAirCondition.setRemote_id(jsonObject.getString("remote_id")); - tuYaAirCondition.setWind(jsonObject.getString("wind")); - tuYaAirCondition.setTemp(jsonObject.getString("temp")); - tuYaAirCondition.setRemote_index(jsonObject.getString("remote_index")); + tuYaAirCondition.setRemote_id(remoteId); + tuYaAirCondition.setWind(wind); + tuYaAirCondition.setTemp(temp); + tuYaAirCondition.setRemote_index(remoteIndex); } catch (JSONException e) { - log.info("控制失败:失败信息",e); + log.error("解析指令失败:",e); } log.info("开始发送遥控命令至设备:请求地址: {},设备名称: {},设备ID: {},请求参数: {}",url,conDevName,conDevId,tuYaAirCondition.toString()); //轮询2次下发指令,防止偶然性第一次没启动 @@ -170,11 +174,11 @@ public class HttpUtil { log.info("返回结果:{}",response); return response; }catch (Exception e){ - log.info("请求红外控制指令失败:{}",e); + log.error("请求红外控制指令失败:{}",e); if(SchduledTasks.conDevMap.containsKey(conDevId)){ SchduledTasks.conDevMap.remove(conDevId); } } - return null; + return ""; } } diff --git a/cloud/mypulsar/src/main/resources/iotmapper/iotmapper.xml b/cloud/mypulsar/src/main/resources/iotmapper/iotmapper.xml index 47f05a0..3f11d80 100644 --- a/cloud/mypulsar/src/main/resources/iotmapper/iotmapper.xml +++ b/cloud/mypulsar/src/main/resources/iotmapper/iotmapper.xml @@ -7,7 +7,7 @@ --> @@ -20,16 +20,25 @@ + + + + \ No newline at end of file -- libgit2 0.21.0