| 
					
				 | 
			
			
				@@ -1,18 +1,29 @@ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 package com.zoniot.suntront.service.impl; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.alibaba.fastjson.JSON; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.zoniot.suntront.model.FileData; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.zoniot.suntront.repository.MeterReadRecord; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.zoniot.suntront.repository.MeterReadRecordRepository; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import com.zoniot.suntront.service.ReadingMeterService; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import com.zoniot.suntront.util.Md5Util; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import lombok.extern.slf4j.Slf4j; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.beans.factory.annotation.Autowired; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.beans.factory.annotation.Value; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.http.HttpEntity; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.http.HttpHeaders; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.http.MediaType; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.http.ResponseEntity; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import org.springframework.stereotype.Service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import org.springframework.web.client.RestTemplate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import javax.annotation.Resource; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.io.UnsupportedEncodingException; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.text.SimpleDateFormat; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.time.LocalDate; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 import java.time.format.DateTimeFormatter; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.Base64; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-import java.util.List; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import java.util.*; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+import static com.google.common.collect.Lists.newArrayList; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 @Slf4j 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 @Service 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -23,21 +34,67 @@ public class ReadingMeterServiceImpl implements ReadingMeterService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Value("${customerId}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     Integer customerId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Value("${appId}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    String appId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Value("${appSecret}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    String appSecret; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Value("${apiUrl}") 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    String apiUrl; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private RestTemplate restTemplate = new RestTemplate(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    @Autowired 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private MeterReadRecordRepository repository; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     @Override 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    public void getReadingMeter(LocalDate readingTime) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public void executeReadingMeter() { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String timeStamp=new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Integer date = Integer.valueOf(LocalDate.now().plusDays(-1).format(DateTimeFormatter.ofPattern("yyyyMMdd"))); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        List<MeterReadRecord> list = repository.findByCustomerIdAndReadDateAndReadStatus(customerId,date,"2"); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (list != null && list.size() > 0) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            log.info("meterReadRecord size={}",list.size()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            for (MeterReadRecord meterReadRecord : list) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                //log.info("meterReadRecord={}",meterReadRecord); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                FileData data = new FileData(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                data.setMeterAddr(meterReadRecord.getMeterNo()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                data.setMeterTypeId(24); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                data.setReadNumber(Double.valueOf(meterReadRecord.getLastValid())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                data.setReadDate(new Date()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                data.setMeterTime(meterReadRecord.getReadTime()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                data.setValveState(convertValveState(meterReadRecord.getValveState())); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                request(data,timeStamp); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private Integer convertValveState(Integer valveState){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (valveState != null) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if(valveState != 0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return  1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            }else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                return  2; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return 1; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    private String getAccessToken(String dateTime){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        String appId="hx123"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        String appSecret="1842e6e6725c5589"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        return appId + appSecret + dateTime; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private String getAccessToken(String timeStamp){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //使用 MD5 加密(appId + appSecret + 时间戳) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return Md5Util.MD5(appId + appSecret + timeStamp); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-    private String getAuthorization(String dateTime){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        String appId="hx123"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        String key = appId + ":" + dateTime; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private String getAuthorization(String timeStamp){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //使用 Base64 编码(appId + 冒号(英文) + 时间戳) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String key = appId + ":" + timeStamp; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             return Base64.getEncoder().encodeToString(key.getBytes("utf-8")); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } catch (UnsupportedEncodingException e) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -46,5 +103,28 @@ public class ReadingMeterServiceImpl implements ReadingMeterService { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         return null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				     } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private void request(FileData data,String timeStamp){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String tokenUrl = apiUrl + "/api/meters/UploadData"; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        HttpHeaders headers = new HttpHeaders(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        headers.setContentType(MediaType.APPLICATION_JSON); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        headers.add("AccessToken", getAccessToken(timeStamp)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        headers.add("Authorization", getAuthorization(timeStamp)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        String content = JSON.toJSONString(data); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("content={}",content); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        HttpEntity<String> request = new HttpEntity<>(content,headers); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ResponseEntity<String> responseEntity = null; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            responseEntity = restTemplate.postForEntity(tokenUrl, request, String.class); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        }catch (Exception e){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            log.error("request error e={} {}",e.getMessage(),e); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        log.info("responseEntity : {} {}", responseEntity.getStatusCodeValue(),responseEntity.getBody()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 |