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.
CaiQie/RfidWeb/FormMain.cs

372 lines
10 KiB
C#

using DB;
using DB.Entity;
using DB.Service;
using HslCommunication.LogNet;
using HZH_Controls.Forms;
using NewLife.Caching;
using NewLife.Threading;
using RfidWeb.Frm;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Security.Policy;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tool;
using Tool.Model;
namespace RfidWeb
{
public partial class FormMain : Form
{
private ILogNet logNet = ILogNetFactory.GetLogNet;
private ICache cache;
TimerX _timer;
List<Button> buttons=new List<Button>();
private ReadBufLogService readBufService = new ReadBufLogService();
public FormMain()
{
cache=Cache.Default;
var currentPlcIp = RfidSetting.Current.PlcIp;
DbInfo.Init(typeof(UserInfo).Assembly);
InitializeComponent();
if (this.FormBorderStyle == FormBorderStyle.None)
{
this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.WindowState = FormWindowState.Normal;
}
else
{
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
Init();
}
private void Init()
{
ShowLed();
_timer = new TimerX(TimeState, null, 2000, 1000);
_timer.Async=true;
// 如果所有的日志在记录之前需要在控制台显示出来
logNet.BeforeSaveToFile += (object sender, HslEventArgs e) =>
{
Console.WriteLine(e.HslMessage.ToString());
};
logNet.ConsoleOutput = true;
buttons.Clear();
buttons.Add(btnShow);
buttons.Add(btnArgument);
buttons.Add(btnAlarm);
buttons.Add(btnAccount);
buttons.Add(btnMes);
buttons.Add(buttonUpdateLog);
buttons.Add(btnBarCode);
btnShow_Click(new object(), EventArgs.Empty);
}
/// <summary>移除过期的缓存项</summary>
void TimeState(Object state)
{
bool isExit = UserManager.IsExit();
this.Invoke(() =>
{
labTime.Text = DateTime.Now.ToFullString();
//不存在
if (!isExit)
{
labUser.ForeColor = System.Drawing.SystemColors.Window;
labUser.Text = "未登录(未授权)";
CacheManager.Act(CacheKeyManager.FormMain, () =>
{
ThreadPoolX.QueueUserWorkItem(() =>
{
PlcConnect.Instance.Write(HmiPointInfo.Safety_level, int.Parse("0"));
});
});
}
else
{
var dto = UserManager.GetUser();
labUser.Text = $"{dto.UserName}({dto.RoleName})";
labUser.ForeColor = Color.FromArgb(21, 182, 173);
}
});
if (!PlcConnect.IsSuccess) return;
ThreadPoolX.QueueUserWorkItem(UpdateLog);
ThreadPoolX.QueueUserWorkItem(ReadBufLog);
}
private UpdateLogService updateLogService = new UpdateLogService();
private PointService pointService = new PointService();
private void ShowLed()
{
var _cancellationTokenSource = new CancellationTokenSource();
var token = _cancellationTokenSource.Token;
Task.Run(async () =>
{
while (!token.IsCancellationRequested)
{
// 在这里执行你的轮询逻辑
var result =await PlcConnect.Instance.ReadPlcTypeAsync();
hslLanternSimplePlc.LanternBackground = result.IsSuccess
? Color.LimeGreen : Color.Red;
// 在主线程更新 UI
//Invoke(new Action(() =>
//{
//}));
PlcConnect.IsSuccess = result.IsSuccess;
await Task.Delay(1000, token); // 每隔一秒延时
}
}, token);
}
private void ReadBufLog()
{
CacheManager.Act(CacheKeyManager.ReadBufLog, () =>
{
var barCodea = HmiPointInfo.GetBufA();
if (barCodea.Item1)
{
readBufService.Insert("A", barCodea.Item2);
}
var barCodeb = HmiPointInfo.GetBufB();
if (barCodeb.Item1)
{
readBufService.Insert("B", barCodeb.Item2);
}
var barCodec = HmiPointInfo.GetBufC();
if (barCodec.Item1)
{
readBufService.Insert("C", barCodec.Item2);
}
});
}
/// <summary>
/// 参数变化
/// </summary>
private void UpdateLog()
{
if (UserManager.IsExit())
{
CacheManager.Act(CacheKeyManager.UpdateLog, () =>
{
var dic = pointService.GetValueName();
var plc = PlcConnect.Instance;
var userDto = UserManager.GetUser();
var strings = dic.Keys;
foreach (var se in strings)
{
var res = plc.ReadInt32(se);
if (res.IsSuccess)
{
string name = dic[se];
updateLogService.AddOrUpdateLog(userDto, name, res.Content);
}
}
});
}
}
private void btnExit_Click(object sender, EventArgs e)
{
if (FrmDialog.ShowDialog(this, "是否退出系统?", "提示窗体", true)
== System.Windows.Forms.DialogResult.OK)
{
Application.Exit();
}
}
private void btnArgument_Click(object sender, EventArgs e)
{
CheckLogin();
var isLogin = UserManager.IsExit();
if (isLogin)
{
panContent.Controls.Clear();
panContent.Controls.Add(new FormPar());
CheckButton(btnArgument);
}
}
private void CheckLogin()
{
bool isLogin = UserManager.IsExit();
if (!isLogin)
{
FormLogin loigLogin = new FormLogin();
loigLogin.StartPosition = FormStartPosition.CenterScreen; // 设置窗口显示在屏幕中央
loigLogin.ShowDialog();
}
}
private void btnAlarm_Click(object sender, EventArgs e)
{
CheckLogin();
var isLogin = UserManager.IsExit();
if (isLogin)
{
panContent.Controls.Clear();
panContent.Controls.Add(new FromAlarmLog());
CheckButton(btnAlarm);
}
}
/// <summary>
/// 帐号管理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnAccount_Click(object sender, EventArgs e)
{
CheckLogin();
var isLogin = UserManager.IsExit();
if (isLogin)
{
panContent.Controls.Clear();
panContent.Controls.Add(new FormAccount());
CheckButton(btnAccount);
}
}
Color checkColor = System.Drawing.Color.FromArgb(168, 188, 195);
Color defaultColor = System.Drawing.Color.FromArgb(21, 181, 173);
private void CheckButton(Button btn)
{
foreach (var bt in buttons)
{
if (bt.BackColor != defaultColor)
{
bt.BackColor = defaultColor;
}
}
btn.Focus();
btn.BackColor = checkColor;
}
private void btnShow_Click(object sender, EventArgs e)
{
panContent.Controls.Clear();
panContent.Controls.Add(new UserAlarmShow());
CheckButton(btnShow);
}
private void labUser_Click(object sender, EventArgs e)
{
if (labUser.Text != "未登录(未授权)")
{
FromEn fromEn = new FromEn();
fromEn.StartPosition = FormStartPosition.CenterScreen; // 设置窗口显示在屏幕中央
fromEn.ShowDialog();
if (!UserManager.IsExit())
{
btnShow_Click(new object(), EventArgs.Empty);
}
}
else
{
CheckLogin();
}
}
private void buttonUpdateLog_Click(object sender, EventArgs e)
{
panContent.Controls.Clear();
panContent.Controls.Add(new FromUploadLog());
CheckButton(buttonUpdateLog);
}
private void btnMes_Click(object sender, EventArgs e)
{
//
CheckLogin();
var isLogin = UserManager.IsExit();
if (isLogin)
{
panContent.Controls.Clear();
panContent.Controls.Add(new FrmPlan());
CheckButton(btnMes);
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("开发中");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("开发中");
}
private void btnBarCode_Click(object sender, EventArgs e)
{
panContent.Controls.Clear();
panContent.Controls.Add(new FrmBarCodeGridView());
CheckButton(btnBarCode);
}
}
}