Exemplo n.º 1
0
        public bool changeVal(string key, int hh, int min, string newVal, ref string changedVal, ref string message)
        {
            Logger.info(String.Format("Изменение значения по объекту [{0}] [{1}:{2}]:", key, hh, min));
            DateTime   date   = DateStart.AddMinutes(min).AddHours(hh);
            DataRecord rec    = Data[key];
            string     oldVal = rec.Values[date];


            Logger.info(String.Format("==={0}: Дата: {1} OldVal={2} newVal={3}", rec.Name, date.ToString("dd.MM.yyyy HH:mm"), oldVal, newVal));

            bool result = true;

            /*changedVal = newVal;
             * message = String.Format("Изменено значение [{0}] [{1}] ====== OldVal={2} ======  newVal={3} ",
             *              date.ToString("dd.MM.yyyy HH:mm"), rec.Name, oldVal, newVal);
             * return result;*/

            SqlConnection con = getConnection();

            try {
                con.Open();
                SqlTransaction trans = con.BeginTransaction();
                Logger.info(String.Format("======Удаление значения"));
                string comSTR = "";
                comSTR = String.Format("DELETE FROM DATA WHERE OBJECT={0} AND OBJTYPE={1} AND ITEM={2} AND PARNUMBER=12 AND DATA_DATE='{3}'",
                                       rec.Source.Obj, rec.Source.ObjType, rec.Source.Item, date.ToString(DateFormat));
                Logger.info(comSTR);
                SqlCommand command = new SqlCommand(comSTR, con);
                command.Transaction = trans;
                command.ExecuteNonQuery();
                Logger.info(String.Format("=========OK"));

                if (newVal != "---")
                {
                    Logger.info(String.Format("======Вставка значения"));
                    comSTR = String.Format("INSERT INTO Data (parnumber, object, objtype, item, value0, value1, data_date, rcvstamp, season, appid, p2kstatus, p2kstatush) SELECT	12, {0}, {1}, {2}, {3}, {3}, '{4}', '{4}', {5}, 2, 0, 0",
                                           rec.Source.Obj, rec.Source.ObjType, rec.Source.Item, newVal.Replace(",", "."), date.ToString(DateFormat), CurrentSeason);
                    Logger.info(comSTR);
                    command             = new SqlCommand(comSTR, con);
                    command.Transaction = trans;
                    command.ExecuteNonQuery();
                    Logger.info(String.Format("=========OK"));
                }
                Logger.info(String.Format("======Применение транзакции"));
                trans.Commit();
                Logger.info(String.Format("=========OK"));
                message = String.Format("Изменено значение [{0}] [{1}] ====== OldVal={2} ======  newVal={3} ",
                                        date.ToString("dd.MM.yyyy HH:mm"), rec.Name, oldVal, newVal);
            } catch (Exception e) {
                Logger.info(String.Format("===Ошибка в транзакции, значение не изменено " + e.ToString()));
                con.Close();
                result     = false;
                changedVal = rec.Values[date];
            }

            try {
                Logger.info(String.Format("======Чтение значения"));
                if (con.State != System.Data.ConnectionState.Open)
                {
                    con.Open();
                }
                string comSTR = "";
                comSTR = String.Format("SELECT VALUE0 FROM DATA WHERE OBJECT={0} AND OBJTYPE={1} AND ITEM={2} AND PARNUMBER=12 AND DATA_DATE='{3}'",
                                       rec.Source.Obj, rec.Source.ObjType, rec.Source.Item, date.ToString(DateFormat));
                SqlCommand command = new SqlCommand(comSTR, con);
                object     nv      = command.ExecuteScalar();
                if (nv == null)
                {
                    changedVal       = "---";
                    rec.Values[date] = "---";
                    Logger.info(String.Format("========значение не найдено: {0}", changedVal));
                }
                else
                {
                    double val = Convert.ToDouble(nv);
                    rec.Values[date] = String.Format("{0:0.##}", val).Replace(",", ".");
                    changedVal       = rec.Values[date];
                    Logger.info(String.Format("========получено значение: {0}", changedVal));
                }
                con.Close();
            } catch (Exception e) {
                Logger.info("===Ошибка при чтении нового значения " + e.ToString());
            }
            try {
                if (con.State == System.Data.ConnectionState.Open)
                {
                    con.Close();
                }
            } catch { }

            return(result);
        }
Exemplo n.º 2
0
        public void ReadNames()
        {
            Logger.info(String.Format("Чтение наименований "));
            Names = new Dictionary <string, string>();
            Data  = new Dictionary <string, DataRecord>();

            /*for (int i = 0; i <= 50; i++) {
             *      string name = "Parametr" + i.ToString();
             *      string codeStr = String.Format("{0}_{1}_{2}", Source.Obj, Source.ObjType, i);
             *      Names.Add(codeStr, name);
             *
             *      DataRecord rec = new DataRecord();
             *      rec.Key = codeStr;
             *      rec.Name = name;
             *      rec.Source = SourceInfo.getFullInfo(codeStr);
             *      rec.Values = new Dictionary<DateTime, string>();
             *      foreach (DateTime date in Dates) {
             *              rec.Values.Add(date, "---");
             *      }
             *      Data.Add(codeStr, rec);
             * }
             * return;*/
            try {
                SqlConnection con = getConnection();
                con.Open();
                string comSTR = "";

                if (ObjType == 0)
                {
                    comSTR = String.Format("select dev.name+' '+sen.name,sen.code,dev.code from devices as dev left join sensors as sen on dev.id=sen.stationid where dev.code in ({0}) order by dev.name+' '+sen.name", String.Join(",", Objs));
                }
                else
                {
                    comSTR = String.Format("SELECT NAME,CODE,0 FROM CLIENTS order by code");
                }
                SqlCommand    command = new SqlCommand(comSTR, con);
                SqlDataReader reader  = command.ExecuteReader();
                while (reader.Read())
                {
                    string name = reader.GetString(0);
                    int    code = reader.GetInt32(1);
                    int    obj  = reader.GetInt32(2);
                    name = String.Format("{0}_{1}: {2}", obj, code, name);
                    string codeStr = String.Format("{0}_{1}_{2}", obj, ObjType, code);
                    Names.Add(codeStr, name);

                    DataRecord rec = new DataRecord();
                    rec.Key    = codeStr;
                    rec.Name   = name;
                    rec.Source = SourceInfo.getFullInfo(codeStr);
                    rec.Values = new Dictionary <DateTime, string>();
                    foreach (DateTime date in Dates)
                    {
                        rec.Values.Add(date, "---");
                    }
                    Data.Add(codeStr, rec);
                }
                reader.Close();
                con.Close();
            } catch (Exception e) {
                Logger.info("Ошибка при чтении наименований источника " + e.ToString());
            }
        }