Service.kt 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package io.nexilis.service
  2. import android.util.Log
  3. import io.newuniverse.SDK.nuSDKService
  4. import io.nexilis.service.core.Data
  5. import io.nexilis.service.core.decrypt
  6. import kotlinx.coroutines.*
  7. import java.util.concurrent.ConcurrentHashMap
  8. private val passArray = charArrayOf(
  9. '6',
  10. 'g',
  11. 'o',
  12. 'y',
  13. 'k',
  14. 't',
  15. 'u',
  16. 'j',
  17. 't',
  18. 'O',
  19. 'Q',
  20. 'J',
  21. 'Y',
  22. '0',
  23. 't',
  24. '9',
  25. 'z',
  26. 'l',
  27. 'u',
  28. 'Y',
  29. 'g',
  30. 'o',
  31. '1',
  32. '8',
  33. '4',
  34. 'y',
  35. 'g',
  36. 'k',
  37. '7',
  38. 'y',
  39. 'y',
  40. 'k',
  41. 'i',
  42. 'i',
  43. 'g'
  44. )
  45. private const val DEFAULT_TIMEOUT: Long = 30 * 1000
  46. internal val pass = String(passArray).decrypt()
  47. internal const val namePreference = "cpaas"
  48. internal const val tag = "SAPI"
  49. internal val apiScope = CoroutineScope(SupervisorJob())
  50. class Service {
  51. companion object {
  52. fun sendSync(data: Data): Data? {
  53. try {
  54. Log.d(tag, "Service:sGetResponse >>> $data")
  55. val response = nuSDKService.getInstance(pass)
  56. .sGetResponse(pass, data.toString(), DEFAULT_TIMEOUT, false)
  57. ?: return null
  58. Log.d(tag, "Service:sGetResponse <<< $response")
  59. val r = Data()
  60. if (r.parse(response)) {
  61. return r
  62. }
  63. } catch (e: Exception) {
  64. Log.e(tag, e.message, e)
  65. }
  66. return null
  67. }
  68. fun sendAsync(data: Data): String? {
  69. try {
  70. Log.d(tag, "Service:sSend:$data")
  71. var d: Any? = data.toString()
  72. if (data.media.isNotEmpty()) {
  73. d = data.toByteArray()
  74. }
  75. return nuSDKService.getInstance(pass).sSend(pass, d, 1, DEFAULT_TIMEOUT)
  76. } catch (e: Exception) {
  77. Log.e(tag, e.message, e)
  78. }
  79. return null
  80. }
  81. val map = ConcurrentHashMap<String, suspend CoroutineScope.(Data) -> Unit>()
  82. fun sendAsync(data: Data, callback: suspend CoroutineScope.(Data) -> Unit) {
  83. map[data.status] = callback
  84. sendAsync(data)
  85. }
  86. fun response(data: Data): String? {
  87. try {
  88. Log.d(tag, "Service:sSendResponse:$data")
  89. return nuSDKService.getInstance(pass)
  90. .sSendResponse(pass, data.packetId, data.toString(), DEFAULT_TIMEOUT)
  91. } catch (e: Exception) {
  92. Log.e(tag, e.message, e)
  93. }
  94. return null
  95. }
  96. fun sendAck(pin: String?, id: String) {
  97. try {
  98. Log.d(tag, "Service:sendAck:$id")
  99. pin?.let {
  100. sendAsync(
  101. Data(
  102. code = "A02",
  103. status = id,
  104. f_pin = it,
  105. bodies = mutableMapOf("A18" to id)
  106. )
  107. )
  108. }
  109. } catch (e: Exception) {
  110. Log.e(tag, e.message, e)
  111. }
  112. }
  113. }
  114. }