Skip to content

Commit

Permalink
增加展厅项目相关代码
Browse files Browse the repository at this point in the history
  • Loading branch information
RSJWY committed Mar 7, 2024
1 parent 0a67408 commit 4f25953
Show file tree
Hide file tree
Showing 6 changed files with 818 additions and 0 deletions.
5 changes: 5 additions & 0 deletions API网站/Vuforia.url
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[{000214A0-0000-0000-C000-000000000046}]
Prop3=19,11
[InternetShortcut]
IDList=
URL=https://library.vuforia.com/sites/default/files/references/unity/index.html
63 changes: 63 additions & 0 deletions 展厅相关代码/UDP监听-支持UTF8&HEX/UDPController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Net;
using UnityEngine;

public class UDPController : MonoBehaviour
{
static UDPController _instance;
public static UDPController instance { get { return _instance; } }

string commandmsg;

public Action<string> MsgActionEvent;

private void Awake()
{
_instance = this;
}

private void Update()
{
UDPService.instance.UnityUpdate();
//取出数据
commandmsg = UDPService.instance.GetMsgData();
if (commandmsg != null)
{
//有数据,执行事件
MsgActionEvent?.Invoke(commandmsg);
}
}

public void Init(string ip, int port)
{
string lowerip= ip.ToLower();
//检查是不是监听全部IP
if (lowerip != "any")
{
//指定IP
//检查IP和Port是否合法
if (MyTool.MatchIP(ip) && MyTool.MatchPort(port))
{
UDPService.instance.Init(ip, port);
return;
}
}
else
{
//监听全部IP
//检查Port是否合法
if (MyTool.MatchPort(port))
{
UDPService.instance.Init(IPAddress.Any, port);
return;
}
}
//全部错误则使用默认参数
UDPService.instance.Init(IPAddress.Any, 56688);
}

private void OnDestroy()
{
UDPService.instance.Close();
}
}
276 changes: 276 additions & 0 deletions 展厅相关代码/UDP监听-支持UTF8&HEX/UDPService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using UnityEngine;

