diff --git a/iot-ocp-businessrules-service/src/main/resources/com/redhat/examples/iot/datacenter/monitor/sensor-rules.drl b/iot-ocp-businessrules-service/src/main/resources/com/redhat/examples/iot/datacenter/monitor/sensor-rules.drl index a5a4a60..e00c43e 100644 --- a/iot-ocp-businessrules-service/src/main/resources/com/redhat/examples/iot/datacenter/monitor/sensor-rules.drl +++ b/iot-ocp-businessrules-service/src/main/resources/com/redhat/examples/iot/datacenter/monitor/sensor-rules.drl @@ -26,6 +26,17 @@ rule "OrganVibrationTooHigh" update( $t ); end +rule "OrganLightTooHigh" + lock-on-active true + when + $t : Measure( category == "Organ", dataType == "light", payload != null, Double.parseDouble(payload) >= 50 ) + then + System.out.println("Rule 'high light' fired as light is " + $t.getPayload()); + $t.setErrorCode(1); + $t.setErrorMessage("Ambient light too high"); + update( $t ); +end + rule "MachineTemperatureTooHigh" lock-on-active true when @@ -47,3 +58,14 @@ rule "MachineVibrationTooHigh" $t.setErrorMessage("Vibration too high"); update( $t ); end + +rule "MachineLightTooHigh" + lock-on-active true + when + $t : Measure( category == "Machine", dataType == "light", payload != null, Double.parseDouble(payload) >= 50 ) + then + System.out.println("Rule 'high light' fired as light is " + $t.getPayload()); + $t.setErrorCode(1); + $t.setErrorMessage("Ambient light too high"); + update( $t ); +end diff --git a/iot-ocp-software-sensor/src/main/java/com/redhat/examples/iot/sensor/LightSensor.java b/iot-ocp-software-sensor/src/main/java/com/redhat/examples/iot/sensor/LightSensor.java new file mode 100644 index 0000000..ac4e5b2 --- /dev/null +++ b/iot-ocp-software-sensor/src/main/java/com/redhat/examples/iot/sensor/LightSensor.java @@ -0,0 +1,89 @@ +package com.redhat.examples.iot.sensor; + +import java.util.concurrent.ThreadLocalRandom; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; + +import com.redhat.examples.iot.model.Measure; + +@Component(SensorType.LIGHT) +@Scope("prototype") +public class LightSensor implements Sensor { + + @Value("${sensor.light.enabled}") + private boolean enabled; + + @Value("${sensor.light.frequency}") + public int frequency; + + @Value("${sensor.light.start}") + private int start; + + @Value("${sensor.light.maxIteration}") + private int maxIteration; + + @Value("${sensor.light.minIteration}") + private int minIteration; + + @Value("${sensor.light.minRange}") + private int minRange; + + @Value("${sensor.light.maxRange}") + private int maxRange; + + public double currentValue; + + public int count = 0; + + @PostConstruct + @Override + public void initAndReset() { + currentValue = start; + } + + + @Override + public int getFrequency() { + return frequency; + } + + @Override + public String getType() { + return SensorType.LIGHT; + } + + @Override + public boolean isEnabled() { + return enabled; + } + + @Override + public Measure calculateCurrentMeasure(Measure measure) { + + + if(count > 0) { + + // Calculate random value from range + double randValue = ThreadLocalRandom.current().nextDouble(minIteration, (maxIteration+1)); + currentValue = currentValue + randValue; + + if(currentValue < minRange || currentValue > maxRange) { + initAndReset(); + } + + } + + measure.setType(getType()); + measure.setPayload(String.valueOf(currentValue)); + + ++count; + return measure; + } + + + +} diff --git a/iot-ocp-software-sensor/src/main/java/com/redhat/examples/iot/sensor/SensorType.java b/iot-ocp-software-sensor/src/main/java/com/redhat/examples/iot/sensor/SensorType.java index ff08e71..0be72c7 100644 --- a/iot-ocp-software-sensor/src/main/java/com/redhat/examples/iot/sensor/SensorType.java +++ b/iot-ocp-software-sensor/src/main/java/com/redhat/examples/iot/sensor/SensorType.java @@ -6,5 +6,5 @@ public class SensorType { public static final String GPS = "gps"; public static final String COUNTDOWN = "countdown"; public static final String TEMPERATURE = "temperature"; - + public static final String LIGHT = "light"; } diff --git a/iot-ocp-software-sensor/src/main/resources/application.properties b/iot-ocp-software-sensor/src/main/resources/application.properties index eed0a6b..eb69141 100644 --- a/iot-ocp-software-sensor/src/main/resources/application.properties +++ b/iot-ocp-software-sensor/src/main/resources/application.properties @@ -1,7 +1,7 @@ # Application Properties app.name=iot-ocp device.id=iottest -iot.types=temperature,vibration,gps,countdown +iot.types=temperature,vibration,gps,countdown,light device.categories=Organ,Machine # MQTT @@ -44,3 +44,12 @@ sensor.temperature.minRange=40 sensor.temperature.maxRange=75 sensor.temperature.minIteration=0 sensor.temperature.maxIteration=1 + +# Light +sensor.light.enabled=true +sensor.light.frequency=5 +sensor.light.start=0 +sensor.light.minRange=0 +sensor.light.maxRange=25000 +sensor.light.minIteration=0 +sensor.light.maxIteration=3