public int AddMessageToTable(MessageObject stateobj) { try { string Add = "insert into MessageTable (NodeID, ttl, State_1, State_2, State_3, State_4, State_5, State_6, State) values (" + stateobj.nodeid + "," + stateobj.ttl + ", \"" + stateobj.statetype1 + " \", \"" + stateobj.statetype2 + "\", \"" + stateobj.statetype3 + "\", \"" + stateobj.statetype4 + " \" , \"" + stateobj.statetype5 + " \", \"" + stateobj.statetype6 + " \", \"" + stateobj.state + " \")"; Console.WriteLine(Add); SQLiteCommand command = new SQLiteCommand(Add, SQLiteConnection); command.ExecuteNonQuery(); return 1; } catch (SQLiteException e) { Console.WriteLine(e.Message); return 0; } }
private void testFunction() { Data data = new Data(); data.AddNode(1, 1, "192.168.1.1", "LEDBlinky"); // data.AddState(0, "0", 0, 0); int ti = data.AddState(1, "LED", 0, 0); Console.WriteLine("addstate success = " + ti.ToString()); MessageObject testobj = new MessageObject(); testobj.nodeid = 1; testobj.statetype1 = "1"; testobj.state = "on"; data.AddMessageToTable(testobj); MessageObject[] obj = data.GetMessage(1); string t = obj[0].Json(data); Console.WriteLine(t); }
public MessageObject[] GetMessage(int NodeID) { // returns an array of messages // calcaute the amount of results to know array size string count = "select count(*) from MessageTable where NodeID = " + NodeID.ToString(); SQLiteCommand command = new SQLiteCommand(count, SQLiteConnection); SQLiteDataReader reader = command.ExecuteReader(); reader.Read(); int arraysize = Int32.Parse(reader[0].ToString()); //create object MessageObject[] returnobj = new MessageObject[arraysize]; string getMessages = "select * from MessageTable where NodeID = " + NodeID.ToString(); SQLiteCommand commandobj = new SQLiteCommand(getMessages, SQLiteConnection); SQLiteDataReader objReader = commandobj.ExecuteReader(); int i = 0; while (objReader.Read()) { MessageObject read = new MessageObject(); read.nodeid = Int32.Parse(objReader["NodeID"].ToString()); read.ttl = Int32.Parse(objReader["ttl"].ToString()); read.statetype1 = objReader["State_1"].ToString(); read.statetype2 = objReader["State_2"].ToString(); read.statetype3 = objReader["State_3"].ToString(); read.statetype4 = objReader["State_4"].ToString(); read.statetype5 = objReader["State_5"].ToString(); read.statetype6 = objReader["State_6"].ToString(); read.state = objReader["State"].ToString(); returnobj[i] = read; i++; } return returnobj; }
// Called when a URL is requested private void Callback(IAsyncResult result) { HttpListenerContext context = null; HttpListenerResponse response = null; HttpListener listener = (HttpListener)result.AsyncState; if (!listener.IsListening) return; context = listener.EndGetContext(result); response = context.Response; AsyncProcessing(listener); string UriRequest = context.Request.Url.AbsoluteUri; string LocalPathRequest = context.Request.Url.LocalPath; HTTPGetArgumentParse ParsedURI = new HTTPGetArgumentParse(UriRequest); byte[] buffer = null; if (LocalPathRequest.Length == 7 && LocalPathRequest.Substring(0, 7).Equals("/a.crap")) { if (ParsedURI.getType() == 0) { // Command recieved sendRTS(ParsedURI.getNodeID()); int numberOfParameters = ParsedURI.getParameterList().Length; MessageObject recievedMessage = new MessageObject(ParsedURI.getNodeID(), ParsedURI.getParameter("st1"), ParsedURI.getParameter("st2"), ParsedURI.getParameter("st3"), ParsedURI.getParameter("st4"), ParsedURI.getParameter("st5"), ParsedURI.getParameter("st6"), ParsedURI.getParameter("state")); dataObject.AddMessageToTable(recievedMessage); } else if (ParsedURI.getType() == 1) { // Update recieved } // Put the file in the buffer, and send to the client buffer = Encoding.UTF8.GetBytes("<html><body><h1>NodeID: " + ParsedURI.getNodeID().ToString() + "</h></body></html>"); mainViewForm.Invoke(addToLogDelegate, "Legitimate request made."); } else { string page = Directory.GetCurrentDirectory() + "\\accessdenied.html"; // Read file TextReader tr = new StreamReader(page); string msg = tr.ReadToEnd(); // Put the file in the buffer buffer = Encoding.UTF8.GetBytes(msg); mainViewForm.Invoke(addToLogDelegate, "Access denied page served."); } // Send the buffer to the client response.ContentLength64 = buffer.Length; Stream st = response.OutputStream; st.Write(buffer, 0, buffer.Length); context.Response.Close(); }