liuxiaolong
2019-05-06 71af9c46c24b562acc00a71ec6c97c04eb048d9c
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package com.cloud.device.controller;
 
 
import com.alibaba.fastjson.JSONObject;
import com.cloud.device.constants.Constants;
import com.cloud.device.model.Device;
import com.cloud.device.model.Dic;
import com.cloud.device.service.DeviceService;
import com.cloud.device.service.DicService;
import com.cloud.device.service.NodeService;
import com.cloud.device.vo.MenuTreeVo;
import com.cloud.model.common.Page;
import com.cloud.model.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.util.*;
 
@Slf4j
@Api(value = "DeviceController",description = "设备管理")
@RequestMapping(value = "/data/api-d/device")
@RestController
public class DeviceController {
 
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DicService dicService;
    @Autowired
    private NodeService nodeService;
 
    @RequestMapping("/getDeviceType")
    @ApiOperation(value = "获取设备类型", notes = "获取设备类型", httpMethod = "POST",produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result getDeviceType(){
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("type", Dic.DIC_DEVICE_TYPE);
 
            return Result.ok(Constants.RESULT_QUERY_OK, dicService.getDicByType(map));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @GetMapping("/getDeviceBrand")
    @ApiOperation(value = "获取设备品牌", notes = "获取设备品牌", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result getDeviceBrand(){
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("type", Dic.DIC_DEVICE_BRAND);
            return Result.ok(Constants.RESULT_QUERY_OK, dicService.getDicByType(map));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @GetMapping("/getStatus")
    @ApiOperation(value = "获取设备状态字典列表", notes = "获取设备状态字典列表", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result getStatusList(){
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("type", Dic.DIC_DEVICE_STATUS);
            return Result.ok(Constants.RESULT_QUERY_OK, dicService.getDicByType(map));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @GetMapping("/findPlatDeviceList")
    @ApiOperation(value = "获取平台设备列表(分页)",notes = "获取平台设备列表(分页)",httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
        @ApiImplicitParam(name ="param", value = "无用参数", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "start", value = "起始页", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "length", value = "条数", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "orgId", value = "组织机构id", required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name ="condition", value = "设备名称/设备id/设备位置", required = false, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "type", value = "类型(MENU或其它)", required = true, dataType = "String", paramType = "query")
    })
    public Page<Device> findPlatDeviceList(@RequestParam Map<String,Object> param){
        return deviceService.findPlatDeviceList(param);
 
    }
 
    @GetMapping("/findAllPlatDeviceList")
    @ApiOperation(value = "获取所有非集群监控设备列表(不分页)",notes = "获取所有非集群监控设备列表(不分页)",httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name ="param", value = "无用参数", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "组织机构id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name ="condition", value = "设备名称/设备id/设备位置", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型(MENU或其它)", required = true, dataType = "String", paramType = "query")
    })
    public Result findAllPlatDeviceList(@RequestParam Map<String,Object> param){
        try {
            return Result.ok(Constants.RESULT_QUERY_OK, deviceService.findAllPlatDeviceList(param));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @ApiOperation(value = "通过区域查找设备", notes = "通过区域查找设备", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "无用参数", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "condition", value = "设备名称/设备位置", required = false, dataType = "String", paramType = "query")
    })
    @GetMapping("/findByArea")
    public Result findByArea(@RequestParam Map<String,Object> param){
        try {
            return Result.ok(Constants.RESULT_QUERY_OK, deviceService.findByArea(param));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @ApiOperation(value = "通过区域查找设备", notes = "通过区域查找设备", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "无用参数", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "condition", value = "设备名称/设备位置", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "组织机构id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型(MENU或其它)", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("/findAllDeviceByArea")
    public Result findAllDeviceByArea(@RequestParam Map<String, Object> param){
        try {
            return Result.ok(Constants.RESULT_QUERY_OK, deviceService.findAllDeviceByArea(param));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @RequestMapping("/getDeviceById")
    @ApiOperation(value = "通过id查找设备", notes = "通过id查找设备", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "设备id", required = true, dataType = "String",paramType = "query")
    })
    public Result getDeviceById(@RequestParam String id){
        try {
            return Result.ok(Constants.RESULT_QUERY_OK, deviceService.selectById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
 
    }
 
    @RequestMapping("/getNodeByDeviceId")
    @ApiOperation(value = "根据摄像机id获取摄像机信息(节点信息)",notes = "根据摄像机id获取摄像机信息(节点信息)",
            httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "摄像机id", required = true, dataType = "String", paramType = "query")
    })
    public Result getNodeByDeviceId(@RequestParam String deviceId){
        try {
            JSONObject jsonResult = new JSONObject();
            Device device = deviceService.selectById(deviceId);
            if(device !=null){
                jsonResult.put("device", device);
                if(StringUtils.isNotEmpty(device.getNodeId())){
                    jsonResult.put("node", nodeService.selectById(device.getNodeId()));
                }
            }
            return Result.ok(Constants.RESULT_QUERY_OK, jsonResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @RequestMapping("/saveDevice")
    @ApiOperation(value = "保存设备",notes = "保存设备",httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "device", value = "设备实例", required = true, dataType = "Device", paramType = "body")
//            @ApiImplicitParam(name = "orgId", value = "所属组织机构id", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "name", value="设备名称", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "type", value = "设备类型(摄像机或PAD或分析的字典值)", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "brand", value = "设备品牌", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "ip", value = "设备ip", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "port", value = "端口", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "address", value = "位置", required = true, dataType = "String", paramType = "query")
    })
    public Result saveDevice(@RequestBody Device device){
        try {
            //查找此ip是否有重复的设备
            Device existDevice = deviceService.findByIp(device.getIp());
            if(existDevice !=null){
                if(StringUtils.isEmpty(device.getId()) || (StringUtils.isNotEmpty(device.getId()) && !device.getId().equals(existDevice.getId()))){
                    return Result.custom("重复添加摄像机",500,false,"设备ip不允许重复");
                }
            }
 
 
            return Result.ok(Constants.RESULT_SAVE_OK, deviceService.saveDevice(device));
 
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @GetMapping("/updateOrg")
    @ApiOperation(value = "更新设备所属组织", notes = "更新设备所属组织", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "设备id",required = true, dataType = "String", paramType = "query"),
        @ApiImplicitParam(name = "orgId", value = "组织id", required = true, dataType = "Integer", paramType = "query")
    })
    public Result updateOrg(String id,Integer orgId){
        try {
            log.info("id:"+id+",orgId:"+orgId);
            if(StringUtils.isNotEmpty(id) && orgId > 0)
                return Result.ok(Constants.RESULT_SAVE_OK, deviceService.updateOrg(id, orgId));
            else
                return Result.custom("更新失败,参数不正确",401,false,"");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @RequestMapping("/deleteDevice")
    @ApiOperation(value = "删除设备", notes = "删除设备", httpMethod = "POST", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备id", required = true, dataType = "String", paramType = "query")
    })
    public Result deleteDevice(@RequestParam String id){
        try {
            return Result.ok(Constants.RESULT_DELETE_OK, deviceService.deleteById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @ApiOperation(value="获取设备组织机构树", notes = "获取设备组织机构",httpMethod = "GET",produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "无用参数", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称、模糊查询", required = false, dataType = "String", paramType = "query")
    })
    @RequestMapping("/getOrgDeviceTree")
    private List<MenuTreeVo> getOrgDeviceTree(@RequestParam Map<String, Object> param){
        try {
            return deviceService.getOrgDeviceTree(param);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
 
    @ApiOperation(value = "地图中获取所有设备", notes = "地图中获取所有设备", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @GetMapping("/findAllDevices")
    public Result findAllDevices(){
        try {
            return Result.ok(Constants.RESULT_QUERY_OK, deviceService.findAllDevices());
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @ApiOperation(value = "更新设备经纬度", notes = "更新设备经纬度", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "无用参数", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "longitude", value = "经度", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "latitude", value = "纬度", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("/updatePosById")
    public Result updatePosById(@RequestParam Map<String,Object> param){
        try {
            return Result.ok(Constants.RESULT_SAVE_OK, deviceService.updatePosById(param));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @ApiOperation(value = "更新节点公网ip和端口", notes = "更新节点公网ip和端口", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param", value = "无用参数", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "publicIp", value = "公网ip", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "publicPort", value = "公网端口", required = true, dataType = "String", paramType = "query")
    })
    @GetMapping("/updatePublicIpPort")
    public Result updatePublicIpPort(@RequestParam Map<String,Object> param){
        try {
            return Result.ok(Constants.RESULT_SAVE_OK, deviceService.updatePublicIpPort(param));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }
 
    @ApiOperation(value = "通过devId获取节点信息", notes = "通过devId获取节点信息", httpMethod = "GET", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "devId", value = "devId",required = true,dataType = "String",paramType = "query")
    })
    @GetMapping("/getNodeByDevId")
    public Result getNodeByDevId(@RequestParam String devId){
        return Result.ok(Constants.RESULT_QUERY_OK, deviceService.getNodeByDevId(devId));
    }
 
    @ApiOperation(value = "getDevSnapshot",notes =
            "获取实时流截图{\"devId\":\"DS-2CD2T46WDA2-I20180622AACHC30488278\"}",
            httpMethod = "POST",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequestMapping("/getDevSnapshot")
    public Result getDevSnapshot(@RequestBody Map reqMap, HttpServletRequest req) throws ParseException, IOException {
        String devId = (String) reqMap.get("devId");
        if (StringUtils.isBlank(devId)){
            throw new RuntimeException("获取实时流截图 devId:"+devId+"为空。");
        }
        log.info("获取实时流截图 devId:"+devId);
        JSONObject url = deviceService.getDevSnapshot(devId);  //  :80/dddd.jpg
        return  Result.ok("获取实时流截图成功。",url);
    }
 
 
//    @AuthNoneIgnore
//    @ApiOperation(value = "测试设备连接", notes = "测试设备连接" ,httpMethod = "GET",produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "str_brand", value = "品牌",required = true,dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "str_ip", value = "ip", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "n_port", value = "端口", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "str_username", value = "用户名", required = true, dataType = "String", paramType = "query"),
//            @ApiImplicitParam(name = "str_password", value = "密码", required = true, dataType = "String", paramType = "query")
//    })
//    public Result testDeviceConn(@RequestParam Map<String,Object> param){
//
//        return Result.ok(Constants.RESULT_QUERY_OK, deviceService.testDeviceConn(param));
//    }
 
}