kongzy
2024-07-01 47a751cb301d05276ae5d75145d57b2d090fe4e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.nanometer.smartlab.api.schedule;
 
import com.alibaba.fastjson.JSON;
import com.nanometer.smartlab.entity.BaseMeta;
import com.nanometer.smartlab.service.*;
import com.nanometer.smartlab.util.Constants;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
 
import javax.annotation.Resource;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
 
/**
 * Created by Caspar on 18/4/22.
 */
@Component
//@EnableScheduling
public class ReagentSyncSchedule {
 
    private static Logger logger = Logger.getLogger(ReagentSyncSchedule.class);
 
    @Value("${reagent.flow.url}")
    private String reagentFlowUrl;
    @Value("${reagent.status.url}")
    private String reagentStatusUrl;
    @Value("${sensors.url}")
    private String reagentSensorsUrl;
    @Lazy
    @Resource
    private BaseMetaService baseMetaService;
    @Lazy
    @Resource
    private OpeReagentStatusService reagentStatusService;
    @Lazy
    @Resource
    private OpeUseFlowService opeUseFlowService;
    @Lazy
    @Resource
    private SysContainerSensorsService sysContainerSensorsService;
    @Lazy
    @Resource
    private SysUserService sysUserService;
 
    private static String reagentStatusLastdate = null;
    private static String reagentFlowLastdate = null;
 
    //@Scheduled(fixedDelayString = "${reagent.status.schedule.delay}")
//    public void syncReagentStatus(){
//        Date current = new Date();
//        BaseMeta baseMeta = null;
//        baseMeta = baseMetaService.getBaseMeta("syncreagentstatuslastdata");
//        if(reagentStatusLastdate == null) {
//            reagentStatusLastdate = baseMeta.getMetaValue();
//        }
//
//        RestTemplate client = new RestTemplate();
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//        MultiValueMap<String, String> varParams= new LinkedMultiValueMap<String, String>();
//        varParams.add("startTime",reagentStatusLastdate);
//        String lastDate = DateFormatUtils.format(current, Constants.TIME_PATTERN_YYYY_MM_DD_HH_MM_SS);
//        varParams.add("endTime",lastDate);
//
//        logger.info("startTime: "+reagentStatusLastdate+"lastDate "+lastDate);
//
//        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(varParams, headers);
//        ResponseEntity<LinkedHashMap> response = client.postForEntity(reagentStatusUrl, requestEntity , LinkedHashMap.class );
//        List<LinkedHashMap> data = (ArrayList)response.getBody().get("data");
//        if(CollectionUtils.isEmpty(data)==false){
//            logger.info("result data: "+ JSON.toJSONString(data));
//            reagentStatusService.syncOpeReagentStatus(data);
//        }
//        reagentStatusLastdate = lastDate;
//        baseMeta.setMetaValue(reagentStatusLastdate);
//        baseMetaService.updateBaseMeta(baseMeta);
//    }
//
//    //@Scheduled(fixedDelayString = "${reagent.flow.schedule.delay}")
//    public void syncReagentFlow() throws ParseException{
//        Date current = new Date();
//        BaseMeta baseMeta = null;
//        baseMeta = baseMetaService.getBaseMeta("syncreagentflowlastdata");
//        if(reagentFlowLastdate == null) {
//            reagentFlowLastdate = baseMeta.getMetaValue();
//        }
//
//        RestTemplate client = new RestTemplate();
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//        MultiValueMap<String, String> varParams= new LinkedMultiValueMap<String, String>();
//        varParams.add("startTime",reagentFlowLastdate);
//        String lastDate = DateFormatUtils.format(current, Constants.TIME_PATTERN_YYYY_MM_DD_HH_MM_SS);
//        varParams.add("endTime",lastDate);
//
//        logger.info("startTime: "+reagentStatusLastdate+"lastDate "+lastDate);
//
//        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(varParams, headers);
//        ResponseEntity<LinkedHashMap> response = client.postForEntity(reagentFlowUrl, requestEntity , LinkedHashMap.class );
//        List<LinkedHashMap> data = (ArrayList)response.getBody().get("data");
//        if(CollectionUtils.isEmpty(data)==false){
//            opeUseFlowService.syncReagentFlow(data);
//            //[{"status":1,"containerCode":"c1000-001","updateTime":"2018-04-15 11:23:49","reagentCode":"r2010-0001","idcard":"u001"}....]
//            logger.info("result data: "+JSON.toJSONString(data));
//        }
//        baseMeta.setMetaValue(lastDate);
//        reagentFlowLastdate = lastDate;
//        baseMetaService.updateBaseMeta(baseMeta);
//    }
//
//    //@Scheduled(fixedDelayString = "${sensors.schedule.delay}")
//    public void syncSensors() {
//        Date current = new Date();
//        BaseMeta baseMeta = baseMetaService.getBaseMeta("syncsensorslastdata");
//        RestTemplate client = new RestTemplate();
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
//        MultiValueMap<String, String> varParams= new LinkedMultiValueMap<String, String>();
//        varParams.add("startTime",baseMeta.getMetaValue());
//        varParams.add("endTime",DateFormatUtils.format(current,Constants.TIME_PATTERN_YYYY_MM_DD_HH_MM_SS));
//        HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(varParams, headers);
//        ResponseEntity<LinkedHashMap> response = client.postForEntity(reagentSensorsUrl, requestEntity , LinkedHashMap.class );
//        LinkedHashMap body = response.getBody();
//        List<LinkedHashMap> data = body == null ? null : (ArrayList) body.get("data");
//        if(!CollectionUtils.isEmpty(data)){
//            try {
//                logger.info("result data: " + JSON.toJSONString(data));
//                sysContainerSensorsService.syncSysContainerSensors(data);
//                //{"data": [{"containerCode": "c1000-003", "Temp": 28.0, "Flag": 16, "Humidity": 58, "updateTime": "2018-04-18 13:37:46", "voc1": 20}], "func": "sensors", "method": "POST", "message": "OK", "code": 0}
//                baseMeta.setMetaValue(DateFormatUtils.format(current, Constants.TIME_PATTERN_YYYY_MM_DD_HH_MM_SS));
//                baseMetaService.updateBaseMeta(baseMeta);
//            }catch (Exception e){
//                logger.error("error occured.", e);
//            }
//        }
//    }
//
//
//    @Scheduled(cron = "0 0 2 1 * ?")
//    public void updateUserPoint() {
//        logger.debug("updateUserPoint start,and now is "+new Date());
//        sysUserService.updateUserPointBySelective(20,null);
//        logger.debug("updateUserPoint end");
//    }
}