FusionManager.kt 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package com.doverfuelingsolutions.issp.fusion
  2. import androidx.lifecycle.Lifecycle
  3. import androidx.lifecycle.LifecycleObserver
  4. import androidx.lifecycle.OnLifecycleEvent
  5. import com.doverfuelingsolutions.issp.R
  6. import com.doverfuelingsolutions.issp.api.FuelInfoApi
  7. import com.doverfuelingsolutions.issp.api.entity.NozzleInfo
  8. import com.doverfuelingsolutions.issp.data.GlobalData
  9. import com.doverfuelingsolutions.issp.fusion.callback.OnFusionEvent
  10. import com.doverfuelingsolutions.issp.utils.StringUtil
  11. import com.doverfuelingsolutions.issp.utils.log.DFSLog
  12. import com.doverfuelingsolutions.issp.utils.sp.SPKeys
  13. import com.doverfuelingsolutions.issp.utils.sp.SPUtil
  14. import com.wayne.www.waynelib.fdc.FdcClient
  15. import com.wayne.www.waynelib.fdc.OnFdcClientStateChangedListener
  16. import com.wayne.www.waynelib.fdc.OnFdcMessageReceivedListener
  17. import com.wayne.www.waynelib.fdc.OnFdcServiceResponseReceivedListener
  18. import com.wayne.www.waynelib.fdc.message.FdcMessage
  19. import com.wayne.www.waynelib.fdc.message.ServiceResponse
  20. import com.wayne.www.waynelib.fdc.message.ServiceResponseLogOn
  21. import kotlinx.coroutines.*
  22. import kotlin.coroutines.resume
  23. import kotlin.coroutines.suspendCoroutine
  24. object FusionManager : LifecycleObserver, OnFdcClientStateChangedListener,
  25. OnFdcServiceResponseReceivedListener,
  26. OnFdcMessageReceivedListener {
  27. private var mStateFusion: FdcClient.FdcClientState = FdcClient.FdcClientState.Stopped
  28. private var mIsLogin = false
  29. private val nozzleList = arrayListOf<NozzleInfo>()
  30. private val coroutineIO = CoroutineScope(Dispatchers.IO)
  31. val stateFusion: FdcClient.FdcClientState get() = mStateFusion
  32. val isLogin: Boolean get() = mIsLogin
  33. var onFusionEvent: OnFusionEvent? = null
  34. override fun onFdcClientStateChanged(sender: FdcClient?, state: FdcClient.FdcClientState?) {
  35. DFSLog.i("Fusion: state = ${state?.name}")
  36. if (sender == null || state == null) return
  37. mStateFusion = state
  38. when (state) {
  39. FdcClient.FdcClientState.Connected -> loginFetchInfo()
  40. else -> {}
  41. }
  42. }
  43. override fun onServiceResponseReceived(sender: FdcClient?, serviceResponse: ServiceResponse?) {
  44. if (sender == null || serviceResponse == null) return
  45. // DFSLog.v("onServiceResponseReceived")
  46. }
  47. override fun onFdcMessageReceived(sender: FdcClient?, fdcMessage: FdcMessage?) {
  48. if (sender == null || fdcMessage == null) return
  49. // DFSLog.v("onFdcMessageReceived")
  50. }
  51. @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
  52. fun initialize() {
  53. FdcClient.getDefault().addOnFdcClientStateChangedListeners(this)
  54. FdcClient.getDefault().addOnFdcServiceResponseReceivedListeners(this)
  55. FdcClient.getDefault().addOnFdcMessageReceivedListeners(this)
  56. FdcClient.getDefault().start(
  57. SPUtil.getString(SPKeys.MIDDLE_IP),
  58. SPUtil.getString(SPKeys.MIDDLE_PORT).toInt(),
  59. GlobalData.serialNumber.get(),
  60. SPUtil.getString(SPKeys.MIDDLE_WORKSTATION_ID).toInt(),
  61. null
  62. )
  63. coroutineIO.launch {
  64. delay(12000)
  65. if (mStateFusion != FdcClient.FdcClientState.Connected) {
  66. onFusionEvent?.onFusionInit(FusionError.Timeout, StringUtil.get(R.string.connect_timeout))
  67. }
  68. }
  69. }
  70. @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
  71. fun close() {
  72. val manager = this
  73. coroutineIO.launch {
  74. logout()
  75. FdcClient.getDefault().removeOnFdcClientStateChangedListeners(manager)
  76. FdcClient.getDefault().removeOnFdcServiceResponseReceivedListeners(manager)
  77. FdcClient.getDefault().removeOnFdcMessageReceivedListeners(manager)
  78. FdcClient.getDefault().stop()
  79. }
  80. }
  81. private fun loginFetchInfo() {
  82. coroutineIO.launch {
  83. // retry if failed
  84. val success = if (login()) true else {
  85. DFSLog.d("Fusion: retry login after logout")
  86. logout()
  87. delay(7000)
  88. login()
  89. }
  90. if (!success) {
  91. mIsLogin = false
  92. onFusionEvent?.onFusionInit(FusionError.Login, StringUtil.get(R.string.login_fail))
  93. return@launch
  94. } else {
  95. mIsLogin = true
  96. }
  97. val resultNozzles = FuelInfoApi.getNozzleInfo()
  98. if (resultNozzles.success && resultNozzles.data != null) {
  99. if (nozzleList.isNotEmpty()) nozzleList.clear()
  100. nozzleList.addAll(resultNozzles.data)
  101. onFusionEvent?.onFusionInit(FusionError.Success)
  102. } else {
  103. onFusionEvent?.onFusionInit(FusionError.GetNozzleInfo, StringUtil.get(R.string.fail_get_nozzle))
  104. }
  105. }
  106. }
  107. private suspend fun login() = suspendCoroutine<Boolean> {
  108. val port = SPUtil.getString(SPKeys.MIDDLE_PORT).toInt()
  109. FdcClient.getDefault().sendLogonRequestAsync(port, port, "00.07", { _, response ->
  110. if (response != null && response is ServiceResponseLogOn) {
  111. val status = response.singleFdcData.fdcStatus
  112. DFSLog.d("Fusion: try login response = $status")
  113. if (status.equals(FusionConstants.CODE_SUCCESS, true)) {
  114. DFSLog.d("Fusion: login succeeded")
  115. it.resume(true)
  116. } else {
  117. DFSLog.d("Fusion: login failed")
  118. it.resume(false)
  119. }
  120. }
  121. }, 10000)
  122. }
  123. private suspend fun logout() = suspendCoroutine<Boolean> {
  124. FdcClient.getDefault().sendLogOffRequestAsync({ _, response ->
  125. val status = response?.singleFdcData?.fdcStatus
  126. DFSLog.d("Fusion: try logout response = $status")
  127. if (status != null && status.equals(FusionConstants.CODE_SUCCESS, true)) {
  128. DFSLog.d("Fusion: logout succeeded")
  129. it.resume(true)
  130. } else {
  131. DFSLog.d("Fusion: logout failed")
  132. it.resume(false)
  133. }
  134. }, 10000)
  135. }
  136. }