public void parseRequest() { String request = streamReadLine(inputStream); string[] tokens = request.Split(' '); if (tokens.Length != 3) { throw new Exception("invalid http request line"); } http_method = tokens[0].ToUpper(); http_url = tokens[1]; http_protocol_versionstring = tokens[2]; var spaceIndex = http_url.IndexOf("%20", System.StringComparison.Ordinal); string s; s = spaceIndex != -1 ? http_url.Substring(0, spaceIndex) : http_url; var url = new Uri("http://www.example.com" + s); var url_path = url.AbsolutePath; NameValueCollection qscollection = HttpUtility.ParseQueryString(url.Query); string ppu_na = null; if (!string.IsNullOrEmpty(qscollection["na"])) { ppu_na = qscollection["na"]; } //var pac = qscollection.Get(0).ToLower(); /* switch (qscollection.Get(0)) * {*/ if (url_path.IndexOf("/home/ts") > -1) { Debug.WriteLine("ts"); using (var context = new db_sqlceEntities()) { try { var package_ts = new package_ts() { pt = url_path, na = qscollection["na"], r1 = Convert.ToInt32(qscollection["r1"]), r2 = Convert.ToInt32(qscollection["r2"]), pw1 = Convert.ToInt32(qscollection["pw1"]), pw2 = Convert.ToInt32(qscollection["pw2"]), gp = Convert.ToInt32(qscollection["gp"]), te = Convert.ToInt32(qscollection["te"]), vc = Convert.ToInt32(qscollection["vc"]), vg = Convert.ToInt32(qscollection["vg"]), ti = Convert.ToInt32(qscollection["ti"]), sn1 = qscollection["sn1"], sn2 = qscollection["sn2"], ismail = false, da = DateTime.ParseExact(qscollection["da"], "yyMMdd", null).Date, date_time_inc = DateTime.Now }; context.package_ts.Add(package_ts); context.SaveChanges(); } catch (DbEntityValidationException ex) { MessageBox.Show(ex.ToString()); } } } if (url_path.IndexOf("/home/iz") > -1) { Debug.WriteLine("iz"); using (var context = new db_sqlceEntities()) { try { string rw_1 = qscollection["r1"].Substring(0, 8); string rw_2 = qscollection["r2"].Substring(0, 8); string r1 = qscollection["r1"].Substring(8, 96); string r2 = qscollection["r2"].Substring(8, 96); IEnumerable <string> groups1 = Enumerable.Range(0, r1.Length / 4) .Select(i => r1.Substring(i * 4, 4)); List <int> rw1 = new List <int>(); foreach (var grp in groups1) { rw1.Add(Convert.ToInt32(grp)); } IEnumerable <string> groups2 = Enumerable.Range(0, r2.Length / 4) .Select(i => r2.Substring(i * 4, 4)); List <int> rw2 = new List <int>(); foreach (var grp in groups2) { rw2.Add(Convert.ToInt32(grp)); } var package_iz = new package_iz() { pt = url_path, na = qscollection["na"], r1 = Convert.ToInt32(rw_1), r1_0 = Convert.ToInt32(groups1.ElementAt(0)), r1_1 = Convert.ToInt32(groups1.ElementAt(1)), r1_2 = Convert.ToInt32(groups1.ElementAt(2)), r1_3 = Convert.ToInt32(groups1.ElementAt(3)), r1_4 = Convert.ToInt32(groups1.ElementAt(4)), r1_5 = Convert.ToInt32(groups1.ElementAt(5)), r1_6 = Convert.ToInt32(groups1.ElementAt(6)), r1_7 = Convert.ToInt32(groups1.ElementAt(7)), r1_8 = Convert.ToInt32(groups1.ElementAt(8)), r1_9 = Convert.ToInt32(groups1.ElementAt(9)), r1_10 = Convert.ToInt32(groups1.ElementAt(10)), r1_11 = Convert.ToInt32(groups1.ElementAt(11)), r1_12 = Convert.ToInt32(groups1.ElementAt(12)), r1_13 = Convert.ToInt32(groups1.ElementAt(13)), r1_14 = Convert.ToInt32(groups1.ElementAt(14)), r1_15 = Convert.ToInt32(groups1.ElementAt(15)), r1_16 = Convert.ToInt32(groups1.ElementAt(16)), r1_17 = Convert.ToInt32(groups1.ElementAt(17)), r1_18 = Convert.ToInt32(groups1.ElementAt(18)), r1_19 = Convert.ToInt32(groups1.ElementAt(19)), r1_20 = Convert.ToInt32(groups1.ElementAt(20)), r1_21 = Convert.ToInt32(groups1.ElementAt(21)), r1_22 = Convert.ToInt32(groups1.ElementAt(22)), r1_23 = Convert.ToInt32(groups1.ElementAt(23)), // r1_24 = Convert.ToInt32(groups1.ElementAt(24)), r2 = Convert.ToInt32(rw_2), r2_0 = Convert.ToInt32(groups2.ElementAt(0)), r2_1 = Convert.ToInt32(groups2.ElementAt(1)), r2_2 = Convert.ToInt32(groups2.ElementAt(2)), r2_3 = Convert.ToInt32(groups2.ElementAt(3)), r2_4 = Convert.ToInt32(groups2.ElementAt(4)), r2_5 = Convert.ToInt32(groups2.ElementAt(5)), r2_6 = Convert.ToInt32(groups2.ElementAt(6)), r2_7 = Convert.ToInt32(groups2.ElementAt(7)), r2_8 = Convert.ToInt32(groups2.ElementAt(8)), r2_9 = Convert.ToInt32(groups2.ElementAt(9)), r2_10 = Convert.ToInt32(groups2.ElementAt(10)), r2_11 = Convert.ToInt32(groups2.ElementAt(11)), r2_12 = Convert.ToInt32(groups2.ElementAt(12)), r2_13 = Convert.ToInt32(groups2.ElementAt(13)), r2_14 = Convert.ToInt32(groups2.ElementAt(14)), r2_15 = Convert.ToInt32(groups2.ElementAt(15)), r2_16 = Convert.ToInt32(groups2.ElementAt(16)), r2_17 = Convert.ToInt32(groups2.ElementAt(17)), r2_18 = Convert.ToInt32(groups2.ElementAt(18)), r2_19 = Convert.ToInt32(groups2.ElementAt(19)), r2_20 = Convert.ToInt32(groups2.ElementAt(20)), r2_21 = Convert.ToInt32(groups2.ElementAt(21)), r2_22 = Convert.ToInt32(groups2.ElementAt(22)), r2_23 = Convert.ToInt32(groups2.ElementAt(23)), //r2_24 = Convert.ToInt32(groups2.ElementAt(24)), pw1 = Convert.ToInt32(qscollection["pw1"]), pw2 = Convert.ToInt32(qscollection["pw2"]), gp = Convert.ToInt32(qscollection["gp"]), te = qscollection["te"], vc = Convert.ToInt32(qscollection["vc"]), vg = Convert.ToInt32(qscollection["vg"]), ti = Convert.ToInt32(qscollection["ti"]), sn1 = qscollection["sn1"], sn2 = qscollection["sn2"], ismail = false, da = DateTime.ParseExact(qscollection["da"], "yyMMdd", null).Date, date_inc = DateTime.Now }; context.package_iz.Add(package_iz); context.SaveChanges(); var que = context.counters.FirstOrDefault(c => c.serial_amspi == ppu_na); if (que != null) { que.serial_r1 = qscollection["sn1"]; que.serial_r2 = qscollection["sn2"]; context.SaveChanges(); } } catch (DbEntityValidationException ex) { MessageBox.Show(ex.ToString()); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } } if (url_path.IndexOf("/home/tm") > -1) { Debug.WriteLine("tm"); using (var context = new db_sqlceEntities()) { try { var package_tm = new package_tm() { pt = url_path, na = qscollection["na"], ismail = false, date_inc = DateTime.Now }; context.package_tm.Add(package_tm); context.SaveChanges(); } catch (DbEntityValidationException ex) { MessageBox.Show(ex.ToString()); } } } if (url_path.IndexOf("/home/on") > -1) { Debug.WriteLine("on"); using (var context = new db_sqlceEntities()) { try { var package_on = new package_on() { pt = url_path, na = qscollection["na"], r1 = Convert.ToInt32(qscollection["r1"]), r2 = Convert.ToInt32(qscollection["r2"]), pw1 = Convert.ToInt32(qscollection["pw1"]), pw2 = Convert.ToInt32(qscollection["pw2"]), gp = Convert.ToInt32(qscollection["gp"]), te = Convert.ToInt32(qscollection["te"]), vc = Convert.ToInt32(qscollection["vc"]), vg = Convert.ToInt32(qscollection["vg"]), ti = Convert.ToInt32(qscollection["ti"]), sn1 = qscollection["sn1"], sn2 = qscollection["sn2"], ismail = false, da = DateTime.ParseExact(qscollection["da"], "yyMMdd", null).Date, date_time_inc = DateTime.Now }; context.package_on.Add(package_on); context.SaveChanges(); } catch (DbEntityValidationException ex) { MessageBox.Show(ex.ToString()); } } } if (url_path.IndexOf("/home/al") > -1) { Debug.WriteLine("al"); var wr_txt = ""; var war = Convert.ToInt32(qscollection.Get(2)); switch (war) { case (0): wr_txt = "Вскрыте крышки!"; break; case (1): wr_txt = "Расход за час для первого счетчика больше предельного!"; break; case (2): wr_txt = "Расход за час для второго счетчика больше предельного!"; break; } using (var context = new db_sqlceEntities()) { try { var package_al = new package_al() { pt = url_path, na = qscollection["na"], wr = war, date_inc = DateTime.Now, ismail = false, wr_txt = wr_txt }; context.package_al.Add(package_al); context.SaveChanges(); var query1 = from c in context.objects_place from cou in context.counters.Where(cou => cou.objects_place_id == c.id).DefaultIfEmpty() where cou.serial_amspi == package_al.na select new { c.name }; string rr = query1.FirstOrDefault().name; MessageBox.Show("ППУ-РМ №: " + package_al.na + " " + rr + " - " + package_al.wr_txt, "АВАРИЯ!", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification); } catch (DbEntityValidationException ex) { MessageBox.Show(ex.ToString()); } } } log.Write(http_url); }
private void LoadMassagesInDb(Message message, Pop3Client client, int i_msg) { string[] separator = new string[] { "\r\n", "\n" }; MessageHeader headers = client.GetMessageHeaders(i_msg); RfcMailAddress from = headers.From; if (from.HasValidMailAddress && from.Address.Equals(this.mail_ppu)) { if (message.Headers.Subject.Contains("_TEST")) { Debug.WriteLine("MAIL_TS"); var mail_strings = message.FindFirstPlainTextVersion() .GetBodyAsText() .Split(separator, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Split(new[] { '=' })); Dictionary <string, string> dict = new Dictionary <string, string>(); foreach (var item in mail_strings) { dict.Add(item[0], item[1]); } using (var context = new db_sqlceEntities()) { try { var package_ts = new package_ts() { pt = "/home/ts", na = dict["na"], r1 = Convert.ToInt32(dict["r1"]), r2 = Convert.ToInt32(dict["r2"]), pw1 = Convert.ToInt32(dict["pw1"]), pw2 = Convert.ToInt32(dict["pw2"]), gp = Convert.ToInt32(dict["gp"]), te = Convert.ToInt32(dict["te"]), vc = Convert.ToInt32(dict["vc"]), vg = Convert.ToInt32(dict["vg"]), ti = Convert.ToInt32(dict["ti"]), sn1 = dict["sn1"], sn2 = dict["sn2"], mail_uid = client.GetMessageUid(i_msg), ismail = true, da = DateTime.ParseExact(dict["da"], "yyMMdd", null).Date, date_time_inc = DateTime.Now }; context.package_ts.Add(package_ts); context.SaveChanges(); } catch (DbEntityValidationException ex) { MessageBox.Show(ex.ToString()); } } log.Write(message.FindFirstPlainTextVersion() .GetBodyAsText()); } if (message.Headers.Subject.Contains("_TM")) { Debug.WriteLine("MAIL_TM"); var mail_strings = message.FindFirstPlainTextVersion() .GetBodyAsText() .Split(separator, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Split(new[] { '=' })); Dictionary <string, string> dict = new Dictionary <string, string>(); foreach (var item in mail_strings) { dict.Add(item[0], item[1]); } using (var context = new db_sqlceEntities()) { try { var package_tm = new package_tm() { pt = "/home/tm", na = dict["na"], ismail = true, mail_uid = client.GetMessageUid(i_msg), date_inc = DateTime.Now }; context.package_tm.Add(package_tm); context.SaveChanges(); } catch (DbEntityValidationException ex) { MessageBox.Show(ex.ToString()); } } } if (message.Headers.Subject.Contains("_ON")) { Debug.WriteLine("MAIL_ON"); var mail_strings = message.FindFirstPlainTextVersion() .GetBodyAsText() .Trim() .Split(separator, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Split(new[] { '=' })); Dictionary <string, string> dict = new Dictionary <string, string>(); foreach (var item in mail_strings) { dict.Add(item[0], item[1]); } using (var context = new db_sqlceEntities()) { try { var package_on = new package_on() { pt = "/home/on", na = dict["na"], r1 = Convert.ToInt32(dict["r1"]), r2 = Convert.ToInt32(dict["r2"]), pw1 = Convert.ToInt32(dict["pw1"]), pw2 = Convert.ToInt32(dict["pw2"]), gp = Convert.ToInt32(dict["gp"]), te = Convert.ToInt32(dict["te"]), vc = Convert.ToInt32(dict["vc"]), vg = Convert.ToInt32(dict["vg"]), ti = Convert.ToInt32(dict["ti"]), sn1 = dict["sn1"], sn2 = dict["sn2"], mail_uid = client.GetMessageUid(i_msg), ismail = true, da = DateTime.ParseExact(dict["da"], "yyMMdd", null).Date, date_time_inc = DateTime.Now }; context.package_on.Add(package_on); context.SaveChanges(); } catch (DbEntityValidationException ex) { MessageBox.Show(ex.ToString()); } } log.Write(message.FindFirstPlainTextVersion() .GetBodyAsText()); } if (message.Headers.Subject.Contains("_AL")) { Debug.WriteLine("MAIL_AL"); var mail_strings = message.FindFirstPlainTextVersion() .GetBodyAsText() .Split(separator, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Split(new[] { '=' })); Dictionary <string, string> dict = new Dictionary <string, string>(); foreach (var item in mail_strings) { dict.Add(item[0], item[1]); } var wr_txt = ""; var war = Convert.ToInt32(dict["w"]); switch (war) { case (0): wr_txt = "Вскрыте крышки!"; break; case (1): wr_txt = "Расход за час для первого счетчика больше предельного!"; break; case (2): wr_txt = "Расход за час для второго счетчика больше предельного!"; break; } using (var context = new db_sqlceEntities()) { try { var package_al = new package_al() { pt = "/home/wr", na = dict["na"], wr = war, date_inc = DateTime.Now, ismail = true, mail_uid = client.GetMessageUid(i_msg), wr_txt = wr_txt }; context.package_al.Add(package_al); context.SaveChanges(); } catch (DbEntityValidationException ex) { MessageBox.Show(ex.ToString()); } } } if (message.Headers.Subject.Contains("_Data")) { Debug.WriteLine("MAIL_IZ"); var mail_strings = message.FindFirstPlainTextVersion() .GetBodyAsText() .Trim() .Split(separator, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Split(new[] { '=' })); Dictionary <string, string> dict = mail_strings.ToDictionary(item => item[0], item => item[1]); using (var context = new db_sqlceEntities()) { try { string rw_1 = dict["r1"].Substring(0, 8); string rw_2 = dict["r2"].Substring(0, 8); string r1 = dict["r1"].Substring(8, 96); string r2 = dict["r2"].Substring(8, 96); // r1 = r1.Replace('g', '0'); // r2 = r2.Replace('g', '0'); r1 = Regex.Replace(r1, @"([^0-9])(\d){3}", "0000"); r2 = Regex.Replace(r2, @"([^0-9])(\d){3}", "0000"); IEnumerable <string> groups1 = Enumerable.Range(0, r1.Length / 4) .Select(i => r1.Substring(i * 4, 4)); List <int> rw1 = new List <int>(); foreach (var grp in groups1) { rw1.Add(Convert.ToInt32(grp)); } IEnumerable <string> groups2 = Enumerable.Range(0, r2.Length / 4) .Select(i => r2.Substring(i * 4, 4)); List <int> rw2 = new List <int>(); foreach (var grp in groups2) { rw2.Add(Convert.ToInt32(grp)); } string ppu_na = null; if (!string.IsNullOrEmpty(dict["na"])) { ppu_na = dict["na"]; } var package_iz = new package_iz() { pt = "/home/iz", na = dict["na"], r1 = Convert.ToInt32(rw_1), r1_0 = Convert.ToInt32(groups1.ElementAt(0)), r1_1 = Convert.ToInt32(groups1.ElementAt(1)), r1_2 = Convert.ToInt32(groups1.ElementAt(2)), r1_3 = Convert.ToInt32(groups1.ElementAt(3)), r1_4 = Convert.ToInt32(groups1.ElementAt(4)), r1_5 = Convert.ToInt32(groups1.ElementAt(5)), r1_6 = Convert.ToInt32(groups1.ElementAt(6)), r1_7 = Convert.ToInt32(groups1.ElementAt(7)), r1_8 = Convert.ToInt32(groups1.ElementAt(8)), r1_9 = Convert.ToInt32(groups1.ElementAt(9)), r1_10 = Convert.ToInt32(groups1.ElementAt(10)), r1_11 = Convert.ToInt32(groups1.ElementAt(11)), r1_12 = Convert.ToInt32(groups1.ElementAt(12)), r1_13 = Convert.ToInt32(groups1.ElementAt(13)), r1_14 = Convert.ToInt32(groups1.ElementAt(14)), r1_15 = Convert.ToInt32(groups1.ElementAt(15)), r1_16 = Convert.ToInt32(groups1.ElementAt(16)), r1_17 = Convert.ToInt32(groups1.ElementAt(17)), r1_18 = Convert.ToInt32(groups1.ElementAt(18)), r1_19 = Convert.ToInt32(groups1.ElementAt(19)), r1_20 = Convert.ToInt32(groups1.ElementAt(20)), r1_21 = Convert.ToInt32(groups1.ElementAt(21)), r1_22 = Convert.ToInt32(groups1.ElementAt(22)), r1_23 = Convert.ToInt32(groups1.ElementAt(23)), // r1_24 = Convert.ToInt32(groups1.ElementAt(24)), r2 = Convert.ToInt32(rw_2), r2_0 = Convert.ToInt32(groups2.ElementAt(0)), r2_1 = Convert.ToInt32(groups2.ElementAt(1)), r2_2 = Convert.ToInt32(groups2.ElementAt(2)), r2_3 = Convert.ToInt32(groups2.ElementAt(3)), r2_4 = Convert.ToInt32(groups2.ElementAt(4)), r2_5 = Convert.ToInt32(groups2.ElementAt(5)), r2_6 = Convert.ToInt32(groups2.ElementAt(6)), r2_7 = Convert.ToInt32(groups2.ElementAt(7)), r2_8 = Convert.ToInt32(groups2.ElementAt(8)), r2_9 = Convert.ToInt32(groups2.ElementAt(9)), r2_10 = Convert.ToInt32(groups2.ElementAt(10)), r2_11 = Convert.ToInt32(groups2.ElementAt(11)), r2_12 = Convert.ToInt32(groups2.ElementAt(12)), r2_13 = Convert.ToInt32(groups2.ElementAt(13)), r2_14 = Convert.ToInt32(groups2.ElementAt(14)), r2_15 = Convert.ToInt32(groups2.ElementAt(15)), r2_16 = Convert.ToInt32(groups2.ElementAt(16)), r2_17 = Convert.ToInt32(groups2.ElementAt(17)), r2_18 = Convert.ToInt32(groups2.ElementAt(18)), r2_19 = Convert.ToInt32(groups2.ElementAt(19)), r2_20 = Convert.ToInt32(groups2.ElementAt(20)), r2_21 = Convert.ToInt32(groups2.ElementAt(21)), r2_22 = Convert.ToInt32(groups2.ElementAt(22)), r2_23 = Convert.ToInt32(groups2.ElementAt(23)), //r2_24 = Convert.ToInt32(groups2.ElementAt(24)), pw1 = Convert.ToInt32(dict["pw1"]), pw2 = Convert.ToInt32(dict["pw2"]), gp = Convert.ToInt32(dict["gp"]), te = dict["te"], vc = Convert.ToInt32(dict["vc"]), vg = Convert.ToInt32(dict["vg"]), ti = Convert.ToInt32(dict["ti"]), sn1 = dict["sn1"], sn2 = dict["sn2"], da = DateTime.ParseExact(dict["da"], "yyMMdd", null).Date, ismail = true, mail_uid = client.GetMessageUid(i_msg), date_inc = DateTime.Now }; context.package_iz.Add(package_iz); context.SaveChanges(); var qu = context.counters.FirstOrDefault(c => c.serial_amspi == ppu_na); if (qu != null) { qu.serial_r1 = dict["sn1"]; qu.serial_r2 = dict["sn2"]; context.SaveChanges(); } } catch (DbEntityValidationException ex) { MessageBox.Show(ex.ToString()); } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } log.Write(message.FindFirstPlainTextVersion() .GetBodyAsText()); } } }