private void ExecuteAlgorithms(DataSet Set) { Console.WriteLine("Executing each algorithm on the data"); //foreach algorithms that exists ... //foreach (ob //write the data to the stream ... StreamWriter logger = new StreamWriter("Log.csv", false); foreach (DataRow row in Set.Tables[0].Rows) { currentID = row["node"].ToString(); Node CurrentBlindNode; //Positioning.Point pos = new Positioning.Point(0, 0); Point pos = new Point(0, 0); if (!BlindNodes.Exists(ExistsNode)) { BlindNodes.Add(new Node(row["node"].ToString(), MyDb, row["ANode"].ToString(), Convert.ToDouble(row["RSSI"].ToString()))); //Console.WriteLine("Added new BN to be positioned\n\n\n"); CurrentBlindNode = BlindNodes.Find(ExistsNode); } else { CurrentBlindNode = BlindNodes.Find(ExistsNode); CurrentBlindNode.AddAnchor(row["ANode"].ToString(), Convert.ToDouble(row["RSSI"].ToString())); } logger.Write(row["idlocalization"].ToString() + ","); logger.Write(row["time"].ToString() + ","); logger.Write(currentID + ","); logger.Write(CurrentBlindNode.Anchors.Count + ","); //centroid localization logger.Write("CL,"); try { pos = CentroidLocalization.CalculatePosition(CurrentBlindNode); logger.Write(pos.x + ","); logger.Write(pos.y + ","); } catch (ApplicationException) { logger.Write("null,"); logger.Write("null"); } //min-max logger.Write("Min-Max,"); try { pos = MinMaxSimpleModel.CalculatePosition(CurrentBlindNode); logger.Write(pos.x + ","); logger.Write(pos.y + ","); } catch (ApplicationException) { logger.Write("null,"); logger.Write("null"); } logger.WriteLine(""); logger.Flush(); } logger.Close(); }
/// <summary> /// Sub-function that takes a dataset which holds (a number of) sensormeasurements and saves these to the database. /// Note: the MAC-address (SunSpot) or the Unique ID (TelosB) has to be in the database or the measurement will be ignored and not saved tot he database. /// </summary> /// <param name="Set">DataSet with the table that contains the sensormeasurement.</param> /// <returns>The last UID of the inserted row (which means an int >= 1) anything else means the last insert failed.</returns> private DataSet AddSensorMeasurements(DataSet Set) { DataSet returnSet = new DataSet(); DataSet tempSet = new DataSet(); double temp; int tempint; string cmd = ""; foreach (DataRow row in Set.Tables[0].Rows) //Run through every sensor in the xml-message { string CheckIfNodeInDB = "call getNodeID('" + row["ID"].ToString() + "');"; try { if (MySQLAllowedConn) { tempSet = MySQLConn.Query(CheckIfNodeInDB); } } catch (Exception e_mysql) { SocketServer.LogError(e_mysql, "LogServer.txt"); } #region add node to DB if (tempSet.Tables[0].Rows.Count < 1) //If the sensor is in the database, proceed with inserting the new measurements into the database. { string addTelosb = "call addSensorToDBTelosb('" + row["ID"] + "'," + "2);"; try { //Update the MySQL-database (if it is available) if (MySQLAllowedConn) { returnSet = MySQLConn.Query(addTelosb); } Console.WriteLine("Query OK"); } catch (Exception e_mysql) { Console.WriteLine("Failed to add node to the DB"); SocketServer.LogError(e_mysql, "LogServer.txt"); //return Set; } } #endregion if (Set.DataSetName == "SensorMeasurements") { int TimeSecs; if (int.TryParse(row["Time"].ToString(), out TimeSecs)) { row["Time"] = ConvertUnixToLocalTimeStamp(TimeSecs); //SunSpot sends the timestamp as unix-timestamp, convert it to normal timestamp. } //Create the command that we send to the database to insert the new row. cmd = "call addSensorMeasurements('" + row["ID"] + "','" + row["Time"] + "','" + row["Temp"] + "','" + row["Light"] + "'," + ((double.TryParse(row["Humidity"].ToString(), out temp)) ? row["Humidity"] : "null") + "," + ((int.TryParse(row["Power"].ToString(), out tempint)) ? row["Power"] : "null") + "," + ((double.TryParse(row["TiltX"].ToString(), out temp)) ? row["TiltX"] : "null") + "," + ((double.TryParse(row["TiltY"].ToString(), out temp)) ? row["TiltY"] : "null") + "," + ((double.TryParse(row["TiltZ"].ToString(), out temp)) ? row["TiltZ"] : "null") + "," + ((double.TryParse(row["AccX"].ToString(), out temp)) ? row["AccX"] : "null") + "," + ((double.TryParse(row["AccY"].ToString(), out temp)) ? row["AccY"] : "null") + "," + ((double.TryParse(row["AccZ"].ToString(), out temp)) ? row["AccZ"] : "null") + ",'" + ((int.TryParse(row["Button1"].ToString(), out tempint)) ? row["Button1"] : "0") + "'," + ((int.TryParse(row["Button2"].ToString(), out tempint)) ? row["Button2"] : "null") + ",'" + ((double.TryParse(row["LED1"].ToString(), out temp)) ? row["LED1"] : "null") + "','" + ((double.TryParse(row["LED2"].ToString(), out temp)) ? row["LED2"] : "null") + "','" + ((double.TryParse(row["LED3"].ToString(), out temp)) ? row["LED3"] : "null") + "','" + ((double.TryParse(row["LED4"].ToString(), out temp)) ? row["LED4"] : "null") + "','" + ((double.TryParse(row["LED5"].ToString(), out temp)) ? row["LED5"] : "null") + "','" + ((double.TryParse(row["LED6"].ToString(), out temp)) ? row["LED6"] : "null") + "','" + ((double.TryParse(row["LED7"].ToString(), out temp)) ? row["LED7"] : "null") + "','" + ((double.TryParse(row["LED8"].ToString(), out temp)) ? row["LED8"] : "null") + "'," + ((int.TryParse(row["Polling"].ToString(), out tempint)) ? row["Polling"] : "null") + ");"; Console.WriteLine("AddSensorMeasurements OK"); Console.WriteLine("WSNID is: \n" + row["ID"].ToString()); //#region NewSensorData //XElementExtended payload = new XElementExtended("Event", // new XElementExtended("ID", row["ID"].ToString()), // new XElementExtended("Type", "NewSensorData"), // new XElementExtended("Data", // new XElementExtended("Temperature", row["Temp"].ToString()), // new XElementExtended("Light", row["Light"].ToString()))); ////payload["ID"] = row["ID"].ToString(); ////payload["Type"] = "NewSensorData"; ////payload["Data"] = new XElementExtended(new XElementExtended("Temperature", row["Temp"].ToString()),new XElementExtended("Light", row["Light"].ToString())); //NewSensorDataEvent(this, new EventMessage("1", payload.ToString())); if (NewSensorDataEvent != null) { EventMessage EventData = new EventMessage(); EventData.EventSubscriptionId = "1"; EventData.EventType = "NewSensorData"; EventData.TagBlink["TagID"] = row["idnode"].ToString(); EventData.TagBlink["Temperature"] = row["temperature"].ToString(); EventData.TagBlink["Light"] = row["Light"].ToString(); NewSensorDataEvent(this, EventData); } //#endregion } else if (Set.DataSetName == "LocationMessage") { int TimeSecs; if (int.TryParse(row["Time"].ToString(), out TimeSecs)) { row["Time"] = ConvertUnixToLocalTimeStamp(TimeSecs); //SunSpot sends the timestamp as unix-timestamp, convert it to normal timestamp. } currentID = row["ID"].ToString(); Positioning.Point pos = new Positioning.Point(0, 0); Node CurrentBlindNode; if (!BlindNodes.Exists(ExistsNode)) { BlindNodes.Add(new Node(row["ID"].ToString(), MySQLConn, row["ANode"].ToString(), Convert.ToDouble(row["RSSI"].ToString()))); Console.WriteLine("Added new BN to be positioned\n\n\n"); CurrentBlindNode = BlindNodes.Find(ExistsNode); } else { CurrentBlindNode = BlindNodes.Find(ExistsNode); CurrentBlindNode.AddAnchor(row["ANode"].ToString(), Convert.ToDouble(row["RSSI"].ToString())); } //TODO: switch on the bulletlist or whatever you use to select the algorithm pos = CentroidLocalization.CalculatePosition(CurrentBlindNode); Console.WriteLine("Position succesfully calculated, x = {0}, y = {1}", pos.x.ToString(), pos.y.ToString()); //Create the command that we send to the database to insert the new row. cmd = "call addLocalizationData(" + ((int.TryParse(row["RSSI"].ToString(), out tempint)) ? row["RSSI"] : "null") + "," + pos.x.ToString() + "," + pos.y.ToString() + "," + ((int.TryParse(row["Z"].ToString(), out tempint)) ? row["Z"] : "null") + "," + row["ID"] + ",'" + row["Time"] + "'," + ((int.TryParse(row["ANode"].ToString(), out tempint)) ? row["ANode"] : "null") + ",'" + "Centroid Localization" + "');"; Console.WriteLine("Add location OK"); //#region NewPosition //conform to the RTLS ANSI API //XElementExtended payload = new XElementExtended("Event", // new XElementExtended("ID", row["ID"].ToString()), // new XElementExtended("Type", "NewPosition"), // new XElementExtended("Data", // new XElementExtended("X", row["X"].ToString()), // new XElementExtended("Y", row["Y"].ToString()))); //NewPositionEvent(this, new EventMessage("2" ,payload.ToString())); //#endregion } else if (Set.DataSetName == "StatusMessage") { int TimeSecs; if (int.TryParse(row["Time"].ToString(), out TimeSecs)) { row["Time"] = ConvertUnixToLocalTimeStamp(TimeSecs); //SunSpot sends the timestamp as unix-timestamp, convert it to normal timestamp. } //Create the command that we send to the database to insert the new row. cmd = "call addStatus(" + row["ID"] + ",'" + row["Time"] + "'," + row["Active"] + "," + row["AN"] + "," + ((int.TryParse(row["X"].ToString(), out tempint)) ? row["X"] : "null") + "," + ((int.TryParse(row["Y"].ToString(), out tempint)) ? row["Y"] : "null") + "," + ((int.TryParse(row["SampleRate"].ToString(), out tempint)) ? row["SampleRate"] : "null") + "," + ((int.TryParse(row["LocRate"].ToString(), out tempint)) ? row["LocRate"] : "null") + "," + ((int.TryParse(row["Leds"].ToString(), out tempint)) ? row["Leds"] : "null") + "," + ((int.TryParse(row["Power"].ToString(), out tempint)) ? row["Power"] : "null") + "," + ((int.TryParse(row["Frequency"].ToString(), out tempint)) ? row["Frequency"] : "null") + "," + ((int.TryParse(row["Conn"].ToString(), out tempint)) ? row["Conn"] : "null") + ");"; Console.WriteLine("Add Status OK"); } //try { //Update the MySQL-database (if it is available) if (MySQLAllowedConn) { returnSet = MySQLConn.Query(cmd); } Console.WriteLine("Query OK"); } } return(returnSet); }