医院废弃物信息化管理平台
“mitao”
2025-12-24 3763145f34d7706923d55edc32f5cfbc8bc08b92
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
package cn.besteam.modules.medical.biz;
 
import cn.besteam.common.constant.ErrorCode;
import cn.besteam.common.exception.BusinessException;
import cn.besteam.common.util.RedisUtil;
import cn.besteam.modules.medical.entity.MwMonitorDevice;
import cn.besteam.modules.system.entity.SysDict;
import cn.besteam.modules.system.entity.SysDictItem;
import cn.besteam.modules.system.service.ISysDictItemService;
import cn.besteam.modules.system.service.ISysDictService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import java.util.List;
 
@Service
@Slf4j
public class MonitorDeviceApiService {
 
    //登录
    private static final String LOGIN_URL = "https://video-1.scjakj.com/StandardApiAction_login.action?account=%s&password=%s";
 
    //添加设备
    private static final String ADD_DEVICE_URL = "https://video-1.scjakj.com/StandardApiAction_addDevice.action?jsession=%s&devIdno=%s&devType=7&factoryType=0&companyName=%s&account=%s&channelNum=%s";
 
    //绑定设备
    private static final String BIND_DEVICE_URL = "https://video-1.scjakj.com/StandardApiAction_addVehicle.action?jsession=%s&vehiIdno=%s&devIdno=%s&devType=7&factoryType=0&companyName=%s&account=%s";
 
    //删除设备
    private static final String DELETE_DEVICE_URL = "https://video-1.scjakj.com/StandardApiAction_deleteVehicle.action?jsession=%s&vehiIdno=%s&delDevice=1";
 
    //查询设备状态
    private static final String SELState_DEVICE_URL = "https://video-1.scjakj.com/StandardApiAction_getDeviceOlStatus.action?jsession=%s&devIdno=%s";
 
    //默认用户名、公司名
    private static final String DEFAULT_ACCOUNT = "scjakj", DEFAULT_COMPANY = "scjakj", DEFAULT_PASSWORD = "123456";
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private ISysDictService sysDictService;
 
    @Autowired
    private ISysDictItemService sysDictItemService;
 
    /**
     * 登录
     *
     * @return
     */
    public String getJSession() {
 
        MonitorConfig monitorConfig = getMonitorConfig();
        String key = "monitor:device:jsession";
        String jsession = redisUtil.get(key);
        if (jsession == null) {
            RestTemplate restTemplate = new RestTemplate();
            String completeUrl = String.format(LOGIN_URL, monitorConfig.getAccount(), monitorConfig.getPassword());
            log.info("视频服务器:登录接口传参数据:{}", completeUrl);
            ApiResult result = restTemplate.getForObject(completeUrl, ApiResult.class);
            log.info("视频服务器:登录接口返回数据:{}", result);
            if (result == null) {
                throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败");
            }
            if (result.getResult().equals(0)) {
                jsession = result.getJsession();
            } else {
                throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败");
            }
        }
        redisUtil.set(key, jsession, 300);
        return jsession;
    }
 
    /**
     * 绑定设备
     *
     * @param mwMonitorDevice
     * @return
     */
    public boolean bindDevice(MwMonitorDevice mwMonitorDevice) {
 
        MonitorConfig monitorConfig = getMonitorConfig();
        RestTemplate restTemplate = new RestTemplate();
        String completeUrl = String.format(BIND_DEVICE_URL, getJSession(), mwMonitorDevice.getDevIdno(), mwMonitorDevice.getDevIdno(), monitorConfig.getCompany(), monitorConfig.getAccount());
        log.info("视频服务器:绑定设备接口传参数据:{}", completeUrl);
        ApiResult result = restTemplate.getForObject(completeUrl, ApiResult.class);
        log.info("视频服务器:绑定设备接口返回数据:{}", result);
        if (result == null) {
            throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败");
        }
        // if (result.getResult().equals(20)) {
        //     throw new BusinessException(ErrorCode.ERR_500, "Current device does not match the company");
        // } else if (result.getResult().equals(0) || result.getResult().equals(16)) {
        //     return true;
        // } else {
        //     return false;
        // }
        if (result.getResult().equals(0)) {
            return true;
        } else {
            if (StringUtils.isNotEmpty(result.getMessage()) && result.getMessage().contains("already exists")) {
                throw new BusinessException(ErrorCode.ERR_500, "监控设备已存在");
            }
            throw new BusinessException(ErrorCode.ERR_500, result.getMessage());
        }
    }
 
