终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

337 lines
13 KiB

  1. using BPASmart.Model;
  2. using BPASmartClient.Compiler;
  3. using BPASmartClient.DATABUS;
  4. using BPASmartClient.MessageName.EnumHelp;
  5. using BPASmartClient.SCADAControl;
  6. using Newtonsoft.Json;
  7. using StackExchange.Redis;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Controls;
  16. using System.Windows.Data;
  17. using System.Windows.Documents;
  18. using System.Windows.Input;
  19. using System.Windows.Media;
  20. using System.Windows.Media.Imaging;
  21. using System.Windows.Navigation;
  22. using System.Windows.Shapes;
  23. using System.Windows.Threading;
  24. namespace BPASmartClient.SCADAControl.CustomerControls
  25. {
  26. /// <summary>
  27. /// TheRedis.xaml 的交互逻辑
  28. /// </summary>
  29. public partial class TheRedis :UserControl, IExecutable, IDisposable
  30. {
  31. Image imageZC = null;
  32. Image imageYC = null;
  33. Image imageGZ = null;
  34. TextBlock textBlock = null;
  35. FRedisClient fRedisClient = new FRedisClient();
  36. public TheRedis()
  37. {
  38. InitializeComponent();
  39. Width = 40;
  40. Height = 40;
  41. this.SizeChanged += TheRedis_SizeChanged;
  42. }
  43. private void TheRedis_SizeChanged(object sender,SizeChangedEventArgs e)
  44. {
  45. if (textBlock == null)
  46. {
  47. foreach (Image tb in FindVisualChildren<Image>(this))
  48. {
  49. if (tb.Tag != null)
  50. {
  51. if (tb.Tag.ToString() == "正常")
  52. {
  53. imageZC = tb;
  54. }
  55. if (tb.Tag.ToString() == "运行")
  56. {
  57. imageYC = tb;
  58. }
  59. if (tb.Tag.ToString() == "故障")
  60. {
  61. imageGZ = tb;
  62. }
  63. }
  64. }
  65. foreach (TextBlock tb in FindVisualChildren<TextBlock>(this))
  66. {
  67. if (tb.Tag != null)
  68. {
  69. if (tb.Tag.ToString() == "显示文字")
  70. {
  71. textBlock = tb;
  72. }
  73. }
  74. }
  75. }
  76. }
  77. public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
  78. {
  79. if (depObj != null)
  80. {
  81. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  82. {
  83. DependencyObject child = VisualTreeHelper.GetChild(depObj,i);
  84. if (child != null && child is T)
  85. {
  86. yield return (T)child;
  87. }
  88. foreach (T childOfChild in FindVisualChildren<T>(child))
  89. {
  90. yield return childOfChild;
  91. }
  92. }
  93. }
  94. }
  95. public string ControlType => "控件";
  96. private bool isExecuteState;
  97. public bool IsExecuteState
  98. {
  99. get { return isExecuteState; }
  100. set
  101. {
  102. isExecuteState = value;
  103. if (IsExecuteState)
  104. {
  105. Register();
  106. //Style = null;
  107. }
  108. }
  109. }
  110. DispatcherTimer timer = new DispatcherTimer();
  111. public void Register()
  112. {
  113. if (Direction == 0)
  114. {
  115. try
  116. {
  117. if (!string.IsNullOrEmpty(DataSouceInformation))
  118. {
  119. fRedisClient.Connect(DataSouceInformation);
  120. }
  121. }
  122. catch (Exception ex)
  123. {
  124. Direction = 2;
  125. }
  126. }
  127. timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
  128. timer.Tick += Timer_Tick;
  129. timer.Start();
  130. }
  131. Task<bool> isSuccess;
  132. private async void Timer_Tick(object sender,EventArgs e)
  133. {
  134. Config.GetInstance().RunJsScipt(TikcExecute);
  135. if (fRedisClient._connection != null && fRedisClient._connection.IsConnected)
  136. {
  137. Direction = 1;
  138. }
  139. else
  140. {
  141. Direction = 0;
  142. try
  143. {
  144. if (!string.IsNullOrEmpty(DataSouceInformation))
  145. fRedisClient.Connect(DataSouceInformation);
  146. }
  147. catch (Exception ex)
  148. {
  149. Direction = 2;
  150. }
  151. }
  152. if (Direction == 1) //定时读取数据
  153. {
  154. try
  155. {
  156. if (!string.IsNullOrEmpty(DeviceName))
  157. {
  158. RedisValue obj = fRedisClient.RedisGet(DeviceName);
  159. List<DeviceDataModel> str = JsonConvert.DeserializeObject<List<DeviceDataModel>>(obj.ToString());
  160. Dictionary<string,DeviceDataModel> keys = new Dictionary<string,DeviceDataModel>();
  161. str?.ForEach(par =>
  162. {
  163. keys[par.VarName] = par;
  164. });
  165. Class_DataBus.GetInstance().Dic_DeviceData[DeviceName] = keys;
  166. if (PropertyChange != null) PropertyChange(this,null);
  167. }
  168. else
  169. {
  170. fRedisClient.GetKeys()?.ToList().ForEach(par => {
  171. List<DeviceDataModel> str = JsonConvert.DeserializeObject<List<DeviceDataModel>>(par.Value);
  172. Dictionary<string,DeviceDataModel> keys = new Dictionary<string,DeviceDataModel>();
  173. str?.ForEach(par =>
  174. {
  175. keys[par.VarName] = par;
  176. });
  177. Class_DataBus.GetInstance().Dic_DeviceData[par.Key] = keys;
  178. });
  179. if (PropertyChange != null) PropertyChange(this,null);
  180. }
  181. }
  182. catch (Exception ex)
  183. {
  184. }
  185. }
  186. }
  187. public void Start() => timer.Start();
  188. public void Stop() => timer.Stop();
  189. public void Dispose()
  190. {
  191. timer.Stop();
  192. }
  193. #region 属性
  194. [Category("值设定")]
  195. public int Direction
  196. {
  197. get { return (int)GetValue(DirectionProperty); }
  198. set { SetValue(DirectionProperty,value); }
  199. }
  200. public static readonly DependencyProperty DirectionProperty =
  201. DependencyProperty.Register("Direction",typeof(int),typeof(TheRedis),
  202. new PropertyMetadata(0,new PropertyChangedCallback(OnPropertyChanged)));
  203. private static void OnPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
  204. {
  205. (d as TheRedis)?.Refresh();
  206. }
  207. public void Refresh()
  208. {
  209. if (textBlock != null)
  210. {
  211. if (Direction == 1)
  212. {
  213. imageZC.Visibility = Visibility.Collapsed;
  214. imageYC.Visibility = Visibility.Visible;
  215. imageGZ.Visibility = Visibility.Collapsed;
  216. textBlock.Visibility = Visibility.Visible;
  217. textBlock.Text = "运行中";
  218. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF2077EC"));
  219. }
  220. else if (Direction == 2)
  221. {
  222. imageZC.Visibility = Visibility.Collapsed;
  223. imageYC.Visibility = Visibility.Collapsed;
  224. imageGZ.Visibility = Visibility.Visible;
  225. textBlock.Visibility = Visibility.Visible;
  226. textBlock.Text = "故障";
  227. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FFFF0202"));
  228. }
  229. else
  230. {
  231. imageZC.Visibility = Visibility.Visible;
  232. imageYC.Visibility = Visibility.Collapsed;
  233. imageGZ.Visibility = Visibility.Collapsed;
  234. textBlock.Visibility = Visibility.Visible;
  235. textBlock.Text = "未运行";
  236. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF02F9FF"));
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// 执行内容
  242. /// </summary>
  243. [Category("事件")]
  244. public string TikcExecute
  245. {
  246. get { return (string)GetValue(TikcExecuteProperty); }
  247. set { SetValue(TikcExecuteProperty,value); }
  248. }
  249. public static readonly DependencyProperty TikcExecuteProperty =
  250. DependencyProperty.Register("TikcExecute",typeof(string),typeof(TheRedis),new PropertyMetadata(string.Empty));
  251. #endregion
  252. #region 数据绑定模块
  253. public event EventHandler PropertyChange; //声明一个事件
  254. [Category("数据绑定-数据来源")]
  255. public int TimeCount
  256. {
  257. get { return (int)GetValue(TimeCountProperty); }
  258. set { SetValue(TimeCountProperty,value); }
  259. }
  260. public static readonly DependencyProperty TimeCountProperty =
  261. DependencyProperty.Register("TimeCount",typeof(int),typeof(TheRedis),new PropertyMetadata(100));
  262. [Category("数据绑定-数据来源")]
  263. public string DataSouceInformation
  264. {
  265. get { return (string)GetValue(DataSouceInformationProperty); }
  266. set { SetValue(DataSouceInformationProperty,value); }
  267. }
  268. public static readonly DependencyProperty DataSouceInformationProperty =
  269. DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheRedis),new PropertyMetadata("124.222.238.75:16000,password=123456,defaultDatabase=1"));
  270. [Category("数据绑定-数据来源")]
  271. public string DeviceName
  272. {
  273. get { return (string)GetValue(DeviceNameProperty); }
  274. set { SetValue(DeviceNameProperty,value); }
  275. }
  276. public static readonly DependencyProperty DeviceNameProperty =
  277. DependencyProperty.Register("DeviceName",typeof(string),typeof(TheRedis),new PropertyMetadata(string.Empty));
  278. [Category("数据绑定")]
  279. public string FDataSouce
  280. {
  281. get { return (string)GetValue(FDataSouceProperty); }
  282. set { SetValue(FDataSouceProperty,value); }
  283. }
  284. public static readonly DependencyProperty FDataSouceProperty =
  285. DependencyProperty.Register("FDataSouce",typeof(string),typeof(TheRedis),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onFDataSouceChanged)));
  286. private static void onFDataSouceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheRedis)?.DataSouceRefresh();
  287. public void DataSouceRefresh()
  288. {
  289. try
  290. {
  291. if (!string.IsNullOrEmpty(FDataSouce))
  292. {
  293. GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code,new object[] { FDataSouce });
  294. }
  295. }
  296. catch (Exception ex)
  297. {
  298. }
  299. }
  300. public static string _code = "public string main(string message) \n{ \n //请在此填写你的代码\n\n return message; \n}\n";
  301. [Category("数据绑定")]
  302. public string Code
  303. {
  304. get { return (string)GetValue(CodeProperty); }
  305. set { SetValue(CodeProperty,value); }
  306. }
  307. public static readonly DependencyProperty CodeProperty =
  308. DependencyProperty.Register("Code",typeof(string),typeof(TheRedis),new PropertyMetadata(_code));
  309. [Category("数据绑定")]
  310. public string GenerateData
  311. {
  312. get { return (string)GetValue(GenerateDataProperty); }
  313. set { SetValue(GenerateDataProperty,value); }
  314. }
  315. public static readonly DependencyProperty GenerateDataProperty =
  316. DependencyProperty.Register("GenerateData",typeof(string),typeof(TheRedis),new PropertyMetadata(string.Empty));
  317. #endregion
  318. }
  319. }