DecoderData.java 7.74 KB
package com.glsx.pushsdk.common;

import java.util.ArrayList;
import java.util.List;

import com.glsx.pushsdk.model.Entry;
import com.glsx.pushsdk.model.ForceExitEntry;
import com.glsx.pushsdk.model.ForceOffLineData;
import com.glsx.pushsdk.model.GetOffLineContentItem;
import com.glsx.pushsdk.model.GetOffLineIdItem;
import com.glsx.pushsdk.model.GetOfflLineContentDataResult;
import com.glsx.pushsdk.model.GetOfflLineDataResult;
import com.glsx.pushsdk.model.LoginResult;
import com.glsx.pushsdk.model.SdkVersionResult;
import com.glsx.pushsdk.model.proto.MsgClient;
import com.google.protobuf.InvalidProtocolBufferException;

/**
 * 解析服务器返回业务数据
 *
 * @author YFZX-yulg
 */
public class DecoderData {

    /**
     * 获取返回的登录数据
     *
     * @param bytes
     * @return
     * @throws InvalidProtocolBufferException
     */
    public static LoginResult getLoginResult(byte[] bytes) {

        LoginResult mLoginResult = new LoginResult();
        try {
            MsgClient.ClientLoginRsp loginRsp = MsgClient.ClientLoginRsp
                    .parseFrom(bytes);
            mLoginResult.setCode(loginRsp.getNRetCode());
            mLoginResult.setErrMessage(loginRsp.getChErrDesc().toStringUtf8());
            mLoginResult.setGlsxkey(loginRsp.getChGlsxKey().toStringUtf8());
            mLoginResult.setSdkVersion(loginRsp.getChSdkVno().toStringUtf8());
        } catch (InvalidProtocolBufferException e) {
        }

        return mLoginResult;
    }

    /**
     * 心跳包回调数据
     *
     * @param bytes
     * @return
     */
    public static Entry getHearBeat(byte[] bytes) {

        Entry mEntry = new Entry();
        try {
            MsgClient.ClientHeartBeatRsp heartBeatRsp = MsgClient.ClientHeartBeatRsp
                    .parseFrom(bytes);
            mEntry.setCode(heartBeatRsp.getNRetCode());
            mEntry.setErrMessage(heartBeatRsp.getChErrDesc().toStringUtf8());
        } catch (InvalidProtocolBufferException e) {
        }

        return mEntry;
    }

    /**
     * 获取离线回调数据
     *
     * @param bytes
     * @return
     */
    public static GetOfflLineDataResult getOffLineData(byte[] bytes) {

        GetOfflLineDataResult mEntry = new GetOfflLineDataResult();
        try {
            MsgClient.ClientGetOfflienMsgRsp getOffLineMsgRsp = MsgClient.ClientGetOfflienMsgRsp
                    .parseFrom(bytes);
            mEntry.setCode(getOffLineMsgRsp.getNRetCode());
            mEntry.setErrMessage(getOffLineMsgRsp.getChErrDesc().toStringUtf8());
            mEntry.setCount(getOffLineMsgRsp.getMsgIdsCount());
            List<GetOffLineIdItem> mGetOffLineIdItem = new ArrayList<GetOffLineIdItem>();
            for (int i = 0; i < getOffLineMsgRsp.getMsgIdsList().size(); i++) {
                GetOffLineIdItem item = new GetOffLineIdItem();
                item.setMsgId(getOffLineMsgRsp.getMsgIdsList().get(i)
                        .getChMsgId().toStringUtf8());
                mGetOffLineIdItem.add(item);
            }
            mEntry.setmListMsgId(mGetOffLineIdItem);

        } catch (InvalidProtocolBufferException e) {
        }

        return mEntry;
    }

