SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
AlertsMgr.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Net;
5 using System.IO;
6 using Seci.Definitions;
7 using System.ComponentModel;
8 using System.Xml;
9 using System.Xml.Serialization;
10 
11 namespace Seci.Managers
12 {
13  public static class AlertsMgr
14  {
15  private static string PHONE_NUMBERS_FILENAME = "phone_numbers.xml";
16 
17  private static Tools.PhoneNumbersList PhoneNumbers = new Tools.PhoneNumbersList();
18  private static Tools.AlertsBox Alerts = new Tools.AlertsBox();
19  private static BackgroundWorker _sender;
20 
21  public static void SetAlerts(Tools.AlertsBox alerts)
22  {
23  Alerts = alerts;
24  }
25 
26  public static void SetPhoneNumbers(Tools.PhoneNumbersList numbers)
27  {
28  PhoneNumbers = numbers;
29  }
30 
31  public static void CheckBlockInRange(Definitions.BlockInfo block)
32  {
33  if (Alerts != null && Alerts.BlockLimitsAlerts.ContainsKey(block.BlockName.ToLower()))
34  {
35  BlockAlert temp = Alerts.BlockLimitsAlerts[block.BlockName.ToLower()];
36  double value;
37 
38  if (!temp.Enabled) return;
39 
40  if (Double.TryParse(block.CurrentValue, out value))
41  {
42  if (value <= temp.HighLimit && value >= temp.LowLimit)
43  {
44  //In range
45  if (temp.OutOfRange)
46  {
47  //If it comes back into range then reset the last message time
48  temp.TimeWentOutOfRange = new DateTime();
49  }
50  temp.OutOfRange = false;
51  }
52  else
53  {
54  if (!temp.OutOfRange)
55  {
56  temp.OutOfRange = true;
57  temp.TimeWentOutOfRange = DateTime.Now;
58  }
59 
60  //Out of range
61  TimeSpan diff = DateTime.Now - temp.TimeWentOutOfRange;
62  if (diff > temp.DelayBeforeSms)
63  {
64  //Check a message has not been sent in the last hour
65  if (DateTime.Now - temp.LastMessageSent > new TimeSpan(1, 0, 0))
66  {
67  //Send a message
68  temp.LastMessageSent = DateTime.Now;
69  sendMessage("The " + block.BlockName + " block has gone out of range on " + Seci.Definitions.Status.Instrument.ToUpper());
70  }
71  }
72  }
73  }
74  }
75  }
76 
77  public static void CheckInWaiting(Boolean waiting)
78  {
79  if (Alerts != null && Alerts.WaitingStateAlert != null)
80  {
81  if (waiting)
82  {
83  if (!Alerts.WaitingStateAlert.OutOfRange)
84  {
85  Alerts.WaitingStateAlert.OutOfRange = true;
86  Alerts.WaitingStateAlert.TimeWentOutOfRange = DateTime.Now;
87  }
88 
89  TimeSpan diff = DateTime.Now - Alerts.WaitingStateAlert.TimeWentOutOfRange;
90  if (diff > Alerts.WaitingStateAlert.DelayBeforeSms)
91  {
92  //Check a message has not been sent in the last hour
93  if (DateTime.Now - Alerts.WaitingStateAlert.LastMessageSent > new TimeSpan(1, 0, 0))
94  {
95  //Send a message
96  Alerts.WaitingStateAlert.LastMessageSent = DateTime.Now;
97 
98  sendMessage("The " + Seci.Definitions.Status.Instrument.ToUpper() + " system has been in the WAITING state for longer than the set limit");
99  }
100  }
101  }
102  else
103  {
104  if (Alerts.WaitingStateAlert.OutOfRange)
105  {
106  //If it comes out of waiting then reset the last message time
107  Alerts.WaitingStateAlert.TimeWentOutOfRange = new DateTime();
108  }
109  Alerts.WaitingStateAlert.OutOfRange = false;
110  }
111  }
112  }
113 
114  private static void sendMessage(String message)
115  {
116  _sender = new BackgroundWorker();
117  _sender.DoWork += new DoWorkEventHandler(_sender_DoWork);
118  _sender.RunWorkerAsync(message);
119  }
120 
121  static void _sender_DoWork(object sender, DoWorkEventArgs e)
122  {
123  String message = e.Argument.ToString();
124 
125  if (!String.IsNullOrEmpty(message) && PhoneNumbers.Numbers.Count > 0)
126  {
127  foreach (Tools.PhoneNumber pn in PhoneNumbers.Numbers)
128  {
129  try
130  {
131  // Create a request using a URL that can receive a post.
132  WebRequest request = WebRequest.Create("http://www.kapow.co.uk/scripts/sendsms.php");
133  // Set the Method property of the request to POST.
134  request.Method = "POST";
135  // Create POST data and convert it to a byte array.
136  string postData = "username=isissms&password=camacdead&mobile=" + pn.Number + "&sms=" + message;
137  byte[] byteArray = Encoding.UTF8.GetBytes(postData);
138  // Set the ContentType property of the WebRequest.
139  request.ContentType = "application/x-www-form-urlencoded";
140  // Set the ContentLength property of the WebRequest.
141  request.ContentLength = byteArray.Length;
142  // Get the request stream.
143  Stream dataStream = request.GetRequestStream();
144  // Write the data to the request stream.
145  dataStream.Write(byteArray, 0, byteArray.Length);
146  // Close the Stream object.
147  dataStream.Close();
148  // Get the response.
149  WebResponse response = request.GetResponse();
150  }
151  catch(Exception err)
152  {
153  Seci.Helpers.ErrorLogger.SeciError("Alerts Manager", err);
154  }
155  }
156  }
157  }
158 
160  {
161  Tools.PhoneNumbersList copy = PhoneNumbers.Clone() as Tools.PhoneNumbersList;
162  return copy;
163  }
164 
166  {
167  Tools.AlertsBox copy = Alerts.Clone() as Tools.AlertsBox;
168  return copy;
169  }
170 
172  {
173  Tools.AlertsBox copy = Alerts.Clone() as Tools.AlertsBox;
174  Tools.AlertsBox newcopy = new Tools.AlertsBox();
175 
176  newcopy.WaitingStateAlert = copy.WaitingStateAlert;
177  //newcopy.PhoneNumbers = copy.PhoneNumbers;
178 
179  foreach (String key in copy.BlockLimitsAlerts.Keys)
180  {
181  //Don't add the alert if the block does not exist any more and if it belongs to a subconfiguration
182  if (BlockMgr.Blocks.Contains(key) && String.IsNullOrEmpty(Alerts.BlockLimitsAlerts[key].OwningConfiguration))
183  {
184  newcopy.AddBlockAlert(copy.BlockLimitsAlerts[key].Blockname, copy.BlockLimitsAlerts[key].BlockType, copy.BlockLimitsAlerts[key].LowLimit, copy.BlockLimitsAlerts[key].HighLimit, copy.BlockLimitsAlerts[key].Enabled);
185  }
186  }
187 
188  return newcopy;
189  }
190 
198  public static void AddComponentBox(Tools.AlertsBox master, Tools.AlertsBox toAdd, String compName)
199  {
200  if (master == null)
201  {
202  master = Alerts;
203  }
204 
205  //Only use the sub-config's waiting alert if the main config does not have one
206  if (toAdd.WaitingStateAlert != null && master.WaitingStateAlert == null)
207  {
208  master.WaitingStateAlert = toAdd.WaitingStateAlert;
209  master.WaitingStateAlert.OwningConfiguration = compName;
210  }
211 
212  //Add the alerts
213  foreach (String key in toAdd.BlockLimitsAlerts.Keys)
214  {
215  //Don't overwrite any set in the main configuration
216  if (!master.BlockLimitsAlerts.ContainsKey(key))
217  {
218  master.BlockLimitsAlerts.Add(key, toAdd.BlockLimitsAlerts[key]);
219  master.BlockLimitsAlerts[key].OwningConfiguration = compName;
220  }
221  }
222  }
223 
224  public static void ClearAlerts()
225  {
226  Alerts = new Tools.AlertsBox();
227  }
228 
229  public static void SavePhoneNumbers()
230  {
231  FileStream stream = new FileStream(Seci.Definitions.Status.ConfigDir + "\\" + PHONE_NUMBERS_FILENAME, FileMode.Create);
232 
233  try
234  {
235  //Serialise the config
236  XmlSerializer serialize = new XmlSerializer(typeof(Tools.PhoneNumbersList));
237  serialize.Serialize(stream, PhoneNumbers);
238 
239  }
240  catch (Exception e)
241  {
242  //throw error up to next level
243  //throw new ArgumentException(e.Message);
244  }
245  finally
246  {
247  stream.Close();
248  }
249  }
250 
251  public static void LoadPhoneNumbers()
252  {
253  FileInfo fi = new FileInfo(Seci.Definitions.Status.ConfigDir + "\\" + PHONE_NUMBERS_FILENAME);
254 
255  if (!fi.Exists)
256  {
257  return;
258  }
259 
260  FileStream stream = new FileStream(fi.FullName, FileMode.Open, FileAccess.Read);
261 
262  try
263  {
264  XmlSerializer deserialize = new XmlSerializer(typeof(Tools.PhoneNumbersList));
265  PhoneNumbers = (Tools.PhoneNumbersList)deserialize.Deserialize(stream);
266  }
267  catch (Exception e)
268  {
269  //throw error up to next level
270  //throw new ArgumentException(e.Message);
271  }
272  finally
273  {
274  stream.Close();
275  }
276  }
277  }
278 }
DateTime LastMessageSent
Definition: Alert.cs:17
Boolean Contains(String blockName)
Wrapper for standard Dictionary &quot;Contains&quot; All keys are lowercase.
static Tools.PhoneNumbersList GetPhoneNumbersCopy()
Definition: AlertsMgr.cs:159
static void sendMessage(String message)
Definition: AlertsMgr.cs:114
static void SetPhoneNumbers(Tools.PhoneNumbersList numbers)
Definition: AlertsMgr.cs:26
static BlockDictionary Blocks
Definition: BlockMgr.cs:21
static void _sender_DoWork(object sender, DoWorkEventArgs e)
Definition: AlertsMgr.cs:121
static Tools.AlertsBox GetAlertsCopy()
Definition: AlertsMgr.cs:165
The manager class for Block related stuff.
Definition: BlockMgr.cs:11
static void AddComponentBox(Tools.AlertsBox master, Tools.AlertsBox toAdd, String compName)
Method for adding another AlertsBox to the current AlertsBox, i.e. when loading a sub-configuration...
Definition: AlertsMgr.cs:198
static void SetAlerts(Tools.AlertsBox alerts)
Definition: AlertsMgr.cs:21
TimeSpan DelayBeforeSms
Definition: Alert.cs:19
static BackgroundWorker _sender
Definition: AlertsMgr.cs:19
static Tools.AlertsBox GetAlertsCopyForSaving()
Definition: AlertsMgr.cs:171
static void ClearAlerts()
Definition: AlertsMgr.cs:224
static void CheckInWaiting(Boolean waiting)
Definition: AlertsMgr.cs:77
static void LoadPhoneNumbers()
Definition: AlertsMgr.cs:251
static void SavePhoneNumbers()
Definition: AlertsMgr.cs:229
static void CheckBlockInRange(Definitions.BlockInfo block)
Definition: AlertsMgr.cs:31