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.
Cigarette/WindowsFormsApp2/UserControl1.cs

652 lines
30 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using System.Runtime.InteropServices;
namespace WindowsFormsApp2
{
public partial class UserControl1 : UserControl
{
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
const int NumberOfSupportedCameras = 8;
public Thread recvThread = null;
public Thread filerecvThread = null;
bool m_Done = false;
public static byte[] clientIP = new byte[] { 192, 168, 10, 127 };
public static byte[] localIP = new byte[] { 192, 168, 10, 127 };
public static int ordPort = 3333;
public static int feedPort = 2999;
public static int filePort = 2999;
UdpClient u = null;
UdpClient fileu = null;
public static int[] status = new int[5];
DialogSetup setup = new DialogSetup();
protected virtual void OnClosing(System.ComponentModel.CancelEventArgs e)
{
m_Done = true;
}
public static void SendMsg(string obj)
{
try
{
UdpClient udpcSend = new UdpClient(0);
byte[] sendbytes = Encoding.UTF8.GetBytes(obj);
// IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse("192.168.31.243"), 3333);
IPEndPoint remoteIpep = new IPEndPoint(new IPAddress(clientIP), ordPort);
udpcSend.Send(sendbytes, sendbytes.Length, remoteIpep);
udpcSend.Close();
}
catch
{
}
}
public void RecMsg()
{
try
{
u = new UdpClient(new IPEndPoint(new IPAddress(localIP), feedPort));
recvThread = new Thread(new ThreadStart(Received));
recvThread.Priority = ThreadPriority.Normal;
recvThread.Start();
fileu = new UdpClient(new IPEndPoint(new IPAddress(localIP), filePort));
filerecvThread = new Thread(new ThreadStart(FileReceived));
filerecvThread.Priority = ThreadPriority.Normal;
filerecvThread.Start();
}
catch
{
//SOCKETEventArrive("receive:Nullerror");
}
}
private void FileSend(String FileName ,int port)
{
try
{
UdpClient udpcSend = new UdpClient(0);
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
StreamReader sw = new StreamReader(fs, Encoding.ASCII);
String filename = Path.GetFileName(FileName);
String sync = "SYNC";
String FileStartFrame = sync;
FileStartFrame = FileStartFrame + "str";
FileStartFrame = FileStartFrame + filename;
byte[] sendbytes = Encoding.ASCII.GetBytes(FileStartFrame);
udpcSend.Send(sendbytes, sendbytes.Length, new IPEndPoint(new IPAddress(clientIP), port));
//尚未读取的⽂件内容长度
long left = fs.Length;
//存储读取结果
char[] bytes = new char[2000];
//每次读取长度
int maxLength = bytes.Length;
//读取位置
int start = 0;
//实际返回结果长度
int num = 0;
//当⽂件未读取长度⼤于0时不断进⾏读取
while (left > 0)
{
fs.Position = start;
num = 0;
if (left < maxLength)
num = sw.Read(bytes, 0, Convert.ToInt32(left));
else
num = sw.Read(bytes, 0, maxLength);
if (num == 0)
break;
start += num;
left -= num;
String FileFrame = sync;
FileFrame = FileFrame + "con";
FileFrame = FileFrame + new string(bytes,0, num + 7);
sendbytes = Encoding.ASCII.GetBytes(FileFrame);
udpcSend.Send(sendbytes, sendbytes.Length, new IPEndPoint(new IPAddress(clientIP), port));
Thread.Sleep(10); //防止系统资源耗尽
}
String FileEndFrame = sync;
FileEndFrame = FileEndFrame + "end";
FileEndFrame = FileEndFrame + fs.Length.ToString();
sendbytes = Encoding.ASCII.GetBytes(FileEndFrame);
udpcSend.Send(sendbytes, sendbytes.Length, new IPEndPoint(new IPAddress(clientIP), port));
sw.Close();
fs.Close();
udpcSend.Close();
}
catch
{
throw;
}
}
private void FileReceived()
{
FileStream fs = null;
StreamWriter sw = null;
String filename = "";
while (!m_Done)
{
IPEndPoint endpoint = null;
if (fileu != null && null != filerecvThread && filerecvThread.IsAlive)
{
//接收数据
try
{
Byte[] data = fileu.Receive(ref endpoint);
//得到数据的ACSII的字符串形式
String receiveString = Encoding.Default.GetString(data);
if (receiveString != null)
{
String str = receiveString.Substring(0, 4);
if(str == "SYNC")
{
str = receiveString.Substring(4, 3);
if(str == "str")
{
filename = receiveString.Substring(7, receiveString.Length - 7);
if (fs == null)
fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fs.Seek(0, SeekOrigin.Begin);
fs.SetLength(0);
}
else if(str == "con")
{
str = receiveString.Substring(7, receiveString.Length - 7);
if(fs != null)
{
if(sw == null)
sw = new StreamWriter(fs, Encoding.Default);
sw.Write(str);
}
}
else if (str == "end")
{
str = receiveString.Substring(7, receiveString.Length - 7);
if (sw != null)
{
sw.Close();
sw = null;
}
if (fs != null)
{
fs.Close();
fs = null;
}
if(filename == "conf.txt")
{
string startuppath = Application.StartupPath;
var p = System.Diagnostics.Process.Start(startuppath + "/" + filename);
if (p != null)
SetForegroundWindow(p.MainWindowHandle);
}
}
}
}
}
catch
{
throw;
}
}
Thread.Sleep(10); //防止系统资源耗尽
}
}
private void Received()
{
//ASCII 编码
Encoding ASCII = Encoding.ASCII;
while (!m_Done)
{
IPEndPoint endpoint = null;
if (u != null && null != recvThread && recvThread.IsAlive)
{
//接收数据
try
{
Byte[] data = u.Receive(ref endpoint);
//得到数据的ACSII的字符串形式
String receiveString = ASCII.GetString(data);
if (receiveString != null)
{
if (receiveString.CompareTo("OpNoLogin") == 0)
{
// 需要登录
DialogLogin dialogLogin = new DialogLogin();
dialogLogin.IsAdmin = false;
dialogLogin.StartPosition = FormStartPosition.CenterScreen;
// dialogLogin.Location = new Point((SystemInformation.PrimaryMonitorSize.Width - dialogLogin.Width) / 2,(SystemInformation.PrimaryMonitorSize.Height - dialogLogin.Height) / 2);
dialogLogin.TopMost = true;
dialogLogin.ShowDialog();
}
else if (receiveString.CompareTo("AdminNoLogin") == 0)
{
// 需要登录
DialogLogin dialogLogin = new DialogLogin();
dialogLogin.IsAdmin = true;
dialogLogin.StartPosition = FormStartPosition.CenterScreen;
// dialogLogin.Location = new Point((SystemInformation.PrimaryMonitorSize.Width - dialogLogin.Width) / 2,(SystemInformation.PrimaryMonitorSize.Height - dialogLogin.Height) / 2);
dialogLogin.TopMost = true;
dialogLogin.ShowDialog();
}
else if (receiveString.CompareTo("UnKickFail") == 0)
{
CheckBox2Progress(false);
}
else if (receiveString.CompareTo("UnKickOk") == 0)
{
CheckBox2Progress(true);
}
else if (receiveString.CompareTo("DebugEnStillWork") == 0)
{
CheckBox1Progress(false);
DialogResult dr = MessageBox.Show("请先点击‘暂停工作’按钮,才能进入调试模式", "操作顺序检查", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
else if (receiveString.CompareTo("DebugDisStillWork") == 0)
{
CheckBox1Progress(false);
DialogResult dr = MessageBox.Show("请先点击‘暂停工作’按钮,才能进入调试模式", "操作顺序检查", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
else if (receiveString.CompareTo("DebugEnFail") == 0)
{
CheckBox1Progress(false);
}
else if (receiveString.CompareTo("DebugDisFail") == 0)
{
CheckBox1Progress(true);
}
else if (receiveString.CompareTo("DebugEn") == 0)
{
CheckBox1Progress(true);
}
else if (receiveString.CompareTo("DebugDis") == 0)
{
CheckBox1Progress(false);
}
else if (receiveString.CompareTo("KickFail") == 0)
{
CheckBox2Progress(true);
}
else if (receiveString.CompareTo("LoginIn") == 0)
{
// CheckBox1Progress(true);
}
else if (receiveString.CompareTo("PswError") == 0)
{
DialogResult dr = MessageBox.Show("密码错误,请重新输入", "提示消息", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
else if (receiveString.CompareTo("Need_Login") == 0)
{
DialogResult dr = MessageBox.Show("请先点击解锁按钮,进入管理员模式", "权限检查", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
else if (receiveString.CompareTo("CheckWorkState") == 0)
{
DialogResult dr = MessageBox.Show("请先点击“暂停工作”按钮,进入暂停模式,才可以进行换班操作", "状态检查", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
else if (receiveString.CompareTo("UnlockOk") == 0)
{
button3.BackgroundImage = WindowsFormsApp2.Properties.Resources._lock;
}
else if (receiveString.CompareTo("LockOk") == 0)
{
button3.BackgroundImage = WindowsFormsApp2.Properties.Resources.unlock;
}
else if (receiveString.CompareTo("StillDebug") == 0)
{
DialogResult dr = MessageBox.Show("请先点击“调试模式”复选框,取消调试模式,才可以开始工作", "操作顺序检查", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
else if (receiveString.CompareTo("KickOk") == 0)
{
CheckBox2Progress(false);
}
else if (receiveString.CompareTo("CanGetSetting") == 0)
{
SendMsg("GETSETTINGPRO:" + NumberOfSupportedCameras.ToString());
var task = Task.Run(async delegate
{
await Task.Delay(500);
setup.UpdateDialog();
setup.StartPosition = FormStartPosition.CenterScreen;//中心
setup.TopMost = true;
setup.ShowDialog();
});
}
else if (receiveString.CompareTo("CanSetConf") == 0)
{
try
{
FileSend("conf.txt", filePort);
DialogResult dr = MessageBox.Show("配置已发送", "状态", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
catch
{
DialogResult dr = MessageBox.Show("配置发送失败", "状态", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
}
else if (receiveString.CompareTo("RestartFailInWorking") == 0) // still in working when restart application
{
DialogResult dr = MessageBox.Show("请先点击‘暂停工作’按钮,再重启程序", "操作顺序检查", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
else if (receiveString.CompareTo("Work") == 0)
{
label6.Text = " 运行中 ";
label8.BackColor = Color.Lime;
label6.BackColor = Color.Lime;
}
else if (receiveString.CompareTo("NoWork") == 0) //no work
{
label6.Text = " 未运行 ";
label8.BackColor = Color.Yellow;
label6.BackColor = Color.Yellow;
}
else if (receiveString.CompareTo("UpSettingOk") == 0)
{
DialogResult dr = MessageBox.Show("保存参数成功", "状态", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
else if (receiveString.CompareTo("ReStartOk") == 0)
{
///重启恢复状态
label6.Text = " 运行中 ";
label8.BackColor = Color.Lime;
label6.BackColor = Color.Lime;
DialogResult dr = MessageBox.Show("启动成功", "状态", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
}
else
{
if(receiveString.IndexOf('_') != -1)
{
string[] str = receiveString.Split(new char[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
if(str[0] == "total")
{
textBox2.Text=str[1];
}
else if(str[0] == "totallast")
{
textBox1.Text=str[1];
}
else if(str[0] == "getsetting")
{
int index =0;
DialogSetup.cam_setting.ok = true;
DialogSetup.cam_setting.IsAutoOpenCam = Convert.ToBoolean(int.Parse(str[1]));
DialogSetup.cam_setting.IsAutoWork = Convert.ToBoolean(int.Parse(str[2]));
DialogSetup.cam_setting.saveimage = int.Parse(str[3]);
DialogSetup.cam_setting.shift_time = int.Parse(str[1]);
DialogSetup.cam_setting.shift_byhand = int.Parse(str[1]);
for (;index<NumberOfSupportedCameras;index++)
{
DialogSetup.cam_setting.camerasetting[index].expo = int.Parse(str[4+index*3]);
DialogSetup.cam_setting.camerasetting[index].gain = int.Parse(str[5+index*3]);
DialogSetup.cam_setting.camerasetting[index].filt = int.Parse(str[6+index*3]);
}
index = 4+NumberOfSupportedCameras*3;
DialogSetup.cam_setting.changeshift.Ah =int.Parse(str[index]);
DialogSetup.cam_setting.changeshift.Am =int.Parse(str[index+1]);
DialogSetup.cam_setting.changeshift.Bh =int.Parse(str[index+2]);
DialogSetup.cam_setting.changeshift.Bm =int.Parse(str[index+3]);
DialogSetup.cam_setting.changeshift.Ch =int.Parse(str[index+4]);
DialogSetup.cam_setting.changeshift.Cm =int.Parse(str[index+5]);
index = index +6;
for(int i =0;i<NumberOfSupportedCameras;i++)
{
DialogSetup.cam_setting.Cameraglue[i].shoot[0] = int.Parse(str[index + i*3]);
DialogSetup.cam_setting.Cameraglue[i].shoot[1] = int.Parse(str[index + i*3+1]);
DialogSetup.cam_setting.Cameraglue[i].shoot[2] = int.Parse(str[index + i*3+2]);
}
setup.UpdateDialog();
// setup.ShowDialog();
}
else if(str[0] == "alarm")
{
int alarm_code = int.Parse(str[1]);
if(alarm_code == 0)
{
label7.Text = "无报警";
}
if (alarm_code == 7)
{
label7.Text = "重连1#相机";
}
if (alarm_code == 8)
{
label7.Text = "重连2#相机";
}
if (alarm_code == 9)
{
label7.Text = "重连3#相机";
}
if (alarm_code == 10)
{
label7.Text = "重连4#相机";
}
if (alarm_code == 11)
{
label7.Text = "重连5#相机";
}
if (alarm_code == 12)
{
label7.Text = "重连6#相机";
}
if (alarm_code == 13)
{
label7.Text = "重连7#相机";
}
if (alarm_code == 14)
{
label7.Text = "重连8#相机";
}
}
}
}
}
}
catch
{
//SOCKETEventArrive("receive:Nullerror");
}
}
Thread.Sleep(10); //防止系统资源耗尽
}
}
delegate void CheckBox1Delegate(bool newPos);
private void CheckBox1Progress(bool newPos)
{
// 判断是否在线程中访问
if (!this.checkBox1.InvokeRequired)
{
// 不是的话直接操作控件
this.checkBox1.Checked = newPos;
}
else
{
// 是的话启用delegate访问
CheckBox1Delegate checkBox1Progress = new CheckBox1Delegate(CheckBox1Progress);
// 如使用Invoke会等到函数调用结束而BeginInvoke不会等待直接往后走
this.BeginInvoke(checkBox1Progress, new object[] { newPos });
}
}
delegate void CheckBox2Delegate(bool newPos);
private void CheckBox2Progress(bool newPos)
{
// 判断是否在线程中访问
if (!this.checkBox2.InvokeRequired)
{
// 不是的话直接操作控件
this.checkBox2.Checked = newPos;
}
else
{
// 是的话启用delegate访问
CheckBox2Delegate checkBox2Progress = new CheckBox2Delegate(CheckBox2Progress);
// 如使用Invoke会等到函数调用结束而BeginInvoke不会等待直接往后走
this.BeginInvoke(checkBox2Progress, new object[] { newPos });
}
}
public UserControl1()
{
InitializeComponent();
RecMsg();
if(status[0] == 1)
{
button3.BackgroundImage = WindowsFormsApp2.Properties.Resources._lock;
}
else
{
button3.BackgroundImage = WindowsFormsApp2.Properties.Resources.unlock;
}
if (status[1] == 1)
{
checkBox1.CheckState = CheckState.Checked;
}
else
{
checkBox1.CheckState = CheckState.Unchecked;
}
if (status[2] == 1)
{
checkBox2.CheckState = CheckState.Checked;
}
else
{
checkBox2.CheckState = CheckState.Unchecked;
}
}
private void button1_Click(object sender, EventArgs e)
{
SendMsg("START");
}
private void button2_Click(object sender, EventArgs e)
{
SendMsg("END");
}
private void button3_Click(object sender, EventArgs e)
{
SendMsg("LOCK");
}
private void button4_Click(object sender, EventArgs e)
{
SendMsg("GETSETTING");
}
private void button5_Click(object sender, EventArgs e)
{
SendMsg("CHANGESHIFT");
}
private void button6_Click(object sender, EventArgs e)
{
SendMsg("CLEARNIMAGE");
}
private void checkBox1_Click(object sender, EventArgs e)
{
if (this.checkBox1.Checked == true)
{
SendMsg("DEBUGEN");
this.checkBox1.Checked = false;
}
else
{
SendMsg("DEBUGDIS");
this.checkBox1.Checked = true;
}
}
private void checkBox2_Click(object sender, EventArgs e)
{
if (this.checkBox2.Checked == true)
{
SendMsg("UNKICK");
this.checkBox2.Checked = false;
}
else
{
SendMsg("KICK");
this.checkBox2.Checked = true;
}
}
public void closeSocket()
{
try
{
if(recvThread != null)
recvThread.Abort();
if(filerecvThread != null)
filerecvThread.Abort();
if(u != null)
u.Close();
if(fileu != null)
fileu.Close();
}
catch
{
//SOCKETEventArrive("receive:Nullerror");
}
}
private void ReSet_Click(object sender, EventArgs e)
{
SendMsg("RESET");
}
private void UserControl1_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Start();
}
private void label7_DoubleClick(object sender, EventArgs e)
{
SendMsg("CLEARALARM");
}
private void button7_Click(object sender, EventArgs e)
{
SendMsg("GETALARMRECORD");
var task = Task.Run(async delegate
{
await Task.Delay(500);
Alarm_Record Alarm = new Alarm_Record();
Alarm.UpdateDialog();
Alarm.StartPosition = FormStartPosition.CenterScreen;//中心
Alarm.TopMost = true;//置顶
Alarm.ShowDialog();
});
}
private void timer1_Tick(object sender, EventArgs e)
{
this.toolStripStatusLabel1.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
}
}
}