Can't read AlarmServer database table in panel, works in Simulator

A forum devoted to the discussion of all topics having to do with scripting and other advanced programming using iX Developer.
Post Reply
Ivan Videsvall
Posts: 6
Joined: Thu Jul 09, 2020 9:36 am

Can't read AlarmServer database table in panel, works in Simulator

Post by Ivan Videsvall »

edit: This problem progressed to the inability to read the AlarmServer table. See the post below this one for up to date details.

I wish to get an up-to-date list of active alarms (and datetimes) after the AlarmChange event has been triggered. If the database is read in the AlarmChange event handler it contains the alarm date from before the latest alarm event. This I guess is because to the database update takes more time than the code in the event handler to execute.

I tried to delay the database read by use of a Timer, but I can't get the Timer event handler to run in the AlarmServer script.

I'll get back with a followup with some code.

Is there a trick or a working example that delays the database read after an alarm event?
Last edited by Ivan Videsvall on Thu Dec 03, 2020 5:51 am, edited 1 time in total.

Ivan Videsvall
Posts: 6
Joined: Thu Jul 09, 2020 9:36 am

Re: Alarm database read on AlarmChange event is not up-to-date

Post by Ivan Videsvall »

Instead of triggering on AlarmChange I made a workaround that reads the database periodically after an initial delay on panel startup to allow for the system to stabilize. This is done by using a timer that sets a ready tag. This tag is then checked by the system tag SystemTagSecond event handler.

ScriptModule Startup:

Code: Select all

using System.Data.SQLite;

public partial class Startup
    {
        public static Timer DbDelay;

        void Startup_Created(System.Object sender, System.EventArgs e)
        {
           // setup database read delay timer
            DbDelay = new Timer();
            DbDelay.Interval = 45000;
            DbDelay.Tick += SetDbReady;
            DbDelay.Enabled = true;
        }

        void SetDbReady(System.Object sender, System.EventArgs e)
        {
            DbDelay.Enabled = false;
            Globals.Tags.DbReady.Value = true;
            Globals.Tags.alarms.Value = "DB timer elapsed";
        }
    }
Tags:

Code: Select all

void SystemTagSecond_ValueChange(System.Object sender, Core.Api.DataSource.ValueChangedEventArgs e)
    {           
        if (!Globals.Tags.DbReady.Value) return; // DBready delay timer has not yet elapsed

        Globals.Tags.alarms.Value += ", " + Globals.Tags.SystemTagSecond.Value;
        
        if (Globals.Tags.SystemTagSecond.Value % 10 == 0)
        {               
            string con = "DataSource = Database.db; ReadOnly = True";
            using (SQLiteConnection scon = new SQLiteConnection(con))
            {
                scon.Open();
                Globals.Tags.alarms.Value += ", db open";

                // check that the AlarmServer table is present
                string query = "SELECT name FROM sqlite_master WHERE type='table' AND name='AlarmServer'";
                SQLiteCommand cmd = new SQLiteCommand(query, scon);
                Globals.Tags.alarms.Value += ", master table queried";

                using (SQLiteDataReader rdr = cmd.ExecuteReader())
                {
                    if (!rdr.HasRows) return;
                }

                Globals.Tags.alarms.Value += ", alarm table exists";
                
                // get active alarms
                query = "SELECT Text, ActiveTime FROM AlarmServer WHERE State='Active' ORDER BY ActiveTime DESC";
                cmd = new SQLiteCommand(query, scon);
                Globals.Tags.alarms.Value += ", alarms table queried";

// process alarms

                scon.Close();
                Globals.Tags.Alarms_5pos.Value += ", db closed";
            }
        }
    }
}
Now this works in the Simulator but only partially in the panel. In the panel it seems to not find the AlarmServer table.

iX Developer 2.40 SP3
X2 Pro 7 panel
v8 build 773

Post Reply