2021与蓝度共同重构项目,服务端
liuhaonan
2022-10-14 09afe52c3ef2a09055fb621b25aa5416394a5382
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package com.sandu.ximon.admin.service;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.object.BaseConditionVO;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.common.util.SpringContextHolder;
import com.sandu.ximon.admin.manager.iot.frame.A5Frame;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5AtmosphereHeartBeatTimeReportInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5AtmosphereOperationReportInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.report.A5AtmosphereQueryVersionReportInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.request.AtmosphereQueryHeartBeatTimeReqInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.request.AtmosphereQueryVersionReqInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.request.AtmosphereRebootReqInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.request.AtmosphereSetHeartBeatTimeReqInnerFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.dto.CommonFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.enums.A5OrderEnum;
import com.sandu.ximon.admin.manager.iot.rrpc.enums.AtmoFunctionCode;
import com.sandu.ximon.admin.manager.iot.rrpc.mainboard.MainBoardInvokeSyncService;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.RedisUtils;
import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils;
import com.sandu.ximon.admin.utils.StringUtil;
import com.sandu.ximon.dao.bo.AirEquipmentNongGengBo;
import com.sandu.ximon.dao.domain.AirEquipment;
import com.sandu.ximon.dao.domain.AirEquipmentNongGeng;
import com.sandu.ximon.dao.mapper.AirEquipmentNongGengMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author ZZQ
 * @date 2022/4/25 14:43
 */
@Service
@AllArgsConstructor
public class AirEquipmentNongGengService extends BaseServiceImpl<AirEquipmentNongGengMapper, AirEquipmentNongGeng> {
    private final AirEquipmentNongGengMapper airEquipmentNongGengMapper;
 
 
    /**
     * 添加大气设备数据
     *
     * @return
     */
    public void addAirEquipment(AirEquipmentNongGeng airEquipmentNongGeng) {
        AirEquipmentNongGeng one = getOne(Wrappers.lambdaQuery(AirEquipmentNongGeng.class).eq(AirEquipmentNongGeng::getMac, airEquipmentNongGeng.getMac()));
        if (one == null) {
            //TODO
            save(airEquipmentNongGeng);
            /**
             * 添加农耕大气设备 日志记录开始
             */
            List<String> listCode = new ArrayList<>(1);
            listCode.add(airEquipmentNongGeng.getMac());
            String content = "{ 设备id:" + airEquipmentNongGeng.getId() + "设备code:" + airEquipmentNongGeng.getMac() +
                    "}";
 
            StoreOperationRecordsUtils.storeOperationData(listCode, null, "添加农耕大气设备", content);
            /**
             * 添加农耕大气设备 日志记录结束
             */
        }
    }
 
    /**
     * 模糊查询
     */
    public List<AirEquipmentNongGengBo> listAirEquipmentByKeyword(BaseConditionVO baseConditionVO, String keyword) {
        if (baseConditionVO != null) {
            PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize());
        }
        List<AirEquipmentNongGengBo> airEquipmentBos;
//        if (SecurityUtils.getClientId() == null) {
//            airEquipmentBos = airEquipmentNongGengMapper.listAirEquipmentByIds(keyword, null);
//        } else {
//            airEquipmentBos = airEquipmentNongGengMapper.listAirEquipmentByIds(keyword, SecurityUtils.getUserId());
//        }
 
        if (SecurityUtils.getClientId() == null) {
            airEquipmentBos = airEquipmentNongGengMapper.listAirEquipmentByIds(keyword, null);
        } else {
            airEquipmentBos = airEquipmentNongGengMapper.listAirEquipmentByIds(keyword, SecurityUtils.getUserId());
        }
 
