Shuxia Ning
2024-07-26 19db3c68c67e27531e716567cefaa266e71a2baf
IStation.Service/07-global/01-helper/GlobalHelper.cs
@@ -5,7 +5,6 @@
    /// </summary>
    public class GlobalHelper
    {
        #region 默认配置
        #region 泵标志
@@ -108,6 +107,18 @@
        public readonly static string 二取4泵_累计运行时间 = "10170";
        public readonly static string 二取5泵_累计运行时间 = "10171";
        public readonly static string 二取1泵_检修状态 = "10191";
        public readonly static string 二取2泵_检修状态 = "10192";
        public readonly static string 二取3泵_检修状态 = "10193";
        public readonly static string 二取4泵_检修状态 = "10194";
        public readonly static string 二取5泵_检修状态 = "10195";
        public readonly static string 二取1泵_累计运行时间_历史 = "二取水1号主水泵.累计运行时间";
        public readonly static string 二取2泵_累计运行时间_历史 = "二取水2号主水泵.累计运行时间";
        public readonly static string 二取3泵_累计运行时间_历史 = "二取水3号主水泵.累计运行时间";
        public readonly static string 二取4泵_累计运行时间_历史 = "二取水4号主水泵.累计运行时间";
        public readonly static string 二取5泵_累计运行时间_历史 = "二取水5号主水泵.累计运行时间";
        #endregion
@@ -117,36 +128,29 @@
        public readonly static string 嘉定2线_瞬时流量 = "10139";
        public readonly static string 嘉定3线_瞬时流量 = "10140";
        public readonly static string 嘉定1线_瞬时流量_长江管网图 = "10159";
        public readonly static string 嘉定2线_瞬时流量_长江管网图 = "10160";
        public readonly static string 嘉定3线_瞬时流量_长江管网图 = "10161";
        public readonly static string 嘉定1线_表头累计 = "10143";
        public readonly static string 嘉定2线_表头累计 = "10144";
        public readonly static string 嘉定3线_表头累计 = "10145";
        public readonly static string 嘉定1线_累计流量_长江管网图 = "10164";
        public readonly static string 嘉定2线_累计流量_长江管网图 = "10165";
        public readonly static string 嘉定3线_累计流量_长江管网图 = "10166";
        public readonly static string 嘉定1线_压力 = "10128";
        public readonly static string 嘉定2线_压力 = "10129";
        public readonly static string 嘉定3线_压力 = "10130";
        public readonly static string 嘉定1线_压力_长江管网图 = "10154";
        public readonly static string 嘉定2线_压力_长江管网图 = "10155";
        public readonly static string 嘉定3线_压力_长江管网图 = "10156";
        public readonly static string 一输_老前池南侧液位 = "10011";
        public readonly static string 一输_老前池北侧液位 = "10012";
        public readonly static string 一输_新前池液位 = "10013";
        public readonly static string 一输11泵_出口压力 = "10026";
        public readonly static string 一输12泵_出口压力 = "10027";
@@ -156,7 +160,6 @@
        public readonly static string 一输16泵_出口压力 = "10031";
        public readonly static string 一输17泵_出口压力 = "10032";
        public readonly static string 一输18泵_出口压力 = "10033";
        public readonly static string 一输11泵_转速 = "10084";
        public readonly static string 一输12泵_转速 = "10085";
@@ -171,7 +174,6 @@
        public readonly static string 一输14泵_频率 = "10069";
        public readonly static string 一输17泵_频率 = "10070";
        public readonly static string 一输18泵_频率 = "10071";
        public readonly static string 一输11泵_运行状态 = "10046";
        public readonly static string 一输12泵_运行状态 = "10047"; //  10150 is null
@@ -205,7 +207,14 @@
        public readonly static string 一输17泵_有功功率 = "10107";
        public readonly static string 一输18泵_有功功率 = "10108";
        public readonly static string 一输11泵_累计运行时间_历史 = "一输水泵11号变频单泵.水泵累计运行时间";
        public readonly static string 一输12泵_累计运行时间_历史 = "一输水泵12号变频单泵1.水泵运行时间";
        public readonly static string 一输13泵_累计运行时间_历史 = "一输水泵13号变频单泵.水泵累计运行时间";
        public readonly static string 一输14泵_累计运行时间_历史 = "一输水泵14号变频单泵.水泵累计运行时间";
        public readonly static string 一输15泵_累计运行时间_历史 = "HF一输水15号工频控制图.水泵累计运行时间";
        public readonly static string 一输16泵_累计运行时间_历史 = "HF一输水16号工频控制图.水泵累计运行时间";
        public readonly static string 一输17泵_累计运行时间_历史 = "一输水泵17号变频单泵.水泵累计运行时间";
        public readonly static string 一输18泵_累计运行时间_历史 = "一输水泵18号变频单泵.水泵累计运行时间";
        #endregion
@@ -229,7 +238,6 @@
        public readonly static string DN2400总管_累计流量_长江管网图 = "10162";
        public readonly static string DN2700总管_累计流量_长江管网图 = "10163";
        public readonly static string 二输21泵_泵井液位 = "10004";
        public readonly static string 二输22泵_泵井液位 = "10005";
        public readonly static string 二输23泵_泵井液位 = "10006";
@@ -237,7 +245,6 @@
        public readonly static string 二输25泵_泵井液位 = "10008";
        public readonly static string 二输26泵_泵井液位 = "10009";
        public readonly static string 二输27泵_泵井液位 = "10010";
        public readonly static string 二输21泵_出水压力 = "10019";
        public readonly static string 二输22泵_出水压力 = "10020";
@@ -247,7 +254,6 @@
        public readonly static string 二输26泵_出水压力 = "10024";
        public readonly static string 二输27泵_出水压力 = "10025";
        public readonly static string 二输21泵_瞬时流量 = "10039";
        public readonly static string 二输22泵_瞬时流量 = "10040";
        public readonly static string 二输23泵_瞬时流量 = "10041";
@@ -255,7 +261,6 @@
        public readonly static string 二输25泵_瞬时流量 = "10043";
        public readonly static string 二输26泵_瞬时流量 = "10044";
        public readonly static string 二输27泵_瞬时流量 = "10045";
        public readonly static string 二输21泵_累计流量 = "10059";
        public readonly static string 二输22泵_累计流量 = "10060";
@@ -265,7 +270,6 @@
        public readonly static string 二输26泵_累计流量 = "10064";
        public readonly static string 二输27泵_累计流量 = "10065";
        public readonly static string 二输21泵_有功电能 = "10131";
        public readonly static string 二输22泵_有功电能 = "10132";
        public readonly static string 二输23泵_有功电能 = "10133";
@@ -274,14 +278,12 @@
        public readonly static string 二输26泵_有功电能 = "10136";
        public readonly static string 二输27泵_有功电能 = "10137";
        public readonly static string 二输22泵_频率 = "10095";
        public readonly static string 二输23泵_频率 = "10096";
        public readonly static string 二输24泵_频率 = "10097";
        public readonly static string 二输25泵_频率 = "10098";
        public readonly static string 二输26泵_频率 = "10099";
        public readonly static string 二输27泵_频率 = "10100";
        public readonly static string 二输21泵_转速 = "10114";
        public readonly static string 二输22泵_转速 = "10115";
@@ -291,7 +293,6 @@
        public readonly static string 二输26泵_转速 = "10119";
        public readonly static string 二输27泵_转速 = "10120";
        public readonly static string 二输21泵_有功功率 = "10121";
        public readonly static string 二输22泵_有功功率 = "10122";
        public readonly static string 二输23泵_有功功率 = "10123";
@@ -299,7 +300,6 @@
        public readonly static string 二输25泵_有功功率 = "10125";
        public readonly static string 二输26泵_有功功率 = "10126"; // "10151"; is null
        public readonly static string 二输27泵_有功功率 = "10127";
        public readonly static string 二输21泵_运行状态 = "10077";
        public readonly static string 二输22泵_运行状态 = "10078";
@@ -309,7 +309,6 @@
        public readonly static string 二输26泵_运行状态 = "10082";
        public readonly static string 二输27泵_运行状态 = "10083";
        public readonly static string 二输21泵_运行时间 = "10180";
        public readonly static string 二输22泵_运行时间 = "10181";
        public readonly static string 二输23泵_运行时间 = "10182";
@@ -318,12 +317,27 @@
        public readonly static string 二输26泵_运行时间 = "10185";
        public readonly static string 二输27泵_运行时间 = "10186";
        public readonly static string 二输21泵_检修状态 = "10196";
        public readonly static string 二输22泵_检修状态 = "10197";
        public readonly static string 二输23泵_检修状态 = "10198";
        public readonly static string 二输24泵_检修状态 = "10199";
        public readonly static string 二输25泵_检修状态 = "10200";
        public readonly static string 二输26泵_检修状态 = "10201";
        public readonly static string 二输27泵_检修状态 = "10202";
        public readonly static string 二输21泵_累计运行时间_历史 = "二输水21号水泵运行参数.运行时间";
        public readonly static string 二输22泵_累计运行时间_历史 = "二输水22号水泵运行参数.运行时间";
        public readonly static string 二输23泵_累计运行时间_历史 = "二输水23号水泵运行参数.运行时间";
        public readonly static string 二输24泵_累计运行时间_历史 = "二输水24号水泵运行参数.运行时间";
        public readonly static string 二输25泵_累计运行时间_历史 = "二输水25号水泵运行参数.运行时间";
        public readonly static string 二输26泵_累计运行时间_历史 = "二输水26号水泵运行参数.运行时间";
        public readonly static string 二输27泵_累计运行时间_历史 = "二输水27号水泵运行参数.运行时间";
        #endregion
        #endregion
        #region 泵标志 和 众毅Scada对接标签 映射
        /// <summary>
        /// 泵标志运行状态映射字典 1输水 
@@ -366,7 +380,6 @@
            {Flag18, 一输_新前池液位}
        };
        /// <summary>
        /// 泵标志进口水位映射字典 2输水
        /// </summary>
