Skip to content
This repository was archived by the owner on Mar 15, 2024. It is now read-only.

Commit e4dd8e7

Browse files
committed
Cleanup
1 parent e680f13 commit e4dd8e7

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

app/src/main/java/com/q42/milightremotecontrol/MainActivity.kt

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,32 +28,13 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
2828
setContentView(R.layout.activity_main)
2929

3030
controller = MiLightController(this@MainActivity)
31-
// controller.discover()
31+
3232
launch(Dispatchers.IO) {
33-
val ip = try {
34-
InetAddress.getByName(bridge)
35-
} catch (ex: UnknownHostException) {
36-
withContext(Dispatchers.Main) {
37-
toast("Error while looking for bridge:\n${ex.message}")
38-
}
39-
null
40-
}
41-
ip?.let {
42-
Log.i(TAG, "$ip")
43-
controller.bridgeAddress = ip
44-
}
33+
discoverBridge()
4534
}
4635

4736
discover.setOnClickListener {
48-
launch(Dispatchers.IO) {
49-
50-
val bridgeAddress = async { controller.discover() }
51-
52-
withContext(Dispatchers.Main) {
53-
54-
bridge_id.text = bridgeAddress.await().toString()
55-
}
56-
}
37+
discoverBridge()
5738
}
5839

5940
on_button.setOnClickListener {
@@ -102,6 +83,18 @@ class MainActivity : AppCompatActivity(), CoroutineScope {
10283
}
10384
}
10485

86+
fun discoverBridge() {
87+
launch(Dispatchers.IO) {
88+
89+
val bridgeAddress = async { controller.discover() }
90+
91+
withContext(Dispatchers.Main) {
92+
93+
bridge_id.text = bridgeAddress.await().toString()
94+
}
95+
}
96+
}
97+
10598

10699
override fun onDestroy() {
107100
super.onDestroy()

app/src/main/java/com/q42/milightremotecontrol/MiLightController.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,18 @@ class MiLightController(
4848
socket.receive(receivePacket)
4949
val session1 = UByte.fromByte(receivePacket.data[19]).toInt()
5050
val session2 = UByte.fromByte(receivePacket.data[20]).toInt()
51-
Log.i(TAG, "session id: $session1 $session2")
51+
Log.d(TAG, "session id: $session1 $session2")
5252
return Session(socket, session1, session2)
5353
}
5454

5555
private fun sendCommand(cmd: ByteArray) {
5656

5757
if (bridgeAddress == null) {
58-
Log.i(TAG, "NO BRIDGE")
58+
Log.w(TAG, "NO BRIDGE")
5959
return
6060
}
6161

62+
Log.d(TAG, "Request session")
6263
launch(Dispatchers.IO) {
6364
try {
6465
// request session
@@ -122,8 +123,6 @@ class MiLightController(
122123
withContext(Dispatchers.Main) {
123124
Log.i(TAG, ("received ${response.length} bytes from ${response.address}: $data"))
124125
}
125-
126-
socket.close()
127126
}
128127

129128
bridgeAddress = response.address

0 commit comments

Comments
 (0)