Search in sources :

Example 1 with HTTPResponseService

use of com.ociweb.gl.api.HTTPResponseService 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);
    HTTPResponseService responseService = channel.newHTTPResponseService();
    runtime.addRestListener((reader) -> {
        if (reader.structured().isEqual(COLOR, RED)) {
            return responseService.publishHTTPResponse(reader, turnOnRed(channel) ? 200 : 500);
        } else if (reader.structured().isEqual(COLOR, GREEN)) {
            return responseService.publishHTTPResponse(reader, turnOnGreen(channel) ? 200 : 500);
        } else if (reader.structured().isEqual(COLOR, YELLOW)) {
            return responseService.publishHTTPResponse(reader, turnOnYellow(channel) ? 200 : 500);
        } else {
            return responseService.publishHTTPResponse(reader, 404);
        }
    }).includeRoutes(webRoute);
}
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) FogCommandChannel(com.ociweb.iot.maker.FogCommandChannel) HTTPResponseService(com.ociweb.gl.api.HTTPResponseService)

Example 2 with HTTPResponseService

use of com.ociweb.gl.api.HTTPResponseService in project GreenLightning by oci-pronghorn.

the class OpenCloseTestServer method declareBehavior.

@Override
public void declareBehavior(GreenRuntime runtime) {
    HTTPResponseService respClose = runtime.newCommandChannel().newHTTPResponseService();
    runtime.addRestListener((r) -> {
        HeaderWritable headers = (w) -> {
            w.write(HTTPHeaderDefaults.CONNECTION, "close");
        };
        return respClose.publishHTTPResponse(r, headers, null, Writable.NO_OP);
    }).includeRoutes(alwaysCloseRoute);
    HTTPResponseService respOpen = runtime.newCommandChannel().newHTTPResponseService();
    runtime.addRestListener((r) -> {
        respOpen.publishHTTPResponse(r, 200);
        return true;
    }).includeRoutes(neverCloseRoute);
}
Also used : HTTPHeaderDefaults(com.ociweb.pronghorn.network.config.HTTPHeaderDefaults) GreenRuntime(com.ociweb.gl.api.GreenRuntime) HTTPResponseService(com.ociweb.gl.api.HTTPResponseService) Writable(com.ociweb.gl.api.Writable) HeaderWritable(com.ociweb.pronghorn.network.http.HeaderWritable) GreenFramework(com.ociweb.gl.api.GreenFramework) GreenApp(com.ociweb.gl.api.GreenApp) HeaderWritable(com.ociweb.pronghorn.network.http.HeaderWritable) HTTPResponseService(com.ociweb.gl.api.HTTPResponseService)

Example 3 with HTTPResponseService

use of com.ociweb.gl.api.HTTPResponseService in project GreenLightning by oci-pronghorn.

the class AllRoutesExample2 method declareBehavior.

@Override
public void declareBehavior(GreenRuntime runtime) {
    final GreenCommandChannel cmd = runtime.newCommandChannel();
    final HTTPResponseService responseService = cmd.newHTTPResponseService();
    RestListener listener = new RestListener() {

        @Override
        public boolean restRequest(HTTPRequestReader request) {
            // TODO: this route is the wrong value
            int id = request.getRouteId();
            System.out.println(id);
            return responseService.publishHTTPResponse(request, 200);
        }
    };
    runtime.addRestListener(listener).includeAllRoutes();
}
Also used : RestListener(com.ociweb.gl.api.RestListener) HTTPResponseService(com.ociweb.gl.api.HTTPResponseService) HTTPRequestReader(com.ociweb.gl.api.HTTPRequestReader) GreenCommandChannel(com.ociweb.gl.api.GreenCommandChannel)

Example 4 with HTTPResponseService

use of com.ociweb.gl.api.HTTPResponseService in project GreenLightning by oci-pronghorn.

the class EchoExampleApp method declareBehavior.

@Override
public void declareBehavior(GreenRuntime runtime) {
    HTTPResponseService resp = runtime.newCommandChannel().newHTTPResponseService();
    runtime.addRestListener("restListener", (r) -> {
        HeaderWritable headers = new HeaderWritable() {

            @Override
            public void write(HeaderWriter writer) {
                writer.write(HTTPHeaderDefaults.DNT, "true");
                writer.write(HTTPHeaderDefaults.STRICT_TRANSPORT_SECURITY, "hello");
            }
        };
        Writable writable = new Writable() {

            @Override
            public void write(ChannelWriter writer) {
            // no response
            }
        };
        return resp.publishHTTPResponse(r, headers, null, writable);
    }).includeAllRoutes();
}
Also used : HTTPHeaderDefaults(com.ociweb.pronghorn.network.config.HTTPHeaderDefaults) GreenRuntime(com.ociweb.gl.api.GreenRuntime) HTTPResponseService(com.ociweb.gl.api.HTTPResponseService) Writable(com.ociweb.gl.api.Writable) HeaderWriter(com.ociweb.pronghorn.network.http.HeaderWriter) HeaderWritable(com.ociweb.pronghorn.network.http.HeaderWritable) GreenFramework(com.ociweb.gl.api.GreenFramework) ChannelWriter(com.ociweb.pronghorn.pipe.ChannelWriter) GreenApp(com.ociweb.gl.api.GreenApp) HeaderWritable(com.ociweb.pronghorn.network.http.HeaderWritable) Writable(com.ociweb.gl.api.Writable) HeaderWritable(com.ociweb.pronghorn.network.http.HeaderWritable) HeaderWriter(com.ociweb.pronghorn.network.http.HeaderWriter) HTTPResponseService(com.ociweb.gl.api.HTTPResponseService) ChannelWriter(com.ociweb.pronghorn.pipe.ChannelWriter)

Aggregations

HTTPResponseService (com.ociweb.gl.api.HTTPResponseService)4 GreenApp (com.ociweb.gl.api.GreenApp)2 GreenCommandChannel (com.ociweb.gl.api.GreenCommandChannel)2 GreenFramework (com.ociweb.gl.api.GreenFramework)2 GreenRuntime (com.ociweb.gl.api.GreenRuntime)2 Writable (com.ociweb.gl.api.Writable)2 HTTPHeaderDefaults (com.ociweb.pronghorn.network.config.HTTPHeaderDefaults)2 HeaderWritable (com.ociweb.pronghorn.network.http.HeaderWritable)2 HTTPRequestReader (com.ociweb.gl.api.HTTPRequestReader)1 PubSubService (com.ociweb.gl.api.PubSubService)1 RestListener (com.ociweb.gl.api.RestListener)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