use of com.ociweb.gl.api.PubSubService 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);
PubSubService pubService = channel0.newPubSubService();
runtime.addPubSubListener((topic, payload) -> {
turnOnRed(channel0);
channel0.block(State.REDLIGHT.getTime());
pubService.publishTopic("GREEN", w -> {
});
return true;
}).addSubscription("RED");
final FogCommandChannel channel1 = runtime.newCommandChannel(FogRuntime.PIN_WRITER | FogRuntime.I2C_WRITER);
PubSubService pubSubService = channel1.newPubSubService();
runtime.addPubSubListener((topic, payload) -> {
turnOnGreen(channel1);
channel1.block(State.GREENLIGHT.getTime());
pubSubService.publishTopic("YELLOW", w -> {
});
return true;
}).addSubscription("GREEN");
final FogCommandChannel channel2 = runtime.newCommandChannel(FogRuntime.PIN_WRITER | FogRuntime.I2C_WRITER);
PubSubService pubService2 = channel2.newPubSubService();
runtime.addPubSubListener((topic, payload) -> {
turnOnYellow(channel2);
channel2.block(State.YELLOWLIGHT.getTime());
pubService2.publishTopic("RED", w -> {
});
return true;
}).addSubscription("YELLOW");
final FogCommandChannel channel4 = runtime.newCommandChannel(FogRuntime.PIN_WRITER | FogRuntime.I2C_WRITER | GreenCommandChannel.DYNAMIC_MESSAGING);
PubSubService pubService4 = channel4.newPubSubService();
runtime.addStartupListener(() -> {
pubService4.publishTopic("RED", w -> {
});
});
}
use of com.ociweb.gl.api.PubSubService in project GreenLightning by oci-pronghorn.
the class MQTTApp method declareBehavior.
@Override
public void declareBehavior(final GreenRuntime runtime) {
// optional 2 topics, optional transform lambda
runtime.bridgeSubscription("topic/ingress", mqttConfig);
// optional 2 topics, optional transform lambda
runtime.bridgeTransmission("topic/egress", mqttConfig);
final MsgCommandChannel cmdChnl = runtime.newCommandChannel();
final PubSubService pubSubService = cmdChnl.newPubSubService();
TimeListener timeListener = new TimeListener() {
@Override
public void timeEvent(long time, int iteration) {
Writable writable = new Writable() {
@Override
public void write(ChannelWriter writer) {
Date d = new Date(System.currentTimeMillis());
System.err.println("sent " + d);
writer.writeUTF8Text("egress body " + d);
}
};
pubSubService.publishTopic("topic/egress", writable);
}
};
runtime.addTimePulseListener(timeListener);
final MsgCommandChannel cmd = runtime.newCommandChannel();
final PubSubService pubSubService2 = cmd.newPubSubService();
PubSubListener listener = new PubSubListener() {
@Override
public boolean message(CharSequence topic, ChannelReader payload) {
System.out.print("\ningress body: ");
payload.readUTFOfLength(payload.available(), System.out);
System.out.println();
Writable writable = new Writable() {
@Override
public void write(ChannelWriter writer) {
writer.writeUTF("second step test message");
}
};
pubSubService2.publishTopic("localtest", writable);
return true;
}
};
runtime.addPubSubListener(listener).addSubscription("topic/ingress");
PubSubListener localTest = new PubSubListener() {
@Override
public boolean message(CharSequence topic, ChannelReader payload) {
System.out.println("got topic " + topic + " payload " + payload.readUTF());
return true;
}
};
runtime.addPubSubListener(localTest).addSubscription("localtest");
}
use of com.ociweb.gl.api.PubSubService in project GreenLightning by oci-pronghorn.
the class MassiveBehavior method declareBehavior.
@Override
public void declareBehavior(GreenRuntime runtime) {
// runtime.addTimePulseListener(new stopperBehavior(runtime));
int i = 7;
while (--i >= 0) {
final GreenCommandChannel cmd = runtime.newCommandChannel();
final PubSubService pubSubServce = cmd.newPubSubService();
final String topic = "topic" + i;
final int value = i;
final Writable writable = new Writable() {
@Override
public void write(ChannelWriter writer) {
writer.writePackedInt(value);
}
};
TimeListener pubs = new TimeListener() {
@Override
public void timeEvent(long time, int iteration) {
if (!pubSubServce.publishTopic(topic, writable)) {
System.out.println("overloaded can not publish " + value);
}
}
};
runtime.addTimePulseListener(pubs);
PubSubListener subs = new PubSubListener() {
public boolean message(CharSequence topic, ChannelReader payload) {
return true;
}
};
runtime.addPubSubListener(subs).addSubscription(topic);
}
}
Aggregations