|
|
|
package com.zhonglai.luhui.central.control;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
import com.ruoyi.common.core.domain.Message;
|
|
|
|
import com.ruoyi.common.core.domain.MessageCode;
|
|
|
|
import com.ruoyi.common.utils.StringUtils;
|
|
|
|
import com.ruoyi.common.utils.html.HttpUtils;
|
|
|
|
import com.zhonglai.luhui.device.domain.IotDevice;
|
|
|
|
import com.zhonglai.luhui.redis.service.RedisCache;
|
|
|
|
import okhttp3.Response;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
@Service
|
|
|
|
public class DeviceControlService {
|
|
|
|
@Autowired
|
|
|
|
private RedisCache redisCache;
|
|
|
|
private String redisHostPath = "luhui:mqttservice:device:device:";
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private DeviceDBOperationService deviceDBOperationService;
|
|
|
|
|
|
|
|
private IotDevice getRedisIotDevice(String imei)
|
|
|
|
{
|
|
|
|
return (IotDevice)redisCache.getCacheObject(redisHostPath+imei);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 固件版本更新
|
|
|
|
* @param imei 主机imei
|
|
|
|
* @param firmwareVersion 版本号
|
|
|
|
* @param code 版本码
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Message firmwareUp(String imei,String firmwareVersion,Integer code)
|
|
|
|
{
|
|
|
|
IotDevice iotDevice = getRedisIotDevice(imei);
|
|
|
|
if(null == iotDevice || StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"设备不在线");
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/control/"+imei;
|
|
|
|
Map<String,Object> valueMap = new HashMap<>();
|
|
|
|
valueMap.put("firmwareVersion",firmwareVersion);
|
|
|
|
valueMap.put("code",code);
|
|
|
|
return post(url, jsonObject -> jsonObject.put("0",valueMap));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 设备重启
|
|
|
|
* @param imei 主机imei
|
|
|
|
* @param restart 1重启,2复位,3恢复出厂值
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Message restart(String imei ,Integer restart)
|
|
|
|
{
|
|
|
|
IotDevice iotDevice = getRedisIotDevice(imei);
|
|
|
|
if(null == iotDevice || StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"设备不在线");
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/control/"+imei;
|
|
|
|
Map<String,Object> map = new HashMap<>();
|
|
|
|
Map<String,Object> valueMap = new HashMap<>();
|
|
|
|
valueMap.put("restart",restart);
|
|
|
|
return post(url, jsonObject -> jsonObject.put("0",valueMap));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取指定设备版本信息
|
|
|
|
* @param imei 主机imei
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public Message getFirmwareVersion(String imei) throws IOException {
|
|
|
|
IotDevice iotDevice = getRedisIotDevice(imei);
|
|
|
|
if(null == iotDevice || StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"设备不在线");
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/getFirmwareVersion/"+iotDevice.getMqtt_username();
|
|
|
|
return postFrom(url, formBody -> {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 强行断开链接
|
|
|
|
* @param imei 主机imei
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public Message closeSession(String imei) {
|
|
|
|
IotDevice iotDevice = getRedisIotDevice(imei);
|
|
|
|
if(null == iotDevice || StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"设备不在线");
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/closeSession/"+imei;
|
|
|
|
return postFrom(url, formBody -> {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 删除主机
|
|
|
|
* @param imei 主机imei
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Message delIotDevice(String imei) {
|
|
|
|
IotDevice iotDevice = getRedisIotDevice(imei);
|
|
|
|
if(null == iotDevice || StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"设备不在线");
|
|
|
|
}
|
|
|
|
deviceDBOperationService.deleteIotDeviceByClient_id(imei);
|
|
|
|
deviceDBOperationService.deleteIotTerminalByDeviceId(imei);
|
|
|
|
if(StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
return new Message(MessageCode.DEFAULT_SUCCESS_CODE,"删除成功");
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/delIotDevice/"+imei;
|
|
|
|
|
|
|
|
return postFrom(url, formBody -> {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 删除终端
|
|
|
|
* @param imei 主机imei
|
|
|
|
* @param number 终端编号
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Message delIotTerminal(String imei,String number) {
|
|
|
|
IotDevice iotDevice = getRedisIotDevice(imei);
|
|
|
|
if(null == iotDevice || StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"设备不在线");
|
|
|
|
}
|
|
|
|
deviceDBOperationService.deleteIotTerminalById(imei+"_"+number);
|
|
|
|
if(StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
return new Message(MessageCode.DEFAULT_SUCCESS_CODE,"删除成功");
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/delIotTerminal/"+imei+"/"+number;
|
|
|
|
|
|
|
|
return postFrom(url, formBody -> {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 读取属性
|
|
|
|
* @param imei 主机imei
|
|
|
|
* @param sensor_number 传感器编号(0,1_1,10_1)
|
|
|
|
* @param attributes 属性集合(id1,id2,id3)
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public Message readAttribute(String imei,String sensor_number,String attributes) throws IOException {
|
|
|
|
IotDevice iotDevice = getRedisIotDevice(imei);
|
|
|
|
if(null == iotDevice || StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"设备不在线");
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/read/"+imei;
|
|
|
|
return post(url, jsonObject -> jsonObject.put(sensor_number,attributes));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 设置主机自定义参数
|
|
|
|
* @param imei 主机imei
|
|
|
|
* @param summary 自定义数据json字符串
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public Message upSummary(String imei,String summary) {
|
|
|
|
IotDevice iotDevice = getRedisIotDevice(imei);
|
|
|
|
if(null == iotDevice || StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"设备不在线");
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/control/"+imei;
|
|
|
|
Map<String,Object> valueMap = new HashMap<>();
|
|
|
|
valueMap.put("summary",JSONObject.parseObject(summary));
|
|
|
|
return post(url, jsonObject -> jsonObject.put("0",valueMap));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 修改指定终端属性
|
|
|
|
* @param imei 主机imei
|
|
|
|
* @param number 终端编号(如:1_1)
|
|
|
|
* @param config 配置参数json字符串
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public Message upTerminalConfig(String imei,String number,@RequestBody Map<String,Object> config) throws IOException {
|
|
|
|
IotDevice iotDevice = getRedisIotDevice(imei);
|
|
|
|
if(null == iotDevice || StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"设备不在线");
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/control/"+imei;
|
|
|
|
return post(url, jsonObject -> jsonObject.put(number,config));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 批量修改终端属性
|
|
|
|
* @param imei 主机imei
|
|
|
|
* @param map 批量数据json字符串
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
|
|
|
public Message batchUpTerminalConfig(String imei,@RequestBody Map<String,Object> map) {
|
|
|
|
IotDevice iotDevice = getRedisIotDevice(imei);
|
|
|
|
if(null == iotDevice || StringUtils.isEmpty(iotDevice.getListen_service_ip()))
|
|
|
|
{
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"设备不在线");
|
|
|
|
}
|
|
|
|
String url = "http://"+iotDevice.getListen_service_ip()+"device/control/"+imei;
|
|
|
|
return post(url, jsonObject -> {
|
|
|
|
for (String key:map.keySet())
|
|
|
|
{
|
|
|
|
jsonObject.put(key, map.get(key));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Message post(String url, HttpUtils.JsonBody jsonBody)
|
|
|
|
{
|
|
|
|
Response response = null;
|
|
|
|
try {
|
|
|
|
response = HttpUtils.postJsonBody(url, jsonBody);
|
|
|
|
if(null != response.body() && StringUtils.isNotEmpty(response.body().string()))
|
|
|
|
{
|
|
|
|
Message message = com.alibaba.fastjson.JSONObject.parseObject(response.body().string(),Message.class);
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"指令转发失败请联系管理员");
|
|
|
|
}
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"指令执行失败请稍后重试");
|
|
|
|
}
|
|
|
|
private Message postFrom(String url,HttpUtils.FromBody fromBody )
|
|
|
|
{
|
|
|
|
Response response1 = null;
|
|
|
|
try {
|
|
|
|
response1 = HttpUtils.postFromBody(url, builder -> {
|
|
|
|
}, fromBody);
|
|
|
|
if(null != response1.body() && StringUtils.isNotEmpty(response1.body().string()))
|
|
|
|
{
|
|
|
|
Message message = com.alibaba.fastjson.JSONObject.parseObject(response1.body().string(),Message.class);
|
|
|
|
return message;
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"指令转发失败请联系管理员");
|
|
|
|
}
|
|
|
|
return new Message(MessageCode.DEFAULT_FAIL_CODE,"指令执行失败请稍后重试");
|
|
|
|
}
|
|
|
|
|
|
|
|
} |