Commit a79befc2 by 王宇航

1.6.1

1、調整一下,更靈活,但是對接必須按順序來調用方法
parent 307257db
package com.gingersoft.connect.utils package com.gingersoft.connect.utils
import android.content.Context
import com.gingersoft.connect.bean.MessageBuilder import com.gingersoft.connect.bean.MessageBuilder
import java.net.InetSocketAddress
import java.net.URI import java.net.URI
class GingerSoftConnect { class GingerSoftConnect {
...@@ -32,4 +34,36 @@ class GingerSoftConnect { ...@@ -32,4 +34,36 @@ class GingerSoftConnect {
fun onError(ex: Exception?) {} fun onError(ex: Exception?) {}
} }
/**
* 接收數據的服務
*/
private var mWebSocketServer: MyWebSocketServer? = null
private var nsdUtils: NSDUtils? = null
fun registerDiscoveryService(context: Context) {
// 註冊NSD,讓後面開啟的KDS能主動找到POS設備,然後從POS設備拿食品信息
nsdUtils = NSDUtils()
nsdUtils?.registerKDS(context)
}
fun startMsgReceiveService() {
// 開啟服務,接收KDS發來的消息
mWebSocketServer = MyWebSocketServer(InetSocketAddress(12581))
mWebSocketServer?.isReuseAddr = true // 設置地址可複用,避免APP被用戶強制殺掉後,再次打開報錯Address already in use
mWebSocketServer?.start()
}
/**
* 插入自己的數據處理類
* 目前系統有的都在Action中,以後新增功能,不需要改這個庫,在KDS和POS端調整即可
*/
fun inputTransmitter(type: String, process: MsgProcess) {
MsgParser.parserMap[type] = process
}
fun unregisterDiscoveryService() {
mWebSocketServer?.stop()
}
} }
\ No newline at end of file
...@@ -18,12 +18,15 @@ class NSDUtils { ...@@ -18,12 +18,15 @@ class NSDUtils {
fun registerKDS(context: Context) { fun registerKDS(context: Context) {
val nsdManager = context.getSystemService(Context.NSD_SERVICE) as NsdManager val nsdManager = context.getSystemService(Context.NSD_SERVICE) as NsdManager
// 先註銷之前的
unregisterService(context)
val serviceInfo = NsdServiceInfo() val serviceInfo = NsdServiceInfo()
serviceInfo.serviceName = serviceName serviceInfo.serviceName = serviceName
serviceInfo.serviceType = serviceType serviceInfo.serviceType = serviceType
serviceInfo.port = 12583 serviceInfo.port = 12582
registrationListener = NSDRegistrationListener() registrationListener = NSDRegistrationListener()
// 註冊,這樣KDS才能收到
nsdManager.registerService( nsdManager.registerService(
serviceInfo, serviceInfo,
NsdManager.PROTOCOL_DNS_SD, NsdManager.PROTOCOL_DNS_SD,
...@@ -59,12 +62,12 @@ class NSDUtils { ...@@ -59,12 +62,12 @@ class NSDUtils {
} }
} }
fun unregisterService(context: Context) { private fun unregisterService(context: Context) {
if (registrationListener != null && registrationListener!!.isRegistered) { if (registrationListener != null && registrationListener!!.isRegistered) {
try { try {
val nsdManager = context.getSystemService(Context.NSD_SERVICE) as NsdManager val nsdManager = context.getSystemService(Context.NSD_SERVICE) as NsdManager
nsdManager.unregisterService(registrationListener) nsdManager.unregisterService(registrationListener)
} catch (e: IllegalArgumentException) { } catch (_: IllegalArgumentException) {
} finally { } finally {
registrationListener = null registrationListener = null
......
package com.gingersoft.connect.utils
import android.content.Context
import com.gingersoft.connect.bean.Action
import com.gingersoft.connect.bean.GetFoodInfoProcess
import com.gingersoft.connect.bean.GetSoldOutInfoProcess
import java.net.InetSocketAddress
object SoldOut {
/**
* 接收數據的服務
*/
private var mWebSocketServer: MyWebSocketServer? = null
private var nsdUtils: NSDUtils? = null
fun init(context: Context) {
// 註冊NSD,讓後面開啟的KDS能主動找到POS設備,然後從POS設備拿食品信息
nsdUtils = NSDUtils()
nsdUtils?.registerKDS(context)
// 開啟服務,接收KDS發來的消息
mWebSocketServer = MyWebSocketServer(InetSocketAddress(12581))
mWebSocketServer?.isReuseAddr = true // 設置地址可複用,避免APP被用戶強制殺掉後,再次打開報錯Address already in use
mWebSocketServer?.start()
}
fun initFoodInfoTransmitter(process: GetFoodInfoProcess) {
MsgParser.parserMap[Action.FoodInfo.toString()] = process
}
fun initSoldOutInfoTransmitter(process: GetSoldOutInfoProcess) {
MsgParser.parserMap[Action.SoldOutInfo.toString()] = process
}
fun stopServer() {
mWebSocketServer?.stop()
}
}
\ No newline at end of file
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