From e3f3f62315a5789cd812b96954b67bffda4a0f5a Mon Sep 17 00:00:00 2001
From: wei <kaede@kaededeMacBook-Air.local>
Date: 星期四, 01 四月 2021 16:33:55 +0800
Subject: [PATCH] 金辉项目,萤石摄像头更新
---
HDL_ON/DAL/DriverLayer/Control_TcpServer.cs | 359 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 359 insertions(+), 0 deletions(-)
diff --git a/HDL_ON/DAL/DriverLayer/Control_TcpServer.cs b/HDL_ON/DAL/DriverLayer/Control_TcpServer.cs
new file mode 100644
index 0000000..82a2caa
--- /dev/null
+++ b/HDL_ON/DAL/DriverLayer/Control_TcpServer.cs
@@ -0,0 +1,359 @@
+锘�/*
+ * 璇ョ被鐢ㄤ簬绠$悊tcp浣滀负鏈嶅姟绔繛鎺ラ�氳
+ */
+using System;
+using System.Collections.Generic;
+using System.Net.Sockets;
+using System.Threading;
+using System.Net;
+using Shared;
+using HDL_ON.Entity;
+
+namespace HDL_ON.DriverLayer
+{
+ /// <summary>
+ /// TCP鏈嶅姟绔�
+ /// </summary>
+ public class Control_TcpServer
+ {
+ private Socket ServerSocket = null;//鏈嶅姟绔�
+ public Dictionary<string, MySession> dic_ClientSocket = new Dictionary<string, MySession>();//tcp瀹㈡埛绔瓧鍏�
+ private Dictionary<string, Thread> dic_ClientThread = new Dictionary<string, Thread>();//绾跨▼瀛楀吀,姣忔柊澧炰竴涓繛鎺ュ氨娣诲姞涓�鏉$嚎绋�
+ private bool Flag_Listen = true;//鐩戝惉瀹㈡埛绔繛鎺ョ殑鏍囧織
+
+ /// <summary>
+ /// 鍚姩鏈嶅姟
+ /// </summary>
+ /// <param name="port">绔彛鍙�</param>
+ public bool OpenServer(int port = 8586)
+ {
+ try
+ {
+ Flag_Listen = true;
+ // 鍒涘缓璐熻矗鐩戝惉鐨勫鎺ュ瓧锛屾敞鎰忓叾涓殑鍙傛暟锛�
+ ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
+ // 鍒涘缓鍖呭惈ip鍜岀鍙e彿鐨勭綉缁滆妭鐐瑰璞★紱
+ IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, port);
+ try
+ {
+ // 灏嗚礋璐g洃鍚殑濂楁帴瀛楃粦瀹氬埌鍞竴鐨刬p鍜岀鍙d笂锛�
+ ServerSocket.Bind(endPoint);
+ }
+ catch
+ {
+ return false;
+ }
+ // 璁剧疆鐩戝惉闃熷垪鐨勯暱搴︼紱
+ ServerSocket.Listen(100);
+ // 鍒涘缓璐熻矗鐩戝惉鐨勭嚎绋嬶紱
+ Thread Thread_ServerListen = new Thread(ListenConnecting);
+ Thread_ServerListen.IsBackground = true;
+ Thread_ServerListen.Start();
+
+ MainPage.Log("鍚姩tcp渚﹀惉");
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+ /// <summary>
+ /// 鍏抽棴鏈嶅姟
+ /// </summary>
+ public void CloseServer()
+ {
+ lock (dic_ClientSocket)
+ {
+ foreach (var item in dic_ClientSocket)
+ {
+ item.Value.Close();//鍏抽棴姣忎竴涓繛鎺�
+ }
+ dic_ClientSocket.Clear();//娓呴櫎瀛楀吀
+ }
+ lock (dic_ClientThread)
+ {
+ foreach (var item in dic_ClientThread)
+ {
+ item.Value.Abort();//鍋滄绾跨▼
+ }
+ dic_ClientThread.Clear();
+ }
+ Flag_Listen = false;
+ //ServerSocket.Shutdown(SocketShutdown.Both);//鏈嶅姟绔笉鑳戒富鍔ㄥ叧闂繛鎺�,闇�瑕佹妸鐩戝惉鍒扮殑杩炴帴閫愪釜鍏抽棴
+ if (ServerSocket != null)
+ ServerSocket.Close();
+
+ }
+ /// <summary>
+ /// 鐩戝惉瀹㈡埛绔姹傜殑鏂规硶锛�
+ /// </summary>
+ private void ListenConnecting()
+ {
+ while (Flag_Listen) // 鎸佺画涓嶆柇鐨勭洃鍚鎴风鐨勮繛鎺ヨ姹傦紱
+ {
+ try
+ {
+ Socket sokConnection = ServerSocket.Accept(); // 涓�鏃︾洃鍚埌涓�涓鎴风鐨勮姹傦紝灏辫繑鍥炰竴涓笌璇ュ鎴风閫氫俊鐨� 濂楁帴瀛楋紱
+ // 灏嗕笌瀹㈡埛绔繛鎺ョ殑 濂楁帴瀛� 瀵硅薄娣诲姞鍒伴泦鍚堜腑锛�
+ string str_EndPoint = sokConnection.RemoteEndPoint.ToString();
+ MySession myTcpClient = new MySession() { TcpSocket = sokConnection };
+ //鍒涘缓绾跨▼鎺ユ敹鏁版嵁
+ Thread th_ReceiveData = new Thread(ReceiveData);
+ th_ReceiveData.IsBackground = true;
+ th_ReceiveData.Start(myTcpClient);
+ //鎶婄嚎绋嬪強瀹㈡埛杩炴帴鍔犲叆瀛楀吀
+ dic_ClientThread.Add(str_EndPoint, th_ReceiveData);
+ dic_ClientSocket.Add(str_EndPoint, myTcpClient);
+ }
+ catch
+ {
+
+ }
+ Thread.Sleep(200);
+ }
+ }
+ /// <summary>
+ /// 鎺ユ敹鏁版嵁
+ /// </summary>
+ /// <param name="sokConnectionparn"></param>
+ private void ReceiveData(object sokConnectionparn)
+ {
+ MySession tcpClient = sokConnectionparn as MySession;
+ Socket socketClient = tcpClient.TcpSocket;
+ bool Flag_Receive = true;
+
+ while (Flag_Receive)
+ {
+ try
+ {
+ // 瀹氫箟涓�涓�2M鐨勭紦瀛樺尯锛�
+ byte[] arrMsgRec = new byte[1024 * 1024 * 2];
+ // 灏嗘帴鍙楀埌鐨勬暟鎹瓨鍏ュ埌杈撳叆 arrMsgRec涓紱
+ int length = -1;
+ try
+ {
+ length = socketClient.Receive(arrMsgRec); // 鎺ユ敹鏁版嵁锛屽苟杩斿洖鏁版嵁鐨勯暱搴︼紱
+ }
+ catch (Exception ex)
+ {
+ MainPage.Log($"tcpListener error 1 : {ex.Message}");
+
+ Flag_Receive = false;
+ // 浠庨�氫俊绾跨▼闆嗗悎涓垹闄よ涓柇杩炴帴鐨勯�氫俊绾跨▼瀵硅薄锛�
+ string keystr = socketClient.RemoteEndPoint.ToString();
+ dic_ClientSocket.Remove(keystr);//鍒犻櫎瀹㈡埛绔瓧鍏镐腑璇ocket
+ dic_ClientThread[keystr].Abort();//鍏抽棴绾跨▼
+ dic_ClientThread.Remove(keystr);//鍒犻櫎瀛楀吀涓绾跨▼
+
+ tcpClient = null;
+ socketClient = null;
+ break;
+ }
+ byte[] buf = new byte[length];
+ Array.Copy(arrMsgRec, buf, length);
+ lock (tcpClient.m_Buffer)
+ {
+ //tcpClient.m_Buffer.Add(buf);
+ var tcpDataString = System.Text.Encoding.UTF8.GetString(arrMsgRec, 0, length);
+ if (!string.IsNullOrEmpty(tcpDataString))
+ {
+ AnalysisTcpData(socketClient, tcpDataString);
+ }
+
+ MainPage.Log(tcpDataString);
+ }
+ }
+ catch (Exception ex)
+ {
+ MainPage.Log($"tcpListener error 2 : {ex.Message}");
+ }
+ Thread.Sleep(100);
+ }
+ }
+ /// <summary>
+ /// 鑾峰彇鍒楄〃鏁版嵁鍥炶皟鏂规硶
+ /// </summary>
+ public Action<string> GetListResponseAction;
+ string tcpFunctionOidJsonString = "";
+ /// <summary>
+ /// 澶勭悊tcp鏁版嵁
+ /// </summary>
+ void AnalysisTcpData(Socket socket, string tcpBodyDataString)
+ {
+ MainPage.Log($"0001 tcpDataString:\r\n {tcpBodyDataString}");
+ var tcpDataObj = Control.Ins.AnalysisReceiveData(tcpBodyDataString);
+ if (tcpDataObj.BodyDataString == null)
+ {
+ return;
+ }
+ switch (tcpDataObj.Topic)
+ {
+ case CommunicationTopic.AddDeviceOids:
+ tcpFunctionOidJsonString = tcpDataObj.BodyDataString;
+ break;
+ case CommunicationTopic.AddFunctions:
+ var addSidFunction = Newtonsoft.Json.JsonConvert.DeserializeObject<TcpTransmissionSidsObj>(tcpDataObj.BodyDataString);
+ Application.RunOnMainThread(() =>
+ {
+ var tipDialog = new UI.UpdataSidDataDialog();
+ var tcpLocalFunction = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Function>>(Newtonsoft.Json.JsonConvert.SerializeObject(addSidFunction.objects));
+ tipDialog.ShowDialog(tcpLocalFunction, tcpFunctionOidJsonString);
+ });
+ break;
+ case CommunicationTopic.BusPcGetAppOids:
+
+ var bytes = Common.FileUtlis.Files.ReadFile(DB_ResidenceData.OidSavePathName);
+ if (bytes.Length > 0)
+ {
+ socket.Send(bytes);
+ MainPage.Log($"send oid list to 8586 prot :{ System.Text.Encoding.UTF8.GetString(bytes)}");
+ }
+ break;
+ case CommunicationTopic.BusPcGetAppSids:
+ var sendSidObj = new TcpTransmissionSidsObj();
+ sendSidObj.id = Control.Ins.msg_id.ToString();
+ sendSidObj.time_stamp = "";
+ //杞崲鎴恇us闇�瑕佺殑鏁版嵁鏍煎紡
+ var localFunction = FunctionList.List.GetDeviceFunctionList();
+ var localFunctionString = Newtonsoft.Json.JsonConvert.SerializeObject(localFunction);
+ var tcpFunction = Newtonsoft.Json.JsonConvert.DeserializeObject<List<BusSidObj>>(localFunctionString);
+
+ sendSidObj.objects.AddRange(tcpFunction);
+ var sendSidJson = Newtonsoft.Json.JsonConvert.SerializeObject(sendSidObj);
+ var sendSidBytes = System.Text.Encoding.UTF8.GetBytes(sendSidJson);
+ socket.Send(sendSidBytes, sendSidBytes.Length, SocketFlags.OutOfBand);
+ MainPage.Log($"send sid list to 8586 prot :{sendSidJson}");
+ break;
+ }
+ }
+
+
+ /// <summary>
+ /// 鍙戦�佹暟鎹粰鎸囧畾鐨勫鎴风
+ /// </summary>
+ /// <param name="_endPoint">瀹㈡埛绔鎺ュ瓧</param>
+ /// <param name="_buf">鍙戦�佺殑鏁扮粍</param>
+ /// <returns></returns>
+ public bool SendData(string _endPoint, byte[] _buf)
+ {
+ MySession myT = new MySession();
+ if (dic_ClientSocket.TryGetValue(_endPoint, out myT))
+ {
+ myT.Send(_buf);
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+
+ /// <summary>
+ /// 浼氳瘽绔�
+ /// </summary>
+ public class MySession
+ {
+ public Socket TcpSocket;//socket瀵硅薄
+ public List<byte> m_Buffer = new List<byte>();//鏁版嵁缂撳瓨鍖�
+
+ public MySession()
+ {
+
+ }
+
+ /// <summary>
+ /// 鍙戦�佹暟鎹�
+ /// </summary>
+ /// <param name="buf"></param>
+ public void Send(byte[] buf)
+ {
+ if (buf != null)
+ {
+ TcpSocket.Send(buf);
+ }
+ }
+ /// <summary>
+ /// 鑾峰彇杩炴帴鐨刬p
+ /// </summary>
+ /// <returns></returns>
+ public string GetIp()
+ {
+ IPEndPoint clientipe = (IPEndPoint)TcpSocket.RemoteEndPoint;
+ string _ip = clientipe.Address.ToString();
+ return _ip;
+ }
+ /// <summary>
+ /// 鍏抽棴杩炴帴
+ /// </summary>
+ public void Close()
+ {
+ TcpSocket.Shutdown(SocketShutdown.Both);
+ }
+ /// <summary>
+ /// 鎻愬彇姝g‘鏁版嵁鍖�
+ /// </summary>
+ public byte[] GetBuffer(int startIndex, int size)
+ {
+ byte[] buf = new byte[size];
+ m_Buffer.CopyTo(startIndex, buf, 0, size);
+ m_Buffer.RemoveRange(0, startIndex + size);
+ return buf;
+ }
+
+ /// <summary>
+ /// 娣诲姞闃熷垪鏁版嵁
+ /// </summary>
+ /// <param name="buffer"></param>
+ public void AddQueue(byte[] buffer)
+ {
+ m_Buffer.AddRange(buffer);
+ }
+ /// <summary>
+ /// 娓呴櫎缂撳瓨
+ /// </summary>
+ public void ClearQueue()
+ {
+ m_Buffer.Clear();
+ }
+ }
+
+ /// <summary>
+ /// bus杞欢浼犺緭鍔熻兘鐨剆id鍒楄〃瀵硅薄
+ /// </summary>
+ public class TcpTransmissionSidsObj
+ {
+ public List<BusSidObj> objects = new List<BusSidObj>();
+
+ public string id = "";
+ public string time_stamp = "";
+
+ }
+
+ /// <summary>
+ /// bus杞欢浼犺緭鐨勫姛鑳藉璞�
+ /// 浠呭湪涓巄us杞欢浼犻�掓暟鎹椂浣跨敤锛�
+ /// Function瀵硅薄鍖呭惈浜嗚瀵硅薄鐨勬墍鏈夊唴瀹癸紝
+ /// 姝ゅ璞′负浜嗗噺灏戜紶杈撴暟鎹娇鐢�
+ /// </summary>
+ public class BusSidObj
+ {
+ public string sid = "";// "0301011234567801012301230123";
+
+ /// <summary>
+ /// A鍗忚鍔熻兘鐨勫睘鎬�
+ /// 濡傦細鏄疉C鍔熻兘锛氬睘鎬э細on_off,mode,fan,temperature
+ /// </summary>
+ public List<FunctionAttributes> attributes = new List<FunctionAttributes>();
+
+ /// <summary>
+ /// 澶囨敞
+ /// </summary>
+ public string name;
+
+ public string omodel;
+
+ }
+
+}
\ No newline at end of file
--
Gitblit v1.8.0