fix tests, removed Trigger reflection

This commit is contained in:
Milos Kozak 2021-04-10 14:36:55 +02:00
parent e4d70a7303
commit d898a374e6
40 changed files with 135 additions and 185 deletions

View file

@ -53,7 +53,7 @@ import java.util.*
@PrepareForTest( @PrepareForTest(
ConfigBuilderPlugin::class, ConstraintChecker::class, SP::class, Context::class, ConfigBuilderPlugin::class, ConstraintChecker::class, SP::class, Context::class,
OpenAPSAMAPlugin::class, OpenAPSSMBPlugin::class, TreatmentsPlugin::class, TreatmentService::class, OpenAPSAMAPlugin::class, OpenAPSSMBPlugin::class, TreatmentsPlugin::class, TreatmentService::class,
VirtualPumpPlugin::class, DetailedBolusInfoStorage::class, GlimpPlugin::class, Profiler::class, VirtualPumpPlugin::class, DetailedBolusInfoStorage::class, TemporaryBasalStorage::class, GlimpPlugin::class, Profiler::class,
UserEntryLogger::class, IobCobCalculatorPlugin::class, LoggerUtils::class, AppRepository::class) UserEntryLogger::class, IobCobCalculatorPlugin::class, LoggerUtils::class, AppRepository::class)
class ConstraintsCheckerTest : TestBaseWithProfile() { class ConstraintsCheckerTest : TestBaseWithProfile() {

View file

@ -139,17 +139,17 @@ class SmsCommunicatorPluginTest : TestBaseWithProfile() {
null null
}.`when`(commandQueue).bolus(anyObject(), ArgumentMatchers.any(Callback::class.java)) }.`when`(commandQueue).bolus(anyObject(), ArgumentMatchers.any(Callback::class.java))
Mockito.doAnswer { invocation: InvocationOnMock -> Mockito.doAnswer { invocation: InvocationOnMock ->
val callback = invocation.getArgument<Callback>(4) val callback = invocation.getArgument<Callback>(5)
callback.result = PumpEnactResult(injector).success(true).isPercent(true).percent(invocation.getArgument(0)).duration(invocation.getArgument(1)) callback.result = PumpEnactResult(injector).success(true).isPercent(true).percent(invocation.getArgument(0)).duration(invocation.getArgument(1))
callback.run() callback.run()
null null
}.`when`(commandQueue).tempBasalPercent(ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyBoolean(), anyObject(), ArgumentMatchers.any(PumpSync.TemporaryBasalType::class.java), ArgumentMatchers.any(Callback::class.java)) }.`when`(commandQueue).tempBasalPercent(ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyBoolean(), anyObject(), anyObject(), ArgumentMatchers.any(Callback::class.java))
Mockito.doAnswer { invocation: InvocationOnMock -> Mockito.doAnswer { invocation: InvocationOnMock ->
val callback = invocation.getArgument<Callback>(4) val callback = invocation.getArgument<Callback>(5)
callback.result = PumpEnactResult(injector).success(true).isPercent(false).absolute(invocation.getArgument(0)).duration(invocation.getArgument(1)) callback.result = PumpEnactResult(injector).success(true).isPercent(false).absolute(invocation.getArgument(0)).duration(invocation.getArgument(1))
callback.run() callback.run()
null null
}.`when`(commandQueue).tempBasalAbsolute(ArgumentMatchers.anyDouble(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyBoolean(), anyObject(), ArgumentMatchers.any(PumpSync.TemporaryBasalType::class.java), ArgumentMatchers.any(Callback::class.java)) }.`when`(commandQueue).tempBasalAbsolute(ArgumentMatchers.anyDouble(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyBoolean(), anyObject(), anyObject(), ArgumentMatchers.any(Callback::class.java))
Mockito.doAnswer { invocation: InvocationOnMock -> Mockito.doAnswer { invocation: InvocationOnMock ->
val callback = invocation.getArgument<Callback>(2) val callback = invocation.getArgument<Callback>(2)
callback.result = PumpEnactResult(injector).success(true).isPercent(false).absolute(invocation.getArgument(0)).duration(invocation.getArgument(1)) callback.result = PumpEnactResult(injector).success(true).isPercent(false).absolute(invocation.getArgument(0)).duration(invocation.getArgument(1))

View file

@ -121,7 +121,7 @@ class IobCobCalculatorPluginTest : TestBase() {
} }
@Test @Test
fun createBucketedData5minTest() { fun createBucketedData5minTest1() {
val bgReadingList: MutableList<GlucoseValue> = ArrayList() val bgReadingList: MutableList<GlucoseValue> = ArrayList()
// Super data should not be touched // Super data should not be touched
@ -205,6 +205,11 @@ class IobCobCalculatorPluginTest : TestBase() {
Assert.assertEquals(90.0, iobCobCalculatorPlugin.bucketedData!![1].value, 1.0) Assert.assertEquals(90.0, iobCobCalculatorPlugin.bucketedData!![1].value, 1.0)
Assert.assertEquals(50.0, iobCobCalculatorPlugin.bucketedData!![5].value, 1.0) Assert.assertEquals(50.0, iobCobCalculatorPlugin.bucketedData!![5].value, 1.0)
Assert.assertEquals(40.0, iobCobCalculatorPlugin.bucketedData!![6].value, 1.0) Assert.assertEquals(40.0, iobCobCalculatorPlugin.bucketedData!![6].value, 1.0)
}
@Test
fun createBucketedData5minTest2() {
val bgReadingList: MutableList<GlucoseValue> = ArrayList()
//bucketed data should be null if no bg data available //bucketed data should be null if no bg data available
iobCobCalculatorPlugin.bgReadings = ArrayList() iobCobCalculatorPlugin.bgReadings = ArrayList()

View file

@ -48,6 +48,8 @@ class TreatmentsPluginTest : TestBaseWithProfile() {
} }
} }
} }
@Test fun dumy() {}
/* /*
private lateinit var insulinOrefRapidActingPlugin: InsulinOrefRapidActingPlugin private lateinit var insulinOrefRapidActingPlugin: InsulinOrefRapidActingPlugin
private lateinit var sot: TreatmentsPlugin private lateinit var sot: TreatmentsPlugin

View file

@ -14,7 +14,6 @@ import info.nightscout.androidaps.database.AppRepository
import info.nightscout.androidaps.interfaces.ActivePluginProvider import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.interfaces.IobCobCalculator import info.nightscout.androidaps.interfaces.IobCobCalculator
import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.interfaces.TreatmentsInterface
import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.bus.RxBusWrapper
import info.nightscout.androidaps.plugins.general.automation.dialogs.ChooseTriggerDialog import info.nightscout.androidaps.plugins.general.automation.dialogs.ChooseTriggerDialog
@ -26,10 +25,8 @@ import info.nightscout.androidaps.services.LastLocationDataContainer
import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.resources.ResourceHelper
import info.nightscout.androidaps.utils.sharedPreferences.SP import info.nightscout.androidaps.utils.sharedPreferences.SP
import org.json.JSONException
import org.json.JSONObject import org.json.JSONObject
import javax.inject.Inject import javax.inject.Inject
import kotlin.reflect.full.primaryConstructor
abstract class Trigger(val injector: HasAndroidInjector) { abstract class Trigger(val injector: HasAndroidInjector) {
@ -50,7 +47,7 @@ abstract class Trigger(val injector: HasAndroidInjector) {
} }
abstract fun shouldRun(): Boolean abstract fun shouldRun(): Boolean
abstract fun toJSON(): String abstract fun dataJSON(): JSONObject
abstract fun fromJSON(data: String): Trigger abstract fun fromJSON(data: String): Trigger
abstract fun friendlyName(): Int abstract fun friendlyName(): Int
@ -77,23 +74,56 @@ abstract class Trigger(val injector: HasAndroidInjector) {
root.addView(title) root.addView(title)
} }
fun toJSON(): String =
JSONObject()
.put("type", this::class.java.simpleName)
.put("data", dataJSON())
.toString()
fun instantiate(obj: JSONObject): Trigger? { fun instantiate(obj: JSONObject): Trigger? {
try {
val type = obj.getString("type") val type = obj.getString("type")
val data = obj.getJSONObject("data") val data = obj.getJSONObject("data")
val clazz = Class.forName(type).kotlin //val clazz = Class.forName(type).kotlin
return (clazz.primaryConstructor?.call(injector) as Trigger).fromJSON(data?.toString() //return (clazz.primaryConstructor?.call(injector) as Trigger).fromJSON(data?.toString() ?: "")
?: "") return when (type) {
} catch (e: ClassNotFoundException) { TriggerAutosensValue::class.java.name, // backward compatibility
aapsLogger.error("Unhandled exception", e) TriggerAutosensValue::class.java.simpleName -> TriggerAutosensValue(injector).fromJSON(data?.toString() ?: "")
} catch (e: InstantiationException) { TriggerBg::class.java.name,
aapsLogger.error("Unhandled exception", e) TriggerBg::class.java.simpleName -> TriggerBg(injector).fromJSON(data?.toString() ?: "")
} catch (e: IllegalAccessException) { TriggerBolusAgo::class.java.name,
aapsLogger.error("Unhandled exception", e) TriggerBolusAgo::class.java.simpleName -> TriggerBolusAgo(injector).fromJSON(data?.toString() ?: "")
} catch (e: JSONException) { TriggerBTDevice::class.java.name,
aapsLogger.error("Unhandled exception", e) TriggerBTDevice::class.java.simpleName -> TriggerBTDevice(injector).fromJSON(data?.toString() ?: "")
TriggerIob::class.java.name,
TriggerIob::class.java.simpleName -> TriggerIob(injector).fromJSON(data?.toString() ?: "")
TriggerCOB::class.java.name,
TriggerCOB::class.java.simpleName -> TriggerCOB(injector).fromJSON(data?.toString() ?: "")
TriggerConnector::class.java.name,
TriggerConnector::class.java.simpleName -> TriggerConnector(injector).fromJSON(data?.toString() ?: "")
TriggerDelta::class.java.name,
TriggerDelta::class.java.simpleName -> TriggerDelta(injector).fromJSON(data?.toString() ?: "")
TriggerDummy::class.java.name,
TriggerDummy::class.java.simpleName -> TriggerDummy(injector).fromJSON(data?.toString() ?: "")
TriggerIob::class.java.name,
TriggerIob::class.java.simpleName -> TriggerIob(injector).fromJSON(data?.toString() ?: "")
TriggerLocation::class.java.name,
TriggerLocation::class.java.simpleName -> TriggerLocation(injector).fromJSON(data?.toString() ?: "")
TriggerProfilePercent::class.java.name,
TriggerProfilePercent::class.java.simpleName -> TriggerProfilePercent(injector).fromJSON(data?.toString() ?: "")
TriggerPumpLastConnection::class.java.name,
TriggerPumpLastConnection::class.java.simpleName -> TriggerPumpLastConnection(injector).fromJSON(data?.toString() ?: "")
TriggerRecurringTime::class.java.name,
TriggerRecurringTime::class.java.simpleName -> TriggerRecurringTime(injector).fromJSON(data?.toString() ?: "")
TriggerTempTarget::class.java.name,
TriggerTempTarget::class.java.simpleName -> TriggerTempTarget(injector).fromJSON(data?.toString() ?: "")
TriggerTime::class.java.name,
TriggerTime::class.java.simpleName -> TriggerTime(injector).fromJSON(data?.toString() ?: "")
TriggerTimeRange::class.java.name,
TriggerTimeRange::class.java.simpleName -> TriggerTimeRange(injector).fromJSON(data?.toString() ?: "")
TriggerWifiSsid::class.java.name,
TriggerWifiSsid::class.java.simpleName -> TriggerWifiSsid(injector).fromJSON(data?.toString() ?: "")
else -> throw ClassNotFoundException(type)
} }
return null
} }
fun createAddButton(context: Context, trigger: TriggerConnector): ImageButton { fun createAddButton(context: Context, trigger: TriggerConnector): ImageButton {

View file

@ -46,15 +46,10 @@ class TriggerAutosensValue(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("value", autosens.value) .put("value", autosens.value)
.put("comparator", comparator.value.toString()) .put("comparator", comparator.value.toString())
return JSONObject()
.put("type", this::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val d = JSONObject(data) val d = JSONObject(data)

View file

@ -39,15 +39,10 @@ class TriggerBTDevice(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
@Synchronized override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("comparator", comparator.value.toString()) .put("comparator", comparator.value.toString())
.put("name", btDevice.value) .put("name", btDevice.value)
return JSONObject()
.put("type", this::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val d = JSONObject(data) val d = JSONObject(data)

View file

@ -63,16 +63,11 @@ class TriggerBg(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("bg", bg.value) .put("bg", bg.value)
.put("comparator", comparator.value.toString()) .put("comparator", comparator.value.toString())
.put("units", bg.units) .put("units", bg.units)
return JSONObject()
.put("type", this::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val d = JSONObject(data) val d = JSONObject(data)

View file

@ -56,15 +56,10 @@ class TriggerBolusAgo(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("minutesAgo", minutesAgo.value) .put("minutesAgo", minutesAgo.value)
.put("comparator", comparator.value.toString()) .put("comparator", comparator.value.toString())
return JSONObject()
.put("type", this::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val d = JSONObject(data) val d = JSONObject(data)

View file

@ -16,6 +16,7 @@ import org.json.JSONObject
import java.text.DecimalFormat import java.text.DecimalFormat
class TriggerCOB(injector: HasAndroidInjector) : Trigger(injector) { class TriggerCOB(injector: HasAndroidInjector) : Trigger(injector) {
private val minValue = 0 private val minValue = 0
private val maxValue = sp.getInt(R.string.key_treatmentssafety_maxcarbs, 48) private val maxValue = sp.getInt(R.string.key_treatmentssafety_maxcarbs, 48)
var cob: InputDouble = InputDouble(0.0, minValue.toDouble(), maxValue.toDouble(), 1.0, DecimalFormat("1")) var cob: InputDouble = InputDouble(0.0, minValue.toDouble(), maxValue.toDouble(), 1.0, DecimalFormat("1"))
@ -55,15 +56,10 @@ class TriggerCOB(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
@Synchronized override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("carbs", cob.value) .put("carbs", cob.value)
.put("comparator", comparator.value.toString()) .put("comparator", comparator.value.toString())
return JSONObject()
.put("type", this::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val d = JSONObject(data) val d = JSONObject(data)

View file

@ -19,6 +19,7 @@ import org.json.JSONObject
import java.util.* import java.util.*
class TriggerConnector(injector: HasAndroidInjector) : Trigger(injector) { class TriggerConnector(injector: HasAndroidInjector) : Trigger(injector) {
var list: MutableList<Trigger> = ArrayList() var list: MutableList<Trigger> = ArrayList()
private var connectorType: Type = Type.AND private var connectorType: Type = Type.AND
@ -40,6 +41,7 @@ class TriggerConnector(injector: HasAndroidInjector) : Trigger(injector) {
} }
companion object { companion object {
fun labels(resourceHelper: ResourceHelper): List<String> { fun labels(resourceHelper: ResourceHelper): List<String> {
val list: MutableList<String> = ArrayList() val list: MutableList<String> = ArrayList()
for (t in values()) { for (t in values()) {
@ -79,16 +81,12 @@ class TriggerConnector(injector: HasAndroidInjector) : Trigger(injector) {
return result return result
} }
@Synchronized override fun toJSON(): String { override fun dataJSON(): JSONObject {
val array = JSONArray() val array = JSONArray()
for (t in list) array.put(t.toJSON()) for (t in list) array.put(t.toJSON())
val data = JSONObject() return JSONObject()
.put("connectorType", connectorType.toString()) .put("connectorType", connectorType.toString())
.put("triggerList", array) .put("triggerList", array)
return JSONObject()
.put("type", TriggerConnector::class.java.name)
.put("data", data)
.toString()
} }
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {

View file

@ -13,7 +13,6 @@ import info.nightscout.androidaps.plugins.general.automation.elements.InputDelta
import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement import info.nightscout.androidaps.plugins.general.automation.elements.LabelWithElement
import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder import info.nightscout.androidaps.plugins.general.automation.elements.LayoutBuilder
import info.nightscout.androidaps.plugins.general.automation.elements.StaticLabel import info.nightscout.androidaps.plugins.general.automation.elements.StaticLabel
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.GlucoseStatus
import info.nightscout.androidaps.utils.JsonHelper import info.nightscout.androidaps.utils.JsonHelper
import org.json.JSONObject import org.json.JSONObject
import java.text.DecimalFormat import java.text.DecimalFormat
@ -25,6 +24,7 @@ class TriggerDelta(injector: HasAndroidInjector) : Trigger(injector) {
var comparator: Comparator = Comparator(resourceHelper) var comparator: Comparator = Comparator(resourceHelper)
companion object { companion object {
private const val MMOL_MAX = 4.0 private const val MMOL_MAX = 4.0
private const val MGDL_MAX = 72.0 private const val MGDL_MAX = 72.0
} }
@ -85,17 +85,12 @@ class TriggerDelta(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("value", delta.value) .put("value", delta.value)
.put("units", units) .put("units", units)
.put("deltaType", delta.deltaType) .put("deltaType", delta.deltaType)
.put("comparator", comparator.value.toString()) .put("comparator", comparator.value.toString())
return JSONObject()
.put("type", this::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val d = JSONObject(data) val d = JSONObject(data)

View file

@ -2,6 +2,7 @@ package info.nightscout.androidaps.plugins.general.automation.triggers
import com.google.common.base.Optional import com.google.common.base.Optional
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import org.json.JSONObject
// Used for instantiation of other triggers only // Used for instantiation of other triggers only
class TriggerDummy(injector: HasAndroidInjector, val shouldRun: Boolean = false) : Trigger(injector) { class TriggerDummy(injector: HasAndroidInjector, val shouldRun: Boolean = false) : Trigger(injector) {
@ -10,7 +11,7 @@ class TriggerDummy(injector: HasAndroidInjector, val shouldRun: Boolean = false)
return shouldRun return shouldRun
} }
override fun toJSON(): String { override fun dataJSON(): JSONObject {
throw NotImplementedError("An operation is not implemented") throw NotImplementedError("An operation is not implemented")
} }

View file

@ -44,15 +44,10 @@ class TriggerIob(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
@Synchronized override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("insulin", insulin.value) .put("insulin", insulin.value)
.put("comparator", comparator.value.toString()) .put("comparator", comparator.value.toString())
return JSONObject()
.put("type", this::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val d = JSONObject(data) val d = JSONObject(data)

View file

@ -12,6 +12,7 @@ import org.json.JSONObject
import java.text.DecimalFormat import java.text.DecimalFormat
class TriggerLocation(injector: HasAndroidInjector) : Trigger(injector) { class TriggerLocation(injector: HasAndroidInjector) : Trigger(injector) {
var latitude = InputDouble(0.0, -90.0, +90.0, 0.000001, DecimalFormat("0.000000")) var latitude = InputDouble(0.0, -90.0, +90.0, 0.000001, DecimalFormat("0.000000"))
var longitude = InputDouble(0.0, -180.0, +180.0, 0.000001, DecimalFormat("0.000000")) var longitude = InputDouble(0.0, -180.0, +180.0, 0.000001, DecimalFormat("0.000000"))
var distance = InputDouble(200.0, 0.0, 100000.0, 10.0, DecimalFormat("0")) var distance = InputDouble(200.0, 0.0, 100000.0, 10.0, DecimalFormat("0"))
@ -56,18 +57,13 @@ class TriggerLocation(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("latitude", latitude.value) .put("latitude", latitude.value)
.put("longitude", longitude.value) .put("longitude", longitude.value)
.put("distance", distance.value) .put("distance", distance.value)
.put("name", name.value) .put("name", name.value)
.put("mode", modeSelected.value) .put("mode", modeSelected.value)
return JSONObject()
.put("type", this::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val d = JSONObject(data) val d = JSONObject(data)

View file

@ -14,6 +14,7 @@ import info.nightscout.androidaps.utils.JsonHelper
import org.json.JSONObject import org.json.JSONObject
class TriggerProfilePercent(injector: HasAndroidInjector) : Trigger(injector) { class TriggerProfilePercent(injector: HasAndroidInjector) : Trigger(injector) {
var pct = InputPercent() var pct = InputPercent()
var comparator = Comparator(resourceHelper) var comparator = Comparator(resourceHelper)
@ -55,15 +56,10 @@ class TriggerProfilePercent(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
@Synchronized override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("percentage", pct.value) .put("percentage", pct.value)
.put("comparator", comparator.value.toString()) .put("comparator", comparator.value.toString())
return JSONObject()
.put("type", this::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val d = JSONObject(data) val d = JSONObject(data)

View file

@ -16,6 +16,7 @@ import info.nightscout.androidaps.utils.JsonHelper.safeGetString
import org.json.JSONObject import org.json.JSONObject
class TriggerPumpLastConnection(injector: HasAndroidInjector) : Trigger(injector) { class TriggerPumpLastConnection(injector: HasAndroidInjector) : Trigger(injector) {
var minutesAgo = InputDuration() var minutesAgo = InputDuration()
var comparator = Comparator(resourceHelper) var comparator = Comparator(resourceHelper)
@ -56,15 +57,10 @@ class TriggerPumpLastConnection(injector: HasAndroidInjector) : Trigger(injector
return false return false
} }
override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("minutesAgo", minutesAgo.value) .put("minutesAgo", minutesAgo.value)
.put("comparator", comparator.value.toString()) .put("comparator", comparator.value.toString())
return JSONObject()
.put("type", this::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val d = JSONObject(data) val d = JSONObject(data)

View file

@ -44,16 +44,13 @@ class TriggerRecurringTime(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
override fun toJSON(): String { override fun dataJSON(): JSONObject {
val data = JSONObject() val data = JSONObject()
.put("time", time.value) .put("time", time.value)
for (i in days.weekdays.indices) { for (i in days.weekdays.indices) {
data.put(InputWeekDay.DayOfWeek.values()[i].name, days.weekdays[i]) data.put(InputWeekDay.DayOfWeek.values()[i].name, days.weekdays[i])
} }
return JSONObject() return data
.put("type", TriggerRecurringTime::class.java.name)
.put("data", data)
.toString()
} }
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {

View file

@ -43,14 +43,9 @@ class TriggerTempTarget(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("comparator", comparator.value.toString()) .put("comparator", comparator.value.toString())
return JSONObject()
.put("type", TriggerTempTarget::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val d = JSONObject(data) val d = JSONObject(data)

View file

@ -41,14 +41,9 @@ class TriggerTime(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("runAt", time.value) .put("runAt", time.value)
return JSONObject()
.put("type", this::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val o = JSONObject(data) val o = JSONObject(data)

View file

@ -50,15 +50,10 @@ class TriggerTimeRange(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("start", range.start) .put("start", range.start)
.put("end", range.end) .put("end", range.end)
return JSONObject()
.put("type", this::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): TriggerTimeRange { override fun fromJSON(data: String): TriggerTimeRange {
val o = JSONObject(data) val o = JSONObject(data)

View file

@ -16,12 +16,14 @@ import org.json.JSONObject
import javax.inject.Inject import javax.inject.Inject
class TriggerWifiSsid(injector: HasAndroidInjector) : Trigger(injector) { class TriggerWifiSsid(injector: HasAndroidInjector) : Trigger(injector) {
@Inject lateinit var receiverStatusStore: ReceiverStatusStore @Inject lateinit var receiverStatusStore: ReceiverStatusStore
var ssid = InputString() var ssid = InputString()
var comparator = Comparator(resourceHelper) var comparator = Comparator(resourceHelper)
@Suppress("unused") constructor(injector: HasAndroidInjector, ssid: String, compare: Comparator.Compare) : this(injector) { @Suppress("unused")
constructor(injector: HasAndroidInjector, ssid: String, compare: Comparator.Compare) : this(injector) {
this.ssid = InputString(ssid) this.ssid = InputString(ssid)
comparator = Comparator(resourceHelper, compare) comparator = Comparator(resourceHelper, compare)
} }
@ -55,15 +57,10 @@ class TriggerWifiSsid(injector: HasAndroidInjector) : Trigger(injector) {
return false return false
} }
override fun toJSON(): String { override fun dataJSON(): JSONObject =
val data = JSONObject() JSONObject()
.put("ssid", ssid.value) .put("ssid", ssid.value)
.put("comparator", comparator.value.toString()) .put("comparator", comparator.value.toString())
return JSONObject()
.put("type", this::class.java.name)
.put("data", data)
.toString()
}
override fun fromJSON(data: String): Trigger { override fun fromJSON(data: String): Trigger {
val d = JSONObject(data) val d = JSONObject(data)

View file

@ -48,7 +48,7 @@ class AutomationEventTest : TestBase() {
event.addAction(ActionLoopEnable(injector)) event.addAction(ActionLoopEnable(injector))
// export to json // export to json
val eventJsonExpected = "{\"autoRemove\":false,\"readOnly\":false,\"trigger\":\"{\\\"data\\\":{\\\"connectorType\\\":\\\"AND\\\",\\\"triggerList\\\":[\\\"{\\\\\\\"data\\\\\\\":{\\\\\\\"connectorType\\\\\\\":\\\\\\\"AND\\\\\\\",\\\\\\\"triggerList\\\\\\\":[]},\\\\\\\"type\\\\\\\":\\\\\\\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector\\\\\\\"}\\\"]},\\\"type\\\":\\\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector\\\"}\",\"title\":\"Test\",\"systemAction\":false,\"actions\":[\"{\\\"type\\\":\\\"info.nightscout.androidaps.plugins.general.automation.actions.ActionLoopEnable\\\"}\"],\"enabled\":true}" val eventJsonExpected = "{\"autoRemove\":false,\"readOnly\":false,\"trigger\":\"{\\\"data\\\":{\\\"connectorType\\\":\\\"AND\\\",\\\"triggerList\\\":[\\\"{\\\\\\\"data\\\\\\\":{\\\\\\\"connectorType\\\\\\\":\\\\\\\"AND\\\\\\\",\\\\\\\"triggerList\\\\\\\":[]},\\\\\\\"type\\\\\\\":\\\\\\\"TriggerConnector\\\\\\\"}\\\"]},\\\"type\\\":\\\"TriggerConnector\\\"}\",\"title\":\"Test\",\"systemAction\":false,\"actions\":[\"{\\\"type\\\":\\\"info.nightscout.androidaps.plugins.general.automation.actions.ActionLoopEnable\\\"}\"],\"enabled\":true}"
Assert.assertEquals(eventJsonExpected, event.toJSON()) Assert.assertEquals(eventJsonExpected, event.toJSON())
// clone // clone

View file

@ -3,34 +3,15 @@ package info.nightscout.androidaps.plugins.general.automation.triggers
import com.google.common.base.Optional import com.google.common.base.Optional
import dagger.android.AndroidInjector import dagger.android.AndroidInjector
import dagger.android.HasAndroidInjector import dagger.android.HasAndroidInjector
import org.json.JSONObject
class DummyTrigger(var result: Boolean) : Trigger(HasAndroidInjector { AndroidInjector { } }) { class DummyTrigger(var result: Boolean) : Trigger(HasAndroidInjector { AndroidInjector { } }) {
override fun shouldRun(): Boolean { override fun shouldRun(): Boolean = result
return result override fun dataJSON(): JSONObject = JSONObject()
} override fun fromJSON(data: String): Trigger = DummyTrigger(result)
override fun friendlyName(): Int = 0
override fun toJSON(): String { override fun friendlyDescription(): String = " "
return "" override fun icon(): Optional<Int?> = Optional.absent()
} override fun duplicate(): Trigger = DummyTrigger(result)
override fun fromJSON(data: String): Trigger {
return DummyTrigger(result)
}
override fun friendlyName(): Int {
return 0
}
override fun friendlyDescription(): String {
return " "
}
override fun icon(): Optional<Int?> {
return Optional.absent()
}
override fun duplicate(): Trigger {
return DummyTrigger(result)
}
} }

View file

@ -88,7 +88,7 @@ class TriggerAutosensValueTest : TriggerTestBase() {
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value)
} }
private var asJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"value\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerAutosensValue\"}" private var asJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"value\":410},\"type\":\"TriggerAutosensValue\"}"
@Test @Test
fun toJSONTest() { fun toJSONTest() {

View file

@ -15,7 +15,7 @@ class TriggerBTDeviceTest : TriggerTestBase() {
var now = 1514766900000L var now = 1514766900000L
private var someName = "Headset" private var someName = "Headset"
private var btJson = "{\"data\":{\"comparator\":\"ON_CONNECT\",\"name\":\"Headset\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerBTDevice\"}" private var btJson = "{\"data\":{\"comparator\":\"ON_CONNECT\",\"name\":\"Headset\"},\"type\":\"TriggerBTDevice\"}"
@Test fun shouldRun() { @Test fun shouldRun() {
@Suppress("UNUSED_VARIABLE") @Suppress("UNUSED_VARIABLE")

View file

@ -69,7 +69,7 @@ class TriggerBgTest : TriggerTestBase() {
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value)
} }
private var bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"bg\":4.1,\"units\":\"mmol\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerBg\"}" private var bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"bg\":4.1,\"units\":\"mmol\"},\"type\":\"TriggerBg\"}"
@Test @Test
fun toJSONTest() { fun toJSONTest() {

View file

@ -77,7 +77,7 @@ class TriggerBolusAgoTest : TriggerTestBase() {
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value)
} }
private var lbJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"minutesAgo\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerBolusAgo\"}" private var lbJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"minutesAgo\":410},\"type\":\"TriggerBolusAgo\"}"
@Test fun toJSONTest() { @Test fun toJSONTest() {
val t: TriggerBolusAgo = TriggerBolusAgo(injector).setValue(410).comparator(Comparator.Compare.IS_EQUAL) val t: TriggerBolusAgo = TriggerBolusAgo(injector).setValue(410).comparator(Comparator.Compare.IS_EQUAL)
Assert.assertEquals(lbJson, t.toJSON()) Assert.assertEquals(lbJson, t.toJSON())

View file

@ -54,7 +54,7 @@ class TriggerCOBTest : TriggerTestBase() {
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value)
} }
private var bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"carbs\":4},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerCOB\"}" private var bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"carbs\":4},\"type\":\"TriggerCOB\"}"
@Test fun toJSONTest() { @Test fun toJSONTest() {
val t: TriggerCOB = TriggerCOB(injector).setValue(4.0).comparator(Comparator.Compare.IS_EQUAL) val t: TriggerCOB = TriggerCOB(injector).setValue(4.0).comparator(Comparator.Compare.IS_EQUAL)
Assert.assertEquals(bgJson, t.toJSON()) Assert.assertEquals(bgJson, t.toJSON())

View file

@ -77,7 +77,7 @@ class TriggerConnectorTest : TriggerTestBase() {
} }
companion object { companion object {
const val empty = "{\"data\":{\"connectorType\":\"AND\",\"triggerList\":[]},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector\"}" const val empty = "{\"data\":{\"connectorType\":\"AND\",\"triggerList\":[]},\"type\":\"TriggerConnector\"}"
const val oneItem = "{\"data\":{\"connectorType\":\"AND\",\"triggerList\":[\"{\\\"data\\\":{\\\"connectorType\\\":\\\"AND\\\",\\\"triggerList\\\":[]},\\\"type\\\":\\\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector\\\"}\"]},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerConnector\"}" const val oneItem = "{\"data\":{\"connectorType\":\"AND\",\"triggerList\":[\"{\\\"data\\\":{\\\"connectorType\\\":\\\"AND\\\",\\\"triggerList\\\":[]},\\\"type\\\":\\\"TriggerConnector\\\"}\"]},\"type\":\"TriggerConnector\"}"
} }
} }

View file

@ -72,7 +72,7 @@ class TriggerDeltaTest : TriggerTestBase() {
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value)
} }
private var deltaJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"deltaType\":\"DELTA\",\"units\":\"mg/dl\",\"value\":4.1},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerDelta\"}" private var deltaJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"deltaType\":\"DELTA\",\"units\":\"mg/dl\",\"value\":4.1},\"type\":\"TriggerDelta\"}"
@Test @Test
fun toJSONTest() { fun toJSONTest() {

View file

@ -57,7 +57,7 @@ class TriggerIobTest : TriggerTestBase() {
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value)
} }
private var bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"insulin\":4.1},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerIob\"}" private var bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"insulin\":4.1},\"type\":\"TriggerIob\"}"
@Test fun toJSONTest() { @Test fun toJSONTest() {
val t: TriggerIob = TriggerIob(injector).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL) val t: TriggerIob = TriggerIob(injector).setValue(4.1).comparator(Comparator.Compare.IS_EQUAL)
Assert.assertEquals(bgJson, t.toJSON()) Assert.assertEquals(bgJson, t.toJSON())

View file

@ -73,7 +73,7 @@ class TriggerLocationTest : TriggerTestBase() {
// Currently unavailable due to problems with Location mocking // Currently unavailable due to problems with Location mocking
} }
private var locationJson = "{\"data\":{\"mode\":\"OUTSIDE\",\"distance\":2,\"latitude\":213,\"name\":\"\",\"longitude\":212},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerLocation\"}" private var locationJson = "{\"data\":{\"mode\":\"OUTSIDE\",\"distance\":2,\"latitude\":213,\"name\":\"\",\"longitude\":212},\"type\":\"TriggerLocation\"}"
@Test fun toJSONTest() { @Test fun toJSONTest() {
val t = TriggerLocation(injector) val t = TriggerLocation(injector)
t.latitude.setValue(213.0) t.latitude.setValue(213.0)

View file

@ -55,7 +55,7 @@ class TriggerProfilePercentTest : TriggerTestBase() {
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value)
} }
private val bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"percentage\":110},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerProfilePercent\"}" private val bgJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"percentage\":110},\"type\":\"TriggerProfilePercent\"}"
@Test fun toJSONTest() { @Test fun toJSONTest() {
val t: TriggerProfilePercent = TriggerProfilePercent(injector).setValue(110.0).comparator(Comparator.Compare.IS_EQUAL) val t: TriggerProfilePercent = TriggerProfilePercent(injector).setValue(110.0).comparator(Comparator.Compare.IS_EQUAL)
Assert.assertEquals(bgJson, t.toJSON()) Assert.assertEquals(bgJson, t.toJSON())

View file

@ -53,7 +53,7 @@ class TriggerPumpLastConnectionTest : TriggerTestBase() {
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value)
} }
private var lbJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"minutesAgo\":410},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerPumpLastConnection\"}" private var lbJson = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"minutesAgo\":410},\"type\":\"TriggerPumpLastConnection\"}"
@Test fun toJSONTest() { @Test fun toJSONTest() {
val t: TriggerPumpLastConnection = TriggerPumpLastConnection(injector).setValue(410).comparator(Comparator.Compare.IS_EQUAL) val t: TriggerPumpLastConnection = TriggerPumpLastConnection(injector).setValue(410).comparator(Comparator.Compare.IS_EQUAL)
Assert.assertEquals(lbJson, t.toJSON()) Assert.assertEquals(lbJson, t.toJSON())

View file

@ -35,7 +35,7 @@ class TriggerRecurringTimeTest : TriggerTestBase() {
Assert.assertTrue(t.shouldRun()) Assert.assertTrue(t.shouldRun())
} }
private var timeJson = "{\"data\":{\"WEDNESDAY\":false,\"MONDAY\":false,\"THURSDAY\":false,\"SUNDAY\":false,\"TUESDAY\":false,\"FRIDAY\":false,\"SATURDAY\":false,\"time\":4444},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerRecurringTime\"}" private var timeJson = "{\"data\":{\"WEDNESDAY\":false,\"MONDAY\":false,\"THURSDAY\":false,\"SUNDAY\":false,\"TUESDAY\":false,\"FRIDAY\":false,\"SATURDAY\":false,\"time\":4444},\"type\":\"TriggerRecurringTime\"}"
@Test @Test
fun toJSONTest() { fun toJSONTest() {

View file

@ -43,7 +43,7 @@ class TriggerTempTargetTest : TriggerTestBase() {
Assert.assertEquals(ComparatorExists.Compare.NOT_EXISTS, t1.comparator.value) Assert.assertEquals(ComparatorExists.Compare.NOT_EXISTS, t1.comparator.value)
} }
private var ttJson = "{\"data\":{\"comparator\":\"EXISTS\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTempTarget\"}" private var ttJson = "{\"data\":{\"comparator\":\"EXISTS\"},\"type\":\"TriggerTempTarget\"}"
@Test fun toJSONTest() { @Test fun toJSONTest() {
val t: TriggerTempTarget = TriggerTempTarget(injector).comparator(ComparatorExists.Compare.EXISTS) val t: TriggerTempTarget = TriggerTempTarget(injector).comparator(ComparatorExists.Compare.EXISTS)
Assert.assertEquals(ttJson, t.toJSON()) Assert.assertEquals(ttJson, t.toJSON())

View file

@ -18,7 +18,7 @@ import org.powermock.modules.junit4.PowerMockRunner
class TriggerTimeRangeTest : TriggerTestBase() { class TriggerTimeRangeTest : TriggerTestBase() {
var now = 754 // in minutes from midnight var now = 754 // in minutes from midnight
private var timeJson = "{\"data\":{\"start\":753,\"end\":784},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTimeRange\"}" private var timeJson = "{\"data\":{\"start\":753,\"end\":784},\"type\":\"TriggerTimeRange\"}"
@Before @Before
fun mock() { fun mock() {

View file

@ -36,7 +36,7 @@ class TriggerTimeTest : TriggerTestBase() {
Assert.assertFalse(t.shouldRun()) Assert.assertFalse(t.shouldRun())
} }
private var timeJson = "{\"data\":{\"runAt\":1514766840000},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTime\"}" private var timeJson = "{\"data\":{\"runAt\":1514766840000},\"type\":\"TriggerTime\"}"
@Test fun toJSONTest() { @Test fun toJSONTest() {
val t: TriggerTime = TriggerTime(injector).runAt(now - T.mins(1).msecs()) val t: TriggerTime = TriggerTime(injector).runAt(now - T.mins(1).msecs())
Assert.assertEquals(timeJson, t.toJSON()) Assert.assertEquals(timeJson, t.toJSON())

View file

@ -56,7 +56,7 @@ class TriggerWifiSsidTest : TriggerTestBase() {
Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value) Assert.assertEquals(Comparator.Compare.IS_EQUAL_OR_LESSER, t.comparator.value)
} }
var json = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"ssid\":\"aSSID\"},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerWifiSsid\"}" var json = "{\"data\":{\"comparator\":\"IS_EQUAL\",\"ssid\":\"aSSID\"},\"type\":\"TriggerWifiSsid\"}"
@Test fun toJSONTest() { @Test fun toJSONTest() {
val t: TriggerWifiSsid = TriggerWifiSsid(injector).setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL) val t: TriggerWifiSsid = TriggerWifiSsid(injector).setValue("aSSID").comparator(Comparator.Compare.IS_EQUAL)
Assert.assertEquals(json, t.toJSON()) Assert.assertEquals(json, t.toJSON())