终端一体化运控平台
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.
 
 
 

456 regels
18 KiB

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