示例#1
0
        /// <summary>
        /// Returns a new instance of <see cref="ControlModuleConnection"/>.
        /// </summary>
        /// <param name="name">Name of the connection.</param>
        /// <param name="module">Control module associated.</param>
        public ControlModuleConnection(string name, ControlModule module)
        {
            Initialize();

            this.Name      = name.Trim();
            this.DecoderID = module.ID;
        }
示例#2
0
        /// <summary>
        /// Returns a new instance of <see cref="ControlModuleConnection"/>.
        /// </summary>
        /// <param name="module">Control module associated.</param>
        /// <param name="element">Associated element of the output.</param>
        public ControlModuleConnection(ControlModule module, ElementBase element)
        {
            Initialize();

            this.DecoderID = module.ID;
            this.ElementID = element.ID;
        }
示例#3
0
        /// <summary>
        /// Returns a new instance of <see cref="ControlModuleConnection"/>.
        /// </summary>
        /// <param name="name">Conetion name.</param>
        /// <param name="module">Control module associated.</param>
        /// <param name="address">Digital address.</param>
        /// <param name="output">Output index (starting by 1).</param>
        public ControlModuleConnection(string name, ControlModule module, int address, int output)
        {
            Initialize();

            this.Name      = name.Trim();
            this.DecoderID = module.ID;
            this.Address   = address;
            this.Output    = output;
        }
        /// <summary>
        /// Get all connections of the specified module.
        /// </summary>
        /// <param name="module">Module.</param>
        /// <returns></returns>
        public System.Data.DataTable FindByModule(ControlModule module)
        {
            string sql = string.Empty;

            Logger.LogDebug(this, "[CLASS].FindByModule({0})", module);

            try
            {
                Connect();

                sql = @"SELECT DISTINCT 
                        ao.id       As ""#ID"", 
                        ao.name     As ""Output"", 
                        b.name      As ""Block"", 
                        ao.address  As ""Address"", 
                        ao.blockid  As ""BlockID""
                    FROM 
                        " + ControlModuleConnectionManager.SQL_TABLE + @" ao 
                        LEFT JOIN " + ElementManager.SQL_TABLE + @" b On ao.blockid = b.id 
                    WHERE 
                        ao.decoderid = @decoderid 
                    ORDER BY 
                        ao.name  Asc, 
                        ao.[index] Asc";

                SetParameter("decoderid", module.ID);

                System.Data.DataTable dt = ExecuteDataTable(sql);

                foreach (DataRow row in dt.Rows)
                {
                    if ((Int64)row[4] == 0)
                    {
                        row[2] = "<empty>";
                    }
                }

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

                throw;
            }
            finally
            {
                Disconnect();
            }
        }
        /// <summary>
        /// Read a category from the current reader record.
        /// </summary>
        internal static ControlModule ReadEntityRecord(SQLiteDataReader reader)
        {
            ControlModule record = new ControlModule();

            record.ID           = reader.GetInt32(0);
            record.Name         = reader.IsDBNull(1) ? string.Empty : reader.GetString(1);
            record.Manufacturer = reader.IsDBNull(2) ? string.Empty : reader.GetString(2);
            record.Model        = reader.IsDBNull(3) ? string.Empty : reader.GetString(3);
            record.Outputs      = reader.IsDBNull(4) ? 1 : reader.GetInt32(4);
            record.StartAddress = reader.IsDBNull(5) ? 0 : reader.GetInt32(5);
            record.Type         = reader.IsDBNull(6) ? ControlModule.ModuleType.Accessory : (ControlModule.ModuleType)reader.GetInt32(6);
            record.Notes        = reader.IsDBNull(7) ? string.Empty : reader.GetString(7);

            return(record);
        }
        /// <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>
        /// Update the specified control module.
        /// </summary>
        /// <param name="module">An instance with the updated data.</param>
        public void Update(ControlModule module)
        {
            string sql = string.Empty;

            Logger.LogDebug(this, "[CLASS].Update([{0}])", module);

            try
            {
                Connect();

                sql = @"UPDATE 
                        " + ControlModuleManager.SQL_TABLE + @" 
                    SET 
                        name           = @name, 
                        manufacturer   = @manufacturer, 
                        model          = @model,
                        outputs        = @outputs,
                        startaddress   = @startaddress,
                        type           = @type,
                        description    = @description
                    WHERE 
                        id = @id";

                SetParameter("name", module.Name);
                SetParameter("manufacturer", module.Manufacturer);
                SetParameter("model", module.Model);
                SetParameter("outputs", module.Outputs);
                SetParameter("startaddress", module.StartAddress);
                SetParameter("type", module.Type);
                SetParameter("description", module.Notes);
                SetParameter("id", module.ID);

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

                throw;
            }
            finally
            {
                Disconnect();
            }
        }
        /// <summary>
        /// Gets all blocks from a specified switchboard panel.
        /// </summary>
        /// <param name="decoderId">The switchboard panel unique identifier (DB).</param>
        /// <returns>The requested list of <see cref="ElementBase"/>.</returns>
        public List <ControlModuleConnection> GetByDecoder(int decoderId)
        {
            string                         sql    = string.Empty;
            ControlModule                  module = null;
            ControlModuleConnection        item   = null;
            List <ControlModuleConnection> items  = new List <ControlModuleConnection>();

            Logger.LogDebug(this, "[CLASS].GetByDecoder({0})", decoderId);

            try
            {
                Connect();

                sql = @"SELECT 
                        " + ControlModuleManager.SQL_FIELDS_SELECT + @" 
                    FROM 
                        " + ControlModuleManager.SQL_TABLE + @" 
                    WHERE 
                        id = @id";

                SetParameter("id", decoderId);

                using (SQLiteDataReader reader = ExecuteReader(sql))
                {
                    if (reader.Read())
                    {
                        module = ControlModuleManager.ReadEntityRecord(reader);
                    }
                }

                if (module == null)
                {
                    return(items);
                }

                sql = @"SELECT 
                        " + ControlModuleConnectionManager.SQL_FIELDS_SELECT + @" 
                    FROM 
                        " + ControlModuleConnectionManager.SQL_TABLE + @" 
                    WHERE 
                        decoderid = @decoderid 
                    ORDER BY 
                        name";

                SetParameter("decoderid", decoderId);

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

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

                throw;
            }
            finally
            {
                Disconnect();
            }
        }
        /// <summary>
        /// Adds a new module into the current project.
        /// </summary>
        /// <param name="module">An instance of <see cref="ControlModule"/> containing the data.</param>
        public void Add(ControlModule module)
        {
            int    addr = 0;
            string sql  = string.Empty;
            ControlModuleConnectionManager connManager = null;

            Logger.LogDebug(this, "[CLASS].Add([{0}])", module);

            try
            {
                Connect();

                // Adds the new decoder
                sql = @"INSERT INTO 
                        " + ControlModuleManager.SQL_TABLE + @" (" + ControlModuleManager.SQL_FIELDS_INSERT + @") 
                    VALUES 
                        (@name, @manufacturer, @model, @outputs, @startaddress, @type, @description)";

                SetParameter("name", module.Name);
                SetParameter("manufacturer", module.Manufacturer);
                SetParameter("model", module.Model);
                SetParameter("outputs", module.Outputs);
                SetParameter("startaddress", module.StartAddress);
                SetParameter("type", module.Type);
                SetParameter("description", module.Notes);

                ExecuteNonQuery(sql);

                // Gets the generated new ID
                sql = @"SELECT 
                        Max(id) As id 
                    FROM 
                        " + ControlModuleManager.SQL_TABLE;

                module.ID = (int)ExecuteScalar(sql);

                Disconnect();

                // Initialize the address for the sensor module
                addr = module.StartAddress;

                // Generate dummy connections with appropriate name
                connManager = new ControlModuleConnectionManager(this.Settings);
                for (int i = 1; i <= module.Outputs; i++)
                {
                    switch (module.Type)
                    {
                    case ControlModule.ModuleType.Accessory:
                        connManager.Add(new ControlModuleConnection(i.ToString(), module));
                        break;

                    case ControlModule.ModuleType.Sensor:
                        connManager.Add(new ControlModuleConnection(i.ToString(), module, addr, i));
                        break;
                    }

                    // Update address by sensor output group
                    if (i % 4 == 0)
                    {
                        addr++;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogError(this, ex);

                throw;
            }
            finally
            {
                Disconnect();
            }
        }