public async Task <IActionResult> GetWeatherBySiteId(int siteId)
        {
            JObject weather = await CommonFactory.RetriveWeather(siteId, _redisDb);

            if (weather == null)
            {
                return(BadRequest());
            }
            else
            {
                return(Ok(weather));
            }
        }
        public async Task <IActionResult> GetStatisticsCurrentValue(int lawcodelevel = 1)
        {
            JObject row_j = null;

            try
            {
                IEnumerable <VwContractorsite> sites = GetAvaliableSites();
                if (sites.Count() == 0)
                {
                    return(Ok());
                }

                var group_sites           = sites.GroupBy(MakeClusterKey(lawcodelevel), value => value);
                IEnumerable <int> siteIds = sites.Select(x => x.SiteId);

                JArray        result     = new JArray();
                List <double> total_socs = new List <double>();
                foreach (IGrouping <string, VwContractorsite> row in group_sites)
                {
                    double        total_energy_power       = 0;
                    double        total_actPwr_charging    = 0;
                    double        total_actPwr_discharging = 0;
                    List <double> socs         = new List <double>();
                    List <double> sohs         = new List <double>();
                    int           first_siteid = -1;
                    foreach (VwContractorsite site in row)
                    {
                        if (first_siteid == -1)
                        {
                            first_siteid = site.SiteId;
                        }
                        // PV
                        string target_redis_key = CommonFactory.CreateRedisKey(site.SiteId, 4, "PV*");
                        var    redis_keys       = CommonFactory.SearchKeys(_redisConn, target_redis_key);
                        foreach (RedisKey pv_key in redis_keys)
                        {
                            double TotalActivePower = (double)await _redisDb.HashGetAsync(pv_key, "TotalActivePower");

                            total_energy_power += TotalActivePower;
                        }

                        // PCS
                        target_redis_key = CommonFactory.CreateRedisKey(site.SiteId, 1, "PCS*");
                        redis_keys       = CommonFactory.SearchKeys(_redisConn, target_redis_key);
                        foreach (RedisKey key in redis_keys)
                        {
                            double TotalActivePower = (double)await _redisDb.HashGetAsync(key, "actPwrKw");

                            if (TotalActivePower > 0)
                            {
                                total_actPwr_discharging += TotalActivePower;
                            }
                            else
                            {
                                total_actPwr_charging += TotalActivePower;
                            }
                        }

                        // BMS
                        target_redis_key = CommonFactory.CreateRedisKey(site.SiteId, 2, "BMS*");
                        redis_keys       = CommonFactory.SearchKeys(_redisConn, target_redis_key);
                        foreach (RedisKey key in redis_keys)
                        {
                            double soc = (double)await _redisDb.HashGetAsync(key, "bms_soc");

                            socs.Add(soc);
                            total_socs.Add(soc);
                            double soh = (double)await _redisDb.HashGetAsync(key, "bms_soh");

                            sohs.Add(soh);
                        }
                    }

                    JObject weather_obj = new JObject();
                    if (row.Count() > 0)
                    {
                        // Weather
                        weather_obj = await CommonFactory.RetriveWeather(row.FirstOrDefault().SiteId, _redisDb);
                    }

                    row_j = new JObject();
                    row_j.Add("LawCode", row.Key);
                    row_j.Add("total_pvpower", total_energy_power);
                    row_j.Add("total_charging", Math.Abs(total_actPwr_charging));
                    row_j.Add("total_discharging", total_actPwr_discharging);
                    row_j.Add("average_soc", socs.Count() > 0 ? socs.Average() : 0);
                    row_j.Add("average_soh", sohs.Count() > 0 ? sohs.Average() : 0);
                    row_j.Add("total_count", row.Count());
                    row_j.Add("weather", weather_obj);
                    result.Add(row_j);
                }

                JObject center_weather = await CommonFactory.RetriveWeather(0, _redisDb);

                return(Ok(new { group = result, total_average_soc = total_socs.Average(), total_event_count = 0, total_sites_count = siteIds.Count(), controlcenter_weather = center_weather }));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }
        public async Task <IActionResult> GetStatisticsCurrentValueByRcc()
        {
            JObject row_j = null;

            try
            {
                IQueryable <IGrouping <int, int> > idsGroup = GetRccCodeGroup();
                JArray        result     = new JArray();
                List <double> total_socs = new List <double>();
                foreach (IGrouping <int, int> row in idsGroup)
                {
                    double        total_energy_power       = 0;
                    double        total_actPwr_charging    = 0;
                    double        total_actPwr_discharging = 0;
                    List <double> socs        = new List <double>();
                    List <double> sohs        = new List <double>();
                    JObject       weather_obj = null;
                    foreach (int SiteId in row)
                    {
                        if (weather_obj == null)
                        {
                            weather_obj = await CommonFactory.RetriveWeather(SiteId, _redisDb);
                        }
                        // PV
                        string target_redis_key = CommonFactory.CreateRedisKey(SiteId, 4, "PV*");
                        var    redis_keys       = CommonFactory.SearchKeys(_redisConn, target_redis_key);
                        foreach (RedisKey pv_key in redis_keys)
                        {
                            double TotalActivePower = (double)await _redisDb.HashGetAsync(pv_key, "TotalActivePower");

                            total_energy_power += TotalActivePower;
                        }

                        // PCS
                        target_redis_key = CommonFactory.CreateRedisKey(SiteId, 1, "PCS*");
                        redis_keys       = CommonFactory.SearchKeys(_redisConn, target_redis_key);
                        foreach (RedisKey key in redis_keys)
                        {
                            double TotalActivePower = (double)await _redisDb.HashGetAsync(key, "actPwrKw");

                            if (TotalActivePower > 0)
                            {
                                total_actPwr_discharging += TotalActivePower;
                            }
                            else
                            {
                                total_actPwr_charging += TotalActivePower;
                            }
                        }

                        // BMS
                        target_redis_key = CommonFactory.CreateRedisKey(SiteId, 2, "BMS*");
                        redis_keys       = CommonFactory.SearchKeys(_redisConn, target_redis_key);
                        foreach (RedisKey key in redis_keys)
                        {
                            double soc = (double)await _redisDb.HashGetAsync(key, "bms_soc");

                            socs.Add(soc);
                            total_socs.Add(soc);
                            double soh = (double)await _redisDb.HashGetAsync(key, "bms_soh");

                            sohs.Add(soh);
                        }
                    }

                    row_j = new JObject();
                    row_j.Add("rcc", row.Key);
                    row_j.Add("total_pvpower", total_energy_power);
                    row_j.Add("total_charging", Math.Abs(total_actPwr_charging));
                    row_j.Add("total_discharging", total_actPwr_discharging);
                    row_j.Add("average_soc", socs.Count() > 0 ? socs.Average() : 0);
                    row_j.Add("average_soh", sohs.Count() > 0 ? sohs.Average() : 0);
                    row_j.Add("total_count", row.Count());
                    row_j.Add("weather", weather_obj);
                    result.Add(row_j);
                }

                JObject center_weather = await CommonFactory.RetriveWeather(0, _redisDb);

                return(Ok(new { group = result, total_average_soc = total_socs.Average(), total_event_count = 0, controlcenter_weather = center_weather }));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex));
            }
        }