Commit 631fbdf8370b71ef939f85cc70b7132e6e3a0da2

Authored by 徐泉
1 parent 2cbbb164
Exists in master

物联网涂鸦接口修改优化

cloud/mypulsar/pom.xml
... ... @@ -41,7 +41,11 @@
41 41 <systemPath>${project.basedir}/libs/sqljdbc4-4.2-6.jar</systemPath>
42 42 </dependency>
43 43  
44   -
  44 + <dependency>
  45 + <groupId>dom4j</groupId>
  46 + <artifactId>dom4j</artifactId>
  47 + <version>1.6.1</version>
  48 + </dependency>
45 49 <dependency>
46 50 <groupId>io.springfox</groupId>
47 51 <artifactId>springfox-swagger2</artifactId>
... ... @@ -140,13 +144,13 @@
140 144 <configuration>
141 145 <includeSystemScope>true</includeSystemScope>
142 146 <layout>ZIP</layout>
143   - <includes>
144   - <include>
145   - <!-- 排除所有Jar -->
146   - <groupId>nothing</groupId>
147   - <artifactId>nothing</artifactId>
148   - </include>
149   - </includes>
  147 +<!-- <includes>-->
  148 +<!-- <include>-->
  149 +<!-- &lt;!&ndash; 排除所有Jar &ndash;&gt;-->
  150 +<!-- <groupId>nothing</groupId>-->
  151 +<!-- <artifactId>nothing</artifactId>-->
  152 +<!-- </include>-->
  153 +<!-- </includes>-->
150 154 </configuration>
151 155 </plugin>
152 156 </plugins>
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/MyRunnerableInt.java
1 1 package com.example.mypulsar;
2 2  
3 3 import com.alibaba.fastjson.JSON;
4   -import com.example.mypulsar.bean.CalDevContrl;
5   -import com.example.mypulsar.bean.DeviceBean;
6   -import com.example.mypulsar.bean.TuYaReceiverBean;
7   -import com.example.mypulsar.bean.TuYaReceiverBeanStatus;
  4 +import com.example.mypulsar.bean.*;
8 5 import com.example.mypulsar.dao.DeviceDao;
9 6 import com.example.mypulsar.message.MessageVO;
10 7 import com.example.mypulsar.mq.AESBase64Utils;
... ... @@ -14,6 +11,7 @@ import com.example.mypulsar.task.SchduledTasks;
14 11 import com.example.mypulsar.utils.ControlUtils;
15 12 import com.example.mypulsar.utils.PulsarConsumerPoolFactory;
16 13 import com.example.mypulsar.utils.ThreadPoolFactory;
  14 +import lombok.extern.slf4j.Slf4j;
17 15 import org.apache.pulsar.client.api.Consumer;
18 16 import org.apache.pulsar.client.impl.TopicMessageIdImpl;
19 17 import org.slf4j.Logger;
... ... @@ -22,14 +20,17 @@ import org.springframework.beans.factory.annotation.Autowired;
22 20 import org.springframework.boot.ApplicationArguments;
23 21 import org.springframework.boot.ApplicationRunner;
24 22 import org.springframework.stereotype.Component;
  23 +import org.springframework.util.CollectionUtils;
25 24 import org.springframework.web.client.RestTemplate;
26 25 import sun.rmi.runtime.Log;
27 26  
28 27 import java.text.SimpleDateFormat;
  28 +import java.util.ArrayList;
29 29 import java.util.Date;
30 30 import java.util.List;
31 31 import java.util.function.BiConsumer;
32 32  
  33 +@Slf4j
