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);
}
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);
});
}
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);
});
}
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);
});
}
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 -> {
});
});
}
Aggregations