/// <summary> /// Initializes all components /// </summary> public void Init() { _trackingController = new TrackingController(this, picMinePlan); _messagingController = new MessagingController(this); _reportsController = new ReportsController(); // Set labels llblSiteInfo.Text = MineSite.GetInstance().siteName; llblUsername.Text = User.GetInstance().username; lblUserType.Text = User.GetInstance().isAdmin ? "Administrator" : "Regular User"; // Load routers into treeview LoadRoutersToTree(); picMinePlan.Controls.Clear(); _trackingController.AddAllRoutersToMap(); LoadAllActiveMembers(); // Set mapscale number incrementer nudMapScale.Value = (decimal)(MineSite.GetInstance().mapScale); nudMapScale.Font = new Font(nudMapScale.Font, FontStyle.Regular); btnSaveScale.Enabled = false; // Subscribe mainform to let it deal with member changes foreach (Member member in MineSite.GetInstance().siteMembers.Values) { member.OnInfoUpdated += LoadAllActiveMembers; member.OnPathUpdated += LoadRoutersToTree; } }
/// <summary> /// Opens a dialog to load a jpg, jpeg, or bmp image of map data /// </summary> public void LoadMap(PictureBox picMinePlan) { Stream imageStream = null; OpenFileDialog openImageFileDialog = new OpenFileDialog(); openImageFileDialog.InitialDirectory = System.IO.Directory.GetCurrentDirectory(); openImageFileDialog.Filter = "Image Files (*.bmp, *.jpg, *.jpeg)|*.bmp;*.jpg;*.jpeg"; openImageFileDialog.FilterIndex = 1; openImageFileDialog.RestoreDirectory = true; if (openImageFileDialog.ShowDialog() == DialogResult.OK) { try { if ((imageStream = openImageFileDialog.OpenFile()) != null) { using (imageStream) { Image image = Image.FromStream(imageStream); picMinePlan.Image = image; MineSite.GetInstance().localMapFileAddr = openImageFileDialog.FileName; MineSiteUpdate(); } } } catch (Exception ex) { MessageBox.Show("Error: Could not open file. \n", ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
private void nudMapScale_ValueChanged(object sender, EventArgs e) { MineSite.GetInstance().UpdateScale((float)(nudMapScale.Value)); nudMapScale.Font = new Font(nudMapScale.Font, FontStyle.Bold); btnSaveScale.Enabled = true; }
/// <summary> /// Updates the existing mine site in the database /// </summary> /// <returns></returns> public bool MineSiteUpdate() { using (SqlConnection sqlCon = new SqlConnection(Properties.Settings.Default.TMS_DatabaseConnectionString)) { string cmdString = "UPDATE Site SET siteName=@siteName, mapScale=@mapScale, localMapFileAddr=@localMapFileAddr WHERE Id=@siteId"; SqlCommand oCmd = new SqlCommand(cmdString, sqlCon); oCmd.Parameters.AddWithValue("@siteName", MineSite.GetInstance().siteName); oCmd.Parameters.AddWithValue("@mapScale", MineSite.GetInstance().mapScale); oCmd.Parameters.AddWithValue("@localMapFileAddr", MineSite.GetInstance().localMapFileAddr); oCmd.Parameters.AddWithValue("@siteId", MineSite.GetInstance().siteId); sqlCon.Open(); try { int rows = oCmd.ExecuteNonQuery(); // Update all MineSite related MainForm components _mainForm.MineSiteUpdated(); return(true); } catch (SqlException e) { MessageBox.Show("Error", e.Message, MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } }
/// <summary> /// Adds a router image to the map and event handlers for clicking. /// </summary> /// <param name="router">The router to be added to the map.</param> public void AddOneRouterToMap(Router router) { PictureBox picRouter = new PictureBox(); _picMinePlan.Controls.Add(picRouter); picRouter.BackColor = Color.Transparent; OnRouterUpdate(picRouter, router); picRouter.Size = new Size(picRouter.Image.Width, picRouter.Image.Height); // Set up events for the router updating picRouter.MouseDown += (sender, e) => { ShowMemberPosition(sender, e, router); }; // Subscribe to router updates router.OnUpdated += () => { OnRouterUpdate(picRouter, router); }; router.OnDeleted += () => { _picMinePlan.Controls.Remove(picRouter); }; MineSite.GetInstance().OnUpdated += (scale) => { OnRouterUpdate(picRouter, router); }; }
/// <summary> /// Loads all the routers and members to the treeview /// </summary> public void LoadRoutersToTree() { tvAllRouters.BeginUpdate(); tvAllRouters.Nodes[0].Nodes.Clear(); foreach (Router router in MineSite.GetInstance().siteRouters) { TreeNode routerNode = new TreeNode(router.ToString()); tvAllRouters.Nodes[0].Nodes.Add(routerNode); foreach (Member member in router.hasConnectedMembers) { if (member.IsActive()) { TreeNode memberNode = new TreeNode(member.ToString()); routerNode.Nodes.Add(memberNode); } } } tvAllRouters.Nodes[0].ExpandAll(); tvAllRouters.Nodes[0].Text = MineSite.GetInstance().siteName; tvAllRouters.EndUpdate(); }
/// <summary> /// Adds a new router to the database. Also add the router to the MineSite. /// </summary> /// <param name="rId"></param> /// <param name="addr"></param> /// <param name="loc"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="isBlocked"></param> /// <returns></returns> public bool RouterCreate(string rId, string addr, string loc, int x, int y, bool isBlocked) { using (SqlConnection sqlCon = new SqlConnection(Properties.Settings.Default.TMS_DatabaseConnectionString)) { string cmdString = "INSERT INTO Routers(Id, address, location, x, y, isBlocked, siteId) VALUES(@Id, @address, @location, @x, @y, @isBlocked, @siteId)"; sqlCon.Open(); SqlCommand oCmd = new SqlCommand(cmdString, sqlCon); oCmd.Parameters.AddWithValue("@Id", rId); oCmd.Parameters.AddWithValue("@address", addr); oCmd.Parameters.AddWithValue("@location", loc); oCmd.Parameters.AddWithValue("@x", x); oCmd.Parameters.AddWithValue("@y", y); oCmd.Parameters.AddWithValue("@isBlocked", isBlocked ? 1 : 0); oCmd.Parameters.AddWithValue("@siteId", MineSite.GetInstance().siteId); try { int rows = oCmd.ExecuteNonQuery(); Router router = new Router(rId, addr, loc, x, y, isBlocked); MineSite.GetInstance().siteRouters.Add(router); _mainForm.AddNewCreatedRouter(router); return(true); } catch (SqlException e) { MessageBox.Show("Error", e.Message, MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } }
/// <summary> /// Adds every site router to the map /// </summary> public void AddAllRoutersToMap() { foreach (Router router in MineSite.GetInstance().siteRouters) { AddOneRouterToMap(router); } }
/// <summary> /// Removes a router from the database /// </summary> /// <param name="router">The router to be removed.</param> /// <returns>True if successfully removed, false otherwise.</returns> public bool RouterDelete(Router router) { using (SqlConnection sqlCon = new SqlConnection(Properties.Settings.Default.TMS_DatabaseConnectionString)) { string cmdString = "DELETE FROM Routers WHERE Id=@rId"; sqlCon.Open(); SqlCommand oCmd = new SqlCommand(cmdString, sqlCon); oCmd.Parameters.AddWithValue("@rId", router.routerId); try { int rows = oCmd.ExecuteNonQuery(); // Remove router objects router.Dispose(); MineSite.GetInstance().siteRouters.Remove(router); return(true); } catch (SqlException e) { MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } }
private void LoadRouterIDs() { foreach (Router router in MineSite.GetInstance().siteRouters) { lstRouters.Items.Add(router); } }
/// <summary> /// Creates a sensor in the database. /// </summary> /// <param name="sensorId"></param> /// <returns></returns> public bool SensorCreate(string sensorId) { using (SqlConnection sqlCon = new SqlConnection(Properties.Settings.Default.TMS_DatabaseConnectionString)) { string cmdString = "INSERT INTO Sensors(Id) VALUES(@Id)"; sqlCon.Open(); SqlCommand oCmd = new SqlCommand(cmdString, sqlCon); oCmd.Parameters.AddWithValue("@Id", sensorId); try { int rows = oCmd.ExecuteNonQuery(); MineSite.GetInstance().siteSensors.Add(sensorId); } catch (SqlException e) { MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } return(true); }
/// <summary> /// Creates a member object in the database and adds it to MineSite /// </summary> public bool MemberCreate(string memberNo, string fName, string mName, string lName, string addr, string prov, string city, int pinNo, string phoneNo, string mobileNo, bool isVehicle, string tagId) { using (SqlConnection sqlCon = new SqlConnection(Properties.Settings.Default.TMS_DatabaseConnectionString)) { string cmdString = "INSERT INTO Members(memberNo, fName, mName, lName, address, province, city, pinNo, phoneNo, mobileNo, isVehicle, tagId, siteId) " + "VALUES(@memberNo, @fName, @mName, @lName, @address, @province, @city, @pinNo, @phoneNo, @mobileNo, @isVehicle, @tagId, @siteId)"; sqlCon.Open(); SqlCommand oCmd = new SqlCommand(cmdString, sqlCon); oCmd.Parameters.AddWithValue("@memberNo", memberNo); oCmd.Parameters.AddWithValue("@fName", fName); oCmd.Parameters.AddWithValue("@mName", mName); oCmd.Parameters.AddWithValue("@lName", lName); oCmd.Parameters.AddWithValue("@address", addr); oCmd.Parameters.AddWithValue("@province", prov); oCmd.Parameters.AddWithValue("@city", city); oCmd.Parameters.AddWithValue("@pinNo", pinNo); oCmd.Parameters.AddWithValue("@phoneNo", phoneNo); oCmd.Parameters.AddWithValue("@mobileNo", mobileNo); oCmd.Parameters.AddWithValue("@isVehicle", isVehicle ? 1 : 0); oCmd.Parameters.AddWithValue("@tagId", tagId); oCmd.Parameters.AddWithValue("@siteId", MineSite.GetInstance().siteId); try { int rows = oCmd.ExecuteNonQuery(); Member member = new Member(memberNo, fName, mName, lName, addr, prov, city, pinNo, phoneNo, mobileNo, isVehicle, tagId); MineSite.GetInstance().siteMembers.Add(member.memberId, member); Shift[] newShifts = { new Shift(new DateTime(0), new DateTime(0)), new Shift(new DateTime(0), new DateTime(0)), new Shift(new DateTime(0), new DateTime(0)) }; AssignShift(member, newShifts); member.OnInfoUpdated += _mainForm.LoadAllActiveMembers; member.OnPathUpdated += _mainForm.LoadRoutersToTree; _mainForm.LoadAllActiveMembers(); return(true); } catch (SqlException e) { MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } }
private void LoadAllSensors() { lstSensors.Items.Clear(); foreach (string sId in MineSite.GetInstance().siteSensors) { lstSensors.Items.Add(sId); } }
/// <summary> /// Clears the current members and loads all members into the combo box /// </summary> private void LoadMembersList() { lstMembers.Items.Clear(); foreach (Member member in MineSite.GetInstance().siteMembers.Values) { lstMembers.Items.Add(member); } }
private void btnLoadSite_Click(object sender, EventArgs e) { if (!cboSites.SelectedValue.ToString().Equals(MineSite.GetInstance().siteId.ToString())) { _masterController.OpenMainForm((int)cboSites.SelectedValue); } else { MessageBox.Show("This site is already loaded."); } }
private void llblName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { string siteName = GetNewSitename(); if (!siteName.Equals("")) { MineSite.GetInstance().siteName = siteName; bool didComplete = _controller.MineSiteUpdate(); } }
private void txtId_Leave(object sender, EventArgs e) { if (MineSite.GetInstance().ContainsRouter(mtxtId.Text) != null) { epRID.SetError(mtxtId, "This ID already exists."); } else { epRID.Clear(); } }
/// <summary> /// Load all active miners to list /// </summary> public void LoadAllActiveMembers() { lstActiveMiners.Items.Clear(); foreach (Member member in MineSite.GetInstance().siteMembers.Values) { if (!member.isVehicle && member.IsActive()) { lstActiveMiners.Items.Add(member); } } }
public TrackingController(MainForm m, PictureBox pm) { _mainForm = m; _picMinePlan = pm; MineSite.GetInstance().OnUpdated += (new_scale) => { DrawGridLines(); }; InitUSB(); }
private void mtxtNewSensor_Leave(object sender, EventArgs e) { if (MineSite.GetInstance().siteSensors.Contains(mtxtNewSensor.Text)) { epSId.SetError(mtxtNewSensor, "This ID already exists."); btnCreate.Enabled = false; } else { epSId.Clear(); btnCreate.Enabled = true; } }
/// <summary> /// Updates the legend image of a router after it has been updated /// </summary> /// <param name="picRouter"></param> /// <param name="router"></param> private void OnRouterUpdate(PictureBox picRouter, Router router) { picRouter.Location = new Point((int)(router.posX * MineSite.GetInstance().mapScale - picRouter.Width / 2), (int)(router.posY * MineSite.GetInstance().mapScale - picRouter.Width / 2)); if (router.HasActiveMembers()) { picRouter.Image = router.isBlocked ? TMS.Properties.Resources.router_blocked_miner_map : TMS.Properties.Resources.router_active_miner_map; } else { picRouter.Image = router.isBlocked ? TMS.Properties.Resources.router_blocked_map : TMS.Properties.Resources.router_active_map; } HideRouterForm(); }
/// <summary> /// After a router position has been chosen on the mine plan picture /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void SelectRouterPosClick(object sender, MouseEventArgs e) { if (e != null) { _routerForm.SetRouterPosFromMap((int)(e.X / MineSite.GetInstance().mapScale), (int)(e.Y / MineSite.GetInstance().mapScale)); } foreach (Control c in _picMinePlan.Controls) { c.Visible = true; } _picMinePlan.MouseDown -= SelectRouterPosClick; isSelectingPos = false; }
public MessagesSendForm(MessagingController c, bool isDefaultBroadcast) { InitializeComponent(); _controller = c; lblUsername.Text = User.GetInstance().username; lblDate.Text = DateTime.Now.ToString("yyyy-MM-dd"); foreach (Member member in MineSite.GetInstance().siteMembers.Values) { lstMembers.Items.Add(member); } rbBroadcast.Checked = isDefaultBroadcast; }
private void tvAllRouters_AfterSelect(object sender, TreeViewEventArgs e) { Router router = MineSite.GetInstance().ContainsRouter(e.Node.Text.Split(',')[0]); if (router != null) { _trackingController.ShowMemberPosition(sender, null, router); } else { router = MineSite.GetInstance().ContainsRouter(e.Node.Parent.Text.Split(',')[0]); if (router != null) { _trackingController.ShowMemberPosition(sender, null, router); } } }
private void LoadAllInfo() { // Fill labels llblName.Text = MineSite.GetInstance().siteName; lblScale.Text = "Current Map Scale: " + MineSite.GetInstance().mapScale; lblPath.Text = MineSite.GetInstance().localMapFileAddr; // Fill list of miners foreach (Member member in MineSite.GetInstance().siteMembers.Values) { lstMiners.Items.Add(member); } // Fill list of routers foreach (Router router in MineSite.GetInstance().siteRouters) { lstRouters.Items.Add(router); } }
/// <summary> /// Closes the login form and opens the main form /// </summary> public void OpenMainForm(int siteId) { _loginForm.Hide(); if (_mainForm != null) { MineSite.GetInstance().Dispose(); } else { _mainForm = new MainForm(this); } LoadAllSiteInfo(siteId); _mainForm.Init(); _mainForm.Show(); LoadMap(_picMinePlan, MineSite.GetInstance().localMapFileAddr); }
/// <summary> /// Opens a window over the router that was selected on the map. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <param name="router"></param> public void ShowMemberPosition(object sender, MouseEventArgs e, Router router) { if (routerMapForm == null) { routerMapForm = new RouterMapForm(this); routerMapForm.TopLevel = false; _picMinePlan.Controls.Add(routerMapForm); } // Center of the router int mouseX = (int)(router.posX * MineSite.GetInstance().mapScale); // Under the router int mouseY = (int)(router.posY * MineSite.GetInstance().mapScale); // Calculates the form's x and y so it stays in the tracking window int x = _picMinePlan.Width - mouseX >= routerMapForm.Width ? mouseX : mouseX - routerMapForm.Width; int y = _picMinePlan.Height - mouseY >= routerMapForm.Height ? mouseY : mouseY - routerMapForm.Height; routerMapForm.Show(router); routerMapForm.Location = new Point(x, y); routerMapForm.BringToFront(); }
/// <summary> /// Processes a byte stream from the coordinator /// </summary> /// <param name="data">Data formatted so we have a member id and a router ID like MXXXXRXXXX</param> public void ReadDataFromCoordinator(byte[] data) { int memberNoL = (int)data[0]; int routerNoL = (int)data[1]; char[] chars = new char[memberNoL]; System.Buffer.BlockCopy(data, 2, chars, 0, memberNoL * sizeof(char)); string memberId = new string(chars); chars = new char[routerNoL]; System.Buffer.BlockCopy(data, 2 + memberNoL * sizeof(char), chars, 0, routerNoL * sizeof(char)); string routerNo = new string(chars); Member member = MineSite.GetInstance().siteMembers[memberId]; Router router = MineSite.GetInstance().ContainsRouter(routerNo); if (router != null && member != null) { member.AppendRouter(router); CreatePathElement(member, router); } }
public void MineSiteUpdated() { UpdateMineSiteList(); LoadRoutersToTree(); llblSiteInfo.Text = MineSite.GetInstance().siteName; }
/// <summary> /// Loads all info into MineSite /// </summary> private void LoadAllSiteInfo(int siteId) { // Load site info string siteName = "", mapAddr = ""; float mapScale = 0; List <Router> routers = new List <Router>(); Dictionary <string, Member> members = new Dictionary <string, Member>(); List <string> sensors = new List <string>(); // Load routers and members using (SqlConnection sqlCon = new SqlConnection(Properties.Settings.Default.TMS_DatabaseConnectionString)) { string cmdString = "SELECT * FROM Site WHERE Id=@siteId"; SqlCommand oCmd = new SqlCommand(cmdString, sqlCon); oCmd.Parameters.AddWithValue("@siteId", siteId); sqlCon.Open(); using (SqlDataReader oReader = oCmd.ExecuteReader()) { while (oReader.Read()) { siteId = Int32.Parse(oReader["Id"].ToString()); siteName = oReader["siteName"].ToString(); mapAddr = oReader["localMapFileAddr"].ToString(); mapScale = float.Parse(oReader["mapScale"].ToString()); } } Dictionary <string, Router> routerMap = new Dictionary <string, Router>(); Dictionary <string, Member> memberMap = new Dictionary <string, Member>(); // Load all routers cmdString = "SELECT * FROM Routers WHERE siteId = @siteId"; oCmd = new SqlCommand(cmdString, sqlCon); oCmd.Parameters.AddWithValue("@siteId", siteId); using (SqlDataReader oReader = oCmd.ExecuteReader()) { while (oReader.Read()) { Router router = new Router(oReader["Id"].ToString(), oReader["address"].ToString(), oReader["location"].ToString(), Int32.Parse(oReader["x"].ToString()), Int32.Parse(oReader["y"].ToString()), bool.Parse(oReader["isBlocked"].ToString())); routers.Add(router); routerMap.Add(router.routerId, router); } } // Load all members and shifts cmdString = "SELECT * FROM Members m, Shifts s WHERE siteId = @siteId AND m.memberNo=s.memberNo"; oCmd = new SqlCommand(cmdString, sqlCon); oCmd.Parameters.AddWithValue("@siteId", siteId); using (SqlDataReader oReader = oCmd.ExecuteReader()) { string lastId = ""; Member member = null; while (oReader.Read()) { if (lastId.Equals(oReader["memberNo"].ToString())) { member.assignedShifts.Add(new Shift( DateTime.Parse(oReader["start"].ToString()), DateTime.Parse(oReader["end"].ToString()))); } else { lastId = oReader["memberNo"].ToString(); member = new Member(oReader["memberNo"].ToString(), oReader["fName"].ToString(), oReader["mName"].ToString(), oReader["lName"].ToString(), oReader["address"].ToString(), oReader["province"].ToString(), oReader["city"].ToString(), Int32.Parse(oReader["pinNo"].ToString()), oReader["phoneNo"].ToString(), oReader["mobileNo"].ToString(), oReader["isVehicle"].ToString() == "1" ? true : false, oReader["tagId"].ToString()); member.assignedShifts.Add(new Shift( DateTime.Parse(oReader["start"].ToString()), DateTime.Parse(oReader["end"].ToString()))); members.Add(member.memberId, member); memberMap.Add(member.memberId, member); } } } // Load all sensors cmdString = "SELECT * FROM Sensors"; oCmd = new SqlCommand(cmdString, sqlCon); using (SqlDataReader oReader = oCmd.ExecuteReader()) { while (oReader.Read()) { sensors.Add(oReader["Id"].ToString()); } } cmdString = "SELECT * FROM PathElement pe, Routers r WHERE pe.routerId=r.Id AND r.siteId=@siteId ORDER BY timeVisited DESC"; oCmd = new SqlCommand(cmdString, sqlCon); oCmd.Parameters.AddWithValue("@siteId", siteId); using (SqlDataReader oReader = oCmd.ExecuteReader()) { while (oReader.Read()) { Member member = memberMap[oReader["memberId"].ToString()]; Router router = routerMap[oReader["routerId"].ToString()]; // Use the member map and router map from above to create each miner's path member.path.AddLast(router); // Add to router's connected members if this is the member's first router if (member.path.Count == 1) { router.hasConnectedMembers.AddLast(member); } } } } MineSite.GetInstance().Init(siteId, siteName, mapAddr, mapScale, routers, members, sensors); }