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 = "目标";
///
/// 移除所有"Logic"界面
///
public static void RemoveAllView()
{
MainPage.BasePageView.RemoveViewByTag("Logic");
}
/// Converts an array of bytes into a formatted string of hex digits (ex: E4 CA B2)
/// The array of bytes to be translated into a string of hex digits.
/// Returns a well formatted string of hex digits with spacing.
static string byteArrayToHexString(byte[] data)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (byte b in data)
{
sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
}
return sb.ToString().ToUpper();
}
/**
* int转byte[]
* 该方法将一个int类型的数据转换为byte[]形式,因为int为32bit,而byte为8bit所以在进行类型转换时,知会获取低8位,
* 丢弃高24位。通过位移的方式,将32bit的数据转换成4个8bit的数据。注意 &0xff,在这当中,&0xff简单理解为一把剪刀,
* 将想要获取的8位数据截取出来。
* @param i 一个int数字
* @return byte[]
*/
public static byte[] int2ByteArray(int i)
{
byte[] result = new byte[4];
result[0] = (byte)((i >> 24) & 0xFF);
result[1] = (byte)((i >> 16) & 0xFF);
result[2] = (byte)((i >> 8) & 0xFF);
result[3] = (byte)(i & 0xFF);
return result;
}
///
/// 获取时间戳
///
///
static int getTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return (int)ts.TotalSeconds;
}
///
/// 生成逻辑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 = byteArrayToHexString(int2ByteArray(getTimeStamp()));
logicId = sOidBeginsWith + sTimeSpan;
logicId += "15";
logicId += "1501";
//1501 物模型为逻辑, 0001 表示 1 号逻辑功能
int maxId = 1;
for (int i = 0; i < Logic.LogicList.Count; i++)
{
string s = Logic.LogicList[i].sid.Substring(20, 4);
int iThisSceneId = Convert.ToInt16(s, 16);
if (iThisSceneId > maxId)
maxId = iThisSceneId;
}
logicId += (maxId + 1).ToString("X4");//逻辑号 两个byte
logicId += "0000";
}
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 GetGatewayRoomList()
{
return HDL_ON.Entity.SpatialInfo.CurrentSpatial.RoomList;
}
///
/// 获取网关房间列表
///
///
public static List GetGatewayRoomList(string name)
{
List roomList = new List();
Entity.Room room1 = new Entity.Room();
room1.roomName = name;//默认一个房间名为:全部区域
room1.uid = "全部区域";//默认sid用识别该房间
roomList.Add(room1);//默认添加到房间列表里
var roomLists = GetGatewayRoomList();
foreach (var room in roomLists)
{
var devlist = GetRoomDevice(room);
if (devlist.Count == 0)
{
//过滤掉没有设备的房间;
continue;
}
roomList.Add(room);
}
return roomList;
}
///
/// 获取网关设备列表
///
///
public static List GetGatewayDeviceList()
{
return Entity.FunctionList.List.GetDeviceFunctionList();
}
///
/// 获取网关场景列表
///
///
public static List GetSceneList()
{
return HDL_ON.Entity.FunctionList.List.scenes;
}
///
/// 获取房间的设备列表
///
/// 当前房间
///
public static List GetRoomDevice(HDL_ON.Entity.Room room)
{
List deviceLists = new List();
List lists = GetGatewayDeviceList();
foreach (var dev in lists)
{
if (dev.roomIds.Find((id) => id == room.uid) != null)
{
deviceLists.Add(dev);
}
if (room.uid == "全部区域")
{
//房间名为全部区域时,显示网关全部设备
deviceLists = lists;
}
}
return deviceLists;
}
///
/// 获取当个设备
///
/// 设备唯一标识
///
public static HDL_ON.Entity.Function GetDevice(string sid)
{
HDL_ON.Entity.Function device = new Entity.Function() { name = "Unknown" };
List deviceLists = GetGatewayDeviceList();
foreach (var dev in deviceLists)
{
if (dev.sid == sid)
{
device = dev;
break;
}
}
return device;
}
///
/// 获取当个场景
///
/// 场景唯一标识
///
public static HDL_ON.Entity.Scene GetSecne(string sid)
{
HDL_ON.Entity.Scene scene = new Entity.Scene() { name = "Unknown" };
List sceneLists = GetSceneList();
foreach (var sce in sceneLists)
{
if (sce.sid == sid)
{
scene = sce;
break;
}
}
return scene;
}
///
/// 获取房间名(即是=区域名称)
///
/// 设备
///
public static string GetGetRoomName(HDL_ON.Entity.Function device)
{
string roomName = "";
List roomLists = GetGatewayRoomList();
foreach (var dev in device.roomIds)
{
var room = roomLists.Find((c) => c.uid == dev);
if (room != null)
{
roomName += room.floorName + "." + room.roomName + ",";
}
}
return roomName.TrimEnd(',');
}
///
/// 获取设备类型图标
///
/// 设备类型
///
public static string GetIconPath(FunctionType functionType)
{
string strPath = "";
switch (functionType)
{
case FunctionType.Relay:
case FunctionType.RGB:
case FunctionType.RGBW:
case FunctionType.ColorTemperature:
case FunctionType.Dimmer:
{
strPath = "LogicIcon/lightloguc.png";
}
break;
case FunctionType.Curtain:
case FunctionType.RollingShutter:
case FunctionType.MotorCurtain:
{
strPath = "LogicIcon/curtainlogic.png";
}
break;
case FunctionType.AC:
{
strPath = "LogicIcon/airconditionerlogic.png";
}
break;
case FunctionType.FloorHeating:
{
strPath = "LogicIcon/heatlogic.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 || device.functionType == FunctionType.ColorTemperature || device.functionType == FunctionType.RGB || device.functionType == FunctionType.RGBW);
if (lightjosn != null)
{
deviceStrTypeList.Add(Language.StringByID(StringId.Lights));
}
var curtainjosn = deviceList.Find((device) => device.functionType == FunctionType.Curtain || device.functionType == FunctionType.MotorCurtain || device.functionType == FunctionType.RollingShutter);
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));
}
var floorHeating = deviceList.Find((device) => device.functionType == FunctionType.FloorHeating);
if (floorHeating != null)
{
deviceStrTypeList.Add(Language.StringByID(StringId.FloorHeating));
}
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);
functionTypeList.Add(FunctionType.RGB);
functionTypeList.Add(FunctionType.RGBW);
functionTypeList.Add(FunctionType.ColorTemperature);
}
else if (deviceType == Language.StringByID(StringId.Curtain))
{
functionTypeList.Add(FunctionType.Curtain);
functionTypeList.Add(FunctionType.RollingShutter);
functionTypeList.Add(FunctionType.MotorCurtain);
}
else if (deviceType == Language.StringByID(StringId.AC))
{
functionTypeList.Add(FunctionType.AC);
}
else if (deviceType == Language.StringByID(StringId.FloorHeating))
{
functionTypeList.Add(FunctionType.FloorHeating);
}
return functionTypeList;
}
///
/// 条件/目标支持设备
///
///
public static List GetSupportEquipment(string if_type)
{
List deviceTypeList = new List();
switch (if_type)
{
case condition_if:
{
deviceTypeList.Add(FunctionType.Relay);
deviceTypeList.Add(FunctionType.RGB);
deviceTypeList.Add(FunctionType.RGBW);
deviceTypeList.Add(FunctionType.Dimmer);
deviceTypeList.Add(FunctionType.ColorTemperature);
deviceTypeList.Add(FunctionType.Curtain);
deviceTypeList.Add(FunctionType.RollingShutter);
deviceTypeList.Add(FunctionType.MotorCurtain);
deviceTypeList.Add(FunctionType.AC);
deviceTypeList.Add(FunctionType.FloorHeating);
}
break;
case target_if:
{
deviceTypeList.Add(FunctionType.Relay);
deviceTypeList.Add(FunctionType.RGB);
deviceTypeList.Add(FunctionType.RGBW);
deviceTypeList.Add(FunctionType.Dimmer);
deviceTypeList.Add(FunctionType.ColorTemperature);
deviceTypeList.Add(FunctionType.Curtain);
deviceTypeList.Add(FunctionType.RollingShutter);
deviceTypeList.Add(FunctionType.MotorCurtain);
deviceTypeList.Add(FunctionType.AC);
deviceTypeList.Add(FunctionType.FloorHeating);
}
break;
}
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 = GetSupportEquipment(str);
//返回房间设备列表
var roomDeviceList = GetRoomDevice(room);
//返回最终支持显示出来的设备列表
var list = GetShowDeviceList(functionTypeList, roomDeviceList);
return list;
}
}
}