public void ClusteredTriLaterationTest() { MySQLClass MyDB = new MySQLClass("DRIVER={MySQL ODBC 3.51 Driver};SERVER=localhost;DATABASE=senseless;UID=root;PASSWORD=admin;OPTION=3;"); Node BlindNode = new Node("Test", MyDB); BlindNode.NewAnchor("1", 50.00, 0.00, 0.00, 1); BlindNode.NewAnchor("2", 50.00, 2.00, 0.00, 1); BlindNode.NewAnchor("3", 50.00, 2.00, 2.00, 1); Node.FilterMethod filterMethod = RangeBasedPositioning.AverageFilter; Point expected = new Point(1,1); Point actual; actual = ClusterTrilateration.CalculatePosition(BlindNode, filterMethod, false); //Assert.AreEqual(expected, actual); Assert.Inconclusive("Verify the correctness of this test method."); }
/// <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; }
/// <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); }
/// <summary> /// Parses the blind node data /// Adds the node to BlindNodes /// Updates the RSS /// Performs the requested localization algorithm /// Forms the query to put the RSS and postion in the database /// Possibly throws an event as described in the event section /// </summary> /// <param name="row"></param> /// <param name="nodeId"></param> /// <returns></returns> private string ParseBlind(DataRow row, string nodeId) { string cmd, currentID; int tempint; 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 CurrentNode; if (!BlindNodes.Exists(BN => BN.WsnIdProperty == currentID)) { BlindNodes.Add(new Node(row["ID"].ToString(), MySQLConn)); Console.WriteLine("Added new BN to be positioned\n\n\n"); } CurrentNode = BlindNodes.Find(BN => BN.WsnIdProperty == currentID); CurrentNode.UpdateAnchors(row["ANode"].ToString(), Convert.ToDouble(row["RSSI"].ToString()), Convert.ToInt32(row["VANs"]), DateTime.Now); //TODO: check if automatically updated CurrentNode = BlindNodes.Find(BN => BN.WsnIdProperty == currentID); Node.FilterMethod myFilter = new Node.FilterMethod(RangeBasedPositioning.MedianFilter);; Node.RangingMethod myRanging; if (SelectedCalibration != "Disabled") myRanging = new Node.RangingMethod(RangeBasedPositioning.Ranging); else myRanging = new Node.RangingMethod(RangeBasedPositioning.DefaultRanging); switch (SelectedFilter) { case "Median": myFilter = new Node.FilterMethod(RangeBasedPositioning.MedianFilter); break; case "Average": myFilter = new Node.FilterMethod(RangeBasedPositioning.AverageFilter); break; case "NoFilter": myFilter = new Node.FilterMethod(RangeBasedPositioning.NoFilter); break; } switch (SelectedAlgorithm) { case "CentroidLocalization": //pos = CentroidLocalization.CalculatePosition(CurrentNode); pos = CentroidLocalization.CalculatePosition(CurrentNode); break; case "MinMax": pos = MinMax.CalculatePosition(CurrentNode, myFilter, myRanging, UseMultihop); break; case "Trilateration": pos = ExtendedTrilateration.CalculatePosition(CurrentNode, myFilter, myRanging, UseMultihop); break; case "ExtendedTrilateration": pos = ExtendedTrilateration.CalculatePosition(CurrentNode, myFilter, myRanging, UseMultihop); break; case "ExtendedMinMax": pos = MinMaxExtended.CalculatePosition(CurrentNode, myFilter, myRanging, UseMultihop); break; case "WeightedCentroidLocalization": pos = WCL.CalculatePosition(CurrentNode, myFilter); break; case "LSTrilateration": pos = LSTrilateration.CalculatePosition(CurrentNode, myFilter, myRanging, UseMultihop); break; } if (pos != null) Console.WriteLine("Position was calculated for node: " + row["ID"] + " X = " + pos.x.ToString() + " Y = " + pos.y.ToString()); else { Console.WriteLine("Position for node: " + row["ID"] + " could not be calculated"); if (CurrentNode.Anchors.Count < 3) Console.WriteLine("Reason: too few anchor nodes!: " + CurrentNode.Anchors.Count.ToString()); else Console.WriteLine("Unspecified error"); } //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") + ","; if (pos != null) cmd += pos.x.ToString().Replace(',','.') + ", " + pos.y.ToString().Replace(',','.') + ", "; else cmd += "null, null, "; cmd += ((int.TryParse(row["Z"].ToString(), out tempint)) ? row["Z"] : "null") + "," + row["ID"] + ",'" + row["Time"] + "'," + ((int.TryParse(row["ANode"].ToString(), out tempint)) ? row["ANode"] : "null") + ",'" + SelectedAlgorithm + "');"; //add the position to the position table AddPosition(row, pos, 0); #region LocationUpdated if (LocationUpdated != null && pos != null) { EventMessage EventData = new EventMessage(); //EventData.EventType = "LocationUpdated"; EventData.TagBlink["TagID"] = nodeId; EventData.TagBlink["Accuracy"] = WsnEngine.CheckMapBounds(ref pos.x, ref pos.y, "2"); EventData.TagBlink["MapID"] = "WsnEngine1map2"; EventData.TagBlink["X"] = pos.x.ToString(); EventData.TagBlink["Y"] = pos.y.ToString(); LocationUpdated(this, EventData); } #endregion return cmd; }