package com.dy.common.mw.protocol; 
 | 
  
 | 
import java.io.Serializable; 
 | 
  
 | 
import com.alibaba.fastjson2.JSON ; 
 | 
import com.alibaba.fastjson2.JSONObject; 
 | 
  
 | 
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;  
 | 
     
 | 
    /** 
 | 
     * RTU协议名称,这个可以为空,因为RTU上行数据解析出协议名称,然后系统把协议名称保存在RTU数据记录中,从此协议名称就不再空了 
 | 
     */ 
 | 
    public String protocol; 
 | 
     
 | 
    /** 
 | 
     * RTU 地址 
 | 
     */ 
 | 
    public String rtuAddr ; 
 | 
  
 | 
    /** 
 | 
     * 命令类型:Rtu命令、针对监控中间件的命令 
 | 
     * 由com.dy.common.mw.protocol.CommandType类定义 
 | 
     */ 
 | 
    public String type ; 
 | 
     
 | 
    /** 
 | 
     * 功能码 
 | 
     */ 
 | 
    public String code ;  
 | 
  
 | 
    /** 
 | 
     * rtu返回命令结果 发向目的地web URL 
 | 
     */ 
 | 
    public String rtuResultSendWebUrl ; 
 | 
  
 | 
    /** 
 | 
     * 具体参数数据 
 | 
     */ 
 | 
    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)?"内部命令":"RTU命令") + "\n" ; 
 | 
        s += (code == null ? "" : ("功能码=" + code + "\n")) ; 
 | 
        s += (rtuResultSendWebUrl == null ? "" : ("回调网址=" + rtuResultSendWebUrl + "\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{ 
 | 
            String json = JSON.toJSONString(this) ; 
 | 
            return json ; 
 | 
        }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{ 
 | 
            JSONObject jsonObject = JSON.parseObject(json); 
 | 
            Command command = jsonObject.to(Command.class) ; 
 | 
            //command.param = jsonObject.getObject("param", class); 
 | 
            return command ; 
 | 
        }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 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 getRtuResultSendWebUrl() { 
 | 
        return rtuResultSendWebUrl; 
 | 
    } 
 | 
  
 | 
    public void setRtuResultSendWebUrl(String rtuResultSendWebUrl) { 
 | 
        this.rtuResultSendWebUrl = rtuResultSendWebUrl; 
 | 
    } 
 | 
  
 | 
    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 ; 
 | 
    } 
 | 
    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 ; 
 | 
    } 
 | 
     
 | 
} 
 |