From 9e36c27071f2104f922130d0313073a5e056c12a Mon Sep 17 00:00:00 2001 From: Elvis Date: Wed, 6 Mar 2019 13:58:06 +0800 Subject: [PATCH] 添加服务的关闭与开启 --- C5/app/src/main/java/com/sunvote/xpadapp/MainActivity.java | 17 +++++++++++++++++ C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SendPacket.java | 25 ------------------------- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SenderManager.java | 283 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SerialManager.java | 29 ----------------------------- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/XpadApiService.java | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/XpadApiServiceInfoProxyManager.java | 236 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/BatchNumberVote.java | 2 +- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/BatchSingleVote.java | 2 +- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/LoginInVote.java | 2 +- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/NumberVote.java | 2 +- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SelectOtherVote.java | 4 ++-- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SelectVote.java | 2 +- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/ServiceTypeVote.java | 2 +- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SingleVote.java | 2 +- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/Vote.java | 2 +- C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/listener/AbsDownloadProcess.java | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/listener/IUploadListener.java | 18 ++++++++++++++++++ C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFFileDownloadModule.java | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFFileUploadModule.java | 358 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFMessageUploadModule.java | 301 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SendPacket.java | 25 +++++++++++++++++++++++++ C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SenderManager.java | 283 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SerialManager.java | 29 +++++++++++++++++++++++++++++ C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/XpadApiServiceInfoProxyManager.java | 240 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ C5/xpadapi/src/main/java/com/sunvote/xpadapi/util/LogUtil.java | 316 ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- C5/xpadprotocal/build/libs/xpadprotocal.jar | Bin 0 -> 76978 bytes C5/xpadprotocal/build/tmp/jar/MANIFEST.MF | 2 ++ 27 files changed, 1685 insertions(+), 921 deletions(-) delete mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SendPacket.java delete mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SenderManager.java delete mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SerialManager.java delete mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/XpadApiServiceInfoProxyManager.java create mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/listener/AbsDownloadProcess.java create mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/listener/IUploadListener.java create mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFFileDownloadModule.java create mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFFileUploadModule.java create mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFMessageUploadModule.java create mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SendPacket.java create mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SenderManager.java create mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SerialManager.java create mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/XpadApiServiceInfoProxyManager.java delete mode 100644 C5/xpadapi/src/main/java/com/sunvote/xpadapi/util/LogUtil.java create mode 100644 C5/xpadprotocal/build/libs/xpadprotocal.jar create mode 100644 C5/xpadprotocal/build/tmp/jar/MANIFEST.MF diff --git a/C5/app/src/main/java/com/sunvote/xpadapp/MainActivity.java b/C5/app/src/main/java/com/sunvote/xpadapp/MainActivity.java index 7930b71..bfa7fed 100644 --- a/C5/app/src/main/java/com/sunvote/xpadapp/MainActivity.java +++ b/C5/app/src/main/java/com/sunvote/xpadapp/MainActivity.java @@ -1236,6 +1236,23 @@ public class MainActivity extends BaseActivity implements ComListener { mBaseInfo = info; LogUtil.i(TAG, "baseEvent:" + info); meetingId = info.confId; + //attrib bit 6 1 表示服务可以申请,0,表示服务关闭 + if((mBaseInfo.attrib & 0x20) != 0){ + runOnUiThread(new Runnable() { + @Override + public void run() { + service.setVisibility(View.VISIBLE); + + } + }); + }else{ + runOnUiThread(new Runnable() { + @Override + public void run() { + service.setVisibility(View.GONE); + } + }); + } if (info.confId != 0) { if (dbm != null && dbm.confId != info.confId) { dbm.closeDB(); diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SendPacket.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SendPacket.java deleted file mode 100644 index 20c5c8e..0000000 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SendPacket.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.sunvote.xpadapi.service; - -public class SendPacket { - - private byte[] datas; - - /** - * 0 未发送 - * 1 已发送 - * 2 发送成功 - */ - private int sendOK ; - - /** - * 发送次数 - */ - private int sendTimes; - - /** - * 是否有序 有序需要第一个才发送 - * 无序,则可全部发送 - */ - private boolean canAll; - -} diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SenderManager.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SenderManager.java deleted file mode 100644 index cb8b0dd..0000000 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SenderManager.java +++ /dev/null @@ -1,283 +0,0 @@ -package com.sunvote.xpadapi.service; - -import com.sunvote.xpadapi.service.bean.BatchNumberVote; -import com.sunvote.xpadapi.service.bean.BatchSingleVote; -import com.sunvote.xpadapi.service.bean.LoginInVote; -import com.sunvote.xpadapi.service.bean.NumberVote; -import com.sunvote.xpadapi.service.bean.Packet; -import com.sunvote.xpadapi.service.bean.SelectOtherVote; -import com.sunvote.xpadapi.service.bean.SelectVote; -import com.sunvote.xpadapi.service.bean.ServiceTypeVote; -import com.sunvote.xpadapi.service.bean.SingleVote; -import com.sunvote.xpadapi.service.listener.OnSendPacket; -import com.sunvote.xpadapi.service.listener.OnSender; - -public final class SenderManager { - - private static SenderManager instance = null; - - private SenderManager() { - } - - public static SenderManager getInstance() { - if (instance == null) { - synchronized (SenderManager.class) { - if (instance == null) { - instance = new SenderManager(); - } - } - } - return instance; - } - - /** - * 发送 - * - * @param singleVote - */ - public void sendSingleVote(final SingleVote singleVote) { - sendSingleVote(singleVote, null); - } - - public void sendVoteAllOK(final OnSender sender) { - SingleVote singleVote = new SingleVote(1); - sendSingleVote(singleVote, sender); - } - - public void sendSingleVote(final SingleVote singleVote, final OnSender onSender) { - if (singleVote != null) { - byte[] datas = singleVote.toBytes(); - XpadApiServiceInfoProxyManager.getInstance().sendPacket(datas, new OnSendPacket() { - @Override - public void onSuccess(Packet packet) { - if (onSender != null) { - onSender.onSuccess(singleVote); - } - } - - @Override - public void onFail(Packet packet) { - if (onSender != null) { - onSender.onFail(singleVote); - } - } - }); - } else { - if (onSender != null) { - onSender.onFail(singleVote); - } - } - } - - public void sendSelectVote(final SelectVote selectVote) { - sendSelectVote(selectVote, null); - } - - public void sendSelectVote(final SelectVote selectVote, final OnSender onSender) { - if (selectVote != null) { - XpadApiServiceInfoProxyManager.getInstance().sendPacket(selectVote.toBytes(), new OnSendPacket() { - @Override - public void onSuccess(Packet packet) { - if (onSender != null) { - onSender.onSuccess(selectVote); - } - } - - @Override - public void onFail(Packet packet) { - if (onSender != null) { - onSender.onFail(selectVote); - } - } - }); - } else { - if (onSender != null) { - onSender.onFail(selectVote); - } - } - } - - public void sendNumberVote(final NumberVote numberVote) { - sendNumberVote(numberVote, null); - } - - public void sendNumberVote(final NumberVote numberVote, final OnSender sender) { - if (numberVote != null) { - XpadApiServiceInfoProxyManager.getInstance().sendPacket(numberVote.toBytes(), new OnSendPacket() { - @Override - public void onSuccess(Packet packet) { - if (sender != null) { - sender.onSuccess(numberVote); - } - } - - @Override - public void onFail(Packet packet) { - if (sender != null) { - sender.onFail(numberVote); - } - } - }); - } else { - if (sender != null) { - sender.onFail(numberVote); - } - } - } - - public void sendLoginInVote(final LoginInVote loginInVote) { - sendLoginInVote(loginInVote, null); - } - - public void sendLoginInVote(final LoginInVote loginInVote, final OnSender sender) { - if (loginInVote != null) { - XpadApiServiceInfoProxyManager.getInstance().sendPacket(loginInVote.toBytes(), new OnSendPacket() { - @Override - public void onSuccess(Packet packet) { - if (sender != null) { - sender.onSuccess(loginInVote); - } - } - - @Override - public void onFail(Packet packet) { - if (sender != null) { - sender.onFail(loginInVote); - } - } - }); - } else { - if (sender != null) { - sender.onFail(loginInVote); - } - } - } - - public void sendCancelAllBatchSingleVote(final OnSender sender) { - BatchSingleVote batchSingleVote = new BatchSingleVote(); - sendBatchSingleVote(batchSingleVote, sender); - } - - public void sendSubmitlAllBatchSingleVote(final OnSender sender) { - BatchSingleVote batchSingleVote = new BatchSingleVote(); - batchSingleVote.setAllOk(1); - sendBatchSingleVote(batchSingleVote, sender); - } - - public void sendBatchSingleVote(final BatchSingleVote batchSingleVote) { - sendBatchSingleVote(batchSingleVote, null); - } - - public void sendBatchSingleVote(final BatchSingleVote batchSingleVote, final OnSender sender) { - if (batchSingleVote != null) { - XpadApiServiceInfoProxyManager.getInstance().sendPacket(batchSingleVote.toBytes(), new OnSendPacket() { - @Override - public void onSuccess(Packet packet) { - if (sender != null) { - sender.onSuccess(batchSingleVote); - } - } - - @Override - public void onFail(Packet packet) { - if (sender != null) { - sender.onFail(batchSingleVote); - } - } - }); - } else { - if (sender != null) { - sender.onFail(batchSingleVote); - } - } - } - - public void sendBatchNumberVoteOK(final BatchNumberVote batchNumberVote, final OnSender sender) { - batchNumberVote.setAllOk(1); - sendBatchNumberVote(batchNumberVote, sender); - } - - public void sendBatchNumberVote(final BatchNumberVote batchNumberVote) { - sendBatchNumberVote(batchNumberVote, null); - } - - public void sendBatchNumberVote(final BatchNumberVote batchNumberVote, final OnSender sender) { - if (batchNumberVote != null) { - XpadApiServiceInfoProxyManager.getInstance().sendPacket(batchNumberVote.toBytes(), new OnSendPacket() { - @Override - public void onSuccess(Packet packet) { - if (sender != null) { - sender.onSuccess(batchNumberVote); - } - } - - @Override - public void onFail(Packet packet) { - if (sender != null) { - sender.onFail(batchNumberVote); - } - } - }); - } else { - if (sender != null) { - sender.onFail(batchNumberVote); - } - } - } - - public void sendSelectOtherVote(SelectOtherVote selectOtherVote) { - sendSelectOtherVote(selectOtherVote, null); - } - - public void sendSelectOtherVote(final SelectOtherVote selectOtherVote, final OnSender sender) { - if (selectOtherVote != null) { - XpadApiServiceInfoProxyManager.getInstance().sendPacket(selectOtherVote.toBytes(), new OnSendPacket() { - @Override - public void onSuccess(Packet packet) { - if (sender != null) { - sender.onSuccess(selectOtherVote); - } - } - - @Override - public void onFail(Packet packet) { - if (sender != null) { - sender.onFail(selectOtherVote); - } - } - }); - } else { - if (sender != null) { - sender.onFail(selectOtherVote); - } - } - } - - public void sendServiceTypeVote(final ServiceTypeVote serviceTypeVote) { - sendServiceTypeVote(serviceTypeVote, null); - } - - public void sendServiceTypeVote(final ServiceTypeVote serviceTypeVote, final OnSender sender) { - if (serviceTypeVote != null) { - XpadApiServiceInfoProxyManager.getInstance().sendPacket(serviceTypeVote.toBytes(), new OnSendPacket() { - @Override - public void onSuccess(Packet packet) { - if (sender != null) { - sender.onSuccess(serviceTypeVote); - } - } - - @Override - public void onFail(Packet packet) { - if (sender != null) { - sender.onFail(serviceTypeVote); - } - } - }); - } else { - if (sender != null) { - sender.onFail(serviceTypeVote); - } - } - } -} diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SerialManager.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SerialManager.java deleted file mode 100644 index 17cd62d..0000000 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/SerialManager.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.sunvote.xpadapi.service; - -public final class SerialManager { - - private static SerialManager instance = null ; - - private SerialManager(){ - } - - public static SerialManager getInstance(){ - if(instance == null){ - synchronized(SerialManager.class){ - if(instance == null){ - instance = new SerialManager(); - } - } - } - return instance; - } - - private byte seq = 0 ; - - public byte getSeq() { - if (seq == 0xfe) { - seq = 0 ; - } - return ++seq; - } -} diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/XpadApiService.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/XpadApiService.java index 479dff3..ae5007d 100644 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/XpadApiService.java +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/XpadApiService.java @@ -5,38 +5,39 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.hardware.usb.UsbManager; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; -import com.sunvote.udptransfer.UDPModule; import com.sunvote.udptransfer.stream.SunVoteInputStream; import com.sunvote.udptransfer.work.BaseStationProcessWork; import com.sunvote.udptransfer.work.RepeatMessageManager; import com.sunvote.udptransfer.work.SDKProcessWork; +import com.sunvote.util.LogUtil; import com.sunvote.xpadapi.ISunvoteApi; -import com.sunvote.xpadapi.service.bean.HeartBeat; -import com.sunvote.xpadapi.service.bean.Packet; -import com.sunvote.xpadapi.service.listener.OnDataReceiver; import com.sunvote.xpadapi.service.bean.BaseInfo; import com.sunvote.xpadapi.service.bean.BaseVoteInfo; +import com.sunvote.xpadapi.service.bean.HeartBeat; import com.sunvote.xpadapi.service.bean.KeypadInfo; import com.sunvote.xpadapi.service.bean.ModelInfo; import com.sunvote.xpadapi.service.bean.OnLineInfo; +import com.sunvote.xpadapi.service.bean.Packet; +import com.sunvote.xpadapi.service.listener.OnDataReceiver; +import com.sunvote.xpadapi.service.logic.XpadApiServiceInfoProxyManager; import com.sunvote.xpadapi.usb.UsbTransferManager; import com.sunvote.xpadapi.util.ByteUtils; import com.sunvote.xpadapi.util.Cons; import com.sunvote.xpadapi.util.Crc16; -import com.sunvote.xpadapi.util.LogUtil; import java.io.ByteArrayOutputStream; -import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +/** + * @Author Elvis + */ public class XpadApiService extends Service { private static final String TAG = XpadApiService.class.getSimpleName(); @@ -52,7 +53,7 @@ public class XpadApiService extends Service { try { temp.onDataReceiver(datas); } catch (Exception ex) { - LogUtil.e(TAG, "onDataReceiver", ex); + LogUtil.e(TAG, "onDataReceiver" + temp.getClass().getSimpleName(), ex); } } } @@ -173,19 +174,6 @@ public class XpadApiService extends Service { }; - private UsbTransferManager.OnUsbConnectListener connectListener = new UsbTransferManager.OnUsbConnectListener() { - @Override - public boolean onConnect(boolean isConnected) { - if (isConnected) { - // - getKeypadParam(); - sleepTimes(100); - getWorkMode(); - } - return true; - } - }; - private Runnable sendTask = new Runnable() { @Override public void run() { @@ -309,10 +297,11 @@ public class XpadApiService extends Service { } private void firmUpdateResponse(byte[] datas) { + } private void voteResultSendResponse(byte[] datas) { - + // 下层模块收到了,说明不了什么,所以不干什么 } private void checkBaseStatusResponse(byte[] datas) { @@ -583,4 +572,159 @@ public class XpadApiService extends Service { sendInnerPacket(packet); } + public void applyFileUpload(int length, String filename, byte anstype,byte index) { + byte[] mBuffer = new byte[0x1F + 4]; + Arrays.fill(mBuffer, (byte) 0x0); + mBuffer[0] = (byte) 0xF5; + mBuffer[1] = (byte) 0xAA; + mBuffer[2] = (byte) 0xAA; + mBuffer[3] = (byte) 0x1F; + mBuffer[4] = 0x73; + mBuffer[5] = (byte) 0XFF;// 流水号 + mBuffer[6] = (byte) 0X01;// 流水号 + mBuffer[7] = (byte) 15;// MSGTYPE + mBuffer[8] = (byte) anstype;// ANSTYPE 添加5 文件上传指令 + mBuffer[9] = index;// 高位 + Packet packet = new Packet(); + packet.setDatas(mBuffer); + packet.setSendNo(Packet.CommunicationTestMatchNum); + sendInnerPacket(packet); + } + + public void packetReceptionConfirmed(byte[] keyid, byte packid, byte packH, byte packtype) { + byte[] mBuffer = new byte[0x1F + 4]; + Arrays.fill(mBuffer, (byte) 0x0); + mBuffer[0] = (byte) 0xF5; + mBuffer[1] = (byte) 0xAA; + mBuffer[2] = (byte) 0xAA; + mBuffer[3] = (byte) 0x1F; + + mBuffer[4] = (byte) 0xA0; + int keyId = XpadApiServiceInfoProxyManager.getInstance().getKeypadInfo().getKeyId(); + mBuffer[5] = (byte)((keyId >> 8) & 0xFF); + mBuffer[6] = (byte)((keyId) & 0xFF); + + mBuffer[7] = 3;//CMD 3 多包接收完毕应答 + mBuffer[8] = packtype;//PACKTYPE 多包类型 + mBuffer[9] = packid;//数据包标识码 + Packet packet = new Packet(); + packet.setDatas(mBuffer); + packet.setSendNo(Packet.CommunicationTestMatchNum); + sendInnerPacket(packet); + } + + public void uploadFileData(byte[] keyid, byte packid, byte packH, byte packL, byte[] datas, byte packtype) { + uploadFileData(keyid, packid, packH, packL, datas, 0, datas.length, packtype); + } + + /** + * @param keyid 键盘编号 + * @param packid 包id + * @param packH 数据段id + * @param packL 数据片id + * @param datas 数据 + * @param offset 偏移位置 + * @param length 长度 + * @Auther Elvis + * 上传数据包(多包应答) + */ + public void uploadFileData(byte[] keyid, byte packid, byte packH, byte packL, byte[] datas, int offset, int length, byte packtype) { + LogUtil.i(TAG, "onMutilPacketData packid=" + packid + ",packH=" + packH + ",packL=" + packL + ",offset=" + offset); + byte[] mBuffer = new byte[0x1F + 4]; + Arrays.fill(mBuffer, (byte) 0x0); + mBuffer[0] = (byte) 0xF5; + mBuffer[1] = (byte) 0xAA; + mBuffer[2] = (byte) 0xAA; + mBuffer[3] = (byte) 0x1F; + + mBuffer[4] = (byte) 0xA0; + int keyId = XpadApiServiceInfoProxyManager.getInstance().getKeypadInfo().getKeyId(); + mBuffer[5] = (byte)((keyId >> 8) & 0xFF); + mBuffer[6] = (byte)((keyId) & 0xFF); + + mBuffer[7] = 2;//CMD 1 回应多包数据 + mBuffer[8] = packtype;//PACKTYPE 多包类型 + mBuffer[9] = packid;//数据包标识码 + mBuffer[10] = packH;//数据段编号 + mBuffer[11] = packL;//数据片编号 + for (int i = 0; i < length && i < 16 && i < datas.length - offset; i++) { + mBuffer[12 + i] = datas[offset + i]; + } + Packet packet = new Packet(); + packet.setDatas(mBuffer); + packet.setSendNo(Packet.CommunicationTestMatchNum); + sendInnerPacket(packet); + } + + /** + * @param packid 包id + * 多包接收确认(多包接收应答) + */ + public void packetConfirmation(byte[] keyid, byte packid, byte packH, byte packL, byte[] names, byte packtype) { + LogUtil.i(TAG, "onMutilPacketRespone packid=" + packid + ",packH=" + packH + ",packL=" + packL + ",names=" + names); + byte[] mBuffer = new byte[0x1F + 4]; + Arrays.fill(mBuffer, (byte) 0x0); + mBuffer[0] = (byte) 0xF5; + mBuffer[1] = (byte) 0xAA; + mBuffer[2] = (byte) 0xAA; + mBuffer[3] = (byte) 0x1F; + + mBuffer[4] = (byte) 0xA0; + int keyId = XpadApiServiceInfoProxyManager.getInstance().getKeypadInfo().getKeyId(); + mBuffer[5] = (byte)((keyId >> 8) & 0xFF); + mBuffer[6] = (byte)((keyId) & 0xFF); + mBuffer[5] = keyid[0]; + mBuffer[6] = keyid[1]; + + mBuffer[7] = 1;//CMD 1 + mBuffer[8] = packtype;//PACKTYPE 多包类型 + mBuffer[9] = packid;//数据包标识码 + mBuffer[10] = packH; + mBuffer[11] = packL; + if (names != null) { + for (int i = 0; i < 14 && i < names.length; i++) { + mBuffer[12 + i] = names[i]; + } + } + + Packet packet = new Packet(); + packet.setDatas(mBuffer); + packet.setSendNo(Packet.CommunicationTestMatchNum); + sendInnerPacket(packet); + } + + private void keepFirmMode() { + byte[] mBuffer = new byte[0x1F + 4]; + Arrays.fill(mBuffer, (byte) 0x0); + mBuffer[0] = (byte) 0xF5; + mBuffer[1] = (byte) 0xAA; + mBuffer[2] = (byte) 0xAA; + mBuffer[3] = (byte) 10; + + mBuffer[4] = 0x78; + mBuffer[5] = 0x02; + + Packet packet = new Packet(); + packet.setDatas(mBuffer); + packet.setSendNo(Packet.CommunicationTestMatchNum); + sendInnerPacket(packet); + } + + private void eraseFlash(int fileLen) { + byte[] mBuffer = new byte[0x1F + 4]; + Arrays.fill(mBuffer, (byte) 0x0); + mBuffer[0] = (byte) 0xF5; + mBuffer[1] = (byte) 0xAA; + mBuffer[2] = (byte) 0xAA; + mBuffer[3] = (byte) 10; + mBuffer[4] = 0x78; + mBuffer[5] = 0x03; + mBuffer[6] = 0; + mBuffer[7] = (byte) (fileLen / 1024); + + Packet packet = new Packet(); + packet.setDatas(mBuffer); + packet.setSendNo(Packet.CommunicationTestMatchNum); + sendInnerPacket(packet); + } } diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/XpadApiServiceInfoProxyManager.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/XpadApiServiceInfoProxyManager.java deleted file mode 100644 index e3962bc..0000000 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/XpadApiServiceInfoProxyManager.java +++ /dev/null @@ -1,236 +0,0 @@ -package com.sunvote.xpadapi.service; - -import com.sunvote.xpadapi.service.bean.BaseInfo; -import com.sunvote.xpadapi.service.bean.BaseVoteInfo; -import com.sunvote.xpadapi.service.bean.KeypadInfo; -import com.sunvote.xpadapi.service.bean.ModelInfo; -import com.sunvote.xpadapi.service.bean.OnLineInfo; -import com.sunvote.xpadapi.service.bean.Packet; -import com.sunvote.xpadapi.service.listener.BaseInfoChanageListener; -import com.sunvote.xpadapi.service.listener.BaseVoteInfoChanageListener; -import com.sunvote.xpadapi.service.listener.KeyPadinfoChanageListener; -import com.sunvote.xpadapi.service.listener.ModelInfoChanageListener; -import com.sunvote.xpadapi.service.listener.OnSendPacket; -import com.sunvote.xpadapi.service.listener.OnlineInfoChanageListener; -import com.sunvote.xpadapi.util.LogUtil; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -/** - * 服务信息 - */ -public final class XpadApiServiceInfoProxyManager { - - public static final String TAG = XpadApiServiceInfoProxyManager.class.getSimpleName(); - private static XpadApiServiceInfoProxyManager instance = null ; - private List sendList = Collections.synchronizedList(new ArrayList()); - private Object lock = new Object(); - private String charset = "GB2312" ; - private XpadApiService service; - - public void setCharset(String charset) { - this.charset = charset; - } - - public void setService(XpadApiService service) { - this.service = service; - } - - public XpadApiService getService() { - return service; - } - - public String getCharset() { - return charset; - } - - private XpadApiServiceInfoProxyManager(){ - } - - public static XpadApiServiceInfoProxyManager getInstance(){ - if(instance == null){ - synchronized(XpadApiServiceInfoProxyManager.class){ - if(instance == null){ - instance = new XpadApiServiceInfoProxyManager(); - } - } - } - return instance; - } - - private ModelInfo modelInfo; - private ModelInfoChanageListener modelInfoListener; - - public void setModelInfo(ModelInfo modelInfo) { - if(modelInfoListener != null){ - try{ - modelInfoListener.onModelInfoChanage(modelInfo); - }catch (Exception ex){ - LogUtil.e(TAG,"onModelInfoChanage",ex); - } - } - this.modelInfo = modelInfo; - } - - public ModelInfo getModelInfo() { - return modelInfo; - } - - public void setModelInfoListener(ModelInfoChanageListener modelInfoListener) { - this.modelInfoListener = modelInfoListener; - } - - private BaseInfo baseInfo; - private BaseInfoChanageListener baseInfoListener; - - public void setBaseInfo(BaseInfo baseInfo) { - if(baseInfoListener != null){ - try { - baseInfoListener.onBaseInfoChange(baseInfo); - }catch (Exception ex){ - LogUtil.e(TAG,"onBaseInfoChange",ex); - } - } - this.baseInfo = baseInfo; - } - - public BaseInfo getBaseInfo() { - return baseInfo; - } - - public void setBaseInfoListener(BaseInfoChanageListener baseInfoListener) { - this.baseInfoListener = baseInfoListener; - } - - private BaseVoteInfo baseVoteInfo; - private BaseVoteInfoChanageListener baseVoteInfoListener; - - public void setBaseVoteInfo(BaseVoteInfo baseVoteInfo) { - if(baseVoteInfoListener != null){ - try { - baseVoteInfoListener.onBaseVoteInfoListener(baseVoteInfo); - }catch (Exception ex){ - LogUtil.e(TAG,"onBaseVoteInfoListener",ex); - } - } - this.baseVoteInfo = baseVoteInfo; - } - - public BaseVoteInfo getBaseVoteInfo() { - return baseVoteInfo; - } - - public void setBaseVoteInfoListener(BaseVoteInfoChanageListener baseVoteInfoListener) { - this.baseVoteInfoListener = baseVoteInfoListener; - } - - private KeypadInfo keypadInfo; - private KeyPadinfoChanageListener keyPadinfoChanageListener; - - public void setKeypadInfo(KeypadInfo keypadInfo) { - if(keyPadinfoChanageListener != null){ - try{ - keyPadinfoChanageListener.onKeyPadinfoChanage(keypadInfo); - }catch (Exception ex){ - LogUtil.e(TAG,"onKeyPadinfoChanage",ex); - } - } - this.keypadInfo = keypadInfo; - } - - public KeypadInfo getKeypadInfo() { - if(keypadInfo == null){ - keypadInfo = new KeypadInfo(); - } - return keypadInfo; - } - - public void setKeyPadinfoChanageListener(KeyPadinfoChanageListener keyPadinfoChanageListener) { - this.keyPadinfoChanageListener = keyPadinfoChanageListener; - } - - private OnLineInfo onLineInfo; - private OnlineInfoChanageListener onlineInfoChanageListener; - - public void setOnLineInfo(OnLineInfo onLineInfo) { - if(onlineInfoChanageListener != null){ - try{ - onlineInfoChanageListener.onOnlineInfoChanage(onLineInfo); - }catch (Exception ex){ - LogUtil.e(TAG,"onOnlineInfoChanage",ex); - } - } - this.onLineInfo = onLineInfo; - } - - public OnLineInfo getOnLineInfo() { - return onLineInfo; - } - - public void setOnlineInfoChanageListener(OnlineInfoChanageListener onlineInfoChanageListener) { - this.onlineInfoChanageListener = onlineInfoChanageListener; - } - - public void sendPacket(byte[] datas,final OnSendPacket sendPacket){ - Packet packet = new Packet(); - packet.setDatas(datas); - packet.setSendNo(datas[5] & 0xFF); - packet.setOnSendPacket(new OnSendPacket() { - @Override - public void onSuccess(Packet packet) { - synchronized (lock){ - sendList.remove(packet); - } - try{ - sendPacket.onSuccess(packet); - }catch (Exception ex){ - LogUtil.e(TAG,"sendPacket.onSuccess",ex); - } - } - - @Override - public void onFail(Packet packet) { - synchronized (lock){ - sendList.remove(packet); - } - try{ - sendPacket.onFail(packet); - }catch (Exception ex){ - LogUtil.e(TAG,"sendPacket.onFail",ex); - } - } - }); - synchronized (lock){ - if(!exsit(packet)){ - sendList.add(packet); - } - } - if(service != null){ - service.immediately(); - } - } - - public List getSendList() { - return sendList; - } - - private boolean exsit(Packet packet){ - for(Packet packet1: sendList){ - if(packet.getSendNo() == packet1.getSendNo()){ - return true; - } - } - return false; - } - - public Packet findPacket(int sendNo){ - for(Packet packet: sendList){ - if(packet.getSendNo() == sendNo){ - return packet; - } - } - return null; - } -} diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/BatchNumberVote.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/BatchNumberVote.java index 4b97372..9251e87 100644 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/BatchNumberVote.java +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/BatchNumberVote.java @@ -1,6 +1,6 @@ package com.sunvote.xpadapi.service.bean; -import com.sunvote.xpadapi.service.SerialManager; +import com.sunvote.xpadapi.service.logic.SerialManager; import java.util.Arrays; diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/BatchSingleVote.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/BatchSingleVote.java index 8c08a25..4aa028f 100644 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/BatchSingleVote.java +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/BatchSingleVote.java @@ -1,6 +1,6 @@ package com.sunvote.xpadapi.service.bean; -import com.sunvote.xpadapi.service.SerialManager; +import com.sunvote.xpadapi.service.logic.SerialManager; import java.util.Arrays; diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/LoginInVote.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/LoginInVote.java index 2efde4f..2cb36ad 100644 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/LoginInVote.java +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/LoginInVote.java @@ -1,6 +1,6 @@ package com.sunvote.xpadapi.service.bean; -import com.sunvote.xpadapi.service.SerialManager; +import com.sunvote.xpadapi.service.logic.SerialManager; import java.util.Arrays; diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/NumberVote.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/NumberVote.java index d70f869..3f0fbfa 100644 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/NumberVote.java +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/NumberVote.java @@ -1,6 +1,6 @@ package com.sunvote.xpadapi.service.bean; -import com.sunvote.xpadapi.service.SerialManager; +import com.sunvote.xpadapi.service.logic.SerialManager; import java.util.Arrays; diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SelectOtherVote.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SelectOtherVote.java index 5cf081f..f8a409c 100644 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SelectOtherVote.java +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SelectOtherVote.java @@ -1,7 +1,7 @@ package com.sunvote.xpadapi.service.bean; -import com.sunvote.xpadapi.service.SerialManager; -import com.sunvote.xpadapi.service.XpadApiServiceInfoProxyManager; +import com.sunvote.xpadapi.service.logic.SerialManager; +import com.sunvote.xpadapi.service.logic.XpadApiServiceInfoProxyManager; import com.sunvote.xpadapi.util.LogUtil; import java.util.Arrays; diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SelectVote.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SelectVote.java index 7de230e..bc7e5b4 100644 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SelectVote.java +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SelectVote.java @@ -1,6 +1,6 @@ package com.sunvote.xpadapi.service.bean; -import com.sunvote.xpadapi.service.SerialManager; +import com.sunvote.xpadapi.service.logic.SerialManager; import java.util.Arrays; diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/ServiceTypeVote.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/ServiceTypeVote.java index 21a972d..4dd291d 100644 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/ServiceTypeVote.java +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/ServiceTypeVote.java @@ -1,6 +1,6 @@ package com.sunvote.xpadapi.service.bean; -import com.sunvote.xpadapi.service.SerialManager; +import com.sunvote.xpadapi.service.logic.SerialManager; import java.util.Arrays; diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SingleVote.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SingleVote.java index e004611..77e755f 100644 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SingleVote.java +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/SingleVote.java @@ -1,6 +1,6 @@ package com.sunvote.xpadapi.service.bean; -import com.sunvote.xpadapi.service.SerialManager; +import com.sunvote.xpadapi.service.logic.SerialManager; import java.util.Arrays; diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/Vote.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/Vote.java index de9184c..7458402 100644 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/Vote.java +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/bean/Vote.java @@ -1,6 +1,6 @@ package com.sunvote.xpadapi.service.bean; -import com.sunvote.xpadapi.service.SerialManager; +import com.sunvote.xpadapi.service.logic.SerialManager; import java.util.Arrays; diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/listener/AbsDownloadProcess.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/listener/AbsDownloadProcess.java new file mode 100644 index 0000000..41614fb --- /dev/null +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/listener/AbsDownloadProcess.java @@ -0,0 +1,115 @@ +package com.sunvote.xpadapi.service.listener; + +import java.io.File; +import java.io.IOException; + +/** + * Created by Elvis on 2018/3/20 11:18 + * Email:Eluis@psunsky.com + * 版权所有:长沙中天电子设计开发有限公司 + * Description: 人大通用版XPadAppRendaMac + */ + +public abstract class AbsDownloadProcess { + + private static String foldPath = "/sdcard/Sunvote/download/"; + + protected static String fileName = "default.obj"; + + private long length ; + private long curComplete = 0 ; + + private static File file ; + + public File getFile() { + if(file == null){ + curComplete = 0 ; + File folder = new File(foldPath); + if(!folder.exists()){ + folder.mkdirs(); + } + if(fileName == null || "".equals(fileName.trim())){ + fileName = "default.obj" ; + } + file = new File(foldPath , fileName); + file.deleteOnExit(); + if(!file.exists()){ + try { + file.createNewFile(); + } catch (IOException e) { + onDownloadError(e); + } + } + } + return file; + } + + protected long getLength() { + return length; + } + + public String getFoldPath() { + return foldPath; + } + + protected void setLength(long length) { + this.length = length; + curComplete = 0 ; + } + + public void setFileName(String fileName) { + if(!(fileName == null || "".equals(fileName.trim()))) { + this.fileName = fileName; + file = null; + } + } + + protected String getFileName() { + return fileName; + } + + public void setFoldPath(String foldPath) { + this.foldPath = foldPath; + } + + protected void onStartDownload(){ + onStartDownload(getFile(),getLength()); + } + + public abstract void onStartDownload(File file,long length); + + protected void onDownloadAddProcess(int curPack) { + curComplete += curPack; + double result = 0; + if(curComplete <= length){ + result = (double)curComplete / length ; + }else{ + result = 1; + } + + try{ + onDownloadProcess(result); + }catch (Exception ex){ + ex.printStackTrace(); + } + try{ + int ri = (int)(result * 100); + onDownloadProcess(ri + "%"); + }catch (Exception ex){ + ex.printStackTrace(); + } + } + + public abstract void onDownloadProcess(double process); + + public abstract void onDownloadProcess(String process); + + protected void onStopDownload(){ + onStopDownload(getFile()); + } + + public abstract void onStopDownload(File file); + + public abstract void onDownloadError(Exception e); + +} diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/listener/IUploadListener.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/listener/IUploadListener.java new file mode 100644 index 0000000..7244f83 --- /dev/null +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/listener/IUploadListener.java @@ -0,0 +1,18 @@ +package com.sunvote.xpadapi.service.listener; + +/** + * Created by Elvis on 2018/3/22 13:43 + * Email:Eluis@psunsky.com + * 版权所有:长沙中天电子设计开发有限公司 + * Description: 人大通用版XPadAppRendaMac + */ +public interface IUploadListener { + + public void onUploadStart(); + + public void onUploadProcess(double process); + + public void onUploadStop(); + + public void onFail(); +} diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFFileDownloadModule.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFFileDownloadModule.java new file mode 100644 index 0000000..d4671be --- /dev/null +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFFileDownloadModule.java @@ -0,0 +1,121 @@ +package com.sunvote.xpadapi.service.logic; + +import android.os.Handler; +import android.os.Looper; + +import com.sunvote.util.LogUtil; +import com.sunvote.xpadapi.service.listener.AbsDownloadProcess; + +import java.io.File; + +/** + * Created by Elvis on 2018/3/20 11:16 + * Email:Eluis@psunsky.com + * 版权所有:长沙中天电子设计开发有限公司 + * Description: 文件下载模块 + */ +public class RFFileDownloadModule { + + private RFFileDownloadModule(){ + handler = new Handler(Looper.getMainLooper()); + } + private Handler handler ; + private long overtime = 15 * 1000 ; + private static RFFileDownloadModule instance = new RFFileDownloadModule(); + + public static RFFileDownloadModule getInstance() { + return instance; + } + + private AbsDownloadProcess downloadProcess; + private AbsDownloadProcess tdownloadProcess = new AbsDownloadProcess() { + + private boolean start = false; + private double process = 0 ; + private Runnable runnable = new Runnable() { + @Override + public void run() { + onDownloadError(new Exception("overtime")); + } + }; + + @Override + public void onStartDownload(File file, long length) { + start = true; + process = 0; + LogUtil.i("onStartDownload", file.getName() + "," + length); + if(downloadProcess != null){ + downloadProcess.onStartDownload(file,length); + } + handler.removeCallbacks(runnable); + handler.postDelayed(runnable,overtime); + } + + @Override + public void onDownloadProcess(double process) { + this.process = process; + LogUtil.i("onDownloadProcess", "process," + process); + if(downloadProcess != null && start){ + downloadProcess.onDownloadProcess(process); + } + handler.removeCallbacks(runnable); + handler.postDelayed(runnable,overtime); + } + + @Override + public void onDownloadProcess(String process) { + LogUtil.i("onDownloadProcess", "process," + process); + if(downloadProcess != null && start){ + downloadProcess.onDownloadProcess(process); + } + handler.removeCallbacks(runnable); + handler.postDelayed(runnable,overtime); + } + + @Override + public void onStopDownload(File file) { + LogUtil.i("onStopDownload", "process," + file.getName()); + if(downloadProcess != null && start){ + if(process >= 1.0){ + downloadProcess.onStopDownload(file); + }else{ + downloadProcess.onDownloadError(new Exception("file download error")); + } + } + start = false; + handler.removeCallbacks(runnable); + file.deleteOnExit(); + } + + @Override + public void onDownloadError(Exception e) { + LogUtil.i("onStopDownload", "error", e); + if(downloadProcess != null && start){ + downloadProcess.onDownloadError(e); + } + start = false; + handler.removeCallbacks(runnable); + } + }; + + public void setOvertime(long overtime) { + if(overtime > 0){ + this.overtime = overtime; + } + } + + public synchronized void registerDownloadProcess(AbsDownloadProcess downloadProcess){ + this.downloadProcess = downloadProcess ; + } + + public synchronized void unRegisterDownloadProcess(AbsDownloadProcess downloadProcess){ + this.downloadProcess = null; + } + + protected synchronized AbsDownloadProcess getDownloadProcess() { + + return tdownloadProcess; + } + + +} diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFFileUploadModule.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFFileUploadModule.java new file mode 100644 index 0000000..9e35e11 --- /dev/null +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFFileUploadModule.java @@ -0,0 +1,358 @@ +package com.sunvote.xpadapi.service.logic; + +import android.os.Handler; +import android.os.HandlerThread; + +import com.sunvote.util.LogUtil; +import com.sunvote.xpadapi.service.listener.IUploadListener; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; + +/** + * Created by Elvis on 2018/3/21 16:13 + * Email:Eluis@psunsky.com + * 版权所有:长沙中天电子设计开发有限公司 + * Description: 文件上传模块 + */ +public class RFFileUploadModule { + + public static final byte PACKTYPE = 5 ; + public static final int SLEEP_TIME = 25 ; + public static byte index = 0 ; + private RFFileUploadModule(){ + handlerThread = new HandlerThread(RFFileUploadModule.class.getSimpleName()); + handlerThread.start(); + handler = new Handler(handlerThread.getLooper()); + } + + private static RFFileUploadModule instance ; + + public static RFFileUploadModule getInstance() { + if(instance == null){ + synchronized (RFFileUploadModule.class){ + if(instance == null){ + instance = new RFFileUploadModule(); + } + } + } + return instance; + } + + private IUploadListener uploadListener = new IUploadListener() { + @Override + public void onUploadStart() { + System.out.println("onUploadStart"); + } + + @Override + public void onUploadProcess(double process) { + System.out.println("onUploadProcess:" + process); + } + + @Override + public void onUploadStop() { + System.out.println("onUploadStop"); + } + + @Override + public void onFail() { + System.out.println("onFail"); + } + }; + + + private String filename; + private byte[] datas = new byte[1024]; + private int length = 1024; + private HandlerThread handlerThread ; + private Handler handler ; + private byte[] keyid = new byte[2]; + private Runnable task; + private int packTotal ; + private boolean isStop = false ; + + /** + * 文件名不能超过16个字符(中文不能超过8个),超过16个字符将会出现截断。 + * 文件长度不能超过65535个字节 + * @param file 文件 + * @return 是否成功发起上传 + */ + public boolean uploadFile(File file){ + LogUtil.i("RFFileUploadModule","uploadFile" + file); + if(file != null){ + length = (int)file.length(); + filename = file.getName(); + datas = new byte[length+4]; + FileInputStream fis = null ; + try { + fis = new FileInputStream(file); + int start = 0 ; + // 把文件数据读入字节数组。 + while(start < length){ + int ret = fis.read(datas,start,length-start); + if(ret < 0){ + break; + } + start += ret ; + } + datas[length] = 0x13 ; + datas[length + 1] = 0x0 ; + datas[length + 2] = 0x10 ; + datas[length + 3] = 0x0 ; + + if(start >= length){ + task = new A0(filename,length); + handler.removeCallbacks(task); + handler.post(task); + return true; + } + + } catch (Exception e) { + e.printStackTrace(); + }finally { + if(fis != null){ + try { + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + fis = null; + } + } + } + return false; + } + + public void setKeyid(byte[] keyid) { + setKeyid(keyid,0); + } + public void setKeyid(byte[] keyid,int start) { + this.keyid[0] = keyid[start]; + this.keyid[1] = keyid[start+1]; + } + + public void onUploadFile(byte data, byte[] keyid, byte packid, byte packH, byte[] packLs) { + if (data == 1) { + setKeyid(keyid); + packetConfirmation(packid); + } + if (data == 2) { + uploadData(packid, packH, packLs); + } + if (data == 3) { + packetReceptionConfirmed(packid, packH); + } + } + + public void uploadData(byte packid, byte packH, byte[] packLs){ + System.out.println("uploadData"); + handler.removeCallbacks(task); + if(task instanceof A2){ + ((A2)task).setStop(true); + } + task = new A2(keyid,packid,packH,datas,packLs); + handler.post(task); + if(uploadListener != null){ + double process = 0 ; + if(packTotal > 0){ + int count = 0 ; + int sen = (packLs[1] & 0xFF) * 256 + (packLs[0] & 0XFF); + for(int i = 0 ; i < 16 ; i++){ + if((sen & (1 << i)) == 0){ + count ++ ; + } + } + int been = packH * 16 + count ; + if(been > packTotal){ + been = packTotal ; + } + process = (double)(been) / packTotal ; + } + uploadListener.onUploadProcess(process); + } + } + + public void packetConfirmation(byte packid){ + System.out.println("packetConfirmation"); + handler.removeCallbacks(task); + byte[] names = null; + if(filename != null){ + try { + names = filename.getBytes("GB2312"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + task = new A1(keyid,(byte)((length + 4 ) / 65535), (byte)(((length + 4) % 65535) / 256),(byte)((length + 4) % 256),names); + handler.post(task); + } + + public void packetReceptionConfirmed(byte packid,byte packH){ + System.out.println("packetReceptionConfirmed"); + handler.removeCallbacks(task); + task = new A3(keyid,packid,packH); + handler.post(task); + } + + public boolean uploadFile(byte[] datas){ + System.out.println("uploadFile" + datas); + if(datas != null){ + length = datas.length ; + filename = null ; + this.datas = new byte[length + 4]; + System.arraycopy(datas,0,this.datas,0,length); + datas[length] = 0x13 ; + datas[length + 1] = 0x0 ; + datas[length + 2] = 0x10 ; + datas[length + 3] = 0x0 ; + task = new A0(filename,length); + handler.removeCallbacks(task); + handler.post(task); + return true; + } + return false; + } + + + public void setUploadListener(IUploadListener uploadListener) { + this.uploadListener = uploadListener; + } + + class A0 implements Runnable{ + private int length ; + private String filename; + private int times ; + public A0(String filename,int length){ + this.length = length ; + this.filename = filename; + packTotal = length / 16 + 1; + times = 0 ; + index++; + } + + @Override + public void run() { + LogUtil.i("RFFileUploadModule","A0 run"); + XpadApiServiceInfoProxyManager.getInstance().getService().applyFileUpload(this.length,this.filename,PACKTYPE,index); + times ++ ; + if(task != null && times < 5){ + handler.removeCallbacks(task); + handler.postDelayed(task,5000); + } + } + } + + class A1 implements Runnable{ + + private byte[] keyid; + private byte packid ; + private byte packH ; + private byte packL ; + private byte[] names ; + private int times ; + + public A1(byte[] keyid, byte packid,byte packH,byte packL,byte[] names) { + LogUtil.i("RFFileUploadModule","A1"); + this.keyid = keyid; + this.packid = packid; + this.packH = packH ; + this.packL = packL ; + this.names = names ; + times = 0 ; + } + + @Override + public void run() { + LogUtil.i("RFFileUploadModule","A1 run"); + XpadApiServiceInfoProxyManager.getInstance().getService().packetConfirmation(this.keyid,this.packid,this.packH,this.packL,this.names,PACKTYPE); + times ++ ; + if(task != null && times < 5){ + handler.removeCallbacks(task); + handler.postDelayed(task,5000); + } + if(uploadListener != null){ + uploadListener.onUploadStart(); + isStop = false; + } + } + } + + class A2 implements Runnable{ + private byte[] keyid; + private byte packid; + private byte packH; + private byte[] datas ; + private byte[] packLs; + private int times ; + private boolean stop = false; + + public void setStop(boolean stop) { + this.stop = stop; + } + + public A2(byte[] keyid, byte packid, byte packH, byte[] datas, byte[] packLs) { + LogUtil.i("RFFileUploadModule","A2"); + this.keyid = keyid; + this.packid = packid; + this.packH = packH; + this.datas = datas; + this.packLs = packLs; + times = 0 ; + } + + @Override + public void run() { + LogUtil.i("RFFileUploadModule","A2 run"); + int packLsi = (packLs[1] & 0xFF) * 256 + (packLs[0] & 0xFF); + int offset = 0 ; + int length = 16; + System.out.println("packLsi:" + packLsi); + for(byte packL = 0 ;packL < 16 && !stop ;packL ++) { + if((packLsi & (1 << packL)) != 0){ + offset = ((packid * 65535) + (packH) * 256 + 16 * (packL)) & 0xFFFF; + XpadApiServiceInfoProxyManager.getInstance().getService().uploadFileData(this.keyid, this.packid, this.packH, (byte)(packL ), this.datas, offset, length,PACKTYPE); + // need sleep ???? + try{ + Thread.sleep(SLEEP_TIME); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + + times ++ ; + if(task != null && times < 5){ + handler.removeCallbacks(task); + handler.postDelayed(task,5000); + } + } + } + + class A3 implements Runnable{ + private byte[] keyid; + private byte packid ; + private byte packH; + private int times ; + + public A3(byte[] keyid, byte packid, byte packH) { + LogUtil.i("RFFileUploadModule","A3"); + this.keyid = keyid; + this.packid = packid; + this.packH = packH; + times = 0 ; + } + + @Override + public void run() { + LogUtil.i("RFFileUploadModule","A3 run"); + XpadApiServiceInfoProxyManager.getInstance().getService().packetReceptionConfirmed(this.keyid,this.packid,this.packH,PACKTYPE); + if(uploadListener != null && !isStop){ + isStop = true; + uploadListener.onUploadStop(); + } + } + } +} diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFMessageUploadModule.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFMessageUploadModule.java new file mode 100644 index 0000000..ea99227 --- /dev/null +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/RFMessageUploadModule.java @@ -0,0 +1,301 @@ +package com.sunvote.xpadapi.service.logic; + +import android.os.Handler; +import android.os.HandlerThread; + +import java.io.UnsupportedEncodingException; + +/** + * Created by Elvis on 2018/3/27 9:48 + * Email:Eluis@psunsky.com + * 版权所有:长沙中天电子设计开发有限公司 + * Description: 人大通用版 + * 短消息上传模块 + */ +public class RFMessageUploadModule { + + public static final byte PACKTYPE = 3 ; + private static RFMessageUploadModule instance ; + + private IUploadListener uploadListener ; + + private String filename; + private byte[] datas = "test message".getBytes(); + private int length = 1024; + private HandlerThread handlerThread ; + private Handler handler ; + private byte[] keyid; + private Runnable task; + private int packTotal ; + private boolean stop = false; + + private RFMessageUploadModule(){ + handlerThread = new HandlerThread(RFMessageUploadModule.class.getSimpleName()); + handlerThread.start(); + handler = new Handler(handlerThread.getLooper()); + } + + public static RFMessageUploadModule getInstance() { + if(instance == null){ + instance = new RFMessageUploadModule(); + } + return instance; + } + + public void setMessageContent(byte[] messageContent) { + this.datas = messageContent; + uploadMsgData(datas); + removeTask(); + } + + public void setMessageContent(String message) { + try { + this.datas = message.getBytes("GB2312"); + uploadMsgData(datas); + removeTask(); + handler.removeCallbacks(failCall); + handler.postDelayed(failCall,10000); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private boolean uploadMsgData(byte[] ds){ + System.out.println("uploadMsgData" + ds); + if(ds != null){ + length = ds.length ; + filename = null ; + this.datas = new byte[length ]; + System.arraycopy(ds,0,this.datas,0,length); +// this.datas[length] = 0x13 ; +// this.datas[length + 1] = 0x0 ; +// this.datas[length + 2] = 0x10 ; +// this.datas[length + 3] = 0x0 ; + task = new A0(filename,length); + handler.removeCallbacks(task); + handler.post(task); + return true; + } + return false; + } + + + public void setKeyid(byte[] keyid) { + this.keyid = keyid; + } + + public void uploadData(byte packid, byte packH, byte[] packLs){ + System.out.println("uploadData"); + handler.removeCallbacks(task); + task = new A2(keyid,packid,packH,datas,packLs); + handler.post(task); + if(uploadListener != null){ + double process = 0 ; + if(packTotal > 0){ + int count = 0 ; + int sen = (packLs[1] & 0xFF) * 256 + (packLs[0] & 0XFF); + for(int i = 0 ; i < 16 ; i++){ + if((sen & (1 << i)) != 0){ + count ++ ; + } + } + process = (double)(packH * 16 + count) / packTotal ; + } + uploadListener.onUploadProcess(process); + } + } + + public void packetConfirmation(byte packid){ + System.out.println("packetConfirmation"); + handler.removeCallbacks(task); + byte[] names = null; + if(filename != null){ + try { + names = filename.getBytes("GB2312"); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } + } + task = new A1(keyid,(byte)((length ) / 65535), (byte)(((length ) % 65535) / 256),(byte)(((length ) % 256 / 16 + (length % 16 == 0 ? 0 : 1))),names); + handler.post(task); + } + + public void packetReceptionConfirmed(byte packid,byte packH){ + System.out.println("packetReceptionConfirmed"); + handler.removeCallbacks(task); + task = new A3(keyid,packid,packH); + handler.post(task); + } + + public boolean uploadMessage(byte[] datas){ + System.out.println("uploadMsgData" + datas); + if(datas != null){ + length = datas.length ; + filename = null ; + this.datas = new byte[length ]; + System.arraycopy(datas,0,this.datas,0,length); +// datas[length] = 0x13 ; +// datas[length + 1] = 0x0 ; +// datas[length + 2] = 0x10 ; +// datas[length + 3] = 0x0 ; + task = new A0(filename,length); + handler.removeCallbacks(task); + handler.post(task); + return true; + } + return false; + } + + + public void setUploadListener(IUploadListener uploadListener) { + this.uploadListener = uploadListener; + } + + class A0 implements Runnable{ + private int length ; + private String filename; + private int times ; + public A0(String filename,int length){ + this.length = length ; + this.filename = filename; + packTotal = length / 16 + 1; + times = 0 ; + } + + @Override + public void run() { + XPadApi.getInstance().applyFileUpload(this.length,this.filename,PACKTYPE,RFFileUploadModule.index); + } + } + + class A1 implements Runnable{ + + private byte[] keyid; + private byte packid ; + private byte packH ; + private byte packL ; + private byte[] names ; + private int times ; + + public A1(byte[] keyid, byte packid,byte packH,byte packL,byte[] names) { + this.keyid = keyid; + this.packid = packid; + this.packH = packH ; + this.packL = packL ; + this.names = names ; + times = 0 ; + } + + @Override + public void run() { + XPadApi.getInstance().packetConfirmation(this.keyid,this.packid,this.packH,this.packL,this.names,PACKTYPE); + times ++ ; + if(task != null && times < 5){ + handler.postDelayed(task,1000); + } + if(uploadListener != null){ + uploadListener.onUploadStart(); + } + + handler.removeCallbacks(failCall); + handler.postDelayed(failCall,10000); + } + } + + class A2 implements Runnable{ + private byte[] keyid; + private byte packid; + private byte packH; + private byte[] datas ; + private byte[] packLs; + private int times ; + + public A2(byte[] keyid, byte packid, byte packH, byte[] datas, byte[] packLs) { + this.keyid = keyid; + this.packid = packid; + this.packH = packH; + this.datas = datas; + this.packLs = packLs; + times = 0 ; + } + + @Override + public void run() { + + int packLsi = packLs[1] * 256 + packLs[0]; + int offset = 0 ; + int length = 16; + System.out.println("packLsi:" + packLsi); + for(byte packL = 0 ;packL < 16 ;packL ++) { + if((packLsi & (1 << packL)) != 0){ + offset = ((packid * 65535) + (packH) * 256 + 16 * (packL)) & 0xFFFF; + XPadApi.getInstance().uploadFileData(this.keyid, this.packid, this.packH, (byte)(packL), this.datas, offset, length,PACKTYPE); + try{ + Thread.sleep(50); + } catch (Exception e) { + e.printStackTrace(); + } + } + } + times ++ ; + if(task != null && times < 5){ + handler.postDelayed(task,1000); + } + handler.removeCallbacks(failCall); + handler.postDelayed(failCall,10000); + } + } + + class A3 implements Runnable{ + private byte[] keyid; + private byte packid ; + private byte packH; + + public A3(byte[] keyid, byte packid, byte packH) { + this.keyid = keyid; + this.packid = packid; + this.packH = packH; + } + + @Override + public void run() { + XPadApi.getInstance().packetReceptionConfirmed(this.keyid,this.packid,this.packH,PACKTYPE); + if(uploadListener != null){ + uploadListener.onUploadStop(); + } + handler.removeCallbacks(failCall); + } + } + + public void onUploadMessage(byte data, byte[] keyid, byte packid, byte packH, byte[] packLs) { + LogUtil.i("RFMessageUploadModule","onUploadMessage:" + data); + if (data == 1) { + setKeyid(keyid); + packetConfirmation(packid); + } + if (data == 2) { + uploadData(packid, packH, packLs); + } + if (data == 3) { + packetReceptionConfirmed(packid, packH); + } + } + + private void removeTask(){ + handler.postDelayed(new Runnable() { + @Override + public void run() { + handler.removeCallbacks(task); + } + },5000); + } + + private Runnable failCall = new Runnable() { + @Override + public void run() { + if(uploadListener != null){ + uploadListener.onFail(); + } + } + }; +} diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SendPacket.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SendPacket.java new file mode 100644 index 0000000..7c7e20d --- /dev/null +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SendPacket.java @@ -0,0 +1,25 @@ +package com.sunvote.xpadapi.service.logic; + +public class SendPacket { + + private byte[] datas; + + /** + * 0 未发送 + * 1 已发送 + * 2 发送成功 + */ + private int sendOK ; + + /** + * 发送次数 + */ + private int sendTimes; + + /** + * 是否有序 有序需要第一个才发送 + * 无序,则可全部发送 + */ + private boolean canAll; + +} diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SenderManager.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SenderManager.java new file mode 100644 index 0000000..bd3581b --- /dev/null +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SenderManager.java @@ -0,0 +1,283 @@ +package com.sunvote.xpadapi.service.logic; + +import com.sunvote.xpadapi.service.bean.BatchNumberVote; +import com.sunvote.xpadapi.service.bean.BatchSingleVote; +import com.sunvote.xpadapi.service.bean.LoginInVote; +import com.sunvote.xpadapi.service.bean.NumberVote; +import com.sunvote.xpadapi.service.bean.Packet; +import com.sunvote.xpadapi.service.bean.SelectOtherVote; +import com.sunvote.xpadapi.service.bean.SelectVote; +import com.sunvote.xpadapi.service.bean.ServiceTypeVote; +import com.sunvote.xpadapi.service.bean.SingleVote; +import com.sunvote.xpadapi.service.listener.OnSendPacket; +import com.sunvote.xpadapi.service.listener.OnSender; + +public final class SenderManager { + + private static SenderManager instance = null; + + private SenderManager() { + } + + public static SenderManager getInstance() { + if (instance == null) { + synchronized (SenderManager.class) { + if (instance == null) { + instance = new SenderManager(); + } + } + } + return instance; + } + + /** + * 发送 + * + * @param singleVote + */ + public void sendSingleVote(final SingleVote singleVote) { + sendSingleVote(singleVote, null); + } + + public void sendVoteAllOK(final OnSender sender) { + SingleVote singleVote = new SingleVote(1); + sendSingleVote(singleVote, sender); + } + + public void sendSingleVote(final SingleVote singleVote, final OnSender onSender) { + if (singleVote != null) { + byte[] datas = singleVote.toBytes(); + XpadApiServiceInfoProxyManager.getInstance().sendPacket(datas, new OnSendPacket() { + @Override + public void onSuccess(Packet packet) { + if (onSender != null) { + onSender.onSuccess(singleVote); + } + } + + @Override + public void onFail(Packet packet) { + if (onSender != null) { + onSender.onFail(singleVote); + } + } + }); + } else { + if (onSender != null) { + onSender.onFail(singleVote); + } + } + } + + public void sendSelectVote(final SelectVote selectVote) { + sendSelectVote(selectVote, null); + } + + public void sendSelectVote(final SelectVote selectVote, final OnSender onSender) { + if (selectVote != null) { + XpadApiServiceInfoProxyManager.getInstance().sendPacket(selectVote.toBytes(), new OnSendPacket() { + @Override + public void onSuccess(Packet packet) { + if (onSender != null) { + onSender.onSuccess(selectVote); + } + } + + @Override + public void onFail(Packet packet) { + if (onSender != null) { + onSender.onFail(selectVote); + } + } + }); + } else { + if (onSender != null) { + onSender.onFail(selectVote); + } + } + } + + public void sendNumberVote(final NumberVote numberVote) { + sendNumberVote(numberVote, null); + } + + public void sendNumberVote(final NumberVote numberVote, final OnSender sender) { + if (numberVote != null) { + XpadApiServiceInfoProxyManager.getInstance().sendPacket(numberVote.toBytes(), new OnSendPacket() { + @Override + public void onSuccess(Packet packet) { + if (sender != null) { + sender.onSuccess(numberVote); + } + } + + @Override + public void onFail(Packet packet) { + if (sender != null) { + sender.onFail(numberVote); + } + } + }); + } else { + if (sender != null) { + sender.onFail(numberVote); + } + } + } + + public void sendLoginInVote(final LoginInVote loginInVote) { + sendLoginInVote(loginInVote, null); + } + + public void sendLoginInVote(final LoginInVote loginInVote, final OnSender sender) { + if (loginInVote != null) { + XpadApiServiceInfoProxyManager.getInstance().sendPacket(loginInVote.toBytes(), new OnSendPacket() { + @Override + public void onSuccess(Packet packet) { + if (sender != null) { + sender.onSuccess(loginInVote); + } + } + + @Override + public void onFail(Packet packet) { + if (sender != null) { + sender.onFail(loginInVote); + } + } + }); + } else { + if (sender != null) { + sender.onFail(loginInVote); + } + } + } + + public void sendCancelAllBatchSingleVote(final OnSender sender) { + BatchSingleVote batchSingleVote = new BatchSingleVote(); + sendBatchSingleVote(batchSingleVote, sender); + } + + public void sendSubmitlAllBatchSingleVote(final OnSender sender) { + BatchSingleVote batchSingleVote = new BatchSingleVote(); + batchSingleVote.setOk(1); + sendBatchSingleVote(batchSingleVote, sender); + } + + public void sendBatchSingleVote(final BatchSingleVote batchSingleVote) { + sendBatchSingleVote(batchSingleVote, null); + } + + public void sendBatchSingleVote(final BatchSingleVote batchSingleVote, final OnSender sender) { + if (batchSingleVote != null) { + XpadApiServiceInfoProxyManager.getInstance().sendPacket(batchSingleVote.toBytes(), new OnSendPacket() { + @Override + public void onSuccess(Packet packet) { + if (sender != null) { + sender.onSuccess(batchSingleVote); + } + } + + @Override + public void onFail(Packet packet) { + if (sender != null) { + sender.onFail(batchSingleVote); + } + } + }); + } else { + if (sender != null) { + sender.onFail(batchSingleVote); + } + } + } + + public void sendBatchNumberVoteOK(final BatchNumberVote batchNumberVote, final OnSender sender) { + batchNumberVote.setOk(1); + sendBatchNumberVote(batchNumberVote, sender); + } + + public void sendBatchNumberVote(final BatchNumberVote batchNumberVote) { + sendBatchNumberVote(batchNumberVote, null); + } + + public void sendBatchNumberVote(final BatchNumberVote batchNumberVote, final OnSender sender) { + if (batchNumberVote != null) { + XpadApiServiceInfoProxyManager.getInstance().sendPacket(batchNumberVote.toBytes(), new OnSendPacket() { + @Override + public void onSuccess(Packet packet) { + if (sender != null) { + sender.onSuccess(batchNumberVote); + } + } + + @Override + public void onFail(Packet packet) { + if (sender != null) { + sender.onFail(batchNumberVote); + } + } + }); + } else { + if (sender != null) { + sender.onFail(batchNumberVote); + } + } + } + + public void sendSelectOtherVote(SelectOtherVote selectOtherVote) { + sendSelectOtherVote(selectOtherVote, null); + } + + public void sendSelectOtherVote(final SelectOtherVote selectOtherVote, final OnSender sender) { + if (selectOtherVote != null) { + XpadApiServiceInfoProxyManager.getInstance().sendPacket(selectOtherVote.toBytes(), new OnSendPacket() { + @Override + public void onSuccess(Packet packet) { + if (sender != null) { + sender.onSuccess(selectOtherVote); + } + } + + @Override + public void onFail(Packet packet) { + if (sender != null) { + sender.onFail(selectOtherVote); + } + } + }); + } else { + if (sender != null) { + sender.onFail(selectOtherVote); + } + } + } + + public void sendServiceTypeVote(final ServiceTypeVote serviceTypeVote) { + sendServiceTypeVote(serviceTypeVote, null); + } + + public void sendServiceTypeVote(final ServiceTypeVote serviceTypeVote, final OnSender sender) { + if (serviceTypeVote != null) { + XpadApiServiceInfoProxyManager.getInstance().sendPacket(serviceTypeVote.toBytes(), new OnSendPacket() { + @Override + public void onSuccess(Packet packet) { + if (sender != null) { + sender.onSuccess(serviceTypeVote); + } + } + + @Override + public void onFail(Packet packet) { + if (sender != null) { + sender.onFail(serviceTypeVote); + } + } + }); + } else { + if (sender != null) { + sender.onFail(serviceTypeVote); + } + } + } +} diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SerialManager.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SerialManager.java new file mode 100644 index 0000000..ac04307 --- /dev/null +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/SerialManager.java @@ -0,0 +1,29 @@ +package com.sunvote.xpadapi.service.logic; + +public final class SerialManager { + + private static SerialManager instance = null ; + + private SerialManager(){ + } + + public static SerialManager getInstance(){ + if(instance == null){ + synchronized(SerialManager.class){ + if(instance == null){ + instance = new SerialManager(); + } + } + } + return instance; + } + + private byte seq = 0 ; + + public byte getSeq() { + if (seq == 0xfe) { + seq = 0 ; + } + return ++seq; + } +} diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/XpadApiServiceInfoProxyManager.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/XpadApiServiceInfoProxyManager.java new file mode 100644 index 0000000..df5e407 --- /dev/null +++ b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/service/logic/XpadApiServiceInfoProxyManager.java @@ -0,0 +1,240 @@ +package com.sunvote.xpadapi.service.logic; + +import com.sunvote.xpadapi.service.XpadApiService; +import com.sunvote.xpadapi.service.bean.BaseInfo; +import com.sunvote.xpadapi.service.bean.BaseVoteInfo; +import com.sunvote.xpadapi.service.bean.KeypadInfo; +import com.sunvote.xpadapi.service.bean.ModelInfo; +import com.sunvote.xpadapi.service.bean.OnLineInfo; +import com.sunvote.xpadapi.service.bean.Packet; +import com.sunvote.xpadapi.service.listener.BaseInfoChanageListener; +import com.sunvote.xpadapi.service.listener.BaseVoteInfoChanageListener; +import com.sunvote.xpadapi.service.listener.KeyPadinfoChanageListener; +import com.sunvote.xpadapi.service.listener.ModelInfoChanageListener; +import com.sunvote.xpadapi.service.listener.OnSendPacket; +import com.sunvote.xpadapi.service.listener.OnlineInfoChanageListener; +import com.sunvote.xpadapi.util.LogUtil; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +/** + * 服务信息 + */ +public final class XpadApiServiceInfoProxyManager { + + public static final String TAG = XpadApiServiceInfoProxyManager.class.getSimpleName(); + private static XpadApiServiceInfoProxyManager instance = null ; + private List sendList = Collections.synchronizedList(new ArrayList()); + private Object lock = new Object(); + private String charset = "GB2312" ; + private XpadApiService service; + + public void setCharset(String charset) { + this.charset = charset; + } + + public void setService(XpadApiService service) { + this.service = service; + } + + public XpadApiService getService() { + return service; + } + + public String getCharset() { + return charset; + } + + private XpadApiServiceInfoProxyManager(){ + } + + public static XpadApiServiceInfoProxyManager getInstance(){ + if(instance == null){ + synchronized(XpadApiServiceInfoProxyManager.class){ + if(instance == null){ + instance = new XpadApiServiceInfoProxyManager(); + } + } + } + return instance; + } + + private ModelInfo modelInfo; + private ModelInfoChanageListener modelInfoListener; + + public void setModelInfo(ModelInfo modelInfo) { + this.modelInfo = modelInfo; + if(modelInfoListener != null){ + try{ + modelInfoListener.onModelInfoChanage(modelInfo); + }catch (Exception ex){ + LogUtil.e(TAG,"onModelInfoChanage",ex); + } + } + } + + public ModelInfo getModelInfo() { + return modelInfo; + } + + public void setModelInfoListener(ModelInfoChanageListener modelInfoListener) { + this.modelInfoListener = modelInfoListener; + } + + private BaseInfo baseInfo; + private BaseInfoChanageListener baseInfoListener; + + public void setBaseInfo(BaseInfo baseInfo) { + this.baseInfo = baseInfo; + if(baseInfoListener != null){ + try { + baseInfoListener.onBaseInfoChange(baseInfo); + }catch (Exception ex){ + LogUtil.e(TAG,"onBaseInfoChange",ex); + } + } + + } + + public BaseInfo getBaseInfo() { + return baseInfo; + } + + public void setBaseInfoListener(BaseInfoChanageListener baseInfoListener) { + this.baseInfoListener = baseInfoListener; + } + + private BaseVoteInfo baseVoteInfo; + private BaseVoteInfoChanageListener baseVoteInfoListener; + + public void setBaseVoteInfo(BaseVoteInfo baseVoteInfo) { + this.baseVoteInfo = baseVoteInfo; + if(baseVoteInfoListener != null){ + try { + baseVoteInfoListener.onBaseVoteInfoListener(baseVoteInfo); + }catch (Exception ex){ + LogUtil.e(TAG,"onBaseVoteInfoListener",ex); + } + } + + } + + public BaseVoteInfo getBaseVoteInfo() { + return baseVoteInfo; + } + + public void setBaseVoteInfoListener(BaseVoteInfoChanageListener baseVoteInfoListener) { + this.baseVoteInfoListener = baseVoteInfoListener; + } + + private KeypadInfo keypadInfo; + private KeyPadinfoChanageListener keyPadinfoChanageListener; + + public void setKeypadInfo(KeypadInfo keypadInfo) { + this.keypadInfo = keypadInfo; + if(keyPadinfoChanageListener != null){ + try{ + keyPadinfoChanageListener.onKeyPadinfoChanage(keypadInfo); + }catch (Exception ex){ + LogUtil.e(TAG,"onKeyPadinfoChanage",ex); + } + } + + } + + public KeypadInfo getKeypadInfo() { + if(keypadInfo == null){ + keypadInfo = new KeypadInfo(); + } + return keypadInfo; + } + + public void setKeyPadinfoChanageListener(KeyPadinfoChanageListener keyPadinfoChanageListener) { + this.keyPadinfoChanageListener = keyPadinfoChanageListener; + } + + private OnLineInfo onLineInfo; + private OnlineInfoChanageListener onlineInfoChanageListener; + + public void setOnLineInfo(OnLineInfo onLineInfo) { + this.onLineInfo = onLineInfo; + if(onlineInfoChanageListener != null){ + try{ + onlineInfoChanageListener.onOnlineInfoChanage(onLineInfo); + }catch (Exception ex){ + LogUtil.e(TAG,"onOnlineInfoChanage",ex); + } + } + } + + public OnLineInfo getOnLineInfo() { + return onLineInfo; + } + + public void setOnlineInfoChanageListener(OnlineInfoChanageListener onlineInfoChanageListener) { + this.onlineInfoChanageListener = onlineInfoChanageListener; + } + + public void sendPacket(byte[] datas,final OnSendPacket sendPacket){ + Packet packet = new Packet(); + packet.setDatas(datas); + packet.setSendNo(datas[5] & 0xFF); + packet.setOnSendPacket(new OnSendPacket() { + @Override + public void onSuccess(Packet packet) { + synchronized (lock){ + sendList.remove(packet); + } + try{ + sendPacket.onSuccess(packet); + }catch (Exception ex){ + LogUtil.e(TAG,"sendPacket.onSuccess",ex); + } + } + + @Override + public void onFail(Packet packet) { + synchronized (lock){ + sendList.remove(packet); + } + try{ + sendPacket.onFail(packet); + }catch (Exception ex){ + LogUtil.e(TAG,"sendPacket.onFail",ex); + } + } + }); + synchronized (lock){ + if(!exsit(packet)){ + sendList.add(packet); + } + } + if(service != null){ + service.immediately(); + } + } + + public List getSendList() { + return sendList; + } + + private boolean exsit(Packet packet){ + for(Packet packet1: sendList){ + if(packet.getSendNo() == packet1.getSendNo()){ + return true; + } + } + return false; + } + + public Packet findPacket(int sendNo){ + for(Packet packet: sendList){ + if(packet.getSendNo() == sendNo){ + return packet; + } + } + return null; + } +} diff --git a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/util/LogUtil.java b/C5/xpadapi/src/main/java/com/sunvote/xpadapi/util/LogUtil.java deleted file mode 100644 index 4dc7d4c..0000000 --- a/C5/xpadapi/src/main/java/com/sunvote/xpadapi/util/LogUtil.java +++ /dev/null @@ -1,316 +0,0 @@ -package com.sunvote.xpadapi.util; - -import android.os.Environment; -import android.util.Log; - -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.Date; - -public class LogUtil { - - private static FileWriter fileWriter; - - public static final int VERBOSE_LEVER = 2; - public static final int DEBUG_LEVER = 3; - public static final int INFO_LEVER = 4; - public static final int WARN_LEVER = 5; - public static final int ERROR_LEVER = 6; - public static final int ASSERT_LEVER = 7; - - public static int lever = VERBOSE_LEVER - 1 ; - - private static boolean logToFile = false; - private static boolean logToLogcat = true ; - - private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss.SSS"); - - LogUtil() { - throw new RuntimeException("Stub!"); - } - - public static int v(String tag, String msg) { - if (VERBOSE_LEVER > lever) { - if(logToLogcat){ - Log.v(tag, msg); - } - inputToFile("(V):" + msg ); - } - return -1; - } - - private static void init(){ - if(fileWriter == null){ - synchronized (LogUtil.class) { - if(fileWriter == null) { - try { - File path = new File(Environment.getExternalStorageDirectory().getPath() + "/sunvote/log"); - if (!path.exists()) { - path.mkdirs(); - } - File file = new File(Environment.getExternalStorageDirectory().getPath() - + "/sunvote/log/" + simpleDateFormat.format(new Date())+".txt"); - if (!file.exists()) { - file.createNewFile(); - } - fileWriter = new FileWriter(file, true); - - } catch (Exception ex) { - ex.printStackTrace(); - fileWriter = null; - } - } - } - } - } - - public static void enableLogToFile(){ - logToFile = true; - } - - public static void disabelLogToFile(){ - logToFile = false; - } - - public static void enableLogToLogcat(){ - logToLogcat = true; - } - - public static void disableLogToLogcat(){ - logToLogcat = false; - } - - public static void enableLog(){ - lever = VERBOSE_LEVER - 1; - } - - public static void disableLog(){ - lever = ASSERT_LEVER ; - } - - public static int v(String tag, String msg, Throwable tr) { - if(VERBOSE_LEVER > lever){ - if(logToLogcat) { - Log.v(tag, msg, tr); - } - inputToFile("(V):" + msg + Log.getStackTraceString(tr)); - } - return -1; - } - - public static int d(String tag, String msg) { - if(DEBUG_LEVER > lever){ - if(logToLogcat) { - Log.d(tag, msg); - } - inputToFile("(D):" + msg ); - } - return -1; - } - - public static int d(String tag, String msg, Throwable tr) { - if(DEBUG_LEVER > lever){ - if(logToLogcat){ - Log.d(tag,msg,tr); - } - inputToFile("(D):" + msg + Log.getStackTraceString(tr)); - } - return -1; - } - - public static int i(String tag, String msg) { - if(INFO_LEVER > lever){ - if(logToLogcat){ - Log.i(tag,msg); - } - inputToFile("(I):" + msg ); - } - return -1; - } - - public static int i(String tag, String msg, Throwable tr) { - if(INFO_LEVER > lever){ - if(logToLogcat){ - Log.i(tag,msg,tr); - } - inputToFile("(I):" + msg + Log.getStackTraceString(tr)); - } - return -1; - } - - public static int i(String tag,byte[] msg){ - String msgStr = ByteUtils.bytesToHexString(msg); - return i(tag,msgStr); - } - - public static int i(String tag,String msgTag, byte[] msg){ - String msgStr = ByteUtils.bytesToHexString(msg); - return i(tag,msgTag + ":\r\n" + msgStr); - } - - public static int v(String tag,String msgTag, byte[] msg){ - String msgStr = ByteUtils.bytesToHexString(msg); - return v(tag,msgTag + ":\r\n" + msgStr); - } - - public static int i(String tag,String msgTag, byte[] msg,int length){ - String msgStr = ByteUtils.bytesToHexString(msg,length); - return i(tag,msgTag + ":\r\n" + msgStr); - } - - public static int v(String tag,String msgTag, byte[] msg,int length){ - String msgStr = ByteUtils.bytesToHexString(msg,length); - return v(tag,msgTag + ":\r\n" + msgStr); - } - - public static int i(String tag,byte[] msg,Throwable tr){ - String msgStr = ByteUtils.bytesToHexString(msg); - return i(tag,msgStr,tr); - } - - public static int i(String tag,String msgTag,byte[] msg,Throwable tr){ - String msgStr = ByteUtils.bytesToHexString(msg); - return i(tag,msgTag + ":\r\n" + msgStr,tr); - } - - public static int w(String tag, String msg) { - if(WARN_LEVER > lever){ - if(logToLogcat){ - Log.w(tag,msg); - } - inputToFile("(V):" + msg); - } - return -1; - } - - public static int w(String tag, String msg, Throwable tr) { - if(WARN_LEVER > lever){ - if(logToLogcat){ - Log.w(tag,msg,tr); - } - inputToFile("(W):" + msg + Log.getStackTraceString(tr)); - } - return -1; - } - - public static boolean isLoggable(String s, int i){ - return Log.isLoggable(s,i); - } - - public static int w(String tag, Throwable tr) { - if(WARN_LEVER > lever){ - if(logToLogcat){ - Log.w(tag,tr); - } - inputToFile("(W):" + Log.getStackTraceString(tr)); - } - return -1; - } - - public static int e(String tag, String msg) { - if(ERROR_LEVER > lever){ - if(logToLogcat){ - Log.e(tag,msg); - } - inputToFile("(E):" + msg); - } - return -1; - } - - public static int e(String tag,Throwable tr){ - String message = "ERROR" ; - if(tr != null && tr.getMessage() != null){ - message = tr.getMessage(); - } - return e(tag,message,tr); - } - - public static int e(String tag, String msg, Throwable tr) { - if(ERROR_LEVER > lever){ - if(logToLogcat){ - Log.e(tag,msg,tr); - } - inputToFile("(E):" + msg + Log.getStackTraceString(tr)); - } - return -1; - } - - public static int wtf(String tag, String msg) { - if(ASSERT_LEVER > lever){ - if(logToLogcat){ - Log.wtf(tag,msg); - } - inputToFile("(WTF):" + msg); - } - return -1; - } - - public static int wtf(String tag, Throwable tr) { - if(ASSERT_LEVER > lever){ - if(logToLogcat){ - Log.wtf(tag,tr); - } - inputToFile("(WTF):" + Log.getStackTraceString(tr)); - } - return -1; - } - - public static int wtf(String tag, String msg, Throwable tr) { - if(ASSERT_LEVER > lever){ - if(logToLogcat){ - Log.wtf(tag,msg,tr); - } - inputToFile("(WTF):" + msg + Log.getStackTraceString(tr)); - } - return -1; - } - - public static String getStackTraceString(Throwable tr) { - return Log.getStackTraceString(tr); - } - - - public static void stack(){ - Throwable throwable = new Throwable(); - // 需要处理TAG 要读出上面class method的信息,后续添上 - i("STACK",getStackTraceString(throwable)); - } - - private synchronized static void inputToFile(String msg){ - if(logToFile) { - String time = simpleDateFormat.format(new Date()); - try { - init(); - String log = time + "(" + Thread.currentThread().getName() + ",id=" + Thread.currentThread().getId() + ")" + msg + "\r\n"; - if(onLogMessage != null){ - onLogMessage.onLog(time + ":" + msg + "\r\n"); - } - fileWriter.write(log); - fileWriter.flush(); - } catch (Exception ex) { - ex.printStackTrace(); - if(fileWriter != null){ - try { - fileWriter.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - fileWriter = null; - } - } - } - - private static OnLogMessage onLogMessage; - - public static void setOnLogMessage(OnLogMessage onLogMessage) { - LogUtil.onLogMessage = onLogMessage; - } - - public static interface OnLogMessage{ - void onLog(String log); - } -} - diff --git a/C5/xpadprotocal/build/libs/xpadprotocal.jar b/C5/xpadprotocal/build/libs/xpadprotocal.jar new file mode 100644 index 0000000..d4ff4a9 Binary files /dev/null and b/C5/xpadprotocal/build/libs/xpadprotocal.jar differ diff --git a/C5/xpadprotocal/build/tmp/jar/MANIFEST.MF b/C5/xpadprotocal/build/tmp/jar/MANIFEST.MF new file mode 100644 index 0000000..59499bc --- /dev/null +++ b/C5/xpadprotocal/build/tmp/jar/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 + -- libgit2 0.21.4