示例#1
0
        /// <summary>
        /// Handles the CommandReceived event.
        /// </summary>
        private static void server_CommandReceived(object source, WebCommandEventArgs e)
        {
            Debug.Print("Command received:" + e.Command.CommandString);

            switch (e.Command.CommandString)
            {
                case "setled":
                    {
                        // Do you stuff with the command here. Set a led state, return a
                        // sampled value of an analog input, whatever.
                        // Use the ReturnString property to (optionally) return something
                        // to the web user.

                        // Read led state from command and set led state.
                        bool state = ( e.Command.Arguments[0].Equals("on") ? true : false);
                        onBoardLed.Write(state);

                        // Return feedback to web user.
                        e.ReturnString = "<html><body>You called SetLed with argument: " + e.Command.Arguments[0].ToString() + "</body></hmtl>";
                        break;
                    }
            }
        }
        /// <summary>
        /// Starts the server.
        /// </summary>
        private void StartServer()
        {
            using (var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                server.Bind(new IPEndPoint(IPAddress.Any, this.Port));
                server.Listen(1);

                while (!cancel)
                {
                    using (Socket connection = server.Accept())
                    {
                        if (connection.Poll(-1, SelectMode.SelectRead))
                        {
                            // Create buffer and receive raw bytes.
                            byte[] bytes = new byte[connection.Available];
                            int count = connection.Receive(bytes);

                            // Convert to string, will include HTTP headers.
                            string rawData = new string(Encoding.UTF8.GetChars(bytes));
                            WebCommand command = InterpretRequest(rawData);

                            if (command != null)
                            {
                                WebCommandEventArgs args = new WebCommandEventArgs(command);
                                if (CommandReceived != null)
                                {
                                    CommandReceived(this, args);
                                    byte[] returnBytes = Encoding.UTF8.GetBytes(args.ReturnString);
                                    connection.Send(returnBytes, 0, returnBytes.Length, SocketFlags.None);
                                }
                            }
                        }
                    }
                }
            }
        }
示例#3
0
        /// <summary>
        /// Starts the server.
        /// </summary>
        private void StartServer()
        {
            using (var server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
            {
                server.Bind(new IPEndPoint(IPAddress.Any, this.Port));
                server.Listen(1);

                while (!cancel)
                {
                    try
                    {
                        using (Socket connection = server.Accept())
                        {
                            if (connection.Poll(-1, SelectMode.SelectRead))
                            {
                                // Create buffer and receive raw bytes.
                                string rawData = "";
                                int bytesToRead = -1;
                                while (bytesToRead != 0)
                                {
                                    byte[] bytes = new byte[connection.Available];
                                    bytesToRead = connection.Receive(bytes);
                                    rawData += new string(Encoding.UTF8.GetChars(bytes));
                                }
                                if (rawData.Length == 0)
                                    continue;

                                // Convert to string, will include HTTP headers.
                                // string rawData = new string(Encoding.UTF8.GetChars(bytes));
                                WebCommand command = InterpretRequest(rawData);
                                if (command != null)
                                {
                                    WebCommandEventArgs args = new WebCommandEventArgs(command);
                                    if (CommandReceived != null)
                                    {
                                        try
                                        {
                                            CommandReceived(this, args);
                                            //byte[] returnBytes = Encoding.UTF8.GetBytes(args.ReturnString);
                                            //connection.Send(returnBytes, 0, returnBytes.Length, SocketFlags.None);
                                            SendResponse(connection, args.ReturnString);
                                        }
                                        catch (Exception ex)
                                        {
                                            SendResponse(connection, ex.Message, 500);
                                        }
                                    }
                                }
                                else
                                {
                                    SendResponse(connection, "Invalid Command Received", 404);
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.Print(e.Message);
                        Thread.Sleep(1000);
                    }
                }
            }
        }
示例#4
0
        /// <summary>
        /// Handles the CommandReceived event.
        /// </summary>
        private static void server_CommandReceived(object source, WebCommandEventArgs e)
        {
            try
            {
                timeOfLastWebCommandRx = DateTime.Now;

                if (e.Command.CommandString != "status")
                    Debug.Print("Command received:" + e.Command.CommandString);

                // Check for home page
                bool bReturnStatus = false;
                if (e.Command.CommandString == "")
                {
                    e.ReturnString = Resources.GetString(Resources.StringResources.index);
                }
                else if ((e.Command.CommandString == "setup") || (e.Command.CommandString == "setup.html"))
                {
                    e.ReturnString = Resources.GetString(Resources.StringResources.setup);
                }
                else if (e.Command.CommandString == "status")
                {
                    bReturnStatus = true;
                }
                else if (e.Command.CommandString == "addtag")
                {
                    TagInfo tagInfo = new TagInfo();
                    tagInfo.FromWebSerialized(e.Command.Arguments[0].ToString());
                    rfidTagDb.AddTagToDb(tagInfo.mTagId, tagInfo.mHolderName, tagInfo.mIsEnabled);
                    bReturnStatus = true;
                }
                else if (e.Command.CommandString == "testtag")
                {
                    TagInfo tagInfo = new TagInfo();
                    tagInfo.FromWebSerialized(e.Command.Arguments[0].ToString());
                    testTagId = tagInfo.mTagId;
                    testTimer = DateTime.Now;
                    bReturnStatus = true;
                }
                else if (e.Command.CommandString == "main-unlock")
                {
                    mainDoorStrike.Unlock(settingsDb.mainManualTimeout);
                    bReturnStatus = true;
                }
                else if (e.Command.CommandString == "main-lock")
                {
                    mainDoorStrike.Lock();
                    bReturnStatus = true;
                }
                else if (e.Command.CommandString == "inner-unlock")
                {
                    innerDoorStrike.Unlock(settingsDb.innerManualTimeout);
                    bReturnStatus = true;
                }
                else if (e.Command.CommandString == "inner-lock")
                {
                    innerDoorStrike.Lock();
                    bReturnStatus = true;
                }
                else if (e.Command.CommandString == "submit-settings")
                {
                    settingsDb.FromWebSerialized(e.Command.Arguments[0].ToString());
                    settingsDb.WriteValuesToFile();
                    Debug.Print("Submit settings " + e.Command.Arguments[0].ToString());
                    bReturnStatus = true;
                }
                else if (e.Command.CommandString == "get-settings")
                {
                    e.ReturnString = settingsDb.ToWebSerialized();
                }
                else if (e.Command.CommandString == "get-tags")
                {
                    e.ReturnString = rfidTagDb.ToWebHTML();
                }
                else if (e.Command.CommandString == "get-events")
                {
                    e.ReturnString = eventLogger.ToWebHTML(20, true);
                }
                else if (e.Command.CommandString == "get-debug")
                {
                    e.ReturnString = eventLogger.ToWebHTML(20, false);
                }

                if (e.ReturnString == null)
                    e.ReturnString = "";

                // Handle commands that require status to be returned
                if (bReturnStatus)
                {
                    e.ReturnString += GetStatusString();
                }

                if (e.ReturnString == "")
                {
                    e.ReturnString = "<html><body></body></html>";
                }

            }
            catch (Exception excp)
            {
                Debug.Print("Exception in server_CommandReceived " + excp.Message);
            }
        }