|
@@ -3,12 +3,80 @@ package com.doverfuelingsolutions.issp.fusion
|
|
|
import androidx.lifecycle.Lifecycle
|
|
|
import androidx.lifecycle.LifecycleObserver
|
|
|
import androidx.lifecycle.OnLifecycleEvent
|
|
|
+import com.doverfuelingsolutions.issp.data.DataStore
|
|
|
+import com.doverfuelingsolutions.issp.utils.log.DFSLog
|
|
|
+import com.doverfuelingsolutions.issp.utils.sp.SPKeys
|
|
|
+import com.doverfuelingsolutions.issp.utils.sp.SPUtil
|
|
|
+import com.wayne.www.waynelib.fdc.FdcClient
|
|
|
+import com.wayne.www.waynelib.fdc.OnFdcClientStateChangedListener
|
|
|
+import com.wayne.www.waynelib.fdc.OnFdcMessageReceivedListener
|
|
|
+import com.wayne.www.waynelib.fdc.OnFdcServiceResponseReceivedListener
|
|
|
+import com.wayne.www.waynelib.fdc.message.FdcMessage
|
|
|
+import com.wayne.www.waynelib.fdc.message.ServiceResponse
|
|
|
+import com.wayne.www.waynelib.fdc.message.ServiceResponseLogOn
|
|
|
+import kotlinx.coroutines.CoroutineScope
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
|
|
|
-object FusionManager : LifecycleObserver {
|
|
|
+object FusionManager : LifecycleObserver, OnFdcClientStateChangedListener,
|
|
|
+ OnFdcServiceResponseReceivedListener,
|
|
|
+ OnFdcMessageReceivedListener {
|
|
|
+
|
|
|
+ private val coroutineIO = CoroutineScope(Dispatchers.IO)
|
|
|
+
|
|
|
+ override fun onFdcClientStateChanged(sender: FdcClient?, state: FdcClient.FdcClientState?) {
|
|
|
+ DFSLog.i("onFdcClientStateChanged: ${state?.name}")
|
|
|
+ if (sender == null || state == null) return
|
|
|
+
|
|
|
+ when (state) {
|
|
|
+ FdcClient.FdcClientState.Connected -> {
|
|
|
+ // TODO 获取油站相关信息
|
|
|
+ val port = SPUtil.getString(SPKeys.MIDDLE_PORT).toInt()
|
|
|
+ DFSLog.d("try login in Fusion")
|
|
|
+ sender.sendLogonRequestAsync(port, port, "00.07", OnFdcServiceResponseReceivedListener { _, response ->
|
|
|
+ DFSLog.d("login Fusion respond")
|
|
|
+ if (response == null || response !is ServiceResponseLogOn) return@OnFdcServiceResponseReceivedListener
|
|
|
+ DFSLog.d(response.requestID, response.requestType, response.singleFdcData.fdcStatus)
|
|
|
+ if (response.singleFdcData.fdcStatus.equals("ERRCD_NOPERM", true)) {
|
|
|
+ DFSLog.w("Unsuccessful LogOn request, may cause by previous unfinished session, trying LogOff and LogOn again...")
|
|
|
+ FdcClient.getDefault().sendLogOffRequestAsync({ _, _ ->
|
|
|
+ sender.sendLogonRequestAsync(port, port, "00.07", null, 60000)
|
|
|
+ }, 0)
|
|
|
+ }
|
|
|
+ }, 60000)
|
|
|
+ }
|
|
|
+ else -> {}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onServiceResponseReceived(sender: FdcClient?, serviceResponse: ServiceResponse?) {
|
|
|
+ DFSLog.d("onServiceResponseReceived")
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onFdcMessageReceived(sender: FdcClient?, fdcMessage: FdcMessage?) {
|
|
|
+ DFSLog.d("onFdcMessageReceived")
|
|
|
+ }
|
|
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
|
|
|
- fun start() {}
|
|
|
+ fun initialize() {
|
|
|
+ DFSLog.i("initialize")
|
|
|
+ FdcClient.getDefault().addOnFdcClientStateChangedListeners(this)
|
|
|
+ FdcClient.getDefault().addOnFdcServiceResponseReceivedListeners(this)
|
|
|
+ FdcClient.getDefault().addOnFdcMessageReceivedListeners(this)
|
|
|
+ FdcClient.getDefault().start(
|
|
|
+ SPUtil.getString(SPKeys.MIDDLE_IP),
|
|
|
+ SPUtil.getString(SPKeys.MIDDLE_PORT).toInt(),
|
|
|
+ DataStore.sn,
|
|
|
+ SPUtil.getString(SPKeys.MIDDLE_WORKSTATION_ID).toInt(),
|
|
|
+ null
|
|
|
+ )
|
|
|
+ }
|
|
|
|
|
|
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
|
|
|
- fun stop() {}
|
|
|
+ fun close() {
|
|
|
+ DFSLog.i("close")
|
|
|
+ FdcClient.getDefault().removeOnFdcClientStateChangedListeners(this)
|
|
|
+ FdcClient.getDefault().removeOnFdcServiceResponseReceivedListeners(this)
|
|
|
+ FdcClient.getDefault().removeOnFdcMessageReceivedListeners(this)
|
|
|
+ FdcClient.getDefault().stop()
|
|
|
+ }
|
|
|
}
|