@@ -379,8 +392,6 @@
            {Flag26, 二输26泵_泵井液位},
            {Flag27, 二输27泵_泵井液位}
        };
        /// <summary>
        /// 泵标志转速映射字典 1输水
@@ -396,7 +407,6 @@
            {Flag18, 一输18泵_转速}
        };
        /// <summary>
        /// 泵标志转速映射字典 2输水
        /// </summary>
@@ -408,6 +418,46 @@
            {Flag25, 二输25泵_转速},
            {Flag26, 二输26泵_转速},
            {Flag27, 二输27泵_转速}
        };
        /// <summary>
        /// 泵标志检修状态映射字典 2输水
        /// </summary>
        public readonly static Dictionary<int, string> FlagMaintenanceMappingDict2 = new() {
            {Flag21, 二输21泵_检修状态},
            {Flag22, 二输22泵_检修状态},
            {Flag23, 二输23泵_检修状态},
            {Flag24, 二输24泵_检修状态},
            {Flag25, 二输25泵_检修状态},
            {Flag26, 二输26泵_检修状态},
            {Flag27, 二输27泵_检修状态}
        };
        /// <summary>
        /// 泵标志累计时间映射字典 1输水 (分钟单位)
        /// </summary>
        public readonly static Dictionary<string, int> FlagCumulativeRunTimeMappingDict1 = new() {
            {一输11泵_累计运行时间_历史,Flag11},
            {一输12泵_累计运行时间_历史,Flag12},
            {一输13泵_累计运行时间_历史,Flag13},
            {一输14泵_累计运行时间_历史,Flag14},
            {一输15泵_累计运行时间_历史,Flag15},
            {一输16泵_累计运行时间_历史,Flag16},
            {一输17泵_累计运行时间_历史,Flag17},
            {一输18泵_累计运行时间_历史,Flag18}
        };
        /// <summary>
        /// 泵标志累计时间映射字典 2输水 (小时单位)
        /// </summary>
        public readonly static Dictionary<string, int> FlagCumulativeRunTimeMappingDict2 = new() {
            {二输21泵_累计运行时间_历史,Flag21},
            {二输22泵_累计运行时间_历史,Flag22},
            {二输23泵_累计运行时间_历史,Flag23},
            {二输24泵_累计运行时间_历史,Flag24},
            {二输25泵_累计运行时间_历史,Flag25},
            {二输26泵_累计运行时间_历史,Flag26},
            {二输27泵_累计运行时间_历史,Flag27}
        };
        #endregion
