vince
2023-07-31 7ca617f98917b5270a280b61565758216b374fa8
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.ld.igds.protocol.bhzn.verb.analysis;
 
import com.alibaba.fastjson.JSONObject;
import com.ld.igds.common.CoreDeviceService;
import com.ld.igds.constant.DeviceStatus;
import com.ld.igds.io.constant.OrderRespEnum;
import com.ld.igds.io.notify.NotifyWebInvoker;
import com.ld.igds.io.request.ExeDevice;
import com.ld.igds.models.Device;
import com.ld.igds.models.DeviceSer;
import com.ld.igds.protocol.bhzn.verb.command.BaseRemoteImpl;
import com.ld.igds.protocol.bhzn.verb.dto.IoMessage;
import com.ld.igds.protocol.bhzn.verb.dto.Res247;
import com.ld.igds.protocol.bhzn.verb.server.BhznVerbServerUtils;
import com.ld.igds.temp.dto.TempParam;
import com.ld.igds.util.BytesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
/**
 * 设备操作类
 */
@Slf4j
@Component(AnalysisDevice.BEAN_ID)
public class AnalysisDevice extends BaseRemoteImpl {
 
    public static final String BEAN_ID = "bhzn.analysisDevice";
 
    @Autowired
    private CoreDeviceService deviceService;
    @Autowired
    private NotifyWebInvoker notifyInvoker;
 
 
 
    /**
     * 设备状态
     *
     * @param reMessage
     * @param ser
     */
    public void analysis(IoMessage reMessage, DeviceSer ser) {
        Res247 res247 = JSONObject.parseObject(reMessage.getContentStr(),Res247.class);
        AnalysisWin(ser,res247.getWin_Str());
        AnalysisFan(ser,res247.getFan_Str());
        AnalysisAir(ser,res247.getAir_Str());
        AnalysisLight(ser,res247.getLight_Str());
//        BhznVerbServerUtils.add2StatusMap(ser.getCompanyId(), ser.getId(),0, "");
//        }
        deviceService.updateStatus(ser.getCompanyId(), ser.getId(), BhznVerbServerUtils.getStatusMap());
//
        notifyInvoker.notifyAnalysisStatusSuccess(ser.getCompanyId(), ser.getId(), OrderRespEnum.MSG_SUCCESS, "设备状态查询成功并完成解析!");
    }
 
    /**
     * 解析窗户状态
     * @param ser
     * @param winStr
     */
    private void AnalysisWin(DeviceSer ser,Integer[] winStr){
        if(winStr == null || winStr.length == 0)return;
        for (int i=1;i<= winStr.length;i++) {
            BhznVerbServerUtils.add2StatusMap(ser.getCompanyId(), ser.getId(),i, getWinStatus(winStr[i-1]));
        }
    }
    /**
     * 解析风机状态
     * @param ser
     * @param fanStr
     */
    private void AnalysisFan(DeviceSer ser,Integer[] fanStr){
        if(fanStr == null || fanStr.length == 0)return;
        for (int i=1;i<= fanStr.length;i++) {
            BhznVerbServerUtils.add2StatusMap(ser.getCompanyId(), ser.getId(),i+20, getFanStatus(fanStr[i-1]));
        }
    }
    /**
     * 解析空调状态
     * @param ser
     * @param airStr
     */
    private void AnalysisAir(DeviceSer ser,Integer[] airStr){
        if(airStr == null || airStr.length == 0)return;
        Integer s= 0;
        for (int i=1;i<= airStr.length;i++) {
            List<Device> devices = deviceService.getCacheDeviceBySerId(ser.getCompanyId(),ser.getId());
            // 调整缓存
            if (null == devices || devices.isEmpty())
                return;
            // 更新缓存
            Device temp;
            s = airStr[i-1];
            for (int j = 0; j < devices.size(); j++) {
                if(i == (devices.get(j).getPassCode() - 40)){
                    // 4608 -12  00
                    String hexStr = BytesUtil.intToHexStr(s);
                    for (int k =0;k<4;k++){
                        if(hexStr.length() < 16){
                            hexStr = "0" + hexStr;
                        }
                    }
                    // 12
                    String l = hexStr.substring(4,6);
                    //00
                    String h = hexStr.substring(6,8);
                    String hb = BytesUtil.toBinary8String(BytesUtil.hexToInt(h));
                    //00000000 0000 0 000
                    Integer status = Integer.parseInt(hb.substring(4,5));
                    Integer t = BytesUtil.hexToInt(l);
                    BhznVerbServerUtils.add2StatusMap(ser.getCompanyId(), ser.getId(), devices.get(j).getId(), getLightStatus(status));
                    TempParam param = new TempParam();
                    param.setId( devices.get(j).getId());
                    param.setTargetTemp(t+"");
                    param.setTargetStatus(getLightStatus(status));
                    param.setTargetModel("01");
                    List<ExeDevice> exeDeviceList = new ArrayList<>();
                    ExeDevice dev = new ExeDevice();
                    dev.setSerId(ser.getId());
                    exeDeviceList.add(dev);
                    param.setDeviceList(exeDeviceList);
                    param.setCompanyId(ser.getCompanyId());
                    deviceService.updateTempControlInfo(param);
 
                    log.info("空调状态解析完成:"+param.toString());
                    //BhznVerbServerUtils.add2StatusMap(ser.getCompanyId(), ser.getId(),i+40, getWinStatus(airStr[i]));
 
                }
            }
                  }
    }
    /**
     * 解析照明状态
     * @param ser
     * @param lightStr
     */
    private void AnalysisLight(DeviceSer ser,Integer[] lightStr){
        if(lightStr == null || lightStr.length == 0)return;
        for (int i=1;i<= lightStr.length;i++) {
            BhznVerbServerUtils.add2StatusMap(ser.getCompanyId(), ser.getId(),i+60, getLightStatus(lightStr[i-1]));
        }
    }
 
    private String getWinStatus(Integer s){
        if(s == null)return DeviceStatus.ZERO.getCode();
        String hexStr = BytesUtil.intToHexStr(s);
        for (int i =0;i<4;i++){
            if(hexStr.length() < 4){
                hexStr = "0" + hexStr;
            }
        }
        String h = hexStr.substring(0,2);
        String l = hexStr.substring(2,4);
        h = BytesUtil.toBinary8String(BytesUtil.hexToInt(h));
        int kaidu = (BytesUtil.hexToInt(l));
        String temp = h.substring(6,8);
        if("00".equals(temp ) && kaidu == 100){
            return DeviceStatus.OPEN.getCode();
        }
        if("00".equals(temp ) && kaidu == 0){
            return DeviceStatus.CLOSE.getCode();
        }
        if("01".equals(temp )){
            return DeviceStatus.OPEN.getCode();
        }
        if("10".equals(temp )){
            return DeviceStatus.CLOSE.getCode();
        }
 
        return DeviceStatus.ZERO.getCode();
    }
    private String getFanStatus(Integer s){
        if(0 == s){
            return DeviceStatus.CLOSE.getCode();
        }
        if(1 == s){
            return DeviceStatus.OPEN.getCode();
        }
        return DeviceStatus.ZERO.getCode();
    }
 
    private String getLightStatus(Integer s){
        if(0 == s){
            return DeviceStatus.CLOSE.getCode();
        }
        if(1 == s){
            return DeviceStatus.OPEN.getCode();
        }
        return DeviceStatus.ZERO.getCode();
    }
 
}