/// <summary> /// 异步请求更新状态。 /// </summary> public IAsyncAction RefreshAsync() { return(Run(async token => { var http = new HttpClient(); IAsyncAction act = null; IAsyncOperation <string> ope = null; token.Register(() => { act?.Cancel(); ope?.Cancel(); }); try { act = signInUsereg(http); await act; //获取用户信息 ope = http.GetStrAsync(new Uri("http://usereg.tsinghua.edu.cn/user_info.php")); var res1 = await ope; var info1 = Regex.Match(res1, "使用流量\\(IPV4\\).+?(\\d+?)\\(byte\\).+?帐户余额.+?([0-9.]+)\\(元\\)", RegexOptions.Singleline).Groups; if (info1.Count != 3) { var ex = new InvalidOperationException("获取到的数据格式错误。"); ex.Data.Add("HtmlResponse", res1); throw ex; } WebTraffic = new Size(ulong.Parse(info1[1].Value, CultureInfo.InvariantCulture)); Balance = decimal.Parse(info1[2].Value, CultureInfo.InvariantCulture); //获取登录信息 var res2 = await http.GetStrAsync(new Uri("http://usereg.tsinghua.edu.cn/online_user_ipv4.php")); var info2 = Regex.Matches(res2, "<tr align=\"center\">.+?</tr>", RegexOptions.Singleline); var devices = (from Match r in info2 let details = Regex.Matches(r.Value, "(?<=\\<td class=\"maintd\"\\>)(.*?)(?=\\</td\\>)") select new WebDevice(Ipv4Address.Parse(details[0].Value), MacAddress.Parse(details[6].Value)) { WebTraffic = Size.Parse(details[2].Value), LogOnDateTime = DateTime.ParseExact(details[1].Value, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture), DropToken = Regex.Match(r.Value, @"(?<=value="")(\d+)(?="")").Value, HttpClient = http }).ToArray(); var t = deviceList.FirstOrDefault(); if (t != null) { t.HttpClient.Dispose(); } var common = devices.Join(deviceList, n => n, o => o, (n, o) => { o.DropToken = n.DropToken; o.HttpClient = n.HttpClient; o.LogOnDateTime = n.LogOnDateTime; o.WebTraffic = n.WebTraffic; return o; }, new deviceComparer()); for (int i = 0; i < deviceList.Count;) { if (!common.Contains(deviceList[i])) { deviceList[i].Dispose(); deviceList.RemoveAt(i); } else { i++; } } foreach (var item in devices) { if (!common.Contains(item, new deviceComparer())) { deviceList.Add(item); } else { item.Dispose(); } } //全部成功 UpdateTime = DateTime.Now; } catch (LogOnException) { throw; } catch (OperationCanceledException) { throw; } catch (Exception ex) { throw new LogOnException(LogOnExceptionType.ConnectError, ex); } finally { if (deviceList.Count == 0) { http.Dispose(); } } })); }
/// <summary> /// 初始化 <see cref="WebDevice"/> 的实例并设置相关信息。 /// </summary> /// <param name="ip">IP 地址。</param> /// <param name="mac">Mac 地址。</param> /// <param name="deviceFamilyDescription">设备描述。</param> public WebDevice(Ipv4Address ip, MacAddress mac, string deviceFamilyDescription) : this(ip, mac) { this.SetDeviceFimaly(deviceFamilyDescription); }
/// <summary> /// 初始化 <see cref="WebDevice"/> 的实例并设置相关信息。 /// </summary> /// <param name="ip">IP 地址。</param> /// <param name="mac">Mac 地址。</param> public WebDevice(Ipv4Address ip, MacAddress mac) { this.IPAddress = ip; this.Mac = mac; deviceDictChanged += WebDevice_deviceDictChanged; }
/// <summary> /// 初始化 <see cref="WebDevice"/> 的实例并设置相关信息。 /// </summary> /// <param name="ip">IP 地址。</param> /// <param name="mac">Mac 地址。</param> public WebDevice(Ipv4Address ip, MacAddress mac) { this.IPAddress = ip; this.Mac = mac; }