Skip to content

Commit

Permalink
Bugfixing
Browse files Browse the repository at this point in the history
- if Auth token is expired, a new one will be requested correctly
-  model refactoring
  • Loading branch information
navybk committed Aug 29, 2018
1 parent 617440d commit 5b5d591
Show file tree
Hide file tree
Showing 20 changed files with 211 additions and 146 deletions.
12 changes: 11 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ plugins {
}

group 'io.rudolph.netatmo'
version '0.4.0'
version '0.4.1'

repositories {
mavenCentral()
Expand Down Expand Up @@ -82,6 +82,11 @@ jar {
}
}

task sourcesJar(type: Jar, dependsOn: classes) {
classifier = 'sources'
from sourceSets.main.allSource
}

task customFatJar(type: Jar) {

manifest {
Expand All @@ -94,4 +99,9 @@ task customFatJar(type: Jar) {
baseName = 'all-in-one-jar'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}

artifacts {
archives sourcesJar
archives javadocJar
}
4 changes: 2 additions & 2 deletions src/main/java/io/rudolph/netatmo/NetatmoApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class NetatmoApi(userMail: String? = null,
) {

private companion object {
const val BASEAPIENDPOINT = "https://app.netatmo.net/api/"
const val AUTHENDPOINT = "https://app.netatmo.net/oauth2/token"
const val BASEAPIENDPOINT = "https://api.netatmo.com/api/"
const val AUTHENDPOINT = "https://api.netatmo.com/oauth2/token"
}

private val tokenStorage = TokenStorage(accessToken, refreshToken, scope)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract class Module internal constructor(
@JsonProperty("id")
@field:JsonAlias("id", "_id")
@param:JsonAlias("id", "_id")
open val id: String? = null,
open val id: String = "notset",

/**
* 90 = low
Expand Down
73 changes: 49 additions & 24 deletions src/main/java/io/rudolph/netatmo/api/energy/EnergyConnector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.rudolph.netatmo.api.energy

import io.rudolph.netatmo.api.common.CommonConnector
import io.rudolph.netatmo.api.common.model.DeviceType
import io.rudolph.netatmo.api.common.model.Module
import io.rudolph.netatmo.api.energy.model.*
import io.rudolph.netatmo.api.energy.model.module.EnergyModule
import io.rudolph.netatmo.api.energy.model.module.RelayModule
Expand All @@ -10,7 +11,6 @@ import io.rudolph.netatmo.api.energy.model.module.ValveModule
import io.rudolph.netatmo.api.energy.service.EnergyService
import io.rudolph.netatmo.executable
import io.rudolph.netatmo.executable.BodyResultExecutable
import io.rudolph.netatmo.executable.Executable
import io.rudolph.netatmo.executable.PlainCallExecutable
import io.rudolph.netatmo.executable.PlainFunctionExecutable
import io.rudolph.netatmo.oauth2.toTimestamp
Expand Down Expand Up @@ -337,31 +337,56 @@ class EnergyConnector(api: Retrofit) : CommonConnector(api) {
.executable
}

fun getCombinedModule(homeId: String, moduleId: String): PlainFunctionExecutable<Module?> {
val func = inner@{
val module = getHomesData(homeId).executeSync()
?.homes
?.find { it.id == homeId }
?.modules
?.find { it.id == moduleId }
?: return@inner null

getHomeStatus(homeId, module.type).executeSync()
?.homes
?.find { it.id == homeId }
?.modules
?.find { it.id == moduleId }
?.let {
when (module) {
is ValveModule -> module.join(it as ValveModule)
is RelayModule -> module.join(it as RelayModule)
is ThermostatModule -> module.join(it as ThermostatModule)
else -> module
}
}
}
return PlainFunctionExecutable(func)
}

fun getCombinedHome(homeId: String? = null): PlainFunctionExecutable<HomesDataBody?> {
val func = {
getHomesData(homeId).executeSync()
?.let { origin ->
val home = origin.homes.mapNotNull { home ->
val status = getHomeStatus(home.id).executeSync()
?.home
?.find { it.id == home.id }
?.modules ?: return@mapNotNull null
val modules = home.modules.mapNotNull { module ->
status.find { it.id == module.id }
?.let {
when (module) {
is ValveModule -> module.join(it as ValveModule)
is RelayModule -> module.join(it as RelayModule)
is ThermostatModule -> module.join(it as ThermostatModule)
else -> module
}
val func = inner@{
val origin = getHomesData(homeId).executeSync()
return@inner origin?.homes
?.mapNotNull { home ->
val status = getHomeStatus(home.id).executeSync()
?.homes
?.find { it.id == home.id }
?.modules ?: return@mapNotNull null
val modules = home.modules.mapNotNull { module ->
status.find { it.id == module.id }
?.let {
when (module) {
is ValveModule -> module.join(it as ValveModule)
is RelayModule -> module.join(it as RelayModule)
is ThermostatModule -> module.join(it as ThermostatModule)
else -> module
}
}
home.copy(modules = modules)
}
}
origin.copy(homes = home)
}
home.copy(modules = modules)
}?.let {
origin.copy(homes = it)
} ?: origin
}
return PlainFunctionExecutable(func)
}
Expand All @@ -383,10 +408,10 @@ class EnergyConnector(api: Retrofit) : CommonConnector(api) {
@Suppress("UNCHECKED_CAST")
fun <T : EnergyModule<T>> getModuleStatus(homeId: String, module: T): T? {
getHomeStatus(homeId = homeId, deviceType = module.type).executeSync()
?.home
?.homes
?.forEach { home ->
home.modules
?.find { it.id == module.id }
.find { it.id == module.id }
?.let { module ->
return module as? T
}
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/io/rudolph/netatmo/api/energy/model/BaseRoom.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.rudolph.netatmo.api.energy.model

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty


/**
* BaseRoom status definition
*/
@JsonIgnoreProperties(ignoreUnknown = true)
data class BaseRoom(
/**
* Id of the room
*/
@JsonProperty("id")
val id: String? = null,

@JsonProperty("name")
val name: String? = null,

@JsonProperty("type")
val type: String? = null,

@JsonProperty("module_ids")
val moduleIds: List<String>? = null,

@JsonProperty("measure_offset_NAPlug_temperature")
val measureOffsetNAPlugTemperature: Int? = null,

@JsonProperty("measure_offset_NAPlug_estimated_temperature")
val measureOffsetNAPlugEstimatedTemperature: Int? = null
)
4 changes: 3 additions & 1 deletion src/main/java/io/rudolph/netatmo/api/energy/model/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ data class Home(
val thermSetpointDefaultDuration: Long? = null,

@JsonProperty("rooms")
val rooms: List<Room> = listOf(),
val rooms: List<BaseRoom> = listOf(),

@JsonProperty("modules")
val modules: List<Module> = listOf(),
Expand All @@ -47,5 +47,7 @@ data class Home(
rooms.find {
it.moduleIds?.contains(module.id) ?: false
}

fun getActiveSchedule() = schedules.find { it.isSelected == true }
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class HomeStatusBody(
@JsonProperty("home")
val home: MutableList<HomestatusBodyHome>? = null
val homes: List<HomestatusBodyHome> = listOf()
)


This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import io.rudolph.netatmo.api.common.model.Module

data class HomestatusBodyHome(
@JsonProperty("modules")
val modules: List<Module>? = null,
val modules: List<Module> = listOf(),

@JsonProperty("rooms")
val rooms: List<HomeStatusRoom>? = null,
val rooms: List<Room> = listOf(),

@JsonProperty("id")
val id: String? = null
Expand Down
67 changes: 51 additions & 16 deletions src/main/java/io/rudolph/netatmo/api/energy/model/Room.kt
Original file line number Diff line number Diff line change
@@ -1,32 +1,67 @@
package io.rudolph.netatmo.api.energy.model

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import java.time.LocalDateTime


/**
* Room status definition
*/
@JsonIgnoreProperties(ignoreUnknown = true)
data class Room(

/**
* Id of the room
*/
@JsonProperty("id")
val id: String? = null,
val id: String,

@JsonProperty("name")
val name: String? = null,
/**
* False if none of the module of the room are reachable
*/
@JsonProperty("reachable")
val isReachable: Boolean? = null,

/**
* Ambient temperature
*/
@JsonProperty("therm_measured_temperature")
val thermMeasuredTemperature: Float? = null,

@JsonProperty("type")
val type: String? = null,
/**
* Only if room has valves
*/
@JsonProperty("heating_power_request")
val heatingPowerRequest: Float? = null,

@JsonProperty("module_ids")
val moduleIds: List<String>? = null,
/**
* setpoint temperature
*/
@JsonProperty("therm_setpoint_temperature")
val thermSetpointTemperature: Float? = null,

/**
* manual
* max
* off
* schedule
* away
* hg
*/
@JsonProperty("therm_setpoint_mode")
val thermSetpointMode: ThermMode? = null,

/**
* Start time for a manual setpoint
*/
@JsonProperty("therm_setpoint_start_time")
val thermSetpointStartTime: LocalDateTime? = null,

/**
* End time for a manual setpoint
*/
@JsonProperty("therm_setpoint_end_time")
val thermSetpointEndTime: LocalDateTime? = null,

@JsonProperty("measure_offset_NAPlug_temperature")
val measureOffsetNAPlugTemperature: Int? = null,
@JsonProperty("anticipating")
val anticipating: Boolean = false,

@JsonProperty("measure_offset_NAPlug_estimated_temperature")
val measureOffsetNAPlugEstimatedTemperature: Int? = null
@JsonProperty("open_window")
val openWindow: Boolean = false
)
Loading

0 comments on commit 5b5d591

Please sign in to comment.