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 subjectDic = new Dictionary {}; private Action softAction; /// /// 配置文件 科目=软件&岗位,软件&岗位 /// private Dictionary> softseting; //public List Soft = new List();//操作软件通信 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>(); tmps.ForEach(a=> { string[] p=a.Split('='); string[] datas=p[1].Split(','); List ruanjianlist = new List(); 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(); //} } /// /// 注册软件消息处理函数 /// /// 参数(消息内容,软件名) public void SetSoftAction(Action softAction) { this.softAction += softAction; } /// /// 二维软件消息处理 /// public void SoftHandle(MessageModel message) { if (softAction != null) { softAction.Invoke(message.str, message.canShu.ToString()); } } /// /// 是否自己执行 /// /// /// 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(); } /// /// 启动二维软件 (故障课目为1,非故障为0) /// 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]))); } } } /// /// 远程切换科目 /// 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("不存在此二维科目"); } } }