コード例 #1
0
        static public string CreateReplyMessage(DataClasses.ControllerData controllerdata)
        {
            string replymessage = "";

            DateTime ontime      = DatabaseCalls.GetNextRelayOnTime(controllerdata.UID);
            long     ontime_unix = new DateTimeOffset(ontime).ToUnixTimeSeconds();

            replymessage += "<TimeOn=" + ontime_unix.ToString() + ">";

            DateTime offtime      = DatabaseCalls.GetNextRelayOffTime(controllerdata.UID);
            long     offtime_unix = new DateTimeOffset(offtime).ToUnixTimeSeconds();

            replymessage += "<TimeOff=" + offtime_unix.ToString() + ">";

            string newstate = DatabaseCalls.GetNewRelayState(controllerdata.UID);

            if (newstate != "") //ensure content
            {
                replymessage += "<RelayState=" + newstate + ">";
            }
            else
            {
                Console.WriteLine("No New State");
            }

            return(replymessage);
        }
コード例 #2
0
        internal static void AddNewControllerDataToDatabase(DataClasses.ControllerData controllerdata)
        {
            using (SqlConnection myConnection = getDatabaseConnection())
            {
                using (SqlCommand myCommand = new SqlCommand())
                {
                    string cmdString = "INSERT INTO ControllerDataTable " +
                                       "(UID,RelayState,Temperature,Power) VALUES (@uid, @relaystate, @temperature, @power)";
                    myCommand.Connection  = myConnection;
                    myCommand.CommandText = cmdString;
                    myCommand.Parameters.AddWithValue("@uid", controllerdata.UID);
                    myCommand.Parameters.AddWithValue("@relaystate", controllerdata.RelayState);
                    myCommand.Parameters.AddWithValue("@temperature", controllerdata.Temperature);
                    myCommand.Parameters.AddWithValue("@power", controllerdata.Power);

                    try
                    {
                        myCommand.ExecuteNonQuery();
                    }
                    catch (SqlException error)
                    {
                        Console.WriteLine("Error occured when adding new Controller Data to the DB: " + error.Message);
                    }
                }
            }
        }
コード例 #3
0
        static public DataClasses.ControllerData ParseMessageData(string message)
        {
            //ex data : <UID = JAMESESP8266>< Relay = 0 >< Temperature = 85 >< Power = 88 >< EOM >
            DataClasses.ControllerData newcontrollerdata = new DataClasses.ControllerData();

            try
            {
                char[]   delimiterChars = { '<', '>', };
                string[] rawdatastrings = message.Split(delimiterChars);

                //Set UID
                string rawuid = Array.FindLast(rawdatastrings, s => s.StartsWith("UID"));
                newcontrollerdata.UID = rawuid.Remove(0, rawuid.LastIndexOf('=') + 1);

                //Set RelayState
                string rawrelaystate = Array.FindLast(rawdatastrings, s => s.StartsWith("Relay"));
                newcontrollerdata.RelayState = int.Parse(rawrelaystate.Remove(0, rawrelaystate.LastIndexOf('=') + 1));

                //Set Temperature
                string rawtemperature = Array.FindLast(rawdatastrings, s => s.StartsWith("Temperature"));
                newcontrollerdata.Temperature = int.Parse(rawtemperature.Remove(0, rawtemperature.LastIndexOf('=') + 1));

                //Set Power Usage
                string rawpower = Array.FindLast(rawdatastrings, s => s.StartsWith("Power"));
                newcontrollerdata.Power = int.Parse(rawpower.Remove(0, rawpower.LastIndexOf('=') + 1));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            return(newcontrollerdata);
        }
コード例 #4
0
            public static void ReadCallback(IAsyncResult ar)
            {
                string content = string.Empty;

                // Retrieve the state object and the handler socket
                // from the asynchronous state object.
                StateObject state   = (StateObject)ar.AsyncState;
                Socket      handler = state.workSocket;

                try
                {
                    // Read data from the client socket.
                    int bytesRead = handler.EndReceive(ar);

                    if (bytesRead > 0)
                    {
                        // There  might be more data, so store the data received so far.
                        state.sb.Append(Encoding.ASCII.GetString(
                                            state.buffer, 0, bytesRead));

                        // Check for end-of-message tag. If it is not there, read
                        // more data.
                        content = state.sb.ToString();
                        if (content.IndexOf("<EOM>") > -1)
                        {
                            // All the data has been read from the
                            // client. Display it on the console.
                            Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
                                              content.Length, content);

                            //Parse Message Data
                            DataClasses.ControllerData newcontrollerdata = Messaging.ParseMessageData(content);

                            //Store Message Data
                            Messaging.StoreControllerData(newcontrollerdata);

                            //Create Reply Message to send back to client
                            string replymessage = Messaging.CreateReplyMessage(newcontrollerdata);

                            Console.WriteLine("Returned: " + replymessage);

                            Send(handler, replymessage);
                        }
                        else
                        {
                            // Not all data received. Get more.
                            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                                                 new AsyncCallback(ReadCallback), state);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                    Send(handler, "Nope");
                }
            }
コード例 #5
0
 static public void StoreControllerData(DataClasses.ControllerData controllerdata)
 {
     DatabaseCalls.AddNewControllerDataToDatabase(controllerdata);
 }