|
@@ -1,10 +1,17 @@
|
1
|
1
|
package com.yunzhi.nanyang.controller;
|
2
|
2
|
|
|
3
|
+import com.alibaba.fastjson.JSONObject;
|
3
|
4
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
4
|
5
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
5
|
6
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
6
|
7
|
import com.yunzhi.nanyang.common.BaseController;
|
7
|
8
|
import com.yunzhi.nanyang.common.ResponseBean;
|
|
9
|
+import com.yunzhi.nanyang.common.StringUtils;
|
|
10
|
+import com.yunzhi.nanyang.entity.TaMachinery;
|
|
11
|
+import com.yunzhi.nanyang.entity.TdHatcDevice;
|
|
12
|
+import com.yunzhi.nanyang.service.ITaMachineryService;
|
|
13
|
+import com.yunzhi.nanyang.service.ITdHatcDeviceService;
|
|
14
|
+import com.yunzhi.nanyang.vo.DeviceSummary;
|
8
|
15
|
import io.swagger.annotations.Api;
|
9
|
16
|
import io.swagger.annotations.ApiOperation;
|
10
|
17
|
import io.swagger.annotations.ApiParam;
|
|
@@ -39,81 +46,154 @@ public class TdDeviceController extends BaseController {
|
39
|
46
|
@Autowired
|
40
|
47
|
public ITdDeviceService iTdDeviceService;
|
41
|
48
|
|
|
49
|
+ @Autowired
|
|
50
|
+ public ITdHatcDeviceService iTdHatcDeviceService;
|
42
|
51
|
|
43
|
|
- /**
|
44
|
|
- * 分页查询列表
|
45
|
|
- * @param pageNum
|
46
|
|
- * @param pageSize
|
47
|
|
- * @return
|
48
|
|
- */
|
49
|
|
- @RequestMapping(value="/tdDevice",method= RequestMethod.GET)
|
50
|
|
- @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
|
51
|
|
- public ResponseBean tdDeviceList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
|
52
|
|
- @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception{
|
53
|
|
-
|
54
|
|
- IPage<TdDevice> pg = new Page<>(pageNum, pageSize);
|
55
|
|
- QueryWrapper<TdDevice> queryWrapper = new QueryWrapper<>();
|
56
|
|
- queryWrapper.orderByDesc("create_date");
|
|
52
|
+ @Autowired
|
|
53
|
+ public ITaMachineryService iTaMachineryService;
|
57
|
54
|
|
58
|
|
- IPage<TdDevice> result = iTdDeviceService.page(pg, queryWrapper);
|
59
|
|
- return ResponseBean.success(result);
|
60
|
|
- }
|
|
55
|
+//
|
|
56
|
+// /**
|
|
57
|
+// * 分页查询列表
|
|
58
|
+// * @param pageNum
|
|
59
|
+// * @param pageSize
|
|
60
|
+// * @return
|
|
61
|
+// */
|
|
62
|
+// @RequestMapping(value="/tdDevice",method= RequestMethod.GET)
|
|
63
|
+// @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
|
|
64
|
+// public ResponseBean tdDeviceList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
|
|
65
|
+// @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize) throws Exception{
|
|
66
|
+//
|
|
67
|
+// IPage<TdDevice> pg = new Page<>(pageNum, pageSize);
|
|
68
|
+// QueryWrapper<TdDevice> queryWrapper = new QueryWrapper<>();
|
|
69
|
+// queryWrapper.orderByDesc("create_date");
|
|
70
|
+//
|
|
71
|
+// IPage<TdDevice> result = iTdDeviceService.page(pg, queryWrapper);
|
|
72
|
+// return ResponseBean.success(result);
|
|
73
|
+// }
|
61
|
74
|
|
62
|
75
|
/**
|
63
|
76
|
* 保存对象
|
64
|
77
|
* @param tdDevice 实体对象
|
65
|
78
|
* @return
|
66
|
79
|
*/
|
67
|
|
- @RequestMapping(value="/tdDevice",method= RequestMethod.POST)
|
|
80
|
+ @RequestMapping(value="/admin/device",method= RequestMethod.POST)
|
68
|
81
|
@ApiOperation(value="保存", notes = "保存", httpMethod = "POST", response = ResponseBean.class)
|
69
|
82
|
public ResponseBean tdDeviceAdd(@ApiParam("保存内容") @RequestBody TdDevice tdDevice) throws Exception{
|
70
|
83
|
|
71
|
|
- if (iTdDeviceService.save(tdDevice)){
|
72
|
|
- return ResponseBean.success(tdDevice);
|
73
|
|
- }else {
|
74
|
|
- return ResponseBean.error("保存失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
|
|
84
|
+ boolean isNew = StringUtils.isEmpty(tdDevice.getDeviceId());
|
|
85
|
+
|
|
86
|
+ if (StringUtils.isEmpty(tdDevice.getMachineryId())) {
|
|
87
|
+ return ResponseBean.error("未绑定农机");
|
75
|
88
|
}
|
76
|
|
- }
|
77
|
89
|
|
78
|
|
- /**
|
79
|
|
- * 根据id删除对象
|
80
|
|
- * @param id 实体ID
|
81
|
|
- */
|
82
|
|
- @RequestMapping(value="/tdDevice/{id}", method= RequestMethod.DELETE)
|
83
|
|
- @ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
|
84
|
|
- public ResponseBean tdDeviceDelete(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
|
85
|
|
- if(iTdDeviceService.removeById(id)){
|
86
|
|
- return ResponseBean.success("success");
|
87
|
|
- }else {
|
88
|
|
- return ResponseBean.error("删除失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
|
|
90
|
+ TdDevice origin = iTdDeviceService.getExistBy("machinery_id", tdDevice.getMachineryId(), false, true);
|
|
91
|
+ if (isNew) {
|
|
92
|
+ // 如果是新增操作
|
|
93
|
+ if (null != origin) {
|
|
94
|
+ return ResponseBean.error("该农机已绑定设备");
|
|
95
|
+ }
|
|
96
|
+ } else {
|
|
97
|
+ if (null != origin) {
|
|
98
|
+ // 如果非新增操作, 并且当前的 id 与 数据库 id 不一致
|
|
99
|
+ if (!tdDevice.getDeviceId().equals(origin.getDeviceId())) {
|
|
100
|
+ return ResponseBean.error("该农机已绑定设备");
|
|
101
|
+ }
|
|
102
|
+ } else {
|
|
103
|
+ isNew = true;
|
|
104
|
+ }
|
89
|
105
|
}
|
90
|
|
- }
|
91
|
106
|
|
92
|
|
- /**
|
93
|
|
- * 修改对象
|
94
|
|
- * @param id 实体ID
|
95
|
|
- * @param tdDevice 实体对象
|
96
|
|
- * @return
|
97
|
|
- */
|
98
|
|
- @RequestMapping(value="/tdDevice/{id}",method= RequestMethod.PUT)
|
99
|
|
- @ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
|
100
|
|
- public ResponseBean tdDeviceUpdate(@ApiParam("对象ID") @PathVariable Integer id,
|
101
|
|
- @ApiParam("更新内容") @RequestBody TdDevice tdDevice) throws Exception{
|
|
107
|
+ if (StringUtils.isEmpty(tdDevice.getApiConfig())) {
|
|
108
|
+ return ResponseBean.error("未绑定海康设备");
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ TaMachinery taMachinery = iTaMachineryService.getExistBy("machinery_id", tdDevice.getMachineryId(), false, true);
|
|
112
|
+ if (taMachinery == null) {
|
|
113
|
+ return ResponseBean.error("绑定农机不存在");
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ try {
|
|
117
|
+ DeviceSummary deviceSummary = JSONObject.parseObject(tdDevice.getApiConfig(), DeviceSummary.class);
|
|
118
|
+ if (null == deviceSummary) {
|
|
119
|
+ return ResponseBean.error("未绑定海康设备");
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ if (StringUtils.isEmpty(deviceSummary.getDeviceId())) {
|
|
123
|
+ return ResponseBean.error("未绑定海康设备");
|
|
124
|
+ }
|
102
|
125
|
|
103
|
|
- if (iTdDeviceService.updateById(tdDevice)){
|
104
|
|
- return ResponseBean.success(iTdDeviceService.getById(id));
|
|
126
|
+ TdHatcDevice hatcDevice = iTdHatcDeviceService.getById(deviceSummary.getDeviceId());
|
|
127
|
+ if (null == hatcDevice) {
|
|
128
|
+ return ResponseBean.error("绑定的海康设备不存在");
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ if (null == deviceSummary.getChannel() || deviceSummary.getChannel() < 1) {
|
|
132
|
+ return ResponseBean.error("未设置通道号, 或者通道号不正确");
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ if (null == deviceSummary.getNetType() || deviceSummary.getNetType() < 0 || deviceSummary.getNetType() > 3) {
|
|
136
|
+ return ResponseBean.error("未设置网络类型, 或者网络类型不正确");
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ tdDevice.setDeviceNo(hatcDevice.getTerminalId());
|
|
140
|
+ tdDevice.setDeviceType(hatcDevice.getProductKey());
|
|
141
|
+ taMachinery.setOnlineStatus(hatcDevice.getDeviceStatus());
|
|
142
|
+ } catch (Exception e) {
|
|
143
|
+ e.printStackTrace();
|
|
144
|
+ return ResponseBean.error("海康设备参数无效");
|
|
145
|
+ }
|
|
146
|
+
|
|
147
|
+ boolean isOk = isNew ? iTdDeviceService.save(tdDevice) : iTdDeviceService.updateById(tdDevice);
|
|
148
|
+ iTaMachineryService.updateById(taMachinery);
|
|
149
|
+
|
|
150
|
+ if (isOk){
|
|
151
|
+ return ResponseBean.success(tdDevice);
|
105
|
152
|
}else {
|
106
|
|
- return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
|
|
153
|
+ return ResponseBean.error("保存失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
|
107
|
154
|
}
|
108
|
155
|
}
|
|
156
|
+//
|
|
157
|
+// /**
|
|
158
|
+// * 根据id删除对象
|
|
159
|
+// * @param id 实体ID
|
|
160
|
+// */
|
|
161
|
+// @RequestMapping(value="/tdDevice/{id}", method= RequestMethod.DELETE)
|
|
162
|
+// @ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
|
|
163
|
+// public ResponseBean tdDeviceDelete(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
|
|
164
|
+// if(iTdDeviceService.removeById(id)){
|
|
165
|
+// return ResponseBean.success("success");
|
|
166
|
+// }else {
|
|
167
|
+// return ResponseBean.error("删除失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
|
|
168
|
+// }
|
|
169
|
+// }
|
|
170
|
+//
|
|
171
|
+// /**
|
|
172
|
+// * 修改对象
|
|
173
|
+// * @param id 实体ID
|
|
174
|
+// * @param tdDevice 实体对象
|
|
175
|
+// * @return
|
|
176
|
+// */
|
|
177
|
+// @RequestMapping(value="/tdDevice/{id}",method= RequestMethod.PUT)
|
|
178
|
+// @ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
|
|
179
|
+// public ResponseBean tdDeviceUpdate(@ApiParam("对象ID") @PathVariable Integer id,
|
|
180
|
+// @ApiParam("更新内容") @RequestBody TdDevice tdDevice) throws Exception{
|
|
181
|
+//
|
|
182
|
+// if (iTdDeviceService.updateById(tdDevice)){
|
|
183
|
+// return ResponseBean.success(iTdDeviceService.getById(id));
|
|
184
|
+// }else {
|
|
185
|
+// return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
|
|
186
|
+// }
|
|
187
|
+// }
|
109
|
188
|
|
110
|
189
|
/**
|
111
|
190
|
* 根据id查询对象
|
112
|
191
|
* @param id 实体ID
|
113
|
192
|
*/
|
114
|
|
- @RequestMapping(value="/tdDevice/{id}",method= RequestMethod.GET)
|
|
193
|
+ @RequestMapping(value="/admin/machinery/{id}/device",method= RequestMethod.GET)
|
115
|
194
|
@ApiOperation(value="详情", notes = "详情", httpMethod = "GET", response = ResponseBean.class)
|
116
|
|
- public ResponseBean tdDeviceGet(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
|
117
|
|
- return ResponseBean.success(iTdDeviceService.getById(id));
|
|
195
|
+ public ResponseBean tdDeviceGet(@ApiParam("农机ID") @PathVariable String id) throws Exception{
|
|
196
|
+ TdDevice tdDevice = iTdDeviceService.getExistBy("machinery_id", id, false, true);
|
|
197
|
+ return ResponseBean.success(tdDevice);
|
118
|
198
|
}
|
119
|
199
|
}
|