        return null;
//        return airEquipmentBos;
    }
 
    /**
     * 大气设备列表(农耕,用于首页数据统计)
     */
    public List<AirEquipmentNongGengBo> listAirEquipmentOnHome() {
//        List<AirEquipmentNongGengBo> airEquipmentBos;
//        if (SecurityUtils.getClientId() == null) {
//            airEquipmentBos = airEquipmentNongGengMapper.listAirEquipmentByIds(null, null);
//        } else {
//            airEquipmentBos = airEquipmentNongGengMapper.listAirEquipmentByIds(null, SecurityUtils.getUserId());
//        }
 
//        return airEquipmentBos;
        return null;
    }
 
    /**
     * 删除大气设备数据
     *
     * @param Id
     * @return
     */
    public boolean deleteAirEquipment(Long Id) {
        AirEquipmentNongGeng airEquipmentNongGeng = getById(Id);
        if (airEquipmentNongGeng == null) {
            throw new BusinessException("找不到大气设备数据");
        }
 
        /**
         * 删除农耕大气设备 日志记录开始
         */
        List<String> listCode = new ArrayList<>(1);
        listCode.add(airEquipmentNongGeng.getMac());
        String content = "{ 设备id:" + airEquipmentNongGeng.getId() + "设备code:" + airEquipmentNongGeng.getMac() +
                "}";
 
        StoreOperationRecordsUtils.storeOperationData(listCode, null, "删除农耕大气设备", content);
        /**
         * 删除农耕大气设备 日志记录结束
         */
        return removeById(Id);
    }
 
    /**
     * 根据Mac大气设备数据详情
     */
    public AirEquipmentNongGeng getAirEquipment(String mac) {
        AirEquipmentNongGeng one = getOne(Wrappers.lambdaQuery(AirEquipmentNongGeng.class).eq(AirEquipmentNongGeng::getMac, mac));
        if (RedisUtils.getBean().get(AtmoFunctionCode.AIR_HEARTBEAT_NONG_GENG_STATE.getCode() + mac) != null) {
            one.setState(1);
        } else {
            one.setState(0);
        }
        return one;
    }
 
    /**
     * 设置大气设备心跳包间隔时间
     *
     * @param mac
     * @param time
     */
    public String setHeartBeatTime(String mac, Integer time) {
        if (StringUtil.strIsNullOrEmpty(mac)) {
            throw new BusinessException("灯杆mac参数错误!");
        }
        if (time == null) {
            throw new BusinessException("心跳包间隔时间不能为空!");
        }
 
        if (time < 30) {
            throw new BusinessException("心跳包间隔时间不能少于30s!");
        }
        AirEquipmentNongGeng airEquipmentNongGeng = SpringContextHolder.getBean(AirEquipmentNongGengService.class).
                getOne(Wrappers.lambdaQuery(AirEquipmentNongGeng.class).eq(AirEquipmentNongGeng::getMac, mac).last("limit 1"));
        if (airEquipmentNongGeng == null) {
            throw new BusinessException("大气设备不存在!");
        }
 
        AtmosphereSetHeartBeatTimeReqInnerFrame atmosphereSetHeartBeatTimeReqInnerFrame = new AtmosphereSetHeartBeatTimeReqInnerFrame(time);
        A5Frame a5Frame = new A5Frame(A5OrderEnum.REQUEST_ATMOSPHERE_DATA.getCode(), atmosphereSetHeartBeatTimeReqInnerFrame);
        System.out.println(a5Frame + "          --------a5Frame");
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(mac, a5Frame);
        System.out.println(commonFrame + "         -----------commonFrame");
 
        A5AtmosphereOperationReportInnerFrame a5AtmosphereOperationReportInnerFrame
                = new A5AtmosphereOperationReportInnerFrame().transformFrame(commonFrame.getPayload());
        if (a5AtmosphereOperationReportInnerFrame != null && a5AtmosphereOperationReportInnerFrame.isValidate()) {
            return a5AtmosphereOperationReportInnerFrame.getState();
        } else {
            throw new BusinessException("数据校验异常!");
        }
    }
 
    /**
     * 查询大气设备心跳包时间
     */
    public String QueryHeartBeatTime(String mac) {
        if (StringUtil.strIsNullOrEmpty(mac)) {
            throw new BusinessException("灯杆mac参数错误!");
        }
        AirEquipmentNongGeng airEquipmentNongGeng = SpringContextHolder.getBean(AirEquipmentNongGengService.class).
                getOne(Wrappers.lambdaQuery(AirEquipmentNongGeng.class).eq(AirEquipmentNongGeng::getMac, mac).last("limit 1"));
        if (airEquipmentNongGeng == null) {
            throw new BusinessException("大气设备不存在!");
        }
 
        AtmosphereQueryHeartBeatTimeReqInnerFrame atmosphereQueryHeartBeatTimeReqInnerFrame = new AtmosphereQueryHeartBeatTimeReqInnerFrame();
        A5Frame a5Frame = new A5Frame(A5OrderEnum.REQUEST_ATMOSPHERE_DATA.getCode(), atmosphereQueryHeartBeatTimeReqInnerFrame);
        System.out.println(a5Frame + "          --------a5Frame");
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(mac, a5Frame);
        System.out.println(commonFrame + "         -----------commonFrame");
 
        A5AtmosphereHeartBeatTimeReportInnerFrame a5AtmosphereHeartBeatTimeReportInnerFrame
                = new A5AtmosphereHeartBeatTimeReportInnerFrame().transformFrame(commonFrame.getPayload());
 
        if (a5AtmosphereHeartBeatTimeReportInnerFrame != null && a5AtmosphereHeartBeatTimeReportInnerFrame.isValidate()) {
            return "该大气设备心跳包间隔时间为:" + a5AtmosphereHeartBeatTimeReportInnerFrame.getIntervalTime() + "s";
        } else {
            throw new BusinessException("数据校验异常!");
        }
    }
 
    /**
     * 查询软硬件版本
     *
     * @param mac
     * @return
     */
    public String QueryVersion(String mac) {
        if (StringUtil.strIsNullOrEmpty(mac)) {
            throw new BusinessException("灯杆mac参数错误!");
        }
        AirEquipmentNongGeng airEquipmentNongGeng = SpringContextHolder.getBean(AirEquipmentNongGengService.class).
                getOne(Wrappers.lambdaQuery(AirEquipmentNongGeng.class).eq(AirEquipmentNongGeng::getMac, mac).last("limit 1"));
        if (airEquipmentNongGeng == null) {
            throw new BusinessException("大气设备不存在!");
        }
 
        AtmosphereQueryVersionReqInnerFrame atmosphereQueryVersionReqInnerFrame = new AtmosphereQueryVersionReqInnerFrame();
        A5Frame a5Frame = new A5Frame(A5OrderEnum.REQUEST_ATMOSPHERE_DATA.getCode(), atmosphereQueryVersionReqInnerFrame);
        System.out.println(a5Frame + "          --------a5Frame");
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(mac, a5Frame);
        System.out.println(commonFrame + "         -----------commonFrame");
 
        A5AtmosphereQueryVersionReportInnerFrame a5AtmosphereQueryVersionReportInnerFrame =
                new A5AtmosphereQueryVersionReportInnerFrame().transformFrame(commonFrame.getPayload());
        if (a5AtmosphereQueryVersionReportInnerFrame != null && a5AtmosphereQueryVersionReportInnerFrame.isValidate()) {
            return a5AtmosphereQueryVersionReportInnerFrame.getVersion();
        } else {
            throw new BusinessException("数据校验异常!");
        }
    }
 
    /**
     * 软重启
     *
     * @param mac
     * @return
     */
    public String Reboot(String mac) {
        if (StringUtil.strIsNullOrEmpty(mac)) {
            throw new BusinessException("灯杆mac参数错误!");
        }
 
        AirEquipmentNongGeng airEquipmentNongGeng = SpringContextHolder.getBean(AirEquipmentNongGengService.class).
                getOne(Wrappers.lambdaQuery(AirEquipmentNongGeng.class).eq(AirEquipmentNongGeng::getMac, mac).last("limit 1"));
        if (airEquipmentNongGeng == null) {
            throw new BusinessException("大气设备不存在!");
        }
 
        AtmosphereRebootReqInnerFrame atmosphereRebootReqInnerFrame = new AtmosphereRebootReqInnerFrame();
        A5Frame a5Frame = new A5Frame(A5OrderEnum.REQUEST_ATMOSPHERE_DATA.getCode(), atmosphereRebootReqInnerFrame);
        System.out.println(a5Frame + "          --------a5Frame");
        CommonFrame commonFrame = MainBoardInvokeSyncService.getInstance().sendRRPC(mac, a5Frame);
        System.out.println(commonFrame + "         -----------commonFrame");
 
        A5AtmosphereOperationReportInnerFrame a5AtmosphereOperationReportInnerFrame =
                new A5AtmosphereOperationReportInnerFrame().transformFrame(commonFrame.getPayload());
 
        if (a5AtmosphereOperationReportInnerFrame != null && a5AtmosphereOperationReportInnerFrame.isValidate()) {
            return a5AtmosphereOperationReportInnerFrame.getState();
        } else {
            throw new BusinessException("数据校验异常!");
        }
    }
 
}