zhubaomin
2024-11-12 39be193dece5ad6314aa22a41a2c8675e8057be3
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
package com.dy.pipIrrRemote.rtuUpgrage;
 
import com.dy.common.aop.SsoAop;
import com.dy.common.multiDataSource.DataSourceContext;
import com.dy.common.softUpgrade.state.UpgradeTaskVo;
import com.dy.common.webUtil.BaseResponse;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.common.webUtil.ResultCodeMsg;
import com.dy.pipIrrGlobal.pojoRm.UgRtuProgram;
import com.dy.pipIrrGlobal.pojoRm.UgRtuTask;
import com.dy.pipIrrGlobal.rtuMw.ToRtuMwCom;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
 
/**
 * @Author: liurunyu
 * @Date: 2024/11/12 8:33
 * @Description
 */
@Slf4j
@Tag(name = "rtu远程升级任务", description = "rtu远程升级任务相关操作")
@RestController
@RequestMapping(path = "rtuUpgrade")
public class RtuUpgradeCtrl extends ToRtuMwCom {
    @Autowired
    private RtuUpgradeSv sv ;
 
    @Autowired
    private Environment env;
 
    @Autowired
    private RestTemplate restTemplate;
 
    /**
     * 下发rtu远程升级任务
     * @param id 任务id
     * @return 操作结果
     */
    @Operation(summary = "下发rtu远程升级任务", description = "下发rtu远程升级任务")
    @ApiResponses(value = {
            @ApiResponse(
                    responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE,
                    description = "返回操作成功与否数据(BaseResponse.content:Boolean)",
                    content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
                            schema = @Schema(implementation = Boolean.class))}
            )
    })
    @GetMapping(path = "/issuedTask")
    @SsoAop()
    public BaseResponse<Boolean> issuedTask(String id){
        if(id == null || !id.trim().equals("")){
            return BaseResponseUtils.buildError("任务id不能为空") ;
        }
        UgRtuTask tpo = this.sv.selectTaskById(id) ;
        if(tpo == null){
            return BaseResponseUtils.buildError("任务不存在") ;
        }
        if(tpo.isExecute == 1){
            return BaseResponseUtils.buildError("任务已下发,不能重复下发任务") ;
        }
        UgRtuProgram ppo = this.sv.selectProgramById(tpo.programId) ;
        if(ppo == null){
            return BaseResponseUtils.buildError("任务对应的程序不存在") ;
        }
 
        List<String> taskRtuAddrs = this.sv.selectAllRtuAddrByTask(id) ;
        if(taskRtuAddrs == null || taskRtuAddrs.size() == 0){
            return BaseResponseUtils.buildError("任务所涉及的控制器还未设置") ;
        }
 
        String ugCallbackUrl_rm = env.getProperty("mw." + DataSourceContext.get() + ".ugCallbackUrl_rm" );
        if(ugCallbackUrl_rm == null || ugCallbackUrl_rm.trim().equals("")){
            return BaseResponseUtils.buildError("未配置升级任务回调网址") ;
        }
 
        UpgradeTaskVo vo = new UpgradeTaskVo() ;
        this.valueFromPo(vo, tpo, ppo) ;
        vo.rtuAddrList = taskRtuAddrs ;
        vo.callbackWebUrl = ugCallbackUrl_rm ;
 
        String ugSendUrl = this.getToMwUgUrl(this.env) ;
        BaseResponse res = sendUpgradeTask2Mw(restTemplate, ugSendUrl, vo) ;
        if(res != null){
            if(res.isSuccess()){
                return BaseResponseUtils.buildSuccess(true) ;
            }else{
                log.error("通信中间件执行下发升级任务失败" + (res.getMsg() == null? "" : ("," + res.getMsg()))) ;
                return BaseResponseUtils.buildFail("通信中间件执行失败" + (res.getMsg() == null? "" : ("," + res.getMsg()))) ;
            }
        }else{
            log.error("通信中间件返回结果为null") ;
            return BaseResponseUtils.buildFail("通信中间件返回结果为null") ;
        }
    }
 
    private void valueFromPo(UpgradeTaskVo vo, UgRtuTask tpo, UgRtuProgram ppo){
        vo.id = "" + tpo.id ;
        vo.softFileName = ppo.hexFileName ;
        vo.softStoreAddr = ppo.storeRamAddr ;
        vo.softStartAddr = ppo.startRamAddr ;
        vo.softFileData = ppo.programBytes ;
        vo.softBytesCalculate = ppo.programCalculateBytes ;
        vo.softByteSrc16 = ppo.programCrc16 ;
    }
 
}