|
|
|
package com.zhonglai.luhui.api.controller.iot;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.ruoyi.common.annotation.Log;
|
|
|
|
import com.ruoyi.common.core.domain.Message;
|
|
|
|
import com.ruoyi.common.core.domain.MessageCode;
|
|
|
|
import com.ruoyi.common.enums.BusinessType;
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
import com.ruoyi.common.utils.html.HttpUtils;
|
|
|
|
import com.ruoyi.system.domain.IotDevice;
|
|
|
|
import com.ruoyi.system.service.IIotDeviceService;
|
|
|
|
import com.ruoyi.system.service.IIotTerminalService;
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import io.swagger.annotations.ApiImplicitParam;
|
|
|
|
import io.swagger.annotations.ApiImplicitParams;
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
import okhttp3.Response;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
@Api(tags = "设备控制")
|
|
|
|
@Controller
|
|
|
|
@RequestMapping("/iot/iotDeviceControl")
|
|
|
|
public class IotDeviceControlController {
|
|
|
|
@Autowired
|
|
|
|
private IIotDeviceService iotDeviceService;
|
|
|
|
@Autowired
|
|
|
|
private IIotTerminalService iIotTerminalService;
|
|
|
|
private String getServiceAdrres(HttpServletResponse response,String imei) throws IOException {
|
|
|
|
IotDevice iotDevice = iotDeviceService.selectIotDeviceByClient_id(imei);
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
if(StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
response.getWriter().print(new Message(MessageCode.DEFAULT_FAIL_CODE,"未找到设备监听服务地址"));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return "http://"+iotDevice.getListen_service_ip()+"device/control/"+imei;
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("固件版本更新")
|
|
|
|
@ApiImplicitParams({
|
|
|
|
@ApiImplicitParam(value = "主机imei",name = "imei"),
|
|
|
|
@ApiImplicitParam(value = "版本号",name = "firmwareVersion")
|
|
|
|
})
|
|
|
|
@PreAuthorize("@ss.hasPermi('iot:iotDeviceControl:firmwareUp')")
|
|
|
|
@Log(title = "设备控制", businessType = BusinessType.UPDATE)
|
|
|
|
@ResponseBody
|
|
|
|
@PostMapping("/firmwareUp/{imei}")
|
|
|
|
public String firmwareUp(HttpServletResponse response,@PathVariable String imei,String firmwareVersion,Integer code) throws IOException {
|
|
|
|
String url = getServiceAdrres(response,imei);
|
|
|
|
if(null == url)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
Map<String,Object> map = new HashMap<>();
|
|
|
|
Map<String,Object> valueMap = new HashMap<>();
|
|
|
|
valueMap.put("firmwareVersion",firmwareVersion);
|
|
|
|
valueMap.put("code",code);
|
|
|
|
Response response1 = HttpUtils.postJsonBody(url, formBody -> {
|
|
|
|
formBody.put("0", valueMap);
|
|
|
|
});
|
|
|
|
return response1.body().string();
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("设备重启")
|
|
|
|
@ApiImplicitParams({
|
|
|
|
@ApiImplicitParam(value = "主机imei",name = "imei"),
|
|
|
|
@ApiImplicitParam(value = "restart 1重启,2复位,3恢复出厂值",name = "restart"),
|
|
|
|
})
|
|
|
|
@PreAuthorize("@ss.hasPermi('iot:iotDeviceControl:restart')")
|
|
|
|
@Log(title = "设备控制", businessType = BusinessType.UPDATE)
|
|
|
|
@ResponseBody
|
|
|
|
@PostMapping("/restart/{imei}/{restart}")
|
|
|
|
public String restart(HttpServletResponse response,@PathVariable String imei ,@PathVariable Integer restart) throws IOException {
|
|
|
|
String url = getServiceAdrres(response,imei);
|
|
|
|
if(null == url)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
Map<String,Object> valueMap = new HashMap<>();
|
|
|
|
valueMap.put("restart",restart);
|
|
|
|
Response response1 = HttpUtils.postJsonBody(url, formBody -> {
|
|
|
|
formBody.put("0",valueMap);
|
|
|
|
});
|
|
|
|
return response1.body().string();
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("获取指定设备版本信息")
|
|
|
|
@ApiImplicitParams({
|
|
|
|
@ApiImplicitParam(value = "主机imei",name = "imei"),
|
|
|
|
})
|
|
|
|
@ResponseBody
|
|
|
|
@PostMapping("/getFirmwareVersion/{imei}")
|
|
|
|
public String getFirmwareVersion(HttpServletResponse response,@PathVariable String imei) throws IOException {
|
|
|
|
IotDevice iotDevice = iotDeviceService.selectIotDeviceByClient_id(imei);
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
if(StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
response.getWriter().print(new Message(MessageCode.DEFAULT_FAIL_CODE,"未找到设备监听服务地址"));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/getFirmwareVersion/"+iotDevice.getMqtt_username();
|
|
|
|
|
|
|
|
Response response1 = HttpUtils.postFromBody(url, builder -> {
|
|
|
|
}, formBody -> {
|
|
|
|
});
|
|
|
|
return response1.body().string();
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("强行断开链接")
|
|
|
|
@ApiImplicitParams({
|
|
|
|
@ApiImplicitParam(value = "主机imei",name = "imei"),
|
|
|
|
})
|
|
|
|
@ResponseBody
|
|
|
|
@PostMapping("/closeSession/{imei}")
|
|
|
|
public String closeSession(HttpServletResponse response,@PathVariable String imei) throws IOException {
|
|
|
|
IotDevice iotDevice = iotDeviceService.selectIotDeviceByClient_id(imei);
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
if(StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
response.getWriter().print(new Message(MessageCode.DEFAULT_FAIL_CODE,"未找到设备监听服务地址"));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/closeSession/"+imei;
|
|
|
|
|
|
|
|
Response response1 = HttpUtils.postFromBody(url, builder -> {
|
|
|
|
}, formBody -> {
|
|
|
|
});
|
|
|
|
return response1.body().string();
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("删除主机")
|
|
|
|
@ApiImplicitParams({
|
|
|
|
@ApiImplicitParam(value = "主机imei",name = "imei"),
|
|
|
|
})
|
|
|
|
@Transactional
|
|
|
|
@ResponseBody
|
|
|
|
@PostMapping("/delIotDevice/{imei}")
|
|
|
|
public String delIotDevice(HttpServletResponse response,@PathVariable String imei) {
|
|
|
|
IotDevice iotDevice = iotDeviceService.selectIotDeviceByClient_id(imei);
|
|
|
|
iotDeviceService.deleteIotDeviceByClient_id(imei);
|
|
|
|
iIotTerminalService.deleteIotTerminalByDeviceId(imei);
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
if(StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
return JSONObject.toJSONString(new Message(MessageCode.DEFAULT_SUCCESS_CODE,"删除成功"));
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/delIotDevice/"+imei;
|
|
|
|
|
|
|
|
try {
|
|
|
|
String str = HttpUtils.getResponseString(HttpUtils.postFromBody(url, builder -> {
|
|
|
|
}, formBody -> {
|
|
|
|
}));
|
|
|
|
if(null != str)
|
|
|
|
{
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
}
|
|
|
|
return JSONObject.toJSONString(new Message(MessageCode.DEFAULT_SUCCESS_CODE,"删除成功"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("删除终端")
|
|
|
|
@ApiImplicitParams({
|
|
|
|
@ApiImplicitParam(value = "主机imei",name = "imei"),
|
|
|
|
})
|
|
|
|
@ResponseBody
|
|
|
|
@PostMapping("/delIotTerminal/{imei}/{number}")
|
|
|
|
public String delIotTerminal(@PathVariable String imei,@PathVariable String number) {
|
|
|
|
return iIotTerminalService.deleteIotTerminalById(imei,number);
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation(value = "读取属性")
|
|
|
|
@ApiImplicitParams({
|
|
|
|
@ApiImplicitParam(value = "主机imei",name = "imei"),
|
|
|
|
@ApiImplicitParam(value = "传感器编号(0,1_1,10_1)",name = "sensor_number"),
|
|
|
|
@ApiImplicitParam(value = "属性集合(id1,id2,id3)",name = "attributes"),
|
|
|
|
})
|
|
|
|
@PreAuthorize("@ss.hasPermi('iot:iotDeviceControl:upSummary')")
|
|
|
|
@Log(title = "设备控制", businessType = BusinessType.UPDATE)
|
|
|
|
@ResponseBody
|
|
|
|
@PostMapping("/readAttribute/{imei}/{sensor_number}")
|
|
|
|
public String readAttribute(HttpServletResponse response,@PathVariable String imei,@PathVariable String sensor_number,String attributes) throws IOException {
|
|
|
|
IotDevice iotDevice = iotDeviceService.selectIotDeviceByClient_id(imei);
|
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
|
if(StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
response.getWriter().print(new Message(MessageCode.DEFAULT_FAIL_CODE,"未找到设备监听服务地址"));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/read/"+imei;
|
|
|
|
Map<String,Object> map = new HashMap<>();
|
|
|
|
map.put(sensor_number,attributes);
|
|
|
|
Response response1 = HttpUtils.postJsonBody(url, jsonObject -> jsonObject.putAll(map));
|
|
|
|
return response1.body().string();
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation(value = "设置主机自定义参数",notes = "自定义数据模型:\n" +
|
|
|
|
"{\n" +
|
|
|
|
" \t \"name\": \"wumei-smart\",\n" +
|
|
|
|
" \t \"chip\": \"esp8266\",\n" +
|
|
|
|
" \t \"author\": \"kerwincui\",\n" +
|
|
|
|
" \t \"version\": 1.2,\n" +
|
|
|
|
" \t \"createTime\": \"2022-06-06\"\n" +
|
|
|
|
" }")
|
|
|
|
@ApiImplicitParams({
|
|
|
|
@ApiImplicitParam(value = "主机imei",name = "imei"),
|
|
|
|
@ApiImplicitParam(value = "自定义数据json字符串",name = "summary")
|
|
|
|
})
|
|
|
|
@PreAuthorize("@ss.hasPermi('iot:iotDeviceControl:upSummary')")
|
|
|
|
@Log(title = "设备控制", businessType = BusinessType.UPDATE)
|
|
|
|
@ResponseBody
|
|
|
|
@PostMapping("/upSummary/{imei}")
|
|
|
|
public String upSummary(HttpServletResponse response,@PathVariable String imei,String summary) throws IOException {
|
|
|
|
String url = getServiceAdrres(response,imei);
|
|
|
|
if(null == url)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
Map<String,Object> valueMap = new HashMap<>();
|
|
|
|
valueMap.put("summary",JSONObject.parseObject(summary));
|
|
|
|
Response response1 = HttpUtils.postJsonBody(url,formBody -> {
|
|
|
|
formBody.put("0", valueMap);
|
|
|
|
});
|
|
|
|
return response1.body().string();
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation(value = "修改指定终端属性",notes = "配置参数模型:\n" +
|
|
|
|
"{\n" +
|
|
|
|
" \"id1\":\"value1\",\n" +
|
|
|
|
" \"id2\":\"value2\",\n" +
|
|
|
|
" \"id3\":\"value3\"\n" +
|
|
|
|
" }")
|
|
|
|
@ApiImplicitParams({
|
|
|
|
@ApiImplicitParam(value = "主机imei",name = "imei"),
|
|
|
|
@ApiImplicitParam(value = "终端编号(如:1_1)",name = "number"),
|
|
|
|
@ApiImplicitParam(value = "配置参数json字符串",name = "config")
|
|
|
|
})
|
|
|
|
@PreAuthorize("@ss.hasPermi('iot:iotDeviceControl:upTerminalConfig')")
|
|
|
|
@Log(title = "设备控制", businessType = BusinessType.UPDATE)
|
|
|
|
@ResponseBody
|
|
|
|
@PostMapping("/upTerminalConfig/{imei}/{number}")
|
|
|
|
public String upTerminalConfig(HttpServletResponse response, @PathVariable String imei,@PathVariable String number,@RequestBody Map<String,Object> config) throws IOException {
|
|
|
|
String url = getServiceAdrres(response,imei);
|
|
|
|
if(null == url)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
Map<String,Object> map = new HashMap<>();
|
|
|
|
map.put(number,config);
|
|
|
|
Response response1 = HttpUtils.postJsonBody(url, jsonObject -> jsonObject.putAll(map));
|
|
|
|
return response1.body().string();
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation(value = "批量修改终端属性",notes = "批量数据模型:\n" +
|
|
|
|
"{\n" +
|
|
|
|
" \"1\":{\n" +
|
|
|
|
" \"id1\":\"value1\",\n" +
|
|
|
|
" \"id2\":\"value2\",\n" +
|
|
|
|
" \"id3\":\"value3\"\n" +
|
|
|
|
" },\n" +
|
|
|
|
" \"3\":{\n" +
|
|
|
|
" \"id1\":\"value1\",\n" +
|
|
|
|
" \"id2\":\"value2\",\n" +
|
|
|
|
" \"id3\":\"value3\"\n" +
|
|
|
|
" },\n" +
|
|
|
|
" \"4\":{\n" +
|
|
|
|
" \"id1\":\"value1\",\n" +
|
|
|
|
" \"id2\":\"value2\",\n" +
|
|
|
|
" \"id3\":\"value3\"\n" +
|
|
|
|
" }\n" +
|
|
|
|
"}")
|
|
|
|
@ApiImplicitParam(value = "批量数据json字符串",name = "map")
|
|
|
|
@PreAuthorize("@ss.hasPermi('iot:iotDeviceControl:batchUpTerminalConfig')")
|
|
|
|
@Log(title = "设备控制", businessType = BusinessType.UPDATE)
|
|
|
|
@ResponseBody
|
|
|
|
@PostMapping("/batchUpTerminalConfig/{imei}")
|
|
|
|
public String batchUpTerminalConfig(HttpServletResponse response,@PathVariable String imei,@RequestBody Map<String,Object> map) throws IOException {
|
|
|
|
String url = getServiceAdrres(response,imei);
|
|
|
|
if(null == url)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
Response response1 = HttpUtils.postJsonBody(url, builder -> {
|
|
|
|
|
|
|
|
}, formBody -> {
|
|
|
|
for (String key:map.keySet())
|
|
|
|
{
|
|
|
|
formBody.put(key, map.get(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
return response1.body().string();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} |
...
|
...
|
|