Search in sources :

Example 11 with FogRuntime

use of com.ociweb.iot.maker.FogRuntime in project FogLight-Examples by oci-pronghorn.

the class AppTest method testApp.

@Test
public void testApp() {
    FogRuntime runtime = FogRuntime.test(new IoTApp());
    ScriptedNonThreadScheduler scheduler = (ScriptedNonThreadScheduler) runtime.getScheduler();
    scheduler.startup();
    TestHardware hardware = (TestHardware) runtime.getHardware();
    hardware.setI2CValueToRead((byte) 4, new byte[] { 0, 0, 0 }, 3);
    int iterations = 10;
    while (--iterations >= 0) {
        scheduler.run();
    // test application here
    }
}
Also used : ScriptedNonThreadScheduler(com.ociweb.pronghorn.stage.scheduling.ScriptedNonThreadScheduler) TestHardware(com.ociweb.iot.hardware.impl.test.TestHardware) FogRuntime(com.ociweb.iot.maker.FogRuntime) Test(org.junit.Test)

Example 12 with FogRuntime

use of com.ociweb.iot.maker.FogRuntime in project FogLight-Examples by oci-pronghorn.

the class AppTest method testApp.

@Test
public void testApp() {
    FogRuntime runtime = FogRuntime.test(new IoTApp());
    ScriptedNonThreadScheduler scheduler = (ScriptedNonThreadScheduler) runtime.getScheduler();
    TestHardware hardware = (TestHardware) runtime.getHardware();
    hardware.portReader = new BasicTestPortReader();
    hardware.clearI2CWriteCount();
    // 970 will give us 200 BPM and a delay of 300 ms
    hardware.write(IoTApp.ROTARY_ANGLE_PORT, 970);
    scheduler.startup();
    long lastTime = 0;
    int ticks = 5;
    hardware.clearCaputuredFirstTimes();
    hardware.clearCaputuredHighs();
    boolean isMetronomeRunning = false;
    long startTime = System.currentTimeMillis();
    while (ticks > 0) {
        scheduler.run();
        long time = hardware.getFirstTime(IoTApp.BUZZER_PORT);
        if (0 != time) {
            int high = hardware.getCapturedHigh(IoTApp.BUZZER_PORT);
            if (0 != high) {
                ticks--;
                if (0 != lastTime) {
                    if (isMetronomeRunning) {
                        long durationMs = (time - lastTime);
                        // due to assertions and garbage when unit tests are run we can not be so strict here
                        int overheadForTesting = 10;
                        int window = 300;
                        // this is a little too quick now... and under the threshold.
                        // assertTrue(durationMs+" at "+time, durationMs>=(window-overheadForTesting));
                        assertTrue(durationMs + " at " + time, durationMs <= (window + overheadForTesting));
                    } else {
                        isMetronomeRunning = true;
                    }
                }
                lastTime = time;
                hardware.clearCaputuredFirstTimes();
                hardware.clearCaputuredHighs();
            } else {
            // low
            }
        }
    }
    int count = hardware.getI2CWriteCount();
    System.out.println(count);
    int c = count;
    while (c > 0) {
        hardware.outputLastI2CWrite(System.out, c--).append("\n");
    }
    assertEquals("Did not find all the ticks.", 0, ticks);
    scheduler.shutdown();
    scheduler.awaitTermination(10, TimeUnit.SECONDS);
}
Also used : ScriptedNonThreadScheduler(com.ociweb.pronghorn.stage.scheduling.ScriptedNonThreadScheduler) TestHardware(com.ociweb.iot.hardware.impl.test.TestHardware) BasicTestPortReader(com.ociweb.iot.hardware.impl.test.BasicTestPortReader) FogRuntime(com.ociweb.iot.maker.FogRuntime) Test(org.junit.Test)

Example 13 with FogRuntime

use of com.ociweb.iot.maker.FogRuntime in project FogLight-Examples by oci-pronghorn.

the class IoTApp method configureTimeBasedColorChange.

