Exemplo n.º 1
0
        /// <summary>
        /// Adds data to the cache, and filler(-100 rssi) data if the AP isn't in the data
        /// </summary>
        /// <param name="data"></param>
        /// <param name="gpsData"></param>
        public void AddData(NetworkData[] data, GpsData gpsData)
        {
            if (data == null || data.Length < 1)
            {
                return;
            }

            //Keep a tally of the APs that weren't here.
            List <MacAddress> missing = new List <MacAddress>();

            missing.AddRange(_cache.Keys);

            lock (_cache)
            {
                //Loop through all supplied data and add or update accesspoints
                foreach (NetworkData n2 in data)
                {
                    //Check if the cache contains this AP already
                    if (_cache.ContainsKey(n2.MyMacAddress))
                    {
                        //It does, update it!
                        GetAccessPointByMacAddress(n2.MyMacAddress).AddData(n2, gpsData);
                        //The AP was here, remove it from the missing list
                        missing.Remove(n2.MyMacAddress);
                    }
                    else
                    {
                        //nope, never heard of it. Add it!
                        _cache.Add(n2.MyMacAddress, new AccessPoint(n2)
                        {
                            MyColor = Utilities.GetColor(),
                            Vendor  = _av.GetVendor(n2.MyMacAddress),
                            GpsData = gpsData
                        });
                    }
                }
            }

            //Add filler data for all absent APs
            foreach (MacAddress mac in missing)
            {
                GetAccessPointByMacAddress(mac).AddFiller(
                    DateTime.FromFileTime(data.Max(nd => nd.MyTimestamp.ToFileTime())));
            }
        }
        private void InternalStationsRead()
        {
            // do shit here!
            //
            List <IPInfo> ipInfoDataList = null;
            var           getInfoList    = new Func <List <IPInfo> >(() =>
            {
                if (ipInfoDataList == null)
                {
                    ipInfoDataList = IPInfo.GetIPInfo();
                }
                return(ipInfoDataList);
            });

            // they are not checked yet
            for (int i = 0; i < _stationUsers.Count; i++)
            {
                _stationUsers[0]._checked = false;
            }

            var  newUsers    = new List <StationUser>();
            bool userAdded   = false,
                 userRemoved = false,
                 userUpdated = false;

            foreach (var station in _wlanManager.Stations)
            {
                var stationMac = station.Value.MacAddress;

                var stationUser = _stationUsers.FirstOrDefault(a => a.MacAddress == stationMac);
                if (stationUser == null)
                {
                    stationUser = new StationUser
                    {
                        Status     = StationUser.UserStatus.Connecting,
                        HostName   = "",
                        IpAddress  = "",
                        JoinDate   = DateTime.Now,
                        MacAddress = stationMac,
                        Vendor     = AdapterVendors.GetVendor(stationMac)
                    };
                    _stationUsers.Add(stationUser);
                    newUsers.Add(stationUser);
                    userAdded = true;
                }
                stationUser._checked = true;

                // user is not connected yet!
                if (string.IsNullOrEmpty(stationUser.IpAddress) ||
                    string.IsNullOrEmpty(stationUser.HostName) ||
                    stationUser.HostName.Length == 0)
                {
                    var stationMacCompare = station.Value.MacAddress.ToLowerInvariant().Replace(':', '-');
                    var ipInfo            = getInfoList().FirstOrDefault(a => a.MacAddress.ToLowerInvariant() == stationMacCompare);

                    if (ipInfo != null)
                    {
                        stationUser.IpAddress = ipInfo.IPAddress;
                        stationUser.HostName  = ipInfo.HostName;
                        if (stationUser.HostName == null)
                        {
                            stationUser.HostName         = stationMac;
                            stationUser.HostNameResolved = false;
                        }
                        else
                        {
                            stationUser.HostNameResolved = true;
                        }
                        userUpdated        = true;
                        stationUser.Status = StationUser.UserStatus.Connected;
                    }
                    else
                    {
                        stationUser.HostName         = stationMac;
                        stationUser.IpAddress        = "";
                        stationUser.HostNameResolved = false;
                    }
                }
                else if (stationUser.HostNameResolved == false)
                {
                    if (stationUser.CanTryResolveHost())
                    {
                        if (stationUser.ResolveHostName())
                        {
                            userUpdated = true;
                        }
                    }
                    else
                    {
                        // do not read the host name again
                        stationUser.HostNameResolved = true;
                    }
                }
            }

            for (int i = _stationUsers.Count - 1; i >= 0; i--)
            {
                var user = _stationUsers[i];
                if (!user._checked)
                {
                    userRemoved             = true;
                    _stationUsers[i].Status = StationUser.UserStatus.Disconnect;
                    _stationUsers.RemoveAt(i);
                }
                user._checked = false;
            }

            if (userAdded)
            {
                RaiseOnUserConnected(newUsers);
            }
            if (userUpdated)
            {
                RaiseOnUserUpdated();
            }
            if (userRemoved)
            {
                RaiseOnUserLeave();
            }
        }