|  | @ -3,15 +3,23 @@ package com.yihu.jw.care.service;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.serializer.SerializerFeature;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.DeviceDetailDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.DeviceHealthIndexDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.OnenetDeviceDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.OnenetReceiveRecordDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.util.DeviceDataPushLogUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.util.SecurityOrderUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.device.DeviceHealthIndex;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.device.OnenetDevice;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.device.OnenetReceiveRecord;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.date.DateUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.http.HttpClientUtil;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.scheduling.annotation.Async;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
	
		
			
				|  | @ -35,28 +43,107 @@ public class OnenetService {
 | 
	
		
			
				|  |  |     private DeviceDetailDao deviceDetailDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private OnenetReceiveRecordDao onenetReceiveRecordDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HvDeviceService hvDeviceService;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DeviceDataPushLogUtil dataPushLogUtil;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DeviceHealthIndexDao deviceHealthIndexDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private SecurityOrderUtil orderUtil;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static final String MasterAPIkey ="Da0iDvhQ5H8OD6phWq=tMubBcBw=";
 | 
	
		
			
				|  |  |     private static final String baseUrl = "https://api.heclouds.com";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public static void main(String[] args) {
 | 
	
		
			
				|  |  |         String value = "001F0000161401000000000400005A1200000701C6FFADFFF900000E620245CA71";
 | 
	
		
			
				|  |  |         String order = value.substring(10,14);
 | 
	
		
			
				|  |  |         System.out.println(order);
 | 
	
		
			
				|  |  |         String gas_Level = value.substring(22,24);
 | 
	
		
			
				|  |  |         System.out.println(gas_Level);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 为了防止消息重推 这里只做记录不做业务处理
 | 
	
		
			
				|  |  |      * 为了防止消息重推 这里采取异步处理
 | 
	
		
			
				|  |  |      * (onenet:应用服务器收到平台每一次推送请求后,需要在有限时间内返回响应(目前是5秒),且HTTP响应状态码应设置为200,否则平台会认为请求发送失败,
 | 
	
		
			
				|  |  |      * 进行消息重推。重推采用指数退避策略,每条消息最多重推16次。如果某条消息一直失败,那么会在接下来的2小时45分4秒重推16次,之后不再重试)
 | 
	
		
			
				|  |  |      * @param body
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     @Async
 | 
	
		
			
				|  |  |     public void receive(String body){
 | 
	
		
			
				|  |  |         //{"msg":{"at":1630985527067,"imei":"868591057157041","type":1,"ds_id":"3200_0_5505","value":"001f0000041401000000000000005a1300000e01c6ffb1fffa00000e620245ca71","dev_id":768777611},"msg_signature":"qq8nsaH/QEUuDnjK+o8vgw==","nonce":"z8SlxYY%"}
 | 
	
		
			
				|  |  |         OnenetReceiveRecord record = new OnenetReceiveRecord();
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             JSONObject jsonObject = JSON.parseObject(body);
 | 
	
		
			
				|  |  |             OnenetReceiveRecord record = new OnenetReceiveRecord();
 | 
	
		
			
				|  |  |             record.setContent(jsonObject.getJSONObject("msg").toJSONString());
 | 
	
		
			
				|  |  |             JSONObject msg = jsonObject.getJSONObject("msg");
 | 
	
		
			
				|  |  |             String deviceSn = msg.getString("imei");
 | 
	
		
			
				|  |  |             OnenetDevice onenetDevice = onenetDeviceDao.findBySn(deviceSn);
 | 
	
		
			
				|  |  |             String categoryCode = "";
 | 
	
		
			
				|  |  |             String title = "";
 | 
	
		
			
				|  |  |             if(onenetDevice!=null){
 | 
	
		
			
				|  |  |                 categoryCode = onenetDevice.getCategoryCode();
 | 
	
		
			
				|  |  |                 if("14".equals(categoryCode)){
 | 
	
		
			
				|  |  |                     title = "可燃气体探测器报警信息接收";
 | 
	
		
			
				|  |  |                 }else if("15".equals(categoryCode)){
 | 
	
		
			
				|  |  |                     title = "烟探测器监测信息接收";
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             hvDeviceService.updContactStatus(deviceSn,1);
 | 
	
		
			
				|  |  |             dataPushLogUtil.savePushLog(deviceSn,msg.toJSONString(),title);
 | 
	
		
			
				|  |  |             //未来鹰设备 类型2是设备状态(不准)
 | 
	
		
			
				|  |  |             Integer type = msg.getInteger("type");
 | 
	
		
			
				|  |  |             String value = msg.getString("value");
 | 
	
		
			
				|  |  |             if(type==1){
 | 
	
		
			
				|  |  |                 String order = value.substring(10,14);
 | 
	
		
			
				|  |  |                 if("1401".equals(order)){
 | 
	
		
			
				|  |  |                     //自定义命令数据更新(心跳/设备状态上报)
 | 
	
		
			
				|  |  |                     String gas_Level = value.substring(22,24);
 | 
	
		
			
				|  |  |                     Integer gas = Integer.parseInt(gas_Level, 16);
 | 
	
		
			
				|  |  |                     if(gas>0){
 | 
	
		
			
				|  |  |                         //气体浓度大于0触发报警
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                         String sql = "select count(*) from base_device_health_index where device_sn = '"+deviceSn+"'" +
 | 
	
		
			
				|  |  |                                 " and value>0 and create_time>='"+DateUtil.getNextMinute(-60)+"' ";
 | 
	
		
			
				|  |  |                         Integer count = jdbcTemplate.queryForObject(sql,Integer.class);
 | 
	
		
			
				|  |  |                         if(count==0){
 | 
	
		
			
				|  |  |                             logger.info("气体浓度大于0触发报警,deviceSn:{}",deviceSn);
 | 
	
		
			
				|  |  |                             //1小时内不重复发起
 | 
	
		
			
				|  |  |                             JSONObject tmp = new JSONObject();
 | 
	
		
			
				|  |  |                             tmp.put("gas",gas);
 | 
	
		
			
				|  |  |                             orderUtil.createSecurityOrder(deviceSn,null,new JSONObject(),null,6,"11","preventGasLeakage",JSON.toJSONString(tmp, SerializerFeature.WriteMapNullValue));
 | 
	
		
			
				|  |  |                         }
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                     DeviceHealthIndex index = new DeviceHealthIndex();
 | 
	
		
			
				|  |  |                     index.setDeviceSn(deviceSn);
 | 
	
		
			
				|  |  |                     if("14".equals(categoryCode)){
 | 
	
		
			
				|  |  |                         index.setUnit("%LEL");
 | 
	
		
			
				|  |  |                         index.setDeviceType("1");
 | 
	
		
			
				|  |  |                     }else if("15".equals(categoryCode)){
 | 
	
		
			
				|  |  |                         index.setUnit("%");
 | 
	
		
			
				|  |  |                         index.setDeviceType("2");
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                     index.setValue(gas+"");
 | 
	
		
			
				|  |  |                     index.setRecordTime(DateUtil.getStringDate());
 | 
	
		
			
				|  |  |                     deviceHealthIndexDao.save(index);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             record.setContent(msg.toJSONString());
 | 
	
		
			
				|  |  |             record.setStatus(1);
 | 
	
		
			
				|  |  |             record.setCreateTime(new Date());
 | 
	
		
			
				|  |  |         }catch (Exception e){
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |             record.setStatus(2);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             onenetReceiveRecordDao.save(record);
 | 
	
		
			
				|  |  |         }catch (Exception e){
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 |