Ejemplo n.º 1
0
        public static int SqlUpdate(TransactionUDT udt, string tag)
        {
            int rowCount;

            SqlConnection connection = GetDBConnection(udt.DBSource, udt.DBName, udt.DBSid, udt.DBPwd);

            try
            {
                connection.Open();
            }
            catch (Exception)
            {
                throw;
            }
            try
            {
                // Update statement.
                string sql = "Update " + udt.TableName + " set " + udt.ColumnName + " = @value,PlcTs = @timestamp,RecordTS = @time where TagName = @tag";

                SqlCommand cmd = connection.CreateCommand();
                cmd.CommandText = sql;

                // Add parameter
                cmd.Parameters.Add("@tag", SqlDbType.VarChar).Value        = tag;
                cmd.Parameters.Add("@value", SqlDbType.BigInt).Value       = udt.Data;
                cmd.Parameters.Add("@timestamp", SqlDbType.DateTime).Value = udt.TS;
                cmd.Parameters.Add("@time", SqlDbType.DateTime).Value      = DateTime.Now;

                // Execute Command (for Delete,Insert or Update).
                rowCount = cmd.ExecuteNonQuery();
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                connection.Close();
                connection.Dispose();
                connection = null;
            }

            return(rowCount);
        }
        // Transaction udt timer event
        private void OnTransactionTimer(object sender, ElapsedEventArgs e)
        {
            transactionTimer.Stop();

            Log.Debug("FactoryData Plc Service Transaction Timer Event");
            var resultList   = new List <TransactionRecord>();
            var resultsAvail = false;

            foreach (ListTag elem in transactionList)
            {
                Type   udtType = typeof(TransactionUDT);
                var    udt     = new TransactionUDT();
                string trigger = string.Concat(elem.Name, ".Trigger");
                string ack     = string.Concat(elem.Name, ".Ack");



                // Trigger bit check
                var triggerCheck = bool.Parse(logixTransactionDrv.Read(trigger));

                if (triggerCheck == true)
                {
                    resultsAvail = true;

                    try
                    {
                        udt = (TransactionUDT)logixTransactionDrv.ReadUDT(elem.Name, udtType);
                    }
                    catch (Exception ex)
                    {
                        Log.Error("Read transaction udt Exception: " + ex.Message);
                    }

                    // Transaction record
                    var result = new TransactionRecord
                    {
                        TagName = elem.Name,
                        Udt     = udt
                    };

                    // Populate result list
                    resultList.Add(result);

                    // Set acknowledge bit
                    logixTransactionDrv.Write(ack, 1);
                    logixTransactionDrv.Write(ack, 0);
                }
            }

            if (resultsAvail == true)
            {
                var msgObject = new TransactionResultsMsg
                {
                    Records = resultList
                };

                var type = new Type[] { typeof(TransactionResultsMsg) };

                using (var msg = new SendMessage())
                {
                    msg.Send(msgObject, "TransactionTag_Results", type);
                }

                resultsAvail = false;
            }

            transactionTimer.Start();
        }