protected void configureTimeBasedColorChange(FogRuntime runtime) {
    final FogCommandChannel channel0 = runtime.newCommandChannel(FogRuntime.PIN_WRITER | FogRuntime.I2C_WRITER | GreenCommandChannel.DYNAMIC_MESSAGING);
    runtime.addPubSubListener((topic, payload) -> {
        turnOnRed(channel0);
        channel0.block(State.REDLIGHT.getTime());
        channel0.publishTopic("GREEN", w -> {
        });
        return true;
    }).addSubscription("RED");
    final FogCommandChannel channel1 = runtime.newCommandChannel(FogRuntime.PIN_WRITER | FogRuntime.I2C_WRITER | GreenCommandChannel.DYNAMIC_MESSAGING);
    runtime.addPubSubListener((topic, payload) -> {
        turnOnGreen(channel1);
        channel1.block(State.GREENLIGHT.getTime());
        channel1.publishTopic("YELLOW", w -> {
        });
        return true;
    }).addSubscription("GREEN");
    final FogCommandChannel channel2 = runtime.newCommandChannel(FogRuntime.PIN_WRITER | FogRuntime.I2C_WRITER | GreenCommandChannel.DYNAMIC_MESSAGING);
    runtime.addPubSubListener((topic, payload) -> {
        turnOnYellow(channel2);
        channel2.block(State.YELLOWLIGHT.getTime());
        channel2.publishTopic("RED", w -> {
        });
        return true;
    }).addSubscription("YELLOW");
    final FogCommandChannel channel4 = runtime.newCommandChannel(FogRuntime.PIN_WRITER | FogRuntime.I2C_WRITER | GreenCommandChannel.DYNAMIC_MESSAGING);
    runtime.addStartupListener(() -> {
        channel4.publishTopic("RED", w -> {
        });
    });
}
Also used : Hardware(com.ociweb.iot.maker.Hardware) Port(com.ociweb.iot.maker.Port) GreenCommandChannel(com.ociweb.gl.api.GreenCommandChannel) LED(com.ociweb.iot.grove.simple_digital.SimpleDigitalTwig.LED) FogRuntime(com.ociweb.iot.maker.FogRuntime) NetGraphBuilder(com.ociweb.pronghorn.network.NetGraphBuilder) FogApp(com.ociweb.iot.maker.FogApp) D5(com.ociweb.iot.maker.Port.D5) D6(com.ociweb.iot.maker.Port.D6) FogCommandChannel(com.ociweb.iot.maker.FogCommandChannel) D3(com.ociweb.iot.maker.Port.D3) Grove_LCD_RGB(com.ociweb.iot.grove.lcd_rgb.Grove_LCD_RGB) FogCommandChannel(com.ociweb.iot.maker.FogCommandChannel)

Example 14 with FogRuntime

use of com.ociweb.iot.maker.FogRuntime in project FogLight-Examples by oci-pronghorn.

the class AppTest method testApp.

@Test
public void testApp() {
    FogRuntime runtime = FogRuntime.test(new Snake());
    ScriptedNonThreadScheduler scheduler = (ScriptedNonThreadScheduler) runtime.getScheduler();
    TestHardware hardware = (TestHardware) runtime.getHardware();
    scheduler.startup();
    int iterations = 10;
    while (--iterations >= 0) {
        scheduler.run();
    // test application here
    }
    scheduler.shutdown();
}
Also used : ScriptedNonThreadScheduler(com.ociweb.pronghorn.stage.scheduling.ScriptedNonThreadScheduler) TestHardware(com.ociweb.iot.hardware.impl.test.TestHardware) FogRuntime(com.ociweb.iot.maker.FogRuntime) Test(org.junit.Test)

Aggregations

FogRuntime (com.ociweb.iot.maker.FogRuntime)14 TestHardware (com.ociweb.iot.hardware.impl.test.TestHardware)12 ScriptedNonThreadScheduler (com.ociweb.pronghorn.stage.scheduling.ScriptedNonThreadScheduler)11 Test (org.junit.Test)9 Ignore (org.junit.Ignore)3 GreenCommandChannel (com.ociweb.gl.api.GreenCommandChannel)2 Grove_LCD_RGB (com.ociweb.iot.grove.lcd_rgb.Grove_LCD_RGB)2 LED (com.ociweb.iot.grove.simple_digital.SimpleDigitalTwig.LED)2 BasicTestPortReader (com.ociweb.iot.hardware.impl.test.BasicTestPortReader)2 FogApp (com.ociweb.iot.maker.FogApp)2 FogCommandChannel (com.ociweb.iot.maker.FogCommandChannel)2 Hardware (com.ociweb.iot.maker.Hardware)2 Port (com.ociweb.iot.maker.Port)2 D3 (com.ociweb.iot.maker.Port.D3)2 D5 (com.ociweb.iot.maker.Port.D5)2 D6 (com.ociweb.iot.maker.Port.D6)2 NetGraphBuilder (com.ociweb.pronghorn.network.NetGraphBuilder)2 TestI2CBacking (com.ociweb.iot.hardware.impl.test.TestI2CBacking)1