    /**
     * 根据离线ID获取离线内容回调数据
     *
     * @param bytes
     * @return
     */
    public static GetOfflLineContentDataResult getOffLineContextData(
            byte[] bytes) {

        GetOfflLineContentDataResult mEntry = new GetOfflLineContentDataResult();
        try {
            MsgClient.ClientGetMsgContextByMsgIdsRsp rsp = MsgClient.ClientGetMsgContextByMsgIdsRsp
                    .parseFrom(bytes);

            mEntry.setCode(rsp.getNRetCode());
            mEntry.setErrMessage(rsp.getChErrDesc().toStringUtf8());

            if (rsp.getOMsgContextList() != null
                    && rsp.getOMsgContextList().size() > 0) {

                List<GetOffLineContentItem> mList = new ArrayList<GetOffLineContentItem>();
                for (int i = 0; i < rsp.getOMsgContextList().size(); i++) {
                    GetOffLineContentItem item = new GetOffLineContentItem();
                    item.setMsgId(rsp.getOMsgContextList().get(i).getChMsgId()
                            .toStringUtf8());
                    item.setMsgContext(rsp.getOMsgContextList().get(i)
                            .getChContext().toStringUtf8());
                    item.setMsgTitle(rsp.getOMsgContextList().get(i)
                            .getChTitle().toStringUtf8());
                    item.setMsgType(rsp.getOMsgContextList().get(i).getNType());
                    item.setMsgCreateTime(rsp.getOMsgContextList().get(i)
                            .getChMsgTime().toStringUtf8());
                    item.setMsgAttribute(rsp.getOMsgContextList().get(i).getChAttribute().toStringUtf8());

                    mList.add(item);
                }
                mEntry.setmList(mList);
            }

        } catch (InvalidProtocolBufferException e) {
        }

        return mEntry;
    }

    /**
     * 服务器推送消息
     *
     * @param bytes
     * @return
     */
    public static GetOffLineContentItem getSeviceToClientMsg(byte[] bytes) {

        GetOffLineContentItem mEntry = new GetOffLineContentItem();
        try {
            MsgClient.NotifyMsgToClientReq rsp = MsgClient.NotifyMsgToClientReq
                    .parseFrom(bytes);
            mEntry.setMsgId(rsp.getChMsgId().toStringUtf8());
            mEntry.setMsgContext(rsp.getChContext().toStringUtf8());
            mEntry.setMsgTitle(rsp.getChTitle().toStringUtf8());
            mEntry.setMsgType(rsp.getNType());
            mEntry.setMsgCreateTime(rsp.getChMsgTime().toStringUtf8());
            mEntry.setMsgAttribute(rsp.getChAttribute().toStringUtf8());

        } catch (InvalidProtocolBufferException e) {
        }

        return mEntry;
    }


    /**
     * 强制退出数据
     *
     * @param bytes
     * @return
     */
    public static ForceOffLineData getForceOffLineData(byte[] bytes) {

        ForceOffLineData mEntry = new ForceOffLineData();
        try {
            MsgClient.ForceOfflineToClientReq rsp = MsgClient.ForceOfflineToClientReq
                    .parseFrom(bytes);
            mEntry.setChRstCode(rsp.getChRstCode());
            mEntry.setChGlsxKey(rsp.getChGlsxKey().toStringUtf8());

        } catch (InvalidProtocolBufferException e) {
        }

        return mEntry;
    }


    /**
     * 心跳包回调数据
     *
     * @param bytes
     * @return
     */
    public static SdkVersionResult getSdkVersionData(byte[] bytes) {

        SdkVersionResult mEntry = new SdkVersionResult();
        try {
            MsgClient.ClientGetSdkVersionRsp rsp = MsgClient.ClientGetSdkVersionRsp
                    .parseFrom(bytes);
            mEntry.setCode(rsp.getNRetCode());
            mEntry.setErrMessage(rsp.getChErrDesc().toStringUtf8());
            mEntry.setChAddr(rsp.getChAddr().toStringUtf8());
            mEntry.setChVerNo(rsp.getChVerNo().toStringUtf8());
            mEntry.setnForce(rsp.getNForce());
            mEntry.setnType(rsp.getNType());
            mEntry.setChDescr(rsp.getChDescr().toStringUtf8());

        } catch (InvalidProtocolBufferException e) {
        }

        return mEntry;
    }

    /**
     * 解析服务器强制退出请求
     *
     * @param bytes
     * @return
     */
    public static ForceExitEntry getForceExitLogin(byte[] bytes) {
        // TODO Auto-generated method stub
        ForceExitEntry mEntry = new ForceExitEntry();
        try {
            MsgClient.ForceOfflineToClientReq req = MsgClient.ForceOfflineToClientReq.parseFrom(bytes);
            mEntry.setChGlsxKey(req.getChGlsxKey().toStringUtf8());
            mEntry.setChRstCode(req.getChRstCode());
        } catch (InvalidProtocolBufferException e) {
            // TODO: handle exception
        }
        return mEntry;
    }


}