Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MainActivity {
- // ....
- private fun startRegisterProcess(onFinish: () -> Unit) {
- if (service.isRegistered(applicationContext)) return
- val identity = "${UUID.randomUUID()}"
- service.register(identity, { registerResult ->
- SettingsUtil.setIdentity(applicationContext, identity)
- SettingsUtil.setPreSharedKey(applicationContext, registerResult.preSharedKey)
- onFinish()
- }, { onError("Unable to register app at gateway! Please try it later again") })
- }
- // ...
- }
- class TradfriService(context: Context) {
- private val client: TradfriClient
- private val gson = Gson()
- private val handler = CoroutineExceptionHandler { _, ex ->
- Log.println(Log.ERROR, "TradfriService", Log.getStackTraceString(ex))
- }
- init {
- val gatewayIp = SettingsUtil.getGatewayIp(context) ?: ""
- val securityId = SettingsUtil.getSecurityId(context) ?: ""
- val identity = SettingsUtil.getIdentity(context)
- val preSharedKey = SettingsUtil.getPreSharedKey(context)
- client = TradfriClient(gatewayIp, securityId, identity, preSharedKey)
- }
- fun isRegistered(context: Context): Boolean {
- val identity = SettingsUtil.getIdentity(context)
- val preSharedKey = SettingsUtil.getPreSharedKey(context)
- return identity != null && identity.isNotEmpty() && preSharedKey != null && preSharedKey.isNotEmpty()
- }
- fun register(identity: String, onSuccess: (RegisterResult) -> Unit, onError: () -> Unit) {
- launch(CommonPool + handler) {
- val response = client.register(identity)
- if (response == null) {
- launch(UI + handler) { onError() }
- return@launch
- }
- if (!response.isSuccess) {
- launch(UI + handler) { onError() }
- return@launch
- }
- val result = parseResponse(response, RegisterResult::class.java)
- if (result == null) {
- launch(UI + handler) { onError() }
- return@launch
- }
- launch(UI + handler) { onSuccess(result) }
- }
- }
- fun ping(onSuccess: (String) -> Unit, onError: () -> Unit): Job {
- Log.d(LogName, "_ping")
- return launch(CommonPool + handler) {
- println("_ping start")
- val response = client.ping()
- if (response == null) {
- launch(UI + handler) { onError() }
- return@launch
- }
- if (!response.isSuccess) {
- launch(UI + handler) { onError() }
- return@launch
- }
- launch(UI + handler) { onSuccess(String(response.payload)) }
- println("_ping end")
- }
- }
- }
- class TradfriClient(ip: String,
- private val securityId: String,
- private var identity: String?,
- private var preSharedKey: String?) {
- private val baseUrl = "coaps://$ip:5684"
- private val gson = Gson()
- private val coapRegisterEndpoint: CoapEndpoint = getRegisterCoapEndpoint()
- private val coapEndpoint: CoapEndpoint = getDefaultCoapEndpoint()
- private fun getRegisterCoapEndpoint(): CoapEndpoint {
- val builder = DtlsConnectorConfig.Builder(InetSocketAddress(0))
- builder.setPskStore(StaticPskStore("Client_identity", securityId.toByteArray()))
- builder.setRetransmissionTimeout(50000)
- val dtlsConnector = DTLSConnector(builder.build())
- val network = NetworkConfig.createStandardWithoutFile()
- return CoapEndpoint(dtlsConnector, network)
- }
- private fun getDefaultCoapEndpoint(): CoapEndpoint {
- val builder = DtlsConnectorConfig.Builder(InetSocketAddress(0))
- builder.setPskStore(StaticPskStore(identity ?: "", (preSharedKey ?: "").toByteArray()))
- builder.setRetransmissionTimeout(50000)
- val dtlsConnector = DTLSConnector(builder.build())
- val network = NetworkConfig.createStandardWithoutFile()
- return CoapEndpoint(dtlsConnector, network)
- }
- private fun registerClient(url: String): CoapClient {
- val client = CoapClient(url)
- client.endpoint = coapRegisterEndpoint
- client.timeout = 6000
- return client
- }
- private fun client(url: String): CoapClient {
- val client = CoapClient(url)
- client.endpoint = coapEndpoint
- client.timeout = 6000
- return client
- }
- fun register(identity: String): CoapResponse? {
- Log.d(LogName, "POST $baseUrl/15011/9063")
- return registerClient("$baseUrl/15011/9063").post("{\"9090\": \"$identity\"}", MediaTypeRegistry.APPLICATION_JSON)
- }
- fun ping(): CoapResponse? {
- Log.d(LogName, "GET $baseUrl/.well-known/core")
- return client("$baseUrl/.well-known/core").get()
- }
- }
Add Comment
Please, Sign In to add comment