.NET Gadgeteer Web Services; Picture, Audio, Application

This example uses the GHI Electronics Ethernet_J11D module to set up and run three .NET Gadgeteer Web services that return client Web requests for a picture, an audio file, and an .rtf WordPad application file.

<This sample has been updated for new API.   It demonstrates initialization of Web events using WebServer.SetUpWebEvent.>

The Web services are initialized in the GTM.Module.NetworkModule.NetworkEventHandler as shown in the following code segment.

    void ethernet_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
    {
                    Debug.Print("Connected to network.");

            webEventAudio = WebServer.SetupWebEvent("audio");
            webEventAudio.WebEventReceived += new WebEvent.ReceivedWebEventHandler(webEventAudio_WebEventReceived);

            webEventApplication = WebServer.SetupWebEvent("application");
            webEventApplication.WebEventReceived +=
                new WebEvent.ReceivedWebEventHandler(webEventApplication_WebEventReceived);

            webEventPicture = WebServer.SetupWebEvent("picture");
            webEventPicture.WebEventReceived +=
                new WebEvent.ReceivedWebEventHandler(webEventPicture_WebEventReceived);

            Debug.Print("IP address: " + ethernet.NetworkSettings.IPAddress);
            WebServer.StartLocalServer(ethernet.NetworkSettings.IPAddress, 80);
            led.TurnGreen();
    }

The following screen capture video shows results of Web requests using REST syntax as in the URLs:

The Picture and Application services get files from storage on an SD Card and get System.IO.FileStream objects, read the bytes, and return them in the GTM.Module.NetworkModule.Responder object. The GTM.Module.NetworkModule.Responder.Respond is overloaded. This application uses the version: void Respond(byte[] data, string ContentType), which requires reading the byte array from the stream as shown in the following code from the Application Web service.

    void webEventApplication_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
    {
        if (sdCard.IsCardMounted)
        {
            GT.StorageDevice storage = sdCard.GetStorageDevice();

            using (FileStream stream = storage.OpenRead("\\Application\\ProgramCode.rtf"))
            {
                try
                {
                    byte[] bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, (int)stream.Length);
                    responder.Respond(bytes, "application/rtf");
                }
                catch (Exception ex)
                {
                    responder.Respond("Exception: " + ex.Message +
                            "  Inner Exception: " + ex.InnerException);
                }
            }
        }
    }

The Audio Web service uses the simpler overload of GTM.Module.NetworkModule.Responder.Respond, which can be used to deliver audio files of type .mp3. The memory available on the mainboard limits audio files to about 500KB. The following code runs Audio Web service.

    void webEventAudio_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
    {
        if (sdCard.IsCardMounted)
        {
            GT.StorageDevice storage = sdCard.GetStorageDevice();

            using (FileStream stream = storage.OpenRead("\\Audio\\500.mp3"))
            {
                try
                {
                    responder.Respond(stream);
                }
                catch (Exception ex)
                {
                    responder.Respond("Exception: " + ex.Message + "
                                 Inner Exception: " + ex.InnerException);
                }
            }
        }
    }

That’s about all there is to it. You can set up a .NET Gadgeteer Web service in about three lines of code. The following code is the complete Program.cs file for the application.

using System;
using Microsoft.SPOT;
using System.IO;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;

namespace WebServices
{
    public partial class Program
    {
        GT.Networking.WebEvent webEventAudio;
        GT.Networking.WebEvent webEventApplication;
        GT.Networking.WebEvent webEventPicture;

        void ProgramStarted()
        {
            ethernet.UseDHCP();
            ethernet.NetworkUp += new GTM.Module.NetworkModule.NetworkEventHandler(ethernet_NetworkUp);

            led.TurnRed();
            Debug.Print("Program Started");
            Debug.Print("IP Address: " + ethernet.NetworkSettings.IPAddress);
        }

        void ethernet_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("Connected to network.");

            webEventAudio = WebServer.SetupWebEvent("audio");
            webEventAudio.WebEventReceived += new WebEvent.ReceivedWebEventHandler(webEventAudio_WebEventReceived);

            webEventApplication = WebServer.SetupWebEvent("application");
            webEventApplication.WebEventReceived +=
                new WebEvent.ReceivedWebEventHandler(webEventApplication_WebEventReceived);

            webEventPicture = WebServer.SetupWebEvent("picture");
            webEventPicture.WebEventReceived +=
                new WebEvent.ReceivedWebEventHandler(webEventPicture_WebEventReceived);

            Debug.Print("IP address: " + ethernet.NetworkSettings.IPAddress);
            WebServer.StartLocalServer(ethernet.NetworkSettings.IPAddress, 80);

            led.TurnGreen();
        }

        void webEventPicture_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            if (sdCard.IsCardMounted)
            {
                GT.StorageDevice storage = sdCard.GetStorageDevice();

                using (FileStream stream = storage.OpenRead("\\bitmaps\\RedSpiderSm.bmp"))
                {
                    try
                    {
                        byte[] bytes = new byte[stream.Length];
                        stream.Read(bytes, 0, (int) stream.Length);
                        responder.Respond(bytes, "image/jpeg");
                    }
                    catch(Exception ex)
                    {
                         responder.Respond("Exception: " + ex.Message +
                                             "  Inner Exception: " + ex.InnerException);
                    }
                }
            }
        }

        void webEventApplication_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            if (sdCard.IsCardMounted)
            {
                GT.StorageDevice storage = sdCard.GetStorageDevice();

                using (FileStream stream = storage.OpenRead("\\Application\\ProgramCode.rtf"))
                {
                    try
                    {
                        byte[] bytes = new byte[stream.Length];
                        stream.Read(bytes, 0, (int)stream.Length);
                        responder.Respond(bytes, "application/rtf");
                    }
                    catch (Exception ex)
                    {
                        responder.Respond("Exception: " + ex.Message +
                                           "  Inner Exception: " + ex.InnerException);
                    }
                }
            }
        }

        void webEventAudio_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            if (sdCard.IsCardMounted)
            {
                GT.StorageDevice storage = sdCard.GetStorageDevice();

                using (FileStream stream = storage.OpenRead("\\Audio\\500.mp3"))
                {
                    try
                    {
                        responder.Respond(stream);
                    }
                    catch (Exception ex)
                    {
                        responder.Respond("Exception: " + ex.Message +
                                      "  Inner Exception: " + ex.InnerException);
                    }
                }
            }
        }
    }
}
Advertisements
  1. Un Server TCP con .NET Gadgeteer « Integral Design
  2. Un Server TCP con .NET Gadgeteer (Italiano) « Integral Design
  3. A TCP Server using .NET Gadgeteer « Integral Design

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: