Commit c05c72c6 authored by Andreas Schildbach's avatar Andreas Schildbach
Browse files

SetClockJob: Actually update the time, rather than blinking an LED.

parent fa972779
...@@ -32,3 +32,4 @@ val VIBRA_CHARACTERISTIC_UUID = UUID.fromString("4223020f-2342-2342-2342-2342234 ...@@ -32,3 +32,4 @@ val VIBRA_CHARACTERISTIC_UUID = UUID.fromString("4223020f-2342-2342-2342-2342234
val ROCKETS_CHARACTERISTIC_UUID = UUID.fromString("42230210-2342-2342-2342-234223422342") val ROCKETS_CHARACTERISTIC_UUID = UUID.fromString("42230210-2342-2342-2342-234223422342")
val SINGLE_LED_CHARACTERISTIC_UUID = UUID.fromString("42230211-2342-2342-2342-234223422342") val SINGLE_LED_CHARACTERISTIC_UUID = UUID.fromString("42230211-2342-2342-2342-234223422342")
val LIGHT_SENSOR_CHARACTERISTIC_UUID = UUID.fromString("422302f0-2342-2342-2342-234223422342") val LIGHT_SENSOR_CHARACTERISTIC_UUID = UUID.fromString("422302f0-2342-2342-2342-234223422342")
val TIME_CHARACTERISTIC_UUID = UUID.fromString("42230201-2342-2342-2342-234223422342")
...@@ -30,7 +30,8 @@ import android.content.Context ...@@ -30,7 +30,8 @@ import android.content.Context
import androidx.work.Worker import androidx.work.Worker
import androidx.work.WorkerParameters import androidx.work.WorkerParameters
import de.ccc.events.badge.card10.CARD10_SERVICE_UUID import de.ccc.events.badge.card10.CARD10_SERVICE_UUID
import de.ccc.events.badge.card10.ROCKETS_CHARACTERISTIC_UUID import de.ccc.events.badge.card10.TIME_CHARACTERISTIC_UUID
import java.nio.ByteBuffer
import java.util.concurrent.CountDownLatch import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
...@@ -45,7 +46,7 @@ class SetClockJob(context: Context, workerParameters: WorkerParameters) : Worker ...@@ -45,7 +46,7 @@ class SetClockJob(context: Context, workerParameters: WorkerParameters) : Worker
try { try {
val gattLatch = CountDownLatch(1); val gattLatch = CountDownLatch(1);
var writeLatch: CountDownLatch? = null var writeLatch: CountDownLatch? = null
var rocketsCharacteristic: BluetoothGattCharacteristic? = null var timeCharacteristic: BluetoothGattCharacteristic? = null
val callback = object : BluetoothGattCallback() { val callback = object : BluetoothGattCallback() {
override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) { override fun onConnectionStateChange(gatt: BluetoothGatt, status: Int, newState: Int) {
System.out.println("=== doWork() onConnectionStateChange " + gatt + " / " + status + " / " + newState) System.out.println("=== doWork() onConnectionStateChange " + gatt + " / " + status + " / " + newState)
...@@ -56,7 +57,7 @@ class SetClockJob(context: Context, workerParameters: WorkerParameters) : Worker ...@@ -56,7 +57,7 @@ class SetClockJob(context: Context, workerParameters: WorkerParameters) : Worker
override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) { override fun onServicesDiscovered(gatt: BluetoothGatt, status: Int) {
System.out.println("=== doWork() onServicesDiscovered " + gatt + " / " + status) System.out.println("=== doWork() onServicesDiscovered " + gatt + " / " + status)
val card10Service = gatt.getService(CARD10_SERVICE_UUID) val card10Service = gatt.getService(CARD10_SERVICE_UUID)
rocketsCharacteristic = card10Service.getCharacteristic(ROCKETS_CHARACTERISTIC_UUID) timeCharacteristic = card10Service.getCharacteristic(TIME_CHARACTERISTIC_UUID)
gattLatch.countDown() gattLatch.countDown()
} }
...@@ -72,24 +73,14 @@ class SetClockJob(context: Context, workerParameters: WorkerParameters) : Worker ...@@ -72,24 +73,14 @@ class SetClockJob(context: Context, workerParameters: WorkerParameters) : Worker
if (!gattLatch.await(1, TimeUnit.MINUTES)) if (!gattLatch.await(1, TimeUnit.MINUTES))
return Result.retry() return Result.retry()
// TODO at this point update the clock, rather than blinking the LED val buffer = ByteBuffer.allocate(8)
buffer.putLong(System.currentTimeMillis())
rocketsCharacteristic?.value = ubyteArrayOf(0xffu, 0x00u, 0x00u).toByteArray() timeCharacteristic?.value = buffer.array()
if (gatt.writeCharacteristic(rocketsCharacteristic)) { if (gatt.writeCharacteristic(timeCharacteristic)) {
writeLatch = CountDownLatch(1)
if (!writeLatch.await(1, TimeUnit.MINUTES))
return Result.failure()
System.out.println("=== doWork() rocket flash on")
}
Thread.sleep(1000)
rocketsCharacteristic?.value = ubyteArrayOf(0x00u, 0x00u, 0x00u).toByteArray()
if (gatt.writeCharacteristic(rocketsCharacteristic)) {
writeLatch = CountDownLatch(1) writeLatch = CountDownLatch(1)
if (!writeLatch.await(1, TimeUnit.MINUTES)) if (!writeLatch.await(1, TimeUnit.MINUTES))
return Result.failure() return Result.failure()
System.out.println("=== doWork() rocket flash off") System.out.println("=== doWork() updated time")
} }
} catch (x: Exception) { } catch (x: Exception) {
x.printStackTrace() x.printStackTrace()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment