NewN_UAVPlane/Assets/Zion/Scripts/manage/SoftManage.cs

196 lines
7.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Text;
using System.Linq;
public class SoftManage : MonoBehaviour
{
public static SoftManage Instance;
public string softName = "车控软件";
Dictionary<string, string> subjectDic = new Dictionary<string, string> {};
private Action<string, string> softAction;
/// <summary>
/// 配置文件 科目=软件&岗位,软件&岗位
/// </summary>
private Dictionary<string,List<string[]>> softseting;
//public List<MessageModel> Soft = new List<MessageModel>();//操作软件通信
private void Awake()
{
Instance = this;
subjectDic.Add("转载下车前准备", "3cbb773a-3cb3-43bb-9aa1-8e3bb00c8aeb");
subjectDic.Add("转载下车后撤收", "657e1b34-7a74-401d-a5e6-efcb0275e520");
subjectDic.Add("转载上车前准备", "672b234e-9eee-4de7-989b-3a1df41eb05e");
subjectDic.Add("转载上车后撤收", "1461f02f-d436-4e42-b8ca-6587c5c38f49");
//读配置文件
StartCoroutine(MyNetMQClient.CallGet("http://"+ MyNetMQClient.CallIP + "/3DConfig/SoftSetting.txt", str =>
{
var tmps=str.Split(new char[] {'\r','\n' }, StringSplitOptions.RemoveEmptyEntries).ToList();
var tmpdic =new Dictionary<string, List<string[]>>();
tmps.ForEach(a=>
{
string[] p=a.Split('=');
string[] datas=p[1].Split(',');
List<string[]> ruanjianlist = new List<string[]>();
for (int i = 0; i < datas.Length; i++)
{
ruanjianlist.Add(datas[i].Split('&'));
}
tmpdic.Add(p[0], ruanjianlist);
});
softseting = tmpdic;
}));
}
private void Start()
{
////连接仿真服务器
//ClientSocket.ConnectFangZhenSocekt(LoadManage.Instance.currentPractice.IpAddress, LoadManage.Instance.currentPractice.Port.Value, (a) =>
//{
// if (!a)
// {
// Debug.LogError("连接仿真服务器失败");
// MessagePanel.ShowMessage("连接仿真服务器失败", GameManage.Instance.canvas.transform, b => { });
// }
//});
}
private void Update()
{
//测试代码
//if (Input.GetKeyDown(KeyCode.Space))
//{
// LoadManage.Instance.currentPracticeSubejct = LoadManage.Instance.psubjects[0];
// LoadManage.Instance.currentPracticeSeat = LoadManage.Instance.myPracticeSeat.Find(a => a.PracticeSubjectId == LoadManage.Instance.currentPracticeSubejct.Id);
// StartSoft(LoadManage.Instance.currentPracticeSeat.PracticeId, LoadManage.Instance.currentPracticeSubejct.Name, LoadManage.Instance.currentPracticeSeat.SeatName, LoadManage.Instance.currentPracticeSeat.UserAccount, 0);
//}
//if (Soft.Any())
//{
// var tmps = Soft.GetRange(0, Soft.Count);
// tmps.ForEach(tmp =>
// {
// SoftHandle(tmp);
// });
// Soft.Clear();
//}
}
/// <summary>
/// 注册软件消息处理函数
/// </summary>
/// <param name="softAction">参数(消息内容,软件名)</param>
public void SetSoftAction(Action<string, string> softAction)
{
this.softAction += softAction;
}
/// <summary>
/// 二维软件消息处理
/// </summary>
public void SoftHandle(MessageModel message)
{
if (softAction != null)
{
softAction.Invoke(message.str, message.canShu.ToString());
}
}
/// <summary>
/// 是否自己执行
/// </summary>
/// <param name="softName"></param>
/// <returns></returns>
public bool CanHandle(string softName)
{
if (LoadManage.Instance.currentPracticeSubejct!=null && LoadManage.Instance.currentPracticeSeat!=null)
{
string subjectname = LoadManage.Instance.currentPracticeSubejct.Name;
string seatname=LoadManage.Instance.currentPracticeSeat.SeatName;
if (softseting.ContainsKey(subjectname))
{
if (softseting[subjectname].Any(a => a[0] == softName && a[1] == seatname))
{
return true;
}
}
}
return false;
}
private void OnDestroy()
{
softAction = null;
ClientSocket.CloseFangZhenSocket();
}
/// <summary>
/// 启动二维软件 (故障课目为1非故障为0)
/// </summary>
public void StartSoft(string pratcieId, string subjectName, string seatName, string userAccount, int isGuZhang)
{
//是否开启软件
if(softseting.ContainsKey(subjectName) && softseting[subjectName].Any(a=>a[0]==softName && a[1]==seatName))
{
if (subjectDic.ContainsKey(subjectName))
{
string subjectId = subjectDic[subjectName];
Debug.Log("启动二维软件");
string arguments = string.Format("{0};{1};{2};{3};{4};{5};{6};{7}", pratcieId, subjectId, seatName, userAccount, LoadManage.Instance.currentPractice.IpAddress, LoadManage.Instance.currentPractice.Port, isGuZhang, "none:no");
byte[] arg = Encoding.UTF8.GetBytes(arguments);
byte[] data = new byte[8 + arg.Length];
//type
Array.Copy(BitConverter.GetBytes(20), 0, data, 0, 4);
//参数
Array.Copy(BitConverter.GetBytes(arg.Length), 0, data, 4, 4);
Array.Copy(arg, 0, data, 8, arg.Length);
//UDP远程启动
LoadManage.Instance.UdpSend(data, new IPEndPoint(IPAddress.Parse(MyNetMQClient.remoteServerIP.Split(':')[0]), int.Parse(MyNetMQClient.remoteServerIP.Split(':')[1])));
}
}
}
/// <summary>
/// 远程切换科目
/// </summary>
public void ChangeSoftSubject(string practiceId, string subjectName, string softName, string seatName, string userAccount)
{
if (subjectDic.ContainsKey(subjectName))
{
string subejctId = subjectDic[subjectName];
MessageModel messageModel = new MessageModel(SimOperationEnum.);
messageModel.str = string.Format("{0}|{1}|{2}|{3}|{4}", "切换科目", practiceId, subejctId, seatName, userAccount);
messageModel.str2 = MyNetMQClient.remoteServerIP.Split(':')[0];
messageModel.canShu = softName;
// ClientSocket.SendToTongBuFangZhenServer(messageModel);
string msg=LitJson.JsonMapper.ToJson(messageModel);
byte[] msgbytes=Encoding.UTF8.GetBytes(msg);
byte[] tmps = new byte[4+msgbytes.Length];
Array.Copy(BitConverter.GetBytes(msg.Length), 0, tmps, 0, 4);
Array.Copy(msgbytes, 0, tmps, 4, msgbytes.Length);
MyNetMQClient.instance.Send(LoadManage.Instance.currentRoomArea, 22, tmps);
Debug.Log("切换二维软件科目:" + subjectName);
}
else
{
Debug.LogError("不存在此二维科目");
}
}
}