/// <summary> /// 添加一个车辆到车辆列表中 /// </summary> /// <param name="aVehicle"></param> public void AddVehicle(JsonLastPosition aVehicle) { List <JsonLastPosition> vehicles; if (this.dgvVechiles.DataSource is List <JsonLastPosition> ) { vehicles = this.dgvVechiles.DataSource as List <JsonLastPosition>; } else { vehicles = new List <JsonLastPosition>(); } int tmpIndex = vehicles.FindIndex(x => x.VehicleIndex == aVehicle.VehicleIndex); if (tmpIndex < 0) { vehicles.Insert(0, aVehicle); } else { vehicles[tmpIndex] = aVehicle; this.dgvVechiles.Refresh(); return; } this.dgvVechiles.DataSource = null; this.dgvVechiles.DataSource = vehicles; this.dgvVechiles.Refresh(); }
/// <summary> /// 登陆验证后台工作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void loginWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; if (e.Argument is string[] loginArgs) { // FIXME: 目前是通过获取终端编号的位置信息模拟登陆过程, 后期将通过用户名密码验证API进行登陆 string loginName = loginArgs[0]; string loginPwd = loginArgs[1]; LastLocation = UrlApiHelper.GetLastLocation(loginName); if (LastLocation == null) { worker.ReportProgress(-1, "服务器连接异常..."); return; } else if (LastLocation.Status != 0) { worker.ReportProgress(-1, LastLocation.Message); return; } // 登陆成功 UpdateLoginProgress(50, "登陆成功, 正在载入界面..."); mParentFrm.InvokeIfRequired(frm => frm.LoadUI()); worker.ReportProgress(100); } else { // 失败 worker.ReportProgress(-1, "参数错误"); } }
/// <summary> /// 获取指定终端最新的位置信息 /// </summary> /// <param name="aTerminalId">终端编号</param> /// <returns>null服务器无响应</returns> public static JsonLastPosition GetLastLocation(string aTerminalId) { string jsonStr = GetApiReply(GetLastPosition(aTerminalId)); if (string.IsNullOrEmpty(jsonStr)) { return(null); } JsonLastPosition lastLocation = ModelUtils.GetJsonObject <JsonLastPosition>(jsonStr); if (lastLocation != null) { lastLocation.UpdateTime = DateTime.Now; return(lastLocation); } JsonBase errInfo = ModelUtils.GetJsonObject <JsonBase>(jsonStr); if (errInfo == null) { return(null); } // 重新组合返回错误值 return(new JsonLastPosition { Status = errInfo.Status, Message = errInfo.Message }); }
/// <summary> /// 定位车辆到地图中 /// </summary> /// <param name="aLocation"></param> /// <param name="aIsCenter"></param> public void LocatedVehicle(JsonLastPosition aLocation, bool aIsCenter = true) { mFrmMap?.InvokeIfRequired((frm) => { frm.LocatedVehicle(aLocation, aIsCenter); frm.Show(); }); }
/// <summary> /// 窗体载入过程 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void FrmMain_Load(object sender, EventArgs e) { // 首先进行登陆 DlgLogin dlg = new DlgLogin(this); if (dlg.ShowDialog() != DialogResult.OK) { this.ForceClose(); return; } mLastLocation = dlg.LastLocation; mFrmVehicles.AddVehicle(mLastLocation); }
/// <summary> /// 双击进行定位 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void dgvVechiles_CellDoubleClick(object sender, DataGridViewCellEventArgs e) { if (e.RowIndex < 0) { return; } JsonLastPosition location = this.dgvVechiles.Rows[e.RowIndex].DataBoundItem as JsonLastPosition; if (location == null) { return; } mParent.LocatedVehicle(location); }
/// <summary> /// 定位车辆到地图中 /// </summary> /// <param name="aLocation"></param> /// <param name="aIsCenter"></param> public void LocatedVehicle(JsonLastPosition aLocation, bool aIsCenter = true) { if (aLocation == null) { return; } this.wbMap.BeginInvokeIfRequired((map) => { map.LocatedVehicle(aLocation); if (aIsCenter) { map.SetCenter(aLocation.BdLongitude, aLocation.BdLatitude); } }); }
/// <summary> /// 生成定位车辆的参数 /// </summary> /// <param name="aLocation"></param> /// <param name="aShow">是否显示车辆</param> /// <returns></returns> public static object[] GetLocatedVehicleParams(JsonLastPosition aLocation, bool aShow = true) { int vehicleColor = NORMAL_COLOR; VehicleStatusType tmpState = (VehicleStatusType)aLocation.VehicleStatus; if (tmpState == VehicleStatusType.LongOffline || tmpState == VehicleStatusType.Offline) { vehicleColor = OFFLINE_COLOR; } else if (tmpState == VehicleStatusType.Alarm) { vehicleColor = WARN_COLOR; } return(new object[] { aLocation.LicensePlate, aLocation.BdLongitude, aLocation.BdLatitude, aLocation.Direction, vehicleColor, aShow ? GenInfoWindow(aLocation) : "" }); }
/// <summary> /// 获取信息窗口代码 /// </summary> /// <param name="aLocation"></param> /// <returns></returns> public static string GenInfoWindow(JsonLastPosition aLocation) { StringBuilder htmlBuilder = new StringBuilder(); htmlBuilder.AppendLine("<table width='360' style='font-size: 12px'>"); htmlBuilder.AppendFormat("<tr valign='middle'><td width='60' height='22' align='right'><strong>车牌号码:</strong></td><td width='70'>{0}</td>" + "<td width='95' align='right'><strong>所属企业:</strong></td><td width='135'>{1}</td></tr>\r\n", aLocation.LicensePlate, GlobalCfg.Company); htmlBuilder.AppendFormat("<tr valign='middle'><td height='22' align='right'><strong>实时速度:</strong></td><td>{0}Km/h</td>" + "<td align='right'><strong>经度纬度:</strong></td><td>{1:F5},{2:F6}</td></tr>\r\n", aLocation.Speed, aLocation.Longitude, aLocation.Latitude); htmlBuilder.AppendFormat("<tr valign='middle'><td height='22' align='right'><strong>车辆状态:</strong></td><td colspan='3'>{0}</td></tr>\r\n", aLocation.StatusDescription); htmlBuilder.AppendLine("</table>"); return(htmlBuilder.ToString()); }
/// <summary> /// 在指定地图中定位车辆 /// </summary> /// <param name="aMap"></param> /// <param name="aLocation"></param> public static void LocatedVehicle(this WebMap aMap, JsonLastPosition aLocation) { LocatedVehicle(aMap, GetLocatedVehicleParams(aLocation)); }