作者 钟来

添加http控制功能

正在显示 30 个修改的文件 包含 358 行增加64 行删除
... ... @@ -16,4 +16,9 @@ public interface ThingsModelBase<T> {
boolean checkValue();
void setSaveView(String value);
/**
* 反转值
*/
void reverse(String value);
}
... ...
... ... @@ -15,65 +15,56 @@ import lombok.Data;
@Data
public abstract class ThingsModelItemBase<T> implements ThingsModelBase<T>
{
public static ThingsModelItemBase newhingsModelItem(ThingsModelDataTypeEnum thingsModelDataTypeEnum,IotThingsModel thingsModel,JsonElement jsonElement)
/**
* 下位机端数据转换物模型
* @param thingsModelDataTypeEnum
* @param thingsModel
* @param jsonElement
* @return
*/
public static ThingsModelItemBase newhingsModel(ThingsModelDataTypeEnum thingsModelDataTypeEnum,IotThingsModel thingsModel,JsonElement jsonElement)
{
if (!jsonElement.isJsonNull())
{
String specs = thingsModel.getSpecs();
if(StringUtils.isEmpty(specs))
{
specs = new JsonObject().toString();
}
ThingsModelItemBase thingsModelItemBase = GsonConstructor.get().fromJson(specs,StringModelOutput.class);
switch (thingsModelDataTypeEnum)
{
case STRING:
thingsModelItemBase.setValue(jsonElement.getAsString());
break;
case BOOL:
thingsModelItemBase = GsonConstructor.get().fromJson(specs,BoolModelOutput.class);
thingsModelItemBase.setValue(Boolean.parseBoolean(jsonElement.getAsString()) || jsonElement.getAsString().equals("1"));
break;
case ENUM:
thingsModelItemBase = GsonConstructor.get().fromJson(specs,EnumModelOutput.class);
thingsModelItemBase.setValue(jsonElement.getAsString());
break;
case ARRAY:
thingsModelItemBase = GsonConstructor.get().fromJson(specs,ArrayModelOutput.class);
thingsModelItemBase.setValue(jsonElement.getAsJsonArray());
break;
case DECIMAL:
thingsModelItemBase = GsonConstructor.get().fromJson(specs,DecimalModelOutput.class);
thingsModelItemBase.setValue(jsonElement.getAsBigDecimal());
break;
case INTEGER:
thingsModelItemBase = GsonConstructor.get().fromJson(specs,IntegerModelOutput.class);
thingsModelItemBase.setValue(jsonElement.getAsInt());
break;
}
thingsModelItemBase.setSaveView(jsonElement.getAsString());
ThingsModelItemBase thingsModelItemBase = createThingsModelItemBase(thingsModelDataTypeEnum,thingsModel,jsonElement);
thingsModelItemBase.conversionThingsModel(thingsModel);
thingsModelItemBase.setSaveView(jsonElement.getAsString());
return thingsModelItemBase;
}
return null;
}
public static ThingsModelItemBase newhingsModelItemFromControl(ThingsModelDataTypeEnum thingsModelDataTypeEnum,IotThingsModel thingsModel,JsonElement jsonElement)
/**
* 上位机端数据转换物模型
* @param thingsModelDataTypeEnum
* @param thingsModel
* @param jsonElement
* @return
*/
public static ThingsModelItemBase newhingsModelReverse(ThingsModelDataTypeEnum thingsModelDataTypeEnum,IotThingsModel thingsModel,JsonElement jsonElement)
{
if (!jsonElement.isJsonNull())
{
ThingsModelItemBase thingsModelItemBase = createThingsModelItemBase(thingsModelDataTypeEnum,thingsModel,jsonElement);
thingsModelItemBase.conversionThingsModel(thingsModel);
thingsModelItemBase.reverse(jsonElement.getAsString());
return thingsModelItemBase;
}
return null;
}
private static ThingsModelItemBase createThingsModelItemBase(ThingsModelDataTypeEnum thingsModelDataTypeEnum,IotThingsModel thingsModel,JsonElement jsonElement)
{
String specs = thingsModel.getSpecs();
if(StringUtils.isEmpty(specs))
{
specs = new JsonObject().toString();
}
ThingsModelItemBase thingsModelItemBase = GsonConstructor.get().fromJson(specs,StringModelOutput.class);
ThingsModelItemBase thingsModelItemBase = null;
switch (thingsModelDataTypeEnum)
{
case STRING:
thingsModelItemBase.setSaveView(jsonElement.getAsString());
thingsModelItemBase = GsonConstructor.get().fromJson(specs,StringModelOutput.class);
break;
case BOOL:
thingsModelItemBase = GsonConstructor.get().fromJson(specs,BoolModelOutput.class);
... ... @@ -90,14 +81,12 @@ public abstract class ThingsModelItemBase<T> implements ThingsModelBase<T>
case INTEGER:
thingsModelItemBase = GsonConstructor.get().fromJson(specs,IntegerModelOutput.class);
break;
default:
thingsModelItemBase = GsonConstructor.get().fromJson(specs,StringModelOutput.class);
break;
}
thingsModelItemBase.setSaveView(jsonElement.getAsString());
thingsModelItemBase.conversionThingsModel(thingsModel);
return thingsModelItemBase;
}
return null;
}
public static ThingsModelItemBase newhingsModelItem(JsonObject jsonObject)
{
... ...
... ... @@ -48,4 +48,9 @@ public class ArrayModelOutput extends ThingsModelItemBase<JsonArray>
public void setSaveView(String value) {
setValue(JSONArray.parseObject(value, JsonArray.class));
}
@Override
public void reverse(String value) {
setValue(JSONArray.parseObject(value, JsonArray.class));
}
}
... ...
... ... @@ -48,4 +48,9 @@ public class BoolModelOutput extends ThingsModelItemBase<Boolean>
public void setSaveView(String value) {
setValue(value.equals("1") || Boolean.parseBoolean(value) );
}
@Override
public void reverse(String value) {
setValue(value.equals("1") || Boolean.parseBoolean(value) );
}
}
... ...
... ... @@ -69,6 +69,12 @@ public class DecimalModelOutput extends ThingsModelItemBase<BigDecimal>
}
}
@Override
public void reverse(String value) {
BigDecimal bigDecimalValue = new BigDecimal(value);
setValue(bigDecimalValue);
}
private int getDecimalPlaces(BigDecimal number) {
if (number == null || number.scale() <= 0) {
return 0;
... ...
... ... @@ -69,6 +69,11 @@ public class EnumModelOutput extends ThingsModelItemBase<String>
setValue(value);
}
@Override
public void reverse(String value) {
setValue(value);
}
private static Object conversion(Object data ,String clas) {
try {
return stringToTarget(String.valueOf(data),Class.forName(clas));
... ...
... ... @@ -75,6 +75,12 @@ public class IntegerModelOutput extends ThingsModelItemBase<Integer>
@Override
public void setSaveView(String value) {
BigDecimal bigDecimal = new BigDecimal(value);
setValue(bigDecimal.intValue());
}
@Override
public void reverse(String value) {
BigDecimal bigDecimal = new BigDecimal(value);
setValue(bigDecimal.multiply(new BigDecimal(acy)).intValue());
}
}
... ...
... ... @@ -40,4 +40,9 @@ public class StringModelOutput extends ThingsModelItemBase<String>
public void setSaveView(String value) {
setValue(value);
}
@Override
public void reverse(String value) {
setValue(value);
}
}
... ...
package com.zhonglai.luhui.device.analysis.comm.service;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ruoyi.common.utils.GsonConstructor;
import com.zhonglai.luhui.device.analysis.comm.config.SysParameter;
import com.zhonglai.luhui.device.analysis.comm.db.DeviceService;
import com.zhonglai.luhui.device.analysis.comm.db.mode.TerminalDataThingsModeService;
import com.zhonglai.luhui.device.analysis.comm.dto.ServerDto;
import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelBase;
import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelDataTypeEnum;
import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelItemBase;
import com.zhonglai.luhui.device.analysis.comm.util.DateUtils;
import com.zhonglai.luhui.device.analysis.dto.SaveDataDto;
import com.zhonglai.luhui.device.analysis.dto.topic.AddPostDto;
... ... @@ -11,6 +18,8 @@ import com.zhonglai.luhui.device.analysis.dto.topic.AllPostDto;
import com.zhonglai.luhui.device.domain.IotDevice;
import com.zhonglai.luhui.device.domain.IotTerminal;
import com.zhonglai.luhui.device.analysis.comm.factory.Topic;
import com.zhonglai.luhui.device.domain.IotThingsModel;
import org.apache.commons.lang3.EnumUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
... ... @@ -49,6 +58,9 @@ public class BusinessDataUpdateService {
@Value("${sys.isText:false}")
private Boolean isText;
@Autowired
private TerminalDataThingsModeService terminalDataThingsModeService;
/**
* 更新数据
* @param type
... ... @@ -256,4 +268,6 @@ public class BusinessDataUpdateService {
String[] notNullField = new String[notNullFieldSet.size()];
return notNullFieldSet.toArray(notNullField);
}
}
... ...
... ... @@ -89,7 +89,7 @@ public class DataModeAnalysisService {
{
data_type = ThingsModelDataTypeEnum.STRING.name();
}
ThingsModelItemBase thingsModelItemBase = ThingsModelItemBase.newhingsModelItem(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, gsonData.get(key));
ThingsModelItemBase thingsModelItemBase = ThingsModelItemBase.newhingsModel(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, gsonData.get(key));
if(!thingsModelItemBase.checkValue())
{
... ...
... ... @@ -55,6 +55,7 @@ public class RocketMqService {
org.apache.rocketmq.common.message.Message msg = new org.apache.rocketmq.common.message.Message(topic,null==operation_token?sendTags:operation_token,payload);
try {
org.apache.rocketmq.common.message.Message ms = rocketMQTemplate.getProducer().request(msg,30000l);
JSONObject jsonObject = (JSONObject) JSON.parse(new String(ms.getBody()));
return new Message(MessageCode.getMessageCode(jsonObject.getInteger("code")),jsonObject.getString("message"),jsonObject.get("data"));
} catch (RequestTimeoutException e) {
... ...
... ... @@ -174,7 +174,7 @@ public class IotDeviceController extends BaseController
}
String[] sts = iotProductClass.getSub_topics().split(",");
if(iotDevice.getCompletion_auth()==1)
if(iotDevice.getCompletion_auth()==0)
{
String name= "";
for (int i=0;i<sts.length;i++)
... ...
... ... @@ -13,6 +13,7 @@ import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
/**
... ... @@ -49,10 +50,16 @@ public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFacto
}
@Override
public boolean notice(String deviceId, JsonObject jsonObject) {
public NoticeMessageDto notice(String deviceId, JsonObject jsonObject) {
ParserDeviceHostDto parserDeviceHostDto = DeviceCach.getDeviceHost(deviceId);
HttpNoticeCach.putUpdataData(parserDeviceHostDto,jsonObject);
return true;
NoticeMessageDto noticeMessageDomain = new NoticeMessageDto();
noticeMessageDomain.setTopicconfig(topicconfig);
noticeMessageDomain.setCommd(jsonObject.toString().getBytes(StandardCharsets.UTF_8));
Topic topic = new Topic();
topic.setClientid(deviceId);
noticeMessageDomain.setTopic(topic);
return noticeMessageDomain;
}
... ...
... ... @@ -46,8 +46,8 @@ public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFacto
}
@Override
public boolean notice(String deviceId, JsonObject jsonObject) {
return false;
public NoticeMessageDto notice(String deviceId, JsonObject jsonObject) {
return null;
}
/**
... ...
package com.zhonglai.luhui.device.protocol.factory.config;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.google.gson.internal.LinkedTreeMap;
import com.google.gson.reflect.TypeToken;
import com.ruoyi.common.utils.GsonConstructor;
import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto;
import net.jodah.expiringmap.ExpirationListener;
... ... @@ -30,11 +33,12 @@ public class HttpNoticeCach {
public static void putUpdataData(ParserDeviceHostDto parserDeviceHostDto, JsonObject jsonObject)
{
Map<String, Object> map = JSONObject.parseObject(jsonObject.toString(), new com.alibaba.fastjson.TypeReference<Map<String, Object>>(){});
if(noticeUpdataData.containsKey(parserDeviceHostDto.getId()))
{
noticeUpdataData.put(parserDeviceHostDto.getId(), GsonConstructor.get().fromJson(jsonObject.toString(), HashMap.class));
noticeUpdataData.put(parserDeviceHostDto.getId(), map);
}else {
noticeUpdataData.put(parserDeviceHostDto.getId(),GsonConstructor.get().fromJson(jsonObject.toString(), HashMap.class),parserDeviceHostDto.getDevice_life()*3,TimeUnit.SECONDS);
noticeUpdataData.put(parserDeviceHostDto.getId(),map,parserDeviceHostDto.getDevice_life()*3,TimeUnit.SECONDS);
}
}
... ...
... ... @@ -3,15 +3,18 @@ package com.zhonglai.luhui.device.protocol.factory.control;
import cn.hutool.extra.spring.SpringUtil;
import com.google.gson.JsonObject;
import com.ruoyi.common.utils.GsonConstructor;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.zhonglai.luhui.device.analysis.comm.clien.ClienConnection;
import com.zhonglai.luhui.device.analysis.comm.clien.impl.ClienConnectionImpl;
import com.zhonglai.luhui.device.analysis.comm.config.SysParameter;
import com.zhonglai.luhui.device.analysis.comm.dto.ApiClientRePlyDto;
import com.zhonglai.luhui.device.analysis.comm.dto.TerminalClientRePlyDto;
import com.zhonglai.luhui.device.analysis.comm.factory.Topic;
import com.zhonglai.luhui.device.analysis.comm.service.BusinessDataUpdateService;
import com.zhonglai.luhui.device.analysis.dto.Message;
import com.zhonglai.luhui.device.analysis.dto.MessageCode;
import com.zhonglai.luhui.device.analysis.util.TopicUtil;
import com.zhonglai.luhui.device.domain.IotThingsModel;
import com.zhonglai.luhui.device.protocol.factory.analysis.ProtocolParserFactory;
import com.zhonglai.luhui.device.protocol.factory.config.DeviceCach;
import com.zhonglai.luhui.device.protocol.factory.config.PluginsClassLoader;
... ... @@ -54,7 +57,7 @@ import java.util.concurrent.TimeUnit;
* 设备指令监听服务
*/
@Service
@RocketMQMessageListener(consumerGroup = "deviceCommand", topic = "deviceCommandListen",selectorType = SelectorType.TAG,selectorExpression = "${rocketmq.operationToken}",messageModel = MessageModel.BROADCASTING)
@RocketMQMessageListener(consumerGroup = "${rocketmq.consumerGroup}", topic = "deviceCommandListen",selectorType = SelectorType.TAG,selectorExpression = "${rocketmq.operationToken}",messageModel = MessageModel.BROADCASTING)
public class DeviceCommandListenService implements RocketMQReplyListener<MessageExt, Message>, RocketMQPushConsumerLifecycleListener {
private static final Logger log = LoggerFactory.getLogger(DeviceCommandListenService.class);
... ... @@ -80,6 +83,8 @@ public class DeviceCommandListenService implements RocketMQReplyListener<Message
private String selectorExpression;
private DefaultMQPushConsumer defaultMQPushConsumer;
@Override
public Message onMessage(MessageExt messageExt) {
log.info("监听到消息{}",messageExt);
... ... @@ -124,7 +129,16 @@ public class DeviceCommandListenService implements RocketMQReplyListener<Message
}
return sendMessage(noticeMessageDomain);
case notice:
if(deviceCommandServiceFactory.notice(deviceCommand.getDeviceId(), deviceCommand.getData()))
IotThingsModelService iotThingsModelService = SpringUtils.getBean(IotThingsModelService.class);
JsonObject jsonObject = iotThingsModelService.reverseModeData(parserDeviceHostDto.getIotProduct().getId(), deviceCommand.getData());
noticeMessageDomain = deviceCommandServiceFactory.notice(deviceCommand.getDeviceId(), jsonObject);
if(null == noticeMessageDomain)
{
return new Message(MessageCode.DEFAULT_FAIL_CODE,"该设备不支持写入功能");
}
if(clienNoticeServiceFactory.sendMessage(noticeMessageDomain))
{
return new Message(MessageCode.DEFAULT_SUCCESS_CODE,"指令发送成功");
}else {
... ...
... ... @@ -10,5 +10,5 @@ public interface DeviceCommandServiceFactory {
NoticeMessageDto read(String deviceId, JsonObject jsonObject);
NoticeMessageDto write(String deviceId, JsonObject jsonObject);
boolean notice(String deviceId, JsonObject jsonObject);
NoticeMessageDto notice(String deviceId, JsonObject jsonObject);
}
... ...
... ... @@ -23,7 +23,7 @@ import java.util.Arrays;
import java.util.Set;
@Service
@RocketMQMessageListener(consumerGroup = "deviceCommand", topic = "deviceCommandListen",selectorType = SelectorType.TAG,selectorExpression = "SysCommand",messageModel = MessageModel.BROADCASTING)
@RocketMQMessageListener(consumerGroup = "deviceCommand-${rocketmq.consumerGroup}", topic = "deviceCommandListen",selectorType = SelectorType.TAG,selectorExpression = "SysCommand",messageModel = MessageModel.BROADCASTING)
public class SysCommandListenService implements RocketMQReplyListener<MessageExt, Message> , RocketMQPushConsumerLifecycleListener {
private static final Logger log = LoggerFactory.getLogger(DeviceCommandListenService.class);
... ... @@ -65,6 +65,8 @@ public class SysCommandListenService implements RocketMQReplyListener<MessageExt
case upProductPayloadModelNumber:
iotThingsModelService.upProductPayloadModelNumberCach(deviceCommand.getData().get("product_ids").getAsInt());
return new Message(MessageCode.DEFAULT_SUCCESS_CODE,"更新产品模型编号关系成功");
case delSubscribe: //取消订阅
mqttSubscribeService.unSubscribe(deviceCommand.getData().get("ip").getAsString(),deviceCommand.getData().get("product_ids").getAsString());
default:
return new Message(MessageCode.DEFAULT_FAIL_CODE,"指令类型不存在,请联系管理员");
}
... ...
... ... @@ -36,5 +36,10 @@ public enum CommandType {
/**
* 更新产品模型编号关系
*/
upProductPayloadModelNumber
upProductPayloadModelNumber,
/**
* 删除订阅
*/
delSubscribe
}
... ...
... ... @@ -177,7 +177,7 @@ public class DefaultProtocolPurificationFactoryImpl implements ProtocolPurificat
{
data_type = ThingsModelDataTypeEnum.STRING.name();
}
return ThingsModelItemBase.newhingsModelItem(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, jsonElement);
return ThingsModelItemBase.newhingsModel(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, jsonElement);
}
private DeviceSensorData getDeviceSensorData(Integer time,String sensorNumber,Topic topic,IotThingsModel thingsModel,ThingsModelItemBase thingsModelItemBase)
... ...
package com.zhonglai.luhui.device.protocol.factory.service;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ruoyi.common.utils.GsonConstructor;
import com.ruoyi.common.utils.StringUtils;
import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelBase;
import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelDataTypeEnum;
import com.zhonglai.luhui.device.analysis.comm.dto.thingsmodels.ThingsModelItemBase;
import com.zhonglai.luhui.device.domain.IotProductPayloadModelNumber;
import com.zhonglai.luhui.device.domain.IotProductTranslate;
import com.zhonglai.luhui.device.domain.IotThingsModel;
import com.zhonglai.luhui.device.protocol.factory.config.ProductPayloadModelNumberCach;
import org.apache.commons.lang3.EnumUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
... ... @@ -130,4 +136,149 @@ public class IotThingsModelService {
}
}
}
/**
* 逆向翻译模式数据
* @param product_id
* @param jsonObject
* @return
*/
public JSONObject reverseModeData(Integer product_id, JSONObject jsonObject )
{
if(null != jsonObject && jsonObject.size() !=0 )
{
JSONObject vjsonObject = jsonObject.clone();
return reverseTranslateModeData(product_id,vjsonObject);
}
return null;
}
/**
* 逆向翻译模式数据
* @param product_id
* @param jsonObject
* @return
*/
public JsonObject reverseModeData(Integer product_id, JsonObject jsonObject )
{
if(null != jsonObject && jsonObject.size() !=0 )
{
JSONObject vjsonObject = GsonConstructor.get().fromJson(jsonObject.toString(),JSONObject.class);
vjsonObject = reverseTranslateModeData(product_id,vjsonObject);
if(null != vjsonObject)
{
return GsonConstructor.get().fromJson(vjsonObject.toString(),JsonObject.class);
}
}
return null;
}
/**
* 解析模式数据
* @param product_id
* @param jsonObject
* @return
*/
public JSONObject modeData(Integer product_id, JSONObject jsonObject )
{
if(null != jsonObject && jsonObject.size() !=0 )
{
JSONObject vjsonObject = jsonObject.clone();
return translateModeData(product_id,vjsonObject);
}
return null;
}
/**
* 解析模式数据
* @param product_id
* @param jsonObject
* @return
*/
public JsonObject modeData(Integer product_id, JsonObject jsonObject )
{
if(null != jsonObject && jsonObject.size() !=0 )
{
JSONObject vjsonObject = GsonConstructor.get().fromJson(jsonObject.toString(),JSONObject.class);
vjsonObject = translateModeData(product_id,vjsonObject);
if(null != vjsonObject)
{
return GsonConstructor.get().fromJson(vjsonObject.toString(),JsonObject.class);
}
}
return null;
}
/**
* 解析模式数据
* @param product_id
* @param vjsonObject
* @return
*/
private JSONObject translateModeData(Integer product_id, JSONObject vjsonObject )
{
for(String vkey:vjsonObject.keySet())
{
JSONObject jsData = vjsonObject.getJSONObject(vkey);
for(String key:jsData.keySet())
{
IotThingsModel thingsModel = getThingsModelBase(product_id,key);
String data_type = thingsModel.getData_type().toUpperCase();
if(!EnumUtils.isValidEnum(ThingsModelDataTypeEnum.class,data_type))
{
data_type = ThingsModelDataTypeEnum.STRING.name();
}
ThingsModelBase thingsModelBase = ThingsModelItemBase.newhingsModel(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, GsonConstructor.get().fromJson(jsData.get(key).toString(), JsonElement.class));
jsData.put(key,thingsModelBase);
}
vjsonObject.put(vkey,jsData);
}
return vjsonObject;
}
/**
* 反向翻译模式数据
* @param product_id
* @param vjsonObject
* @return
*/
private JSONObject reverseTranslateModeData(Integer product_id, JSONObject vjsonObject )
{
for(String vkey:vjsonObject.keySet())
{
JSONObject jsData = vjsonObject.getJSONObject(vkey);
for(String key:jsData.keySet())
{
IotThingsModel thingsModel = getThingsModelBase(product_id,key);
String data_type = thingsModel.getData_type().toUpperCase();
if(!EnumUtils.isValidEnum(ThingsModelDataTypeEnum.class,data_type))
{
data_type = ThingsModelDataTypeEnum.STRING.name();
}
ThingsModelItemBase thingsModelBase = ThingsModelItemBase.newhingsModelReverse(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, GsonConstructor.get().fromJson(jsData.get(key).toString(), JsonElement.class));
jsData.put(key,thingsModelBase.getValue());
}
vjsonObject.put(vkey,jsData);
}
return vjsonObject;
}
private IotThingsModel getThingsModelBase(Integer product_id,String identifier)
{
IotThingsModel thingsModel = getIotThingsModel(product_id,identifier);
if(null == thingsModel) //没有配置的 都按字符串处理
{
thingsModel = new IotThingsModel();
thingsModel.setData_type(ThingsModelDataTypeEnum.STRING.name());
thingsModel.setIdentifier(identifier);
thingsModel.setModel_name(identifier);
thingsModel.setIs_top(0);
thingsModel.setIs_monitor(0);
thingsModel.setIs_save_log(0);
thingsModel.setIs_config(0);
JsonObject specs = new JsonObject();
specs.addProperty("maxLength",255);
thingsModel.setSpecs(specs.toString());
}
return thingsModel;
}
}
... ...
... ... @@ -129,6 +129,38 @@ public class MqttSubscribeService {
return null;
}
public Set<String> unSubscribe(String ip,String productids)
{
if(SysParameter.service_ip.equals(ip))
{
try {
List<ProtocolSubTopics> list = persistenceDBService.getProtocolSubTopicsFromProductids(productids,false);
Set<String> sts = new HashSet<>();
for (ProtocolSubTopics protocolSubTopics:list)
{
String[] ps = protocolSubTopics.getSub_topics().split(",");
for (String p:ps)
{
String topic = generateTopicFromSub_topics(protocolSubTopics.getRole_id(),protocolSubTopics.getMqtt_username(),p);
if(topics.contains(topic))
{
topics.remove(topic);
sts.add(topic);
}
}
}
mqttclient.unsubscribe(sts.toArray(new String[sts.size()]));
return sts;
} catch (MqttException e) {
return null;
}
}
return null;
}
/**
* 根据产品订阅信息和ip,生成topic
* ip用户来判断是否和当前服务器ip一直,如果不一直就卸载topic,一致就订阅topic
... ...
... ... @@ -283,4 +283,6 @@ public class PersistenceDBService {
{
return getIotProtocolClass(id).getClassname();
}
}
... ...
... ... @@ -48,8 +48,8 @@ public class DeviceCommandListenServiceImpl implements DeviceCommandServiceFacto
}
@Override
public boolean notice(String deviceId, JsonObject jsonObject) {
return false;
public NoticeMessageDto notice(String deviceId, JsonObject jsonObject) {
return null;
}
... ...
package com.zhonglai.luhui.http.service.proxy.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ruoyi.common.core.domain.AjaxResult;
... ... @@ -11,6 +12,7 @@ import com.zhonglai.luhui.device.domain.IotDevice;
import com.zhonglai.luhui.device.protocol.factory.ProtocolParserAndPurificationFactory;
import com.zhonglai.luhui.device.protocol.factory.comm.DataLogType;
import com.zhonglai.luhui.device.protocol.factory.comm.DeviceDataLog;
import com.zhonglai.luhui.device.protocol.factory.config.DeviceCach;
import com.zhonglai.luhui.device.protocol.factory.config.HttpNoticeCach;
import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto;
import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceInfoDto;
... ... @@ -85,7 +87,6 @@ public class HttpDataProxyController {
}
rmap.put("0",config);
}
List<ParserDeviceInfoDto> parserDeviceInfoDtoList = protocolPurificationModel.getParserDeviceInfoDtoList();
if (null != parserDeviceInfoDtoList && parserDeviceInfoDtoList.size() != 0)
{
... ... @@ -104,9 +105,18 @@ public class HttpDataProxyController {
}
}
rmap.put(parserDeviceInfoDto.getId().substring(parserDeviceInfoDto.getId().indexOf("_")+1),config);
}else if(null != parserDeviceInfoDto.getData() && parserDeviceInfoDto.getData().size()>0 && parserDeviceInfoDto.getData().has("isSendConfig") && parserDeviceInfoDto.getData().get("isSendConfig").getAsBoolean())
{
JsonObject config = DeviceCach.getDeviceInfo(parserDeviceInfoDto.getId()).getConfig();
if(null != config && config.size() !=0)
{
rmap.put(parserDeviceInfoDto.getId().substring(parserDeviceInfoDto.getId().indexOf("_")+1),JSONObject.parseObject(config.toString(), new com.alibaba.fastjson.TypeReference<Map<String, Object>>(){}));
}
}
}
}
if(rmap.size()!= 0)
{
log.info("返回的数据【{}】",rmap);
... ...
package com.zhonglai.luhui.http.service.proxy.service;
import com.google.gson.JsonObject;
import com.ruoyi.common.utils.GsonConstructor;
import com.zhonglai.luhui.device.protocol.factory.config.DeviceCach;
import com.zhonglai.luhui.device.protocol.factory.control.ClienNoticeServiceFactory;
import com.zhonglai.luhui.device.protocol.factory.dto.NoticeMessageDto;
import com.zhonglai.luhui.device.protocol.factory.dto.ParserDeviceHostDto;
import com.zhonglai.luhui.device.protocol.factory.dto.ProtocolPurificationModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;
@Service
public class HttpClienNoticeServiceImpl implements ClienNoticeServiceFactory {
@Autowired
private HttpCallback httpCallback;
@Override
public boolean sendMessage(NoticeMessageDto noticeMessageDomain) {
if(null != noticeMessageDomain.getCommd() && noticeMessageDomain.getCommd().length!=0)
{
String clientid = noticeMessageDomain.getTopic().getClientid();
ParserDeviceHostDto parserDeviceHostDto = DeviceCach.getDeviceHost(clientid);
if(null == parserDeviceHostDto)
{
return false;
}
JsonObject jsonObject = GsonConstructor.get().fromJson(new String(noticeMessageDomain.getCommd()),JsonObject.class);
String topic = "/"+parserDeviceHostDto.getIotProduct().getRole_id()+"/"+parserDeviceHostDto.getIotProduct().getMqtt_username()+"/"+clientid+"/Json/ADD_POST/"+new Long(System.currentTimeMillis()).intValue();
httpCallback.messageArrived(clientid,topic,jsonObject.toString().getBytes(StandardCharsets.UTF_8));
return true;
}
return false;
}
}
... ...
... ... @@ -25,5 +25,6 @@ mqtt:
#rocketmq配置信息
rocketmq:
#nameservice服务器地址(多个以英文逗号隔开)
name-server: 47.115.144.179:9876
name-server: 8.129.224.117:9876
consumerGroup: lh-mqtt-service-listen${random.uuid}
operationToken: local_lh-http-service-proxy
\ No newline at end of file
... ...
... ... @@ -35,4 +35,5 @@ mqtt:
rocketmq:
#nameservice服务器地址(多个以英文逗号隔开)
name-server: 8.129.224.117:9876
consumerGroup: lh-mqtt-service-listen${random.uuid}
operationToken: local_lh-mqtt-service-listen
\ No newline at end of file
... ...
... ... @@ -135,7 +135,7 @@ public class MqttDeviceService extends DeviceService{
String data_type = thingsModel.getData_type().toUpperCase();
Object object = jsonObject.get(skey);
ThingsModelItemBase thingsModelItemBase = ThingsModelItemBase.newhingsModelItemFromControl(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel,gsonobject.get(skey));
ThingsModelItemBase thingsModelItemBase = ThingsModelItemBase.newhingsModelReverse(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel,gsonobject.get(skey));
jsonObject.put(skey,thingsModelItemBase.getCmdView(object));
... ...
... ... @@ -74,7 +74,7 @@ public class ReadReqTopic implements BusinessAgreement<ReadReqDto> {
{
data_type = ThingsModelDataTypeEnum.STRING.name();
}
ThingsModelBase thingsModelBase = ThingsModelItemBase.newhingsModelItem(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, GsonConstructor.get().fromJson(jsData.get(key).toString(), JsonElement.class));
ThingsModelBase thingsModelBase = ThingsModelItemBase.newhingsModel(Enum.valueOf(ThingsModelDataTypeEnum.class,data_type),thingsModel, GsonConstructor.get().fromJson(jsData.get(key).toString(), JsonElement.class));
// Class<ThingsModelBase> aClass = Enum.valueOf(ThingsModelDataTypeEnum.class,data_type).getaClass();
// ThingsModelBase thingsModelBase = JSON.parseObject(thingsModel.getSpecs(),aClass);
// thingsModelBase.conversionThingsModel(thingsModel);
... ...