using Dias_Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dias_Farm
{
public class staticDevice
{
public static List<DeviceInfo> DEVLIST = new List<DeviceInfo>();
public static void Init(string mainid)
{
DEVLIST.Clear();
string path = System.IO.Path.Combine(Environment.CurrentDirectory, "Data", $"DeviceList_{mainid}.Dat");
if (System.IO.File.Exists(path))
{
string data = System.IO.File.ReadAllText(path, Encoding.UTF8);
DEVLIST = DataBus.StringToObject<List<DeviceInfo>>(data);
if (DEVLIST == null)
DEVLIST = new List<DeviceInfo>();
}
if (DEVLIST.Count <= 0)
{
DataDeviceInfo dvi = new DataDeviceInfo(new Setting("Device_Data_Set"))
{
Id = "1001",
Name = "场温度",
Value = 23.50,
Status = 0,
Type = DeviceType.Data,
Description = "场室外温度"
};
dvi.Set.Set("Unit", "℃");
DEVLIST.Add(dvi);
DataDeviceInfo dvi1 = new DataDeviceInfo(new Setting("Device_Contol_Set"))
{
Id = "1002",
Name = "供水闸",
Value = 0,
Status = 0,
Type = DeviceType.Control,
Description = "总水匣开关"
};
DEVLIST.Add(dvi1);
}
}
public static void Add(DeviceInfo dev)
{
if (DEVLIST.Find(s => s.Id == dev.Id) == null)
{
DEVLIST.Add(dev);
}
}
public static DeviceInfo GetDeviceById(string id)
{
return DEVLIST.FirstOrDefault(s => s.Id == id) as DeviceInfo;
}
public static bool RemoveById(string id)
{
return DEVLIST.RemoveAll(s => s.Id == id)==1;
}
public static void Save(string mainid)
{
string path = System.IO.Path.Combine(Environment.CurrentDirectory, "Data", $"DeviceList_{mainid}.Dat");
string data=DataBus.ObjectToString(DEVLIST);
System.IO.File.WriteAllText(path, data,Encoding.UTF8);
}
}
}