| 
					
				 | 
			
			
				@ -1,13 +1,22 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				package com.yihu.jw.care.service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.alibaba.fastjson.JSON; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.alibaba.fastjson.JSONArray; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.alibaba.fastjson.JSONObject; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.care.dao.device.DeviceDetailDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.care.dao.device.OnenetDeviceDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.care.dao.device.OnenetReceiveRecordDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.entity.care.device.OnenetDevice; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import com.yihu.jw.entity.care.device.OnenetReceiveRecord; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				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.stereotype.Service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.ArrayList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.HashMap; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				import java.util.Map; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				/** 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -25,23 +34,91 @@ public class OnenetService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private HttpClientUtil httpClientUtil; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private OnenetDeviceDao onenetDeviceDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private DeviceDetailDao deviceDetailDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private OnenetReceiveRecordDao onenetReceiveRecordDao; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private static final String MasterAPIkey ="Da0iDvhQ5H8OD6phWq=tMubBcBw="; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private static final String baseUrl = "http://api.heclouds.com"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public void receive(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    private static final String baseUrl = "https://api.heclouds.com"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 为了防止消息重推 这里只做记录不做业务处理 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * (onenet:应用服务器收到平台每一次推送请求后,需要在有限时间内返回响应(目前是5秒),且HTTP响应状态码应设置为200,否则平台会认为请求发送失败, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 进行消息重推。重推采用指数退避策略,每条消息最多重推16次。如果某条消息一直失败,那么会在接下来的2小时45分4秒重推16次,之后不再重试) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param body 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    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%"} 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            JSONObject jsonObject = JSON.parseObject(body); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            OnenetReceiveRecord record = new OnenetReceiveRecord(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            record.setContent(jsonObject.getJSONObject("msg").toJSONString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            record.setStatus(1); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            onenetReceiveRecordDao.save(record); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        }catch (Exception e){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 添加设备 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public void addDevice(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String url = baseUrl +"/devices"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<OnenetDevice> onenetDeviceList = onenetDeviceDao.findByDeviceId(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        List<OnenetDevice> onenetDevices = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        for(OnenetDevice device:onenetDeviceList){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                JSONObject json = new JSONObject(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                json.put("title",device.getName()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                json.put("protocol","LWM2M"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                JSONObject auth_info = new JSONObject(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                auth_info.put(device.getSn(),device.getImsi()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                json.put("auth_info",auth_info); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String result = apikeyPost(url,json.toJSONString()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                JSONObject jsonObject = JSON.parseObject(result); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                if(jsonObject.getInteger("errno")==0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String deviceId = jsonObject.getJSONObject("data").getString("device_id"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    String psk = jsonObject.getJSONObject("data").getString("psk"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    device.setDeviceId(deviceId); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    device.setPsk(psk); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    onenetDevices.add(device); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            }catch (Exception e){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                e.printStackTrace(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if(onenetDevices.size()>0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            onenetDeviceDao.save(onenetDevices); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * 同步设备 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public void sysDevice(){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String url = baseUrl +"/"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public void sysDevice(Integer page){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        page = page==null?1:page; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String url = baseUrl +"/devices?page="+page+"&per_page=100"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String result = apikeyGet(url); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        JSONObject jsonObject = JSON.parseObject(result); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        if(jsonObject.getInteger("errno")==0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            List<OnenetDevice> onenetDeviceList = new ArrayList<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("devices"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            for (int i=0;i<jsonArray.size();i++){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                JSONObject json = jsonArray.getJSONObject(0); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String sn = json.getString("rg_id"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                String id = json.getString("id"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                OnenetDevice device = onenetDeviceDao.findBySn(sn); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                if(device!=null){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    device.setDeviceId(id); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                    onenetDeviceList.add(device); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            if(onenetDeviceList.size()>0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				                onenetDeviceDao.save(onenetDeviceList); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@ -53,6 +130,21 @@ public class OnenetService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Map<String,String> headerMap = new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        headerMap.put("api-key", MasterAPIkey); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String result = httpClientUtil.sendGet(url,headerMap); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        logger.info("apikeyGet="+result); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * onenet请求post方法 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param url 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @param param 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     * @return 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    public String apikeyPost(String url,String param){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        Map<String,String> headerMap = new HashMap<>(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        headerMap.put("api-key", MasterAPIkey); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        String result = httpClientUtil.sendPost(url,param,headerMap); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        logger.info("apikeyPost="+result); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				        return result; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				} 
			 |