whycxzp
2023-11-30 096dccea11941767e67fca9236041389c07b8657
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
package com.whyc.util;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.whyc.constant.CMD_Constant;
import com.whyc.dto.Response;
import com.whyc.mapper.CustomMapper;
import org.springframework.util.ReflectionUtils;
 
import java.lang.reflect.Field;
 
public class OpCmdUtil {
 
    /**
     * 查询操作状态
     * @param mapper
     * @param queryWrapper
     * @param opCmd 执行的操作输入
     * @param ackTimeLimitSecond 最大限制返回时间,单位秒
     * @param battIndex 电池组index,可传入[0,1]或者null
     * @return 对比opCmd的操作输出,是否操作成功或者失败
     */
    public static Response getOpStatus(CustomMapper mapper, QueryWrapper<?> queryWrapper, Integer opCmd, Integer ackTimeLimitSecond, Integer battIndex) {
        try {
            int count = ackTimeLimitSecond * 1000 / 250;
            for (int i = 0; i < count; i++) {
                queryWrapper.select("op_cmd");
                if(battIndex != null){
                    queryWrapper.eq("batt_index",battIndex);
                }
                queryWrapper.last(" limit 1");
                Object obj = mapper.selectOne(queryWrapper);
                Class clazz = obj.getClass();
 
                Field fieldOpCmdAck = clazz.getDeclaredField("opCmd");
                ReflectionUtils.makeAccessible(fieldOpCmdAck);
                Integer opCmdAck = (Integer) fieldOpCmdAck.get(obj);
                boolean ackStatus = getAckStatus(opCmd, opCmdAck);
                if (ackStatus) {//操作成功
                    return new Response().set(1, true, "更新完成");
                }
                Thread.sleep(250);
            }
            return new Response().set(1, false, "网络连接失败,更新失败");
        }catch (NoSuchFieldException | IllegalAccessException | InterruptedException e){
            return new Response().set(0,e.toString());
        }
    }
 
    /**
     * 读取
     * @param mapper
     * @param queryWrapper
     * @param opCmd 执行的操作输入
     * @param ackTimeLimitSecond 最大限制返回时间,单位秒
     * @param battIndex 电池组index,可传入[0,1]或者null
     * @return
     */
    public static Response readByUpdateOpCmd(CustomMapper mapper, QueryWrapper<?> queryWrapper, Integer opCmd, Integer ackTimeLimitSecond,Integer battIndex) {
        try {
            //更新cmd
            UpdateWrapper<Object> update = Wrappers.update();
            update.set("op_cmd",opCmd);
            if(battIndex != null){
                update.set("batt_index",battIndex);
            }
            mapper.update(null,update);
            //读取cmd返回
            int count = ackTimeLimitSecond * 1000 / 250;
            QueryWrapper<?> query2 = null;
            if(battIndex != null){
                query2 = queryWrapper.clone();
                query2.eq("batt_index",battIndex);
            }
            for (int i = 0; i < count; i++) {
                queryWrapper.select("op_cmd");
                if(battIndex != null){
                    queryWrapper.eq("batt_index",battIndex);
                }
                queryWrapper.last(" limit 1");
 
                Object obj = mapper.selectOne(queryWrapper);
                Class clazz = obj.getClass();
 
                Field fieldOpCmdAck = clazz.getDeclaredField("opCmd");
                ReflectionUtils.makeAccessible(fieldOpCmdAck);
                Integer opCmdAck = (Integer) fieldOpCmdAck.get(obj);
                boolean ackStatus = getAckStatus(opCmd, opCmdAck);
                if (ackStatus) {//操作成功
                    Object data = mapper.selectList(query2).get(0);
                    return new Response().setII(1, true, data,"读取完成");
                }
                Thread.sleep(250);
            }
            return new Response().set(1, false, "网络连接失败,读取失败");
        }catch (NoSuchFieldException | IllegalAccessException | InterruptedException e){
            return new Response().set(0,e.toString());
        }
    }
 
    /**
     * 持续更新cmd和cmdAck对应关系
     * @param opCmd
     * @param opCmdAck
     * @return
     */
    private static boolean getAckStatus(Integer opCmd,Integer opCmdAck) {
        switch (opCmd){
            case CMD_Constant.CMD_SetACDCARK_Telemetry:{
                return opCmdAck == CMD_Constant.CMD_SetACDCARK_Telemetry_ACK;
            }
            case CMD_Constant.CMD_SetACDCARK_Signal:{
                return opCmdAck == CMD_Constant.CMD_SetACDCARK_Signal_ACK;
            }
            case CMD_Constant.CMD_GetAlarmParam: {
                return opCmdAck ==CMD_Constant.CMD_GetAlarmParam_ACK ;
            }
            case CMD_Constant.CMD_SetAlarmParam:{
                return opCmdAck == CMD_Constant.CMD_SetAlarmParam_ACK ;
            }
            default:
                return false;
        }
    }
}