    /**
     * 添加设备
     *
     * @param mwMonitorDevice
     * @return
     */
    public boolean addDevice(MwMonitorDevice mwMonitorDevice) {
 
        MonitorConfig monitorConfig = getMonitorConfig();
        RestTemplate restTemplate = new RestTemplate();
        String completeUrl = String.format(ADD_DEVICE_URL, getJSession(), mwMonitorDevice.getDevIdno(), monitorConfig.getCompany(), monitorConfig.getAccount(), mwMonitorDevice.getChannelNum());
        log.info("视频服务器:添加设备接口传参数据:{}", completeUrl);
        ApiResult result = restTemplate.getForObject(completeUrl, ApiResult.class);
        log.info("视频服务器:添加设备接口返回数据:{}", result);
        if (result == null) {
            throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败");
        }
        if (result.getResult().equals(0)) {
            return true;
        } else {
            if (StringUtils.isNotEmpty(result.getMessage()) && result.getMessage().contains("already exists")) {
                throw new BusinessException(ErrorCode.ERR_500, "监控设备已存在");
            }
            throw new BusinessException(ErrorCode.ERR_500, result.getMessage());
        }
    }
 
    /**
     * 删除设备
     *
     * @param devIdno
     * @return
     */
    public boolean deleteDevice(String devIdno) {
 
        RestTemplate restTemplate = new RestTemplate();
        String completeUrl = String.format(DELETE_DEVICE_URL, getJSession(), devIdno);
        log.info("视频服务器:删除设备接口传参数据:{}", completeUrl);
        ApiResult result = restTemplate.getForObject(completeUrl, ApiResult.class);
        log.info("视频服务器:删除设备接口返回数据:{}", result);
        if (result == null) {
            throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败");
        }
        if (result.getResult().equals(0)) {
            return true;
        } else {
            throw new BusinessException(ErrorCode.ERR_500, result.getMessage());
        }
    }
 
    /**
     * 获取设备状态:
     * 在线状态
     * 1表示在线,否则不在线。
     *
     * @param devIdno
     * @return
     */
    public Integer  getDeviceState(String devIdno) {
        Integer state=0;
        RestTemplate restTemplate = new RestTemplate();
        String completeUrl = String.format(SELState_DEVICE_URL, getJSession(), devIdno);
        log.info("视频服务器:获取设备状态接口传参数据:{}", completeUrl);
        ApiStateResult result = restTemplate.getForObject(completeUrl, ApiStateResult.class);
        log.info("视频服务器:获取设备状态接口返回数据:{}", result);
        if (result == null) {
            throw new BusinessException(ErrorCode.ERR_500, "视频服务器连接失败");
        }
        if (result.getResult().equals(0)) {
            state=0;
            if(result.getOnlines()!=null && result.getOnlines().size()>0){
                state=result.getOnlines().get(0).getOnline();
            }
        }
        return  state;
    }
 
    @Data
    private static class ApiResult {
 
        private String message;
 
        private Integer result;
 
        private String jsession;
 
    }
 
    @Data
    private static class ApiStateResult {
 
        private Integer result;
 
        private List<ApiOnline> onlines;
 
    }
 
    @Data
    private static class ApiOnline{
        private String did;
        private String vid;
        private Integer online;
    }
 
    @Data
    private static class MonitorConfig {
 
        private String account;
 
        private String company;
 
        private String password;
 
    }
 
    public MonitorConfig getMonitorConfig() {
 
        SysDict sysDict = sysDictService.getOne(new QueryWrapper<SysDict>().lambda().eq(SysDict::getDictCode, "MONITOR_DEVICE_CONFIG"));
        if (sysDict == null) {
            throw new BusinessException(ErrorCode.ERR_500, "请联系管理员进行参数初始化");
        }
        List<SysDictItem> sysDictItemList = sysDictItemService.list(new QueryWrapper<SysDictItem>().lambda().eq(SysDictItem::getDictId, sysDict.getId()));
        if (CollectionUtils.isEmpty(sysDictItemList)) {
            throw new BusinessException(ErrorCode.ERR_500, "请联系管理员进行参数初始化");
        }
        MonitorConfig config = new MonitorConfig();
        for (SysDictItem sysDictItem : sysDictItemList) {
            if (sysDictItem.getItemText().equals("account")) {
                config.setAccount(sysDictItem.getItemValue());
            }
            if (sysDictItem.getItemText().equals("company")) {
                config.setCompany(sysDictItem.getItemValue());
            }
            if (sysDictItem.getItemText().equals("password")) {
                config.setPassword(sysDictItem.getItemValue());
            }
        }
        return config;
    }
 
}