Retrieving Logged Data

Hi,

I send a message to the a service that sets up the logging etc and then should disconnect. Later a message is sent when to retrieve the dowloaded data.

First message sent

//start passive mode
                DeviceService.OPERATION_START_WORKOUT_PASSIVE -> {
                    if (!blueToothAdapter?.isEnabled) {
                        sendMessageToJS(reactContext!!, RightStepNativeModule.NOTIFY_WORKOUT_CANCELLED_BLUETOOTH_OFF)
                    } else {
                        Log.i(TAG, "Device name: ${bluetoothDevice.name}")
                        Log.i(TAG, "onMessageEvent: ${event.operation}")
                        Log.i(TAG, "Service connected: $serviceBinder")
                        metaWearBoard = serviceBinder?.getMetaWearBoard(bluetoothDevice)
                        metaWearBoard?.connectAsync()!!.continueWith { task ->
                            if (task.isFaulted) {
                                Log.i(TAG, "Failed to connect")
                                sendMessageToJS(reactContext!!, RightStepNativeModule.NOTIFY_METAWEAR_CONNECTION_FAILED)
                            } else {
                                Log.i(TAG, "Connected")
                                sendMessageToJS(reactContext!!, RightStepNativeModule.LOGGING_STARTED_EVENT)
                                /* not sure about this */
                                accelerometer?.acceleration()?.addRouteAsync { source ->
                                    source.log { data, env ->
                                        sendMessageToJS(reactContext!!, RightStepNativeModule.TEST_DATA_EVENT)
                                    }
                                }?.continueWith {
                                    logger?.start(true);
                                    accelerometer?.acceleration()?.start()
                                    accelerometer?.start()
                                    sensorLed?.editPattern(Color.GREEN, PatternPreset.PULSE)?.commit()
                                    sensorLed?.play() 
                                    metaWearBoard?.disconnectAsync()?.continueWith {
                                        sensorLed?.stop(true)
                                        //sendMessageToJS(reactContext!!, RightStepNativeModule.NOTIFY_WORKOUT_FINISHED)
                                        //maybe send message to say disconnected and logging?
                                        //reset values just to be sure
                                        //values removed for this discussion

                                    }
                                }

                            }
                        }
                        metaWearBoard?.onUnexpectedDisconnect { status ->
                            Log.i("MainActivity", "Unexpectedly lost connection: $status")
                            sensorLed?.stop(true)
                        }
                    }

                }

the second message to trigger the download of data

DeviceService.OPERATION_STOP_WORKOUT_PASSIVE -> {
                    if (!blueToothAdapter?.isEnabled) {
                        sendMessageToJS(reactContext!!, RightStepNativeModule.NOTIFY_WORKOUT_CANCELLED_BLUETOOTH_OFF)
                    } else {
                        Log.i(TAG, "Device name: ${bluetoothDevice.name}")
                        Log.i(TAG, "onMessageEvent: ${event.operation}")
                        Log.i(TAG, "Service connected: $serviceBinder")
                        metaWearBoard = serviceBinder?.getMetaWearBoard(bluetoothDevice)
                        metaWearBoard?.connectAsync()!!.continueWith { task ->
                            if (task.isFaulted) {
                                Log.i(TAG, "Failed to connect")
                                sendMessageToJS(reactContext!!, RightStepNativeModule.NOTIFY_METAWEAR_CONNECTION_FAILED)
                            } else {
                                Log.i(TAG, "Connected")
                                //sendMessageToJS(reactContext!!, RightStepNativeModule.NOTIFY_METAWEAR_CONNECTED)
                                sensorLed?.stop(true)
                                sendMessageToJS(reactContext!!, RightStepNativeModule.DOWNLOAD_STARTED_EVENT);
                                sensorLed?.editPattern(Color.RED, PatternPreset.PULSE)?.commit()
                                sensorLed?.play()
                                //create a data route for the acclerometer
                                accelerometer?.acceleration()?.addRouteAsync { source ->
                                            source.log { data, env ->
                                               sendMessageToJS(reactContext!!, RightStepNativeModule.TEST_DATA_EVENT)
                                            }
                                        }?.continueWith {
                                        accelerometer?.stop()
                                        sensorLed?.stop(true)
                                        null
                                }
                                logger?.stop()
                                //download data
                                logger?.downloadAsync(100, {update, total ->
                                    sendMessageToJS(reactContext!!, RightStepNativeModule.DOWNLOAD_UPDATE_EVENT);
                                })?.continueWith { task ->
                                    if (task.isFaulted()) {
                                        sendMessageToJS(reactContext!!, RightStepNativeModule.DOWNLOAD_FAIL_EVENT);
                                    } else {

                                        metaWearBoard?.disconnectAsync()?.continueWith {
                                            sendMessageToJS(reactContext!!, RightStepNativeModule.DOWNLOAD_COMPLETE_EVENT);
                                        }
                                    }

                                }

                            }

                        }
                    }

                }

I am unsure how the accelerometer data route works in the case of logging (in streaming mode this approach works). As you may see above I actually try to create the data route twice which im sure is wrong. The download data seems to work and I revieve update and complete events.

Any help to get over this hurdle would be greatly appreciated.

thanks in advance.

Sign In or Register to comment.