GopTimeCalc.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using Pas.ScadaService.Model;
  2. using Spring.Expressions;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. using System;
  7. using System.Collections.Concurrent;
  8. using NewLife.Serialization;
  9. namespace Pas.ScadaService.Core
  10. {
  11. public class GopTimeCalc
  12. {
  13. /// <summary>
  14. /// 点位关联
  15. /// </summary>
  16. private static readonly ConcurrentDictionary<string, List<ConditionDto>> ChangeDic = new ConcurrentDictionary<string, List<ConditionDto>>();
  17. /// <summary>
  18. /// 所有时间
  19. /// </summary>
  20. private static readonly ConcurrentDictionary<string, Data> AllData = new ConcurrentDictionary<string, Model.Data>();
  21. private static IFreeSql Db { get; set; }
  22. /// <summary>
  23. /// 加载配置
  24. /// </summary>
  25. public static void Load(string config, string sql, List<Data> obj)
  26. {
  27. var inputObject = System.IO.File.ReadAllText(config);
  28. var conditionDtoList = inputObject.ToJsonEntity<List<ConditionDto>>();
  29. foreach (var item in conditionDtoList)
  30. {
  31. if (ChangeDic.TryGetValue(HandleParam(item.Change.Replace("@(", "").Replace(")", "")), out var value))
  32. {
  33. value.Add(item);
  34. }
  35. else
  36. {
  37. ChangeDic.TryAdd(HandleParam(item.Change.Replace("@(", "").Replace(")", "")), new List<ConditionDto> { item });
  38. }
  39. }
  40. //初始化数据库
  41. Db = new FreeSql.FreeSqlBuilder()
  42. .UseConnectionString(FreeSql.DataType.SqlServer, sql)
  43. .Build();
  44. //初始化数据
  45. InitData(obj);
  46. }
  47. /// <summary>
  48. /// 初始化数据事件
  49. /// </summary>
  50. /// <param name="obj"></param>
  51. private static void InitData(List<Data> obj)
  52. {
  53. foreach (var data in obj)
  54. {
  55. var key = HandleParam(data.ItemId);
  56. if (!AllData.ContainsKey(key))
  57. {
  58. AllData.TryAdd(key, data);
  59. }
  60. }
  61. SyncDb(obj);
  62. }
  63. /// <summary>
  64. /// 监听事件
  65. /// </summary>
  66. /// <param name="obj"></param>
  67. public static void ChangeData(List<Data> obj)
  68. {
  69. foreach (var data in obj)
  70. {
  71. var key = HandleParam(data.ItemId);
  72. if (AllData.ContainsKey(key))
  73. {
  74. AllData[key] = data;
  75. }
  76. }
  77. SyncDb(obj);
  78. }
  79. /// <summary>
  80. /// 计算点位
  81. /// </summary>
  82. /// <param name="obj"></param>
  83. public static List<CustomLive> CalcPoints(List<Data> obj)
  84. {
  85. var result = new List<CustomLive>();
  86. var resultValue = AllData
  87. .ToDictionary(data => data.Key,
  88. data => GetValue(data.Value.Value.ToString(), data.Value.DataType));
  89. foreach (var data in obj)
  90. {
  91. if (!ChangeDic.TryGetValue(HandleParam(data.ItemId), out var conditionDtoList)) continue;
  92. foreach (var conditionDto in conditionDtoList)
  93. {
  94. var conditionValue = conditionDto.Value;
  95. var conditionParams = GetConditionParams(conditionValue);
  96. var tagNames = new List<string>();
  97. //修正表达式
  98. foreach (var conditionParam in conditionParams)
  99. {
  100. var replace = HandleParam(conditionParam
  101. .Replace("@(", "#")
  102. .Replace(")", ""));
  103. tagNames.Add(replace.Substring(1));
  104. conditionValue = conditionValue.Replace(conditionParam, replace);
  105. }
  106. var value = (bool)ExpressionEvaluator.GetValue(null, conditionValue, resultValue);
  107. if (!value) continue;
  108. var time = AllData.Where(c => tagNames.Contains(c.Key))
  109. .Select(c => HandleTime(c.Value.TimeStamp))
  110. .OrderByDescending(DateTime.Parse)
  111. .FirstOrDefault();
  112. result.Add(new CustomLive
  113. {
  114. CreateTime = DateTime.Now,
  115. GroupName = "",
  116. ProduceBatch = "批次未知",
  117. TagName = conditionDto.Name,
  118. Value = time,
  119. Visible = true
  120. });
  121. }
  122. }
  123. return result;
  124. }
  125. private static object GetValue(string value, string type)
  126. {
  127. switch (type.ToLower())
  128. {
  129. case "string":
  130. return value;
  131. case "bool":
  132. return value=="1" || value.ToLower()== "true";
  133. case "int":
  134. return value.ToInt();
  135. default:
  136. return value.ToDecimal();
  137. }
  138. }
  139. /// <summary>
  140. /// 处理参数
  141. /// </summary>
  142. /// <param name="param"></param>
  143. /// <returns></returns>
  144. private static string HandleParam(string param)
  145. {
  146. return param.Replace(" ", "__").Replace(".", "___");
  147. }
  148. private static string HandleTime(string time)
  149. {
  150. var last = time.Split(':').Last();
  151. var lastLength = last.Length + 1;
  152. return time.Substring(0, time.Length - lastLength) + "." + last;
  153. }
  154. /// <summary>
  155. /// 解析参数
  156. /// </summary>
  157. /// <param name="condition"></param>
  158. /// <returns></returns>
  159. private static List<string> GetConditionParams(string condition)
  160. {
  161. var pattern = @"\@\((.*?)\)";
  162. var regex = new Regex(pattern);
  163. var matches = regex.Matches(condition);
  164. return (from Match match in matches select match.Groups[0].Value).ToList();
  165. }
  166. /// <summary>
  167. /// 同步到数据库
  168. /// </summary>
  169. /// <param name="obj"></param>
  170. private static void SyncDb(List<Model.Data> obj)
  171. {
  172. var customLives = CalcPoints(obj);
  173. foreach (var item in customLives)
  174. {
  175. if (Db.Queryable<CustomLive>()
  176. .Where(c => c.TagName == item.TagName && c.ProduceBatch == "批次未知")
  177. .Any())
  178. {
  179. Db.Update<CustomLive>()
  180. .Set(a => a.Value, item.Value)
  181. .Set(a => a.CreateTime, DateTime.Now)
  182. .Where(a => a.TagName == item.TagName)
  183. .Where(a => a.ProduceBatch == "批次未知")
  184. .ExecuteAffrows();
  185. }
  186. else
  187. {
  188. Db.Insert(item).ExecuteAffrows();
  189. }
  190. }
  191. }
  192. }
  193. }