2021与蓝度共同重构项目,服务端
liuhaonan
2022-01-06 490e61bcf958e840f221f8d21d0a54ac7c99a7c4
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
package com.sandu.ximon.admin.service;
 
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sandu.common.execption.BusinessException;
import com.sandu.common.service.impl.BaseServiceImpl;
import com.sandu.ximon.admin.dto.LightTaskDto;
import com.sandu.ximon.admin.manager.iot.frame.FrameBuilder;
import com.sandu.ximon.admin.manager.iot.frame.IRequestFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.request.A5LightTimerReqInnerFrame;
import com.sandu.ximon.admin.manager.iot.frame.inner.response.A5LightTimerRespInnerFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.dto.WrapResponseCommonFrame;
import com.sandu.ximon.admin.manager.iot.rrpc.enums.A5OrderEnum;
import com.sandu.ximon.admin.manager.iot.rrpc.mainboard.MainBoardInvokeSyncService;
import com.sandu.ximon.admin.param.LightTaskParam;
import com.sandu.ximon.admin.security.SecurityUtils;
import com.sandu.ximon.admin.utils.TaskOrderUtil;
import com.sandu.ximon.dao.domain.LightTask;
import com.sandu.ximon.dao.domain.LightTaskPoleRelation;
import com.sandu.ximon.dao.domain.LightTaskQuartz;
import com.sandu.ximon.dao.domain.Pole;
import com.sandu.ximon.dao.mapper.LightTaskMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
 
/**
 * @author chenjiantian
 * @date 2021/12/15 16:33
 * 路灯任务操作
 */
@Service
@AllArgsConstructor
public class LightTaskService extends BaseServiceImpl<LightTaskMapper, LightTask> {
 
 
    private final LightTaskQuartzService lightTaskQuartzService;
    private final PoleService poleService;
    private final LightTaskPoleRelationService lightTaskPoleRelationService;
 
 
    /**
     * 新增路灯任务
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean addLightTask(LightTaskParam param) {
        if (StrUtil.length(param.getControlOrder()) % LightTaskParam.REQUEST_ORDER_LENGTH != 0) {
            throw new BusinessException("灯控命令格式不正确");
        }
        int week = 0;
        for (Integer w : param.getWeekList()) {
            week |= w;
        }
        LightTask lightTask = new LightTask();
        lightTask.setClientId(SecurityUtils.getClientId());
        lightTask.setTaskName(param.getTaskName());
        lightTask.setWeek(week);
        lightTask.setCloseOrder(param.getCloseOrder());
        lightTask.setOpenOrder(param.getOpenOrder());
        lightTask.setControlOrder(param.getControlOrder());
        lightTask.setCreateUser(SecurityUtils.getUsername());
        if (!save(lightTask)) {
            throw new BusinessException("保存路灯任务失败");
        }
        List<LightTaskQuartz> list = lightTaskQuartzService.addLightTaskQuartz(lightTask);
        if (CollectionUtil.isEmpty(list)) {
            throw new BusinessException("添加定时任务失败");
        }
        // 添加绑定灯杆
        if (!lightTaskPoleRelationService.updateLightTaskPoleRelation(lightTask.getTaskId(), param.getPoleIdList())) {
            throw new BusinessException("绑定灯杆失败");
        }
 
        List<Long> poleIdList = param.getPoleIdList();
        if (CollectionUtil.isNotEmpty(poleIdList)) {
            List<String> deviceCodeList = poleService.listDeviceCodeByIds(poleIdList);
            if (CollectionUtil.isEmpty(deviceCodeList)) {
                throw new BusinessException("找不到灯杆mac,无法发送定时任务");
            }
            // 获取定时灯控命令
            String framePayload = list.get(0).getFramePayload();
 
            // rrpc 发生定时命令
            for (String deviceCode : deviceCodeList) {
                sendTimeRRpc(framePayload, deviceCode);
            }
        }
        return true;
    }
 
    /**
     * 发送灯控请求
     *
     * @param framePayload 灯控参数
     * @param deviceCode   设备吗
     * @return 返回帧
     */
    public A5LightTimerRespInnerFrame sendTimeRRpc(String framePayload, String deviceCode) {
        IRequestFrame requestFrame = FrameBuilder.builderA5().innerFrame(new A5LightTimerReqInnerFrame(framePayload))
                .orderType(A5OrderEnum.REQUEST_LIGHT_DATA.getCode()).build();
        WrapResponseCommonFrame<A5LightTimerRespInnerFrame> responseCommonFrame = MainBoardInvokeSyncService.getInstance()
                .sendRRPC(deviceCode, requestFrame, A5LightTimerRespInnerFrame.class);
        System.out.println("发送结果");
        A5LightTimerRespInnerFrame responseInnerFrame = responseCommonFrame.getResponseInnerFrame();
        System.out.println(responseInnerFrame.getResponseStatus());
        return responseInnerFrame;
    }
 
