📄 workfrm.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.WindowsMobile.PocketOutlook;
using System.IO;
namespace MobileClient_CS
{
public partial class WorkFrm : Form
{
//const int MAXBUFFERLEN = 1024;
//private ManualResetEvent connectDone = new ManualResetEvent(false);
//private Socket ClientSocket = null;
private const int MAXBUFFERLEN = 1024;
private const Byte USER_CODE = 0x01;
public bool m_Processing;
private ManualResetEvent connectDone = new ManualResetEvent(false);
private SmsMessage sms = new SmsMessage();
private string m_PhoneNumber;
private System.Int32 m_TimeOut;
private const string FILE_NAME = "phonenumber.txt";
public WorkFrm()
{
InitializeComponent();
m_Processing = false;
m_TimeOut = 2000;
GetPhoneNumber();
}
private void GetPhoneNumber()
{
if (!File.Exists(FILE_NAME))
{
MessageBox.Show("电话号码文件丢失,或错误");
m_PhoneNumber = "15910650074";
return;
}
using (StreamReader sr = File.OpenText(FILE_NAME))
{
String input;
while ((input = sr.ReadLine()) != null)
{
m_PhoneNumber = input;
}
sr.Close();
}
}
private void ConnectCallback(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
client.EndConnect(ar);
}
catch (Exception err)
{
}
finally
{
connectDone.Set();
}
}
private void menuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void AllLightOn()
{
if (m_Processing)
return;
m_Processing = true;
ProcessingFrm notice = new ProcessingFrm(this);
notice.Show();
notice.Update();
try
{
TcpClient normalClient = new TcpClient();
NetworkStream normalStream;
IPAddress ipAddress = IPAddress.Parse("192.168.1.125");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 50000);
connectDone.Reset();
normalClient.Client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), normalClient);
connectDone.WaitOne(m_TimeOut, false);
if (!normalClient.Client.Connected)
{
notice.SetLable("连接失败,请检查服务器");
Thread.Sleep(2000);
if (normalClient.Client.Connected == true)
{
normalClient.Client.Shutdown(SocketShutdown.Both);
normalClient.Client.Close();
}
}
else
{
Byte[] SendData = new Byte[5];
SendData[0] = (Byte)MOBILE_PC_MESSAGE_TYPE.MPMT_ALL_LIGHT_ON_REQUEST;
SendData[1] = 0x00;
SendData[2] = SendData[3] = SendData[4] = 0;
normalStream = normalClient.GetStream();
normalStream.Write(SendData, 0, SendData.Length);
int bufferSize = normalClient.ReceiveBufferSize;
Byte[] RecvData = new Byte[bufferSize];
normalStream.Read(RecvData, 0, bufferSize);
if (RecvData[0] == (Byte)MOBILE_PC_MESSAGE_TYPE.MPMT_EQUIPMENT_SET_RESPONSE)
{
}
if (normalClient.Client.Connected == true)
{
normalClient.Client.Shutdown(SocketShutdown.Both);
normalClient.Client.Close();
}
Thread.Sleep(10000);
}
}
catch (Exception err)
{
}
notice.Close();
notice.Dispose();
m_Processing = false;
}
private void AllLightOff()
{
if (m_Processing)
return;
m_Processing = true;
ProcessingFrm notice = new ProcessingFrm(this);
notice.Show();
notice.Update();
try
{
TcpClient normalClient = new TcpClient();
NetworkStream normalStream;
IPAddress ipAddress = IPAddress.Parse("192.168.1.125");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 50000);
connectDone.Reset();
normalClient.Client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), normalClient);
connectDone.WaitOne(m_TimeOut, false);
if (!normalClient.Client.Connected)
{
notice.SetLable("连接失败,请检查服务器");
Thread.Sleep(2000);
if (normalClient.Client.Connected == true)
{
normalClient.Client.Shutdown(SocketShutdown.Both);
normalClient.Client.Close();
}
}
else
{
Byte[] SendData = new Byte[7];
SendData[0] = (Byte)MOBILE_PC_MESSAGE_TYPE.MPMT_ALL_LIGHT_OFF_REQUEST;
SendData[1] = 0x00;
SendData[2] = SendData[3] = SendData[4] = 0;
normalStream = normalClient.GetStream();
normalStream.Write(SendData, 0, SendData.Length);
int bufferSize = normalClient.ReceiveBufferSize;
Byte[] RecvData = new Byte[bufferSize];
normalStream.Read(RecvData, 0, bufferSize);
if (RecvData[0] == (Byte)MOBILE_PC_MESSAGE_TYPE.MPMT_EQUIPMENT_SET_RESPONSE)
{
}
if (normalClient.Client.Connected == true)
{
normalClient.Client.Shutdown(SocketShutdown.Both);
normalClient.Client.Close();
}
Thread.Sleep(10000);
}
}
catch (Exception err)
{
}
notice.Close();
notice.Dispose();
m_Processing = false;
}
private void DoorAccessKeyDown(Byte eqID)
{
if (m_Processing)
return;
m_Processing = true;
ProcessingFrm notice = new ProcessingFrm(this);
notice.Show();
notice.Update();
NoticeSupervision();
NoticeAMCapServer();
CoreSender(eqID, notice);
Thread.Sleep(3000);
/* CoreSender(eqID, notice);*/
notice.Close();
notice.Dispose();
m_Processing = false;
}
private void NoticeSupervision()
{
sms.Body = "GZ;GET";
sms.To.Add(new Recipient(m_PhoneNumber));
sms.RequestDeliveryReport = false;
try
{
sms.Send();
}
catch (Exception err)
{
}
}
private void NoticeAMCapServer()
{
try
{
TcpClient normalClient = new TcpClient();
NetworkStream normalStream;
IPAddress ipAddress = IPAddress.Parse("192.168.1.103");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 20000);
connectDone.Reset();
normalClient.Client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), normalClient);
connectDone.WaitOne(m_TimeOut, false);
if (!normalClient.Client.Connected)
{
if (normalClient.Client.Connected == true)
{
normalClient.Client.Shutdown(SocketShutdown.Both);
normalClient.Client.Close();
}
}
else
{
Byte[] SendData = new Byte[7];
SendData[0] = (Byte)MOBILE_PC_MESSAGE_TYPE.MPMT_ACCESS_CONTROL_REQUEST;
SendData[1] = 0x00;
SendData[2] = SendData[3] = SendData[4] = 0;
normalStream = normalClient.GetStream();
normalStream.Write(SendData, 0, SendData.Length);
if (normalClient.Client.Connected == true)
{
normalClient.Client.Shutdown(SocketShutdown.Both);
normalClient.Client.Close();
}
}
}
catch (Exception err)
{
}
}
private void CoreSender(Byte eqID, ProcessingFrm frm)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -