Commit 88706a79 authored by 杨泽宇's avatar 杨泽宇

更新

parent c6f403c4
......@@ -3,6 +3,8 @@ using UnityEngine.Networking;
using UnityEngine;
using TMPro;
using System.IO;
using LitJson;
using System;
public class LoginManager : MonoBehaviour
{
......@@ -29,27 +31,29 @@ public class LoginManager : MonoBehaviour
public float angle;
public bool IsLine1;
public string filePath; // JSON 文件路径
public string path; // JSON 文件路径
private InputFieldData inputFieldData;
private JsonData line_jd = new JsonData();
public GameObject _parent;
private void Awake()
{
OnReceiveMsg.AddListener(_getData);
// 从TextAsset读取数据并反序列化
inputFieldData = LoadData(filePath);
LoadInputFields(); // 在 Awake 中加载输入字段
StartCoroutine(GetJson());
}
//private void Start()
//{
// StartCoroutine(GetPositionData());
// StartCoroutine(FaultInformation());
// StartCoroutine(GetThreeModelSensorData());
//}
public void StartLogin()
{
SaveData();
StartCoroutine(GetPositionData());
StartCoroutine(FaultInformation());
StartCoroutine(GetThreeModelSensorData());
}
protected void _getData(object data, string parameter)
......@@ -158,82 +162,143 @@ public class LoginManager : MonoBehaviour
}
}
public void SaveData()
private IEnumerator GetThreeModelSensorData()
{
if (inputFieldData != null)
while (true)
{
// 更新 InputFieldData 的内容
if (IsLine1)
using (UnityWebRequest request = UnityWebRequest.Get(post_url + "?lineId=" + lineID.text))
{
inputFieldData.productionlineID1 = productionlineID.text;
inputFieldData.lineID1 = lineID.text;
Debug.Log(inputFieldData.productionlineID1);
Debug.Log(inputFieldData.lineID1);
}
else
{
inputFieldData.productionlineID2 = productionlineID.text;
inputFieldData.lineID2 = lineID.text;
}
inputFieldData.faultInterface = faultInterface.text;
inputFieldData.post_url = post_url.text;
// 设置请求头
//request.SetRequestHeader("Content-Type", "application/json");
// 将数据序列化为 JSON 字符串
string json = JsonUtility.ToJson(inputFieldData, true);
// 发送请求并等待响应
yield return request.SendWebRequest();
// 将 JSON 写入文件
File.WriteAllText(filePath, json);
if (!string.IsNullOrEmpty(request.error))
{
Debug.LogError(request.error);
}
else
{
// 解析响应数据
line_jd = JsonMapper.ToObject<JsonData>(request.downloadHandler.text);
Debug.Log("数据已保存到:" + filePath);
if (line_jd != null && line_jd.ContainsKey("data"))
{
setData(_parent, line_jd);
}
}
}
yield return new WaitForSeconds(1f);
}
else
}
private void setData(GameObject parent, JsonData line_jd)
{
foreach (JsonData item in line_jd["data"])
{
Debug.LogWarning("InputFieldData为空,无法保存");
if (parent.transform.Find(item["equipmentName"].ToString()) != null)
{
GameObject _obj = parent.transform.Find(item["equipmentName"].ToString()).gameObject;
if (_obj)
{
_obj.transform.Find("设备名称").Find("Text-名称").GetComponent<TMP_Text>().text = item["equipmentName"].ToString();
switch (item["sensorType"].ToString())
{
case "TILT":
_obj.transform.Find("倾角").gameObject.SetActive(true);
_obj.transform.Find("倾角").Find("Text-数据").GetComponent<TMP_Text>().text = item["data"].ToString();
_obj.transform.Find("倾角").Find("Text-数据").GetComponent<TMP_Text>().color = (int.Parse(item["flag"].ToString()).Equals(1) ? Color.red : Color.green);
break;
case "NOISE":
_obj.transform.Find("噪声").gameObject.SetActive(true);
_obj.transform.Find("噪声").Find("Text-数据").GetComponent<TMP_Text>().text = item["data"].ToString();
_obj.transform.Find("噪声").Find("Text-数据").GetComponent<TMP_Text>().color = (int.Parse(item["flag"].ToString()).Equals(1) ? Color.red : Color.green);
break;
case "PRESSURE":
_obj.transform.Find("压力").gameObject.SetActive(true);
_obj.transform.Find("压力").Find("Text-数据").GetComponent<TMP_Text>().text = item["data"].ToString();
_obj.transform.Find("压力").Find("Text-数据").GetComponent<TMP_Text>().color = (int.Parse(item["flag"].ToString()).Equals(1) ? Color.red : Color.green);
break;
case "TEMPERATURE":
_obj.transform.Find("温度").gameObject.SetActive(true);
_obj.transform.Find("温度").Find("Text-数据").GetComponent<TMP_Text>().text = item["data"].ToString();
_obj.transform.Find("温度").Find("Text-数据").GetComponent<TMP_Text>().color = (int.Parse(item["flag"].ToString()).Equals(1) ? Color.red : Color.green);
break;
case "HUMIDITY":
_obj.transform.Find("湿度").gameObject.SetActive(true);
_obj.transform.Find("湿度").Find("Text-数据").GetComponent<TMP_Text>().text = item["data"].ToString();
_obj.transform.Find("湿度").Find("Text-数据").GetComponent<TMP_Text>().color = (int.Parse(item["flag"].ToString()).Equals(1) ? Color.red : Color.green);
break;
case "SMOG":
_obj.transform.Find("烟雾").gameObject.SetActive(true);
_obj.transform.Find("烟雾").Find("Text-数据").GetComponent<TMP_Text>().text = item["data"].ToString();
_obj.transform.Find("烟雾").Find("Text-数据").GetComponent<TMP_Text>().color = (int.Parse(item["flag"].ToString()).Equals(1) ? Color.red : Color.green);
break;
case "PM2.5_E":
_obj.transform.Find("PM2.5").gameObject.SetActive(true);
_obj.transform.Find("PM2.5").Find("Text-数据").GetComponent<TMP_Text>().text = item["data"].ToString();
_obj.transform.Find("PM2.5").Find("Text-数据").GetComponent<TMP_Text>().color = (int.Parse(item["flag"].ToString()).Equals(1) ? Color.red : Color.green);
break;
case "PM10_E":
_obj.transform.Find("PM10").gameObject.SetActive(true);
_obj.transform.Find("PM10").Find("Text-数据").GetComponent<TMP_Text>().text = item["data"].ToString();
_obj.transform.Find("PM10").Find("Text-数据").GetComponent<TMP_Text>().color = (int.Parse(item["flag"].ToString()).Equals(1) ? Color.red : Color.green);
break;
case "PM1.0_E":
_obj.transform.Find("PM1.0").gameObject.SetActive(true);
_obj.transform.Find("PM1.0").Find("Text-数据").GetComponent<TMP_Text>().text = item["data"].ToString();
_obj.transform.Find("PM1.0").Find("Text-数据").GetComponent<TMP_Text>().color = (int.Parse(item["flag"].ToString()).Equals(1) ? Color.red : Color.green);
break;
}
}
}
}
}
public void LoadInputFields()
private IEnumerator GetJson()
{
// 打印读取到的信息
if (inputFieldData != null)
using (var rq = UnityWebRequest.Get(System.IO.Path.Combine(Application.streamingAssetsPath, path)))
{
// 显示数据
if (IsLine1)
rq.SetRequestHeader("Access-Control-Allow-Origin", "*");
rq.SetRequestHeader("Accept", "*");
yield return rq.SendWebRequest();
if (string.IsNullOrEmpty(rq.error))
{
productionlineID.text= inputFieldData.productionlineID1;
lineID.text= inputFieldData.lineID1;
try
{
inputFieldData = JsonUtility.FromJson<InputFieldData>(rq.downloadHandler.text);
if (IsLine1)
{
productionlineID.text = inputFieldData.productionlineID1;
lineID.text = inputFieldData.lineID1;
}
else
{
productionlineID.text = inputFieldData.productionlineID2;
lineID.text = inputFieldData.lineID2;
}
faultInterface.text = inputFieldData.faultInterface;
post_url.text = inputFieldData.post_url;
}
catch (Exception e)
{
Debug.Log(e.ToString());
}
}
else
{
productionlineID.text = inputFieldData.productionlineID2;
lineID.text = inputFieldData.lineID2;
Debug.Log(rq.error);
}
faultInterface.text = inputFieldData.faultInterface;
post_url.text = inputFieldData.post_url;
}
}
public InputFieldData LoadData(string path)
{
// 检查文件是否存在
if (File.Exists(path))
{
// 读取 JSON 字符串
string json = File.ReadAllText(path);
// 反序列化 JSON 字符串为对象
InputFieldData data = JsonUtility.FromJson<InputFieldData>(json);
return data;
}
else
{
Debug.LogWarning("数据文件不存在:" + path);
return null;
}
}
}
//URL
[System.Serializable]
public class InputFieldData
{
......
......@@ -87,18 +87,10 @@ public class SensorDataManger : MonoBehaviour
public IEnumerator GetThreeModelSensorData1()
{
//string jsonData = "{\"lineId\":\"" + lineId1 + "\"}";
//string post_url = "https://apifoxmock.com/m1/3550441-0-default/maintenance/bigScreen/sensor/getThreeModelSensorData";
while (true)
{
using (UnityWebRequest request = UnityWebRequest.Get(post_url + "?lineId=" + lineId1))
{
// 设置请求头
//request.SetRequestHeader("Content-Type", "application/json");
//// 设置请求体
//byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonData);
//request.uploadHandler = new UploadHandlerRaw(bodyRaw);
// 发送请求并等待响应
yield return request.SendWebRequest();
......@@ -124,8 +116,6 @@ public class SensorDataManger : MonoBehaviour
public IEnumerator GetThreeModelSensorData2()
{
//string jsonData = "{\"lineId\":\"" + lineId2 + "\"}";
//string post = "https://apifoxmock.com/m1/3550441-0-default/maintenance/bigScreen/sensor/getThreeModelSensorData";
while (true)
{
using (UnityWebRequest request = UnityWebRequest.Get(post_url+ "?lineId="+lineId2))
......
{
"productionlineID1": "",
"productionlineID2": "4",
"productionlineID2": "",
"faultInterface": "",
"lineID1": "",
"lineID2": "0",
"lineID2": "",
"post_url": ""
}
\ No newline at end of file
......@@ -23279,7 +23279,8 @@ MonoBehaviour:
data: []
angle: 0
IsLine1: 0
filePath: Assets\Scripts\Core\URL 1.json
path: URL.json
_parent: {fileID: 1692847786}
--- !u!114 &346303892
MonoBehaviour:
m_ObjectHideFlags: 0
......@@ -32436,7 +32437,8 @@ MonoBehaviour:
data: []
angle: 0
IsLine1: 1
filePath: Assets\Scripts\Core\URL.json
path: URL.json
_parent: {fileID: 396184908}
--- !u!114 &560685329
MonoBehaviour:
m_ObjectHideFlags: 0
......@@ -43559,7 +43561,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!4 &817819954
Transform:
m_ObjectHideFlags: 0
......@@ -43582,7 +43584,7 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 817819953}
m_Enabled: 1
m_Enabled: 0
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 05d56e7ba014d4848bc7a5d4f0ae7d38, type: 3}
m_Name:
......@@ -21580,7 +21580,8 @@ MonoBehaviour:
data: []
angle: 0
IsLine1: 0
filePath:
path:
_parent: {fileID: 0}
--- !u!114 &346303892
MonoBehaviour:
m_ObjectHideFlags: 0
......@@ -34162,7 +34163,8 @@ MonoBehaviour:
data: []
angle: 0
IsLine1: 1
filePath: Assets\Scripts\Core\URL.json
path: URL.json
_parent: {fileID: 1654077245}
--- !u!114 &560685329
MonoBehaviour:
m_ObjectHideFlags: 0
......@@ -40075,7 +40077,7 @@ GameObject:
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!4 &679944236
Transform:
m_ObjectHideFlags: 0
......@@ -40098,7 +40100,7 @@ MonoBehaviour:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 679944235}
m_Enabled: 1
m_Enabled: 0
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 05d56e7ba014d4848bc7a5d4f0ae7d38, type: 3}
m_Name:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment