Commit 46f40b6b by 王宇航

1、增加錯誤類型

2、訂單信息增加orderNo
3、發送消息 結果回調 增加errorMessage字段
parent 55a101fd
......@@ -3,7 +3,20 @@
<component name="deploymentTargetDropDown">
<value>
<entry key="app">
<State />
<State>
<runningDeviceTargetSelectedWithDropDown>
<Target>
<type value="RUNNING_DEVICE_TARGET" />
<deviceKey>
<Key>
<type value="SERIAL_NUMBER" />
<value value="9X4TRG5PSCEYZHZX" />
</Key>
</deviceKey>
</Target>
</runningDeviceTargetSelectedWithDropDown>
<timeTargetWasSelectedWithDropDown value="2024-05-28T12:43:00.676406500Z" />
</State>
</entry>
</value>
</component>
......
......@@ -9,7 +9,7 @@ android {
defaultConfig {
applicationId = "com.example.gingersoft_connect"
minSdk = 24
minSdk = 19
targetSdk = 34
versionCode = 1
versionName = "1.0"
......
......@@ -7,10 +7,13 @@ import android.widget.TextView
import com.example.gingersoft_connect.R
import com.gingersoft.connect.SendCallback
import com.gingersoft.connect.bean.Action
import com.gingersoft.connect.bean.FoodInfoBean
import com.gingersoft.connect.bean.MessageBuilder
import com.gingersoft.connect.bean.MessageSender
import com.gingersoft.connect.bean.OrderInfoBean
import com.gingersoft.connect.utils.GingerSoftConnect
import java.text.SimpleDateFormat
import java.util.Date
class MainActivity : Activity() {
......@@ -61,18 +64,44 @@ class MainActivity : Activity() {
tvDisconnect.setOnClickListener {
connect?.disconnect()
}
var count = 0
tvSendMsg.setOnClickListener {
msgSender?.initSendData(Action.KitchenInfo, OrderInfoBean())
count++
val order = OrderInfoBean().apply {
orderId = 1111L + count
orderNo = "00$count"
orderType = 1
pax = 10
senderId = 1965.toString()
senderName = "Glen$count"
sendTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Date())
sendDeviceId = "6464646sdfs"
sendDeviceName = "POS$count"
sendOrderCount = 4 + count
isFirstSendOrder = true
flyPrintInfo = "K1-L2"
foodInfoBeanList = mutableListOf<FoodInfoBean>().apply {
for (i in 0 until 10) {
add(FoodInfoBean().apply {
foodId = "${1 + i}"
printId = "1166$i"
orderId = 1111L + count
foodName = "這是一個名字超級長的食品,"
quantity = count.toString()
price = count * 1.1
type = (i % 3) + 1
parentId = "0"
isPrintRedLetter = false
}
)
}
}
}
msgSender?.initSendData(Action.KitchenInfo, order)
?.asyncSend(object : SendCallback {
override fun callback(status: Boolean, errorCode: Int) {
override fun callback(status: Boolean, errorCode: Int, errorMessage: String) {
runOnUiThread {
tvSendValue.text = if (status) "發送成功" else when (errorCode) {
MessageBuilder.ERROR_CODE_SEND_TIMEOUT -> "發送消息超時"
MessageBuilder.ERROR_CODE_NOT_CONNECT -> "未連接到服務器"
MessageBuilder.ERROR_CODE_CONNECT_TIMEOUT -> "連接服務器超時"
else -> "發生錯誤:$errorCode"
}
tvSendValue.text = if (status) "發送成功" else "發生錯誤:$errorMessage"
}
}
})
......
......@@ -9,7 +9,7 @@ android {
compileSdk = 34
defaultConfig {
minSdk = 24
minSdk = 19
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
......
package com.gingersoft.connect
interface SendCallback {
fun callback(status: Boolean, errorCode: Int)
fun callback(status: Boolean, errorCode: Int, errorMessage: String)
}
\ No newline at end of file
package com.gingersoft.connect.bean
import com.gingersoft.connect.utils.GingerSoftConnect
import com.gingersoft.connect.utils.GsonUtils
import com.gingersoft.connect.utils.MyWebSocketClient
import java.util.concurrent.atomic.AtomicInteger
......@@ -14,12 +15,14 @@ class MessageBuilder(
const val ERROR_CODE_SEND_TIMEOUT = 100
const val ERROR_CODE_NOT_CONNECT = 101
const val ERROR_CODE_CONNECT_TIMEOUT = 102
const val ERROR_CODE_ORDER_INFO_INCORRECT = 103// 訂單信息有誤
const val ERROR_CODE_DATA_NULL = 104// 获取到的数据为null
const val ERROR_CODE_ANALYSIS_FAILED = 105// 數據解析失敗
}
fun initSendData(action: Action, sendData: SendMsgType): MessageSender {
val currentMsgCode = msgCode.incrementAndGet()
val msg = MessageBean(currentMsgCode, action.toString(), sendData)
val msg = MessageBean(currentMsgCode, action.toString(), GsonUtils.GsonString(sendData))
return MessageSender(connect, webSocketClient, msg)
}
......
package com.gingersoft.connect.bean
import android.os.Build.VERSION_CODES.P
import android.util.Log
import com.gingersoft.connect.SendCallback
import com.gingersoft.connect.utils.GingerSoftConnect
import com.gingersoft.connect.utils.GsonUtils
......@@ -52,7 +50,7 @@ class MessageSender(
send(callback)
} else {
// 超時
callback?.callback(false, MessageBuilder.ERROR_CODE_CONNECT_TIMEOUT)
callback?.callback(false, MessageBuilder.ERROR_CODE_CONNECT_TIMEOUT, "連接超時")
}
}
}
......@@ -61,7 +59,7 @@ class MessageSender(
private fun send(callback: SendCallback?) {
if (webSocketClient.isClosed || webSocketClient.isClosing || !webSocketClient.isOpen) {
callback?.callback(false, MessageBuilder.ERROR_CODE_NOT_CONNECT)
callback?.callback(false, MessageBuilder.ERROR_CODE_NOT_CONNECT, "未連接設備")
} else {
webSocketClient.sendMsg(msg.code.toString(), GsonUtils.GsonString(msg), callback)
}
......
......@@ -9,6 +9,10 @@ public class OrderInfoBean implements SendMsgType {
*/
private Long orderId;
/**
* 訂單編號
*/
private String orderNo;
/**
* 訂單類型
*/
private int orderType;
......@@ -73,6 +77,14 @@ public class OrderInfoBean implements SendMsgType {
this.orderId = orderId;
}
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public int getOrderType() {
return this.orderType;
}
......@@ -160,4 +172,12 @@ public class OrderInfoBean implements SendMsgType {
public void setPrintStatus(int printStatus) {
this.printStatus = printStatus;
}
public List<FoodInfoBean> getFoodInfoBeanList() {
return foodInfoBeanList;
}
public void setFoodInfoBeanList(List<FoodInfoBean> foodInfoBeanList) {
this.foodInfoBeanList = foodInfoBeanList;
}
}
package com.gingersoft.connect.bean
class SocketCallbackBean(
val success: Boolean,
val statusCode: Int,
val msgCode: String,
val errorMsg: String
)
\ No newline at end of file
......@@ -3,6 +3,7 @@ package com.gingersoft.connect.utils
import android.util.Log
import com.gingersoft.connect.SendCallback
import com.gingersoft.connect.bean.MessageBuilder
import com.gingersoft.connect.bean.SocketCallbackBean
import kotlinx.coroutines.channels.Channel
import org.java_websocket.client.WebSocketClient
import org.java_websocket.handshake.ServerHandshake
......@@ -27,14 +28,14 @@ class MyWebSocketClient(
}
override fun onMessage(message: String?) {
val bean = GsonUtils.GsonToBean(message, SocketCallbackBean::class.java)
Log.e("eeeaaa", "客戶端收到消息:$message")
if (message != null && map.containsKey(message)) {
map[message]?.callback(true, 200)
if (bean != null && map.containsKey(bean.msgCode)) {
map[message]?.callback(bean.success, bean.statusCode, bean.errorMsg)
timerMap[message]?.cancel()
}
}
override fun onClose(code: Int, reason: String?, remote: Boolean) {
listener?.onClose(code, reason, remote)
status = 2
......@@ -54,7 +55,11 @@ class MyWebSocketClient(
timerMap[msgCode] = Timer()
timerMap[msgCode]?.schedule(object : TimerTask() {
override fun run() {
callback.callback(false, MessageBuilder.ERROR_CODE_SEND_TIMEOUT)
callback.callback(
false,
MessageBuilder.ERROR_CODE_SEND_TIMEOUT,
"接收服務端響應超時"
)
}
}, 10000)
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment