Skip to content

Commit

Permalink
Added light sensor (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabre1041 authored Mar 26, 2017
1 parent 50c341b commit 8cbbf42
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -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;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

0 comments on commit 8cbbf42

Please sign in to comment.