package com.dy.common.mw.protocol; import java.io.Serializable; import com.alibaba.fastjson2.JSON ; public class Command implements Serializable{ public static final long serialVersionUID; static { serialVersionUID = 201211292156L; } /** * 默认命令ID * 如果命令不在数据库中存储,或其他可以用defaultId */ public static final String defaultId = "999999999" ; /** * 本条命令的ID * 一般是命令在数据库中存储记录ID的字符串形式 */ public String id; /** * 水表协议名称,这个可以为空,因为水表上行数据解析出协议名称,然后系统把协议名称保存在水表数据记录中,从此协议名称就不再空了 */ public String protocol; /** * RTU 地址 */ public String rtuAddr ; /** * 命令类型:Rtu命令、针对监控中间件的命令 * 由com.dy.common.mw.protocol.CommandType类定义 */ public String type ; /** * 功能码 */ public String code ; /** * 不返回中间件对命令处理结果(true:不返回处理结果,false或null:返回处理结果) */ public Boolean noRtMwDealRes ; /** * 具体参数数据 */ public Object param ; /** * 其他数据 */ public Object attachment ; public String toString(){ String s = "命令id=" + id + "\n" ; s += (protocol == null ? "" : ("协议=" + protocol + "\n")); s += (rtuAddr == null ? "" : ("Rtu地址=" + rtuAddr + "\n")); s += "命令类型=" + (type.equals(CommandType.innerCommand)?"内部命令":"终端命令") + "\n" ; s += (code == null ? "" : ("功能码=" + code + "\n")) ; s += "返回中间件对命令处理结果=" + (noRtMwDealRes == null?"返回":(noRtMwDealRes?"不返回":"返回") + "\n") ; if(param != null){ s += "参数:" + param ; } if(attachment != null){ s += "其他数据:" + attachment ; } return s ; } /** * 对象转成json * @return json * @throws Exception 异常 */ @SuppressWarnings("unused") public String toJson()throws Exception{ try{ return JSON.toJSONString(this) ; }catch(Exception e){ throw new Exception(e.getMessage() , e ) ; } } /** * json转成对象 * @param json json * @return Command * @throws Exception 异常 */ @SuppressWarnings("unused") public static Command jsonToObject(String json)throws Exception{ try{ return JSON.parseObject(json, Command.class) ; }catch(Exception e){ throw new Exception(e.getMessage() , e ) ; } } /** * 创建返回的出错命令 * @param error 错误消息 * @param commandId 命令ID * @return Command */ @SuppressWarnings("unused") public Command createReturnErrorCommand(String error , String commandId, String code){ if(commandId == null){ this.setId(defaultId) ; }else{ this.setId(commandId) ; } this.code = code ; this.type = CommandType.resultCommand ; CommandBackParam p = new CommandBackParam() ; p.setSuccess(false) ; p.setMessage(error) ; this.param = p ; return this ; } /** * 创建返回的简单成功命令,针对监控中间件的命令 * @param message 消息 * @param commandId 命令ID * @param code 功能码 * @return Command */ @SuppressWarnings("unused") public Command createReturnSuccessCommand(String message , String commandId, String code){ if(commandId == null){ this.setId(defaultId) ; }else{ this.setId(commandId) ; } this.code = code ; this.type = CommandType.resultCommand ; CommandBackParam p = new CommandBackParam() ; p.setSuccess(true) ; p.setMessage(message) ; this.param = p ; return this ; } /* public static void main(String[] args){ String json = "{\"attachment\":null,\"code\":\"03\",\"deviceId\":\"d8c9601f214747d98d47a4736e5\",\"id\":\"999999999\",\"meterNo\":\"23040600377\",\"productId\":16873252,\"param\":\"0002\",\"protocol\":\"HAC_NBhV2_5\",\"rtuAddr\":\"863318060168996\",\"type\":\"outerCommand\"}" ; try { Command com = jsonToObject(json) ; System.out.println(com.param); } catch (Exception e) { e.printStackTrace(); } } */ public String getId() { return id; } public Command setId(String id) { this.id = id; return this ; } public String getRtuAddr() { return rtuAddr; } public Command setRtuAddr(String rtuAddr) { this.rtuAddr = rtuAddr; return this ; } public String getProtocol() { return protocol; } public void setProtocol(String protocol) { this.protocol = protocol; } public String getType() { return type; } public Command setType(String type) { this.type = type; return this ; } public String getCode() { return code; } public Command setCode(String code) { this.code = code; return this ; } @SuppressWarnings("unused") public Boolean getNoRtMwDealRes() { return noRtMwDealRes; } @SuppressWarnings("unused") public void setNoRtMwDealRes(Boolean noRtMwDealRes) { this.noRtMwDealRes = noRtMwDealRes; } public Object getParam() { return param; } public Command setParam(Object param) { this.param = param; return this ; } public Object getAttachment() { return attachment; } @SuppressWarnings("unused") public Command setAttachment(Object attachment) { this.attachment = attachment; return this ; } }