//This function gets the matched waypoint and region from each Clients, //then pass the waypoint and region information to Session public void PassMatchedWaypointAndRegionToSession(object sender, EventArgs args) { CleanAllMappingBeaconList(); RegionWaypointPoint matchedWaypointAndRegion = new RegionWaypointPoint(); matchedWaypointAndRegion._waypointID = (args as WaypointSignalEventArgs)._detectedRegionWaypoint ._waypointID; matchedWaypointAndRegion._regionID = (args as WaypointSignalEventArgs)._detectedRegionWaypoint ._regionID; _event.OnEventCall(new WaypointSignalEventArgs { _detectedRegionWaypoint = matchedWaypointAndRegion }); return; }
public List <WaypointBeaconsMapping> FindTheMappingOfWaypointAndItsBeacon (Guid regionGuid, List <Guid> waypoints) { List <WaypointBeaconsMapping> waypointBeaconsMappings = new List <WaypointBeaconsMapping>(); foreach (Guid waypointID in waypoints) { RegionWaypointPoint regionWaypointPoint = new RegionWaypointPoint(); regionWaypointPoint._regionID = regionGuid; regionWaypointPoint._waypointID = waypointID; List <Guid> beaconIDs = new List <Guid>(); beaconIDs = _navigationGraph.GetAllBeaconIDInOneWaypointOfRegion(regionGuid, waypointID); Dictionary <Guid, int> beaconThresholdMapping = new Dictionary <Guid, int>(); for (int i = 0; i < beaconIDs.Count(); i++) { beaconThresholdMapping.Add (beaconIDs[i], _navigationGraph.GetBeaconRSSIThreshold(regionGuid, beaconIDs[i]) ); } waypointBeaconsMappings.Add(new WaypointBeaconsMapping { _WaypointIDAndRegionID = regionWaypointPoint, _Beacons = beaconIDs, _BeaconThreshold = beaconThresholdMapping }); } return(waypointBeaconsMappings); }
public void DetectWaypoints() { List <BeaconSignalModel> removeSignalBuffer = new List <BeaconSignalModel>(); lock (_bufferLock) { removeSignalBuffer.AddRange( _beaconSignalBuffer.Where(c => c.Timestamp < DateTime.Now.AddMilliseconds(-1500))); foreach (var obsoleteBeaconSignal in removeSignalBuffer) { _beaconSignalBuffer.Remove(obsoleteBeaconSignal); } Dictionary <RegionWaypointPoint, List <BeaconSignal> > scannedData = new Dictionary <RegionWaypointPoint, List <BeaconSignal> >(); Dictionary <RegionWaypointPoint, int> signalAvgValue = new Dictionary <RegionWaypointPoint, int>(); Dictionary <RegionWaypointPoint, List <BeaconSignal> > correctData = new Dictionary <RegionWaypointPoint, List <BeaconSignal> >(); //In ibsclient, a waypoint has at least two beacon UUIDs, //We put all waypoint we get in scannedData //BeaconSignalModel beaconSignalModel1 = new BeaconSignalModel(); //BeaconSignalModel beaconSignalModel2 = new BeaconSignalModel(); //BeaconSignalModel beaconSignalModel3 = new BeaconSignalModel(); //BeaconSignalModel beaconSignalModel4 = new BeaconSignalModel(); //beaconSignalModel1.UUID = new Guid("00000000-0402-5242-3d64-2019010044d0"); //beaconSignalModel2.UUID = new Guid("00000000-0402-5242-3d64-2019010044d0"); //beaconSignalModel3.UUID = new Guid("00000000-0402-5242-3d64-2019010047b2"); //beaconSignalModel4.UUID = new Guid("00000000-0402-5242-3d64-2019010049bf"); //beaconSignalModel1.RSSI = -50; //beaconSignalModel2.RSSI = -60; // beaconSignalModel3.RSSI = -55; //beaconSignalModel4.RSSI = -33; //_beaconSignalBuffer.Add(beaconSignalModel1); //_beaconSignalBuffer.Add(beaconSignalModel2); //_beaconSignalBuffer.Add(beaconSignalModel3); //_beaconSignalBuffer.Add(beaconSignalModel4); foreach (BeaconSignalModel beacon in _beaconSignalBuffer) { foreach (WaypointBeaconsMapping waypointBeaconsMapping in _waypointBeaconsList) { foreach (Guid beaconGuid in waypointBeaconsMapping._Beacons) { if ((beacon.UUID.Equals(beaconGuid)) && (beacon.RSSI > (waypointBeaconsMapping._BeaconThreshold[beacon.UUID] - rssiOption))) { if (!scannedData.Keys.Contains(waypointBeaconsMapping._WaypointIDAndRegionID)) { scannedData.Add(waypointBeaconsMapping._WaypointIDAndRegionID, new List <BeaconSignal> { beacon }); } else { scannedData[waypointBeaconsMapping._WaypointIDAndRegionID].Add(beacon); } } } } } foreach (KeyValuePair <RegionWaypointPoint, List <BeaconSignal> > interestedBeacon in scannedData) { Dictionary <Guid, List <BeaconSignal> > tempSave = new Dictionary <Guid, List <BeaconSignal> >(); foreach (BeaconSignal beaconSignal in interestedBeacon.Value) { if (!tempSave.Keys.Contains(beaconSignal.UUID)) { tempSave.Add(beaconSignal.UUID, new List <BeaconSignal> { beaconSignal }); } else { tempSave[beaconSignal.UUID].Add(beaconSignal); } } if (tempSave.Keys.Count() >= _moreThanTwoIBeacon) { correctData.Add(interestedBeacon.Key, interestedBeacon.Value); } } foreach (KeyValuePair <RegionWaypointPoint, List <BeaconSignal> > calculateData in correctData) { //If a waypoint has at least two beacon UUIDs, //this waypoint might be our interested waypoint. if (calculateData.Value.Count() >= _moreThanTwoIBeacon) { //Sort the beacons by their Rssi calculateData.Value.Sort((x, y) => { return(x.RSSI.CompareTo(y.RSSI)); }); int avgSignal = 0; //If we have more than ten data, we remove the highest 10% //and the lowest 10%, and calculate their average //If we have not more than 10 data, //we just calculate their average if (calculateData.Value.Count() >= 10) { int min = Convert.ToInt32(scannedData.Count() * 0.1); int max = Convert.ToInt32(scannedData.Count() * 0.9); int minus = max - min; for (int i = min; i < max; i++) { avgSignal += calculateData.Value[i].RSSI; } avgSignal = avgSignal / minus; } else { foreach (BeaconSignal value in calculateData.Value) { avgSignal += value.RSSI; } avgSignal = avgSignal / scannedData.Count(); } signalAvgValue.Add(calculateData.Key, avgSignal); } } int tempValue = -100; bool haveThing = false; RegionWaypointPoint possibleRegionWaypoint = new RegionWaypointPoint(); //Compare all data we have, and get the highest Rssi Waypoint as our interested waypoint foreach (KeyValuePair <RegionWaypointPoint, int> calculateMax in signalAvgValue) { if (tempValue < calculateMax.Value) { possibleRegionWaypoint = new RegionWaypointPoint(); possibleRegionWaypoint = calculateMax.Key; haveThing = true; } tempValue = calculateMax.Value; } if (haveThing == true) { _event.OnEventCall(new WaypointSignalEventArgs { _detectedRegionWaypoint = possibleRegionWaypoint }); return; } } }
public void DetectWaypoints() { List <BeaconSignalModel> removeSignalBuffer = new List <BeaconSignalModel>(); lock (_bufferLock) { removeSignalBuffer.AddRange( _beaconSignalBuffer.Where(c => c.Timestamp < DateTime.Now.AddMilliseconds(-1500))); foreach (var obsoleteBeaconSignal in removeSignalBuffer) { _beaconSignalBuffer.Remove(obsoleteBeaconSignal); } Dictionary <RegionWaypointPoint, List <BeaconSignal> > scannedData = new Dictionary <RegionWaypointPoint, List <BeaconSignal> >(); Dictionary <RegionWaypointPoint, int> signalAvgValue = new Dictionary <RegionWaypointPoint, int>(); Dictionary <RegionWaypointPoint, List <BeaconSignal> > correctData = new Dictionary <RegionWaypointPoint, List <BeaconSignal> >(); if (watch.Elapsed.TotalMilliseconds >= _clockResetTime) { watch.Stop(); watch.Reset(); watch.Start(); Utility._ibeaconScan.StopScan(); Utility._ibeaconScan.StartScan(); } //Mapping the interested beacon and cancel the beacon which has //too low threshold foreach (BeaconSignalModel beacon in _beaconSignalBuffer) { foreach (WaypointBeaconsMapping waypointBeaconsMapping in _waypointBeaconsList) { foreach (Guid beaconGuid in waypointBeaconsMapping._Beacons) { if ((beacon.UUID.Equals(beaconGuid)) && (beacon.RSSI > (waypointBeaconsMapping. _BeaconThreshold[beacon.UUID] - rssiOption))) { if (!scannedData.Keys.Contains (waypointBeaconsMapping._WaypointIDAndRegionID)) { scannedData.Add (waypointBeaconsMapping. _WaypointIDAndRegionID, new List <BeaconSignal> { beacon }); } else { scannedData [waypointBeaconsMapping. _WaypointIDAndRegionID] .Add(beacon); } } } } } //Make sure we have got more than two interested guid in each //waypoint foreach (KeyValuePair <RegionWaypointPoint, List <BeaconSignal> > interestedBeacon in scannedData) { //Console.WriteLine("Key : " + interestedBeacon.Value); Dictionary <Guid, List <BeaconSignal> > tempSave = new Dictionary <Guid, List <BeaconSignal> >(); foreach (BeaconSignal beaconSignal in interestedBeacon.Value) { if (!tempSave.Keys.Contains(beaconSignal.UUID)) { tempSave.Add(beaconSignal.UUID, new List <BeaconSignal> { beaconSignal }); } else { tempSave[beaconSignal.UUID].Add(beaconSignal); } } if (tempSave.Keys.Count() >= _moreThanTwoIBeacon) { correctData.Add(interestedBeacon.Key, interestedBeacon.Value); } } foreach (KeyValuePair <RegionWaypointPoint, List <BeaconSignal> > calculateData in correctData) { //If a waypoint has at least two beacon UUIDs, //this waypoint might be our interested waypoint. if (calculateData.Value.Count() >= _moreThanTwoIBeacon) { Dictionary <Guid, List <int> > saveEachBeacons = new Dictionary <Guid, List <int> >(); //Sort the beacons by their Rssi calculateData.Value.Sort((x, y) => { return(x.RSSI.CompareTo(y.RSSI)); }); int avgSignal = 0; //int averageSignal = 0; //List<int> signalOfEachBeacon = new List<int>(); //If we have more than ten data, we remove the highest 10% //and the lowest 10%, and calculate their average //If we have not more than 10 data, //we just calculate their average if (calculateData.Value.Count() >= 10) { int min = Convert.ToInt32(scannedData.Count() * 0.1); int max = Convert.ToInt32(scannedData.Count() * 0.9); int minus = max - min; for (int i = min; i < max; i++) { avgSignal += calculateData.Value[i].RSSI; } avgSignal = avgSignal / minus; } else { foreach (BeaconSignal value in calculateData.Value) { avgSignal += value.RSSI; } avgSignal = avgSignal / calculateData.Value.Count(); } signalAvgValue.Add(calculateData.Key, avgSignal); } } int tempValue = -100; bool haveThing = false; RegionWaypointPoint possibleRegionWaypoint = new RegionWaypointPoint(); // Compare all data we have, and get the highest Rssi Waypoint as // our interested waypoint foreach (KeyValuePair <RegionWaypointPoint, int> calculateMax in signalAvgValue) { if (tempValue < calculateMax.Value) { possibleRegionWaypoint = new RegionWaypointPoint(); possibleRegionWaypoint = calculateMax.Key; haveThing = true; } tempValue = calculateMax.Value; } if (haveThing == true) { watch.Stop(); watch.Reset(); watch.Start(); Console.WriteLine("Matched IBeacon"); _event.OnEventCall(new WaypointSignalEventArgs { _detectedRegionWaypoint = possibleRegionWaypoint }); return; } } }