/// <summary>
        /// Check if a digital address is used.
        /// </summary>
        /// <param name="address">Address to check.</param>
        /// <param name="type">Type of address.</param>
        /// <returns><c>true</c> if the address is used or <c>false</c> if the address is free.</returns>
        public bool IsAddressUsed(int address, ControlModule.ModuleType type)
        {
            string sql = string.Empty;

            Logger.LogDebug(this, "[CLASS].IsAddressUsed({0}, {1})", address, type);

            try
            {
                Connect();

                sql = @"SELECT DISTINCT 
                        Count(*) 
                    FROM 
                        " + ControlModuleManager.SQL_TABLE + @" 
                    WHERE 
                        address = @address And 
                        type = @type";

                SetParameter("address", address);
                SetParameter("type", (int)type);

                return(ExecuteScalar(sql) > 0);
            }
            catch (Exception ex)
            {
                Logger.LogError(this, ex);

                throw;
            }
            finally
            {
                Disconnect();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Get all blocks.
        /// </summary>
        /// <param name="controlType">A value to filter blocks by its control connections.</param>
        /// <returns>A list of blocks controlable with accessory decoders.</returns>
        public IEnumerable <ElementBase> GetAll(ControlModule.ModuleType controlType)
        {
            string             sql   = string.Empty;
            ElementBase        item  = null;
            List <ElementBase> items = new List <ElementBase>();

            Logger.LogDebug(this, "[CLASS].GetAll({0})", controlType);

            try
            {
                Connect();

                sql = @"SELECT 
                        " + ElementManager.SQL_FIELDS_SELECT + @" 
                    FROM 
                        " + ElementManager.SQL_TABLE + @" 
                    ORDER BY 
                        name Asc, 
                        id   Asc";

                using (SQLiteDataReader reader = ExecuteReader(sql))
                {
                    while (reader.Read())
                    {
                        item = ElementManager.ReadEntityRecord(reader);
                        if (item != null &&
                            (controlType == ControlModule.ModuleType.Undefined ||
                             controlType == ControlModule.ModuleType.Accessory && item.AccessoryConnections != null && item.AccessoryConnections.Length > 0 ||
                             controlType == ControlModule.ModuleType.Sensor && item.FeedbackConnections != null && item.FeedbackConnections.Length > 0))
                        {
                            items.Add(item);
                        }
                    }
                }

                return(items);
            }
            catch (Exception ex)
            {
                Logger.LogError(this, ex);

                throw;
            }
            finally
            {
                Disconnect();
            }
        }
        /// <summary>
        /// Get all decoders information in an instance of <see cref="DataTable"/>.
        /// </summary>
        /// <returns>An instance of <see cref="DataTable"/> filled with the requested data.</returns>
        public List <ControlModule> GetByType(ControlModule.ModuleType type)
        {
            string               sql   = string.Empty;
            ControlModule        item  = null;
            List <ControlModule> items = new List <ControlModule>();

            Logger.LogDebug(this, "[CLASS].GetByType({0})", type);

            try
            {
                Connect();

                sql = @"SELECT 
                        " + ControlModuleManager.SQL_FIELDS_SELECT + @" 
                    FROM 
                        " + ControlModuleManager.SQL_TABLE + @" 
                    WHERE 
                        type = @type 
                    ORDER BY 
                        name Asc";

                SetParameter("type", (int)type);

                using (SQLiteDataReader reader = ExecuteReader(sql))
                {
                    while (reader.Read())
                    {
                        item = ControlModuleManager.ReadEntityRecord(reader);
                        if (item != null)
                        {
                            items.Add(item);
                        }
                    }
                }

                return(items);
            }
            catch (Exception ex)
            {
                Logger.LogError(this, ex);

                throw;
            }
            finally
            {
                Disconnect();
            }
        }
        /// <summary>
        /// Get all decoders information in an instance of <see cref="DataTable"/>.
        /// </summary>
        /// <returns>An instance of <see cref="DataTable"/> filled with the requested data.</returns>
        public System.Data.DataTable FindByType(ControlModule.ModuleType type)
        {
            string sql = string.Empty;

            Logger.LogDebug(this, "[CLASS].GetByTypeAsDataTable({0})", type);

            try
            {
                Connect();

                sql = @"SELECT 
                        cm.id             As ""#ID"", 
                        cm.name           As ""Name"", 
                        cm.manufacturer   As ""Manufacturer"", 
                        cm.model          As ""Model"", 
                        cm.outputs        As """ + (type == ControlModule.ModuleType.Accessory ? "Outputs" : "Inputs") + @""", 
                        (SELECT Count(*)
                         FROM   " + ControlModuleConnectionManager.SQL_TABLE + @" cmc 
                         WHERE  cmc.decoderid = cm.id) As ""Used""
                    FROM 
                        " + ControlModuleManager.SQL_TABLE + @" cm 
                    WHERE 
                        cm.type = @type 
                    ORDER BY 
                        cm.name Asc";

                SetParameter("type", (int)type);

                return(ExecuteDataTable(sql));
            }
            catch (Exception ex)
            {
                Logger.LogError(this, ex);

                throw;
            }
            finally
            {
                Disconnect();
            }
        }
        /// <summary>
        /// Get next free address.
        /// </summary>
        /// <param name="type">Type of element.</param>
        /// <returns>An integer value indicating the </returns>
        public int GetNextFree(ControlModule.ModuleType type)
        {
            long idx     = 0;
            long numAdds = 0;

            long[] adds = null;
            string sql  = string.Empty;

            Logger.LogDebug(this, "[CLASS].GetNextFree({0})", type);

            try
            {
                Connect();

                sql = @"SELECT DISTINCT 
                        Max(address) 
                    FROM 
                        " + ControlModuleConnectionManager.SQL_TABLE + @" 
                    WHERE 
                        address > 0";

                numAdds = ExecuteScalar(sql);

                if (numAdds <= 0)
                {
                    return(1);
                }

                // Generate an empty vector to store addresses
                adds = new long[numAdds];
                for (int i = 0; i < numAdds; i++)
                {
                    adds[i] = 0;
                }

                sql = @"SELECT DISTINCT 
                        address 
                    FROM 
                        " + ControlModuleConnectionManager.SQL_TABLE + @" 
                    WHERE 
                        address > 0";

                using (SQLiteDataReader reader = ExecuteReader(sql))
                {
                    while (reader.Read())
                    {
                        idx           = reader.GetInt64(0);
                        adds[idx - 1] = idx;
                    }
                }

                for (int i = 0; i < numAdds; i++)
                {
                    if (adds[i] == 0)
                    {
                        return(i + 1);
                    }
                }

                return(1);
            }
            catch (Exception ex)
            {
                Logger.LogError(this, ex);

                throw;
            }
            finally
            {
                Disconnect();
            }
        }
        /// <summary>
        /// Gets connections of the specified type for the specified element.
        /// </summary>
        /// <param name="element">Element.</param>
        /// <param name="type">Type of control module.</param>
        /// <returns>The requested list of <see cref="ControlModuleConnection"/> related to the specified element.</returns>
        public System.Data.DataTable FindByElement(ElementBase element, ControlModule.ModuleType type)
        {
            int     items = 0;
            string  sql   = string.Empty;
            DataRow row   = null;

            Logger.LogDebug(this,
                            "[CLASS].FindByElement([{0}], {1})",
                            (element != null ? element.ID.ToString() : "null"), type);

            // Return an empty array if element is null or doesn't have outputs
            if (element == null)
            {
                return(null);
            }
            else if (type == ControlModule.ModuleType.Accessory && (element.AccessoryConnections == null || element.AccessoryConnections.Length <= 0))
            {
                return(null);
            }
            else if (type == ControlModule.ModuleType.Sensor && (element.FeedbackConnections == null || element.FeedbackConnections.Length <= 0))
            {
                return(null);
            }

            try
            {
                switch (type)
                {
                case ControlModule.ModuleType.Accessory:
                    items = element.AccessoryConnections.Length;
                    break;

                case ControlModule.ModuleType.Sensor:
                    items = element.FeedbackConnections.Length;
                    break;
                }

                // Generate the data table
                System.Data.DataTable dt = new System.Data.DataTable("Connections");
                dt.Columns.Add("ID", typeof(int));
                dt.Columns.Add("Index", typeof(int));
                dt.Columns.Add("Module", typeof(string));
                dt.Columns.Add("Output", typeof(string));
                dt.Columns.Add("Address", typeof(int));

                // Fill table with empty rows
                for (int i = 0; i < items; i++)
                {
                    dt.Rows.Add(0, 0, "Not connected", string.Empty, 0);
                }

                Connect();

                sql = @"SELECT 
                        cmc.id, 
                        cmc.[index], 
                        cm.name, 
                        cmc.name, 
                        cmc.address
                    FROM 
                        " + ControlModuleConnectionManager.SQL_TABLE + @" cmc 
                        Inner Join " + ControlModuleManager.SQL_TABLE + @" cm On (cm.""id"" = cmc.""decoderid"") 
                    WHERE 
                        blockid = @blockid And 
                        @type   = (SELECT type 
                                   FROM   " + ControlModuleManager.SQL_TABLE + @" deco 
                                   WHERE  cmc.decoderid = deco.id) 
                    ORDER BY 
                        cmc.[index] Asc,
                        cmc.id      Asc";

                SetParameter("blockid", element.ID);
                SetParameter("type", (int)type);

                using (SQLiteDataReader reader = ExecuteReader(sql))
                {
                    while (reader.Read())
                    {
                        // Update the row data
                        row           = dt.Rows[reader.GetInt32(1) - 1];
                        row.ItemArray = new object[]
                        {
                            reader.GetInt32(0),
                            reader.GetInt32(1),
                            reader.GetString(2),
                            reader.GetString(3),
                            reader.GetInt32(4)
                        };
                    }
                }

                return(dt);
            }
            catch (Exception ex)
            {
                Logger.LogError(this, ex);

                throw;
            }
            finally
            {
                Disconnect();
            }
        }
        /// <summary>
        /// Gets connections of the specified type for the specified element.
        /// </summary>
        /// <param name="element">Element.</param>
        /// <param name="type">Type of element.</param>
        /// <returns>The requested list of <see cref="ControlModuleConnection"/> related to the specified element.</returns>
        public ControlModuleConnection[] GetByElement(ElementBase element, ControlModule.ModuleType type)
        {
            string sql = string.Empty;
            ControlModuleConnection item = null;

            ControlModuleConnection[] items = null;

            Logger.LogDebug(this,
                            "[CLASS].GetByElement([{0}], {1})",
                            (element != null ? element.ID.ToString() : "null"), type);

            // Return an empty array if element is null or doesn't have outputs
            if (element == null)
            {
                return(new ControlModuleConnection[0]);
            }
            else if (type == ControlModule.ModuleType.Accessory && (element.AccessoryConnections == null || element.AccessoryConnections.Length <= 0))
            {
                return(new ControlModuleConnection[0]);
            }
            else if (type == ControlModule.ModuleType.Sensor && (element.FeedbackConnections == null || element.FeedbackConnections.Length <= 0))
            {
                return(new ControlModuleConnection[0]);
            }

            try
            {
                switch (type)
                {
                case ControlModule.ModuleType.Accessory:
                    items = new ControlModuleConnection[element.AccessoryConnections.Length];
                    break;

                case ControlModule.ModuleType.Sensor:
                    items = new ControlModuleConnection[element.FeedbackConnections.Length];
                    break;
                }

                Connect();

                sql = @"SELECT 
                        " + ControlModuleConnectionManager.SQL_FIELDS_SELECT + @" 
                    FROM 
                        " + ControlModuleConnectionManager.SQL_TABLE + @" con 
                    WHERE 
                        blockid = @blockid And 
                        @type   = (SELECT type 
                                   FROM   " + ControlModuleManager.SQL_TABLE + @" deco 
                                   WHERE  con.decoderid = deco.id) 
                    ORDER BY 
                        [index] Asc,
                        id      Asc";

                SetParameter("blockid", element.ID);
                SetParameter("type", (int)type);

                using (SQLiteDataReader reader = ExecuteReader(sql))
                {
                    while (reader.Read())
                    {
                        item = ControlModuleConnectionManager.ReadEntityRecord(reader);
                        if (item != null && item.Index <= items.Length)
                        {
                            items[item.Index - 1] = item;
                        }
                    }
                }

                return(items);
            }
            catch (Exception ex)
            {
                Logger.LogError(this, ex);

                throw;
            }
            finally
            {
                Disconnect();
            }
        }