Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Media;
- using System.Net.Mail;
- using System.Text;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Net;
- using System.Data.OleDb;
- using System.Data;
- using OfficeOpenXml;
- using OfficeOpenXml.Drawing;
- using System.Linq;
- namespace WebApplication1
- {
- public partial class WebForm1 : System.Web.UI.Page
- {
- class oranow
- {
- public DateTime OraPassaggio { get; set; }
- }
- public static string SearchNameInExcel(string path, string testosparato)
- {
- string Nome = testosparato;
- using (var pck = new OfficeOpenXml.ExcelPackage())
- {
- using (var stream = File.OpenRead(path))
- {
- pck.Load(stream);
- }
- var ws = pck.Workbook.Worksheets.First();
- var startRow = 2;
- bool esci = false;
- for (var rowNum = startRow; rowNum <= ws.Dimension.End.Row; rowNum++)
- {
- if (esci)
- break;
- var wsRow = ws.Cells[rowNum, 1, rowNum, ws.Dimension.End.Column];
- foreach (var cell in wsRow)
- {
- if (esci)
- {
- Nome = cell.Text;
- break;
- }
- if (cell.Text == testosparato)
- {
- esci = true;
- }
- }
- }
- }
- return Nome;
- }
- protected void TextBox1_TextChanged(object sender, EventArgs e)
- {
- #region Dichiarazione Variabili
- Label1.Text = "";
- Label2.Text = "";
- string testosparato = "";
- bool presente = false;
- testosparato = ((TextBox)sender).Text;
- //string file = @"C:\CellaFiles\LogInfo.txt";
- string mydocpath = @"C:\Users\pc\Desktop\Sito\File CELLA"; // PUBBLICATO
- string file = @mydocpath + @"\LogInfo.txt";
- // string infoFile = @"C:\Users\pc\Desktop\Sito\File CELLA\LogRegister.txt";
- int count = 0;
- #endregion
- string nome = SearchNameInExcel(@"C:\Users\pc\Desktop\Sito\File CELLA\Nominativi Cella.xlsx", testosparato); //PUBBLICO
- //string nome = SearchNameInExcel(@"C:\Users\alfieri.michele\Desktop\Progetto\Progetto cella - locale TEST\Nominativi Cella.xlsx", testosparato);
- if (!string.IsNullOrEmpty(nome)) //se il testo sparato è vuoto non fa nulla
- {
- StringBuilder FileText = new StringBuilder();
- string linetemp = null;
- if (System.IO.File.Exists(file)) //se il file esiste
- {
- #region leggo il file e controllo se utente è già dentro
- using (StreamReader sr = new StreamReader(file))
- {
- while ((linetemp = sr.ReadLine()) != null)
- {
- count += 1;
- string[] utente = linetemp.Split('_');
- if (utente[0] == nome)
- {
- //presente
- presente = true;
- }
- else
- {
- //salva riga in file
- FileText.AppendLine(linetemp);
- }
- }
- }
- #endregion
- }
- #region salvo ingresso e stampo status
- if (presente)
- {
- //deve uscire
- Label1.Text = "USCITA: " + nome; //+ " " + DateTime.Now.ToString("HH:mm");
- count -= 1;
- string totale = nome + "_" + DateTime.Now.ToString("dd/MM/yyyy HH:mm") + "_" + "USCITA" + System.Environment.NewLine;
- File.AppendAllText(@"C:\Users\pc\Desktop\Sito\File CELLA\LogRegister.txt", totale);
- }
- else
- {
- //è entrato
- FileText.AppendLine(nome + "_" + DateTime.Now.ToString("dd/MM/yyyy HH:mm"));
- Label2.Text = "ENTRATA: " + nome; //+ " " + DateTime.Now.ToString("HH:mm");
- count += 1;
- string totale = nome + "_" + DateTime.Now.ToString("dd/MM/yyyy HH:mm") + "_" + "ENTRATA" + System.Environment.NewLine;
- File.AppendAllText(@"C:\Users\pc\Desktop\Sito\File CELLA\LogRegister.txt", totale); //PUBBLICO
- // File.AppendAllText(@"C:\CellaFiles\LogRegister.txt", totale); //TEST
- }
- #endregion
- #region creo file
- using (StreamWriter sw = new StreamWriter(file))
- {
- sw.Write(FileText.ToString());
- }
- #endregion
- }
- ((TextBox)sender).Text = "";
- Label3.Text = count.ToString();
- CheckAlert();
- if (count == 0)
- {
- string ControlloCella = @"C:\Users\pc\Desktop\Sito\File CELLA\ControlSendMessage.txt";
- File.WriteAllText(ControlloCella, String.Empty);
- };
- }
- #region gestione alert e controllo
- protected void CheckAlert()
- {
- //string ControlloTelegram = @"C:\CellaFiles\ControlSendMessage.txt";
- string ControlloTelegram = @"C:\Users\pc\Desktop\Sito\File CELLA\ControlSendMessage.txt";
- StringBuilder FileText = new StringBuilder();
- bool lampeggioGiallo = true;
- bool alert = false;
- // string mydocpath = @"C:\CellaFiles";
- // string file = @mydocpath + @"\LogInfo.txt";
- string file = @"C:\Users\pc\Desktop\Sito\File CELLA\LogInfo.txt";
- int Check = 0;
- string[] ControlloTelegramFiles = File.ReadAllLines(ControlloTelegram);
- if (ControlloTelegramFiles.Length > 0)
- if (ControlloTelegramFiles[0].ToString().Trim() != string.Empty)
- Check = ControlloTelegramFiles.Length;
- string linetemp = null;
- string NomeUtente = "";
- if (System.IO.File.Exists(file))
- {
- using (StreamReader sr = new StreamReader(file))
- {
- while ((linetemp = sr.ReadLine()) != null)
- {
- string[] ora = linetemp.Split('_');
- DateTime oratime = Convert.ToDateTime(ora[1]);
- if (DateTime.Now >= oratime.AddMinutes(1))
- {
- {
- alert = true;
- lampeggioGiallo = false; //NUOVA AGGIUNTA, 1 PERSONA IN CELLA LAMPEGGIA ROSSO
- NomeUtente = ora[0];
- }
- }
- }
- }
- }
- #endregion
- #region se scatta l'alert
- if (alert)
- {
- //SoundPlayer sound = new SoundPlayer(@"C:\AudioFilesCella\Alert.wav"); // TEST
- SoundPlayer sound = new SoundPlayer(@"C:\Users\pc\Desktop\Sito\Audio per CELLA\Alert.wav"); //PUBBLICO
- sound.PlayLooping();
- if (Check == 0)
- {
- try
- {
- SmtpClient client = new SmtpClient();
- client.Port = 587;
- client.Host = "smtp.gmail.com";
- client.EnableSsl = true;
- client.Timeout = 10000;
- client.DeliveryMethod = SmtpDeliveryMethod.Network;
- client.UseDefaultCredentials = false;
- client.Credentials = new System.Net.NetworkCredential("alertpersonaincella@bomigroup.com", "bomi2017");
- 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");
- mm.Subject = "Alert cella -20° + Nome utente: " + NomeUtente;
- mm.Body = "La Persona:" + NomeUtente + " Ha superato il tempo limite dentro la cella, si è pregati di controllare";
- mm.BodyEncoding = UTF8Encoding.UTF8;
- mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
- client.Send(mm);
- /* 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;
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
- HttpWebResponse response = (HttpWebResponse)request.GetResponse();
- if (response.StatusCode == HttpStatusCode.OK)
- {
- }
- response.Close();*/
- }
- catch
- {
- SmtpClient client = new SmtpClient();
- client.Port = 587;
- client.Host = "smtp.gmail.com";
- client.EnableSsl = true;
- client.Timeout = 10000;
- client.DeliveryMethod = SmtpDeliveryMethod.Network;
- client.UseDefaultCredentials = false;
- client.Credentials = new System.Net.NetworkCredential("alertpersonaincella@bomigroup.com", "bomi2017");
- MailMessage mm = new MailMessage("alertpersonaincella@bomigroup.com", "michele.alfieri@bomigroup.com");
- mm.Subject = "Errore invio alert, utente:" + NomeUtente;
- mm.Body = NomeUtente + "_" + "Errore invio email e telegram";
- mm.BodyEncoding = UTF8Encoding.UTF8;
- mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
- client.Send(mm);
- }
- }
- using (StreamWriter WriteInCheckCella = new StreamWriter(ControlloTelegram))
- {
- WriteInCheckCella.Write("Il mio messaggio fisso");
- WriteInCheckCella.Flush();
- }
- ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "AvviaTutto", "avvia()", true);
- //SoundPlayer playstop = new SoundPlayer(@"C:\AudioFilesCella\Alert.wav"); //TEST
- SoundPlayer playstop = new SoundPlayer(@"C:\Users\pc\Desktop\Sito\Audio per CELLA\Alert.wav"); //PUBBLICO
- playstop.Stop();
- }
- int count = 0;
- if (System.IO.File.Exists(file)) //se il file esiste
- {
- string[] fileContent = File.ReadAllLines(file);
- if (fileContent.Length > 0)
- if (fileContent[0].ToString().Trim() != string.Empty)
- count = fileContent.Length;
- }
- if (count > 0 && lampeggioGiallo)
- {
- ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "Multipersona", "Multipersona()", true);
- }
- }
- #endregion
- #region TIMER TICK
- protected void Timer1_Tick(object sender, EventArgs e)
- {
- CheckAlert();
- Label2.Text = "";
- Label1.Text = "";
- }
- #endregion
- #region gestione pass reset
- protected void Button1_Click(object sender, EventArgs e)
- {
- //string mydocpath = @"C:\CellaFiles";
- //string file = @mydocpath + @"\LogInfo.txt";
- string file = @"C:\Users\pc\Desktop\Sito\File CELLA\LogInfo.txt";
- // string ControlloCella = @"C:\CellaFiles\ControlSendMessage.txt";
- string ControlloCella = @"C:\Users\pc\Desktop\Sito\File CELLA\ControlSendMessage.txt";
- string password = "";
- password = TextBox2.Text;
- if (password == ("bomi2017"))
- {
- SoundPlayer playstop = new SoundPlayer(@"C:\Users\pc\Desktop\Sito\Audio per CELLA\Alert.wav");
- //SoundPlayer playstop = new SoundPlayer(@"C:\AudioFilesCella\Alert.wav"); //TEST
- playstop.Stop();
- //if (System.IO.File.Exists(file))
- // System.IO.File.Delete(file);
- File.WriteAllText(file, String.Empty);
- File.WriteAllText(ControlloCella, String.Empty);
- Label3.Text = "0";
- Label2.Text = "";
- Label1.Text = "";
- Response.Cookies.Clear();
- }
- else
- {
- this.Page.ClientScript.RegisterStartupScript(this.GetType(), "MSGBOX", "alert('Password errata!!!');", true);
- }
- }
- #endregion
- #region pageload
- protected void Page_Load(object sender, EventArgs e)
- {
- TextBox1.Text = "";
- }
- }
- }
- #endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement