终端一体化运控平台
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TheRedis.xaml.cs 18 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. using BPASmartClient.Compiler;
  2. using Newtonsoft.Json;
  3. using StackExchange.Redis;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Linq;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using System.Windows.Media;
  11. using System.Windows.Threading;
  12. namespace BPASmartClient.SCADAControl.CustomerControls
  13. {
  14. /// <summary>
  15. /// TheRedis.xaml 的交互逻辑
  16. /// </summary>
  17. public partial class TheRedis : UserControl, IExecutable, IDisposable
  18. {
  19. Image imageZC = null;
  20. Image imageYC = null;
  21. Image imageGZ = null;
  22. TextBlock textBlock = null;
  23. FRedisClient fRedisClient = new FRedisClient();
  24. public TheRedis()
  25. {
  26. InitializeComponent();
  27. Width = 40;
  28. Height = 40;
  29. this.SizeChanged += TheRedis_SizeChanged;
  30. }
  31. private void TheRedis_SizeChanged(object sender, SizeChangedEventArgs e)
  32. {
  33. if (textBlock == null)
  34. {
  35. foreach (Image tb in FindVisualChildren<Image>(this))
  36. {
  37. if (tb.Tag != null)
  38. {
  39. if (tb.Tag.ToString() == "正常")
  40. {
  41. imageZC = tb;
  42. }
  43. if (tb.Tag.ToString() == "运行")
  44. {
  45. imageYC = tb;
  46. }
  47. if (tb.Tag.ToString() == "故障")
  48. {
  49. imageGZ = tb;
  50. }
  51. }
  52. }
  53. foreach (TextBlock tb in FindVisualChildren<TextBlock>(this))
  54. {
  55. if (tb.Tag != null)
  56. {
  57. if (tb.Tag.ToString() == "显示文字")
  58. {
  59. textBlock = tb;
  60. }
  61. }
  62. }
  63. }
  64. }
  65. public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
  66. {
  67. if (depObj != null)
  68. {
  69. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
  70. {
  71. DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
  72. if (child != null && child is T)
  73. {
  74. yield return (T)child;
  75. }
  76. foreach (T childOfChild in FindVisualChildren<T>(child))
  77. {
  78. yield return childOfChild;
  79. }
  80. }
  81. }
  82. }
  83. public string ControlType => "控件";
  84. private bool isExecuteState;
  85. public bool IsExecuteState
  86. {
  87. get { return isExecuteState; }
  88. set
  89. {
  90. isExecuteState = value;
  91. if (IsExecuteState)
  92. {
  93. Register();
  94. }
  95. }
  96. }
  97. DispatcherTimer timer = new DispatcherTimer();
  98. public void Register()
  99. {
  100. if (Direction == 0)
  101. {
  102. try
  103. {
  104. if (!string.IsNullOrEmpty(DataSouceInformation))
  105. {
  106. fRedisClient.Connect(DataSouceInformation);
  107. }
  108. }
  109. catch (Exception ex)
  110. {
  111. Direction = 2;
  112. }
  113. }
  114. timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
  115. timer.Tick += Timer_Tick;
  116. timer.Start();
  117. }
  118. private async void Timer_Tick(object sender, EventArgs e)
  119. {
  120. Config.GetInstance().RunJsScipt(TikcExecute);
  121. if (TestData)
  122. {
  123. keyValuePairs1.Clear();
  124. for (int i = 0; i < 20; i++)
  125. {
  126. Random ran = new Random();
  127. int n = ran.Next(0, 100);
  128. keyValuePairs1[i.ToString()] = "10." + n;
  129. keyValuePairs1["Left"+i.ToString()] = new Random().Next(0,10)>5?true: false;
  130. keyValuePairs1["Right"+i.ToString()] = new Random().Next(0, 10) > 5 ? true : false;
  131. }
  132. Class_DataBus.GetInstance().Dic_RedisData["Test"] = keyValuePairs1;
  133. Class_DataBus.GetInstance().Dic_RedisDataBinding = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(Class_DataBus.GetInstance().Dic_RedisData));
  134. Class_DataBus.GetInstance().refreshDataAction("Test");
  135. }
  136. else
  137. {
  138. if (fRedisClient._connection != null && fRedisClient._connection.IsConnected)
  139. {
  140. Direction = 1;
  141. }
  142. else
  143. {
  144. Direction = 0;
  145. try
  146. {
  147. if (!string.IsNullOrEmpty(DataSouceInformation))
  148. fRedisClient.Connect(DataSouceInformation);
  149. }
  150. catch (Exception ex)
  151. {
  152. Direction = 2;
  153. }
  154. }
  155. if (Direction == 1) //定时读取数据
  156. {
  157. try
  158. {
  159. if (!string.IsNullOrEmpty(DeviceName))
  160. {
  161. RedisValue obj = fRedisClient.RedisGet(DeviceName);
  162. List<ReeisDataModel> str = JsonConvert.DeserializeObject<List<ReeisDataModel>>(obj.ToString());
  163. Dictionary<string, object> keyValues = new Dictionary<string, object>();
  164. Dictionary<string, string> keyValuesType = new Dictionary<string, string>();
  165. str?.ForEach(jon =>
  166. {
  167. keyValues[jon.VarName] = GetValuesType(jon.DataType, jon.VarVaule);
  168. keyValuesType[jon.VarName] = jon.DataType.ToString();
  169. });
  170. Class_DataBus.GetInstance().Dic_RedisData[DeviceName] = keyValues;
  171. Class_DataBus.GetInstance().Dic_RedisDataBinding = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(Class_DataBus.GetInstance().Dic_RedisData));
  172. Class_DataBus.GetInstance().Dic_RedisDataType[DeviceName] = keyValuesType;
  173. Class_DataBus.GetInstance().refreshDataAction(DeviceName);
  174. if (PropertyChange != null) PropertyChange(this, null);
  175. }
  176. else
  177. {
  178. fRedisClient.GetKeys()?.ToList().ForEach(par =>
  179. {
  180. //所有变量集合
  181. //PublishInfo
  182. List<ReeisDataModel> str = JsonConvert.DeserializeObject<List<ReeisDataModel>>(par.Value);
  183. Dictionary<string, object> keyValues = new Dictionary<string, object>();
  184. Dictionary<string, string> keyValuesType = new Dictionary<string, string>();
  185. str?.ForEach(jon =>
  186. {
  187. keyValues[jon.VarName] = GetValuesType(jon.DataType, jon.VarVaule);
  188. keyValuesType[jon.VarName] = jon.DataType.ToString();
  189. });
  190. Class_DataBus.GetInstance().Dic_RedisData[par.Key] = keyValues;
  191. Class_DataBus.GetInstance().Dic_RedisDataBinding = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(Class_DataBus.GetInstance().Dic_RedisData));
  192. Class_DataBus.GetInstance().Dic_RedisDataType[par.Key] = keyValuesType;
  193. Class_DataBus.GetInstance().refreshDataAction(par.Key);
  194. });
  195. if (PropertyChange != null) PropertyChange(this, null);
  196. }
  197. }
  198. catch (Exception ex)
  199. {
  200. }
  201. }
  202. }
  203. }
  204. /// <summary>
  205. /// 根据变量获取变量实际值
  206. /// </summary>
  207. /// <param name="eData"></param>
  208. /// <param name="Value"></param>
  209. /// <returns></returns>
  210. public object GetValuesType(EDataType eData, string Value)
  211. {
  212. try
  213. {
  214. if (!string.IsNullOrEmpty(Value))
  215. {
  216. switch (eData)
  217. {
  218. case EDataType.Bool:
  219. return Convert.ToBoolean(Value);
  220. break;
  221. case EDataType.Byte:
  222. return Convert.ToByte(Value);
  223. break;
  224. case EDataType.Int:
  225. return Convert.ToInt16(Value);
  226. break;
  227. case EDataType.Word:
  228. return Convert.ToUInt16(Value);
  229. break;
  230. case EDataType.Dint:
  231. return Convert.ToInt32(Value);
  232. break;
  233. case EDataType.Dword:
  234. return Convert.ToUInt32(Value);
  235. break;
  236. case EDataType.Float:
  237. return Convert.ToSingle(Value);
  238. break;
  239. default:
  240. break;
  241. }
  242. }
  243. return null;
  244. }
  245. catch (Exception ex)
  246. {
  247. return null;
  248. }
  249. }
  250. public void Start() => timer.Start();
  251. public void Stop() => timer.Stop();
  252. public void Dispose()
  253. {
  254. timer.Stop();
  255. Direction = 0;
  256. FDataSouce = "";
  257. if (IsRun) IsRun = false;
  258. }
  259. #region 属性
  260. public bool TestData
  261. {
  262. get { return (bool)GetValue(TestDataProperty); }
  263. set { SetValue(TestDataProperty, value); }
  264. }
  265. public static readonly DependencyProperty TestDataProperty =
  266. DependencyProperty.Register("TestData", typeof(bool), typeof(TheRedis),
  267. new PropertyMetadata(false, new PropertyChangedCallback(OnTestDataPropertyChanged)));
  268. private static void OnTestDataPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  269. {
  270. (d as TheRedis)?.RefreshTest();
  271. }
  272. #region MyRegion
  273. Dictionary<string, object> keyValuePairs1 = new Dictionary<string, object>();
  274. #endregion
  275. public void RefreshTest()
  276. {
  277. }
  278. [Category("值设定")]
  279. public int Direction
  280. {
  281. get { return (int)GetValue(DirectionProperty); }
  282. set { SetValue(DirectionProperty, value); }
  283. }
  284. public static readonly DependencyProperty DirectionProperty =
  285. DependencyProperty.Register("Direction", typeof(int), typeof(TheRedis),
  286. new PropertyMetadata(0, new PropertyChangedCallback(OnPropertyChanged)));
  287. private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  288. {
  289. (d as TheRedis)?.Refresh();
  290. }
  291. public void Refresh()
  292. {
  293. if (textBlock != null)
  294. {
  295. if (Direction == 1)
  296. {
  297. imageZC.Visibility = Visibility.Collapsed;
  298. imageYC.Visibility = Visibility.Visible;
  299. imageGZ.Visibility = Visibility.Collapsed;
  300. textBlock.Visibility = Visibility.Visible;
  301. textBlock.Text = "运行中";
  302. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF2077EC"));
  303. }
  304. else if (Direction == 2)
  305. {
  306. imageZC.Visibility = Visibility.Collapsed;
  307. imageYC.Visibility = Visibility.Collapsed;
  308. imageGZ.Visibility = Visibility.Visible;
  309. textBlock.Visibility = Visibility.Visible;
  310. textBlock.Text = "故障";
  311. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FFFF0202"));
  312. }
  313. else
  314. {
  315. imageZC.Visibility = Visibility.Visible;
  316. imageYC.Visibility = Visibility.Collapsed;
  317. imageGZ.Visibility = Visibility.Collapsed;
  318. textBlock.Visibility = Visibility.Visible;
  319. textBlock.Text = "未运行";
  320. textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF02F9FF"));
  321. }
  322. }
  323. }
  324. /// <summary>
  325. /// 执行内容
  326. /// </summary>
  327. [Category("事件")]
  328. public string TikcExecute
  329. {
  330. get { return (string)GetValue(TikcExecuteProperty); }
  331. set { SetValue(TikcExecuteProperty, value); }
  332. }
  333. public static readonly DependencyProperty TikcExecuteProperty =
  334. DependencyProperty.Register("TikcExecute", typeof(string), typeof(TheRedis), new PropertyMetadata(string.Empty));
  335. #endregion
  336. #region 数据绑定模块
  337. public event EventHandler PropertyChange; //声明一个事件
  338. [Category("数据绑定-数据来源")]
  339. public int TimeCount
  340. {
  341. get { return (int)GetValue(TimeCountProperty); }
  342. set { SetValue(TimeCountProperty, value); }
  343. }
  344. public static readonly DependencyProperty TimeCountProperty =
  345. DependencyProperty.Register("TimeCount", typeof(int), typeof(TheRedis), new PropertyMetadata(100));
  346. [Category("数据绑定-数据来源")]
  347. public string DataSouceInformation
  348. {
  349. get { return (string)GetValue(DataSouceInformationProperty); }
  350. set { SetValue(DataSouceInformationProperty, value); }
  351. }
  352. public static readonly DependencyProperty DataSouceInformationProperty =
  353. DependencyProperty.Register("DataSouceInformation", typeof(string), typeof(TheRedis), new PropertyMetadata("124.222.238.75:16000,password=123456,defaultDatabase=1"));
  354. [Category("数据绑定-数据来源")]
  355. public string DeviceName
  356. {
  357. get { return (string)GetValue(DeviceNameProperty); }
  358. set { SetValue(DeviceNameProperty, value); }
  359. }
  360. public static readonly DependencyProperty DeviceNameProperty =
  361. DependencyProperty.Register("DeviceName", typeof(string), typeof(TheRedis), new PropertyMetadata(string.Empty));
  362. [Category("数据绑定")]
  363. public string FDataSouce
  364. {
  365. get { return (string)GetValue(FDataSouceProperty); }
  366. set { SetValue(FDataSouceProperty, value); }
  367. }
  368. public static readonly DependencyProperty FDataSouceProperty =
  369. DependencyProperty.Register("FDataSouce", typeof(string), typeof(TheRedis), new PropertyMetadata(string.Empty, new PropertyChangedCallback(onFDataSouceChanged)));
  370. private static void onFDataSouceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheRedis)?.DataSouceRefresh();
  371. public void DataSouceRefresh()
  372. {
  373. try
  374. {
  375. //if (!string.IsNullOrEmpty(FDataSouce))
  376. {
  377. GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code, new object[] { FDataSouce });
  378. }
  379. }
  380. catch (Exception ex)
  381. {
  382. }
  383. }
  384. public static string _code = "public string main(string message) \n{ \n //请在此填写你的代码\n\n return message; \n}\n";
  385. [Category("数据绑定")]
  386. public string Code
  387. {
  388. get { return (string)GetValue(CodeProperty); }
  389. set { SetValue(CodeProperty, value); }
  390. }
  391. public static readonly DependencyProperty CodeProperty =
  392. DependencyProperty.Register("Code", typeof(string), typeof(TheRedis), new PropertyMetadata(_code));
  393. [Category("数据绑定")]
  394. public bool IsRun
  395. {
  396. get { return (bool)GetValue(RunProperty); }
  397. set { SetValue(RunProperty, value); }
  398. }
  399. public static readonly DependencyProperty RunProperty =
  400. DependencyProperty.Register("IsRun", typeof(bool), typeof(TheRedis), new PropertyMetadata(false, new PropertyChangedCallback(onIsRunChanged)));
  401. private static void onIsRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheRedis)?.IsRunRefresh();
  402. public void IsRunRefresh()
  403. {
  404. //测试运行
  405. if (IsRun)
  406. IsExecuteState = true;
  407. else
  408. {
  409. IsExecuteState = false;
  410. Dispose();
  411. }
  412. }
  413. [Category("数据绑定")]
  414. public string GenerateData
  415. {
  416. get { return (string)GetValue(GenerateDataProperty); }
  417. set { SetValue(GenerateDataProperty, value); }
  418. }
  419. public static readonly DependencyProperty GenerateDataProperty =
  420. DependencyProperty.Register("GenerateData", typeof(string), typeof(TheRedis), new PropertyMetadata(string.Empty));
  421. #endregion
  422. }
  423. }