33 34 @Component
34 35 public class MyRunnerableInt implements ApplicationRunner {
35 36  
... ... @@ -63,7 +64,6 @@ public class MyRunnerableInt implements ApplicationRunner {
63 64 * 第二种方式
64 65 */
65 66 private void initMqConsumer() {
66   -
67 67 MqConsumer mqConsumer = MqConsumer.build().serviceUrl(url).accessId(accessId).accessKey(accessKey)
68 68 .maxRedeliverCount(3).messageListener(message -> {
69 69 //消息内容
... ... @@ -71,6 +71,7 @@ public class MyRunnerableInt implements ApplicationRunner {
71 71 MessageVO vo = JSON.parseObject(jsonMessage, MessageVO.class);
72 72 String data = AESBase64Utils.decrypt(vo.getData(), accessKey.substring(8, 24));
73 73 TuYaReceiverBean tuYaReceiverBean = JSON.parseObject(data, TuYaReceiverBean.class);
  74 + log.info("接收消息,消息内容:"+ JSON.toJSONString(tuYaReceiverBean));
74 75 //处理数据上报
75 76 detealData(tuYaReceiverBean);
76 77 }
... ... @@ -79,8 +80,7 @@ public class MyRunnerableInt implements ApplicationRunner {
79 80 try {
80 81 mqConsumer.start();
81 82 } catch (Exception e) {
82   - logger.error("start error:" + e.toString());
83   - e.printStackTrace();
  83 + logger.error("start error:",e);
84 84 }
85 85 }
86 86  
... ... @@ -90,7 +90,6 @@ public class MyRunnerableInt implements ApplicationRunner {
90 90 * @param tuYaReceiverBean
91 91 */
92 92 private void detealData(TuYaReceiverBean tuYaReceiverBean) {
93   -// System.out.println("tuYaReceiverBean:" + tuYaReceiverBean.toString());
94 93 //涂鸦全局唯一ID
95 94 String dataId = tuYaReceiverBean.getDataId();
96 95 //其他事件
... ... @@ -106,42 +105,41 @@ public class MyRunnerableInt implements ApplicationRunner {
106 105 deviceBean.setBizCode(bizCode);
107 106 deviceBean.setDevId(devId);
108 107 deviceBean.setProductKey(productKey);
109   -
110   - if (bizCode == null) {//数据上报事件
  108 + //温度、人感、湿度数据上报事件
  109 + if (bizCode == null) {
111 110 deviceBean.setBizCode("");
  111 + //在线状态:1在线0离线
112 112 deviceBean.setDev_status(1);
  113 + //设备上报事件:code:时间类型; 格式:"status":[{"code":"pir","t":1612283849564,"value":"pir"}]
113 114 List<TuYaReceiverBeanStatus> status = tuYaReceiverBean.getStatus();
114 115 for (TuYaReceiverBeanStatus statue : status) {
115 116 //事件上报处理
116   - initStatue(statue, devId, productKey, dataId, deviceBean);
  117 + initStatue(statue, deviceBean);
117 118 }
118 119 } else {//其他事件上报
119 120 deviceBean.setTime(new SimpleDateFormat("yyyy-MM-dd HH::mm:ss").format(new Date()));
  121 + String name = deviceDao.selectAttendance(devId);
120 122 switch (bizCode) {
121 123 case "offline"://设备掉线
122   - log("设备掉线:" + devId);
  124 + log.info("设备: {},已掉线",name);
123 125 deviceBean.setDev_type(1);
124 126 deviceBean.setDev_status(0);
125 127 updateDevStatus(deviceBean);
126 128 updateStatus(devId, "0");
127 129 break;
128 130 case "online"://设备上线
129   - log("设备上线:" + devId);
  131 + log.info("设备: {},已上线",name);
130 132 deviceBean.setDev_status(1);
131 133 deviceBean.setDev_type(2);
132 134 updateDevStatus(deviceBean);
133 135 break;
134 136 case "nameUpdate"://设备名称修改
135   -
136 137 break;
137 138 case "dpNameUpdate"://功能点修改
138   -
139 139 break;
140 140 case "bindUser"://绑定用户
141   -
142 141 break;
143 142 case "delete"://删除设备
144   -
145 143 break;
146 144 }
147 145 }
... ... @@ -149,7 +147,6 @@ public class MyRunnerableInt implements ApplicationRunner {
149 147 }
150 148  
151 149 private void updateStatus(String devId, String isConnection) {
152   -
153 150 // String clintId = deviceDao.selectAttendance(devId);
154 151 // if (clintId.equals(devId)) {
155 152 deviceDao.updateStatus(devId, isConnection, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
... ... @@ -160,7 +157,6 @@ public class MyRunnerableInt implements ApplicationRunner {
160 157 }
161 158  
162 159 private void updateDevStatus(DeviceBean deviceBean) {
163   -// System.out.println("插入数据:" + deviceBean.toString());
164 160 deviceBean.setDpId("");
165 161 deviceBean.setName("");
166 162 deviceBean.setUid("");
... ... @@ -169,6 +165,7 @@ public class MyRunnerableInt implements ApplicationRunner {
169 165 if (deviceBean.getDataId() == null) deviceBean.setDataId("");
170 166 if (deviceBean.getCode() == null) deviceBean.setCode("");
171 167 if (deviceBean.getProductKey() == null) deviceBean.setProductKey("");
  168 + log("设备上线,更新历史记录: "+JSON.toJSONString(deviceBean));
172 169 int index = deviceDao.addDevice(deviceBean.getDataId(), deviceBean.getDevId(), deviceBean.getProductKey(), deviceBean.getCode(), deviceBean.getValue()
173 170 , deviceBean.getBizCode(), deviceBean.getTime(), deviceBean.getName(), deviceBean.getDpId(), deviceBean.getUid(), deviceBean.getDev_status(), deviceBean.getDev_type());
174 171 updateStatus(deviceBean.getDevId(), "1");
... ... @@ -178,43 +175,59 @@ public class MyRunnerableInt implements ApplicationRunner {
178 175 * 数据上报事件处理
179 176 *
180 177 * @param statue
181   - * @param devId
182   - * @param productKey
183   - * @param dataId
184 178 * @param deviceBean
185 179 */
186   - private void initStatue(TuYaReceiverBeanStatus statue, String devId, String productKey, String dataId, DeviceBean deviceBean) {
187   - String code = statue.getCode();
188   - deviceBean.setCode(code);
189   - deviceBean.setProductKey(productKey);
190   - deviceBean.setDataId(dataId);
  180 + private void initStatue(TuYaReceiverBeanStatus statue,DeviceBean deviceBean) {
  181 + //设备消息类型:1:掉线,2:上线,3:其他事件
191 182 deviceBean.setDev_type(3);
  183 + //事件时间
192 184 long t = statue.getT();
193 185 Date date = new Date();
194 186 date.setTime(t);
195 187 deviceBean.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
  188 + //事件value值
196 189 String value = statue.getValue();
197 190 deviceBean.setValue(value);
198   -
199   -// System.out.println(devId+"-插入数据:" +value);
  191 + //事件类型:温度va_temperature、湿度va_humidity、人感pir
  192 + // 格式:"status":[{"code":"pir","t":1612283849564,"value":"pir"}],"status":[{"code":"va_temperature","t":1612283809697,"value":"1879"}]
  193 + String code = statue.getCode();
  194 + deviceBean.setCode(code);
200 195 switch (code) {
201 196 case "va_temperature"://温度
202 197 deviceBean.setValue(Integer.parseInt(value) / 100 + "");
203   - calOpenOrCloseDevWithtemper(deviceBean);//计算开启或关闭设备
204   - log("温度:" + value);
  198 + //计算开启或关闭设备
  199 +// calOpenOrCloseDevWithtemper(deviceBean);
  200 + log.info("温度:" + deviceBean.getValue());
  201 + //存储 对应设备对应的温度记录
  202 + ControlUtils.tempDevices.put(deviceBean.getDevId(),Integer.valueOf(deviceBean.getValue()));
205 203 break;
206 204 case "va_humidity"://湿度
207 205 deviceBean.setValue(Integer.parseInt(value) / 100 + "");
208   - log("湿度:" + value);
  206 + log.info("湿度:" + deviceBean.getValue());
  207 + break;
  208 + case "pir":
  209 + //人体感应
  210 + log.info("人体感应:" + value);
  211 + if(value.equals("pir")){
  212 + deviceBean.setValue("有人");
  213 + }else{
  214 + deviceBean.setValue("无人");
  215 + }
  216 + setClintTime(deviceBean.getDevId());
  217 + try {
  218 + Thread.sleep(1000);
  219 + //根据场地是否有人控制空调设备
  220 + calOpenOrCloseDevWithPir(deviceBean);
  221 + } catch (InterruptedException e) {
  222 + e.printStackTrace();
  223 + }
209 224 break;
210 225 case "temper_alarm"://防拆报警
211   -
212 226 break;
213 227 case "battery_percentage"://电池电量百分比
214   - log("电池电量百分比:" + value);
  228 + log.info("电池电量百分比:" + value);
215 229 break;
216 230 case "battery_value"://电池电量值
217   -
218 231 break;
219 232 case "battery"://门磁电池电量值
220 233 log("电池电量值:" + value);
... ... @@ -230,48 +243,9 @@ public class MyRunnerableInt implements ApplicationRunner {
230 243 value = (value.equals("true") ? "开门" : "关门");
231 244 log("开关状态:" + (value.equals("true") ? "开门" : "关门"));
232 245 break;
233   - case "va_battery"://电池电量值
234   -
235   - break;
236   - case "switch_1"://开关1
237   -
238   - break;
239   - case "switch_2"://开关2
240   -
241   - break;
242   - case "switch_3"://开关3
243   -
244   - break;
245   - case "switch_4"://开关4
246   -
247   - break;
248   - case "switch_5"://开关5
249   -
250   - break;
251   - case "switch_6"://开关6
252   -
253   - break;
254   - case "study_code":
255   -
256   - break;
257   - case "control":
258   -
259   - break;
260   - case "pir"://人体感应
261   - log("人体感应:" + value);
262   - value = (value.equals("pir") ? "有人" : "无人");
263   -
264   - deviceBean.setValue(value);
265   - try {
266   - Thread.sleep(1000);
267   - calOpenOrCloseDevWithPir(deviceBean);
268   - } catch (InterruptedException e) {
269   - e.printStackTrace();
270   - }
271   - break;
272 246 case "cur_voltage"://当前电压
273 247 // value = Integer.parseInt(value) / 10 + "V";
274   - log(devId + " 当前电压:" + value);
  248 + log(deviceBean.getDevId() + " 当前电压:" + value);
275 249 break;
276 250 case "cur_power"://当前功率
277 251 // value = value.equals("0")?"0":Integer.parseInt(value) / 10 + "W";
... ... @@ -282,7 +256,6 @@ public class MyRunnerableInt implements ApplicationRunner {
282 256 // log(devId + " 当前电流:" + Integer.parseInt(value) / 10 + "A");
283 257 break;
284 258 case "router_mgr"://网关路由
285   -
286 259 break;
287 260 }
288 261 updateDevStatus(deviceBean);
... ... @@ -294,19 +267,71 @@ public class MyRunnerableInt implements ApplicationRunner {
294 267 * @param deviceBean
295 268 */
296 269 private void calOpenOrCloseDevWithPir(DeviceBean deviceBean) {
297   -
  270 +// if (deviceBean.getValue().equals("有人")){
  271 +// deviceBean.setValue("1");
  272 +// } else{
  273 +// deviceBean.setValue("0");
  274 +// }
  275 + //获取人感设备控制
298 276 List<CalDevContrl> calDevContrls = deviceDao.getCalDevContrlWidthDevId(deviceBean.getDevId());
299   -// System.out.println("calOpenOrCloseDevWithPir:"+calDevContrls.toString());
300   - if (null != calDevContrls && calDevContrls.size() > 0) {
301   - if (deviceBean.getValue().equals("有人")) deviceBean.setValue("1");
302   - else deviceBean.setValue("0");
303   -
304   - ControlUtils.getInstance(deviceDao).conTemper(calDevContrls, deviceBean);
  277 + if (CollectionUtils.isEmpty(calDevContrls)) {
  278 + log.info("未关联人感控制设备, 设备ID: "+ deviceBean.getDevId());
  279 + return;
305 280 }
  281 + ControlUtils.getInstance(deviceDao).conTemper(calDevContrls, deviceBean);
  282 + }
306 283  
  284 + /**
  285 + * 设置更新上报时间
  286 + * @param clintId
  287 + */
  288 + private void setClintTime(String clintId){
  289 + List<CalDevContrl> calDevContrls = deviceDao.getCalDevContrlWidthDevId(clintId);
  290 + if(!CollectionUtils.isEmpty(calDevContrls)){
  291 + CalDevContrl calDevContrl = calDevContrls.get(0);
  292 + String assDevice = calDevContrl.getAssDevice();
  293 + //红外遥控器
  294 + CalDevBeContrl hwClint = getCalDevBeCon(assDevice);
  295 + if(hwClint==null) {
  296 + log.info("未查询到控制红外遥控器");
  297 + return;
  298 + }
  299 + //红外遥控设备id
  300 + String conDevId = hwClint.getConDevId();
  301 + SchduledTasks.closeDevMap.put(conDevId,new Date());
  302 + }
  303 + }
  304 +
  305 + private CalDevBeContrl getCalDevBeCon(String assDevice) {
  306 + String[] assDeviceIds = assDevice.split(",");
  307 + List<CalDevBeContrl> devBeContrlList = new ArrayList<>();
  308 + for (int i = 0; i < assDeviceIds.length; i++) {
  309 + String deviceId = assDeviceIds[i];
  310 + //获取温感设备
  311 + List<CalDevContrl> calDevContrl = deviceDao.getTempCalWitdDevid(deviceId);
  312 + if(!CollectionUtils.isEmpty(calDevContrl)){
  313 + for (int j = 0; j < calDevContrl.size(); j++) {
  314 + //温感设备
  315 + CalDevContrl calDevContrl1 = calDevContrl.get(j);
  316 + if (!calDevContrl1.getDevBeId().equals("0")) {
  317 + String[] calDevBeIdStr = calDevContrl1.getDevBeId().split(",");
  318 + if (calDevBeIdStr != null) {
  319 + for (int k = 0; k < calDevBeIdStr.length; k++) {
  320 + //红外设备
  321 + CalDevBeContrl calDevBeContrl = deviceDao.getCalDevBeControl(calDevBeIdStr[k]);
  322 + devBeContrlList.add(calDevBeContrl);
  323 + }
  324 + }
  325 + }
  326 + }
  327 + }
  328 + }
  329 + if(devBeContrlList.size()>0){
  330 + return devBeContrlList.get(0);
  331 + }
  332 + return null;
307 333 }
308 334  
309   -
310 335 /**
311 336 * 根据温度变化控制设备
312 337 *
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/bean/CloseClintBean.java 0 → 100644
... ... @@ -0,0 +1,31 @@
  1 +package com.example.mypulsar.bean;
  2 +
  3 +import java.util.Date;
  4 +
  5 +/**
  6 + * 关闭设备实体
  7 + * @author xuquan
  8 + * @date 2021/2/26 10:10
  9 + */
  10 +
  11 +public class CloseClintBean {
  12 +
  13 + private Date startTime;
  14 + private String hasPerson;
  15 +
  16 + public String getHasPerson() {
  17 + return hasPerson;
  18 + }
  19 +
  20 + public void setHasPerson(String hasPerson) {
  21 + this.hasPerson = hasPerson;
  22 + }
  23 +
  24 + public Date getStartTime() {
  25 + return startTime;
  26 + }
  27 +
  28 + public void setStartTime(Date startTime) {
  29 + this.startTime = startTime;
  30 + }
  31 +}
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/dao/DeviceDao.java
... ... @@ -30,7 +30,7 @@ public interface DeviceDao {
30 30 int updateStatus(@Param("clint_id") String clint_id, @Param("isConnection") String isConnection, @Param("OnlineTime") String OnlineTime);
31 31  
32 32  
33   - @Select("select clint_id from WL_Attendance where clint_id = #{clint_id}")
  33 + @Select("select top 1 name from WL_Attendance where clint_id = #{clint_id}")
34 34 String selectAttendance(@Param("clint_id") String clint_id);
35 35  
36 36 @Select("select * from Iot_Device where devId = #{devId}")
... ... @@ -41,7 +41,7 @@ public interface DeviceDao {
41 41 List<CalDevContrl> getCalDevContrl();
42 42  
43 43  
44   - @Select("select * from CalDevContrl where Devid = #{Devid} and Status = 1")
  44 + @Select("select * from CalDevContrl where Devid = #{Devid} and ModelType =10 and Status = 1")
45 45 List<CalDevContrl> getCalDevContrlWidthDevId(@Param("Devid") String devId);
46 46  
47 47 @Select("select * from CalDevBeContrl where State = 1 and Id = #{Id}")
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/task/SchduledTasks.java
... ... @@ -7,19 +7,22 @@ import com.example.mypulsar.bean.Wl_LinkModeBean;
7 7 import com.example.mypulsar.campusDao.CampusDao;
8 8 import com.example.mypulsar.dao.DeviceDao;
9 9 import com.example.mypulsar.utils.HttpUtil;
10   -import org.slf4j.Logger;
11   -import org.slf4j.LoggerFactory;
  10 +import lombok.extern.slf4j.Slf4j;
  11 +import org.dom4j.Document;
  12 +import org.dom4j.DocumentException;
  13 +import org.dom4j.DocumentHelper;
  14 +import org.dom4j.Element;
12 15 import org.springframework.beans.factory.annotation.Autowired;
13 16 import org.springframework.boot.configurationprocessor.json.JSONArray;
14 17 import org.springframework.boot.configurationprocessor.json.JSONException;
15 18 import org.springframework.scheduling.annotation.Scheduled;
16 19 import org.springframework.stereotype.Component;
17 20  
18   -import java.io.*;
19 21 import java.text.ParseException;
20 22 import java.text.SimpleDateFormat;
21 23 import java.util.*;
22 24  
  25 +@Slf4j
23 26 @Component
24 27 public class SchduledTasks {
25 28  
... ... @@ -29,122 +32,150 @@ public class SchduledTasks {
29 32 @Autowired
30 33 CampusDao campusDao;
31 34  
32   - private Logger logger = LoggerFactory.getLogger(SchduledTasks.class);
33   -
34 35 public static Map<String, Date> conDevMap = new HashMap<>();//存储被控制的设备
35 36  
36   - private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  37 + public static Map<String, Date> closeDevMap = new HashMap<>();
37 38  
38   - SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm");
39   - SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("HH:mm");
  39 + private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
40 40  
41 41 private String closeModel = "{\"mode\":\"0\",\"power\":\"0\",\"remote_id\":\"%s\",\"remote_index\":\"11272\",\"temp\":\"26\",\"wind\":\"0\"}";
42 42  
43 43 boolean isClose = false;
  44 + boolean outClose= false;
44 45  
45 46 @Scheduled(fixedRate = 60000)
46 47 public void closeAttend() {
47   -
48   - logger.error("定时任务执行中......" + conDevMap.size());
49   -
50   - //遍历已经开启的空调设备
51   - Iterator iterator = conDevMap.entrySet().iterator();
52   -
53   - while (iterator.hasNext()) {
54   -
55   -
56   - Map.Entry entry = (Map.Entry) iterator.next();
57   - String conBeDeviceId = (String) entry.getKey();
58   -
59   - logger.error("在线设备:" + conBeDeviceId + "---在线时间:" + simpleDateFormat.format(entry.getValue()));
60   -
61   - Date date = (Date) entry.getValue();
62   - System.out.println("更新设备时间差值:" + ((new Date().getTime()) - date.getTime()));
63   - if (((new Date().getTime()) - date.getTime()) > 10 * 60 * 1000) {//大于十分钟后关闭设备
64   -
65   - String conValue = getRemoteId(deviceDao.getConBeValue(conBeDeviceId));
66   - String name = deviceDao.getWLName(conBeDeviceId);
67   - HttpUtil.addAirCode(String.format(closeModel, conValue), conBeDeviceId, name);
68   - conDevMap.remove(conBeDeviceId);
  48 + log.info(".......定时任务执行设备关闭.......,有在线遥控设备数量" + closeDevMap.size());
  49 + Iterator<Map.Entry<String, Date>> it = closeDevMap.entrySet().iterator();
  50 + while(it.hasNext()) {
  51 + Map.Entry<String, Date> entry = it.next();
  52 + String conBeDeviceId = entry.getKey();
  53 + if(conDevMap.containsKey(conBeDeviceId) && conDevMap.get(conBeDeviceId)!=null){
  54 + Date openTime = entry.getValue();
  55 + log.info("在线设备:" + conBeDeviceId + "---在线时间:" + simpleDateFormat.format(entry.getValue())+"当前时间:"+ simpleDateFormat.format(new Date()));
  56 + //大于十分钟后关闭设备
  57 + if(((new Date().getTime()) - openTime.getTime()) > 5 * 60 * 1000) {
  58 + String conValue = getRemoteId(deviceDao.getConBeValue(conBeDeviceId));
  59 + String name = deviceDao.getWLName(conBeDeviceId);
  60 + log.info("开始执行关闭命令, remote_id: {}",conValue);
  61 + String response = HttpUtil.addAirCode(String.format(closeModel, conValue), conBeDeviceId, name);
  62 + try{
  63 + // 将字符串转为XML
  64 + Document doc = DocumentHelper.parseText(response);
  65 + // 获取根节点
  66 + Element rootElt = doc.getRootElement();
  67 + // 拿到head节点下的子节点title值
  68 + String title = rootElt.elementTextTrim("code");
  69 + log.info("请求结果:" + title);
  70 + if(title.equals("200")){
  71 + conDevMap.remove(conBeDeviceId);
  72 + }
  73 + } catch (DocumentException e) {
  74 + e.printStackTrace();
  75 + } catch (Exception e) {
  76 + e.printStackTrace();
  77 + }
  78 + }
69 79 }
70   -
71 80 }
  81 + if (!outClose){
  82 + initWlModeOut();
  83 + }
  84 + log.info("关闭成功 :" + JSON.toJSONString(conDevMap));
  85 + }
72 86  
73   - int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
74 87  
75   - if (!isClose && hour < 23 && hour > 5)
  88 + @Scheduled(cron = "0 0/30 * * * ?")
  89 + public void closeAttendOut() {
  90 + int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
  91 + if (!isClose && hour < 23 && hour > 5){
76 92 initWlMode();
77   -
78   -
79   -
80   - /* int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
81   -
82   -
83   - System.out.println("关闭设备:" + hour);
84   -
85   - String inuserTime = deviceDao.getInuserTime();
86   - JSONArray jsonArray = JSON.parseArray(inuserTime);
87   -
88   - if (null==jsonArray)return;
89   - if (jsonArray!=null&&jsonArray.size()==0)return;
90   - JSONObject jsonObject = jsonArray.getJSONObject(0);
91   -
92   - String startTime = jsonObject.getString("StartTime");
93   - String endTime = jsonObject.getString("EndTime");
94   - Date startDate = null;
95   - try {
96   - startDate = simpleDateFormat1.parse(startTime);
97   - } catch (ParseException e) {
98   - e.printStackTrace();
99 93 }
100   - Date endDate = null;
101   - try {
102   - endDate = simpleDateFormat1.parse(endTime);
103   - } catch (ParseException e) {
104   - e.printStackTrace();
105   - }
106   - Date date = null;
107   - try {
108   - date = simpleDateFormat2.parse(new Date().getHours() + ":" + new Date().getMinutes());
109   - } catch (ParseException e) {
110   - e.printStackTrace();
111   - }
112   -
113   - long starTime = startDate.getTime();
114   - long indexTime = date.getTime();
115   - long entTime = endDate.getTime()+ 10 * 60 * 1000;//延迟十分钟
116   -
117   - if (!isClose) {
  94 + }
118 95  
119   - if (indexTime < starTime || indexTime >= entTime) {
120   - closeAllDevice();
  96 + /**
  97 + * 根据物联模板判断是否关闭
  98 + */
  99 + private void initWlModeOut() {
  100 + try {
  101 + isClose = true;
  102 + //获取有效的模板
  103 + List<Wl_LinkModeBean> wl_linkModes = deviceDao.getWL_LinkMode("1085");
  104 + if (null != wl_linkModes) {
  105 + for (int i = 0; i < wl_linkModes.size(); i++) {
  106 + Wl_LinkModeBean wl_linkModeBean = wl_linkModes.get(i);
  107 + String field = wl_linkModeBean.getField();//场地id
  108 + String[] fields = field.split(",");//所有场地
  109 + String weekTime = wl_linkModeBean.getWeekTime();//时间
  110 + try {
  111 + JSONArray jsonArray = new JSONArray(weekTime);
  112 + if (null != jsonArray) {
  113 + for (int j = 0; j < jsonArray.length(); j++) {
  114 + org.springframework.boot.configurationprocessor.json.JSONObject jsonObject = jsonArray.getJSONObject(j);
  115 + String weekStr = jsonObject.getString("Week");
  116 + String[] weeks = weekStr.split(",");
  117 + String endTime = jsonObject.getString("EndTime");
  118 + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
  119 + SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm:ss");
  120 + int indexWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
  121 + for (int k = 0; k < weeks.length; k++) {
  122 + int week = Integer.parseInt(weeks[k]);
  123 + if (week == indexWeek) {
  124 + try {
  125 + Date endDate = simpleDateFormat.parse(endTime);
  126 + Date date = simpleDateFormat1.parse(new Date().getHours() + ":" + new Date().getMinutes() + ":" + new Date().getSeconds());
  127 + Date endDate2 = getDateByTime(1,endDate);
  128 + long indexTime = date.getTime();
  129 + long entTime = endDate.getTime();
  130 + long entTime2 = endDate2.getTime();
  131 + //当前时间等于结束时间触发一次指令
  132 + if (indexTime > entTime && indexTime < entTime2) {
  133 + //关闭所有设备
  134 + for (int l = 0; l < fields.length; l++) {
  135 + String fieldId = fields[l];
  136 + //获取红外遥控器
  137 + List<Wl_Attendace> clintIds = deviceDao.getHWAttsWithRoomId(fieldId);
  138 + closeAllDevice(clintIds);
  139 + }
  140 + }
  141 + } catch (ParseException e) {
  142 + log.info("关闭设备异常: ",e);
  143 + }
  144 + }
  145 + }
  146 + }
  147 + }
  148 + } catch (JSONException e) {
  149 + log.info("关闭设备异常: ",e);
  150 + }
  151 + }
  152 + } else {
  153 + //获取红外遥控器
  154 + List<Wl_Attendace> clintIds = deviceDao.getHWIds();
  155 + closeAllDevice(clintIds);
121 156 }
122   -
123   -
  157 + } catch (Exception e) {
  158 + log.info("关闭设备异常: ",e);
124 159 }
  160 + isClose = false;//保证条件一直在执行
  161 + }
125 162  
126   - if (indexTime < entTime && indexTime >= starTime) {
127   - isClose = false;
128   - }*/
129   -
130   -
  163 + public static Date getDateByTime(int length, Date date){
  164 + Calendar cal = Calendar.getInstance();
  165 + cal.setTime(date);//设置起时间
  166 + cal.add(Calendar.MINUTE,length);//把日期往后增加
  167 + return cal.getTime();
131 168 }
132 169  
133 170 /**
134 171 * 根据物联模板判断是否关闭
135 172 */
136 173 private void initWlMode() {
137   -
138 174 try {
139   -
140   -
141 175 isClose = true;
142   -
143 176 //获取有效的模板
144 177 List<Wl_LinkModeBean> wl_linkModes = deviceDao.getWL_LinkMode("1085");
145   -
146 178 if (null != wl_linkModes) {
147   -
148 179 for (int i = 0; i < wl_linkModes.size(); i++) {
149 180  
150 181 Wl_LinkModeBean wl_linkModeBean = wl_linkModes.get(i);
... ... @@ -188,10 +219,8 @@ public class SchduledTasks {
188 219 long entTime = endDate.getTime();
189 220  
190 221 if (indexTime < starTime || indexTime > entTime) {//在时间之外
191   -
192 222 //关闭所有设备
193 223 for (int l = 0; l < fields.length; l++) {
194   -
195 224 String fieldId = fields[l];
196 225 //获取红外遥控器
197 226 List<Wl_Attendace> clintIds = deviceDao.getHWAttsWithRoomId(fieldId);
... ... @@ -199,21 +228,17 @@ public class SchduledTasks {
199 228 }
200 229  
201 230 }
202   -
203 231 } catch (ParseException e) {
204   - e.printStackTrace();
  232 + log.info("关闭设备异常: ",e);
205 233 }
206 234 }
207 235  
208 236 }
209   -
210 237 }
211 238 }
212   -
213 239 } catch (JSONException e) {
214   - e.printStackTrace();
  240 + log.info("关闭设备异常: ",e);
215 241 }
216   -
217 242 }
218 243 } else {
219 244 //获取红外遥控器
... ... @@ -221,26 +246,34 @@ public class SchduledTasks {
221 246 closeAllDevice(clintIds);
222 247 }
223 248 } catch (Exception e) {
224   - e.printStackTrace();
225   - logger.error(e.toString());
  249 + log.info("关闭设备异常: ",e);
226 250 }
227 251 isClose = false;//保证条件一直在执行
228   -
229 252 }
230 253  
231 254 private void closeAllDevice(List<Wl_Attendace> deviceIds) {
232   -
233   -// isClose = true;
234   -// System.out.println("关闭设备" + deviceIds.toString());
235 255 for (int i = 0; i < deviceIds.size(); i++) {
236 256 String deviceId = deviceIds.get(i).getClint_id();
237 257 String conValue = getRemoteId(deviceDao.getConBeValue(deviceId));
238 258 String devName = deviceIds.get(i).getName();
239   -
240   - HttpUtil.addAirCode(String.format(closeModel, conValue), deviceId, devName);
  259 + String response = HttpUtil.addAirCode(String.format(closeModel, conValue), deviceId, devName);
  260 + try{
  261 + // 将字符串转为XML
  262 + Document doc = DocumentHelper.parseText(response);
  263 + // 获取根节点
  264 + Element rootElt = doc.getRootElement();
  265 + // 拿到head节点下的子节点title值
  266 + String title = rootElt.elementTextTrim("code");
  267 + log.info("请求结果:" + title);
  268 + if(title.equals("200")){
  269 + conDevMap.remove(deviceId);
  270 + }
  271 + } catch (DocumentException e) {
  272 + e.printStackTrace();
  273 + } catch (Exception e) {
  274 + e.printStackTrace();
  275 + }
241 276 }
242   -
243   -
244 277 }
245 278  
246 279 /**
... ... @@ -249,7 +282,6 @@ public class SchduledTasks {
249 282 * @param conValue
250 283 */
251 284 private String getRemoteId(String conValue) {
252   -
253 285 try {
254 286 // System.out.println("conValue:" + conValue);
255 287 JSONObject jsonObject = JSON.parseObject(conValue);
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/utils/ControlUtils.java
1 1 package com.example.mypulsar.utils;
2 2  
3   -import com.example.mypulsar.bean.*;
  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 8 import com.example.mypulsar.dao.DeviceDao;
5 9 import com.example.mypulsar.task.SchduledTasks;
6   -import org.apache.pulsar.shade.com.google.gson.Gson;
  10 +import io.micrometer.core.instrument.util.JsonUtils;
  11 +import lombok.extern.slf4j.Slf4j;
7 12 import org.springframework.boot.configurationprocessor.json.JSONArray;
8 13 import org.springframework.boot.configurationprocessor.json.JSONException;
9 14 import org.springframework.boot.configurationprocessor.json.JSONObject;
  15 +import org.springframework.util.CollectionUtils;
10 16 import org.springframework.util.StringUtils;
  17 +import springfox.documentation.spring.web.json.Json;
11 18  
12 19 import java.text.ParseException;
13 20 import java.text.SimpleDateFormat;
14 21 import java.util.*;
15 22  
  23 +@Slf4j
16 24 public class ControlUtils {
17 25  
18 26 private Set<CalDevContrl> deviceSet = new HashSet<>();//保存符合条件的传感器
... ... @@ -26,6 +34,11 @@ public class ControlUtils {
26 34 */
27 35 public static Map<String, Integer> tempDevices = new HashMap<>();
28 36  
  37 + /**
  38 + * 存储人感
  39 + */
  40 + public static Map<String, Integer> umanDevices = new HashMap<>();
  41 +
29 42 public static ControlUtils getInstance(DeviceDao deviceDao) {
30 43 if (null == controlUtils) {
31 44 synchronized (ControlUtils.class) {
... ... @@ -36,10 +49,7 @@ public class ControlUtils {
36 49 }
37 50  
38 51 public ControlUtils(DeviceDao deviceDao) {
39   -
40 52 this.deviceDao = deviceDao;
41   -
42   -
43 53 }
44 54  
45 55 /**
... ... @@ -49,74 +59,57 @@ public class ControlUtils {
49 59 * @param deviceBean 温度传感器对象
50 60 */
51 61 public void conTemper(List<CalDevContrl> calDevContrls, DeviceBean deviceBean) {
52   -// System.out.println("传感器感应设备:deviceBean:" + deviceBean.toString());
53   - FileUtils.getInstance().writeLogs(deviceBean.toString() + "\r\n" + calDevContrls.toString(), FileUtils.mypulsar);
54   - for (CalDevContrl cal :
55   - calDevContrls) {
56   -
57   - String inUseTime = cal.getInUserTime();//联动的时间范围,[{"Week":"5","StartTime":"14:02","EndTime":"14:06"}]
58   -
59   - if (inUseTime == null) break;
60   -// System.out.println("inUseTime:"+inUseTime);
  62 + for (CalDevContrl cal : calDevContrls) {
61 63 try {
  64 + //联动的时间范围,[{"Week":"5","StartTime":"14:02","EndTime":"14:06"}]
  65 + if(StringUtils.isEmpty(cal.getInUserTime())){
  66 + continue;
  67 + }
  68 + String inUseTime =cal.getInUserTime();
62 69 JSONArray jsonArray = new JSONArray(inUseTime);
63   - FileUtils.getInstance().writeLogs(jsonArray.toString(), FileUtils.mypulsar);
  70 + log.info("控制设备ID: {}, 联动时间计划:{}",cal.getDevid(),jsonArray.toString());
64 71 for (int i = 0; i < jsonArray.length(); i++) {
65 72 JSONObject jsonObject = jsonArray.getJSONObject(i);
66   -
67 73 String weekStr = jsonObject.getString("Week");
  74 + //周
68 75 String[] weeks = weekStr.split(",");
69   -
  76 + //开始时间
70 77 String startTime = jsonObject.getString("StartTime");
71   -
72 78 String endTime = jsonObject.getString("EndTime");
73   -
74 79 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
75 80 SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("HH:mm:ss");
76   -
77 81 int indexWeek = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1;
78   - FileUtils.getInstance().writeLogs(weekStr.toString() + "当前周:" + indexWeek, FileUtils.mypulsar);
  82 + log.info("周计划日:{},当前日期:周:{}",weekStr,indexWeek);
79 83 for (int j = 0; j < weeks.length; j++) {
80 84 int week = Integer.parseInt(weeks[j]);
81 85 if (week == indexWeek) {
82   -
83 86 Date startDate = simpleDateFormat.parse(startTime);
84 87 Date endDate = simpleDateFormat.parse(endTime);
85 88 Date date = simpleDateFormat1.parse(new Date().getHours() + ":" + new Date().getMinutes() + ":" + new Date().getSeconds());
86   -
87 89 long starTime = startDate.getTime();
88   - long indexTime = date.getTime();
89 90 long entTime = endDate.getTime();
90   -// System.out.println("时间比对:starTime:" + starTime + "-----indexTime:" + indexTime + "----entTime:" + entTime);
91   - FileUtils.getInstance().writeLogs("开始时间:" + (indexTime < entTime) + "结束时间:" + (indexTime > starTime), FileUtils.mypulsar);
  91 + //当前系统时间
  92 + long indexTime = date.getTime();
92 93 if (indexTime < entTime && indexTime > starTime) {
93   - //比较器类型
  94 + log.info("时间符合:继续执行开启设备");
94 95 int compreType = cal.getCompreType();
95   -
96 96 switch (compreType) {
97 97 case 1://固定值
98 98 calCompre(cal, deviceBean);
99 99 break;
100 100 case 2://当天日期
101   -
102 101 break;
103 102 }
104 103 }
105   -
106 104 }
107 105 }
108   -
109   -// System.out.println("jsonObject:" + jsonObject.toString());
110 106 }
111   -
112 107 } catch (JSONException e) {
113 108 e.printStackTrace();
114 109 } catch (ParseException e) {
115 110 e.printStackTrace();
116 111 }
117   -
118 112 }
119   -
120 113 }
121 114  
122 115 /**
... ... @@ -124,55 +117,39 @@ public class ControlUtils {
124 117 * @param deviceBean 控制设备
125 118 */
126 119 private void calCompre(CalDevContrl cal, DeviceBean deviceBean) {
127   -
128   - int thrsoldValue = 0;
129   -
130   - FileUtils.getInstance().writeLogs(cal.toString(), "caldevCntrol.txt");
131   -
132   - if (cal.getModelType() == 10) {//人体感应
133   - if (cal.getThreValue().equals("有人")) thrsoldValue = 1;
134   - else {
135   - thrsoldValue = 0;
136   - if (cal.getThreValue().equals("无人")) return;
137   - //无人的时候不做处理 , 在定时服务中处理
138   -// return;
139   - }
140   - } else {
141   - //温度传感器 不直接计算
142   -// thrsoldValue = Integer.parseInt(cal.getThreValue());//设定的阈值
  120 + if (deviceBean.getValue().equals("无人")){
  121 + return;
143 122 }
144   -
145   - int value = Integer.parseInt(deviceBean.getValue());//实际的值
146   - System.out.println("thrsoldValue:" + thrsoldValue + " value:" + value + " cal:" + cal.toString());
147   - switch (cal.getComPre()) {
148   - case 1://大于
149   - if (value > thrsoldValue)//启动触发条件
150   - startDevice(cal);//开启联动的设备
151   - break;
152   - case 2://等于
153   -
154   - if (value == thrsoldValue)//有人的时候会执行
155   - {
156   - boolean temp = calTemp(cal.getAssDevice());
157   - System.out.println("--------------------温度是否达标:" + temp + "-------------");
158   - if (temp)//温度符合要求
159   - startDevice(cal);//开启联动的设备
  123 + log.info("温度是否达标:,关联设备:{}",cal.getAssDevice());
  124 +// //温度符合要求
  125 + boolean isok = false;
  126 + //人感设备对应的关联联动设备
  127 + if (!StringUtils.isEmpty(cal.getAssDevice())) {
  128 + String[] devices = cal.getAssDevice().split(",");
  129 + for (int i = 0; i < devices.length; i++) {
  130 + 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 + }
160 147 }
161   -
162   - break;
163   - case 3://小于
164   -
165   - if (value < thrsoldValue)
166   - startDevice(cal);//开启联动的设备
167   -
168   - break;
169   - default://不触发条件
170   -
171   - removeSet(cal);
172   -
173   - break;
  148 + }
  149 + if(isok){
  150 + startDevice(cal,deviceBean.getValue());
  151 + }
174 152 }
175   -
176 153 }
177 154  
178 155 /**
... ... @@ -183,64 +160,50 @@ public class ControlUtils {
183 160 private synchronized boolean calTemp(String assDevice) {
184 161 if (!StringUtils.isEmpty(assDevice)) {
185 162 String[] devices = assDevice.split(",");
186   - FileUtils.getInstance().writeLogs(assDevice + "设备数量:" + devices.length, "tempisOk.txt");
187 163 for (int i = 0; i < devices.length; i++) {
188   -
189 164 String device = devices[i];
190   - FileUtils.getInstance().writeLogs(device, "tempisOk.txt");
191   -
  165 + //湿度传感器设备
192 166 List<CalDevContrl> calDevContrls = deviceDao.getTempCalWitdDevid(device);
193   -
194   - for (int j = 0; j < calDevContrls.size(); j++) {
195   - String tempDevId = calDevContrls.get(j).getDevid();
196   - if (tempDevices.containsKey(tempDevId)) {//温度记录了
197   -
198   - int tempear = tempDevices.get(tempDevId);
199   -
200   - boolean temp = tempIsOk(tempear, calDevContrls.get(j));
201   - if (temp) return true;
202   -
203   - } else {//沒有記錄溫度按照30度
204   - boolean temp = tempIsOk(28, calDevContrls.get(j));
205   - if (temp) return true;
  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 + }
206 180 }
207   -
208 181 }
209   -
210   -
211 182 }
212   -
213 183 }
214   -
215 184 return false;
216 185 }
217 186  
  187 + /**
  188 + * 温度比较
  189 + * @param tempear
  190 + * @param calDevContrl
  191 + * @return
  192 + */
218 193 private boolean tempIsOk(int tempear, CalDevContrl calDevContrl) {
219   -
220   - FileUtils.getInstance().writeLogs(calDevContrl.toString(), "tempisOk.txt");
221   - if (calDevContrl.getComPre() != 2) {
222   - System.out.println("溫度比較:" + tempear + " 比較的值:" + calDevContrl.getThreValue() + " 比较方式:" + calDevContrl.getComPre());
223   -
224   - switch (calDevContrl.getComPre()) {
225   - case 1://小于
226   -
227   - if (tempear <= Integer.parseInt(calDevContrl.getThreValue())) {
228   - return true;
229   - }
230   -
231   - break;
232   - case 3://大于
233   -
234   - if (tempear >= Integer.parseInt(calDevContrl.getThreValue())) {
235   - return true;
236   - }
237   -
238   - break;
239   -
  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;
  199 + }
  200 + }
  201 + if(calDevContrl.getComPre() ==3){
  202 + if (tempear >= Integer.parseInt(calDevContrl.getThreValue())) {
  203 + log.info("温度比较: 实际温度:{},大于制制冷临界值:{},准备开启制冷。",tempear,calDevContrl.getThreValue());
  204 + return true;
240 205 }
241   -
242 206 }
243   -
244 207 return false;
245 208 }
246 209  
... ... @@ -268,101 +231,90 @@ public class ControlUtils {
268 231 *
269 232 * @param cal
270 233 */
271   - private void startDevice(CalDevContrl cal) {
272   -
  234 + private void startDevice(CalDevContrl cal,String value) {
  235 +// deviceSet.add(cal);
  236 + //设备类型 10:人感;9温感
273 237 int modelType = cal.getModelType();
274   -
275   - deviceSet.add(cal);
276   -
277   - String assDevice = cal.getAssDevice();//联动的设备id
278   -
279   -// boolean isExit = isExist(assDevice.split(","));
280   - boolean isExit = true;
281   -
282   - System.out.println("是否符合联动条件:" + isExit);
283   -
284   - if (isExit) {//符合联动条件
285   -
286   - List<CalDevBeContrl> calDevBeContrlList = getCalDevBeCon(assDevice);
287   -
288   - for (int i = 0; i < calDevBeContrlList.size(); i++) {
289   - CalDevBeContrl calDevBeContrl = calDevBeContrlList.get(i);
290   -// System.out.println("控制对象:" + calDevBeContrl.toString());
291   - String conDevId = calDevBeContrl.getConDevId();//被控制的设备id
292   - String conValue = calDevBeContrl.getConValue();
293   - String conCodes = calDevBeContrl.getConCode();
294   -
295   - String[] conCodeStr = conCodes.split(",");
296   -
297   - for (int j = 0; j < conCodeStr.length; j++) {
298   - String conCode = conCodeStr[j];
299   - switch (modelType) {
300   - case 1://开关
301   - HttpUtil.controlDev(conDevId, conCode, conValue);
302   - break;
303   - case 2://插座
304   - HttpUtil.controlDev(conDevId, conCode, conValue);
305   - break;
306   - case 3://排插
307   - HttpUtil.controlDev(conDevId, conCode, conValue);
308   - break;
309   - case 9://温度传感器
310   - break;
311   - case 10://人体感应触发空调开关
312   - case 12://万能遥控器
313   - if (SchduledTasks.conDevMap.containsKey(conDevId)) {//设备已开,更新时间
314   - SchduledTasks.conDevMap.put(conDevId, new Date());
315   - System.out.println("更新设备时间");
316   - } else {//开启设备
317   - SchduledTasks.conDevMap.put(conDevId, new Date());
318   - System.out.println("开启设备");
319   - String name = deviceDao.getWLName(conDevId);
320   - HttpUtil.addAirCode(conValue, conDevId, name);
321   - }
322   - break;
323   - }
  238 + //人感设备关联的遥控设备
  239 + String assDevice = cal.getAssDevice();
  240 + //取得红外遥控设备
  241 + List<CalDevBeContrl> calDevBeContrlList = getCalDevBeCon(assDevice);
  242 + if(CollectionUtils.isEmpty(calDevBeContrlList)){
  243 + log.info("未查询到控制红外遥控器");
  244 + return;
  245 + }
  246 + for (int i = 0; i < calDevBeContrlList.size(); i++) {
  247 + CalDevBeContrl calDevBeContrl = calDevBeContrlList.get(i);
  248 + //红外遥控设备id
  249 + String conDevId = calDevBeContrl.getConDevId();
  250 + String conCodes = calDevBeContrl.getConCode();
  251 + //遥控设备,执行命令
  252 + String conValue = calDevBeContrl.getConValue();
  253 + String[] conCodeStr = conCodes.split(",");
  254 + //设备名
  255 + 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;
324 284 }
325   -
326 285 }
327   -
328 286 }
329   -
330 287 }
331 288  
332 289 /**
333   - * 获取被控制的设备
334   - *
  290 + * 获取红外遥控控制的设备
335 291 * @param assDevice
336 292 * @return
337 293 */
338 294 private List<CalDevBeContrl> getCalDevBeCon(String assDevice) {
339   -
340 295 String[] assDeviceIds = assDevice.split(",");
341   -
342 296 List<CalDevBeContrl> devBeContrlList = new ArrayList<>();
343   -
344 297 for (int i = 0; i < assDeviceIds.length; i++) {
345 298 String deviceId = assDeviceIds[i];
346   -
347   - List<CalDevContrl> calDevContrl = deviceDao.getCalDevContrlWidthDevId(deviceId);
348   -
349   - for (int j = 0; j < calDevContrl.size(); j++) {
350   -
351   - CalDevContrl calDevContrl1 = calDevContrl.get(j);
352   -
353   - if (!calDevContrl1.getDevBeId().equals("0")) {
354   - String[] calDevBeIdStr = calDevContrl1.getDevBeId().split(",");
355   - if (calDevBeIdStr != null) {
356   - for (int k = 0; k < calDevBeIdStr.length; k++) {
357   - CalDevBeContrl calDevBeContrl = deviceDao.getCalDevBeControl(calDevBeIdStr[k]);
358   - devBeContrlList.add(calDevBeContrl);
  299 + //获取温感设备
  300 + List<CalDevContrl> calDevContrl = deviceDao.getTempCalWitdDevid(deviceId);
  301 + if(!CollectionUtils.isEmpty(calDevContrl)){
  302 + for (int j = 0; j < calDevContrl.size(); j++) {
  303 + //温感设备
  304 + CalDevContrl calDevContrl1 = calDevContrl.get(j);
  305 + if (!calDevContrl1.getDevBeId().equals("0")) {
  306 + String[] calDevBeIdStr = calDevContrl1.getDevBeId().split(",");
  307 + if (calDevBeIdStr != null) {
  308 + for (int k = 0; k < calDevBeIdStr.length; k++) {
  309 + //红外设备
  310 + CalDevBeContrl calDevBeContrl = deviceDao.getCalDevBeControl(calDevBeIdStr[k]);
  311 + devBeContrlList.add(calDevBeContrl);
  312 + }
359 313 }
360 314 }
361 315 }
362 316 }
363   -
364 317 }
365   -
366 318 return devBeContrlList;
367 319 }
368 320  
... ...
cloud/mypulsar/src/main/java/com/example/mypulsar/utils/HttpUtil.java
... ... @@ -3,6 +3,8 @@ package com.example.mypulsar.utils;
3 3 import com.example.mypulsar.bean.Command;
4 4 import com.example.mypulsar.bean.TuYaAirCondition;
5 5 import com.example.mypulsar.bean.TuYaCommand;
  6 +import com.example.mypulsar.task.SchduledTasks;
  7 +import lombok.extern.slf4j.Slf4j;
6 8 import org.springframework.boot.configurationprocessor.json.JSONException;
7 9 import org.springframework.boot.configurationprocessor.json.JSONObject;
8 10 import org.springframework.core.io.FileSystemResource;
... ... @@ -20,14 +22,12 @@ import java.io.File;
20 22 import java.io.InputStreamReader;
21 23 import java.net.HttpURLConnection;
22 24 import java.net.URL;
23   -import java.util.ArrayList;
24   -import java.util.HashMap;
25   -import java.util.List;
26   -import java.util.Map;
  25 +import java.util.*;
27 26  
28 27 /**
29 28 * http 工具类
30 29 */
  30 +@Slf4j
31 31 public class HttpUtil {
32 32  
33 33 public static String post(String requestUrl, String accessToken, String params)
... ... @@ -112,8 +112,8 @@ public class HttpUtil {
112 112  
113 113 String responseEntity = restTemplate.postForObject(url, tuYaCommand, String.class);
114 114  
115   - System.out.println("tuYaCommand:" + tuYaCommand.toString() + "----deviceId:" + deviceId);
116   - System.out.println("responseEntity:" + responseEntity);
  115 + log.info("tuYaCommand:" + tuYaCommand.toString() + "----deviceId:" + deviceId);
  116 + log.info("responseEntity:" + responseEntity);
117 117 return responseEntity.equals("1");
118 118 }
119 119  
... ... @@ -142,35 +142,34 @@ public class HttpUtil {
142 142 * @return
143 143 */
144 144 public static String addAirCode(String conValue, String conDevId, String conDevName) {
145   -// System.out.println("url-------------------------------conValue:" + conValue);
146   -
147   - String url = String.format("http://120.26.116.253:5555/TuYa/%s/addAirCode1", conDevId);
148   - RestTemplate restTemplate = new RestTemplate();
149   -
150   - TuYaAirCondition tuYaAirCondition = new TuYaAirCondition();
151   - String power = "";
152 145 try {
153   - JSONObject jsonObject = new JSONObject(conValue);
154   - power = jsonObject.getString("power");
155   - tuYaAirCondition.setMode(jsonObject.getString("mode"));
156   - tuYaAirCondition.setPower(power);
157   - tuYaAirCondition.setRemote_id(jsonObject.getString("remote_id"));
158   - tuYaAirCondition.setWind(jsonObject.getString("wind"));
159   - tuYaAirCondition.setTemp(jsonObject.getString("temp"));
160   - tuYaAirCondition.setRemote_index(jsonObject.getString("remote_index"));
161   -
162   -
163   - } catch (JSONException e) {
164   - e.printStackTrace();
  146 + String url = String.format("http://120.26.116.253:5555/TuYa/%s/addAirCode", conDevId);
  147 + RestTemplate restTemplate = new RestTemplate();
  148 + TuYaAirCondition tuYaAirCondition = new TuYaAirCondition();
  149 + String power = "";
  150 + try {
  151 + JSONObject jsonObject = new JSONObject(conValue);
  152 + power = jsonObject.getString("power");
  153 + tuYaAirCondition.setMode(jsonObject.getString("mode"));
  154 + tuYaAirCondition.setPower(power);
  155 + tuYaAirCondition.setRemote_id(jsonObject.getString("remote_id"));
  156 + tuYaAirCondition.setWind(jsonObject.getString("wind"));
  157 + tuYaAirCondition.setTemp(jsonObject.getString("temp"));
  158 + tuYaAirCondition.setRemote_index(jsonObject.getString("remote_index"));
  159 + } catch (JSONException e) {
  160 + log.info("控制失败:失败信息",e);
  161 + }
  162 + log.info("开始发送遥控命令至设备:请求地址: {},设备名称: {},设备ID: {},请求参数: {}",url,conDevName,conDevId,tuYaAirCondition.toString());
  163 + String response = restTemplate.postForObject(url, tuYaAirCondition, String.class);
  164 + log.info("返回结果:{}",response);
  165 +// FileUtils.getInstance().writeLogs(String.format("%s设备%s--控制%s", power.equals("1") ? "成功" : "失败", conDevName + "-" + conDevId, response), FileUtils.addAirCode);
  166 + return response;
  167 + }catch (Exception e){
  168 + log.info("请求红外控制指令失败:{}",e);
  169 + if(SchduledTasks.conDevMap.containsKey(conDevId)){
  170 + SchduledTasks.conDevMap.remove(conDevId);
  171 + }
165 172 }
166   -
167   -
168   - System.out.println("url-------------------------------:" + url + " conValue:" + tuYaAirCondition.toString());
169   - String response = restTemplate.postForObject(url, tuYaAirCondition, String.class);
170   - FileUtils.getInstance().writeLogs(String.format("%s设备%s--控制%s", power.equals("1") ? "成功" : "失败", conDevName + "-" + conDevId, response), FileUtils.addAirCode);
171   - System.out.println("response----------------------------------:" + response);
172   - return response;
173   -
  173 + return null;
174 174 }
175   -
176 175 }
... ...
cloud/mypulsar/src/main/resources/application.yaml
... ... @@ -2,6 +2,8 @@ server:
2 2 port: 10009
3 3  
4 4 spring:
  5 + profiles:
  6 + active: dev
5 7 application:
6 8 name: mypulsar
7 9 datasource:
... ...
cloud/mypulsar/src/main/resources/logback-spring.xml 0 → 100644
... ... @@ -0,0 +1,54 @@
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<configuration>
  3 + <!--
  4 + SpringBoot下,默认会加载logback-spring.xml,其他项目建议文件名改为logback.xml
  5 + 目录 org/springframework/boot/logging/logback/*.xml下有一些log的基础配置
  6 + 不是所有的项目都是Springboot,这里为了做成通用的logback.xml文件,对这里进行修改
  7 + -->
  8 + <!--<include resource="org/springframework/boot/logging/logback/defaults.xml" />-->
  9 +
  10 + <!--日志文件存储的基础路径: ${user.home} 为当前服务器用户主目录-->
  11 + <property name="LOG_PATH" value="D:/mypulsar-logs"/>
  12 +
  13 + <!--日志文件基础名称-->
  14 + <property name="BASE_FILE_NAME" value="mypulsar"/>
  15 + <property name="LOG_FILE_MAX_SIZE" value="10MB"/>
  16 + <property name="LOG_FILE_MAX_HISTORY" value="3"/>
  17 +
  18 + <!--控制台的输出-->
  19 + <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
  20 + <!-- 设置输出格式-->
  21 + <encoder>
  22 + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} \(L%line\) - %msg%n</pattern>
  23 + </encoder>
  24 + </appender>
  25 +
  26 + <!--文件输出-->
  27 + <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
  28 + <!--输出文件名-->
  29 + <file>${LOG_PATH}/${BASE_FILE_NAME}.log</file>
  30 + <!--日志文件格式-->
  31 + <encoder>
  32 + <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} \(L%line\) - %msg%n</pattern>
  33 + </encoder>
  34 +
  35 + <!-- 日志文件滚动策略-->
  36 + <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
  37 + <!--历史文件存放格式和路径:-->
  38 + <fileNamePattern>${LOG_PATH}/history/${BASE_FILE_NAME}.%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
  39 + <!-- 文件最大的大小-->
  40 + <maxFileSize>${LOG_FILE_MAX_SIZE}</maxFileSize>
  41 + <!-- 日志文件保留天数 -->
  42 + <maxHistory>${LOG_FILE_MAX_HISTORY}</maxHistory>
  43 + </rollingPolicy>
  44 + </appender>
  45 +
  46 + <!--测试环境和开发环境打印控制台输出-->
  47 + <springProfile name="test,dev">
  48 + <root level="INFO">
  49 + <appender-ref ref="console" />
  50 + <appender-ref ref="file" />
  51 + </root>
  52 + </springProfile>
  53 +
  54 +</configuration>
0 55 \ No newline at end of file
... ...