Esempio n. 1
0
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            if (ValidateInput())
            {
                if (serverPtr2 == null)
                {
                    MessageBox.Show("You didn't started server");
                    return;
                }
                HashTableValues htv = new HashTableValues();
                htv.valid      = true;
                htv.ip         = textBox2.Text;
                htv.locationID = textBox3.Text;
                // Its valid, update Hashtable, update sender IP, and clear texboxes
                HashtableAndDatabaseClass.AddToHashTable(textBox1.Text, htv);
                serverPtr2.updateSender(textBox2.Text);

                // Update setting file
                Properties.Settings.Default.FormManualMAC      = textBox1.Text;
                Properties.Settings.Default.FormManualIP       = textBox2.Text;
                Properties.Settings.Default.FormManualLocation = textBox3.Text;
                Properties.Settings.Default.Save(); // Make it persistant


                // Clear, in case u want to add new device
                textBox2.Text = "";
                textBox1.Text = "";
                textBox3.Text = "";
            }
        }
        private void UpdateDatabase()
        {
            Thread t = new Thread(() => // I want it to be async
            {
                try
                {
                    HashtableAndDatabaseClass.FetchValuesFromDatabase(oracleDB);
                    DateTime localDate  = DateTime.Now;
                    var culture         = new CultureInfo("en-GB");
                    string formatedTime = localDate.ToString(culture);
                    SetDBText("DB: Last time updated > " + formatedTime);
                    SetDBStatusText("DB Status: OK");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Retrieve from database failed");
                    Console.WriteLine(e.ToString());
                    SetDBStatusText("DB Status: DB is down");
                }
                finally
                {
                    updateDBChangeEnabledState("true"); // Update button enable status
                }
            }
                                  ); // End lambda expression

            t.Start();
        }
Esempio n. 3
0
        } // End constructor

        public void StartReceive()
        {
            try
            {
                using (socket)
                {
                    formMain.SetStatusText("Status: Running");
                    formMain.ChangeButtonState(true);
                    while (true)
                    {
                        byte[] data = new byte[1024];

                        if (socket.Available > 0)
                        {
                            IPEndPoint tempasd = new IPEndPoint(IPAddress.Any, 0);
                            data = socket.Receive(ref tempasd);
                            if (data.GetLength(0) == 4) // Is this get status response
                            {
                                MessageProcesser.ProcessStatusResponse(data);
                            }
                            else // For now it is only received Alarm
                            {
                                string     xmlData = System.Text.Encoding.Default.GetString(data);
                                senderInfo si      = new senderInfo();

                                // Assign default value
                                si.errorCode = "none";

                                si = MessageProcesserXML.ProcessAlarmResponseXML(xmlData);
                                if (si.error)
                                {
                                    continue; // Something went wrong, abort
                                }

                                si.trueIPAddress = HashtableAndDatabaseClass.FetchIPFromMac(si.mac);
                                if (si.trueIPAddress == null)
                                {
                                    continue; // Hashtable doesn't countain that MAC, abort everything (writing in log and display too)
                                }
                                si.locationID = HashtableAndDatabaseClass.FetchLocationIDFromMac(si.mac);
                                // we know that we have that key, so dont need to check

                                if (!formMain.GetDebugStatus())
                                {   // If debug checkbox is not checked try to write to database
                                    try
                                    {
                                        HashtableAndDatabaseClass.InsertMessageInDatabase(si, Storage.ReadConnectionString());
                                        formMain.SetDBStatusText("DB Status: OK");
                                    }
                                    catch (Exception)
                                    {
                                        //WriteOnDisplayAndStorage(si);
                                        WriteOnDisplayAndStorageDBDown(si);
                                        formMain.SetDBStatusText("DB Status: DB is down"); // Database is down, update status
                                        continue;
                                    }
                                }
                                // Write log and to display
                                WriteOnDisplayAndStorage(si); // Will use information stored on si structure


                                // Form responce
                                byte[] finalMessage = Encoding.ASCII.GetBytes("ACK " + si.seq);

                                si.trueIPAddress = si.trueIPAddress.Trim(); // Some people use whitespaces infront of IP  :(
                                try
                                {
                                    sender = new IPEndPoint(IPAddress.Parse(si.trueIPAddress), defaultUDPSendingPort);
                                    socket.Send(finalMessage, finalMessage.Length, sender);
                                }
                                catch
                                {
                                    Console.WriteLine("IP from database is corrupted \n");
                                    FormCustomConsole.AddText("IP from database is corrupted" + Environment.NewLine);
                                }
                            } // End else from get data length
                        }     // End  if (socket.Available > 0)

                        if (Kill == true)
                        {
                            if (socket.Available > 0)
                            {
                                Console.WriteLine("More data needs to be read first \n");
                                continue;
                            }
                            Kill = false;
                            Console.WriteLine("Phase Exit \n");
                            FormCustomConsole.AddText("Phase Exit" + Environment.NewLine);
                            formMain.SetStatusText("Status: Stopped");
                            isActive = false;
                            return;
                        }
                        Thread.Sleep(10);
                    } // While
                }     // End using(socket)
            }         // End try
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine("Something went wrong with server \n");
                FormCustomConsole.AddText("Something went wrong with server" + Environment.NewLine);
                formMain.SetStatusText("Status: Error");
                isActive = false;
            }
        } // StartReceive