public MainForm() { InitializeComponent(); dbconnprop = new DatabaseConnectionProperties("mccsrv0", null, "accesscontrol", "MaMCaq9Jb3fvVr7d"); try { if (File.Exists("appstate.bin")) { FileStream fstream = new FileStream("appstate.bin", FileMode.Open); BinaryFormatter bFormatter = new BinaryFormatter(); dbconnprop = (DatabaseConnectionProperties)bFormatter.Deserialize(fstream); expander_monitors = (List <ExpanderMonitor>)bFormatter.Deserialize(fstream); panel_monitors = (List <PanelMonitor>)bFormatter.Deserialize(fstream); control_groups = (List <AccessControlGroup>)bFormatter.Deserialize(fstream); fstream.Close(); } } catch (Exception ex) { ErrorLogManager.AppendLog("Failed To Read Configuration From File", true); try{ File.Delete("appstate.bin"); } catch (Exception ex2) { ErrorLogManager.AppendLog("Failed To Delete Configuration File", true); } } AccessControlLogManager.DatabaseConnectionProperties = dbconnprop; ErrorLogManager.AppendLog("Application Initialized", true); }
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; if (closing_thread == null || !closing_thread.IsAlive) { closing_thread = new Thread(delegate() { Stop(); //someone has tried to close the form, save the settings try { FileStream fstream = new FileStream("appstate.bin", FileMode.Create); BinaryFormatter bFormatter = new BinaryFormatter(); bFormatter.Serialize(fstream, dbconnprop); bFormatter.Serialize(fstream, expander_monitors); bFormatter.Serialize(fstream, panel_monitors); bFormatter.Serialize(fstream, control_groups); fstream.Close(); } catch (Exception ex) { ErrorLogManager.AppendLog("Error Writing Configuration To File", true); } Invoke(new Action(() => Dispose())); ErrorLogManager.AppendLog("Application Closed", true); }); closing_thread.Start(); } }
private void Stop() { //stop other tasks here if (altmon != null) { altmon.Stop(); altmon = null; } //accgrpsmon.Stop(); foreach (ExpanderMonitor expmon in expander_monitors) { expmon.Stop(); } foreach (PanelMonitor pnlmon in panel_monitors) { pnlmon.Stop(); } server_active = false; ErrorLogManager.AppendLog("Server Stopped", true); }
private bool OpenConnection() { try { tcpClient = new TcpClient(); tcpClient.Connect(connprop.IPAddress, connprop.TCPPort); netStream = tcpClient.GetStream(); netStream.ReadTimeout = 2000; netStream.WriteTimeout = 200; return(true); } catch (Exception ex) { ErrorLogManager.AppendLog("Failed To Connect To Panel - \"" + connprop.ToString() + "\"", true); return(false); } }
private void Start() { foreach (ExpanderMonitor expmon in expander_monitors) { expmon.Start(); } foreach (PanelMonitor pnlmon in panel_monitors) { pnlmon.Start(); } //accgrpsmon = new AccessControlGroupMonitor(control_groups); //accgrpsmon.Start(); altmon = new AltMonitor(expander_monitors[0], panel_monitors, dbconnprop, "default"); altmon.Start(); //start other tasks here server_active = true; ErrorLogManager.AppendLog("Server Started", true); }
private void NewCardPresented(PanelMonitor pnlmon, PanelEventArgs pnlevargs) { try { using (MySqlConnection sqlconn = new MySqlConnection(dbconnprop.ConnectionString)) { sqlconn.Open(); //lookup using (MySqlCommand cmdName = new MySqlCommand("select data from `" + table_name + "` where uid=" + pnlevargs.PanelState.Card.UID, sqlconn)) using (MySqlDataReader reader = cmdName.ExecuteReader()) { if (reader.Read()) { if (reader[0] != DBNull.Value) { byte[] bytes = (byte[])reader["data"]; AccessProperties accessprop; using (MemoryStream ms = new MemoryStream()) { ms.Write(bytes, 0, bytes.Length); ms.Position = 0; accessprop = (AccessProperties)bf.Deserialize(ms); } //add to processing queue if (!accessprop.ForceDisable) { if (accessprop.EnabledFrom <= DateTime.Now || accessprop.ForceEnable) { if (accessprop.EnabledTo >= DateTime.Now || accessprop.ForceEnable) { //card is active; continue verifying List <ExpanderModificationProperties> temp = accessprop.ProcessActivations(); int associated_car = pnlmon.AssociatedCar; if (temp.Count > 0) { pnlmon.AnimateLED(PanelCommand.ANIMATION_APPROVAL); PanelMonitor monitor = pnlmon; AccessControlCard card = pnlevargs.PanelState.Card; Task.Run(delegate() { AccessControlLogManager.AddAccessControlLogEntry(card.UID, new AccessControlLogEntry(DateTime.Now, "User Approved Entry", card, expmon.ToString(), monitor.ToString())); }); } else { pnlmon.AnimateLED(PanelCommand.ANIMATION_DECLINE); PanelMonitor monitor = pnlmon; AccessControlCard card = pnlevargs.PanelState.Card; Task.Run(delegate() { AccessControlLogManager.AddAccessControlLogEntry(card.UID, new AccessControlLogEntry(DateTime.Now, "User Declined Entry", card, expmon.ToString(), monitor.ToString())); }); } foreach (ExpanderModificationProperties expmodprop in temp) { expmodprop.AssociatedCar = associated_car; expmodprops.Add(expmodprop); } } else { pnlmon.AnimateLED(PanelCommand.ANIMATION_DECLINE); } } else { pnlmon.AnimateLED(PanelCommand.ANIMATION_DECLINE); } } else { pnlmon.AnimateLED(PanelCommand.ANIMATION_DECLINE); } } } else { pnlmon.AnimateLED(PanelCommand.ANIMATION_DECLINE); } } } } catch (MySqlException mysqlex) { ErrorLogManager.AppendLog("AltMonitor - MySqlException Occured During Card Lookup", true); } catch (Exception ex) { ErrorLogManager.AppendLog("AltMonitor - General Error Occured During Card Lookup", true); } }