Search in sources :

Example 1 with PubSubService

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 -> {
        });
    });
}
Also used : Hardware(com.ociweb.iot.maker.Hardware) HTTPResponseService(com.ociweb.gl.api.HTTPResponseService) LED(com.ociweb.iot.grove.simple_digital.SimpleDigitalTwig.LED) FogRuntime(com.ociweb.iot.maker.FogRuntime) NetGraphBuilder(com.ociweb.pronghorn.network.NetGraphBuilder) FogCommandChannel(com.ociweb.iot.maker.FogCommandChannel) Grove_LCD_RGB(com.ociweb.iot.grove.lcd_rgb.Grove_LCD_RGB) Port(com.ociweb.iot.maker.Port) PubSubService(com.ociweb.gl.api.PubSubService) GreenCommandChannel(com.ociweb.gl.api.GreenCommandChannel) FogApp(com.ociweb.iot.maker.FogApp) D5(com.ociweb.iot.maker.Port.D5) D6(com.ociweb.iot.maker.Port.D6) D3(com.ociweb.iot.maker.Port.D3) PubSubService(com.ociweb.gl.api.PubSubService) FogCommandChannel(com.ociweb.iot.maker.FogCommandChannel)

Example 2 with PubSubService

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");
}
Also used : TimeListener(com.ociweb.gl.api.TimeListener) MsgCommandChannel(com.ociweb.gl.api.MsgCommandChannel) ChannelReader(com.ociweb.pronghorn.pipe.ChannelReader) PubSubListener(com.ociweb.gl.api.PubSubListener) PubSubService(com.ociweb.gl.api.PubSubService) Writable(com.ociweb.gl.api.Writable) Date(java.util.Date) ChannelWriter(com.ociweb.pronghorn.pipe.ChannelWriter)

Example 3 with PubSubService

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);
    }
}
Also used : TimeListener(com.ociweb.gl.api.TimeListener) ChannelReader(com.ociweb.pronghorn.pipe.ChannelReader) PubSubListener(com.ociweb.gl.api.PubSubListener) PubSubService(com.ociweb.gl.api.PubSubService) Writable(com.ociweb.gl.api.Writable) GreenCommandChannel(com.ociweb.gl.api.GreenCommandChannel) ChannelWriter(com.ociweb.pronghorn.pipe.ChannelWriter)

Aggregations

PubSubService (com.ociweb.gl.api.PubSubService)3 GreenCommandChannel (com.ociweb.gl.api.GreenCommandChannel)2 PubSubListener (com.ociweb.gl.api.PubSubListener)2 TimeListener (com.ociweb.gl.api.TimeListener)2 Writable (com.ociweb.gl.api.Writable)2 ChannelReader (com.ociweb.pronghorn.pipe.ChannelReader)2 ChannelWriter (com.ociweb.pronghorn.pipe.ChannelWriter)2 HTTPResponseService (com.ociweb.gl.api.HTTPResponseService)1 MsgCommandChannel (com.ociweb.gl.api.MsgCommandChannel)1 Grove_LCD_RGB (com.ociweb.iot.grove.lcd_rgb.Grove_LCD_RGB)1 LED (com.ociweb.iot.grove.simple_digital.SimpleDigitalTwig.LED)1 FogApp (com.ociweb.iot.maker.FogApp)1 FogCommandChannel (com.ociweb.iot.maker.FogCommandChannel)1 FogRuntime (com.ociweb.iot.maker.FogRuntime)1 Hardware (com.ociweb.iot.maker.Hardware)1 Port (com.ociweb.iot.maker.Port)1 D3 (com.ociweb.iot.maker.Port.D3)1 D5 (com.ociweb.iot.maker.Port.D5)1 D6 (com.ociweb.iot.maker.Port.D6)1 NetGraphBuilder (com.ociweb.pronghorn.network.NetGraphBuilder)1