This project is read-only.

MicroFramework server side


The code below process the incoming request and set the Fez Panda II/OutputPort at the level required.

using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.Hardware;
using System.Threading;
using System.Text;

using Button = GHIElectronics.NETMF.FEZ.FEZ_Components.Button;
using System.Collections;

namespace FEZ_PandaApplication1
{
    public class Program : IServiceSensor
    {
        private object sync;
        private OutputPort rele0;
        private OutputPort rele1;
        private OutputPort rele2;
        private short runmode;

        // ------------------------------------  

        public static void Main()
        {
            Program p = new Program();
            try
            {
                p.StartProgram();
            }
            catch (Exception ex)
            {
                string mess = ex.Message;
            }
        }

        // ------------------------------------ 

        private void StartProgram()
        {
            sync = new object();
            ConfigureSensor();
            WebServer server = new WebServer(13000, this);
            server.NotifyParamEvent += new NotifyParamEventHandler(NotifyParam);
            server.InitListener();
            runmode = 1;
        }

        private void ConfigureSensor()
        {
            rele0 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
            rele1 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di1, false);
            rele2 = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.Di3, false);
            //bool value1 = rele0.Read();
            //rele0.Write(true);
            //bool value2 = rele0.Read();
            //rele0.Write(false);
            //bool value3 = rele0.Read();
        }     

        private void NotifyParam(object sender, string command, Hashtable param)
        {           
            lock (sync)
            {
                string number = (string)param["number"];
                if (command == "turnOn")
                {
                    bool status = true;
                    if (number == "1")
                        rele0.Write(status);
                    if (number == "2")
                        rele1.Write(status);
                    if (number == "3")
                        rele2.Write(status);
                }
                if (command == "turnOff")
                {
                    bool status = false;
                    if (number == "1")
                        rele0.Write(status);
                    if (number == "2")
                        rele1.Write(status);
                    if (number == "3")
                        rele2.Write(status);
                }
            }
        }

        public object GetSensorValue(string command, Hashtable param)
        {
            object value = null;
            string number = (string)param["number"];
            if (command == "status")
            {
                if (number == "1")
                    value = rele0.Read();
                if (number == "2")
                    value = rele1.Read();
                if (number == "3")
                    value = rele2.Read();
            }
            return value;
        }
    }
}


using System;
using System.Text;
using Microsoft.SPOT;
using System.Threading;
using System.Collections;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using System.Net.Sockets;
using System.IO;

// abilitare se con si e' con scheda reale
using GHIElectronics.NETMF.Net.NetworkInformation;
using GHIElectronics.NETMF.Net;
// abilitare se con si e' con scheda reale

using Socket = System.Net.Sockets.Socket;

namespace FEZ_PandaApplication1
{
    public class WebRequest2 : WebRequest1
    {
        public WebRequest2(WebServer value1, HttpListenerContext value2)
            : base(value1, value2)
        {
           
        }

        protected override void OnProcessRequestG(string url, 
                 string input, Hashtable table)
        {
            string number = (string)table["number"];
            string command = (string)table["command"]; 
            if ( command == "status"  )
            {
                object value = GetSensorValue(command, table);
                GetResultCmd1(value.ToString(), table);
            }
            if ( command == "turnOn"  )
            {
                OnNotifyParam(command, table);
                GetResultCmd2(table);
            }
            if ( command == "turnOff" )
            {
                
                OnNotifyParam(command, table);
                GetResultCmd3(table);
            }           
        }

        private void GetResultCmd1(string value, Hashtable table)
        {
            string param = value + ": " + DateTime.Now.ToString();
            string fcallback = (string)table["callback"];
            if (fcallback != null && fcallback != "")
                param = fcallback + "({ result:'" + param + "'})";
            SendResponse(param);
        }

        private void GetResultCmd2(Hashtable table)
        {
            string param = "on  -> " + DateTime.Now.ToString();
            string fcallback = (string)table["callback"];
            if (fcallback != null && fcallback != "")
                param = fcallback + "({ result:'" + param + "'})";
            SendResponse(param);
        }

        private void GetResultCmd3(Hashtable table)
        {
            string param = "off -> " +  DateTime.Now.ToString();
            string fcallback = (string)table["callback"];
            if (fcallback != null && fcallback != "")
                param = fcallback + "({ result:'" + param + "'})";
            SendResponse(param);
        }
    }
}



Windows Phone/Silverlight and MicroFramework (How create a remote control)
Windows Phone/PhoneGap and MicroFramework (How create a remote control)
MicroFramework server side (How create a remote control)
Windows Phone + Micro Framework (using tile push notification)


Last edited Aug 6, 2012 at 6:41 PM by mastefano64, version 9

Comments

No comments yet.