Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.01 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Media;
  4. using System.Net.Mail;
  5. using System.Text;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Net;
  10. using System.Data.OleDb;
  11. using System.Data;
  12. using OfficeOpenXml;
  13. using OfficeOpenXml.Drawing;
  14. using System.Linq;
  15.  
  16.  
  17.  
  18. namespace WebApplication1
  19. {
  20. public partial class WebForm1 : System.Web.UI.Page
  21. {
  22. class oranow
  23. {
  24. public DateTime OraPassaggio { get; set; }
  25. }
  26.  
  27. public static string SearchNameInExcel(string path, string testosparato)
  28. {
  29. string Nome = testosparato;
  30. using (var pck = new OfficeOpenXml.ExcelPackage())
  31. {
  32. using (var stream = File.OpenRead(path))
  33. {
  34. pck.Load(stream);
  35. }
  36. var ws = pck.Workbook.Worksheets.First();
  37. var startRow = 2;
  38. bool esci = false;
  39. for (var rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
  40. {
  41. if (esci)
  42. break;
  43. var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
  44.  
  45. foreach (var cell in wsRow)
  46. {
  47. if (esci)
  48. {
  49. Nome = cell.Text;
  50. break;
  51. }
  52.  
  53. if (cell.Text == testosparato)
  54. {
  55. esci = true;
  56. }
  57. }
  58.  
  59. }
  60. }
  61. return Nome;
  62. }
  63.  
  64. protected void TextBox1_TextChanged(object sender, EventArgs e)
  65. {
  66. #region Dichiarazione Variabili
  67. Label1.Text = "";
  68. Label2.Text = "";
  69. string testosparato = "";
  70. bool presente = false;
  71. testosparato = ((TextBox)sender).Text;
  72.  
  73.  
  74. //string file = @"C:\CellaFiles\LogInfo.txt";
  75.  
  76.  
  77. string mydocpath = @"C:\Users\pc\Desktop\Sito\File CELLA"; // PUBBLICATO
  78. string file = @mydocpath + @"\LogInfo.txt";
  79. // string infoFile = @"C:\Users\pc\Desktop\Sito\File CELLA\LogRegister.txt";
  80.  
  81. int count = 0;
  82.  
  83. #endregion
  84. string nome = SearchNameInExcel(@"C:\Users\pc\Desktop\Sito\File CELLA\Nominativi Cella.xlsx", testosparato); //PUBBLICO
  85. //string nome = SearchNameInExcel(@"C:\Users\alfieri.michele\Desktop\Progetto\Progetto cella - locale TEST\Nominativi Cella.xlsx", testosparato);
  86. if (!string.IsNullOrEmpty(nome)) //se il testo sparato è vuoto non fa nulla
  87. {
  88.  
  89. StringBuilder FileText = new StringBuilder();
  90. string linetemp = null;
  91. if (System.IO.File.Exists(file)) //se il file esiste
  92. {
  93. #region leggo il file e controllo se utente è già dentro
  94. using (StreamReader sr = new StreamReader(file))
  95. {
  96. while ((linetemp = sr.ReadLine()) != null)
  97. {
  98. count += 1;
  99. string[] utente = linetemp.Split('_');
  100. if (utente[0] == nome)
  101. {
  102. //presente
  103. presente = true;
  104. }
  105. else
  106. {
  107. //salva riga in file
  108. FileText.AppendLine(linetemp);
  109. }
  110. }
  111. }
  112. #endregion
  113. }
  114.  
  115. #region salvo ingresso e stampo status
  116. if (presente)
  117. {
  118. //deve uscire
  119. Label1.Text = "USCITA: " + nome; //+ " " + DateTime.Now.ToString("HH:mm");
  120. count -= 1;
  121. string totale = nome + "_" + DateTime.Now.ToString("dd/MM/yyyy HH:mm") + "_" + "USCITA" + System.Environment.NewLine;
  122. File.AppendAllText(@"C:\Users\pc\Desktop\Sito\File CELLA\LogRegister.txt", totale);
  123. }
  124. else
  125. {
  126. //è entrato
  127. FileText.AppendLine(nome + "_" + DateTime.Now.ToString("dd/MM/yyyy HH:mm"));
  128.  
  129. Label2.Text = "ENTRATA: " + nome; //+ " " + DateTime.Now.ToString("HH:mm");
  130. count += 1;
  131.  
  132. string totale = nome + "_" + DateTime.Now.ToString("dd/MM/yyyy HH:mm") + "_" + "ENTRATA" + System.Environment.NewLine;
  133. File.AppendAllText(@"C:\Users\pc\Desktop\Sito\File CELLA\LogRegister.txt", totale); //PUBBLICO
  134. // File.AppendAllText(@"C:\CellaFiles\LogRegister.txt", totale); //TEST
  135.  
  136.  
  137.  
  138. }
  139. #endregion
  140.  
  141. #region creo file
  142. using (StreamWriter sw = new StreamWriter(file))
  143. {
  144. sw.Write(FileText.ToString());
  145. }
  146. #endregion
  147. }
  148.  
  149. ((TextBox)sender).Text = "";
  150. Label3.Text = count.ToString();
  151. CheckAlert();
  152.  
  153. if (count == 0)
  154. {
  155. string ControlloCella = @"C:\Users\pc\Desktop\Sito\File CELLA\ControlSendMessage.txt";
  156. File.WriteAllText(ControlloCella, String.Empty);
  157. };
  158. }
  159. #region gestione alert e controllo
  160. protected void CheckAlert()
  161. {
  162. //string ControlloTelegram = @"C:\CellaFiles\ControlSendMessage.txt";
  163. string ControlloTelegram = @"C:\Users\pc\Desktop\Sito\File CELLA\ControlSendMessage.txt";
  164. StringBuilder FileText = new StringBuilder();
  165. bool lampeggioGiallo = true;
  166. bool alert = false;
  167. // string mydocpath = @"C:\CellaFiles";
  168. // string file = @mydocpath + @"\LogInfo.txt";
  169.  
  170. string file = @"C:\Users\pc\Desktop\Sito\File CELLA\LogInfo.txt";
  171.  
  172. int Check = 0;
  173.  
  174.  
  175. string[] ControlloTelegramFiles = File.ReadAllLines(ControlloTelegram);
  176. if (ControlloTelegramFiles.Length > 0)
  177. if (ControlloTelegramFiles[0].ToString().Trim() != string.Empty)
  178. Check = ControlloTelegramFiles.Length;
  179.  
  180.  
  181. string linetemp = null;
  182. string NomeUtente = "";
  183. if (System.IO.File.Exists(file))
  184. {
  185. using (StreamReader sr = new StreamReader(file))
  186. {
  187. while ((linetemp = sr.ReadLine()) != null)
  188. {
  189. string[] ora = linetemp.Split('_');
  190.  
  191. DateTime oratime = Convert.ToDateTime(ora[1]);
  192. if (DateTime.Now >= oratime.AddMinutes(1))
  193. {
  194. {
  195. alert = true;
  196. lampeggioGiallo = false; //NUOVA AGGIUNTA, 1 PERSONA IN CELLA LAMPEGGIA ROSSO
  197. NomeUtente = ora[0];
  198. }
  199. }
  200. }
  201. }
  202. }
  203. #endregion
  204. #region se scatta l'alert
  205. if (alert)
  206. {
  207. //SoundPlayer sound = new SoundPlayer(@"C:\AudioFilesCella\Alert.wav"); // TEST
  208. SoundPlayer sound = new SoundPlayer(@"C:\Users\pc\Desktop\Sito\Audio per CELLA\Alert.wav"); //PUBBLICO
  209. sound.PlayLooping();
  210. if (Check == 0)
  211. {
  212. try
  213. {
  214.  
  215. SmtpClient client = new SmtpClient();
  216. client.Port = 587;
  217. client.Host = "smtp.gmail.com";
  218. client.EnableSsl = true;
  219. client.Timeout = 10000;
  220. client.DeliveryMethod = SmtpDeliveryMethod.Network;
  221. client.UseDefaultCredentials = false;
  222. client.Credentials = new System.Net.NetworkCredential("alertpersonaincella@bomigroup.com", "bomi2017");
  223. MailMessage mm = new MailMessage("alertpersonaincella@bomigroup.com", "michele.alfieri@bomigroup.com"); // qualita@bomigroup.com, manutenzioniesicurezza@bomigroup.com, Alessio.Piasentin@bomigroup.com, maria.pirino@bomigroup.com, Sabrina.Sala@bomigroup.com");
  224. mm.Subject = "Alert cella -20° + Nome utente: " + NomeUtente;
  225. mm.Body = "La Persona:" + NomeUtente + " Ha superato il tempo limite dentro la cella, si è pregati di controllare";
  226. mm.BodyEncoding = UTF8Encoding.UTF8;
  227. mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  228. client.Send(mm);
  229.  
  230. /* string urlAddress = "https://api.telegram.org/bot337704739:AAH4Ljzw_lLNtnqaG5B5otgk5659dq3AWK0/sendmessage?chat_id=-1001062381722&text= E' stato rilevato un accesso prolungato nella cella -20° di Vaprio d'Adda, da parte dell'Utente: " + NomeUtente;
  231. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
  232. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  233. if (response.StatusCode == HttpStatusCode.OK)
  234. {
  235. }
  236. response.Close();*/
  237.  
  238. }
  239. catch
  240. {
  241. SmtpClient client = new SmtpClient();
  242. client.Port = 587;
  243. client.Host = "smtp.gmail.com";
  244. client.EnableSsl = true;
  245. client.Timeout = 10000;
  246. client.DeliveryMethod = SmtpDeliveryMethod.Network;
  247. client.UseDefaultCredentials = false;
  248. client.Credentials = new System.Net.NetworkCredential("alertpersonaincella@bomigroup.com", "bomi2017");
  249. MailMessage mm = new MailMessage("alertpersonaincella@bomigroup.com", "michele.alfieri@bomigroup.com");
  250. mm.Subject = "Errore invio alert, utente:" + NomeUtente;
  251. mm.Body = NomeUtente + "_" + "Errore invio email e telegram";
  252. mm.BodyEncoding = UTF8Encoding.UTF8;
  253. mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
  254. client.Send(mm);
  255. }
  256. }
  257. using (StreamWriter WriteInCheckCella = new StreamWriter(ControlloTelegram))
  258. {
  259. WriteInCheckCella.Write("Il mio messaggio fisso");
  260. WriteInCheckCella.Flush();
  261. }
  262.  
  263. ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "AvviaTutto", "avvia()", true);
  264. //SoundPlayer playstop = new SoundPlayer(@"C:\AudioFilesCella\Alert.wav"); //TEST
  265. SoundPlayer playstop = new SoundPlayer(@"C:\Users\pc\Desktop\Sito\Audio per CELLA\Alert.wav"); //PUBBLICO
  266. playstop.Stop();
  267. }
  268. int count = 0;
  269.  
  270. if (System.IO.File.Exists(file)) //se il file esiste
  271. {
  272. string[] fileContent = File.ReadAllLines(file);
  273. if (fileContent.Length > 0)
  274. if (fileContent[0].ToString().Trim() != string.Empty)
  275. count = fileContent.Length;
  276. }
  277.  
  278. if (count > 0 && lampeggioGiallo)
  279. {
  280. ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Multipersona", "Multipersona()", true);
  281. }
  282. }
  283.  
  284. #endregion
  285. #region TIMER TICK
  286. protected void Timer1_Tick(object sender, EventArgs e)
  287. {
  288. CheckAlert();
  289. Label2.Text = "";
  290. Label1.Text = "";
  291. }
  292. #endregion
  293. #region gestione pass reset
  294. protected void Button1_Click(object sender, EventArgs e)
  295. {
  296. //string mydocpath = @"C:\CellaFiles";
  297. //string file = @mydocpath + @"\LogInfo.txt";
  298.  
  299.  
  300. string file = @"C:\Users\pc\Desktop\Sito\File CELLA\LogInfo.txt";
  301. // string ControlloCella = @"C:\CellaFiles\ControlSendMessage.txt";
  302. string ControlloCella = @"C:\Users\pc\Desktop\Sito\File CELLA\ControlSendMessage.txt";
  303. string password = "";
  304. password = TextBox2.Text;
  305. if (password == ("bomi2017"))
  306.  
  307. {
  308.  
  309.  
  310. SoundPlayer playstop = new SoundPlayer(@"C:\Users\pc\Desktop\Sito\Audio per CELLA\Alert.wav");
  311. //SoundPlayer playstop = new SoundPlayer(@"C:\AudioFilesCella\Alert.wav"); //TEST
  312. playstop.Stop();
  313. //if (System.IO.File.Exists(file))
  314. // System.IO.File.Delete(file);
  315. File.WriteAllText(file, String.Empty);
  316. File.WriteAllText(ControlloCella, String.Empty);
  317. Label3.Text = "0";
  318. Label2.Text = "";
  319. Label1.Text = "";
  320. Response.Cookies.Clear();
  321. }
  322. else
  323. {
  324. this.Page.ClientScript.RegisterStartupScript(this.GetType(), "MSGBOX", "alert('Password errata!!!');", true);
  325. }
  326. }
  327. #endregion
  328. #region pageload
  329. protected void Page_Load(object sender, EventArgs e)
  330. {
  331. TextBox1.Text = "";
  332. }
  333. }
  334. }
  335. #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement