第一次提交
This commit is contained in:
@ -0,0 +1,27 @@
|
||||
package com.fastbee.modbusToJson;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* @author gsb
|
||||
* @date 2023/8/14 17:24
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class FYModel {
|
||||
|
||||
/**
|
||||
* 采集点标识符
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 数量
|
||||
*/
|
||||
private Integer quality;
|
||||
/**
|
||||
* 数值
|
||||
*/
|
||||
private String value;
|
||||
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package com.fastbee.modbusToJson;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.fastbee.common.annotation.SysProtocol;
|
||||
import com.fastbee.common.constant.FastBeeConstant;
|
||||
import com.fastbee.common.core.mq.DeviceReport;
|
||||
import com.fastbee.common.core.mq.MQSendMessageBo;
|
||||
import com.fastbee.common.core.mq.message.DeviceData;
|
||||
import com.fastbee.common.core.mq.message.FunctionCallBackBo;
|
||||
import com.fastbee.common.core.thingsModel.NeuronModel;
|
||||
import com.fastbee.common.core.thingsModel.ThingsModelSimpleItem;
|
||||
import com.fastbee.common.core.thingsModel.ThingsModelValuesInput;
|
||||
import com.fastbee.common.exception.ServiceException;
|
||||
import com.fastbee.common.utils.DateUtils;
|
||||
import com.fastbee.protocol.base.protocol.IProtocol;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author gsb
|
||||
* @date 2022/10/10 16:55
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@SysProtocol(name = "Modbus转Json解析协议",protocolCode = FastBeeConstant.PROTOCOL.ModbusToJson,description = "modbus转json解析协议")
|
||||
public class ModbusToJsonProtocolService implements IProtocol {
|
||||
|
||||
/**
|
||||
* 解析json格式数据
|
||||
*
|
||||
*/
|
||||
@Override
|
||||
public DeviceReport decode(DeviceData deviceData, String clientId) {
|
||||
try {
|
||||
DeviceReport reportMessage = new DeviceReport();
|
||||
String data = new String(deviceData.getData(),StandardCharsets.UTF_8);
|
||||
List<ThingsModelSimpleItem> result = new ArrayList<>();
|
||||
NeuronModel model = JSON.parseObject(data, NeuronModel.class);
|
||||
JSONObject values = model.getValues();
|
||||
for (Map.Entry<String, Object> entry : values.entrySet()) {
|
||||
ThingsModelSimpleItem item = new ThingsModelSimpleItem();
|
||||
item.setTs(DateUtils.getNowDate());
|
||||
item.setValue(entry.getValue()+"");
|
||||
item.setId(entry.getKey());
|
||||
result.add(item);
|
||||
}
|
||||
reportMessage.setThingsModelSimpleItem(result);
|
||||
reportMessage.setClientId(clientId);
|
||||
reportMessage.setSerialNumber(clientId);
|
||||
reportMessage.setSources(data);
|
||||
return reportMessage;
|
||||
}catch (Exception e){
|
||||
throw new ServiceException("数据解析异常"+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FunctionCallBackBo encode(MQSendMessageBo message) {
|
||||
try {
|
||||
FunctionCallBackBo callBack = new FunctionCallBackBo();
|
||||
String msg = JSONObject.toJSONString(message.getParams());
|
||||
callBack.setSources(msg);
|
||||
callBack.setMessage(msg.getBytes());
|
||||
return callBack;
|
||||
}catch (Exception e){
|
||||
log.error("=>指令编码异常,device={},data={}",message.getSerialNumber(),
|
||||
message.getParams());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user