-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
iot-ocp-software-sensor/src/main/java/com/redhat/examples/iot/sensor/LightSensor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters