387 lines
11 KiB
C#
387 lines
11 KiB
C#
using Netly;
|
||
using Netly.Core;
|
||
using Newtonsoft.Json;
|
||
using System;
|
||
using System.IO;
|
||
using System.Text;
|
||
using UnityEngine;
|
||
using UnityEngine.XR;
|
||
|
||
public class VR1 : MonoBehaviour
|
||
{
|
||
TcpClient client;
|
||
|
||
bool isConnecting;
|
||
int wearingState;
|
||
|
||
DateTime connTime, dateTime;
|
||
|
||
Register register = new Register();
|
||
VRInfo vrinfo = new VRInfo();
|
||
Logout logout = new Logout();
|
||
BackInfo backInfo = new BackInfo();
|
||
|
||
public UploadInfoToWeb uploadInfoToWeb;
|
||
|
||
DateTime begin_at;
|
||
string app;
|
||
void Awake()
|
||
{
|
||
ShowLog(Application.persistentDataPath);
|
||
CreateDirectory();
|
||
}
|
||
void Start()
|
||
{
|
||
if (PlayerPrefs.GetInt("group") == 0)
|
||
{
|
||
PlayerPrefs.SetInt("group", 1);
|
||
}
|
||
Invoke("Init", 0.5f);
|
||
}
|
||
|
||
void Init()
|
||
{
|
||
ReadConfig();
|
||
|
||
connTime = DateTime.Now;
|
||
dateTime = DateTime.Now.AddSeconds(-25);
|
||
|
||
if (Application.isEditor)
|
||
{
|
||
Config.SN = "PA9410PGJ6170013H";
|
||
}
|
||
else
|
||
{
|
||
Config.SN = PicoAPI._Instance.PicoSN();
|
||
}
|
||
|
||
client = new TcpClient(framing: false);
|
||
client.OnOpen(open);
|
||
client.OnClose(close);
|
||
client.OnError(error);
|
||
client.OnEvent(msgEvent);
|
||
client.OnData(msgData);
|
||
}
|
||
void ReadConfig()
|
||
{
|
||
if (File.Exists(Config.IpConfig))
|
||
{
|
||
string[] str = File.ReadAllText(Config.IpConfig).Split('\n');
|
||
Config.TcpHost = str[0];
|
||
WebNet.webIp = str[1];
|
||
|
||
WebNet.assetsUrl = $"{WebNet.webIp}/api/dev/login";
|
||
WebNet.DownAssetsUrl = $"{WebNet.webIp}/storage/resource";
|
||
WebNet.UploadInfoUrl = $"{WebNet.webIp}/api/dev/uploadUsageInfo";
|
||
WebNet.getDeviceList = $"{WebNet.webIp}/api/tablet/getDeviceList";
|
||
WebNet.uploadEQ = $"{WebNet.webIp}/api/dev/uploadEQ";
|
||
}
|
||
else
|
||
{
|
||
string str = $"{Config.TcpHost}\n{WebNet.webIp}";
|
||
File.WriteAllText(Config.IpConfig, str);
|
||
}
|
||
|
||
ShowLog($"webIp<49><70>{WebNet.webIp}");
|
||
}
|
||
|
||
/// <summary>
|
||
/// <20><><EFBFBD><EFBFBD>Ŀ¼
|
||
/// </summary>
|
||
void CreateDirectory()
|
||
{
|
||
GameTools.CreateDirectory(Config.AppPath);
|
||
GameTools.CreateDirectory(Config.AppIconPath);
|
||
GameTools.CreateDirectory(Config.VideoPath);
|
||
GameTools.CreateDirectory(Config.VideoIconPath);
|
||
GameTools.CreateDirectory(Config.ImagePath);
|
||
GameTools.CreateDirectory(Config.FilePath);
|
||
GameTools.CreateDirectory(Config.MapPath);
|
||
GameTools.CreateDirectory(Config.PCappPath);
|
||
GameTools.CreateDirectory(Config.PCappIconPath);
|
||
GameTools.CreateDirectory(Config.OtherPath);
|
||
GameTools.CreateDirectory(Config.AssetsInfoPath);
|
||
GameTools.CreateDirectory(Config.UseRecordPath);
|
||
}
|
||
|
||
private void OnApplicationFocus(bool focus)
|
||
{
|
||
if (focus && isConnecting)
|
||
{
|
||
sendLogout();
|
||
try
|
||
{
|
||
if (!string.IsNullOrEmpty(app))
|
||
{
|
||
//uploadInfoToWeb.UploadPlayInfo(app, Config.SN, begin_at, 1);
|
||
app = string.Empty;
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
ShowLog($"<22>ϴ<EFBFBD><CFB4><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>:{e.Message}");
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
void Update()
|
||
{
|
||
|
||
//ͷ<><CDB7><EFBFBD>仯
|
||
if (Application.platform == RuntimePlatform.Android)
|
||
{
|
||
if (InputDevices.GetDeviceAtXRNode(XRNode.Head).TryGetFeatureValue(CommonUsages.userPresence, out bool userPresence))
|
||
{
|
||
if (userPresence)
|
||
{
|
||
wearingState = 1;
|
||
}
|
||
else
|
||
{
|
||
wearingState = 0;
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
//<2F><><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD>
|
||
if (client != null)
|
||
{
|
||
if (!isConnecting)
|
||
{
|
||
if (DateTime.Now > connTime.AddSeconds(2))
|
||
{
|
||
ShowLog("<22><><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD>");
|
||
connTime = DateTime.Now;
|
||
ReadConfig();
|
||
client.Open(new Host(Config.TcpHost, Config.TcpPort));
|
||
ShowLog($"<22><><EFBFBD>ӵ<EFBFBD>ip<69><70>ַ<EFBFBD><D6B7>{Config.TcpHost}");
|
||
Invoke("sendRegister", 1f);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
if (DateTime.Now > dateTime.AddSeconds(2))
|
||
{
|
||
dateTime = DateTime.Now;
|
||
sendvrInfo(Config.SN, "u", int.Parse(PicoAPI._Instance.PicoPower()), 1, wearingState);
|
||
//sendvrInfo(Config.SN, "u", 100, 1, 1);
|
||
ShowLog("<22><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8>Ϣ<EFBFBD><CFA2>");
|
||
}
|
||
}
|
||
}
|
||
|
||
if (InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.primaryButton, out bool ispri))
|
||
{
|
||
if (ispri)
|
||
{
|
||
if (InputDevices.GetDeviceAtXRNode(XRNode.RightHand).TryGetFeatureValue(CommonUsages.triggerButton, out bool islogout))
|
||
{
|
||
if (islogout)
|
||
{
|
||
ShowLog("ע<><D7A2>");
|
||
sendLogout();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
#region <EFBFBD><EFBFBD>ӡ<EFBFBD><EFBFBD>־
|
||
public void ShowLog(string str)
|
||
{
|
||
Debug.Log(str);
|
||
}
|
||
#endregion
|
||
|
||
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
void sendMessage<T>(uint type, T msg)
|
||
{
|
||
try
|
||
{
|
||
// <20><><EFBFBD>л<EFBFBD><D0BB><EFBFBD>Ϣ
|
||
string json = JsonConvert.SerializeObject(msg);
|
||
Debug.Log($"<22><><EFBFBD>͵<EFBFBD>json: {json}");
|
||
byte[] msgBytes = Encoding.UTF8.GetBytes(json);
|
||
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD><DCB3>ȣ<EFBFBD><C8A3><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD> + <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD> + <20><>β<EFBFBD><CEB2>0<EFBFBD><30>
|
||
int totalLength = sizeof(uint) + msgBytes.Length + 1; // <20><><EFBFBD>ͳ<EFBFBD><CDB3><EFBFBD> + <20><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD> + 1<><31><EFBFBD><EFBFBD>β<EFBFBD><CEB2>0<EFBFBD><30>
|
||
|
||
// ʹ<><CAB9> MemoryStream <20><> BinaryWriter <20><><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD>
|
||
using (var stream = new MemoryStream())
|
||
using (var writer = new BinaryWriter(stream))
|
||
{
|
||
writer.Write(totalLength); // д<><D0B4><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD>
|
||
writer.Write(type); // д<><D0B4><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
|
||
writer.Write(msgBytes); // д<><D0B4><EFBFBD><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
|
||
writer.Write((byte)0); // д<><D0B4><EFBFBD><EFBFBD>β<EFBFBD><CEB2>0
|
||
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
client.ToData(stream.ToArray());
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣʧ<CFA2><CAA7>: " + ex.Message);
|
||
}
|
||
}
|
||
//
|
||
void open()
|
||
{
|
||
isConnecting = true;
|
||
ShowLog("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||
}
|
||
void close()
|
||
{
|
||
isConnecting = false;
|
||
ShowLog("<22>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD>");
|
||
}
|
||
void error(Exception e)
|
||
{
|
||
ShowLog($"<22><><EFBFBD>Ӵ<EFBFBD><D3B4><EFBFBD>:{e.Message}");
|
||
}
|
||
void msgEvent(string name, byte[] bytes)
|
||
{
|
||
string msg = NE.GetString(bytes);
|
||
|
||
}
|
||
void msgData(byte[] bytes)
|
||
{
|
||
try
|
||
{
|
||
if (bytes == null || bytes.Length < 8)
|
||
{
|
||
Debug.LogError("<22><>Ч<EFBFBD><D0A7><EFBFBD>ݰ<EFBFBD><DDB0><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ȳ<EFBFBD><C8B2><EFBFBD>");
|
||
return;
|
||
}
|
||
|
||
int length = BitConverter.ToInt32(bytes, 0);
|
||
int type = BitConverter.ToInt32(bytes, 4);
|
||
|
||
if (bytes.Length < length)
|
||
{
|
||
Debug.LogError($"<22><><EFBFBD>ݲ<EFBFBD><DDB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԥ<EFBFBD>ڳ<EFBFBD><DAB3><EFBFBD>: {length}, ʵ<>ʳ<EFBFBD><CAB3><EFBFBD>: {bytes.Length}");
|
||
return;
|
||
}
|
||
|
||
if (bytes.Length > 9)
|
||
{
|
||
int msgLength = length - 4 - 1; // <20><><EFBFBD><EFBFBD>һλ<D2BB><CEBB><EFBFBD>ֽ<EFBFBD>0
|
||
Memory<byte> msgMemory = new Memory<byte>(bytes, 8, msgLength);
|
||
string msg = Encoding.UTF8.GetString(msgMemory.Span);
|
||
Debug.LogError($"<22>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>Ϣ:{msg}");
|
||
|
||
switch (type)
|
||
{
|
||
case 12:
|
||
VRControl vrControl = JsonConvert.DeserializeObject<VRControl>(msg);
|
||
if (string.Equals(vrControl.sn, Config.SN))
|
||
{
|
||
switch (vrControl.com)
|
||
{
|
||
case 1:
|
||
PicoAPI._Instance.ShutDown();
|
||
break;
|
||
case 2:
|
||
PicoAPI._Instance.Reboot();
|
||
break;
|
||
case 3:
|
||
PicoAPI._Instance.SetVolumeNum(vrControl.volume);
|
||
break;
|
||
case 4:
|
||
float ipd = (float)vrControl.volume / 100;
|
||
PicoAPI._Instance.SetIpd(ipd);
|
||
break;
|
||
|
||
}
|
||
}
|
||
break;
|
||
case 3:
|
||
Pad1GroupInfo pad1GroupInfo = JsonConvert.DeserializeObject<Pad1GroupInfo>(msg);
|
||
foreach (var item in pad1GroupInfo.vrs)
|
||
{
|
||
if (string.Equals(item.sn, Config.SN))
|
||
{
|
||
Config.Group = pad1GroupInfo.group;
|
||
PlayerPrefs.SetInt("group", (int)Config.Group);
|
||
try
|
||
{
|
||
if (uploadInfoToWeb)
|
||
{
|
||
app = pad1GroupInfo.app;
|
||
begin_at = DateTime.Now;
|
||
//uploadInfoToWeb.UploadPlayInfo(pad1GroupInfo.app, Config.SN, begin_at, 1);
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
ShowLog($"<22>ϴ<EFBFBD><CFB4><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>:{e.Message}");
|
||
}
|
||
PicoAPI._Instance.OpenApp(pad1GroupInfo.apk);
|
||
break;
|
||
}
|
||
}
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"<22><>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD>쳣: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region <EFBFBD><EFBFBD>Ϣ
|
||
|
||
/// <summary>
|
||
/// ע<><D7A2>
|
||
/// </summary>
|
||
void sendRegister()
|
||
{
|
||
register.type = 1;
|
||
register.user = "VR1";
|
||
register.sn = Config.SN;
|
||
sendMessage(1, register);
|
||
}
|
||
|
||
/// <summary>
|
||
/// ע<><D7A2>
|
||
/// </summary>
|
||
void sendLogout()
|
||
{
|
||
logout.group = Config.Group = (uint)PlayerPrefs.GetInt("group");
|
||
logout.sn = Config.SN;
|
||
sendMessage(6, logout);
|
||
}
|
||
/// <summary>
|
||
/// <20><><EFBFBD><EFBFBD>vr<76>豸<EFBFBD><E8B1B8>Ϣ
|
||
/// </summary>
|
||
void sendvrInfo(string sn, string name, int power, int status, int wear)
|
||
{
|
||
vrinfo.sn = sn;
|
||
vrinfo.user = name;
|
||
vrinfo.human = 0;
|
||
vrinfo.power = power;
|
||
vrinfo.status = status;
|
||
vrinfo.wear = wear;
|
||
sendMessage(2, vrinfo);
|
||
}
|
||
|
||
/// <summary>
|
||
/// <20>ظ<EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>Ϣ
|
||
/// </summary>
|
||
void sendBackInfo()
|
||
{
|
||
backInfo.sn = Config.SN;
|
||
backInfo.url = PicoAPI._Instance.GetCastUrl();
|
||
sendMessage(14, backInfo);
|
||
}
|
||
#endregion
|
||
|
||
}
|