    public List<LightTaskDto> listLightTask(int pageNo, int pageSize, String keyword) {
        LambdaQueryWrapper<LightTask> wrapper = Wrappers.lambdaQuery(LightTask.class);
        if (StrUtil.isNotBlank(keyword)) {
            wrapper.like(LightTask::getTaskName, keyword);
        }
        Long clientId = SecurityUtils.getClientId();
        if (clientId != null) {
            wrapper.eq(LightTask::getClientId, clientId);
        }
 
        PageHelper.startPage(pageNo, pageSize);
        List<LightTask> list = list(wrapper);
 
        Page<LightTaskDto> page = new Page<>();
        BeanUtils.copyProperties(list,page);
        for (LightTask lightTask : list) {
            LightTaskDto lightTaskDto = new LightTaskDto();
            BeanUtils.copyProperties(lightTask,lightTaskDto);
            lightTaskDto.setWeekList(TaskOrderUtil.parseLightWeek2List(lightTask.getWeek()));
            page.add(lightTaskDto);
        }
        return page;
    }
 
 
    @Transactional(rollbackFor = Exception.class)
    public boolean updateLightTask(Long taskId, LightTaskParam param) {
        if (StrUtil.length(param.getControlOrder()) % LightTaskParam.REQUEST_ORDER_LENGTH != 0) {
            throw new BusinessException("灯控命令格式不正确");
        }
        LightTask lightTask = getById(taskId);
        if (lightTask == null) {
            throw new BusinessException("找不到路灯任务");
        }
        int week = 0;
        for (Integer w : param.getWeekList()) {
            week |= w;
        }
        lightTask.setTaskName(param.getTaskName());
        lightTask.setWeek(week);
        lightTask.setControlOrder(param.getControlOrder());
        lightTask.setOpenOrder(param.getOpenOrder());
        lightTask.setCloseOrder(param.getCloseOrder());
        lightTask.setUpdateTime(LocalDateTime.now());
 
        if (!updateById(lightTask)) {
            throw new BusinessException("编辑任务失败");
        }
 
        List<LightTaskQuartz> list = lightTaskQuartzService.updateLightTaskQuartz(lightTask);
        if (CollectionUtil.isEmpty(list)) {
            throw new BusinessException("编辑定时任务失败");
        }
 
        // 添加绑定灯杆
        if (!lightTaskPoleRelationService.updateLightTaskPoleRelation(lightTask.getTaskId(), param.getPoleIdList())) {
            throw new BusinessException("绑定灯杆失败");
        }
        List<Long> poleIdList = param.getPoleIdList();
        if (CollectionUtil.isNotEmpty(poleIdList)) {
            List<String> deviceCodeList = poleService.listDeviceCodeByIds(poleIdList);
            if (CollectionUtil.isEmpty(deviceCodeList)) {
                throw new BusinessException("找不到灯杆mac,无法发送定时任务");
            }
            // 获取定时灯控命令
            String framePayload = list.get(0).getFramePayload();
 
            // rrpc 发生定时命令
            for (String deviceCode : deviceCodeList) {
                sendTimeRRpc(framePayload, deviceCode);
            }
        }
 
        return true;
    }
 
    @Transactional(rollbackFor = Exception.class)
    public boolean delLightTask(List<Long> taskIdList) {
        List<LightTask> lightTaskList = listByIds(taskIdList);
        if (CollectionUtil.isEmpty(lightTaskList)) {
            throw new BusinessException("找不到任务信息");
        }
 
        // 删除任务
        if (!removeByIds(taskIdList)) {
            throw new BusinessException("删除任务失败");
        }
 
        // 删除绑定灯杆
        if (!lightTaskPoleRelationService.remove(Wrappers.lambdaQuery(LightTaskPoleRelation.class).in(LightTaskPoleRelation::getTaskId, taskIdList))) {
            throw new BusinessException("删除绑定灯杆失败");
        }
 
        // 删除定时器
        if (!lightTaskQuartzService.removeLightTaskQuartz(taskIdList)) {
            throw new BusinessException("删除定时器失败");
        }
 
        return true;
    }
 
    public Map<Object, Object> detailLightTask(Long taskId) {
        LightTask lightTask = getById(taskId);
        if(lightTask == null){
            throw new BusinessException("找不到任务");
        }
        List<Long> poleIdList = lightTaskPoleRelationService.listPoleIdByTaskId(taskId);
        List<Pole> poles = poleService.listByIds(poleIdList);
        return MapUtil.builder().put("task",lightTask).put("poles",poles).build();
    }
}