Guest User

Untitled

a guest
Jan 21st, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. class MainActivity {
  2. // ....
  3. private fun startRegisterProcess(onFinish: () -> Unit) {
  4. if (service.isRegistered(applicationContext)) return
  5.  
  6. val identity = "${UUID.randomUUID()}"
  7. service.register(identity, { registerResult ->
  8. SettingsUtil.setIdentity(applicationContext, identity)
  9. SettingsUtil.setPreSharedKey(applicationContext, registerResult.preSharedKey)
  10.  
  11. onFinish()
  12. }, { onError("Unable to register app at gateway! Please try it later again") })
  13. }
  14. // ...
  15. }
  16.  
  17. class TradfriService(context: Context) {
  18. private val client: TradfriClient
  19. private val gson = Gson()
  20.  
  21. private val handler = CoroutineExceptionHandler { _, ex ->
  22. Log.println(Log.ERROR, "TradfriService", Log.getStackTraceString(ex))
  23. }
  24.  
  25. init {
  26. val gatewayIp = SettingsUtil.getGatewayIp(context) ?: ""
  27. val securityId = SettingsUtil.getSecurityId(context) ?: ""
  28. val identity = SettingsUtil.getIdentity(context)
  29. val preSharedKey = SettingsUtil.getPreSharedKey(context)
  30.  
  31. client = TradfriClient(gatewayIp, securityId, identity, preSharedKey)
  32. }
  33.  
  34. fun isRegistered(context: Context): Boolean {
  35. val identity = SettingsUtil.getIdentity(context)
  36. val preSharedKey = SettingsUtil.getPreSharedKey(context)
  37.  
  38. return identity != null && identity.isNotEmpty() && preSharedKey != null && preSharedKey.isNotEmpty()
  39. }
  40.  
  41. fun register(identity: String, onSuccess: (RegisterResult) -> Unit, onError: () -> Unit) {
  42. launch(CommonPool + handler) {
  43. val response = client.register(identity)
  44.  
  45. if (response == null) {
  46. launch(UI + handler) { onError() }
  47. return@launch
  48. }
  49.  
  50. if (!response.isSuccess) {
  51. launch(UI + handler) { onError() }
  52. return@launch
  53. }
  54.  
  55. val result = parseResponse(response, RegisterResult::class.java)
  56. if (result == null) {
  57. launch(UI + handler) { onError() }
  58. return@launch
  59. }
  60.  
  61. launch(UI + handler) { onSuccess(result) }
  62. }
  63. }
  64.  
  65. fun ping(onSuccess: (String) -> Unit, onError: () -> Unit): Job {
  66. Log.d(LogName, "_ping")
  67. return launch(CommonPool + handler) {
  68. println("_ping start")
  69. val response = client.ping()
  70. if (response == null) {
  71. launch(UI + handler) { onError() }
  72. return@launch
  73. }
  74.  
  75. if (!response.isSuccess) {
  76. launch(UI + handler) { onError() }
  77. return@launch
  78. }
  79.  
  80. launch(UI + handler) { onSuccess(String(response.payload)) }
  81. println("_ping end")
  82. }
  83. }
  84. }
  85.  
  86. class TradfriClient(ip: String,
  87. private val securityId: String,
  88. private var identity: String?,
  89. private var preSharedKey: String?) {
  90.  
  91. private val baseUrl = "coaps://$ip:5684"
  92.  
  93. private val gson = Gson()
  94. private val coapRegisterEndpoint: CoapEndpoint = getRegisterCoapEndpoint()
  95. private val coapEndpoint: CoapEndpoint = getDefaultCoapEndpoint()
  96.  
  97. private fun getRegisterCoapEndpoint(): CoapEndpoint {
  98. val builder = DtlsConnectorConfig.Builder(InetSocketAddress(0))
  99. builder.setPskStore(StaticPskStore("Client_identity", securityId.toByteArray()))
  100. builder.setRetransmissionTimeout(50000)
  101.  
  102. val dtlsConnector = DTLSConnector(builder.build())
  103.  
  104. val network = NetworkConfig.createStandardWithoutFile()
  105. return CoapEndpoint(dtlsConnector, network)
  106. }
  107.  
  108. private fun getDefaultCoapEndpoint(): CoapEndpoint {
  109. val builder = DtlsConnectorConfig.Builder(InetSocketAddress(0))
  110. builder.setPskStore(StaticPskStore(identity ?: "", (preSharedKey ?: "").toByteArray()))
  111. builder.setRetransmissionTimeout(50000)
  112.  
  113. val dtlsConnector = DTLSConnector(builder.build())
  114.  
  115. val network = NetworkConfig.createStandardWithoutFile()
  116.  
  117. return CoapEndpoint(dtlsConnector, network)
  118. }
  119.  
  120. private fun registerClient(url: String): CoapClient {
  121. val client = CoapClient(url)
  122. client.endpoint = coapRegisterEndpoint
  123. client.timeout = 6000
  124. return client
  125. }
  126.  
  127. private fun client(url: String): CoapClient {
  128. val client = CoapClient(url)
  129. client.endpoint = coapEndpoint
  130. client.timeout = 6000
  131. return client
  132. }
  133.  
  134. fun register(identity: String): CoapResponse? {
  135. Log.d(LogName, "POST $baseUrl/15011/9063")
  136. return registerClient("$baseUrl/15011/9063").post("{\"9090\": \"$identity\"}", MediaTypeRegistry.APPLICATION_JSON)
  137. }
  138.  
  139. fun ping(): CoapResponse? {
  140. Log.d(LogName, "GET $baseUrl/.well-known/core")
  141. return client("$baseUrl/.well-known/core").get()
  142. }
  143. }
Add Comment
Please, Sign In to add comment