internal class UDPService
{
static object tihslock=new();
static UDPService _instance;
internal static UDPService instance
{
get
{
if (_instance == null)
{
lock (tihslock)
{
if (_instance == null)
{
_instance = new();
}
}
}
return _instance;
}
}


/// <summary>
/// 监听端口
/// </summary>
int port = 6003;
/// <summary>
/// 监听IP
/// </summary>
IPAddress ip = IPAddress.Any;
/// <summary>
/// UDP Socket
/// </summary>
Socket udpClient;
/// <summary>
/// 存储消息来源客户端信息
/// </summary>
EndPoint clientEndPoint;
/// <summary>
/// 存储接收到的数据
/// </summary>
byte[] ReceiveData = new byte[1024];
/// <summary>
/// 接收到的消息数组队列
/// </summary>
ConcurrentQueue<byte[]> reciveByteMsgQueue = new ConcurrentQueue<byte[]>();

/// <summary>
/// 处理完毕的待执行的消息队列
/// </summary>
ConcurrentQueue<string> MsgQueue = new();
/// <summary>
/// 消息发送队列
/// </summary>
ConcurrentQueue<byte[]> SendMsgQueue = new ConcurrentQueue<byte[]>();
/// <summary>
/// 通知多线程自己跳出
/// </summary>
bool isThreadOver;
/// <summary>
/// 是否初始化过
/// </summary>
bool isInit;
/// <summary>
/// 消息处理线程
/// </summary>
Thread MsgThread;
/// <summary>
/// 消息发送线程
/// </summary>
Thread sendMsgThread;

/// <summary>
/// 传入指定IP
/// </summary>
/// <param name="_ip"></param>
/// <param name="_port"></param>
internal void Init(string _ip, int _port)
{
ip = IPAddress.Parse(_ip);
port = _port;
Init();
}
/// <summary>
/// 使用设置好的IPAddress
/// </summary>
/// <param name="_ipAddress"></param>
/// <param name="_port"></param>
internal void Init(IPAddress _ipAddress, int _port)
{
ip = _ipAddress;
port = _port;
Init();
}

/// <summary>
/// 初始化
/// </summary>
/// <param name="_port"></param>
void Init()
{
if (isInit)
{
return;
}

try
{
isThreadOver = false;
udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

//配置监听
IPEndPoint ipendpoint = new IPEndPoint(ip, port);
udpClient.Bind(ipendpoint);


clientEndPoint = new IPEndPoint(IPAddress.Any, 0);
//开启异步监听
udpClient.BeginReceiveFrom(ReceiveData, 0, ReceiveData.Length,
SocketFlags.None, ref clientEndPoint, ReceiveMessageToQueue, clientEndPoint);

MsgThread = new Thread(Msghandle);
MsgThread.IsBackground = true;//后台运行
MsgThread.Start();
//sendMsgThread = new Thread(SendMsgHandle);
//sendMsgThread.IsBackground = true;//后台运行
//sendMsgThread.Start();

isInit = true;
Debug.Log($"UDP启动监听{udpClient.LocalEndPoint.ToString()}成功");
}
catch (Exception e)
{
Debug.LogError($"UDP启动监听 {udpClient.LocalEndPoint.ToString()} 失败!!错误信息:\n {e.ToString()}");
isThreadOver = true;
}
}

/// <summary>
/// 接收消息并存放
/// </summary>
/// <param name="iar"></param>
void ReceiveMessageToQueue(IAsyncResult iar)
{
if (isThreadOver)
{
return;
}
try
{
int receiveDataLength = udpClient.EndReceiveFrom(iar, ref clientEndPoint);
//如果有数据
if (receiveDataLength > 0)
{
byte[] data = new byte[receiveDataLength];
Buffer.BlockCopy(ReceiveData, 0, data, 0, receiveDataLength);
reciveByteMsgQueue.Enqueue(data);
}
}
catch (Exception e)
{
Debug.LogError($"UDP启动监听 {udpClient.LocalEndPoint.ToString()} 失败!!错误信息:\n {e.ToString()}");
}
finally
{
//继续监听
udpClient.BeginReceiveFrom(ReceiveData, 0, ReceiveData.Length,
SocketFlags.None, ref clientEndPoint, ReceiveMessageToQueue, clientEndPoint);
}
}
/// <summary>
/// 关闭监听
/// </summary>
public void Close()
{
isThreadOver = true;
if (udpClient != null)
{
udpClient.Close();
}
isInit = false;
}
/// <summary>
/// 数据处理
/// </summary>
internal void Msghandle()
{
while (true)
{
if (isThreadOver)
{
return;
}
if (!reciveByteMsgQueue.IsEmpty)
{
//取出数据
byte[] _data;
reciveByteMsgQueue.TryDequeue(out _data);
//处理数据,分别尝试不同协议
//string _strASCII = Encoding.ASCII.GetString(_data);
string _strUTF8 = Encoding.UTF8.GetString(_data);
string _strHex = BitConverter.ToString(_data);

string _utf8 = _strUTF8.Replace(" ", "");
string _hex = _strHex.Replace("-", " ");
//Debug.Log($"UTF8:{IsHex(_utf8)},HEX:{IsHex(_hex)}");
//优先检查UTF8
if (IsHex(_utf8))
{
//UTF8是正确的指令
MsgQueue.Enqueue(_strUTF8);
}
else
{
//Hex任何时刻都是16进值,则传出,交给使用者判断
MsgQueue.Enqueue(_hex);
}
}
}
}
//取出接收到的指令列信息
internal string GetMsgData()
{
if (MsgQueue.Count <= 0)
{
return null;
}
string msg;
MsgQueue.TryDequeue(out msg);
return msg;
}
//unityupdate调用,检测部分线程是否异常退出
internal void UnityUpdate()
{
//线程是否出错
if (MsgThread != null)
{
if (MsgThread.IsAlive == false && isInit == true)
{
//重新创建本线程
MsgThread = new Thread(Msghandle);
MsgThread.IsBackground = true;//后台运行
MsgThread.Start();
}
}
}
void SendMsgHandle()
{
if (isThreadOver)
{
return;
}
}


public static bool IsHex(string input)
{
// 正则表达式匹配16进制值
string hexPattern = @"^[0-9A-Fa-f]+$";
return Regex.IsMatch(input, hexPattern);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using Leap.Unity;
using UnityEngine;

public class LeapMotionPrefab : MonoBehaviour
{
/// <summary>
/// 手部模型
/// </summary>
public GameObject handModle;
/// <summary>
/// leapmotion设备连接器
/// </summary>
public LeapServiceProvider leapmotionServer;
/// <summary>
/// 握拳手势数据
/// </summary>
public HandPoseDetector woQuanPose;
/// <summary>
/// 翻书手势数据
/// </summary>
public HandPoseDetector fanShuPose;

private void Awake() {
leapmotionServer.OnDeviceSafe+=(device)=>{
Debug.Log($"连接到设备\n设备ID:{device.DeviceID}\n序列号:{device.SerialNumber}\n设备型号:{device.Type}");
};
}
}
Loading

0 comments on commit 4f25953

Please sign in to comment.