Example #1
0
 private void editAlarm_Click(object sender, EventArgs e)
 {
     if (server.AlarmHandler.AlarmList.Count() > 0)
     {
         AlarmElement  alarmSelected = server.AlarmHandler.GetAlarm((int)AlarmListDGW.Rows[AlarmListDGW.CurrentRow.Index].Cells[0].Value);
         EditAlarmForm newEditForm   = new EditAlarmForm(alarmSelected, (int)AlarmListDGW.Rows[AlarmListDGW.CurrentRow.Index].Cells[0].Value, server.Groups.GetDic());
         newEditForm.Show();
     }
 }
 public EditAlarmForm(AlarmElement editAlarmForm, int Id, Dictionary <string, List <string> > groups)
 {
     InitializeComponent();
     alarmElement = editAlarmForm;
     id           = Id;
     foreach (KeyValuePair <string, List <string> > pair in groups)
     {
         groupCmbox.Items.Add(pair.Key);
     }
     group = groups;
 }
        private void Run()
        {
            //Run Start the HTTP SERVER LISTNER
            try
            {
                listener = new HttpListener();
                if (listenedAddresses != string.Empty)
                {
                    listener.Prefixes.Add(listenedAddresses + "/");
                    listener.Start();
                }
                else
                {
                    isWorked = false;
                }
            }
            catch (Exception listenerException)
            {
                stop();
            }
            //Start the HTTP CLIENT
            try
            {
                if (postAdresses != string.Empty)
                {
                    restClient = new RestClient(postAdresses);
                    Post       = new HttpPost();
                }
            }
            catch (Exception restClientException)
            {
                stop();
                throw new Exception(restClientException.Message);
            }

            //EVERYTHING IS OK!
            while (isWorked)
            {
                string responseString = "";
                byte[] buffer;
                try
                {
                    System.IO.Stream output;
                    //WAIT FOR GETTING VALUES
                    var context = listener.GetContext();

                    //Get values

                    string[] request    = context.Request.RawUrl.Remove(0, 1).Split(',');
                    string   requestId  = request[0];
                    string   valueOfReq = request[1];
                    int      idAlarm    = int.Parse(requestId);

                    AlarmElement Alarm = AlarmHandler.GetAlarm(idAlarm);
                    if (Alarm != null)
                    {
                        List <string> PhonesNumbers = new List <string>();
                        List <string> personList    = Groups.GetGroup(Alarm.group);
                        if (personList != null)
                        {
                            foreach (string p in personList)
                            {
                                string phoneNumber = Persons.GetPersons(p).Phone;
                                PhonesNumbers.Add(phoneNumber);
                            }

                            if (PhonesNumbers != null)
                            {
                                string textToSend = Alarm.alarmText.Replace("$", valueOfReq);
                                //Send SMS
                                foreach (string Phone in PhonesNumbers)
                                {
                                    Post.SendSMS(restClient, Phone, textToSend, user, password);
                                }
                                responseString = "AOK";
                                buffer         = Encoding.UTF8.GetBytes(responseString);
                                context.Response.ContentLength64 = buffer.Length;
                                output = context.Response.OutputStream;
                                output.Write(buffer, 0, buffer.Length);
                                output.Close();
                            }
                            else
                            {
                                responseString = "NOK";
                                buffer         = Encoding.UTF8.GetBytes(responseString);
                                context.Response.ContentLength64 = buffer.Length;
                                output = context.Response.OutputStream;
                                output.Write(buffer, 0, buffer.Length);
                                output.Close();
                            }
                        }
                        else
                        {
                            responseString = "NOK";
                            buffer         = Encoding.UTF8.GetBytes(responseString);
                            context.Response.ContentLength64 = buffer.Length;
                            output = context.Response.OutputStream;
                            output.Write(buffer, 0, buffer.Length);
                            output.Close();
                        }
                    }
                    else
                    {
                        responseString = "NOK";
                        buffer         = Encoding.UTF8.GetBytes(responseString);
                        context.Response.ContentLength64 = buffer.Length;
                        output = context.Response.OutputStream;
                        output.Write(buffer, 0, buffer.Length);
                        output.Close();
                    }
                }
                catch (Exception)
                {
                    //stop();//NEED TO CLOSE PORT AND DISPLAY IT!, I DONT NEED TO CLOSE THE PORT IF THERE IS A NOT FOUND PROBLEM!
                }
            }
            stop();
        }