/// <summary> /// method is used to calculate the position of the wifi adapter (client). Be aware of the possibly long break! /// </summary> /// <param name="client">WlanClient</param> /// <returns>Position</returns> public virtual Position findPosition(WlanClient client) { LinkedList <BlindMeasurement> measured = new LinkedList <BlindMeasurement>(); Dictionary <string, RssiCalcHelper> calcList = new Dictionary <string, RssiCalcHelper>(); Wlan.WlanBssEntry[] bsses = client.Interfaces[0].GetNetworkBssList(); int rssi = 0; for (int i = 0; i < this.findPositionCount; i++) { foreach (Wlan.WlanBssEntry bss in bsses) { rssi = bss.rssi; // signal strength string bssid = Converter.getBssid(bss); if (rssi > this.minimumRangeRssi || this.minimumRangeRssi == 0) { if (i != 0) { if (!calcList.ContainsKey(bssid)) { calcList.Add(bssid, new RssiCalcHelper(rssi)); } else if (calcList.ContainsKey(bssid)) { RssiCalcHelper tempHelper = calcList[bssid]; calcList.Remove(bssid); tempHelper.addRssi(rssi); calcList.Add(bssid, tempHelper); } Console.WriteLine("Measurement - AP: bssid=" + bssid + " rssi=" + rssi); } else { Console.WriteLine("1st measurement dropped!"); } } } Console.WriteLine(""); Thread.Sleep(this.scanPause); bsses = client.Interfaces[0].GetNetworkBssList(); client.Interfaces[0].Scan(); } foreach (KeyValuePair <string, RssiCalcHelper> keyValPair in calcList) { string bssid = keyValPair.Key; RssiCalcHelper tempRssiHelper = keyValPair.Value; double averageRssi = tempRssiHelper.getAverageRssi(); double averageFluctuation = tempRssiHelper.getAverageFluctuation(); measured.AddLast(new BlindMeasurement(bssid, averageRssi, averageFluctuation)); Console.WriteLine("Saved Middle value for: " + bssid + " rssi= " + averageRssi + " fluctuation= " + averageFluctuation); } List <SectorDifference> results = new List <SectorDifference>(); int counterMap = 0; foreach (KeyValuePair <int, Sector> keyValPair in this.accessPointsMap) { int sector = keyValPair.Key; Sector tempSector = keyValPair.Value; Console.WriteLine("Sector: " + sector); int counter = 0; Dictionary <string, AccessPoint> tempApMap = tempSector.getAccessPoints(); double result = 0; foreach (BlindMeasurement apData in measured) { char[] delimiter = new char[] { '|' }; string[] splitted = apData.getBssid().Split(delimiter, StringSplitOptions.RemoveEmptyEntries); apData.setBssid(splitted[0]); if (tempApMap.ContainsKey(apData.getBssid())) { AccessPoint apTemp = tempApMap[apData.getBssid()]; double tempResult = Math.Abs(apTemp.getRssi() - apData.getRssi()); if ( (this.standardError == false && tempResult <= apData.getAverageFluctuation()) || (this.standardError == true && tempResult <= this.errorBuffer) ) { tempResult = 0; } result += tempResult; Console.WriteLine("Result: bssid=" + apData.getBssid() + " map " + apTemp.getRssi() + " measured " + apData.getRssi() + " DIF: " + tempResult); counter++; } } counterMap++; double average = 0; if (counter == 0) { Console.WriteLine(""); } else { average = result / counter; Console.WriteLine("calculated range " + result + "/" + counter + " = " + average); results.Add(new SectorDifference(tempSector.getX(), tempSector.getY(), average)); } } Console.WriteLine("Results: "); results.Sort(delegate(SectorDifference s1, SectorDifference s2) { return(s1.getRssi().CompareTo(s2.getRssi())); }); Position tempPos = printFinalResult(results); Thread.Sleep(this.scanPause); return(tempPos); }
/// <summary> /// saves the current map into a file /// </summary> /// <param name="mapName">string</param> public virtual void saveMap(string mapName) { XmlTextWriter xml = new XmlTextWriter(mapName, null); try { //string json = JsonConvert.SerializeObject(this.accessPointsMap); //fileWriter.Write(json); xml.WriteStartDocument(); xml.WriteStartElement("map"); xml.WriteWhitespace(""); int sectorCounter = 0; // cycle through map foreach (KeyValuePair <int, Sector> keyValPair in this.accessPointsMap) { sectorCounter++; int maxSectors = this.accessPointsMap.Count; int sectorNumber = keyValPair.Key; Sector sector = keyValPair.Value; xml.WriteStartElement("sector"); xml.WriteStartElement("number"); xml.WriteString(sectorNumber.ToString()); xml.WriteEndElement(); xml.WriteStartElement("x"); xml.WriteString(sector.getX().ToString()); xml.WriteEndElement(); xml.WriteStartElement("y"); xml.WriteString(sector.getY().ToString()); xml.WriteEndElement(); Dictionary <string, AccessPoint> accessPointDictionary = sector.getAccessPoints(); int apCount = 0; foreach (KeyValuePair <string, AccessPoint> entryPair in accessPointDictionary) { string bssid = entryPair.Key; AccessPoint accessPoint = entryPair.Value; xml.WriteStartElement("accesspoint"); xml.WriteStartElement("bssid"); xml.WriteString(accessPoint.getBssid()); xml.WriteEndElement(); xml.WriteStartElement("rssi"); xml.WriteString(accessPoint.getRssi().ToString()); xml.WriteEndElement(); xml.WriteEndElement(); apCount++; } xml.WriteStartElement("stop"); xml.WriteString("stop"); xml.WriteEndElement(); xml.WriteEndElement(); } xml.WriteEndElement(); xml.WriteEndDocument(); } catch (Exception e) { Console.WriteLine(e.StackTrace); } finally { xml.Close(); } }