Commit 21860538 authored by 聂雨荷's avatar 聂雨荷
Browse files

fix code format

parent d6b0abba
Loading
Loading
Loading
Loading
+101 −9
Original line number Diff line number Diff line
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.MixedReality.Toolkit;
using Script;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.UI;

public class CityManager : MonoBehaviour
{
    
    private bool architectureOpen = false; // 是否打开了 architecture 面板
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(nameof(StartGPS));
        StartCoroutine(nameof(StartBattery));
    }

    // Update is called once per frame
    void Update()
    {
        
    }





    public void SwitchGeographyBoard()
    {
        GameObject board = GameObject.Find("GeographyBoard(Clone)");
@@ -34,13 +32,11 @@ public class CityManager : MonoBehaviour
        }
        else
        {
            
            Destroy(board);
        }
    }



    public void SwitchArchitectureBoard()
    {
        GameObject board = GameObject.Find("ArchitectureBoard(Clone)");
@@ -68,4 +64,100 @@ public class CityManager : MonoBehaviour
            Destroy(board);
        }
    }


    
    IEnumerator StartGPS()
    {
        if (!Input.location.isEnabledByUser)
        {
            User.GetInstance().Latitude = 22.54845664f;
            User.GetInstance().Longitude = 114.06455184f;
            StartCoroutine(nameof(GetLocationDetails));
            yield break;
        }

        // Start service before querying location  
        Input.location.Start();

        // Wait until service initializes  
        int maxWait = 20;
        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
        {
            yield return new WaitForSeconds(1);
            maxWait--;
        }

        // Service didn't initialize in 20 seconds  
        if (maxWait < 1)
        {
            Debug.Log("Timed out");
            yield break;
        }

        // Connection has failed  
        if (Input.location.status == LocationServiceStatus.Failed)
        {
            Debug.Log("Unable to determine device location");
            yield break;
        }

        StartCoroutine(nameof(GetLocationInfo));
    }

    IEnumerator StartBattery()
    {
        // Create aggregate battery object
        while (true)
        {
            User.GetInstance().Battery = int.Parse(SystemInfo.batteryLevel.ToString()) * 100;
            yield return new WaitForSeconds(1);
        }
        // ReSharper disable once IteratorNeverReturns
    }

    // 获得经纬度
    IEnumerator GetLocationInfo()
    {
        while (true)
        {
            if (User.GetInstance().Longitude != float.Parse(Input.location.lastData.longitude.ToString()) ||
                User.GetInstance().Latitude != float.Parse(Input.location.lastData.latitude.ToString()))
            {
                User.GetInstance().Longitude = float.Parse(Input.location.lastData.longitude.ToString());
                User.GetInstance().Latitude = float.Parse(Input.location.lastData.latitude.ToString());
                StartCoroutine(nameof(GetLocationDetails));
            }

            yield return new WaitForSeconds(5);
        }
        // ReSharper disable once IteratorNeverReturns
    }


    // 获得具体的位置信息
    IEnumerator GetLocationDetails()
    {
        string locationUrl = Config.GetBaiduUrl() + "/reverse_geocoding/v3/?";
        locationUrl += "ak=" + Config.BaiduKey + "&";
        locationUrl += "output=json&";
        locationUrl += "coordtype=bd09ll&";
        locationUrl += "location=" + User.GetInstance().Latitude + "," + User.GetInstance().Longitude;
        Debug.Log(locationUrl);

        HttpRequest request = new HttpRequest();
        StartCoroutine(request.Get(locationUrl));
        while (!request.isComplete)
        {
            yield return new WaitForSeconds(1);
        }

        if (int.Parse(request.value["status"].ToString()) == 0)
        {
            User.GetInstance().Address = request.value["result"]["formatted_address"].ToString();
            User.GetInstance().City = request.value["result"]["addressComponent"]["city"].ToString();
            User.GetInstance().Province = request.value["result"]["addressComponent"]["province"].ToString();
            User.GetInstance().Street = request.value["result"]["addressComponent"]["street"].ToString();
        }
    }
}
 No newline at end of file
+10 −0
Original line number Diff line number Diff line
public class Config
{
    public static string GetBaiduUrl()
    {
        return "https://api.map.baidu.com";
    }

    public static string BaiduKey => baidu;
    private static string baidu = "UexT0eqWG8q7X6YF1DQmSw3K3mH5dSw8";
}
 No newline at end of file
+3 −0
Original line number Diff line number Diff line
fileFormatVersion: 2
guid: 45f26172ed45475eb1c3efeacb5ca4c0
timeCreated: 1638179503
 No newline at end of file
