public IHttpActionResult GetOnlineNewestGGAByAccount(string account) { GGAHistoryEntity ggaEntity = new GGAHistoryEntity(); ResultEntity result = new ResultEntity(); try { //找到指定账号的最近gga信息 GGAHistory temp = dal.FindGGAHistoriesByAccount(account)[0]; //判断GGA相关会话是否在线 if (temp.SessionHistory.ConnectionEnd == null) { ggaEntity = temp.ToGGAHistoryEntity(); ggaEntity.Session = temp.SessionHistory.ToSessionHistoryEntity(); } } catch (Exception e) { result.Message = e.Message; NtripProxyLogger.LogExceptionIntoFile("调用接口api/GGAHistory/GetOnlineNewestGGAByAccount异常,异常信息为:" + e.Message); } result.IsSuccess = result.Message == null; result.Data = ggaEntity; return(Json <ResultEntity>(result)); }
/// <summary> /// 添加概略位置 /// </summary> /// <param name="session">概略位置信息</param> /// <returns>是否添加成功</returns> public bool AddGGAHistory(GGAHistory gga) { bool result = false; using (var ctx = new NtripForwardDB()) { ctx.GGAHistories.Add(gga); result = ctx.SaveChanges() == 1; } return(result); }
/// <summary> /// 模型GGAHistory类扩展方法,转换基础信息到GGAHistoryEntity /// </summary> /// <param name="gga">DAL层概略位置</param> /// <returns>WebApi层概略位置实体</returns> public static GGAHistoryEntity ToGGAHistoryEntity(this GGAHistory gga) { GGAHistoryEntity ggaHistoryEntity = new GGAHistoryEntity { ID = gga.ID, Account = gga.Account, AccountType = gga.AccountType, AccountSYS = gga.AccountSYS, FixedTime = (DateTime)gga.FixedTime, Lng = (double)gga.Lng, Lat = (double)gga.Lat, Status = (int)gga.Status, GGAInfo = gga.GGAInfo }; return(ggaHistoryEntity); }
/// <summary> /// 转换WebApi概略位置实体为dal层概略位置基础信息 /// </summary> /// <returns>dal层概略位置</returns> public GGAHistory ToGGAHistory() { GGAHistory ggaHistory = new GGAHistory() { ID = ID, Account = Account, AccountType = AccountType, AccountSYS = AccountSYS, FixedTime = FixedTime, Lng = decimal.Parse(Lng.ToString()), Lat = decimal.Parse(Lat.ToString()), Status = Status, GGAInfo = GGAInfo }; return(ggaHistory); }
private void acceptCallback(IAsyncResult ar) { TcpListener lstn = (TcpListener)ar.AsyncState; TcpClient client = lstn.EndAcceptTcpClient(ar); NetworkStream stream = client.GetStream(); Task.Run(() => { TcpClient qianxunClient = new TcpClient(); qianxunClient.Connect(IPAddress.Parse(qianxunIP), ROMOTE_PORT); //中继缓存数据 string bufferMsg = ""; //主机地址 string host = client.Client.RemoteEndPoint.ToString(); //拨号的账户 ACCOUNT account = new ACCOUNT(); //使用的千寻账户 ACCOUNTSYS accountSYS = new ACCOUNTSYS(); //会话状态,并初始化 SessionHistory session = new SessionHistory(); session.ID = Guid.NewGuid(); session.GGACount = 0; session.FixedCount = 0; //GGA记录频率 int GGARecordRate = settings.GGARecordRate; //GGA初始计数 int GGACounter = 1; //是否继续维持连接 bool isLoop = true; //设置定时器,作为gga发送的中继 System.Timers.Timer timerRepeat = new System.Timers.Timer(); timerRepeat.Interval = settings.GGARepeatRate; timerRepeat.AutoReset = true; timerRepeat.Elapsed += delegate { if (MsgHelper.CheckRequestType(bufferMsg) == 3) { byte[] sendMsg = Connect(qianxunClient, bufferMsg); //将千寻返回数据转发给客户端 stream.Write(sendMsg, 0, sendMsg.Length); } }; timerRepeat.Start(); //设置定时器,如果长时间不发送数据,则关掉tcp连接 System.Timers.Timer timerTimeout = new System.Timers.Timer(); timerTimeout.Interval = settings.Timeout; timerTimeout.AutoReset = false; timerTimeout.Elapsed += delegate { //记录session session.ConnectionEnd = DateTime.Now; if (session.AccountType != null) { sessionDAL.UpdateSessionHistory(session); } //更新在线状态 if (session.AccountType == "Normal") { account.Account_IsOnline = false; accountSYS.AccountSYS_IsOnline = false; accountDAL.UpdateAccount(account); accountSYSDAL.UpdateAccountSYS(accountSYS); } //关闭计时器 timerTimeout.Stop(); timerRepeat.Stop(); //关闭所有连接 stream.Close(); client.Close(); qianxunClient.Close(); //跳出循环 isLoop = false; }; timerTimeout.Start(); #region 步读取 while (isLoop) { //解决cpu100%占用问题 Thread.Sleep(1); try { if (stream.DataAvailable) { //计时器重置 timerTimeout.Stop(); timerTimeout.Start(); //用来存储网络字节流数据 byte[] buffer = new byte[1024]; stream.Read(buffer, 0, buffer.Length); string recMsg = Encoding.UTF8.GetString(buffer); byte[] sendMsg = new byte[1024]; switch (MsgHelper.CheckRequestType(recMsg)) { //浏览器请求 case 1: sendMsg = Connect(qianxunClient, recMsg); break; //手部首次请求 case 2: string[] namePassword = MsgHelper.GetAuthorization(recMsg); account = accountDAL.FindAccountByName(namePassword[0]); //第三方账号 if (account == null) { session.AccountType = "Third"; session.AccountName = namePassword[0]; session.MountPoint = MsgHelper.GetMountPoint(recMsg); session.ConnectionStart = DateTime.Now; session.Client = MsgHelper.GetUserAgent(recMsg); session.ClientAddress = host; session.ErrorInfo = string.Join(":", MsgHelper.GetAuthorization(recMsg)); sessionDAL.AddSessionHistory(session); recMsg = recMsg + Environment.NewLine; sendMsg = Connect(qianxunClient, recMsg); } else { if (account.Account_Password == namePassword[1]) { //账户已过期 if (account.Account_Expire < DateTime.Now) { session.AccountType = "Expire"; session.AccountName = namePassword[0]; session.MountPoint = MsgHelper.GetMountPoint(recMsg); session.ConnectionStart = DateTime.Now; session.Client = MsgHelper.GetUserAgent(recMsg); session.ClientAddress = host; session.ErrorInfo = "账户已过期"; sessionDAL.AddSessionHistory(session); //sendMsg = Connect(qianxunClient, recMsg); } else { //账户已在线 if ((bool)account.Account_IsOnline) { session.AccountType = "Online"; session.AccountName = namePassword[0]; session.MountPoint = MsgHelper.GetMountPoint(recMsg); session.ConnectionStart = DateTime.Now; session.Client = MsgHelper.GetUserAgent(recMsg); session.ClientAddress = host; session.ErrorInfo = "账户已在线"; sessionDAL.AddSessionHistory(session); //sendMsg = Connect(qianxunClient, recMsg); } else { //账户被锁定 if ((bool)account.Account_IsLocked) { session.AccountType = "Locked"; session.AccountName = namePassword[0]; session.MountPoint = MsgHelper.GetMountPoint(recMsg); session.ConnectionStart = DateTime.Now; session.Client = MsgHelper.GetUserAgent(recMsg); session.ClientAddress = host; session.ErrorInfo = "账户已锁定"; sessionDAL.AddSessionHistory(session); //sendMsg = Connect(qianxunClient, recMsg); } else { //账户正常 session.AccountType = "Normal"; //更新账户状态 account.Account_IsOnline = true; account.Account_LastLogin = DateTime.Now; accountDAL.UpdateAccount(account); //找到可替换的系统账号并更新状态 accountSYS = accountSYSDAL.FindSuitableAccountSYS(); accountSYS.AccountSYS_IsOnline = true; accountSYS.AccountSYS_LastLogin = DateTime.Now; accountSYS.AccountSYS_Age++; accountSYSDAL.UpdateAccountSYS(accountSYS); //更新session状态 session.AccountName = account.Account_Name; session.AccountSYSName = accountSYS.AccountSYS_Name; session.ConnectionStart = DateTime.Now; session.Client = MsgHelper.GetUserAgent(recMsg); session.MountPoint = MsgHelper.GetMountPoint(recMsg); session.ClientAddress = host; sessionDAL.AddSessionHistory(session); //替换信息 string replaceMessage = MsgHelper.ReplaceAuthorization(recMsg, accountSYS.AccountSYS_Name, accountSYS.AccountSYS_Password) + Environment.NewLine; sendMsg = Connect(qianxunClient, replaceMessage); } } } } else { //用户名密码错误 session.AccountType = "PasswordError"; session.AccountName = namePassword[0]; session.MountPoint = MsgHelper.GetMountPoint(recMsg); session.ConnectionStart = DateTime.Now; session.Client = MsgHelper.GetUserAgent(recMsg); session.ClientAddress = host; session.ErrorInfo = "用户名密码错误"; account.Account_PasswordOvertime = DateTime.Now; account.Account_PasswordOvercount++; sessionDAL.AddSessionHistory(session); sendMsg = Connect(qianxunClient, recMsg); } } break; //GGA数据采集 case 3: //发送的GGA数据递增 session.GGACount++; //是否固定解 if (MsgHelper.FixModule(recMsg) == 4) { session.FixedCount++; } //更新session信息 sessionDAL.UpdateSessionHistory(session); //中继缓冲gga信息 bufferMsg = recMsg; if ( //是否记录GGA数据 settings.EnableLogGGA //到达指定评率记录GGA数据 && GGACounter % GGARecordRate == 0 //第三方账户不记录GGA数据 && account != null ) { GGAHistory gga = new GGAHistory(); gga.ID = Guid.NewGuid(); gga.Account = account.Account_Name; gga.AccountSYS = accountSYS.AccountSYS_Name; gga.AccountType = "Normal"; gga.FixedTime = DateTime.Now; gga.Lng = Convert.ToDecimal(MsgHelper.GetLng(recMsg)); gga.Lat = Convert.ToDecimal(MsgHelper.GetLat(recMsg)); gga.Status = MsgHelper.FixModule(recMsg); gga.GGAInfo = recMsg; gga.SessionID = session.ID; ggaDAL.AddGGAHistory(gga); } GGACounter++; sendMsg = Connect(qianxunClient, recMsg); break; default: break; } //将千寻返回数据转发给客户端 stream.Write(sendMsg, 0, sendMsg.Length); } } catch (Exception e) { //更新在线状态 if (session.AccountType == "Normal") { account.Account_IsOnline = false; accountSYS.AccountSYS_IsOnline = false; accountDAL.UpdateAccount(account); accountSYSDAL.UpdateAccountSYS(accountSYS); } //结束会话 session.ConnectionEnd = DateTime.Now; //session.ErrorInfo = e.Message; sessionDAL.UpdateSessionHistory(session); //关闭所有连接 stream.Close(); client.Close(); qianxunClient.Close(); timerTimeout.Stop(); timerRepeat.Stop(); break; } } #endregion }); lstn.BeginAcceptTcpClient(new AsyncCallback(acceptCallback), lstn); }