aboutsummaryrefslogtreecommitdiff
path: root/shared/src/commonMain/kotlin
diff options
context:
space:
mode:
Diffstat (limited to 'shared/src/commonMain/kotlin')
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt52
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/CommandsController.kt29
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt34
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofenceController.kt14
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt20
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt10
6 files changed, 159 insertions, 0 deletions
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt
new file mode 100644
index 0000000..6262ec8
--- /dev/null
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt
@@ -0,0 +1,52 @@
+package mx.trackermap.TrackerMap.controllers
+
+import mx.trackermap.TrackerMap.client.apis.AttributesApi
+import mx.trackermap.TrackerMap.client.models.Attribute
+import mx.trackermap.TrackerMap.client.models.Device
+
+class AttributesController(
+ private val attributesApi: AttributesApi,
+ private val sessionController: SessionController)
+{
+ var userAttributes = emptyArray<Attribute>()
+ val deviceAttributes = mutableMapOf<Int, Array<Attribute>>()
+
+ /** userAttributes **/
+
+ suspend fun getUserAttributes() {
+ userAttributes = attributesApi.attributesComputedGet(userId = sessionController.user?.id)
+ }
+
+ fun getUserAttribute(attribute: String) = userAttributes.find { it.attribute == attribute }
+
+ suspend fun createUserAttribute(attribute: Attribute) {
+ userAttributes += attributesApi.attributesComputedPost(attribute)
+ }
+
+ suspend fun updateUserAttribute(attribute: Attribute) {
+ userAttributes = userAttributes.map {
+ if (attribute.id != null && it.id == attribute.id) {
+ attributesApi.attributesComputedIdPut(attribute, attribute.id)
+ } else it
+ }.toTypedArray()
+ }
+
+ suspend fun deleteUserAttribute(attribute: String) {
+ userAttributes.find { it.attribute == attribute }?.id?.let { id ->
+ attributesApi.attributesComputedIdDelete(id)
+ userAttributes = userAttributes.filter { it.id != id }.toTypedArray()
+ }
+ }
+
+ /** deviceAttributes **/
+
+ suspend fun getDeviceAttributes(device: Device) {
+ device.id?.let {
+ deviceAttributes[it] = attributesApi.attributesComputedGet(deviceId = device.id)
+ }
+ }
+
+ fun getDeviceAttribute(device: Device, attribute: String) =
+ deviceAttributes[device.id]?.find { it.attribute == attribute }
+
+} \ No newline at end of file
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/CommandsController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/CommandsController.kt
new file mode 100644
index 0000000..b710763
--- /dev/null
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/CommandsController.kt
@@ -0,0 +1,29 @@
+package mx.trackermap.TrackerMap.controllers
+
+import mx.trackermap.TrackerMap.client.apis.CommandsApi
+import mx.trackermap.TrackerMap.client.models.Command
+import mx.trackermap.TrackerMap.client.models.Device
+
+class CommandsController(
+ val commandsApi: CommandsApi,
+ val sessionController: SessionController)
+{
+ var commands = emptyArray<Command>()
+ val deviceCommands = mutableMapOf<Int, Array<Command>>()
+
+ suspend fun getAllCommands () {
+ sessionController.user?.id?.let {
+ commands = commandsApi.commandsGet(userId = it)
+ }
+ }
+
+ suspend fun getDeviceCommands (device: Device) {
+ device.id?.let {
+ deviceCommands[it] = commandsApi.commandsSendGet (deviceId = device.id)
+ }
+ }
+
+ suspend fun sendCommand (command: Command) {
+ commandsApi.commandsSendPost(command)
+ }
+} \ No newline at end of file
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt
new file mode 100644
index 0000000..2e0d924
--- /dev/null
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt
@@ -0,0 +1,34 @@
+package mx.trackermap.TrackerMap.controllers
+
+import mx.trackermap.TrackerMap.client.apis.DevicesApi
+import mx.trackermap.TrackerMap.client.models.Device
+
+class DeviceController(
+ private val devicesApi: DevicesApi,
+ private val sessionController: SessionController)
+{
+ var devices = emptyArray<Device>()
+
+ suspend fun getDevices() {
+ devices = devicesApi.devicesGet(userId = sessionController.user?.id)
+ }
+
+ fun getDevice(id: Int) = devices.find { it.id == id }
+
+ suspend fun createDevice(device: Device) {
+ devices += devicesApi.devicesPost(device)
+ }
+
+ suspend fun updateDevice(id: Int, device: Device) {
+ devices = devices.map {
+ if (it.id == id)
+ devicesApi.devicesIdPut(id = id, body = device)
+ else it
+ }.toTypedArray()
+ }
+
+ suspend fun deleteDevice(id: Int) {
+ devicesApi.devicesIdDelete(id)
+ devices = devices.filter { it.id != id }.toTypedArray()
+ }
+} \ No newline at end of file
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofenceController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofenceController.kt
new file mode 100644
index 0000000..011d094
--- /dev/null
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofenceController.kt
@@ -0,0 +1,14 @@
+package mx.trackermap.TrackerMap.controllers
+
+import mx.trackermap.TrackerMap.client.apis.GeofencesApi
+import mx.trackermap.TrackerMap.client.models.Geofence
+
+class GeofenceController(val geofencesApi: GeofencesApi) {
+ var geofences = emptyArray<Geofence>()
+
+ suspend fun getGeofences () {
+ geofences = geofencesApi.geofencesGet()
+ }
+
+ fun getGeofence (id: Int) = geofences.find { it.id == id }
+} \ No newline at end of file
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt
new file mode 100644
index 0000000..ba892f6
--- /dev/null
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt
@@ -0,0 +1,20 @@
+package mx.trackermap.TrackerMap.controllers
+
+import mx.trackermap.TrackerMap.client.apis.SessionApi
+import mx.trackermap.TrackerMap.client.models.User
+
+class SessionController(private val sessionApi: SessionApi) {
+ var user: User? = null
+
+ suspend fun createSession (email: String, password: String) {
+ user = sessionApi.sessionPost(email, password)
+ }
+
+ suspend fun getSession () {
+ user = sessionApi.sessionGet()
+ }
+
+ suspend fun deleteSession () {
+ sessionApi.sessionDelete()
+ }
+} \ No newline at end of file
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt
new file mode 100644
index 0000000..a398185
--- /dev/null
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt
@@ -0,0 +1,10 @@
+package mx.trackermap.TrackerMap.controllers
+
+import mx.trackermap.TrackerMap.client.apis.UsersApi
+import mx.trackermap.TrackerMap.client.models.User
+
+class UserController(private val usersApi: UsersApi) {
+ suspend fun updateUser(user: User) {
+ usersApi.usersPost(user)
+ }
+} \ No newline at end of file