|
- using BPASmartClient.Compiler;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Linq;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Media;
- using System.Windows.Threading;
-
-
- namespace BPASmartClient.SCADAControl.CustomerControls
- {
- /// <summary>
- /// TheAPI.xaml 的交互逻辑
- /// </summary>
- public partial class TheAPI :UserControl, IExecutable, IDisposable
- {
- TextBlock textBlock = null;
- public TheAPI()
- {
- InitializeComponent();
- Width = 40;
- Height = 40;
- this.SizeChanged += TheAPI_SizeChanged; ;
- }
-
- private void TheAPI_SizeChanged(object sender,SizeChangedEventArgs e)
- {
- if (textBlock == null)
- {
- 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()
- {
- timer.Interval = TimeSpan.FromMilliseconds(TimeCount);
- timer.Tick += Timer_Tick;
- timer.Start();
-
- }
- private async void Timer_Tick(object sender,EventArgs e)
- {
- Config.GetInstance().RunJsScipt(TikcExecute);
- try
- {
- switch (InterfaceMode)
- {
- case InterfaceModeEnum.POST:
- if (!string.IsNullOrEmpty(DataSouceInformation))
- {
- Direction = 1;
- FDataSouce = APIHelper.GetInstance.HttpRequest(DataSouceInformation,InterfaceParameters);
-
- }
- break;
- case InterfaceModeEnum.GET:
- if (!string.IsNullOrEmpty(DataSouceInformation))
- {
- Direction = 1;
- string returndata=string.Empty;
- string obj_data= APIHelper.GetInstance.GetRequest(DataSouceInformation);
- Dictionary<string, object> obj = JsonConvert.DeserializeObject<Dictionary<string, object>>(obj_data);
- if (obj.ContainsKey("data"))
- {
- returndata = obj_data.ToString();
- } else
- {
- foreach (var item in obj)
- {
- if(item.Value is JObject)
- {
- Dictionary<string, object> itemkey = JsonConvert.DeserializeObject<Dictionary<string, object>>(item.Value.ToString());
- if (itemkey.ContainsKey("data"))
- {
- returndata = item.Value.ToString();
- }
- }
- }
- }
- //List<Dictionary<string, object>> obj2 = JsonConvert.DeserializeObject<List<Dictionary<string, object>>>(obj1["data"].ToString());
- FDataSouce = returndata;
- }
- break;
- case InterfaceModeEnum.PUT:
- if (!string.IsNullOrEmpty(DataSouceInformation))
- {
- Direction = 1;
- //string data = APIHelper.GetInstance.HttpRequest(DataSouceInformation,InterfaceParameters);
- }
- break;
- default:
- break;
- }
- }
- catch (Exception ex)
- {
- Direction = 2;
- }
- }
-
- public void Start() => timer.Start();
- public void Stop() => timer.Stop();
-
- public void Dispose()
- {
- timer.Stop();
- Direction=0;
- FDataSouce = "";
- if (IsRun) IsRun = false;
- }
-
- #region 属性
- [Category("值设定")]
- public int Direction
- {
- get { return (int)GetValue(DirectionProperty); }
- set { SetValue(DirectionProperty,value); }
- }
- public static readonly DependencyProperty DirectionProperty =
- DependencyProperty.Register("Direction",typeof(int),typeof(TheAPI),
- new PropertyMetadata(0,new PropertyChangedCallback(OnPropertyChanged)));
- private static void OnPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
- {
- (d as TheAPI)?.Refresh();
- }
- public void Refresh()
- {
- if (textBlock != null)
- {
- if (Direction == 1)
- {
- textBlock.Visibility = Visibility.Visible;
- textBlock.Text = "运行中";
- textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FF2077EC"));
- }
- else if (Direction == 2)
- {
- textBlock.Visibility = Visibility.Visible;
- textBlock.Text = "故障";
- textBlock.Foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FFFF0202"));
- }
- else
- {
- 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(TheAPI),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(TheAPI),new PropertyMetadata(2000));
- [Category("数据绑定-数据来源")]
- public InterfaceModeEnum InterfaceMode
- {
- get { return (InterfaceModeEnum)GetValue(InterfaceModeProperty); }
- set { SetValue(InterfaceModeProperty,value); }
- }
- public static readonly DependencyProperty InterfaceModeProperty =
- DependencyProperty.Register("InterfaceMode",typeof(InterfaceModeEnum),typeof(TheAPI),new PropertyMetadata(InterfaceModeEnum.GET));
-
- [Category("数据绑定-数据来源")]
- public string InterfaceParameters
- {
- get { return (string)GetValue(InterfaceParametersProperty); }
- set { SetValue(InterfaceParametersProperty,value); }
- }
- public static readonly DependencyProperty InterfaceParametersProperty =
- DependencyProperty.Register("InterfaceParameters",typeof(string),typeof(TheAPI),new PropertyMetadata(string.Empty));
- [Category("数据绑定-数据来源")]
- public string DataSouceInformation
- {
- get { return (string)GetValue(DataSouceInformationProperty); }
- set { SetValue(DataSouceInformationProperty,value); }
- }
- public static readonly DependencyProperty DataSouceInformationProperty =
- DependencyProperty.Register("DataSouceInformation",typeof(string),typeof(TheAPI),new PropertyMetadata("http://datav.dev1.com/api/Alarm/Query"));
- //[Category("数据绑定-数据来源")]
- //public string DeviceName
- //{
- // get { return (string)GetValue(DeviceNameProperty); }
- // set { SetValue(DeviceNameProperty,value); }
- //}
- //public static readonly DependencyProperty DeviceNameProperty =
- // DependencyProperty.Register("DeviceName",typeof(string),typeof(TheAPI),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(TheAPI),new PropertyMetadata(string.Empty,new PropertyChangedCallback(onFDataSouceChanged)));
- private static void onFDataSouceChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) => (d as TheAPI)?.DataSouceRefresh();
- public void DataSouceRefresh()
- {
- try
- {
- //if (!string.IsNullOrEmpty(FDataSouce))
- {
- GenerateData = (string)CSharpConfig.GetInstance().RunCSharp(Code,new object[] { FDataSouce });
- Class_DataBus.GetInstance().Dic_APIData[this.Name] = GenerateData;
- if (PropertyChange != null)
- {
- PropertyChange(this,null);
- }
- }
- }
- 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(TheAPI),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(TheAPI), new PropertyMetadata(false, new PropertyChangedCallback(onIsRunChanged)));
- private static void onIsRunChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => (d as TheAPI)?.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(TheAPI),new PropertyMetadata(string.Empty));
- #endregion
-
-
-
-
- }
- }
-
|