diff --git a/src/main/java/io/rudolph/netatmo/JacksonTransform.kt b/src/main/java/io/rudolph/netatmo/JacksonTransform.kt index f4cb4ac..2ba97e2 100644 --- a/src/main/java/io/rudolph/netatmo/JacksonTransform.kt +++ b/src/main/java/io/rudolph/netatmo/JacksonTransform.kt @@ -12,7 +12,9 @@ import io.rudolph.netatmo.api.energy.model.TemperatureType import io.rudolph.netatmo.api.energy.model.ThermMode import io.rudolph.netatmo.api.energy.model.ZoneType import io.rudolph.netatmo.api.energy.model.transform.* +import io.rudolph.netatmo.api.presence.model.AppType import io.rudolph.netatmo.api.presence.model.EventType +import io.rudolph.netatmo.api.presence.transform.AppTypeDeserializer import io.rudolph.netatmo.api.presence.transform.EventTypeDeserializer import io.rudolph.netatmo.api.weather.model.Measure import io.rudolph.netatmo.api.weather.model.transform.MeasureDeserializer @@ -36,6 +38,7 @@ internal object JacksonTransform { addDeserializer(Scale::class.java, ScaleDeserializer()) addDeserializer(ScaleType::class.java, ScaleTypeDeserializer()) addDeserializer(EventType::class.java, EventTypeDeserializer()) + addDeserializer(AppType::class.java, AppTypeDeserializer()) addSerializer(ThermMode::class.java, ThermModeSerializer()) addSerializer(DeviceType::class.java, DeviceTypeSerializer()) diff --git a/src/main/java/io/rudolph/netatmo/api/presence/model/AppType.kt b/src/main/java/io/rudolph/netatmo/api/presence/model/AppType.kt new file mode 100644 index 0000000..c74f0b6 --- /dev/null +++ b/src/main/java/io/rudolph/netatmo/api/presence/model/AppType.kt @@ -0,0 +1,6 @@ +package io.rudolph.netatmo.api.presence.model + +enum class AppType(val value: String = "unknown") { + CAMERA("app_camera"), + UNKNOWN +} \ No newline at end of file diff --git a/src/main/java/io/rudolph/netatmo/api/presence/model/BaseEvent.kt b/src/main/java/io/rudolph/netatmo/api/presence/model/BaseEvent.kt new file mode 100644 index 0000000..c68f470 --- /dev/null +++ b/src/main/java/io/rudolph/netatmo/api/presence/model/BaseEvent.kt @@ -0,0 +1,13 @@ +package io.rudolph.netatmo.api.presence.model + +import com.fasterxml.jackson.annotation.JsonAlias +import com.fasterxml.jackson.annotation.JsonProperty + +data class BaseEvent( + /** + * Identifier of the event + */ + @JsonProperty("id") + @JsonAlias("id", "event_id") + override val id: String? = null) + : Event() \ No newline at end of file diff --git a/src/main/java/io/rudolph/netatmo/api/presence/model/Event.kt b/src/main/java/io/rudolph/netatmo/api/presence/model/Event.kt index 9b7a17e..31efe76 100644 --- a/src/main/java/io/rudolph/netatmo/api/presence/model/Event.kt +++ b/src/main/java/io/rudolph/netatmo/api/presence/model/Event.kt @@ -1,74 +1,77 @@ package io.rudolph.netatmo.api.presence.model +import com.fasterxml.jackson.annotation.JsonAlias import com.fasterxml.jackson.annotation.JsonProperty -data class Event( - /** - * Subtypes of SD and Alim events. Go to Cameras page for further details. - * - * TODO change type to [EventSubType] - */ - @JsonProperty("sub_type") - val subType: String? = null, +abstract class Event { + /** + * Subtypes of SD and Alim events. Go to Cameras page for further details. + * + * TODO change type to [EventSubType] + */ + @JsonProperty("sub_type") + val subType: EventType? = null - /** - * Camera that detected the event - */ - @JsonProperty("camera_id") - val cameraId: String? = null, + /** + * Camera that detected the event + */ + @JsonProperty("camera_id") + val cameraId: String? = null - /** - * Status of the video (recording, deleted or available) - */ - @JsonProperty("video_status") - val videoStatus: String? = null, + /** + * Status of the video (recording deleted or available) + */ + @JsonProperty("video_status") + val videoStatus: String? = null - /** - * Identifier of the event - */ - @JsonProperty("id") - val id: String? = null, + /** + * Identifier of the event + */ + @JsonProperty("id") + @JsonAlias("id", "event_id") + open val id: String? = null - /** - * Time of occurence of event - */ - @JsonProperty("time") - val time: Int? = null, + /** + * Time of occurence of event + */ + @JsonProperty("time") + val time: Int? = null - /** - * Type of events. - */ - @JsonProperty("type") - val type: EventType? = null, + /** + * Type of events. + */ + @JsonProperty("type") + @JsonAlias("event_type", "type") + val type: EventType? = null - /** - * User facing event description - */ - @JsonProperty("message") - val message: String? = null, + /** + * User facing event description + */ + @JsonProperty("message") + val message: String? = null - /** - * If person was considered "away" before being seen during this event - */ - @JsonProperty("is_arrival") - val isArrival: Boolean? = null, + /** + * If person was considered "away" before being seen during this event + */ + @JsonProperty("is_arrival") + val isArrival: Boolean? = null - /** - * Snapshot id, version and key. (Used in [io.rudolph.netatmo.api.presence.PresenceConnector.getCameraPicture]) - */ - @JsonProperty("snapshot") - val snapshot: Snapshot? = null, + /** + * Snapshot id version and key. (Used in [io.rudolph.netatmo.api.presence.PresenceConnector.getCameraPicture]) + */ + @JsonProperty("snapshot") + val snapshot: Snapshot? = null - /** - * Id of the person the event is about (if any) - */ - @JsonProperty("person_id") - val personId: String? = null, + /** + * Id of the person the event is about (if any) + */ + @JsonProperty("person_id") + val personId: String? = null - /** - * Identifier of the video - */ - @JsonProperty("video_id") - val videoId: String? = null -) \ No newline at end of file + /** + * Identifier of the video + */ + @JsonProperty("video_id") + val videoId: String? = null +} \ No newline at end of file diff --git a/src/main/java/io/rudolph/netatmo/api/presence/model/EventSubType.kt b/src/main/java/io/rudolph/netatmo/api/presence/model/EventSubType.kt deleted file mode 100644 index b3aa45f..0000000 --- a/src/main/java/io/rudolph/netatmo/api/presence/model/EventSubType.kt +++ /dev/null @@ -1,5 +0,0 @@ -package io.rudolph.netatmo.api.presence.model - -enum class EventSubType { - // TODO Add Subtypes from https://dev.netatmo.com/en-US/resources/technical/reference/security#event -} \ No newline at end of file diff --git a/src/main/java/io/rudolph/netatmo/api/presence/model/Events.kt b/src/main/java/io/rudolph/netatmo/api/presence/model/Events.kt index fef2380..6514423 100644 --- a/src/main/java/io/rudolph/netatmo/api/presence/model/Events.kt +++ b/src/main/java/io/rudolph/netatmo/api/presence/model/Events.kt @@ -5,5 +5,5 @@ import com.fasterxml.jackson.annotation.JsonProperty data class Events( @JsonProperty("events_list") - val eventsList: List? = null + val eventsList: List? = null ) \ No newline at end of file diff --git a/src/main/java/io/rudolph/netatmo/api/presence/model/PersonsEvent.kt b/src/main/java/io/rudolph/netatmo/api/presence/model/PersonsEvent.kt new file mode 100644 index 0000000..fe06007 --- /dev/null +++ b/src/main/java/io/rudolph/netatmo/api/presence/model/PersonsEvent.kt @@ -0,0 +1,24 @@ +package io.rudolph.netatmo.api.presence.model + +import com.fasterxml.jackson.annotation.JsonProperty + +data class PersonsEvent( + + @JsonProperty("app_type") + val appType: AppType, + + @JsonProperty("persons") + val persons: List = mutableListOf(), + + @JsonProperty("snapshot_id") + val snapshotId: String?, + + @JsonProperty("snapshot_key") + val snapshotKey: String?, + + @JsonProperty("home_id") + val homeId: String, + + @JsonProperty("home_name") + val homeName: String +) : Event() \ No newline at end of file diff --git a/src/main/java/io/rudolph/netatmo/api/presence/model/PersonsEventPerson.kt b/src/main/java/io/rudolph/netatmo/api/presence/model/PersonsEventPerson.kt new file mode 100644 index 0000000..1175038 --- /dev/null +++ b/src/main/java/io/rudolph/netatmo/api/presence/model/PersonsEventPerson.kt @@ -0,0 +1,17 @@ +package io.rudolph.netatmo.api.presence.model + +import com.fasterxml.jackson.annotation.JsonProperty + +data class PersonsEventPerson( + @JsonProperty("id") + val id: String, + + @JsonProperty("face_id") + val faceId: String, + + @JsonProperty("face_key") + val faceKey: String, + + @JsonProperty("is_known") + val isKnown: Boolean = false +) \ No newline at end of file diff --git a/src/main/java/io/rudolph/netatmo/api/presence/model/PresenceHome.kt b/src/main/java/io/rudolph/netatmo/api/presence/model/PresenceHome.kt index 9950dcc..4613792 100644 --- a/src/main/java/io/rudolph/netatmo/api/presence/model/PresenceHome.kt +++ b/src/main/java/io/rudolph/netatmo/api/presence/model/PresenceHome.kt @@ -21,7 +21,7 @@ data class PresenceHome( val place: Place? = null, @JsonProperty("events") - val events: List? = null, + val events: List? = null, @JsonProperty("modules") val modules: List? = null diff --git a/src/main/java/io/rudolph/netatmo/api/presence/transform/AppTypeDeserializer.kt b/src/main/java/io/rudolph/netatmo/api/presence/transform/AppTypeDeserializer.kt new file mode 100644 index 0000000..4a8cca2 --- /dev/null +++ b/src/main/java/io/rudolph/netatmo/api/presence/transform/AppTypeDeserializer.kt @@ -0,0 +1,17 @@ +package io.rudolph.netatmo.api.presence.transform + +import com.fasterxml.jackson.core.JsonParser +import com.fasterxml.jackson.databind.DeserializationContext +import com.fasterxml.jackson.databind.JsonDeserializer +import io.rudolph.netatmo.api.presence.model.AppType + + +class AppTypeDeserializer : JsonDeserializer() { + override fun deserialize(p: JsonParser?, ctxt: DeserializationContext?): AppType { + return p?.valueAsString?.let { name -> + AppType.values().find { + it.value == name + } + } ?: AppType.UNKNOWN + } +} diff --git a/src/main/java/io/rudolph/netatmo/api/welcome/WelcomeConnector.kt b/src/main/java/io/rudolph/netatmo/api/welcome/WelcomeConnector.kt index b9ec104..273dce8 100644 --- a/src/main/java/io/rudolph/netatmo/api/welcome/WelcomeConnector.kt +++ b/src/main/java/io/rudolph/netatmo/api/welcome/WelcomeConnector.kt @@ -1,8 +1,10 @@ package io.rudolph.netatmo.api.welcome +import io.rudolph.netatmo.JacksonTransform import io.rudolph.netatmo.api.energy.model.BaseResult import io.rudolph.netatmo.api.presence.PresenceConnector import io.rudolph.netatmo.api.presence.model.Events +import io.rudolph.netatmo.api.presence.model.PersonsEvent import io.rudolph.netatmo.api.welcome.service.WelcomeService import io.rudolph.netatmo.executable import io.rudolph.netatmo.executable.BodyResultExecutable @@ -87,4 +89,13 @@ class WelcomeConnector(api: Retrofit) : PresenceConnector(api) { return setPersonsHome(homeId, listOf(personId)) } + fun onExternalCameraEvent(jsonString: String): PersonsEvent? { + return try { + JacksonTransform.deserialize(jsonString) + } catch (e: Exception) { + e.printStackTrace() + null + } + } + } \ No newline at end of file