###Json格式
用户端与服务器通信的消息由于是Json封装的(主要是定义好key和value),项目中采用Gson库,将消息抽象成类似Java中的bean类,每种消息就是一种对象。json字符串<——>消息对象(数组),可以在开发中快速的转化。
——>具体包括如下几个字段:
- 消息类型(下面会具体分析)
- 消息来源:(USER和SERVER)
- 消息内容:
HashMap < String, String > content
由于消息内容的长度不是固定的,通过集合的方式定义。
——>消息类:
public class Message {
private static final Logger logger = LoggerFactory.getLogger(Message.class);
//消息类型
private MessageType type;
//消息来源
private String from;
//消息内容(注意record消息的content是数组格式,需要单独解析)
private HashMap<String, String> content;
public Message() {
content = new HashMap<>(0);
}
public Message(int num) {
content = new HashMap<>(num);
}
public MessageType getType() {
return type;
}
public void setType(MessageType type) {
this.type = type;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public HashMap<String, String> getContent() {
return content;
}
public void setContent(HashMap<String, String> content) {
this.content = content;
}
public void addContent(String tag, String field) {
content.put(tag, field);
}
public int numContent() {
return content.size();
}
public boolean hasContentByTag(String tag) {
return content.containsKey(tag);
}
public String getContentByTag(String tag) {
if (!hasContentByTag(tag)) {
logger.error("content tag is non-existent in message!");
}
return content.get(tag);
}
public String getTagForSingleContent() {
if (content.size() != 1) {
logger.error("content of instruction is not single content!");
}
return content.keySet().iterator().next();
}
public String getFieldForSingleContent() {
if (content.size() != 1) {
logger.error("content of instruction is not single content!");
}
return content.values().iterator().next();
}
}
###消息类型
- 发送类型消息
- BIND:绑定设备(携带PID)
- UNBIND:解除绑定
- MODEL:模式(控制模式、查询模式)
- CONTROL_POSTURE:控制调整姿态(携带姿态位(reset,head,leg,left,right,lift,before,after)和角度)
- QUERY_POSTURE:查询姿态(①向设备直接查询;②向数据库查询(目前的方式))
- QUERY_RECORD:查询记录(主要从数据库中查询,①查询所有日期,②查询指定日期中的所有姿态记录)
- 接收类型消息
- POSTURE:姿态信息
- DONE:设备调整完成,携带姿态信息(还需将姿态记录到数据库中)
- UNDONE:设备正在工作,请稍后发送
- RECORD:数据库记录(日期、指定日期下的姿态)
- SERVER_FEEDBACK:服务器反馈(包括状态码和状态内容,后面具体分析)
key\value | type(类型) | from(来源)) | content(指令内容) |
---|---|---|---|
发送消息类型: | USER | (key/value子对象) | |
BIND | USER | {“PID”:“admin1”} | |
UNBIND | USER | null | |
QUERY_POSTURE | USER | null | |
CONTROL_POSTURE | USER | 如{“head”:“2”} | |
QUERY_RECORD | USER | ①查询记录的所有日期:{“date”:""},②查询指定日期内姿态:{“posture”:“2018-07-10”} | |
接收消息类型: | |||
POSTURE | SERVER | {“head”:0,“leg”:0,“left”:0,“right”:0,“lift”:0,“before”:0,“after”:0,“time”:“2018-07-24 20:01:00”} (8个key/value) | |
DONE | SERVER | {“head”:0,“leg”:0,“left”:0,“right”:0,“lift”:0,“before”:0,“after”:0,“time”:“2018-07-24 20:01:00”} (8个key/value) | |
UNDONE | SERVER | {“1”:“其他用户操作中,请稍后…”}{“2”:“手控盒操作中,请稍后…”} | |
反馈记录所有日期 | RECORD | dates | [{“date”:“2019-04-23”,“todate”:0},…,…] |
反馈指定日期内姿态 | RECORD | dates | 见注释一 |
SERVER_FEEDBACK | SERVER | (一个key/value) |
- 注释一:
[{"id":2859,"pid":"admin1","user_type":"remote","uid":"7f00000108ff00000018","posture_head":0,"posture_leg":0,"posture_left":10,"posture_right":0,"posture_lift":0,"posture_before":0,"posture_after":0,"time":"2019-04-22 23:02:30"},..,...]
###服务器反馈(SERVER_FEEDBACK)
/**
* 检测
*/
const SUCCESS_COMM_CODE = "000";
const SUCCESS_COMM_TAG = "Communication successful.";
const SUCCESS_COMM_TEXT = "通信成功";
const FAIL_ILLEGAL_INSTRUTIONS_CODE = "001";
const FAIL_ILLEGAL_INSTRUIONS_TAG = "illegal instructions!";
const FAIL_ILLEGAL_INSTRUIONS_TEXT = "指令异常";
const FAIL_UNBOUND_CODE = "002";
const FAIL_UNBOUND_TAG = "PID is unbound!";
const FAIL_UNBOUND_TEXT = "设备未绑定";
const FAIL_OFFLINE_CODE = "003";
const FAIL_OFFLINE_TAG = "bed is offline!";
const FAIL_OFFLINE_TEXT = "设备已下线";
/**
* 绑定
*/
const SUCCESS_BIND_CODE = "010";
const SUCCESS_BIND_TAG = "bind successful.";
const SUCCESS_BIND_TEXT = "绑定成功";
const FAIL_BIND_PIDNULL_CODE = "011";
const FAIL_BIND_PIDNULL_TAG = "bind failed! PID is null.";
const FAIL_BIND_PIDNULL_TEXT = "绑定失败!PID不正确";
const FAIL_BIND_OFFLINE_CODE = "012";
const FAIL_BIND_OFFLINE_TAG = "bind failed! Bed is offline.";
const FAIL_BIND_OFFLINE_TEXT = "绑定失败!设备不在线";
/**
* 解除绑定
*/
const SUCCESS_UNBIND_CODE = "020";
const SUCCESS_UNBIND_TAG = "unbind successful.";
const SUCCESS_UNBIND_TEXT = "解除绑定成功";
const FAIL_UNBIND_CODE = "021";
const FAIL_UNBIND_TAG = "unbind fail!";
const FAIL_UNBIND_TEXT = "解除绑定失败";
/**
* 控制姿态
*/
const SUCCESS_CONTROLPOSTURE_CODE = "030";
const SUCCESS_CONTROLPOSTURE_TAG = "forwarding control_posture successful. ";
const SUCCESS_CONTROLPOSTURE_TEXT = "调整姿态成功 ";
const FAIL_CONTROLPOSTURE_POS_CODE = "031";
const FAIL_CONTROLPOSTURE_POS_TAG = "forwarding control_posture fail! pos is error.";
const FAIL_CONTROLPOSTURE_POS_TEXT = "调整姿态失败!姿态位不正确";
const FAIL_CONTROLPOSTURE_ANGLE_CODE = "032";
const FAIL_CONTROLPOSTURE_ANGLE_TAG = "forwarding control_posture fail! angle is error.";
const FAIL_CONTROLPOSTURE_ANGLE_TEXT = "调整姿态失败!角度不正确";
const FAIL_CONTROLPOSTURE_WORKING_CODE = "033";
const FAIL_CONTROLPOSTURE_WORKING_TAG = "bed is working!";
const FAIL_CONTROLPOSTURE_WORKING_TEXT = "设备正在工作中,请稍后..";