+57 −139
Original line number Diff line number Diff line
using System;
using System.Collections;
using System.Runtime.InteropServices;
using Microsoft.MixedReality.Toolkit.Utilities.Gltf.Serialization;
using Script;
using UnityEngine;
using UnityEngine.UI;
using XCharts;
@@ -55,8 +58,8 @@ public class GeographyManager : MonoBehaviour
// #if ENABLE_WINMD_SUPPORT
//         StartStreaming();
// #endif
        StartCoroutine("StartGPS");
        StartCoroutine("StartBattery");
        StartCoroutine(nameof(UserDataSync));
        // StartCoroutine("StartBattery");
    }

    void Awake()
@@ -82,19 +85,7 @@ public class GeographyManager : MonoBehaviour
    // Update is called once per frame
    void Update()
    {
        frameCount++;
        if (Time.realtimeSinceStartup - lastTime >= Interval)
        {
            fps = frameCount / (Time.realtimeSinceStartup - lastTime);
            frameCount = 0;
            lastTime = Time.realtimeSinceStartup;
            fpsChart.AddData(0, fps);
            var tmp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            time.text = "时间:" + tmp;
            fpsChart.AddXAxisData(tmp);
            fpsChart.title.text = $"FPS:{fps:f1}";
        }

        UpdateFrame();
// #if ENABLE_WINMD_SUPPORT
//         int sizeOut = Marshal.SizeOf(typeof(OUTPUT));
//         IntPtr pBuffOut = Marshal.AllocHGlobal(sizeOut);
@@ -108,151 +99,78 @@ public class GeographyManager : MonoBehaviour
// #endif
    }

    IEnumerator StartBattery()
    {
        // Create aggregate battery object
        while (true)
        {
            if (Math.Abs(battery - float.Parse(SystemInfo.batteryLevel.ToString()) * 100) > 0.01)
            {
                battery = float.Parse(SystemInfo.batteryLevel.ToString()) * 100;
                batteryChart.ClearData();
                batteryChart.AddData(0, battery);
            }

            yield return new WaitForSeconds(1);
        }
    }

    IEnumerator StartGPS()
    {
        Debug.Log("Start GPS Initializing!");
        // First, check if user has location service enabled  
        if (!Input.location.isEnabledByUser)
        {
            latitudeValue = 22.54845664f;
            longitudeValue = 114.06455184f;
            latitude.text = "纬度:" + latitudeValue;
            longitude.text = "经度:" + longitudeValue;
            StartCoroutine("GetLocationDetails");
            yield break;
        }

        // Start service before querying location  
        Input.location.Start();

        // Wait until service initializes  
        int maxWait = 20;
        while (Input.location.status == LocationServiceStatus.Initializing && maxWait > 0)
    void UpdateFrame()
    {
            yield return new WaitForSeconds(1);
            maxWait--;
        }

        // Service didn't initialize in 20 seconds  
        if (maxWait < 1)
        frameCount++;
        if (Time.realtimeSinceStartup - lastTime >= Interval)
        {
            print("Timed out");
            yield break;
            fps = frameCount / (Time.realtimeSinceStartup - lastTime);
            frameCount = 0;
            lastTime = Time.realtimeSinceStartup;
            fpsChart.AddData(0, fps);
            var tmp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            time.text = "时间:" + tmp;
            fpsChart.AddXAxisData(tmp);
            fpsChart.title.text = $"FPS:{fps:f1}";
        }

        // Connection has failed  
        if (Input.location.status == LocationServiceStatus.Failed)
        {
            print("Unable to determine device location");
            yield break;
    }

        StartCoroutine("GetLocationInfo");
    }

    IEnumerator GetLocationInfo()
    IEnumerator UserDataSync()
    {
        while (true)
        {
            yield return new WaitForSeconds(1);
            if (longitudeValue != float.Parse(Input.location.lastData.longitude.ToString()) ||
                latitudeValue != float.Parse(Input.location.lastData.latitude.ToString()))
            {
                longitudeValue = float.Parse(Input.location.lastData.longitude.ToString());
                latitudeValue = float.Parse(Input.location.lastData.latitude.ToString());
                latitude.text = "纬度:" + latitudeValue;
                longitude.text = "经度:" + longitudeValue;
                StartCoroutine("GetLocationDetails");
            }
        }
    }

    IEnumerator GetLocationDetails()
    {
        string url = GetBaiduUrl() + "/reverse_geocoding/v3/?";
        url += "ak=" + baidu + "&";
        url += "output=json&";
        url += "coordtype=bd09ll&";
        url += "location=" + latitudeValue + "," + longitudeValue;
        Debug.Log(url);

        HttpRequest request = new HttpRequest();
        StartCoroutine(request.Get(url));
        while (!request.isComplete)
        {
            yield return new WaitForSeconds(1);
        }

        string city, street;
        if (int.Parse(request.value["status"].ToString()) == 0)
        {
            var addressTmp = request.value["result"]["formatted_address"];
            address.text = "位置:" + addressTmp;
            Debug.Log(address.text);
            city = request.value["result"]["addressComponent"]["city"].ToString();
            street = request.value["result"]["addressComponent"]["street"].ToString();
            url = GetBaiduUrl() + "/traffic/v1/road/?";
            url += "ak=" + baidu + "&";
            url += "city=" + city + "&";
            url += "road_name=" + street;
            request = new HttpRequest();
            StartCoroutine(request.Get(url));
            while (!request.isComplete)
            // 电量
            batteryChart.ClearData();
            batteryChart.AddData(0, User.GetInstance().Battery);

            // 地址
            address.text = User.GetInstance().Address;

            // 路况
            latitude.text = "纬度:" + User.GetInstance().Latitude;
            longitude.text = "经度:" + User.GetInstance().Longitude;
            string roadUrl = Config.GetBaiduUrl() + "/traffic/v1/road/?";
            roadUrl += "ak=" + Config.BaiduKey + "&";
            roadUrl += "city=" + User.GetInstance().City + "&";
            roadUrl += "road_name=" + User.GetInstance().Street;
            HttpRequest roadRequest = new HttpRequest();
            StartCoroutine(roadRequest.Get(roadUrl));
            while (!roadRequest.isComplete)
            {
                yield return new WaitForSeconds(1);
            }

            if (int.Parse(request.value["status"].ToString()) == 0)
            if (int.Parse(roadRequest.value["status"].ToString()) == 0)
            {
                roadTraffic.text = "路况:" + request.value["evaluation"]["status_desc"];
                roadTraffic.text = "路况:" + roadRequest.value["evaluation"]["status_desc"];
            }


            url = "http://api.k780.com/?app=weather.today&appkey=62989&sign=7f56d749e9a3daabdf428041093b8076&";
            url += "format=json&";
            url += "cityNm=" + city.Replace("市", "");
            request = new HttpRequest();
            StartCoroutine(request.Get(url));
            while (!request.isComplete)
            // 天气
            string weatherUrl =
                "http://api.k780.com/?app=weather.today&appkey=62989&sign=7f56d749e9a3daabdf428041093b8076&";
            weatherUrl += "format=json&";
            weatherUrl += "cityNm=" + User.GetInstance().City.Replace("市", "");
            HttpRequest weatherRequest = new HttpRequest();
            StartCoroutine(weatherRequest.Get(weatherUrl));
            while (!weatherRequest.isComplete)
            {
                yield return new WaitForSeconds(1);
            }

            if (int.Parse(request.value["success"].ToString()) != 1) yield break;
            weather.text = "当前天气:" + request.value["result"]["temperature_curr"] +
                           "," + request.value["result"]["weather_curr"] +
                           "," + request.value["result"]["wind"] +
                           request.value["result"]["winp"];
            humidity.text = "当前湿度:" + request.value["result"]["humidity"] +
                            ",空气质量:" + request.value["result"]["aqi"];
        }
    }

    private string GetUrl()
            if (int.Parse(weatherRequest.value["success"].ToString()) == 1)
            {
        string ip = "";
        string port = "";
        return ip + "/" + port;
                weather.text = "当前天气:" + weatherRequest.value["result"]["temperature_curr"] +
                               "," + weatherRequest.value["result"]["weather_curr"] +
                               "," + weatherRequest.value["result"]["wind"] +
                               weatherRequest.value["result"]["winp"];
                humidity.text = "当前湿度:" + weatherRequest.value["result"]["humidity"] +
                                ",空气质量:" + weatherRequest.value["result"]["aqi"];
            }

    private string GetBaiduUrl()
    {
        return "https://api.map.baidu.com";
            yield return new WaitForSeconds(5);
        }
        // ReSharper disable once IteratorNeverReturns
    }
}
 No newline at end of file

Assets/Script/User.cs

0 → 100644
+64 −0
Original line number Diff line number Diff line
namespace Script
{
    public class User
    {
        private static User _instance = new User();

        public static User GetInstance()
        {
            return _instance;
        }
        
        private int battery;
        private float latitude;
        private float longitude;
        private string city;
        private string province;
        private string address;
        private string street;


        public int Battery
        {
            get => battery;
            set => battery = value;
        }

        public float Latitude
        {
            get => latitude;
            set => latitude = value;
        }

        public float Longitude
        {
            get => longitude;
            set => longitude = value;
        }

        public string City
        {
            get => city;
            set => city = value;
        }

        public string Province
        {
            get => province;
            set => province = value;
        }

        public string Street
        {
            get => street;
            set => street = value;
        }

        public string Address
        {
            get => address;
            set => address = value;
        }
        
    }
}
 No newline at end of file
Loading