using System;
using System.Collections.Generic;
using System.Threading;
using HDL_ON.DAL.Server;
using Shared;
namespace HDL_ON.Entity
{
public class DB_ResidenceData
{
public DB_ResidenceData()
{
}
///
/// 主人token
///
public string MasterToken;
///
/// 住宅基础信息
///
public RegionInfoRes residecenInfo
{
get
{
if (MainPage.NoLoginMode)
{
return new RegionInfoRes() { Name = "本地模式" };
}
else
{
var curRegion = UserInfo.Current.regionList.Find((obj) => obj.RegionID == CurReginID);
if (curRegion == null)
{
if (UserInfo.Current.regionList != null && UserInfo.Current.regionList.Count > 0)
{
CurReginID = UserInfo.Current.regionList[0].RegionID;
//恢复备份
HDLCommon.Current.RestoreHomeBackup(CurReginID);
return UserInfo.Current.regionList[0];
}
else
{
return new RegionInfoRes();
}
}
//if (curRegion == null)
//{
// CurReginID = UserInfo.Current.regionList[0].RegionID;
// return UserInfo.Current.regionList[0];
//}
return curRegion;
}
}
}
/////
///// 获取网关列表
/////
//void GetHomeGatewayList()
//{
// new Thread(() =>
// {
// //切换住宅后,查询一次网关列表
// new HttpServerRequest().GetHomeGatewayList();
// })
// { IsBackground = true }.Start();
//}
//string curReginID;
/////
///// 当前选择的住宅索引
/////
//public string CurReginID
//{
// set
// {
// curReginID = value;
// GetHomeGatewayList();
// }
// get
// {
// return curReginID;
// }
//}
///
/// 当前选择的住宅索引
///
public string CurReginID = "";
///
/// 功能类型列表
///
public List functionTypeList = new List();
static DB_ResidenceData instance;
public static DB_ResidenceData residenceData
{
get
{
if (instance == null)
{
try
{
var residenceDataBytes = FileUtils.ReadFile("DB_ResidenceData");
var userConfigString = System.Text.Encoding.UTF8.GetString(residenceDataBytes);
DB_ResidenceData temp = null;
if (!string.IsNullOrEmpty(userConfigString))
{
temp = Newtonsoft.Json.JsonConvert.DeserializeObject(userConfigString);
}
if (temp == null)
{
instance = new DB_ResidenceData { };
}
else
{
instance = temp;
}
if (instance.functionTypeList.Count == 0)
{
instance.functionTypeList.AddRange(new List {
ShowFunction.Light,ShowFunction.AC,ShowFunction.Curtain,
ShowFunction.FloorHeating,ShowFunction.DoorLock,
ShowFunction.Electric,ShowFunction.EnergyMonitoring,
ShowFunction.Environmental,
ShowFunction.FreshAir,ShowFunction.Music,
ShowFunction.Panel,ShowFunction.SecurityMonitoring,
ShowFunction.Sensor,ShowFunction.VideoIntercom
});
instance.SaveResidenceData();
}
new Light()
{
sid = "030101123456780202010005ABCD",
name = "灯光1",
attributes = new List() {
new FunctionAttributes { key="on_off", max=100,min = 0, value= new List { "on","off"} },
},
roomIds = new List() { "0001" },
bus_Data = new BusData
{
addresses = "2A06",
loopId = 1,
},
}.SaveFunctionData();
}
catch (Exception ex)
{
MainPage.Log($"住宅数据初始化失败:{ex.Message}");
}
instance.residenceImage = "Classification/Room/Roombg.png";
//初始化住宅功能数据
SpatialInfo.CurrentSpatial.InitRoomFunction();
}
return instance;
}
}
///
/// 退出账号清空数据
///
public void EixtAccount()
{
instance = null;
Rooms = null;
ins_OidList = null;
functionList = null;
if (MainPage.IsRemote)
{
DAL.Mqtt.MqttClient.DisConnectRemote("退出登录,或者切换住宅");
}
}
///
/// 网关详细信息
///
public HomeGatewayInfo HomeGateway;
///
/// 0:bus网关
/// 1:A协议网关
///
public int GatewayType
{
get
{
//#if DEBUG
//return 0;
//#endif
if (HomeGateway == null)
return -1;
if (HomeGateway.gatewayType == "AGATEWAY")
{
return 1;
}
else
{
return 0;
}
}
}
///
/// 检测住宅是否绑定了网关
///
///
public bool CheckWhetherGatewayIsBound()
{
if (HomeGateway != null && !string.IsNullOrEmpty(HomeGateway.mac))
{
return true;
}
else
{
return false;
}
}
///
/// 当前住宅的网关MAC
///
public string residenceGatewayMAC
{
get
{
if (CheckWhetherGatewayIsBound())
{
return HomeGateway.mac.ToUpper();
}
else
{
return "";
}
}
set
{
if (CheckWhetherGatewayIsBound())
{
HomeGateway.mac = value;
}
else
{
HomeGateway = new HomeGatewayInfo() { mac = value };
}
}
}
///
/// 住宅图片
///
public string residenceImage;
///
/// 当前住宅的网关MAC
///
//public string residenceGatewayMAC = "4E47323347591243";//"0281B9078C000000";// "00964E19C4467B1E";
///
/// 保存住宅数据
///
public void SaveResidenceData()
{
if (this == null || this.residecenInfo == null)
return;
var ssd = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(this));
FileUtils.WriteFileByBytes("DB_ResidenceData", ssd);
MainPage.Log("Save DB_ResidenceData");
}
///
/// 当前选择的楼层
///
public string CurFoor = Language.StringByID(StringId.All);
///
/// 房间列表
///
public List Rooms = SpatialInfo.CurrentSpatial.RoomList;
#region 功能数据
public static FunctionList functionList = FunctionList.List;
#endregion
#region oid列表 a协议转bus协议控制使用
///
/// oid数据保存的文件名
///
[Newtonsoft.Json.JsonIgnore]
public static string OidSavePathName = "AProtocolData_FunctionOid";
static List ins_OidList;
public static List functionOidList
{
get
{
if (ins_OidList == null)
{
var bytes = FileUtils.ReadFile(OidSavePathName);
if (bytes == null || bytes.Length == 0)
{
ins_OidList = new List();
}
else
{
var jsonString = System.Text.Encoding.UTF8.GetString(bytes);
if (string.IsNullOrEmpty(jsonString))
{
ins_OidList = new List();
}
else
{
var list = Newtonsoft.Json.JsonConvert.DeserializeObject>(jsonString);
if (list == null)
{
ins_OidList = new List();
}
else
{
ins_OidList = list;
}
}
}
}
return ins_OidList;
}
}
///
/// 保存oid数据,每次保存都会覆盖
///
public static void SaveOidList(string oidJsonString)
{
var oidBytes = System.Text.Encoding.UTF8.GetBytes(oidJsonString);
FileUtils.WriteFileByBytes(OidSavePathName, oidBytes);
}
#endregion
}
}