修改设备图标。连接设备加缓存功能。

This commit is contained in:
suntao committed 2021-04-30 18:03:34 +08:00
1 parent 6399c26f7e
commit 9f4e446850
33 files changed
+594 -971

No files matched your search

+14 -1
View File
@@ -4,10 +4,11 @@ using Assets.Scripts.UI.Prefab.Login;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
public static class App
{
public static string Host = "http://pf.juze.pro/";
public static string Host = "http://192.168.0.97:5082/";
public static string AppVersion = "1.0.0";
@@ -40,4 +41,16 @@ public static class App
public static RideSetting rideSetting = new RideSetting();
public static string pageName { get; set; }
public static int firstEnter { get; set; }
public static IPEndPoint UdpAddress { get; private set; } = new IPEndPoint(IPAddress.Parse("192.168.0.97"), 11000);
public static IPEndPoint TcpAddress { get; private set; } = new IPEndPoint(IPAddress.Parse("192.168.0.97"), 11001);
static App()
{
#if !UNITY_EDITOR
Host = "http://pf.juze.pro/";
UdpAddress = new IPEndPoint(IPAddress.Parse("47.97.84.8"), 21000);
TcpAddress = new IPEndPoint(IPAddress.Parse("47.97.84.8"), 21001);
#endif
}
}
+1 -2
View File
@@ -51,7 +51,6 @@ namespace Assets.Scripts
return _thirdPartApi;
}
}
public static IPEndPoint UdpAddress { get; private set; } = new IPEndPoint(IPAddress.Parse("192.168.0.97"), 11000);
public static IPEndPoint TcpAddress { get; private set; } = new IPEndPoint(IPAddress.Parse("192.168.0.97"), 11001);
}
}
+9 -2
View File
@@ -4,6 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ANT_Managed_Library;
using Assets.Scripts.UI.Prefab.Device;
using UnityEngine;
namespace Assets.Scripts.Devices.Ant
@@ -45,13 +46,14 @@ namespace Assets.Scripts.Devices.Ant
public readonly List<AbstractAntDevice> discoveredDevices = new List<AbstractAntDevice>();
private Action<AbstractAntDevice> _action;
private static AntConnector _antConnector;
public static AntConnector Instance()
public static AntConnector Instance(Action<AbstractAntDevice> action = null)
{
if (_antConnector == null)
{
_antConnector = new AntConnector();
_antConnector._action = action;
}
return _antConnector;
@@ -310,6 +312,11 @@ namespace Assets.Scripts.Devices.Ant
//{
// device2.Connect();
//}
if(_action != null)
{
_action(device2);
}
}
return;
}
+13 -1
View File
@@ -1,5 +1,6 @@
using Assets.Scripts;
using Assets.Scripts.Devices.Ant;
using Assets.Scripts.UI.Prefab.Device;
using Mapbox.Examples;
using System.Collections;
using System.Collections.Generic;
@@ -15,8 +16,19 @@ public class MainController : MonoBehaviour
{
Version = this.transform.Find("GameObject").Find("Version").GetComponent<Text>();
Version.text = "V"+App.AppVersion;
DeviceCache.Init(Application.dataPath);
AntConnector.Instance();
AntConnector.Instance((device2) => {
//自动连接
if (DeviceCache.Exist(device2))
{
Debug.Log("自动连接" + device2.DeviceNumber);
device2.Connect();
}
});
//Debug.Log(Application.dataPath);
}
// Start is called before the first frame update
@@ -9,6 +9,7 @@ using UnityEngine.UI;
using Assets.Scripts.Devices.Ant.Interfaces;
using Assets.Scripts.UI.Control;
using Assets.Scripts;
using Assets.Scripts.UI.Prefab.Device;
public class ConnectDeviceModal : PFUIPanel
{
@@ -83,6 +84,7 @@ public class ConnectDeviceModal : PFUIPanel
if (item.Value.DeviceInfo.Sensor == SensorType)
{
item.Value.DeviceInfo.Disconnect();
DeviceCache.Remove(item.Value.DeviceInfo);
}
}
}
@@ -90,6 +92,7 @@ public class ConnectDeviceModal : PFUIPanel
if(dd != null)
{
dd.DeviceInfo.Connect();
DeviceCache.Add(dd.DeviceInfo);
}
this.Close();
}));
@@ -0,0 +1,88 @@
using Assets.Scripts.Devices.Ant;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using UnityEngine;
using Newtonsoft.Json;
namespace Assets.Scripts.UI.Prefab.Device
{
public class DeviceCache
{
private static Dictionary<string, ushort> dict = null;
private static string _dataPath;
public static void Init(string dataPath) {
if(dict != null)
{
return;
}
_dataPath = dataPath;
if(!System.IO.File.Exists(_dataPath + "/Devices.txt"))
{
dict = new Dictionary<string, ushort>();
return;
}
//var str = PlayerPrefs.GetString("DeviceCache", "");
var str = System.IO.File.ReadAllText(_dataPath + "/Devices.txt", Encoding.UTF8);
if (!string.IsNullOrWhiteSpace(str)) {
dict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, ushort>>(str);
}
else {
dict = new Dictionary<string, ushort>();
}
}
public static void Add(AbstractAntDevice antDevice)
{
Debug.Log("添加设备" + antDevice.DeviceNumber);
//new System.Collections.Concurrent.ConcurrentDictionary<string, string>().AddOrUpdate
//dict.AddOrUpdate(antDevice.Sensor.ToString(), antDevice.DeviceNumber, (key, oldValue)=> {
// Debug.Log("真正添加" + dict.Count);
// Write();
// return antDevice.DeviceNumber;
//});
dict[antDevice.Sensor.ToString()] = antDevice.DeviceNumber;
Write();
}
public static bool Exist(AbstractAntDevice antDevice)
{
ushort a;
if(dict.TryGetValue(antDevice.Sensor.ToString(), out a))
{
return a == antDevice.DeviceNumber;
}
return false;
}
public static void Remove(AbstractAntDevice antDevice)
{
if(dict == null)
{
return;
}
if (dict.ContainsKey(antDevice.Sensor.ToString()))
{
if(dict[antDevice.Sensor.ToString()] == antDevice.DeviceNumber)
{
dict.Remove(antDevice.Sensor.ToString());
Write();
}
}
}
private static void Write()
{
//PlayerPrefs.SetString("DeviceCache", JsonConvert.SerializeObject(dict));
System.IO.File.WriteAllText(_dataPath + "/Devices.txt", JsonConvert.SerializeObject(dict));
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a2ab4d6b3530fa1409f9d132b0545357
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -11,6 +11,7 @@ using Assets.Scripts.Devices.Ant.Interfaces;
using Assets.Scripts.UI.UIEffect;
using Assets.Scripts;
using DG.Tweening;
using Assets.Scripts.UI.Prefab.Device;
public class DeviceView : MonoBehaviour
{
@@ -261,7 +262,13 @@ public class DeviceView : MonoBehaviour
// logo.sprite = logoSprite;
//}
});
DeviceCache.Add(connectedDevice);
Debug.Log("connected");
}
logo.sprite = sprite3;
title.text = connectedDevice.Name;
@@ -271,9 +278,8 @@ public class DeviceView : MonoBehaviour
if (powerValue != null)
{
powerValue.text = GetValue(connectedDevice);
}
}
}
}
}
string GetTitle()