
com.ociweb.iot.hardware.impl.test.TestHardware Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of foglight Show documentation
Show all versions of foglight Show documentation
FogLight is a lightweight runtime that enables makers of all ages and skill levels to create highly
performant apps for embedded devices like Raspberry Pi's.
The newest version!
package com.ociweb.iot.hardware.impl.test;
import java.util.ArrayList;
import java.util.Arrays;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ociweb.gl.api.Behavior;
import com.ociweb.gl.api.MsgRuntime;
import com.ociweb.gl.impl.schema.TrafficAckSchema;
import com.ociweb.gl.impl.schema.TrafficReleaseSchema;
import com.ociweb.gl.impl.stage.ReactiveListenerStage;
import com.ociweb.gl.impl.stage.ReactiveManagerPipeConsumer;
import com.ociweb.iot.hardware.HardwareImpl;
import com.ociweb.iot.hardware.HardwarePlatformType;
import com.ociweb.iot.hardware.IODevice;
import com.ociweb.iot.hardware.impl.DefaultCommandChannel;
import com.ociweb.iot.maker.Port;
import com.ociweb.pronghorn.iot.ReactiveIoTListenerStage;
import com.ociweb.pronghorn.iot.i2c.I2CBacking;
import com.ociweb.pronghorn.iot.rs232.RS232Clientable;
import com.ociweb.pronghorn.iot.schema.I2CCommandSchema;
import com.ociweb.pronghorn.iot.schema.I2CResponseSchema;
import com.ociweb.pronghorn.pipe.Pipe;
import com.ociweb.pronghorn.pipe.PipeConfigManager;
import com.ociweb.pronghorn.stage.PronghornStage;
import com.ociweb.pronghorn.stage.scheduling.GraphManager;
import com.ociweb.pronghorn.stage.scheduling.NonThreadScheduler;
import com.ociweb.pronghorn.stage.scheduling.StageScheduler;
import com.ociweb.pronghorn.stage.test.PipeCleanerStage;
import com.ociweb.pronghorn.stage.test.PipeNoOp;
public class TestHardware extends HardwareImpl {
private static final int MAX_PINS = 127;
private final int[] pinData = new int[MAX_PINS];
public boolean isInUnitTest = false;
private final int[] pinHighValues = new int[MAX_PINS];
private final long[] firstTime = new long[MAX_PINS];
private final long[] lastTime = new long[MAX_PINS];
private static final Logger logger = LoggerFactory.getLogger(TestHardware.class);
private long lastProvidedTime;
private RS232Clientable testSerial = new TestSerial();
public TestPortReader portReader = new SinTestPortReader();
public TestHardware(GraphManager gm, String[] args) {
super(gm, args, 1);
//logger.trace("You are running on the test hardware.");
}
public I2CBacking getI2CBacking() {
if (null == i2cBackingInternal) {
i2cBackingInternal = new TestI2CBacking().configure((byte) 1);
}
return i2cBackingInternal;
}
public void enableTelemetry(boolean enable) {
if (!isInUnitTest && enable) {
super.enableTelemetry();
}
//else do nothing this is a test.
}
protected RS232Clientable buildSerialClient() {
return testSerial;
}
public void setI2CValueToRead(byte address, byte[] data, int length) {
TestI2CBacking testBacking = (TestI2CBacking)getI2CBacking();
testBacking.setValueToRead(address, data, length);
}
public void clearI2CWriteCount() {
TestI2CBacking testBacking = (TestI2CBacking)getI2CBacking();
testBacking.clearWriteCount();
}
public int getI2CWriteCount() {
TestI2CBacking testBacking = (TestI2CBacking)getI2CBacking();
return testBacking.getWriteCount();
}
public A outputLastI2CWrite(A target, int back) {
assert(back>0);
assert(back A outputLastSerialWrite(A target, int back){
byte[] output = new byte[100];
testSerial.readInto(output, 0, 20, output, 0, 0);
for (byte b: output){
System.out.println(b + ", ");
}
return target;
}
@Override
public void coldSetup() {
clearCaputuredHighs();
clearCaputuredFirstTimes();
clearCaputuredLastTimes();
}
public void clearCaputuredHighs() {
Arrays.fill(pinHighValues, Integer.MIN_VALUE);
}
public void clearCaputuredFirstTimes() {
Arrays.fill(firstTime, 0);
}
public void clearCaputuredLastTimes() {
Arrays.fill(lastTime, 0);
}
public int getCapturedHigh(Port port) {
return pinHighValues[port.port];
}
public long getFirstTime(Port port) {
return firstTime[port.port];
}
public long getLastTime(Port port) {
return lastTime[port.port];
}
@Override
public HardwarePlatformType getPlatformType() {
return HardwarePlatformType.TEST;
}
@Override
public int read(Port port) {
IODevice connectedDevice = getConnectedDevice(port);
int range = connectedDevice == null ? 255 : connectedDevice.range();
return portReader.read(port, pinData[port.port], range);
}
@Override
public void write(Port port, int value) {
//System.out.print("write value: "+value);
pinHighValues[port.port] = Math.max(pinHighValues[port.port], value);
pinData[port.port]=value;
lastTime[port.port] = lastProvidedTime;
if (0==firstTime[port.port]) {
firstTime[port.port]=lastProvidedTime;
}
logger.debug("port {} set to {} at {}",port,value,lastProvidedTime);
}
@Override
public DefaultCommandChannel newCommandChannel(int features, int instance,
PipeConfigManager pcm) {
return new DefaultCommandChannel(gm, this, features, instance, pcm);
}
@Override
public DefaultCommandChannel newCommandChannel(int instance,
PipeConfigManager pcm) {
return new DefaultCommandChannel(gm, this, 0, instance, pcm);
}
@Override
public StageScheduler createScheduler(MsgRuntime runtime) {
if (isInUnitTest) {
//NOTE: need to consider different schedulers in the future.
return new NonThreadScheduler(gm);
} else {
return super.createScheduler(runtime);
}
}
@Override
public long currentTimeMillis() {
return lastProvidedTime = super.currentTimeMillis();
}
public long lastProvidedTimeMillis() {
return lastProvidedTime;
}
@Override
public R createReactiveListener(GraphManager gm, Behavior listener,
Pipe>[] inputPipes, Pipe>[] outputPipes,
ArrayList consumers,
int parallelInstance, String nameId) {
assert(null!=listener);
return (R)new ReactiveIoTListenerStage(gm, listener,
inputPipes, outputPipes,
consumers, this, parallelInstance, nameId);
}
public final boolean isTestHardware() {
return true;
}
public void setSerialEcho(boolean on){
((TestSerial)testSerial).setEcho(on);//TODO: is this hard cast okay?
}
@Override
protected void createI2COutputInputStage(MsgRuntime,?,?> runtime,
Pipe[] i2cPipes,
Pipe[] masterI2CgoOut,
Pipe[] masterI2CackIn,
Pipe masterI2CResponsePipe) {
if (null==masterI2CgoOut || masterI2CgoOut.length==0 || allNull(masterI2CgoOut)) {
if (null!=masterI2CResponsePipe) {
PipeNoOp.newInstance(MsgRuntime.getGraphManager(runtime), masterI2CResponsePipe);
}
GraphManager.addNota(MsgRuntime.getGraphManager(runtime), GraphManager.STAGE_NAME, "FauxI2C",
PipeCleanerStage.newInstance(MsgRuntime.getGraphManager(runtime), i2cPipes).stageId);
} else {
//TODO: need to build an empty responder for this case
super.createI2COutputInputStage(runtime,i2cPipes,masterI2CgoOut, masterI2CackIn, masterI2CResponsePipe);
}
}
private boolean allNull(Pipe>[] a) {
int i = a.length;
while (--i>=0) {
if (a[i]!=null) {
return false;
}
}
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy