As mentioned in a previous post, the new version of Software Blockly can get the app metadata in 2 ways. One is to interrogate the Pico device. The other is store it in a database.

Interrogate the Pico device generates data dictionaries of • Device Types • Devices by device type • “Generic” commands by device type. The generic commands are commands that all device of a certain type will support if only to return an Ok- Not supported message.

This meta-info is included as a static class which makes it the same for all instances.

    public static class Info
    {
        public static Dictionary<int, string> TargetDeviceTypes { get; set; }
        public static Dictionary<int, Dictionary<string, int>> GenericCmds { get; set; }
        public static Dictionary<int, Dictionary<int, string>> TargetDevices { get; set; }
    }

The first key for the GenericCmds and TargetDevices is the device type index as per the keys in TargetDeviceTypes.

Nb: Displays have a Misc command which leads to a list of commands specific to that device. Other device types don’t have Misc commands (miscellaneous) , only generic.

As opposed to interrogating the device, a SQLite database file has been included which can be queried for the same content which has been autogenerated after one device interrogation.

    public bool ReadSoftataDataDb(SoftataDbContext context)
    {
        try
        {
            var ctx = context;
            SoftataWebAPI.Data.Db.SqliteDb.ReadDb(ctx);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

Nb: Context: that is previously instantiated instance of the SofataLib library.

In previous versions this was static ReadSoftataDataDb is called via a Service from an ASP.Net Controller method. This version of Blockly uses a Session so that multiple uses can be using it. Previously, the apps, local and remote were single user as much use was made of static data in the top level apps . A user connected and the Pico was put in a connected state and the top level apps assume that. To change activity the top level exited forcing a Pico reset. Now each user has a session. They connect and disconnect before and after each request to the Pico. To change top level functionality, the Pico does not need to be reset. The following is passed from controller methods automatically reconnecting as required.:

    public Socket? _client = null;
    protected Socket? Client
    {
        get
        {
            if (_client != null)
            {
                return _client;
            }
            else
            {
                Tuple<string, int>? connectionDetails = HttpContext.Session.Get<Tuple<string, int>>("ConnectionDetails");
                if (connectionDetails == null)
                    return null;
                _client = _Connect(connectionDetails.Item1, connectionDetails.Item2);
                return _client;
            }
        }
    } 

Note that the Tuple(connection details) is established at first connection to the Pico (not shown here).

ReadSoftataDataDb() populates Info as follows:

public static void ReadDb(SoftataDbContext context)
{
    Info.TargetDeviceTypes = new Dictionary<int, string>();
    Info.TargetDevices = new Dictionary<int, Dictionary<int, string>>();
    Info.GenericCmds = new Dictionary<int, Dictionary<string, int>>();
    using (context)
    {
        var deviceTypes = context.dTypes.ToList();
        var devices = context.Devices.ToList();
        var commands = context.GenericCommands.ToList();

        foreach (var devType in deviceTypes)
        {
            Info.TargetDeviceTypes.Add(devType.SoftataId, devType.Name);
            Info.TargetDevices.Add(devType.SoftataId, new Dictionary<int, string>());
            Info.GenericCmds.Add(devType.SoftataId, new Dictionary<string, int>());
            foreach (var device in devices)
            {
                if (device.DtypeId == devType.Id)
                {
                    Info.TargetDevices[devType.SoftataId].Add(device.SoftataId, device.Name);
                }
            }
            if (commands != null)
            {
                Console.WriteLine("Commands");
                foreach (var cmd in commands)
                {
                    if (cmd.DtypeId == devType.Id)
                    {
                        Info.GenericCmds[devType.SoftataId].Add(cmd.Name, cmd.SoftataId);
                    }
                }
            }
        }
    }
}

There is a mirrored method CreateDb() that takes the interrogated data in Info and writes it to the database. SQLite manifests as a standalone file. Also there is one to claer the database ClearDb(). This is included with the Blockly app.


 TopicSubtopic
<  Prev:   Blockly
   
 This Category Links 
Category:Database Index:Database
<  Prev:   HSQLDB.Net