Page 1 of 1

Unable to select last Active Alarm

Posted: Wed Feb 12, 2020 6:15 am
by Subho
I am trying to select the last active alarm using following script.

void Screen2_Opened(System.Object sender, System.EventArgs e)
{
SQLiteConnection db = new SQLiteConnection("DataSource=Database.db");
string query ="Select [Text] from AlarmServer WHERE State = 'Active' ORDER BY ActiveTime DESC LIMIT 1";
SQLiteCommand comm = new SQLiteCommand(query);
comm.Connection = db;
db.Open();
SQLiteDataReader read = (null);
read = comm.ExecuteReader();
while (read.Read())
{
Globals.Tags.AlarmString.Value = (read["Text"].ToString());
}
read.Close();
db.Close();
}

But it always select Last-1 alarm and show its text in AlarmString tag. Where am I doing wrong?

I have also attached the project for reference.

Re: Unable to select last Active Alarm

Posted: Fri Feb 14, 2020 5:56 pm
by Russ C.
It looks like the database write is too slow for the read.

You could do it this way in a script module:

Code: Select all

void ScriptModule1_Created(System.Object sender, System.EventArgs e)
		{
			Globals.AlarmServer.AlarmActive += AlarmStatus;
		}

		void AlarmStatus(System.Object sender, System.EventArgs e)
		{
			try
			{
				IAlarmEvent alarm = (IAlarmEvent)sender;
            
				Globals.Tags.AlarmString.Value = "";
				Globals.Tags.AlarmString.SetString(alarm.DisplayText);

			}
			catch (Exception) { }
		}
What this does is capture the triggering alarm event and pull its data, so only the most recent active alarm will be distplayed.

And remove the script from your Screen2

Re: Unable to select last Active Alarm

Posted: Sat Feb 15, 2020 9:51 pm
by Subho
Thank you Russ for the script. It solved the problem. :)

I am also wondering if Alarm ID can be selected using the same script and acknowledge that particular Alarm when the "Close" button is pressed.

Re: Unable to select last Active Alarm

Posted: Mon Feb 24, 2020 7:30 am
by nxen
Hi Russ,

is there a way to clear the string when no alarm is active?

Thanks in advance.