|  | @ -0,0 +1,221 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.care.service.device;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.yihu.jw.care.config.AqgConfig;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.DeviceDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.care.dao.device.PatientDeviceDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.device.Device;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.care.device.DevicePatientDevice;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.common.GpsUtil;
 | 
	
		
			
				|  |  | import com.yihu.jw.util.http.HttpClientUtil;
 | 
	
		
			
				|  |  | import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | import org.slf4j.Logger;
 | 
	
		
			
				|  |  | import org.slf4j.LoggerFactory;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.data.redis.core.StringRedisTemplate;
 | 
	
		
			
				|  |  | import org.springframework.http.HttpEntity;
 | 
	
		
			
				|  |  | import org.springframework.http.HttpHeaders;
 | 
	
		
			
				|  |  | import org.springframework.http.HttpMethod;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Component;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | import org.springframework.util.LinkedMultiValueMap;
 | 
	
		
			
				|  |  | import org.springframework.util.MultiValueMap;
 | 
	
		
			
				|  |  | import javax.annotation.PostConstruct;
 | 
	
		
			
				|  |  | import java.text.MessageFormat;
 | 
	
		
			
				|  |  | import java.util.*;
 | 
	
		
			
				|  |  | import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | @Component
 | 
	
		
			
				|  |  | @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | public class PatientDeviceService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private static Logger logger = LoggerFactory.getLogger(PatientDeviceService.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private StringRedisTemplate redisTemplate;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HttpClientUtil httpClientUtil;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private PatientDeviceDao patientDeviceDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private GpsUtil gpsUtil;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private DeviceDao deviceDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     Map<Integer, String> relations = new HashMap<>();
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @PostConstruct
 | 
	
		
			
				|  |  |     public void init() {
 | 
	
		
			
				|  |  |         relations.put(0, "其他");
 | 
	
		
			
				|  |  |         relations.put(1, "父亲");
 | 
	
		
			
				|  |  |         relations.put(2, "母亲");
 | 
	
		
			
				|  |  |         relations.put(3, "老公");
 | 
	
		
			
				|  |  |         relations.put(4, "老婆");
 | 
	
		
			
				|  |  |         relations.put(5, "儿子");
 | 
	
		
			
				|  |  |         relations.put(6, "女儿");
 | 
	
		
			
				|  |  |         relations.put(7, "未知");
 | 
	
		
			
				|  |  |         relations.put(8, "免疫关联");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |      /******************************************* 爱牵挂设备start *****************************************************/
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取爱牵挂管理员cookie
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public synchronized String getCookie(){
 | 
	
		
			
				|  |  |         if(redisTemplate.hasKey(AqgConfig.redisKey)){
 | 
	
		
			
				|  |  |             return redisTemplate.opsForValue().get(AqgConfig.redisKey);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
 | 
	
		
			
				|  |  |         param.add("username",AqgConfig.username);
 | 
	
		
			
				|  |  |         param.add("password",AqgConfig.password);
 | 
	
		
			
				|  |  |         HttpEntity<com.alibaba.fastjson.JSONObject> response = httpClientUtil.cookiePostHttp(AqgConfig.login,param);
 | 
	
		
			
				|  |  |         HttpHeaders responseHeaders = response.getHeaders();
 | 
	
		
			
				|  |  |         String cookie =  responseHeaders.get("Set-Cookie").get(0);
 | 
	
		
			
				|  |  |         logger.info("cookie="+cookie);
 | 
	
		
			
				|  |  |         redisTemplate.opsForValue().set(AqgConfig.redisKey,cookie,AqgConfig.overTime, TimeUnit.HOURS);
 | 
	
		
			
				|  |  |         return cookie;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取爱牵挂设备信息
 | 
	
		
			
				|  |  |      * @param imei
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public com.alibaba.fastjson.JSONObject getAqgDeviceInfo(String imei) throws Exception{
 | 
	
		
			
				|  |  |         String url = MessageFormat.format(AqgConfig.deviceInfo, imei);
 | 
	
		
			
				|  |  |         HttpEntity<com.alibaba.fastjson.JSONObject> response = httpClientUtil.aqgCookieHttp(url, null, HttpMethod.GET, getCookie());
 | 
	
		
			
				|  |  |         com.alibaba.fastjson.JSONObject json = response.getBody();
 | 
	
		
			
				|  |  |         if(!json.getBoolean("success")){
 | 
	
		
			
				|  |  |              throw new Exception(json.getString("error_desc")) ;
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return json.getJSONObject("obj");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 修改删除 爱牵挂亲情号码
 | 
	
		
			
				|  |  |      * @param deviceSn
 | 
	
		
			
				|  |  |      * | name  |  否  | String       | 号码昵称                        |
 | 
	
		
			
				|  |  |     | num          |  否  | String       | 亲情号码,可以是手机或固话        |
 | 
	
		
			
				|  |  |     | dial_flag    |  否  | Int          | 0不设置为紧急呼叫号码,1设置为紧急呼叫号码       |
 | 
	
		
			
				|  |  |     | clear        |  否  | any          | 清空设置,[例如id值为2,如果发送的url带clear,系统只处理clear命令,则上述设置的亲情号2的参数无效,
 | 
	
		
			
				|  |  |     若该设备以前已设置亲情号码2,则以前设置的亲情号码2也会被清空]
 | 
	
		
			
				|  |  |     |
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public com.alibaba.fastjson.JSONObject updAqgDeviceSosInfo(String deviceSn,String seqid,String name,String num,String dial_flag,String clear){
 | 
	
		
			
				|  |  |         //先对移动接口限制做判断。
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         List<DevicePatientDevice> devices = patientDeviceDao.findByDeviceSn(deviceSn);
 | 
	
		
			
				|  |  |         String url ="";
 | 
	
		
			
				|  |  |         if ("4".equals(devices.get(0).getCategoryCode())){//手表
 | 
	
		
			
				|  |  |             Device device = deviceDao.findOne(devices.get(0).getDeviceId());
 | 
	
		
			
				|  |  |             if (device!=null){
 | 
	
		
			
				|  |  |                 if ("X1".equals(device.getModel())){
 | 
	
		
			
				|  |  |                     url = MessageFormat.format(AqgConfig.X1sos_numbers, deviceSn,seqid);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }else {
 | 
	
		
			
				|  |  |             url = MessageFormat.format(AqgConfig.sos_numbers, deviceSn,seqid);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
 | 
	
		
			
				|  |  |         if(StringUtils.isBlank(clear)){
 | 
	
		
			
				|  |  |             param.add("name", name);
 | 
	
		
			
				|  |  |             param.add("num", num);
 | 
	
		
			
				|  |  |             param.add("dial_flag", dial_flag);
 | 
	
		
			
				|  |  |         }else {
 | 
	
		
			
				|  |  |             param.add("clear",clear);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         HttpEntity<com.alibaba.fastjson.JSONObject> response = httpClientUtil.aqgCookieHttp(url, param, HttpMethod.POST, getCookie());
 | 
	
		
			
				|  |  |         return response.getBody();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取设备最新位置数据
 | 
	
		
			
				|  |  |      * @param deviceSn
 | 
	
		
			
				|  |  |      * @param type  0:Gps定位; 1:基站定位 (非必填)
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public com.alibaba.fastjson.JSONObject getDeviceLastLocation(String deviceSn,String type) throws Exception{
 | 
	
		
			
				|  |  |         String url = MessageFormat.format(AqgConfig.device_last_locationData, deviceSn);
 | 
	
		
			
				|  |  |         MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
 | 
	
		
			
				|  |  |         param.add("type", type);
 | 
	
		
			
				|  |  |         HttpEntity<com.alibaba.fastjson.JSONObject> response = httpClientUtil.aqgCookieHttp(url, param, HttpMethod.GET, getCookie());
 | 
	
		
			
				|  |  |         com.alibaba.fastjson.JSONObject responseJSON =  response.getBody();
 | 
	
		
			
				|  |  |         if (responseJSON.getBooleanValue("success")){
 | 
	
		
			
				|  |  |             if (responseJSON.containsKey("obj")&&responseJSON.getJSONObject("obj")!=null){
 | 
	
		
			
				|  |  |                 com.alibaba.fastjson.JSONObject tmp= responseJSON.getJSONObject("obj");
 | 
	
		
			
				|  |  |                 if (tmp.containsKey("locationdata")&&tmp.getJSONObject("locationdata")!=null){
 | 
	
		
			
				|  |  |                     com.alibaba.fastjson.JSONObject locationdataJson= tmp.getJSONObject("locationdata");
 | 
	
		
			
				|  |  |                     if (locationdataJson.containsKey("point")&&locationdataJson.getJSONObject("point")!=null){
 | 
	
		
			
				|  |  |                         com.alibaba.fastjson.JSONObject tmpPoint = locationdataJson.getJSONObject("point");
 | 
	
		
			
				|  |  |                         if (tmpPoint.containsKey("coordinates")&&tmpPoint.getJSONArray("coordinates")!=null){
 | 
	
		
			
				|  |  |                             com.alibaba.fastjson.JSONArray pointArr = tmpPoint.getJSONArray("coordinates");
 | 
	
		
			
				|  |  |                             Double lon = pointArr.getDouble(0);
 | 
	
		
			
				|  |  |                             Double lat = pointArr.getDouble(1);
 | 
	
		
			
				|  |  |                             com.alibaba.fastjson.JSONObject pointJson = gpsUtil.gcj02_To_Bd09(lat,lon);
 | 
	
		
			
				|  |  |                             locationdataJson.put("point",pointJson);
 | 
	
		
			
				|  |  |                         }
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return responseJSON;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      *    | enable       |  否  | Int          |  0,1 是否启用                        |
 | 
	
		
			
				|  |  |      *    | name         |  否  | String       |   安全区名称                           |
 | 
	
		
			
				|  |  |      *    | freq         |  否  | Boolean      |  0,触发一天;1,每日触发        |
 | 
	
		
			
				|  |  |      *    | time_begin   |  否  | Int          |  目标时间与当日0点之间相差的秒数。如8 a.m 8*60*60=28800 |n
 | 
	
		
			
				|  |  |      *    | time_end     |  否  | Int          |  同上                        |
 | 
	
		
			
				|  |  |      *    | safe_area    |  否  | String       | x1,y1;x2,y2;xn,yn  一组由';'分隔的坐标,坐标用','分隔。且需要呈闭环,如A,B,C,D4个点,则应该上传的数据为 A;B;C;D;A  |
 | 
	
		
			
				|  |  |      *    | clear        |  否  | any          | 如果指定clear参数,则上述参数全部无意义,全部重设为缺省值    |
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      * @throws Exception
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public com.alibaba.fastjson.JSONObject updateDeviceFenceArea(String deviceSn,Integer fenceNO,String enable,String name,String freq,
 | 
	
		
			
				|  |  |                                                                  String time_begin,String time_end,String safe_area ,String clear) throws Exception{
 | 
	
		
			
				|  |  |         List<DevicePatientDevice> devices = patientDeviceDao.findByDeviceSn(deviceSn);
 | 
	
		
			
				|  |  |         String url ="";
 | 
	
		
			
				|  |  |         if ("4".equals(devices.get(0).getCategoryCode())){//手表
 | 
	
		
			
				|  |  |             Device device = deviceDao.findOne(devices.get(0).getDeviceId());
 | 
	
		
			
				|  |  |             if (device!=null){
 | 
	
		
			
				|  |  |                 if ("X1".equals(device.getModel())){
 | 
	
		
			
				|  |  |                     url = MessageFormat.format(AqgConfig.X1fence_area, deviceSn,fenceNO);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }else {
 | 
	
		
			
				|  |  |             url = MessageFormat.format(AqgConfig.S3fence_area, deviceSn,fenceNO);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         MultiValueMap<String, String> param = new LinkedMultiValueMap<>();
 | 
	
		
			
				|  |  |         if(StringUtils.isBlank(clear)){
 | 
	
		
			
				|  |  |             if (StringUtils.isNotBlank(safe_area)){
 | 
	
		
			
				|  |  |                 String[] position = safe_area.split(";");
 | 
	
		
			
				|  |  |                 safe_area = "";
 | 
	
		
			
				|  |  |                 for (String tmp:position){
 | 
	
		
			
				|  |  |                     String[] point = tmp.split(",");
 | 
	
		
			
				|  |  |                     safe_area+= gpsUtil.bd09_To_Gcj02_str(Double.parseDouble(point[1]),Double.parseDouble(point[0]))+";";
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             param.add("name", name);
 | 
	
		
			
				|  |  |             param.add("freq", freq);
 | 
	
		
			
				|  |  |             param.add("enable",enable);
 | 
	
		
			
				|  |  |             param.add("time_begin", time_begin);
 | 
	
		
			
				|  |  |             param.add("time_end", time_end);
 | 
	
		
			
				|  |  |             param.add("safe_area", safe_area);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         }else {
 | 
	
		
			
				|  |  |             param.add("clear",clear);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         HttpEntity<com.alibaba.fastjson.JSONObject> response = httpClientUtil.aqgCookieHttp(url, param, HttpMethod.POST, getCookie());
 | 
	
		
			
				|  |  |         return response.getBody();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /******************************************* 爱牵挂设备end *****************************************************/
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |