WysylanieSMS
Skrypt wysylajacy SMS w trakcie alarmuunknown
csharp
a month ago
5.9 kB
3
Indexable
Never
using System; using System.Windows.Forms; using Askom.AsixEvo.Scripting; using System.IO.Ports; using System.Threading; using System.Data; /// <summary> /// The script allows to do something everytime the state of any alarm of given domain changes. /// </summary> public class WysylanieSMS : IScript { static IApplication mApplication; static IAlarmEventInfo mEvInfo; static int sleepTime = 100; static int sleepBetweenMsg = 7000; static SerialPort GSM_Port; static string bufRecvSerialPort = ""; static bool continueReading = false; static Mutex m; static System.Threading.Thread readThread; static int GSM_reg = 0; /***************************************************************************/ public bool Initialize(string[] aParameters, IApplication aApplication) { mApplication = aApplication; mApplication.ScriptSettings.RestartIfFailed = true; InitSMSVirtVar(); //Call AlarmStateChanged method everytime the any alarm from the domain will change mEvInfo = mApplication.RegisterAlarmDomainEvents("AlarmySMS", AlertSMS, AlarmDomainEvents.AlarmBegin); return false; } /***************************************************************************/ void InitSMSVirtVar() { DataTable dt; DataRow[] dr; dt = mApplication.Asbase.ExecuteQuery("select Name, V_Aktywny, V_Uzytkownik, V_Numer from [_asbase_ZZO_Poznan].[dbo].[RECIPESET_SMS_User] order by Name asc"); dr = dt.Select(); for(int i = 0; i < dr.Length; ++i) { mApplication.SetVariableValue("SMS_Recep0" + (i + 1).ToString(), "U0"+(i + 1).ToString()); mApplication.SetVariableValue("SMS_Aktywny0" + (i + 1).ToString(), dr[i]["V_Aktywny"]); mApplication.SetVariableValue("SMS_Uzytkownik0" + (i + 1).ToString(), dr[i]["V_Uzytkownik"]); mApplication.SetVariableValue("SMS_Numer0" + (i + 1).ToString(), dr[i]["V_Numer"]); } } /***************************************************************************/ void AlertSMS(string aDomainId, string aAlarmId, AlarmState aState, AlarmEventType aEventType) { IAlarm actAlarm; IVariable active,number; AlarmProperties actAlarmProperties; actAlarm = mApplication.GetAlarm(aAlarmId); actAlarmProperties = actAlarm.Definition; string msg; msg = CreateMsg(actAlarmProperties.BeginText); GSM_reg=GSM_Init(); //poprawne zarejestrowanie if (GSM_reg == 0) { for (int i = 1; i <= 10; ++i) { active = mApplication.GetVariable("SMS_Aktywny0" + i.ToString(), false); number = mApplication.GetVariable("SMS_Numer0" + i.ToString(), false); if (Convert.ToBoolean(active.VariableState.Value)) SendSMS(Convert.ToString(number.VariableState.Value), msg); System.Threading.Thread.Sleep(sleepBetweenMsg); } } continueReading = false; readThread.Join(); GSM_Port.Close(); System.Threading.Thread.Sleep(500); } /***************************************************************************/ public static int GSM_Init() { readThread = mApplication.CreateThread(Read); m = new Mutex(); bool zarejestrowano = false; int licznik = 0; //string msg; GSM_Port = new SerialPort("COM1"); GSM_Port.BaudRate = 115200; GSM_Port.Parity = Parity.None; GSM_Port.DataBits = 8; GSM_Port.StopBits = StopBits.One; GSM_Port.Handshake = Handshake.None; GSM_Port.ReadTimeout = 10; GSM_Port.WriteTimeout = 1000; GSM_Port.DtrEnable = false; GSM_Port.RtsEnable = false; GSM_Port.Open(); continueReading = true; readThread.Start(); GSM_Port.Write("at+cpin?\r"); System.Threading.Thread.Sleep(sleepTime); m.WaitOne(); if(bufRecvSerialPort.IndexOf("+CPIN: READY") == -1) { m.ReleaseMutex(); GSM_Port.Write("at+cpin=2363\r"); System.Threading.Thread.Sleep(sleepTime); } else m.ReleaseMutex(); do { m.WaitOne(); bufRecvSerialPort = ""; m.ReleaseMutex(); GSM_Port.Write("at+creg?\r"); System.Threading.Thread.Sleep(sleepTime); m.WaitOne(); if(bufRecvSerialPort.IndexOf("+CREG: 0,1") != -1) { //poprawnosc rejestracji w sieci bufRecvSerialPort = ""; zarejestrowano = true; } else ++licznik; m.ReleaseMutex(); }while(!zarejestrowano && licznik < 0); if(licznik == 20) return -1; else return 0; } /***************************************************************************/ public static string CreateMsg(string actAlarmComm) { string alarmDateTime = System.DateTime.Now.ToString(); string msg = actAlarmComm + " (" + alarmDateTime + ") "; msg = msg.Replace("ą", "a"); msg = msg.Replace("ć", "c"); msg = msg.Replace("ę", "e"); msg = msg.Replace("ł", "l"); msg = msg.Replace("ń", "n"); msg = msg.Replace("ó", "o"); msg = msg.Replace("ś", "s"); msg = msg.Replace("ź", "z"); msg = msg.Replace("ż", "z"); msg = msg.Replace("Ą", "A"); msg = msg.Replace("Ć", "C"); msg = msg.Replace("Ę", "E"); msg = msg.Replace("Ł", "L"); msg = msg.Replace("Ń", "N"); msg = msg.Replace("Ó", "O"); msg = msg.Replace("Ś", "S"); msg = msg.Replace("Ź", "Z"); msg = msg.Replace("Ż", "Z"); return msg; } /***************************************************************************/ public static void SendSMS(string number, string msg) { GSM_Port.Write("at+cmgf=1\r"); System.Threading.Thread.Sleep(sleepTime); GSM_Port.Write("at+cmgs=\"" + number + "\"\r"); System.Threading.Thread.Sleep(sleepTime); GSM_Port.Write(msg + (char) 26); } /***************************************************************************/ public static void Read() { while(continueReading) { try { m.WaitOne(); string recvMsg = GSM_Port.ReadLine(); bufRecvSerialPort += recvMsg; m.ReleaseMutex(); } catch(TimeoutException) {m.ReleaseMutex();} } } /***************************************************************************/ public void FinalizeScript() { continueReading = false; readThread.Join(); GSM_Port.Close(); if(mEvInfo != null) { mApplication.UnregisterEvent(mEvInfo); } } }
Leave a Comment