100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Climatecontroll tests
Michal Rutka1d7986cfa0f1
diff --git a/agent/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovy b/agent/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovyindex 6d4b5f82b..d84393394 100644--- a/agent/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovy+++ b/agent/src/test/groovy/org/openremote/test/rules/ClimateControlTest.groovy@@ -6,17 +6,22 @@ import org.openremote.agent.AgentServiceimport org.openremote.agent.rules.RulesProviderimport org.openremote.test.ContainerTraitimport spock.lang.Specification+import spock.util.concurrent.BlockingVariable+import spock.util.concurrent.PollingConditions+import spock.lang.Ignoreimport java.util.stream.Streamclass ClimateControlTest extends Specification implements ContainerTrait {- def "Climate control basic test"() {+ @Ignore+ def "Climate control basic test template"() {given: "a deployment with commands and sensors"def deploymentXml = getClass().getResourceAsStream("/org/openremote/test/rules/climatecontrol/agent.xml")+ def conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)and: "some rules"def rulesProvider = new RulesProvider() {@@ -45,4 +50,135 @@ class ClimateControlTest extends Specification implements ContainerTrait {cleanup: "the server should be stopped"stopContainer(container)}++ def "Person sense test"() {++ given: "a deployment with commands and sensors"+ def deploymentXml = getClass().getResourceAsStream(+ "/org/openremote/test/rules/climatecontrol/agent.xml"+ )+ // def conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)+ def result = new BlockingVariable<String>()++ and: "some rules"+ def rulesProvider = new RulesProvider() {+ @Override+ Stream<Resource> getResources(KieServices kieServices) {+ Stream.of(+ kieServices.getResources().newClassPathResource(+ "org/openremote/test/rules/climatecontrol/ClimateControl.drl"+ )+ )+ }+ }++ and: "the started server"+ def testCommandBuilder = new TestCommandBuilder();+ def agentService = new AgentService(+ deploymentXml,+ testCommandBuilder,+ rulesProvider+ )+ def serverPort = findEphemeralPort()+ def container = startContainer(defaultConfig(serverPort), [agentService])++ // TODO Write tests++ cleanup: "the server should be stopped"+ stopContainer(container)+ }++ def "Estimate times initial values check"() {++ given: "a deployment with commands and sensors"+ def deploymentXml = getClass().getResourceAsStream(+ "/org/openremote/test/rules/climatecontrol/agent.xml"+ )+ def conditions = new PollingConditions(timeout: 60, initialDelay: 0.01)++ and: "some rules"+ def rulesProvider = new RulesProvider() {+ @Override+ Stream<Resource> getResources(KieServices kieServices) {+ Stream.of(+ kieServices.getResources().newClassPathResource(+ "org/openremote/test/rules/climatecontrol/ClimateControl.drl"+ )+ )+ }+ }++ and: "the started server"+ def testCommandBuilder = new TestCommandBuilder();+ def agentService = new AgentService(+ deploymentXml,+ testCommandBuilder,+ rulesProvider+ )+ def serverPort = findEphemeralPort()+ def container = startContainer(defaultConfig(serverPort), [agentService])++ when: "give some time for initialization rules"+// agentService.getContext().getCommands().execute("FS.Tinside.dec.ON")+ Thread.sleep(10)++ then: "check all defaults"+ conditions.eventually {+ assert agentService.getContext().queryValue("VR1.ET") == "--:--"+ assert agentService.getContext().queryValue("VR1.ET.inc") == "OFF"+ assert agentService.getContext().queryValue("VR1.ET.dec") == "OFF"+ assert agentService.getContext().queryValue("VETA.inc") == "OFF"+ assert agentService.getContext().queryValue("VETA.dec") == "OFF"+ assert agentService.getContext().queryValue("VETD.inc") == "OFF"+ assert agentService.getContext().queryValue("VETD.dec") == "OFF"+ assert agentService.getContext().queryValue("VR1.COMFORT") == "20.0°"+ assert agentService.getContext().queryValue("VR1.COMFORT.inc") == "OFF"+ assert agentService.getContext().queryValue("VR1.COMFORT.dec") == "OFF"+ assert agentService.getContext().queryValue("VR1.TEMPERATURE") == "19.5\u00B0"+ assert agentService.getContext().queryValue("VR1.TEMPERATURE.inc") == "OFF"+ assert agentService.getContext().queryValue("VR1.TEMPERATURE.dec") == "OFF"+ assert agentService.getContext().queryValue("VNEXTACTION") == "-"+ assert agentService.getContext().queryValue("VPERSONSENSETIME") == "-"+ assert agentService.getContext().queryValue("VLEAVES") == "1.0"+ assert agentService.getContext().queryValue("VSCORE") == "0"+ assert agentService.getContext().queryValue("VHEATINGSETPOINT") == "16"+ assert agentService.getContext().queryValue("VWINDOW") == "Closed"+ assert agentService.getContext().queryValue("VADVICEDONE") == "OFF"+ assert agentService.getContext().queryValue("VSUMMER") == "No"+ assert agentService.getContext().queryValue("VADVICE") == "You're doing great!"+ assert agentService.getContext().queryValue("VACATION.inc") == "OFF"+ assert agentService.getContext().queryValue("VACATION.dec") == "OFF"+ assert agentService.getContext().queryValue("VACATION") == "0"+ assert agentService.getContext().queryValue("VTOTALSCORE") == "0"+ assert agentService.getContext().queryValue("VLEVEL") == "0"+ assert agentService.getContext().queryValue("VATA") == "-"+ assert agentService.getContext().queryValue("VATD") == "-"+ assert agentService.getContext().queryValue("VPRESENCE") == "No"+ assert agentService.getContext().queryValue("GVconfig") == "OpenRemote"+ assert agentService.getContext().queryValue("VArrivalBackground") == "on"+ assert agentService.getContext().queryValue("VDepartureBackground") == "off"+ assert agentService.getContext().queryValue("FS.Toutside") == "15°"+ assert agentService.getContext().queryValue("FS.Tinside") == "19.5°"+ assert agentService.getContext().queryValue("FS.PIR") == "off"+ assert agentService.getContext().queryValue("FS.Window") == "off"+ assert agentService.getContext().queryValue("VETA.Mon") == "09:00"+ assert agentService.getContext().queryValue("VETD.Mon") == "17:00"+ assert agentService.getContext().queryValue("VETA.Tue") == "09:00"+ assert agentService.getContext().queryValue("VETD.Tue") == "17:00"+ assert agentService.getContext().queryValue("VETA.Wed") == "09:00"+ assert agentService.getContext().queryValue("VETD.Wed") == "17:00"+ assert agentService.getContext().queryValue("VETA.Thu") == "09:00"+ assert agentService.getContext().queryValue("VETD.Thu") == "17:00"+ assert agentService.getContext().queryValue("VETA.Fri") == "09:00"+ assert agentService.getContext().queryValue("VETD.Fri") == "17:00"+ assert agentService.getContext().queryValue("VETA.Sat") == "09:00"+ assert agentService.getContext().queryValue("VETD.Sat") == "17:00"+ assert agentService.getContext().queryValue("VETA.Sun") == "09:00"+ assert agentService.getContext().queryValue("VETD.Sun") == "17:00"+ }++ cleanup: "the server should be stopped"+ stopContainer(container)+ }+}diff --git a/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovy b/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovyindex b3b433850..cd34953f2 100644--- a/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovy+++ b/agent/src/test/groovy/org/openremote/test/rules/PIDTest.groovy@@ -18,7 +18,7 @@ class PIDTest extends Specification implements ContainerTrait {def deploymentXml = getClass().getResourceAsStream("/org/openremote/test/rules/pid/agent.xml")- def conditions = new PollingConditions(timeout: 10, initialDelay: 0.5, factor: 0.25)+ def conditions = new PollingConditions(timeout: 10, initialDelay: 0.25)and: "some rules"def rulesProvider = new RulesProvider() {diff --git a/agent/src/test/resources/org/openremote/test/rules/climatecontrol/ClimateControl.drl b/agent/src/test/resources/org/openremote/test/rules/climatecontrol/ClimateControl.drlindex 304b17a7e..e23da1cb0 100644--- a/agent/src/test/resources/org/openremote/test/rules/climatecontrol/ClimateControl.drl+++ b/agent/src/test/resources/org/openremote/test/rules/climatecontrol/ClimateControl.drl@@ -50,8 +50,6 @@ thencommands.execute("VATD", persistence.readData("VATD","-"));commands.execute("VPRESENCE","No");commands.execute("GVconfig",persistence.readData("GVconfig", "OpenRemote"));- commands.execute("GVtestsPassed",persistence.readData("GVtestsPassed", "0"));- commands.execute("GVtestsFailed",persistence.readData("GVtestsFailed", "0"));commands.execute("VArrivalBackground", "on");commands.execute("VDepartureBackground", "off");end@@ -1277,188 +1275,6 @@ thencommands.execute("VDepartureBackground", "off");end-// PID controller start--rule "-PID: init"-then- commands.execute("GV.PID.Kp",persistence.readData("GV.PID.Kp", "0.6"));- commands.execute("GV.PID.Ki",persistence.readData("GV.PID.Ki", "0.2"));- commands.execute("GV.PID.Kd",persistence.readData("GV.PID.Kd", "0.1"));- commands.execute("GV.PID.Db",persistence.readData("GV.PID.Db", "0.0"));- commands.execute("GV.PID.Sp",persistence.readData("GV.PID.Sp", "1.0"));-end--// PID UI--rule "-PID: increase Kp"- timer(int:300ms)-when- SensorState(sensorName=="GV.PID.Kp", $v: value)- SensorState($s:sensorName=="PID.kp.inc", value=="ON")-then- commands.execute("GV.PID.Kp", String.format("%.1f", Double.parseDouble($v.toString())+0.1) );- commands.execute($s, "off");-end--rule "-PID: decrease Kp"- timer(int:300ms)-when- SensorState(sensorName=="GV.PID.Kp", $v: value)- SensorState($s:sensorName=="PID.kp.dec", value=="ON")-then- commands.execute("GV.PID.Kp", String.format("%.1f", Double.parseDouble($v.toString())-0.1) );- commands.execute($s, "off");-end--rule "-PID: increase Ki"- timer(int:300ms)-when- SensorState(sensorName=="GV.PID.Ki", $v: value)- SensorState($s:sensorName=="PID.ki.inc", value=="ON")-then- commands.execute("GV.PID.Ki", String.format("%.1f", Double.parseDouble($v.toString())+0.1) );- commands.execute($s, "off");-end--rule "-PID: decrease Ki"- timer(int:300ms)-when- SensorState(sensorName=="GV.PID.Ki", $v: value)- SensorState($s:sensorName=="PID.ki.dec", value=="ON")-then- commands.execute("GV.PID.Ki", String.format("%.1f", Double.parseDouble($v.toString())-0.1) );- commands.execute($s, "off");-end--rule "-PID: increase Kd"- timer(int:300ms)-when- SensorState(sensorName=="GV.PID.Kd", $v: value)- SensorState($s:sensorName=="PID.kd.inc", value=="ON")-then- commands.execute("GV.PID.Kd", String.format("%.1f", Double.parseDouble($v.toString())+0.1) );- commands.execute($s, "off");-end--rule "-PID: decrease Kd"- timer(int:300ms)-when- SensorState(sensorName=="GV.PID.Kd", $v: value)- SensorState($s:sensorName=="PID.kd.dec", value=="ON")-then- commands.execute("GV.PID.Kd", String.format("%.1f", Double.parseDouble($v.toString())-0.1) );- commands.execute($s, "off");-end--rule "-PID: increase Db"- timer(int:300ms)-when- SensorState(sensorName=="GV.PID.Db", $v: value)- SensorState($s:sensorName=="PID.db.inc", value=="ON")-then- commands.execute("GV.PID.Db", String.format("%.1f", Double.parseDouble($v.toString())+0.1) );- commands.execute($s, "off");-end--rule "-PID: decrease Db"- timer(int:300ms)-when- SensorState(sensorName=="GV.PID.Db", $v: value)- SensorState($s:sensorName=="PID.db.dec", value=="ON")-then- commands.execute("GV.PID.Db", String.format("%.1f", Double.parseDouble($v.toString())-0.1) );- commands.execute($s, "off");-end--rule "-PID: increase Sp"- timer(int:300ms)-when- SensorState(sensorName=="GV.PID.Sp", $v: value)- SensorState($s:sensorName=="PID.sp.inc", value=="ON")-then- commands.execute("GV.PID.Sp", String.format("%.1f", Double.parseDouble($v.toString())+0.5) );- commands.execute($s, "off");-end--rule "-PID: decrease Sp"- timer(int:300ms)-when- SensorState(sensorName=="GV.PID.Sp", $v: value)- SensorState($s:sensorName=="PID.sp.dec", value=="ON")-then- commands.execute("GV.PID.Sp", String.format("%.1f", Double.parseDouble($v.toString())-0.5) );- commands.execute($s, "off");-end--rule "-PID: reset inc/dec"- salience -10- timer(int: 1s)-when- SensorState($s: sensorName matches "^PID......c$", value!="off")-then- commands.execute($s, "off");-end--// PID algorithm--declare PIDVars-// Input==Output;- output: double- iterm: double- prevInput: double- outMin: double- outMax: double- active: Boolean-end--rule "PID: algo init"-then- PIDVars pv = new PIDVars();- pv.setActive(true);- pv.setOutMax(100);- pv.setOutMin(-100);- insert(pv);-end--rule "--PID: compute"- timer(int: 0 1s)-when- $pv: PIDVars(active==true)- SensorState(sensorName=="GV.PID.Kp", $kp: value)- SensorState(sensorName=="GV.PID.Ki", $ki: value)- SensorState(sensorName=="GV.PID.Kd", $kd: value)- SensorState(sensorName=="GV.PID.Sp", $sp: value)- SensorState(sensorName=="GV.PID.Db", $db: value)-then- double error = Double.parseDouble($sp.toString() ) - $pv.getOutput();- double kp = Double.parseDouble($kp.toString());- double ki = Double.parseDouble($ki.toString()); // * sample time in sec- double kd = Double.parseDouble($kd.toString()); // / sample time in sec- double sp = Double.parseDouble($sp.toString());- double db = Double.parseDouble($db.toString());-- double iterm = $pv.getIterm() + (ki * error);- if(iterm > $pv.getOutMax() ) iterm = $pv.getOutMax();- else if(iterm < $pv.getOutMin() ) iterm = $pv.getOutMin();- $pv.setIterm( iterm );-- //double dInput = (Input - lastInput);- double dInput = ($pv.getOutput() - $pv.getPrevInput() );- $pv.setPrevInput($pv.getOutput());-- // Compute PID Output- double output = kp * error + $pv.getIterm()- kd * dInput;- if(output > $pv.getOutMax()) output = $pv.getOutMax();- else if(output < $pv.getOutMin()) output = $pv.getOutMin();- $pv.setOutput(output);-- commands.execute("PID.Se", String.format("%.4f", error) );- commands.execute("PID.Output", String.format("%.4f", output) );-// LOG.fine($pv.toString() + " error: "+String.format("%f", Double.parseDouble($sp.toString() ) - $pv.getOutput()));-end--// PID end-// Fake sensors startrule "Fake sensors init"