@@ -607,7 +657,7 @@
        #endregion
        #region 获取Scada
        #region 获取实时Scada
        /// <summary>
        /// 众毅实时数据
@@ -708,6 +758,277 @@
            return zy_scada_list;
        }
        /// <summary>
        /// 获取实时众毅Scada数据列表
        /// </summary>
        public static bool ExportTempZyScadaDebugTxt(List<Model.ZyScada> zy_scada_list)
        {
            if (zy_scada_list == null || !zy_scada_list.Any())
            {
                return false;
            }
            var debug_zy_scada_file = Settings.ParasHelper.LocalFile.DataFolderDirectory + "\\" + "zy_scada_debug.txt";
            if (File.Exists(debug_zy_scada_file))
            {
                File.Delete(debug_zy_scada_file);
            }
            var temp_wl_dict = new Dictionary<string, string>() {
                {"R3", 一输_老前池南侧液位},
                {"R2", 一输_老前池北侧液位 },
                {"R1", 一输_新前池液位},
                {"RPump21", 二输21泵_泵井液位},
                {"RPump22", 二输22泵_泵井液位},
                {"RPump23", 二输23泵_泵井液位},
                {"RPump24", 二输24泵_泵井液位},
                {"RPump25", 二输25泵_泵井液位},
                {"RPump26", 二输26泵_泵井液位},
                {"RPump27", 二输27泵_泵井液位}
            };
            var temp_pump_dict = new Dictionary<string, string>() {
                {"Pump11", 一输11泵_运行状态},
                {"Pump12", 一输12泵_运行状态},
                {"Pump13", 一输13泵_运行状态},
                {"Pump14", 一输14泵_运行状态},
                {"Pump15", 一输15泵_运行状态},
                {"Pump16", 一输16泵_运行状态},
                {"Pump17", 一输17泵_运行状态},
                {"Pump18", 一输18泵_运行状态},
                {"R3", 一输_老前池南侧液位},
                {"R2", 一输_老前池北侧液位 },
                {"R1", 一输_新前池液位},
                {"RPump21", 二输21泵_泵井液位},
                {"RPump22", 二输22泵_泵井液位},
                {"RPump23", 二输23泵_泵井液位},
                {"RPump24", 二输24泵_泵井液位},
                {"RPump25", 二输25泵_泵井液位},
                {"RPump26", 二输26泵_泵井液位},
                {"RPump27", 二输27泵_泵井液位},
                {"Pump21", 二输21泵_运行状态},
                {"Pump22", 二输22泵_运行状态},
                {"Pump23", 二输23泵_运行状态},
                {"Pump24", 二输24泵_运行状态},
                {"Pump25", 二输25泵_运行状态},
                {"Pump26", 二输26泵_运行状态},
                {"Pump27", 二输27泵_运行状态}
            };
            var data = new Dictionary<string, Dictionary<string, string>>();
            foreach (var zy_scada in zy_scada_list)
            {
                var code = zy_scada.Code;
                var tag = zy_scada.Tag;
                var time = zy_scada.Time.ToString("u");
                var vals = zy_scada.Value.ToString() ?? "";
                if (string.IsNullOrEmpty(code))
                {
                    if (temp_wl_dict.ContainsKey(tag))
                    {
                        code = temp_wl_dict[tag];
                    }
                    else if (temp_pump_dict.ContainsKey(tag))
                    {
                        code = temp_pump_dict[tag];
                        vals = zy_scada.Value > 0 ? "1" : "0";
                    }
                }
                if (string.IsNullOrEmpty(code))
                    continue;
                data.Add(code, new Dictionary<string, string>());
                data[code]["key"] = tag;
                data[code]["time"] = time;
                data[code]["vals"] = vals;
            }
            var debug_zy_scada_dto = new ZyRealTimeDataDto();
            debug_zy_scada_dto.data = data;
            debug_zy_scada_dto.msg = "测试文件";
            var dto_json = JsonHelper.Object2Json(debug_zy_scada_dto);
            File.WriteAllText(debug_zy_scada_file, dto_json);
            return true;
        }
        #endregion
        #region 获取历史Scada
        /// <summary>
        /// 众毅历史数据
        /// </summary>
        public class ZyHistoryDataDto
        {
            /// <summary>
            ///
            /// </summary>
            public string msg { get; set; }
            /// <summary>
            ///
            /// </summary>
            public List<ZyDataDto> data { get; set; }
            /// <summary>
            ///
            /// </summary>
            public string status { get; set; }
        }
        /// <summary>
        /// 众毅历史数据 data
        /// </summary>
        public class ZyDataDto
        {
            /// <summary>
            ///
            /// </summary>
            public string dataMsg { get; set; }
            /// <summary>
            ///
            /// </summary>
            public List<string[]> vals { get; set; }
            /// <summary>
            ///
            /// </summary>
            public string tagName { get; set; }
        }
        /// <summary>
        /// 获取泵累计运行时间(小时)字典
        /// </summary>
        /// <param name="station1_flag_cumulative_run_time_dict"></param>
        /// <param name="station2_flag_cumulative_run_time_dict"></param>
        /// <param name="date_interval">统计日期间隔(天)</param>
        /// <param name="frequency">数据步长 1h(s:秒 m:分 h:小时 d:天)</param>
        /// <param name="type">数据值类型 first:每个间隔的第一个值  mean:每个间隔的平均值</param>
        /// <returns></returns>
        public static string GetFlagCumulativeRunTimeDict(out Dictionary<int, double> station1_flag_cumulative_run_time_dict, out Dictionary<int, double> station2_flag_cumulative_run_time_dict, int date_interval = 30, string frequency = "1h", string type = "first")
        {
            station1_flag_cumulative_run_time_dict = new Dictionary<int, double>();
            station2_flag_cumulative_run_time_dict = new Dictionary<int, double>();
            if (!Settings.ParasHelper.ZyDocking.Enable)
            {
                return "ZyDocking 未启用";
            }
            var url_prefix = Settings.ParasHelper.ZyDocking.HistoryScadaHttpUrl;
            if (string.IsNullOrEmpty(url_prefix))
            {
                return "HistoryScadaHttpUrl 为空";
            }
            var enter_time_format = "yyyy-MM-dd HH:mm:ss";
            var travel_time_format = new System.Globalization.DateTimeFormatInfo()
            {
                ShortDatePattern = "yyyy-MM-dd'T'HH:mm:ss.SSS Z"
            }; ;
            var now_dt = DateTime.Now;
            var start_dt = now_dt.AddDays(-date_interval);
            var end_dt = now_dt;
            var start_time = start_dt.ToString(enter_time_format);
            var end_time = end_dt.ToString(enter_time_format);
            var tag_list = new List<string>();
            tag_list.AddRange(FlagCumulativeRunTimeMappingDict1.Keys);
            tag_list.AddRange(FlagCumulativeRunTimeMappingDict2.Keys);
            var measurement_max_count = 5;
            var measurements_list = new List<string>();
            for (int i = 0; i < tag_list.Count; i += measurement_max_count)
            {
                var tag_list_temp = tag_list.Skip(i).Take(measurement_max_count).ToList();
                var measurements = Yw.Untity.StringListHelper.ToString(tag_list_temp);
                measurements_list.Add(measurements);
            }
            var log_str_builder = new StringBuilder();
            var zy_scada_list_dict = new Dictionary<string, List<Model.ZyScada>>();
            try
            {
                foreach (var measurements in measurements_list)
                {
                    var url = $"{url_prefix}?" +
                    $"startTime={start_time}&" +
                    $"endTime={end_time}&" +
                    $"type={type}&" +
                    $"frequency={frequency}&" +
                    $"measurements={measurements}";
                    log_str_builder.AppendLine($"历史数据请求:{url}");
                    var response_text = Yw.Untity.HttpRequestHelper.Get(url);
                    var dto = JsonHelper.Json2Object<ZyHistoryDataDto>(response_text);
                    var zy_dada_dto_list = dto?.data;
                    if (zy_dada_dto_list == null || !zy_dada_dto_list.Any())
                    {
                        log_str_builder.AppendLine("无数据");
                        continue;
                    }
                    foreach (var zy_dada_dto in zy_dada_dto_list)
                    {
                        var tag = zy_dada_dto.tagName;
                        var vlus = zy_dada_dto.vals;
                        var zy_scada_list = new List<Model.ZyScada>();
                        foreach (var vlu in vlus)
                        {
                            var time_str = vlu[0];
                            var value_str = vlu[1];
                            if (!double.TryParse(value_str, out double value))
                                continue;
                            var time = Convert.ToDateTime(time_str, travel_time_format).AddHours(-8);
                            var zy_scada = new Model.ZyScada();
                            zy_scada.Time = time;
                            zy_scada.Code = tag;
                            zy_scada.Value = value;
                            zy_scada.Tag = tag;
                            zy_scada_list.Add(zy_scada);
                        }
                        zy_scada_list_dict.Add(tag, zy_scada_list);
                    }
                }
            }
            catch (System.Exception ex)
            {
                log_str_builder.AppendLine(ex.Message);
            }
            foreach (var item in zy_scada_list_dict)
            {
                var tag = item.Key;
                var zy_scada_list = item.Value;
                if (zy_scada_list == null || !zy_scada_list.Any())
                {
                    continue;
                }
                var min_time = zy_scada_list.Min(x => x.Value);
                var max_time = zy_scada_list.Max(x => x.Value);
                var run_time = max_time - min_time ?? 0;
                if (FlagCumulativeRunTimeMappingDict1.ContainsKey(tag))
                {
                    var flag = FlagCumulativeRunTimeMappingDict1[tag];
                    var time = Math.Round(run_time / 60, 1);
                    station1_flag_cumulative_run_time_dict.Add(flag, time);
                }
                else if (FlagCumulativeRunTimeMappingDict2.ContainsKey(tag))
                {
                    var flag = FlagCumulativeRunTimeMappingDict2[tag];
                    var time = Math.Round(run_time, 1);
                    station2_flag_cumulative_run_time_dict.Add(flag, time);
                }
            }
            return log_str_builder.ToString();
        }
        #endregion
        #region 获取调度算法入参
