Skip to content

Instantly share code, notes, and snippets.

@JogahCR
Created Feb 17, 2022
Embed
What would you like to do?
Hatch Homework Project Docs
package co.hatch.deviceClientLib.connectivity
import androidx.annotation.WorkerThread
import co.hatch.deviceClientLib.model.Device
/**
* Interface in charge of abstracting the implementation of general operations to deal with
* hardware devices ([Device]) independently from the underling connectivity technologies they require.
*/
interface ConnectivityClient {
/**
* Discover [Device]s. This method must be called from a worker thread and it is safe to call
* multiple times over time, on each run the list of discovered [Device]s will have
* an up to date [Device.rssi] property.
*
* @return returns a list of [Device]s available
*/
fun discoverDevices(): List<Device>
/**
* Perform a connection to the given [deviceId].
*
* @param deviceId unique identifier of the device to connect to.
* @param onDeviceStateChangeListener instance for on-going callbacks on [Device] state changes
*
* @return true if the operation succeeded, [Device]s in this state will
* reflect [Device.connected] as true, [Device.latestConnectedTime] will be set and it will
* update itself over-time to reflect for how long it has been in
* connected state [Device.elapsedSecsConnected], otherwise false
*
* @throws IllegalStateException when the given [deviceId] do not match any of the
* discovered devices
*/
@WorkerThread
fun connectToDeviceBy(
deviceId: String,
onDeviceStateChangeListener: OnDeviceStateChangeListener
): Boolean
/**
* Perform a name update on the given [deviceId].
*
* @param deviceId unique identifier of the device to connect to.
* @param updatedName new name to set for this [deviceId]
*
* @throws IllegalStateException when the given [deviceId] is not connected.
*/
fun updateDeviceName(deviceId: String, updatedName: String): Boolean
/**
* Performs a disconnect to the given [deviceId].
*
* @throws IllegalStateException when the given [deviceId] do not match any of the
* discovered devices
* */
fun disconnectFromDevice(deviceId: String): Boolean
object Factory {
private val client by lazy { FakeClient() }
/**
* Factory method to create the concrete [ConnectivityClient] implementation.
*/
fun create(): ConnectivityClient = client
}
/**
* Callback interface to communicate back to any consumer the [Device] state updates.
*/
interface OnDeviceStateChangeListener {
fun onDeviceStateChanged(deviceId: String, device: Device)
}
}
package co.hatch.deviceClientLib.model
import java.util.*
/**
* Data model to represent a device that can be discovered by this application.
*/
data class Device(
/**
* Unique identifier
*/
val id: String,
/**
* Device name
*/
val name: String,
/**
* RSSI stands for Received Signal Strength Indicator
* This property represent how strong or week the signal to the device is. Values ranges from
* approximately -50 to -90 where higher the number higher the signal strength.
*/
val rssi: Int,
/**
* Flag that indicates if the device is currently connected or not
*/
val connected: Boolean = false,
/**
* Keeps track for how long the device has been in connected state
*/
val elapsedSecsConnected: Long = 0,
/**
* Keeps track of the last time this device has been in connected state
*/
val latestConnectedTime: Date? = null,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment