예제 #1
0
        private void OneTableUpdate(object obj)
        {
            StorageContext storageContext = (StorageContext)obj;
            TickTimer      tickTimer      = new TickTimer();

            tickTimer.Start();
            DataWareHouse.WriteLog(string.Format("EEEEEEEEEEEEEEEEE [{0}] Table: {1}", Thread.CurrentThread.Name, storageContext._tablePrefix + storageContext._tableSuffix), new string[0]);
            DbConnection dbConnection = null;
            DbCommand    dbCommand    = null;

            try
            {
                if (storageContext._deviceVoltageList != null)
                {
                    DeviceOperation.UpdateDeviceVoltage(storageContext._deviceVoltageList);
                }
                else
                {
                    if (storageContext._lastSensorMaxValue != null)
                    {
                        RackInfo.GenerateAllRackThermal(storageContext._lastSensorMaxValue, storageContext._tDataTime);
                    }
                    else
                    {
                        if (storageContext._AllData4Access != null)
                        {
                            DataWareHouse.AccessMinuteData(storageContext._AllData4Access, 1, storageContext._tDataTime);
                        }
                        else
                        {
                            try
                            {
                                string arg;
                                if (storageContext._tablePrefix.StartsWith("dev_min_"))
                                {
                                    arg = "device_id";
                                }
                                else
                                {
                                    if (!storageContext._tablePrefix.StartsWith("port_min_"))
                                    {
                                        return;
                                    }
                                    arg = "port_id";
                                }
                                dbConnection = new MySqlConnection(string.Concat(new object[]
                                {
                                    "Database=",
                                    DBUrl.DB_CURRENT_NAME,
                                    ";Data Source=",
                                    DBUrl.CURRENT_HOST_PATH,
                                    ";Port=",
                                    DBUrl.CURRENT_PORT,
                                    ";User Id=",
                                    DBUrl.CURRENT_USER_NAME,
                                    ";Password="******";Pooling=true;Min Pool Size=0;Max Pool Size=150;Default Command Timeout=0;charset=utf8;"
                                }));
                                dbConnection.Open();
                                dbCommand = dbConnection.CreateCommand();
                                try
                                {
                                    string text   = string.Format("insert into {0}{1} ({2},power_consumption,power,insert_time) values ", storageContext._tablePrefix, storageContext._tableSuffix, arg);
                                    int    num    = storageContext._tDataTime.Hour * 60 + storageContext._tDataTime.Minute;
                                    string format = string.Format("({{{0}}},{{{1}}},{{{2}}},{3})", new object[]
                                    {
                                        "0",
                                        "1",
                                        "2",
                                        num
                                    });
                                    string        text2         = "";
                                    StringBuilder stringBuilder = new StringBuilder();
                                    int           count         = storageContext._htData.Count;
                                    int           num2          = 0;
                                    stringBuilder.Append(text);
                                    foreach (DictionaryEntry dictionaryEntry in storageContext._htData)
                                    {
                                        num2++;
                                        try
                                        {
                                            double value = storageContext._dataRatio * Convert.ToDouble(dictionaryEntry.Value);
                                            string arg2  = string.Format("{0}", Convert.ToInt64(value));
                                            text2 = "";
                                            string text3 = string.Format(format, dictionaryEntry.Key, "null", arg2);
                                            if (stringBuilder.Length > text.Length)
                                            {
                                                stringBuilder.Append(",");
                                            }
                                            stringBuilder.Append(text3);
                                            if ((num2 >= count || (long)(stringBuilder.Length + text3.Length) > this._maxPacketLength / 2L) && stringBuilder.Length > 0)
                                            {
                                                dbCommand.CommandText = stringBuilder.ToString();
                                                text2 = dbCommand.CommandText;
                                                dbCommand.ExecuteNonQuery();
                                                stringBuilder.Clear();
                                                stringBuilder.Append(text);
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            if (ex.GetType().FullName.Equals("MySql.Data.MySqlClient.MySqlException"))
                                            {
                                                try
                                                {
                                                    if (!string.IsNullOrEmpty(text2))
                                                    {
                                                        dbCommand.CommandText = text2;
                                                        dbCommand.ExecuteNonQuery();
                                                    }
                                                }
                                                catch (Exception ex2)
                                                {
                                                    DataWareHouse.WriteLog("Insert data: {0}{1}, Error:{2}", new string[]
                                                    {
                                                        storageContext._tablePrefix,
                                                        storageContext._tableSuffix,
                                                        ex.Message
                                                    });
                                                    if (!string.IsNullOrEmpty(text2) && ex2.GetType().FullName.Equals("MySql.Data.MySqlClient.MySqlException"))
                                                    {
                                                        string tableName = DBUtil.GetTableName(text2);
                                                        if (tableName != null && tableName.Length > 0)
                                                        {
                                                            DBUtil.SetMySQLInfo(tableName);
                                                        }
                                                        DataWareHouse.WriteLog("        MySQL database may be crashed, EcoSensor Monitor Service will shutdown ", new string[0]);
                                                        DebugCenter.GetInstance().appendToFile("DBERROR~~~~~~~~~~~DBERROR : " + ex2.Message + "\n" + ex2.StackTrace);
                                                        DBTools.Write_DBERROR_Log();
                                                        DBUtil.StopService();
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception)
                                {
                                }
                            }
                            catch (Exception ex3)
                            {
                                if (ex3.GetType().FullName.Equals("MySql.Data.MySqlClient.MySqlException"))
                                {
                                    DebugCenter.GetInstance().appendToFile("DBERROR~~~~~~~~~~~DBERROR : " + ex3.Message + "\n" + ex3.StackTrace);
                                    DBTools.Write_DBERROR_Log();
                                    DBUtil.StopService();
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                try
                {
                    if (dbCommand != null)
                    {
                        dbCommand.Dispose();
                    }
                    if (dbConnection != null)
                    {
                        dbConnection.Close();
                        dbConnection.Dispose();
                    }
                }
                catch
                {
                }
                if (storageContext._htData != null)
                {
                    DataWareHouse.WriteLog(string.Format("----------------- [{0}] finished, count={1}, elapsed={2}", Thread.CurrentThread.Name + " " + storageContext._tablePrefix + storageContext._tableSuffix, storageContext._htData.Count, tickTimer.getElapsed().ToString()), new string[0]);
                }
                else
                {
                    DataWareHouse.WriteLog(string.Format("----------------- [{0}] finished, elapsed={1}", Thread.CurrentThread.Name + " " + storageContext._tablePrefix + storageContext._tableSuffix, tickTimer.getElapsed().ToString()), new string[0]);
                }
                if (storageContext._finishSignal != null)
                {
                    storageContext._finishSignal.Release();
                }
                if (storageContext._finishCounter != null)
                {
                    storageContext._finishCounter.Release();
                }
            }
        }