@@ -746,7 +1067,6 @@
                }
            }
        }
        /// <summary>
        /// 获取泵进口水位字典
@@ -790,11 +1110,36 @@
            }
        }
        /// <summary>
        /// 获取泵站检修标志列表(0是正常,1是检修)
        /// </summary>
        /// <param name="zy_scada_list">众毅Scada列表</param>
        /// <param name="station1_maintenance_flag_list">检修标志列表 1输水</param>
        /// <param name="station2_maintenance_flag_list">检修标志列表 2输水</param>
        public static void GetStationMaintenanceFlagList(List<Model.ZyScada> zy_scada_list, out List<int> station1_maintenance_flag_list, out List<int> station2_maintenance_flag_list)
        {
            station1_maintenance_flag_list = new List<int>();
            station2_maintenance_flag_list = new List<int>();
            if (zy_scada_list == null || !zy_scada_list.Any())
                return;
            //1输水待补充
            foreach (var mapping in FlagMaintenanceMappingDict2)
            {
                var flag = mapping.Key;
                var code = mapping.Value;
                var zy_scada = zy_scada_list.Find(x => x.Code == code);
                if (zy_scada != null && zy_scada.Value == 1)
                {
                    station2_maintenance_flag_list.Add(flag);
                }
            }
        }
        #endregion
    }
}