Search in sources :

Example 1 with FogCommandChannel

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

the class IoTApp method configureWebBasedColorChange.

private void configureWebBasedColorChange(FogRuntime runtime) {
    final FogCommandChannel channel = runtime.newCommandChannel(FogRuntime.PIN_WRITER | FogRuntime.I2C_WRITER | GreenCommandChannel.DYNAMIC_MESSAGING | GreenCommandChannel.NET_RESPONDER);
    runtime.addRestListener((reader) -> {
        if (reader.structured().isEqual(COLOR, RED)) {
            return channel.publishHTTPResponse(reader, turnOnRed(channel) ? 200 : 500);
        } else if (reader.structured().isEqual(COLOR, GREEN)) {
            return channel.publishHTTPResponse(reader, turnOnGreen(channel) ? 200 : 500);
        } else if (reader.structured().isEqual(COLOR, YELLOW)) {
            return channel.publishHTTPResponse(reader, turnOnYellow(channel) ? 200 : 500);
        } else {
            return channel.publishHTTPResponse(reader, 404);
        }
    }).includeRoutes(webRoute);
}
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 2 with FogCommandChannel

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

the class IoTApp method declareBehavior.

@Override
public void declareBehavior(FogRuntime runtime) {
    final FogCommandChannel blinkerChannel = runtime.newCommandChannel(FogRuntime.PIN_WRITER);
    runtime.addTimePulseListener((time, instance) -> {
        blinkerChannel.setValueAndBlock(LED_PORT, true, PAUSE);
        blinkerChannel.setValue(LED_PORT, false);
    });
}
Also used : FogCommandChannel(com.ociweb.iot.maker.FogCommandChannel)

Example 3 with FogCommandChannel

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

the class IoTApp method declareBehavior.

// TODO: rewrite this a a class, can not be done as two lambddas and be responsvie.
@Override
public void declareBehavior(FogRuntime runtime) {
    FogCommandChannel channel = runtime.newCommandChannel(FogRuntime.I2C_WRITER | GreenCommandChannel.DYNAMIC_MESSAGING);
    runtime.addDigitalListener((connection, time, durationMillis, value) -> {
        if (0 == value) {
            // how long was it pressed before this change to up?
            if (durationMillis > 1000) {
                startTime = 0;
                stopTime = 0;
                running = false;
            } else {
                running = startOnUp;
                startOnUp = false;
            }
        } else {
            // toggle clock start or stop
            if (!running) {
                if (0 == startTime) {
                    startTime = System.currentTimeMillis();
                } else {
                    startTime = System.currentTimeMillis() - (stopTime - startTime);
                    stopTime = 0;
                }
                startOnUp = true;
            } else {
                stopTime = System.currentTimeMillis();
                running = false;
            }
        }
    });
    final FogCommandChannel lcdTextChannel = runtime.newCommandChannel(FogRuntime.I2C_WRITER | GreenCommandChannel.DYNAMIC_MESSAGING);
    runtime.addTimePulseListener((time, instance) -> {
        long duration = 0 == startTime ? 0 : stopTime == 0 ? time - startTime : stopTime - startTime;
        LocalDateTime date = LocalDateTime.ofInstant(Instant.ofEpochMilli(duration), zone);
        String text = date.format(formatter);
        Grove_LCD_RGB.commandForText(lcdTextChannel, text);
    });
}
Also used : LocalDateTime(java.time.LocalDateTime) FogCommandChannel(com.ociweb.iot.maker.FogCommandChannel)

Example 4 with FogCommandChannel

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

the class IoTApp method declareBehavior.

@Override
public void declareBehavior(FogRuntime runtime) {
    final FogCommandChannel rgbLightChannel = runtime.newCommandChannel(GreenCommandChannel.DYNAMIC_MESSAGING);
    runtime.addAnalogListener((port, time, durationMillis, average, value) -> {
        switch(port) {
            case // LIGHT_SENSOR_PORT
            A2:
                // value is only 10 bits max
                int leadingZeros = Integer.numberOfLeadingZeros(value) - (32 - 10);
                int level = Math.min(255, (brightness * Math.min(leadingZeros, 8)) / 8);
                Grove_LCD_RGB.commandForColor(rgbLightChannel, level, level, level);
                break;
            case // ANGLE_SENSOR_PORT
            A1:
                brightness = ((AngleSensor.range() / 2) * value) / AngleSensor.range();
                break;
        }
    });
    final FogCommandChannel lcdTextChannel = runtime.newCommandChannel(GreenCommandChannel.DYNAMIC_MESSAGING);
    runtime.addTimePulseListener((time, instance) -> {
        LocalDateTime date = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), zone);
        String text = date.format(formatter1) + "\n" + date.format(formatter2);
        Grove_LCD_RGB.commandForText(lcdTextChannel, text);
    });
}
Also used : LocalDateTime(java.time.LocalDateTime) FogCommandChannel(com.ociweb.iot.maker.FogCommandChannel)

Example 5 with FogCommandChannel

use of com.ociweb.iot.maker.FogCommandChannel 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)

Aggregations

FogCommandChannel (com.ociweb.iot.maker.FogCommandChannel)6 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 FogApp (com.ociweb.iot.maker.FogApp)2 FogRuntime (com.ociweb.iot.maker.FogRuntime)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 LocalDateTime (java.time.LocalDateTime)2