using System;
using System.Collections.Generic;
using Shared;
namespace HDL_ON.UI.UI2.Intelligence.Automation
{
public class LogicMethod
{
public const string condition_if = "条件";
public const string target_if = "目标";
///
/// 生成逻辑sid方法
///
public static string NewSid()
{
string logicId = "";
try
{
string sOidBeginsWith = "000101";//厂商 + 通讯方式
DateTime dt = DateTime.Now;
DateTime startTime = TimeZoneInfo.ConvertTimeToUtc(new DateTime(2020, 1, 1));
long m = (long)((dt - startTime).TotalMilliseconds / 10);
string sTimeSpan = "00000000";
byte[] arry = new byte[4];
arry[0] = (byte)(m & 0xFF);
arry[1] = (byte)((m & 0xFF00) >> 8);
arry[2] = (byte)((m & 0xFF0000) >> 16);
arry[3] = (byte)((m >> 24) & 0xFF);
sTimeSpan = arry[0].ToString("X2") + arry[1].ToString("X2") + arry[2].ToString("X2") + arry[3].ToString("X2");
if (sTimeSpan.Length > 8)
{
sTimeSpan = sTimeSpan.Substring(0, 8);
}
else
{
sTimeSpan = "00000000";
}
logicId = sOidBeginsWith + sTimeSpan;
logicId += "0A";
logicId += "0A01";
//0A01 物模型为逻辑, 0001 表示 1 号逻辑功能
int iTopLogicId = 1;
Random random = new Random();
iTopLogicId = random.Next(0, 255);
iTopLogicId += random.Next(0, 255);
logicId += iTopLogicId.ToString("X4");//逻辑号 两个byte
logicId += "1100";
}
catch
{
return logicId;
}
return logicId;
}
///
/// 封装Dictionary对象
///
/// Dictionary类
/// 健
/// 值
public static void dictionary(Dictionary dic, string key, string value)
{
if (dic.ContainsKey(key)) //判断是否存在键值
{
//键存在移除
dic.Remove(key);
}
//添加键值
dic.Add(key, value);
}
///
/// 获取网关房间列表
///
///
public static List GetGatewayRoomListG()
{
return HDL_ON.Entity.DB_ResidenceData.rooms;
}
///
/// 获取网关房间列表
///
///
public static List GetGatewayRoomListG(string name)
{
List roomList = new List();
Entity.Room room1 = new Entity.Room();
room1.name = name;//默认一个房间名为:全部区域
room1.sid = "全部区域";//默认sid用识别该房间
roomList.Add(room1);//默认添加到房间列表里
var roomLists = GetGatewayRoomListG();
foreach (var room in roomLists)
{
roomList.Add(room);
}
return roomList;
}
///
/// 获取网关设备列表
///
///
public static List GetGatewayDeviceList()
{
return HDL_ON.Entity.DB_ResidenceData.functionList.GetAllFunction();
}
///
/// 获取房间的设备列表
///
/// 当前房间
///
public static List GetRoomDevice(HDL_ON.Entity.Room room)
{
List deviceLists = new List();
List lists = GetGatewayDeviceList();
foreach (var dev in lists)
{
if (dev.roomIdList.Find((id) => id == room.sid) != null)
{
deviceLists.Add(dev);
}
if (room.sid == "全部区域")
{
//房间名为全部区域时,显示网关全部设备
deviceLists = lists;
}
}
return deviceLists;
}
///
/// 获取设备
///
/// 设备唯一标识
///
public static HDL_ON.Entity.Function GetDevice(string sid)
{
HDL_ON.Entity.Function device = new Entity.Function();
List deviceLists = GetGatewayDeviceList();
foreach (var dev in deviceLists)
{
if (dev.sid == sid)
{
device = dev;
break;
}
}
return device;
}
///
/// 获取房间名(即是=区域名称)
///
/// 设备
///
public static string GetGetRoomName(HDL_ON.Entity.Function device)
{
string roomName = "";
List roomLists = GetGatewayRoomListG();
foreach (var dev in device.roomIdList)
{
var room = roomLists.Find((c) => c.sid == dev);
if (room != null)
{
roomName += room.floorName + "." + room.name + ",";
}
}
return roomName.TrimEnd(',');
}
///
/// 获取设备类型图标
///
/// 设备类型
///
public static string GetIconPath(FunctionType functionType)
{
string strPath = "";
switch (functionType)
{
case FunctionType.Relay:
{
strPath = "LogicIcon/lightloguc.png";
}
break;
}
return strPath;
}
///
/// 设备类型的列表(灯光类,窗帘类。。。)
///
/// 设备列表
///
public static List GetDeviceTypeList(List deviceList)
{
List deviceStrTypeList = new List();
deviceStrTypeList.Clear();
var lightjosn = deviceList.Find((device) => device.functionType == FunctionType.Relay || device.functionType == FunctionType.Dimmer);
if (lightjosn != null)
{
deviceStrTypeList.Add(Language.StringByID(StringId.Lights));
}
var curtainjosn = deviceList.Find((device) => device.functionType ==FunctionType.Curtain);
if (curtainjosn != null)
{
deviceStrTypeList.Add(Language.StringByID(StringId.Curtain));
}
var ac = deviceList.Find((device) => device.functionType == FunctionType.AC);
if (ac != null)
{
deviceStrTypeList.Add(Language.StringByID(StringId.AC));
}
return deviceStrTypeList;
}
///
/// 设备类型FunctionType列表
///
/// 设备类型(灯光类,窗帘类。)
///
public static List GetDeviceTypeFunctionList(string deviceType)
{
List functionTypeList = new List();
if (deviceType == Language.StringByID(StringId.Lights))
{
functionTypeList.Add(FunctionType.Relay);
functionTypeList.Add(FunctionType.Dimmer);
}
else if (deviceType == Language.StringByID(StringId.Curtain))
{
functionTypeList.Add(FunctionType.Curtain);
}
return functionTypeList;
}
///
/// 条件支持设备
///
///
public static List GetConditionFunctionTypeDevice()
{
List deviceTypeList = new List();
deviceTypeList.Add(FunctionType.Relay);
return deviceTypeList;
}
///
/// 目标支持设备
///
///
public static List GetTargetFunctionTypeDevice()
{
List deviceTypeList = new List();
return deviceTypeList;
}
///
/// 显示的设备列表
///
/// 源数据列表1
/// 源数据列表2
///
public static List GetShowDeviceList(List functionType, List deviceList)
{
List devList = new List();
foreach (var dev in deviceList)
{
if (functionType.Contains(dev.functionType))
{
///过滤掉不需要显示的设备
devList.Add(dev);
}
}
return devList;
}
///
/// 返回最终支持显示出来的设备列表
///
/// 当前房间
/// 判断符(表示=输入设备和输出设备)
///
public static List GetFunctionDeviceList(Entity.Room room,string str)
{
List functionTypeList=new List();
switch (str) {
case condition_if: {
//返回条件支持设备列表
functionTypeList = GetConditionFunctionTypeDevice();
}
break;
case target_if:
{
//返回目标支持设备列表
functionTypeList = GetTargetFunctionTypeDevice();
}
break;
}
//返回房间设备列表
var roomDeviceList = GetRoomDevice(room);
//返回最终支持显示出来的设备列表
var list= GetShowDeviceList(functionTypeList, roomDeviceList);
return list;
}
}
}