179 lines
5.6 KiB
C#
179 lines
5.6 KiB
C#
using SFB;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using WI;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
|
|
namespace CHN
|
|
{
|
|
public class Panel_ProtocolSetting : SettingPanel, ISingle, IOptionable
|
|
{
|
|
[OptionSection]
|
|
string defaultInfo;
|
|
[OptionKey]
|
|
string machineInfoPath = "./MachineStandardInfo.csv";
|
|
|
|
public UI_InputOrFind UI_StandardInfoFile;
|
|
public List<StandardInfo> standardInfos = new();
|
|
public string[] machineFilters;
|
|
public Action<string[]> onUpdateMachineFilters;
|
|
public Action<List<StandardInfo>,string[]> onUpdateMachine;
|
|
|
|
public UI_ProtocolSetting mqttData;
|
|
public UI_ProtocolSetting httpData;
|
|
|
|
private HTTPRequester httpManager;
|
|
private MQTT mqttManager;
|
|
|
|
public void InitOptionSetting()
|
|
{
|
|
httpManager = FindSingle<HTTPRequester>();
|
|
mqttManager = FindSingle<MQTT>();
|
|
|
|
mqttData = Find<UI_ProtocolSetting>("MQTTData");
|
|
mqttData.InitInputFields(mqttManager);
|
|
|
|
httpData = Find<UI_ProtocolSetting>("HTTPData");
|
|
httpData.InitInputFields(httpManager);
|
|
|
|
UI_StandardInfoFile = Find<UI_InputOrFind>("UI_StandardInfoFile");
|
|
UI_StandardInfoFile.onClickFind += FindStandardInfo;
|
|
}
|
|
public void Update()
|
|
{
|
|
mqttData.ProtocolSuccessCheck(mqttManager);
|
|
httpData.ProtocolSuccessCheck(httpManager);
|
|
}
|
|
|
|
public override void AfterStart()
|
|
{
|
|
ReadStandardInfo(new string[] { machineInfoPath });
|
|
//FindSingle<Canvas_Popup>().panel_library.UpdateFileterList()
|
|
}
|
|
|
|
private void FindStandardInfo()
|
|
{
|
|
StandaloneFileBrowser.OpenFilePanelAsync("Open File", "", "csv", false, ReadStandardInfo);
|
|
}
|
|
|
|
string[] ReadRawFile(string path, char separator)
|
|
{
|
|
FileInfo fileInfo = new FileInfo(path);
|
|
using (var fs = fileInfo.OpenRead())
|
|
{
|
|
var length = fs.Length;
|
|
var buffer = new byte[length];
|
|
fs.Read(buffer, 0, buffer.Length);
|
|
string str = System.Text.Encoding.Default.GetString(buffer);
|
|
return str.Split(separator);
|
|
}
|
|
}
|
|
|
|
|
|
void ReadStandardInfo(string[] file)
|
|
{
|
|
var path = file[0];
|
|
var rawData = ReadRawFile(path, '\n');
|
|
|
|
UI_StandardInfoFile.SetPathText(path);
|
|
UI_StandardInfoFile.SetErrorMessageText("Connected");
|
|
|
|
standardInfos.Clear();
|
|
bool warningChecker = false;
|
|
HashSet<string> filterSet = new();
|
|
for (int i = 1; i < rawData.Length - 1; ++i)
|
|
{
|
|
var r = rawData[i];
|
|
var data = r.Split(',');
|
|
if (data.Length < 2)
|
|
{
|
|
//Debug.LogWarning("Error Data!");
|
|
UI_StandardInfoFile.SetErrorMessageText("Error Data!");
|
|
UI_StandardInfoFile.SetState(UI_InputOrFind.State.Error);
|
|
return;
|
|
}
|
|
var code = data[0];
|
|
var name = data[1];
|
|
if (code == "")
|
|
{
|
|
//Debug.LogWarning("Code is Empty!");
|
|
UI_StandardInfoFile.SetErrorMessageText("Code is Empty!");
|
|
warningChecker = true;
|
|
break;
|
|
}
|
|
|
|
if (name == "")
|
|
{
|
|
//Debug.LogWarning("Name is Empty!");
|
|
UI_StandardInfoFile.SetErrorMessageText("Name is Empty!");
|
|
warningChecker = true;
|
|
break;
|
|
}
|
|
|
|
data = data[2..];
|
|
|
|
int prevLength = data.Length;
|
|
data = data.Distinct().Where(d => d != "" && d != "\r").ToArray();
|
|
for(int a = 0; a < data.Length; ++a)
|
|
{
|
|
data[a] = data[a].Replace('\r',' ');
|
|
}
|
|
|
|
var standardInfo = new StandardInfo() { code = code, name = name, filterInfo = data };
|
|
|
|
|
|
foreach (var f in standardInfo.filterInfo)
|
|
{
|
|
if (f != "")
|
|
filterSet.Add(f);
|
|
}
|
|
|
|
standardInfos.Add(standardInfo);
|
|
}
|
|
if (warningChecker)
|
|
{
|
|
UI_StandardInfoFile.SetState(UI_InputOrFind.State.Warning);
|
|
return;
|
|
}
|
|
|
|
machineFilters = filterSet.ToArray();
|
|
UI_StandardInfoFile.SetState(UI_InputOrFind.State.Good);
|
|
onUpdateMachine?.Invoke(standardInfos, machineFilters);
|
|
//onUpdateMachineFilters?.Invoke(machineFilters);
|
|
}
|
|
|
|
internal override void Reset()
|
|
{
|
|
mqttData.ResetInputFields(mqttManager);
|
|
mqttManager.MQTTConnect();
|
|
|
|
httpData.ResetInputFields(httpManager);
|
|
httpManager.HTTPConnect();
|
|
}
|
|
|
|
internal override void Accept()
|
|
{
|
|
mqttData.AcceptInputFields(mqttManager);
|
|
mqttManager.MQTTConnect();
|
|
|
|
httpData.AcceptInputFields(httpManager);
|
|
httpManager.HTTPConnect();
|
|
}
|
|
internal override void Open()
|
|
{
|
|
gameObject.SetActive(true);
|
|
}
|
|
internal override void Close()
|
|
{
|
|
mqttData.UnAcceptInputFields(mqttManager);
|
|
httpData.UnAcceptInputFields(httpManager);
|
|
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
} |