|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- using BPASmartClient.Compiler;
- using Newtonsoft.Json;
- using StackExchange.Redis;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Threading;
-
- namespace BPASmartClient.SCADAControl.CustomerControls
- {
- /// <summary>
- /// TheRedis.xaml 的交互逻辑
- /// </summary>
- public partial class TheRedis : UserControl, IExecutable, IDisposable
- {
- Image imageZC = null;
- Image imageYC = null;
- Image imageGZ = null;
- TextBlock textBlock = null;
- FRedisClient fRedisClient = new FRedisClient();
- public TheRedis()
- {
- InitializeComponent();
- Width = 40;
- Height = 40;
- this.SizeChanged += TheRedis_SizeChanged;
- }
- private void TheRedis_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- if (textBlock == null)
- {
- foreach (Image tb in FindVisualChildren<Image>(this))
- {
- if (tb.Tag != null)
- {
- if (tb.Tag.ToString() == "正常")
- {
- imageZC = tb;
- }
-
- if (tb.Tag.ToString() == "运行")
- {
- imageYC = tb;
- }
-
- if (tb.Tag.ToString() == "故障")
- {
- imageGZ = tb;
- }
- }
- }
-
- foreach (TextBlock tb in FindVisualChildren<TextBlock>(this))
- {
- if (tb.Tag != null)
- {
- if (tb.Tag.ToString() == "显示文字")
- {
- textBlock = tb;
- }
- }
- }
- }
- }
- public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
- {
- if (depObj != null)
- {
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
- {
- DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
- if (child != null && child is T)
- {
- yield return (T)child;
- }
-
- foreach (T childOfChild in FindVisualChildren<T>(child))
- {
- yield return childOfChild;
- }
- }
- }
- }
- public string ControlType => "控件";
- private bool isExecuteState;
- public bool IsExecuteState
- {
- get { return isExecuteState; }
- set
- {
- isExecuteState = value;
- if (IsExecuteState)
- {
- Register();
- }
- }
- }
-
- DispatcherTimer timer = new DispatcherTimer();
- public void Register()
- {
- if (Direction == 0)
- {
- try
- {
- if (!string.IsNullOrEmpty(DataSouceInformation))
- {
- fRedisClient.Connect(DataSouceInformation);
- }
- }
- catch (Exception ex)
- {
- Direction = 2;
- }
- }
- timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
- timer.Tick += Timer_Tick;
- timer.Start();
-
- }
- private async void Timer_Tick(object sender, EventArgs e)
- {
- Config.GetInstance().RunJsScipt(TikcExecute);
-
- if (TestData)
- {
- keyValuePairs1.Clear();
- for (int i = 0; i < 20; i++)
- {
- Random ran = new Random();
- int n = ran.Next(0, 100);
- keyValuePairs1[i.ToString()] = "10." + n;
- keyValuePairs1["Left"+i.ToString()] = new Random().Next(0,10)>5?true: false;
- keyValuePairs1["Right"+i.ToString()] = new Random().Next(0, 10) > 5 ? true : false;
- }
- Class_DataBus.GetInstance().Dic_RedisData["Test"] = keyValuePairs1;
- Class_DataBus.GetInstance().Dic_RedisDataBinding = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(Class_DataBus.GetInstance().Dic_RedisData));
- Class_DataBus.GetInstance().refreshDataAction("Test");
- }
- else
- {
- if (fRedisClient._connection != null && fRedisClient._connection.IsConnected)
- {
- Direction = 1;
- }
- else
- {
- Direction = 0;
- try
- {
- if (!string.IsNullOrEmpty(DataSouceInformation))
- fRedisClient.Connect(DataSouceInformation);
- }
- catch (Exception ex)
- {
- Direction = 2;
- }
- }
- if (Direction == 1) //定时读取数据
- {
- try
- {
- if (!string.IsNullOrEmpty(DeviceName))
- {
- RedisValue obj = fRedisClient.RedisGet(DeviceName);
- List<ReeisDataModel> str = JsonConvert.DeserializeObject<List<ReeisDataModel>>(obj.ToString());
- Dictionary<string, object> keyValues = new Dictionary<string, object>();
- Dictionary<string, string> keyValuesType = new Dictionary<string, string>();
- str?.ForEach(jon =>
- {
- keyValues[jon.VarName] = GetValuesType(jon.DataType, jon.VarVaule);
- keyValuesType[jon.VarName] = jon.DataType.ToString();
- });
- Class_DataBus.GetInstance().Dic_RedisData[DeviceName] = keyValues;
- Class_DataBus.GetInstance().Dic_RedisDataBinding = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(Class_DataBus.GetInstance().Dic_RedisData));
- Class_DataBus.GetInstance().Dic_RedisDataType[DeviceName] = keyValuesType;
- Class_DataBus.GetInstance().refreshDataAction(DeviceName);
- if (PropertyChange != null) PropertyChange(this, null);
- }
- else
- {
- fRedisClient.GetKeys()?.ToList().ForEach(par =>
- {
- //所有变量集合
- //PublishInfo
- List<ReeisDataModel> str = JsonConvert.DeserializeObject<List<ReeisDataModel>>(par.Value);
- Dictionary<string, object> keyValues = new Dictionary<string, object>();
- Dictionary<string, string> keyValuesType = new Dictionary<string, string>();
- str?.ForEach(jon =>
- {
- keyValues[jon.VarName] = GetValuesType(jon.DataType, jon.VarVaule);
- keyValuesType[jon.VarName] = jon.DataType.ToString();
- });
- Class_DataBus.GetInstance().Dic_RedisData[par.Key] = keyValues;
- Class_DataBus.GetInstance().Dic_RedisDataBinding = JsonConvert.DeserializeObject<Dictionary<string, object>>(JsonConvert.SerializeObject(Class_DataBus.GetInstance().Dic_RedisData));
- Class_DataBus.GetInstance().Dic_RedisDataType[par.Key] = keyValuesType;
- Class_DataBus.GetInstance().refreshDataAction(par.Key);
-
- });
- if (PropertyChange != null) PropertyChange(this, null);
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- }
- }
-
- /// <summary>
- /// 根据变量获取变量实际值
- /// </summary>
- /// <param name="eData"></param>
- /// <param name="Value"></param>
- /// <returns></returns>
- public object GetValuesType(EDataType eData, string Value)
- {
- try
- {
- if (!string.IsNullOrEmpty(Value))
- {
- switch (eData)
- {
- case EDataType.Bool:
- return Convert.ToBoolean(Value);
- break;
- case EDataType.Byte:
- return Convert.ToByte(Value);
- break;
- case EDataType.Int:
- return Convert.ToInt16(Value);
- break;
- case EDataType.Word:
- return Convert.ToUInt16(Value);
- break;
- case EDataType.Dint:
- return Convert.ToInt32(Value);
- break;
- case EDataType.Dword:
- return Convert.ToUInt32(Value);
- break;
- case EDataType.Float:
- return Convert.ToSingle(Value);
- break;
- default:
- break;
- }
- }
- return null;
- }
- catch (Exception ex)
- {
- return null;
- }
- }
-
- public void Start() => timer.Start();
- public void Stop() => timer.Stop();
-
- public void Dispose()
- {
- timer.Stop();
- Direction = 0;
- FDataSouce = "";
- if (IsRun) IsRun = false;
- }
-
- #region 属性
- public bool TestData
- {
- get { return (bool)GetValue(TestDataProperty); }
- set { SetValue(TestDataProperty, value); }
- }
- public static readonly DependencyProperty TestDataProperty =
- DependencyProperty.Register("TestData", typeof(bool), typeof(TheRedis),
- new PropertyMetadata(false, new PropertyChangedCallback(OnTestDataPropertyChanged)));
- private static void OnTestDataPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- (d as TheRedis)?.RefreshTest();
- }
- #region MyRegion
- Dictionary<string, object> keyValuePairs1 = new Dictionary<string, object>();
- #endregion
- public void RefreshTest()
- {
-
-
- }
- [Category("值设定")]
- public int Direction
- {
- get { return (int)GetValue(DirectionProperty); }
- set { SetValue(DirectionProperty, value); }
- }
- public static readonly DependencyProperty DirectionProperty =
- DependencyProperty.Register("Direction", typeof(int), typeof(TheRedis),
- new PropertyMetadata(0, new PropertyChangedCallback(OnPropertyChanged)));
- private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
- {
- (d as TheRedis)?.Refresh();
- }
- public void Refresh()
- {
- if (textBlock != null)
- {
- if (Direction == 1)
- {
- imageZC.Visibility = Visibility.Collapsed;
- imageYC.Visibility = Visibility.Visible;
- imageGZ.Visibility = Visibility.Collapsed;
- textBlock.Visibility = Visibility.Visible;
- textBlock.Text = "运行中";
- textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF2077EC"));
- }
- else if (Direction == 2)
- {
- imageZC.Visibility = Visibility.Collapsed;
- imageYC.Visibility = Visibility.Collapsed;
- imageGZ.Visibility = Visibility.Visible;
- textBlock.Visibility = Visibility.Visible;
- textBlock.Text = "故障";
- textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FFFF0202"));
- }
- else
- {
- imageZC.Visibility = Visibility.Visible;
- imageYC.Visibility = Visibility.Collapsed;
- imageGZ.Visibility = Visibility.Collapsed;
- textBlock.Visibility = Visibility.Visible;
- textBlock.Text = "未运行";
- textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF02F9FF"));
- }
- }
- }
- /// <summary>
- /// 执行内容
- /// </summary>
- [Category("事件")]
- public string TikcExecute
- {
- get { return (string)GetValue(TikcExecuteProperty); }
- set { SetValue(TikcExecuteProperty, value); }
- }
- public static readonly DependencyProperty TikcExecuteProperty =
- DependencyProperty.Register("TikcExecute", typeof(string), typeof(TheRedis), new PropertyMetadata(string.Empty));
- #endregion
-
- #region 数据绑定模块
- public event EventHandler PropertyChange; //声明一个事件
- [Category("数据绑定-数据来源")]
- public int TimeCount
- {
- get { return (int)GetValue(TimeCountProperty); }
- set { SetValue(TimeCountProperty, value); }
- }
- public static readonly DependencyProperty TimeCountProperty =
- DependencyProperty.Register("TimeCount", typeof(int), typeof(TheRedis), new PropertyMetadata(100));
- [Category("数据绑定-数据来源")]
- public string DataSouceInformation
- {
- get { return (string)GetValue(DataSouceInformationProperty); }
- set { SetValue(DataSouceInformationProperty, value); }
- }
- public static readonly DependencyProperty DataSouceInformationProperty =
- DependencyProperty.Register("DataSouceInformation", typeof(string), typeof(TheRedis), new PropertyMetadata("124.222.238.75:16000,password=123456,defaultDatabase=1"));
- [Category("数据绑定-数据来源")]
- public string DeviceName
- {
- get { return (string)GetValue(DeviceNameProperty); }
- set { SetValue(DeviceNameProperty, value); }
- }
- public static readonly DependencyProperty DeviceNameProperty =
- DependencyProperty.Register("DeviceName", typeof(string), typeof(TheRedis), new PropertyMetadata(string.Empty));
- [Category("数据绑定")]
- public string FDataSouce
- {
- get { return (string)GetValue(FDataSouceProperty); }
- set { SetValue(FDataSouceProperty, value); }
- }
- public static readonly DependencyProperty FDataSouceProperty =
- DependencyProperty.Register("FDataSouce", typeof(string), typeof(TheRedis), new PropertyMetadata(string.Empty, new PropertyChangedCallback(onFDataSouceChanged)));
- private static void onFDataSouceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheRedis)?.DataSouceRefresh();
- public void DataSouceRefresh()
- {
- try
- {
- //if (!string.IsNullOrEmpty(FDataSouce))
- {
- GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code, new object[] { FDataSouce });
-
- }
- }
- catch (Exception ex)
- {
-
- }
- }
- public static string _code = "public string main(string message) \n{ \n //请在此填写你的代码\n\n return message; \n}\n";
- [Category("数据绑定")]
- public string Code
- {
- get { return (string)GetValue(CodeProperty); }
- set { SetValue(CodeProperty, value); }
- }
- public static readonly DependencyProperty CodeProperty =
- DependencyProperty.Register("Code", typeof(string), typeof(TheRedis), new PropertyMetadata(_code));
- [Category("数据绑定")]
- public bool IsRun
- {
- get { return (bool)GetValue(RunProperty); }
- set { SetValue(RunProperty, value); }
- }
- public static readonly DependencyProperty RunProperty =
- DependencyProperty.Register("IsRun", typeof(bool), typeof(TheRedis), new PropertyMetadata(false, new PropertyChangedCallback(onIsRunChanged)));
- private static void onIsRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheRedis)?.IsRunRefresh();
- public void IsRunRefresh()
- {
- //测试运行
- if (IsRun)
- IsExecuteState = true;
- else
- {
- IsExecuteState = false;
- Dispose();
- }
- }
- [Category("数据绑定")]
- public string GenerateData
- {
- get { return (string)GetValue(GenerateDataProperty); }
- set { SetValue(GenerateDataProperty, value); }
- }
- public static readonly DependencyProperty GenerateDataProperty =
- DependencyProperty.Register("GenerateData", typeof(string), typeof(TheRedis), new PropertyMetadata(string.Empty));
- #endregion
-
-
- }
- }
|