Esempio n. 1
0
        public override async Task Execute(CallbackQuery callbackQuery, TelegramBotClient bot)
        {
            string[] data = callbackQuery.Data.Split("|");
            long     sensorID;

            if ((data.Length > 1) && long.TryParse(data[1], out sensorID))
            {
                TempDTO sensorTemp = await sensorTempReader.GetTempBySensor(sensorID);

                await bot.AnswerCallbackQueryAsync(callbackQueryId : callbackQuery.Id, text : $"Selected sensor with ID: {sensorID}");

                await bot.SendTextMessageAsync(chatId : callbackQuery.Message.Chat.Id, text : $"Sensor \"{sensorTemp.Name}\": {Math.Round(sensorTemp.Temperature, 1)}°C");
            }
            else
            {
                await bot.SendTextMessageAsync(chatId : callbackQuery.Message.Chat.Id, text : "Wrong command. SensorID is not found");
            }
        }
Esempio n. 2
0
        public ActionResult StringDCGridPartial(int stationId, string date, string hour)
        {
            SetCultureInfo();

            List <TempDTO> strTagNames = DB.stationString
                                         .Join(DB.Tags, r => r.STRING_ID, ro => ro.ID, (r, ro) => new { r, ro })
                                         .Where(x => x.r.STATION_ID == stationId && x.r.IS_DELETED == false)
                                         .GroupBy(x => x.ro.NAME)
                                         .Select(g => new TempDTO {
                NAME = g.Key, ID = g.FirstOrDefault().ro.ID
            })
                                         .ToList();

            for (int i = 0; i < strTagNames.Count; i++)
            {
                strTagNames[i].INPUT_NO = Convert.ToInt32(strTagNames[i].NAME.Split('_')[3].Replace(".", ""));
                strTagNames[i].INV_NO   = Convert.ToInt32(strTagNames[i].NAME.Split('_')[0].Replace("INV", ""));
            }

            strTagNames = strTagNames.OrderBy(x => x.INV_NO).ThenBy(x => x.INPUT_NO).ToList();

            DataTable dt = new DataTable();

            if (strTagNames == null || strTagNames.Count == 0)
            {
                return(PartialView(dt));
            }


            List <string> sameTag = new List <string>();

            if (strTagNames != null)
            {
                string sck = string.Empty;
                foreach (TempDTO tag in strTagNames)
                {
                    sck = tag.NAME.Split('_')[0];

                    if (!dt.Columns.Contains(sck + " (A)"))
                    {
                        dt.Columns.Add(sck + " (A)");
                        sameTag = strTagNames.Where(x => x.NAME.Split('_')[0] == sck).Select(x => x.NAME).ToList();

                        if (sameTag != null)
                        {
                            for (int i = 0; i <= sameTag.Count - 1; i++)
                            {
                                if (dt.Rows.Count == 0 || dt.Rows.Count < (i + 1))
                                {
                                    DataRow rw = dt.NewRow();
                                    rw[sck + " (A)"] = sameTag[i];
                                    dt.Rows.Add(rw);
                                }
                                else
                                {
                                    dt.Rows[i][sck + " (A)"] = sameTag[i];
                                }
                            }
                        }
                    }
                }
            }

            DateTime curDate = DateTime.Now;

            Thread.CurrentThread.CurrentCulture = new CultureInfo("tr-TR");
            List <STR_DTO> values = new List <STR_DTO>();

            if (hour == "")
            {
                values = (from so in DB.StringOzetLive
                          join t in DB.Tags on so.STRING_ID equals t.ID
                          join tct in DB.stationString on t.ID equals tct.STRING_ID
                          where so.STATION_ID == stationId &&
                          tct.STATION_ID == stationId &&
                          tct.IS_DELETED == false &&
                          t.IS_DELETED == false &&
                          tct.IS_DELETED == false
                          select new STR_DTO
                {
                    NAME = t.NAME,
                    ID = t.ID,
                    date = so.INSERT_DATE.Value,
                    VALUE = so.VALUE
                }
                          ).ToList();
                ViewBag.Date = values[0].date;
            }
            else
            {
                DateTime slcDate = DateTime.Parse(date);
                DateTime nowDate = DateTime.Now;

                if (nowDate.Year == slcDate.Year && nowDate.Month == slcDate.Month) //Quarter String
                {
                    string _date = date + " " + hour;
                    ViewBag.Date = _date;
                    string[] hourSplit    = hour.Split(':');
                    var      _convertDate = ConvertNumberFormatDateAndHourQuarter(date, hourSplit[0], hourSplit[1])._begin;
                    values = (from so in DB.StringOzetQuarterHourAVG
                              join t in DB.Tags on so.STRING_ID equals t.ID
                              join tct in DB.stationString on t.ID equals tct.STRING_ID
                              where so.STATION_ID == stationId && tct.STATION_ID == stationId &&
                              tct.IS_DELETED == false &&
                              t.IS_DELETED == false &&
                              so.TARIH_NUMBER == _convertDate
                              select new STR_DTO
                    {
                        NAME = t.NAME,
                        ID = t.ID,
                        VALUE = (float)Math.Round(so.VALUE, 2)
                    }
                              ).ToList();
                }
                else //Hourly String
                {
                    string _date = date + " " + hour;
                    ViewBag.Date = _date;
                    string[] hourSplit    = hour.Split(':');
                    var      _convertDate = ConvertNumberFormatDateAndHour(date, hourSplit[0])._begin;
                    values = (from so in DB.StringOzetAVG
                              join t in DB.Tags on so.STRING_ID equals t.ID
                              join tct in DB.stationString on t.ID equals tct.STRING_ID
                              where so.STATION_ID == stationId && tct.STATION_ID == stationId &&
                              tct.IS_DELETED == false &&
                              t.IS_DELETED == false &&
                              so.TARIH_NUMBER == _convertDate
                              select new STR_DTO
                    {
                        NAME = t.NAME,
                        ID = t.ID,
                        VALUE = (float)Math.Round(so.VALUE, 2)
                    }
                              ).ToList();
                }
            }
            if (values.Count != 0)
            {
                //ViewBag.Date = values[0].date;

                if (dt.Columns.Count > 0 && dt.Rows.Count > 0 && values != null)
                {
                    string cellTagName = string.Empty;
                    foreach (DataRow rw in dt.Rows)
                    {
                        foreach (DataColumn col in dt.Columns)
                        {
                            cellTagName = rw[col].ToString();

                            if (values.Any(x => x.NAME == cellTagName))
                            {
                                rw[col] = values.Where(x => x.NAME == cellTagName).FirstOrDefault().VALUE;
                            }
                            else
                            {
                                rw[col] = "-";
                            }
                        }
                    }
                }
            }
            else
            {
                string cellTagName = string.Empty;
                foreach (DataRow rw in dt.Rows)
                {
                    foreach (DataColumn col in dt.Columns)
                    {
                        cellTagName = rw[col].ToString();
                        rw[col]     = "-";
                    }
                }
            }

            if (values.Count != 0)
            {
                decimal _min;

                List <StringMin> _ListStringMin = new List <StringMin>();

                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    _min = Convert.ToDecimal(dt.Rows[0][j]);

                    StringMin _StringMin = new StringMin();

                    _StringMin.ColumnIndex = j;
                    _StringMin.FieldName   = dt.Columns[j].ColumnName;

                    try
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (i == 11)
                            {
                                var dddd = dt.Rows[i][j];
                            }
                            if (dt.Rows[i][j] != DBNull.Value)
                            {
                                if (dt.Rows[i][j].ToString() != "-")
                                {
                                    if (Convert.ToDecimal(dt.Rows[i][j]) < _min)
                                    {
                                        _min                = Convert.ToDecimal(dt.Rows[i][j].ToString());
                                        _StringMin.Min      = _min;
                                        _StringMin.RowIndex = i;
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw;
                    }
                    _ListStringMin.Add(_StringMin);
                }

                ViewBag.StringMin = _ListStringMin;


                // Max değeri bulma -----------------------------
                decimal _max = 0;

                List <StringDeneme> _ListStringMax = new List <StringDeneme>();

                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    _max = 0;

                    StringDeneme _cStringDeneme = new StringDeneme();

                    _cStringDeneme.ColumnIndex = j;
                    _cStringDeneme.FieldName   = dt.Columns[j].ColumnName;

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i][j] != DBNull.Value)
                        {
                            if (dt.Rows[i][j].ToString() != "-")
                            {
                                if (Convert.ToDecimal(dt.Rows[i][j]) > _max)
                                {
                                    _max = Convert.ToDecimal(dt.Rows[i][j]);
                                    _cStringDeneme.Value    = _max;
                                    _cStringDeneme.RowIndex = i;
                                }
                            }
                        }
                    }

                    // j++;

                    _ListStringMax.Add(_cStringDeneme);
                }

                ViewBag.StringMax = _ListStringMax;


                // max bitti--------

                // Avg değeri bulma ----------------------


                List <StringAvg> ListStringAvg = new List <StringAvg>();

                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    decimal count = 0, sum = 0;

                    if (dt.Columns[i].ColumnName.IndexOf("Name") == -1)
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            decimal _ActiveValue = 0;

                            if (dt.Rows[j][i] != DBNull.Value)
                            {
                                if (dt.Rows[j][i].ToString() != "-")
                                {
                                    _ActiveValue = Decimal.Parse(dt.Rows[j][i].ToString());

                                    if (_ActiveValue > 0)
                                    {
                                        sum += _ActiveValue;

                                        count++;
                                    }
                                }
                            }
                        }

                        StringAvg _StrAvg = new StringAvg();

                        if (count != 0)
                        {
                            _StrAvg.Avg = decimal.Round(sum / count, 2);
                        }
                        else
                        {
                            _StrAvg.Avg = 0;
                        }



                        _StrAvg.FieldName   = dt.Columns[i].ColumnName;
                        _StrAvg.ColumnIndex = i;

                        ListStringAvg.Add(_StrAvg);
                    }
                }
                ViewBag.StringAvg = ListStringAvg;
                //Inverter Toplam
                var invGroup = (from u in ListStringAvg
                                //group u by u.FieldName.Substring(0, 4) into g
                                select new StringInvNameAvg {
                    invName = "Inverter " + u.FieldName, Avg = Math.Round(u.Avg, 2)
                })
                               .ToList();

                ViewBag.ListInvAVG = invGroup;
                // avg bitti-----------------------------------------

                dt.Columns.Add("Tag", typeof(string)).SetOrdinal(0);

                TempDTO mt = strTagNames.AsEnumerable()
                             .GroupBy(r => r.NAME.Split('_')[0])
                             .Select(g => new TempDTO {
                    NAME = g.Key, ID = g.Count()
                })                                                                      //(g => new TempDTO{ NAME = g.OrderByDescending(r => r.NAME.Split('_')[0]  } )
                             .OrderByDescending(x => x.ID).ToList().First();


                List <string> tagTemp = strTagNames.Where(x => x.NAME.StartsWith(mt.NAME + "_")).Select(X => X.NAME).ToList();

                List <OrderValue> _ListOrderValueTemp = new List <OrderValue>();

                for (int i = 0; i < tagTemp.Count; i++)
                {
                    OrderValue _OrderValue = new OrderValue();

                    _OrderValue.InverterNumber = Convert.ToInt32(tagTemp[i].Split('_')[3].Replace(".", ""));
                    _OrderValue.InverterName   = tagTemp[i];

                    _ListOrderValueTemp.Add(_OrderValue);
                }

                List <OrderValue> _ListOrderValue = _ListOrderValueTemp.OrderBy(x => x.InverterNumber).ToList();

                List <string> tagCOl = new List <string>();

                for (int i = 0; i < _ListOrderValue.Count; i++)
                {
                    tagCOl.Add(_ListOrderValue[i].InverterName);
                }

                for (int i = 0; i <= tagCOl.Count - 1; i++)
                {
                    if (dt.Rows.Count < i)
                    {
                        break;
                    }
                    string[] ccc = tagCOl[i].Split('_');

                    if (tagCOl[i].Contains("temp"))
                    {
                        dt.Rows[i]["Tag"] = "Temp.";
                    }
                    else
                    {
                        dt.Rows[i]["Tag"] = ccc[1] + " " + ccc[2] + " " + ccc[3] + " " + ccc[4];
                    }
                }


                //ViewData["columns"] = columns;



                #region ColumnsSums
                DataTable _dtSums = dt.Clone();

                _dtSums.Rows.Clear();

                DataRow _drSums = _dtSums.NewRow();

                for (int i = 1; i < dt.Columns.Count; i++)
                {
                    decimal _sum = 0;

                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        if (dt.Rows[j][i] != DBNull.Value)
                        {
                            if (dt.Rows[j][i].ToString() != "-")
                            {
                                _sum += Convert.ToDecimal(dt.Rows[j][i]);
                            }
                        }
                    }

                    _drSums[i] = _sum;
                }

                _dtSums.Rows.Add(_drSums);
                #endregion

                #region StandardDeviation

                DataTable _dtSD = dt.Clone();
                _dtSD.Rows.Clear();

                DataRow _drSD = _dtSD.NewRow();

                List <StringAvg> _ListStringAvg = ViewBag.StringAvg as List <StringAvg>;

                for (int i = 1; i < dt.Columns.Count; i++)
                {
                    double _value = 0;

                    int n = -1;

                    double _avg = 0;

                    StringAvg _cAvg = _ListStringAvg.Where(x => x.FieldName == dt.Columns[i].ColumnName).FirstOrDefault();

                    if (_cAvg != null)
                    {
                        _avg = Convert.ToDouble(_cAvg.Avg);
                    }

                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        if (dt.Rows[j][i] != DBNull.Value)
                        {
                            if (dt.Rows[j][i].ToString() != "-")
                            {
                                n++;
                                double _value2 = Convert.ToDouble(dt.Rows[j][i]) - _avg;
                                _value2 = _value2 * _value2;
                                _value += _value2;
                            }
                        }
                    }

                    _value   = Math.Sqrt(_value / n);
                    _value   = Math.Round(_value, 2);
                    _drSD[i] = _value;
                }
                _dtSD.Rows.Add(_drSD);

                ViewBag._dtSD = _dtSD;
                #endregion
            }
            return(PartialView(dt));
        }