You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
11 KiB
C#

using CentralControl.DBDAO;
using CommonFunc;
using CommonFunc.Tools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using XGL.Data.DBService;
using XGL.Tools;
using XGL.Views;
namespace XGL.FormItem
{
/// <summary>
/// Main.xaml 的交互逻辑
/// </summary>
public partial class Main : Window
{
public static Main main;
Frame frame1 = new Frame() { Content = new Views.LanJu_Index()};
Timer timer = new Timer();
string deviceCode = Utils.GetAppSetting("DeviceCode");
Timer timerDeciveState = new Timer();
System.Threading.Timer Time { get; set; }
private string _PingSeconds;
public string PingSeconds
{
get { return _PingSeconds; }
set
{
OnPropertyChanged("PingSeconds");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string info)
{
var handler = PropertyChanged;
handler?.Invoke(this, new PropertyChangedEventArgs(info));
}
public enum WindowID
{
frame1
}
public Main()
{
InitializeComponent();
main = this;
//WindowChange(WindowID.frame1);
CommonFunc.Common.ReadConfig();//读取配置文件
timer.Interval = 1000;
timer.Elapsed += Timer_Elapsed;
timer.Start();
//实例化绑定对象
Binding textBinding = new Binding();
//设置要绑定源
textBinding.Source = this;//绑定MainWindow类
//设置要绑定属性
textBinding.Path = new PropertyPath("PingSeconds");//绑定MainWindow类下的TxtValue属性。
textBinding.Mode = BindingMode.TwoWay;//绑定模式双向绑定
//设置绑定到要绑定的控件
this.myTb.SetBinding(TextBlock.TextProperty, textBinding);
InitPage();
GetServiceDelay(Utils.GetServerIP());
}
private void InitPage()
{
if (Utils.GetAppSetting("ClientMode").Equals("0"))
{
LanJu_Prepare prepare = new LanJu_Prepare();
this.Index.Content = prepare;
}
else if (Utils.GetAppSetting("ClientMode").Equals("1"))
{
LanJu_Operator op = new LanJu_Operator();
this.Index.Content = op;
}
else
{
tbDevice.Visibility = Visibility.Collapsed;
shebei.Visibility = Visibility.Collapsed;
DryingRoomUC dryingRoomUC = new DryingRoomUC();
this.Index.Content = dryingRoomUC;
}
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
this.Dispatcher.Invoke(
new Action(() => { LabDate.Content = DateTime.Now.ToString("yyyy-MM-dd"); LabTime.Content = DateTime.Now.ToString("HH:mm:ss"); }
),
System.Windows.Threading.DispatcherPriority.Render) ;
}
public void WindowChange(WindowID windowID)
{
this.Index.Content = frame1;
}
private void Close(object sender, EventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
}
private void btnExit_Click(object sender, RoutedEventArgs e)
{
if (MessageBox.Show("是否确认关闭程序", "提示信息", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes)
{
System.Environment.Exit(System.Environment.ExitCode);
}
}
private void btnZXH_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Minimized;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
TimerDeciveState_Elapsed(null,null);
this.tbOperator.Text = LoginUser.UserName;
this.tbDevice.Text = Utils.GetAppSetting("DeviceCode");
string sitecode = Utils.GetAppSetting("SiteCode");
DBService userDbWareHouse = new DBService();
DataTable dt = userDbWareHouse.GetFactoryInfo(sitecode);
string name = dt == null ? "" : dt.Rows[0][0].ToString();
this.tbFactory.Text = name;
timerDeciveState.Interval = Utils.GetAppSetting("GetDeviceStateInterval") == "" ? 15000 : Convert.ToInt32(Utils.GetAppSetting("GetDeviceStateInterval"));
timerDeciveState.Elapsed += TimerDeciveState_Elapsed;
timerDeciveState.Start();
this.lbVersion.Content = LoginUser.Version;
}
public void GetServiceDelay(string serviceIp)
{
Ping p1 = new Ping();
Time = new System.Threading.Timer((s) =>
{
try
{
p1.SendAsyncCancel();
p1.SendPingAsync(serviceIp, 2000).ContinueWith(result =>
{
try
{
string time = result?.Result?.RoundtripTime.ToString();
//LogHelper.instance.log.Info("ip>>>>>>" + serviceIp + "延迟>>" + time);
Application.Current.Dispatcher.Invoke(() =>
{
if (!string.IsNullOrEmpty(time))
{
_PingSeconds = time;
PingSeconds = time;
this.btnText.Text = time;
}
else
{
_PingSeconds = time;
PingSeconds = "--";
this.btnText.Text = "--";
}
int second = 0;
if (int.TryParse(time, out second))
{
if (second >= 0 && second <= 20)
{
this.btnText.Foreground = Brushes.Green;
//System.Windows.Media.ColorConverter.ConvertFromString("#FF4FEE4F");
}
else if (second > 20 && second <= 40)
{
this.btnText.Foreground = Brushes.Orange;
}
else
{
this.btnText.Foreground = Brushes.Red;
}
}
else { this.btnText.Foreground = Brushes.Red; }
});
}
catch (Exception ex)
{
//LogHelper.instance.log.Info("ip>>>>>>" +ex.Message);
result?.Dispose();
}
});
}
catch (Exception)
{
p1.SendAsyncCancel();
}
}, 1, 0, 2000);
}
SerialHelper serialHelper = new SerialHelper();
int time = 0;
private void TimerDeciveState_Elapsed(object sender, ElapsedEventArgs e)
{
try
{
FormingMachineService formingMachineService = new FormingMachineService();
DataTable dt = formingMachineService.GetFormingMachineState(deviceCode);
if (dt == null)
{
LogHelper.instance.log.Warn("没有找到对应的设备,很有可能是断网了,请检查。");
//time = time + 1;
//报警
serialHelper.ComOn();
serialHelper.redLightOn();
serialHelper.buzzerOn();
serialHelper.ComOff();
return;
}
// 使用Dispatcher来在UI线程上更新UI
this.Dispatcher.Invoke(
new Action(() => { LbDeviceState.Content = dt.Rows[0][0].ToString() == "1" ? "正常" : "异常"; }
),
System.Windows.Threading.DispatcherPriority.Render);
if (dt.Rows[0][0].ToString() == "1" )
{
//if (time == 6)
//{
//取消报警
serialHelper.ComOn();
serialHelper.redLightOff();
serialHelper.buzzerOff();
serialHelper.ComOff();
//}
}
else
{
LogHelper.instance.log.Warn("设备状态异常,开启报警。");
// time = 6;
//报警
serialHelper.ComOn();
serialHelper.redLightOn();
serialHelper.buzzerOn();
serialHelper.ComOff();
}
}
catch (Exception ex)
{
LogHelper.instance.log.Error("获取设备状态时发生异常:"+ex.Message);
}
//if (dt.Rows[0][0].ToString() == "1")
//{
// this.Dispatcher.Invoke(
// new Action(() => { LbDeviceState.Background = Brushes.Green; }
// ),
// System.Windows.Threading.DispatcherPriority.Render);
//}
//else
//{
// this.Dispatcher.Invoke(
// new Action(() => { LbDeviceState.Background = Brushes.Red; }
// ),
// System.Windows.Threading.DispatcherPriority.Render);
//}
}
private void Window_Closing(object sender, CancelEventArgs e)
{
//取消报警
serialHelper.ComOn();
serialHelper.redLightOff();
serialHelper.buzzerOff();
serialHelper.ComOff();
}
private void notsos_Click(object sender, RoutedEventArgs e)
{
//取消报警
serialHelper.ComOn();
serialHelper.redLightOff();
serialHelper.buzzerOff();
serialHelper.ComOff();
}
}
}