using System;
|
using System.Text;
|
using System.Collections.Generic;
|
using System.Threading;
|
using Newtonsoft.Json;
|
using Shared;
|
using Shared.SimpleControl.Phone;
|
using Newtonsoft.Json.Linq;
|
|
|
namespace SuperGateWay
|
{
|
public class Control
|
{
|
|
|
#if simulation
|
static List<SuperCommon> deviceList = new List<SuperCommon> {
|
new SuperCommon {
|
module="relay",
|
name = "8路继电器",
|
uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (1, 1)),
|
objects = {
|
new System.Collections.Generic.Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (1, 1)), SuperCommon.MakeOid (6, 0, 1)).ToString ("X")
|
},
|
new System.Collections.Generic.Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (1, 1)), SuperCommon.MakeOid (6, 1, 2)).ToString ("X")
|
},
|
new System.Collections.Generic.Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (1, 1)), SuperCommon.MakeOid (6, 1, 3)).ToString ("X")
|
},
|
new System.Collections.Generic.Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (1, 1)), SuperCommon.MakeOid (6, 0, 4)).ToString ("X")
|
},
|
new System.Collections.Generic.Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (1, 1)), SuperCommon.MakeOid (5, 0, 5)).ToString ("X")
|
},
|
|
|
}
|
},
|
|
};
|
static List<Scene> sceneList = new List<Scene> ();
|
#endif
|
|
static Queue<PacketInfo> controlList = new Queue<PacketInfo> ();
|
|
public static List<string> GetFileNameList (string path)
|
{
|
var totalCount = 0;
|
{
|
var pathBytes = System.Text.Encoding.UTF8.GetBytes (path);
|
var bytes = new byte [1 + 4 + pathBytes.Length + 1];
|
bytes [0] = 6;
|
Array.Copy (pathBytes, 0, bytes, 5, pathBytes.Length);
|
var packetInfo = new PacketInfo {
|
Command = 6,
|
Buffer = bytes,
|
};
|
addPacketInfo (packetInfo);
|
if (packetInfo.ResponeBytes == null) {
|
return null;
|
}
|
totalCount = packetInfo.ResponeBytes [4] + packetInfo.ResponeBytes [3] * 256;
|
}
|
try {
|
var length = 1;
|
var sb = new List<byte> ();
|
while (length < totalCount + 1) {
|
var pathBytes = System.Text.Encoding.UTF8.GetBytes (path);
|
var bytes = new byte [1 + 4 + pathBytes.Length + 1];
|
bytes [0] = 6;
|
bytes [1] = (byte)(length / 256);
|
bytes [2] = (byte)(length % 256);
|
Array.Copy (pathBytes, 0, bytes, 5, pathBytes.Length);
|
var packetInfo = new PacketInfo {
|
Command = 6,
|
Buffer = bytes,
|
};
|
addPacketInfo (packetInfo);
|
if (packetInfo.ResponeBytes == null) {
|
return null;
|
}
|
length += packetInfo.ResponeBytes [4] + packetInfo.ResponeBytes [3] * 256;
|
for (int i = packetInfo.Buffer.Length; i < packetInfo.ResponeBytes.Length; i++) {
|
sb.Add (packetInfo.ResponeBytes [i]);
|
}
|
}
|
var sr = new System.IO.StringReader (System.Text.Encoding.ASCII.GetString (sb.ToArray ()));
|
string line = null;
|
var list = new List<string> { };
|
while ((line = sr.ReadLine ()) != null) {
|
list.Add (line);
|
}
|
sr.Close ();
|
return list;
|
} catch {
|
return null;
|
}
|
}
|
public static string GetFile (string filePath)
|
{
|
uint fileSize = 0;
|
{
|
var pathBytes = System.Text.Encoding.UTF8.GetBytes (filePath);
|
var bytes = new byte [1 + 4 + pathBytes.Length + 1];
|
bytes [0] = 3;
|
Array.Copy (pathBytes, 0, bytes, 5, pathBytes.Length);
|
var packetInfo = new PacketInfo {
|
Command = 3,
|
Buffer = bytes,
|
};
|
addPacketInfo (packetInfo);
|
if (packetInfo.ResponeBytes == null) {
|
return null;
|
}
|
fileSize = packetInfo.ResponeBytes [4] + (uint)packetInfo.ResponeBytes [3] * 256 + (uint)packetInfo.ResponeBytes [2] * 256 * 256 + (uint)packetInfo.ResponeBytes [1] * 256 * 256 * 256;
|
}
|
try {
|
var length = 0;
|
var sb = new List<byte> ();
|
while (length < fileSize) {
|
var bytes = new byte [1 + 6];
|
bytes [0] = 4;
|
bytes [1] = (byte)((length >> 24) & 0xFF);
|
bytes [2] = (byte)((length >> 16) & 0xFF);
|
bytes [3] = (byte)((length >> 8) & 0xFF);
|
bytes [4] = (byte)((length >> 0) & 0xFF);
|
bytes [5] = (1024 >> 8) & 0xFF;
|
bytes [6] = (1024 >> 0) & 0xFF;
|
var packetInfo = new PacketInfo {
|
Command = 4,
|
Buffer = bytes,
|
};
|
addPacketInfo (packetInfo);
|
if (packetInfo.ResponeBytes == null) {
|
return null;
|
}
|
length += packetInfo.ResponeBytes [6] + packetInfo.ResponeBytes [5] * 256;
|
for (int i = packetInfo.Buffer.Length; i < packetInfo.ResponeBytes.Length; i++) {
|
sb.Add (packetInfo.ResponeBytes [i]);
|
}
|
}
|
//接收完成
|
|
|
|
var completePacket = new PacketInfo {
|
Command = 5,
|
Buffer = new byte [] { 5 },
|
};
|
addPacketInfo (completePacket);
|
|
|
|
return System.Text.Encoding.UTF8.GetString (sb.ToArray ());
|
//return System.Text.Encoding.ASCII.GetString (sb.ToArray ());
|
} catch {
|
return null;
|
}
|
}
|
|
public static void addPacketInfo (PacketInfo packetInfo)
|
{
|
controlList.Enqueue (packetInfo);
|
if (sendThread == null) {
|
sendThread = new Thread (() => {
|
while (true) {
|
try {
|
var item = controlList.Dequeue ();
|
if (item == null) {
|
break;
|
}
|
sendData (item);
|
} catch { }
|
System.Threading.Thread.Sleep (10);
|
}
|
sendThread = null;
|
});
|
sendThread.Start ();
|
}
|
packetInfo.ManualResetEvent.Reset ();
|
packetInfo.ManualResetEvent.WaitOne ();
|
}
|
|
#region 设备
|
/// <summary>
|
/// 获取所有设备UID
|
/// </summary>
|
/// <returns>The devices uid list.</returns>
|
public static List<string> GetDevicesUidList ()
|
{
|
#if simulation
|
var list = new List<string> { };
|
foreach (var device in deviceList) {
|
list.Add (device.uid.ToString ("X"));
|
}
|
return list;
|
#endif
|
return GetFileNameList ("/device");
|
}
|
/// <summary>
|
/// 根据UID来获取设备信息
|
/// </summary>
|
/// <returns>The device by uid.</returns>
|
/// <param name="uid">Uid.</param>
|
public static string GetDeviceByUID (string uid)
|
{
|
#if simulation
|
var device = deviceList.Find ((d) => d.uid.ToString ("X") == uid);
|
if (device == null) {
|
return null;
|
} else {
|
return Newtonsoft.Json.JsonConvert.SerializeObject (device);
|
}
|
#endif
|
return GetFile ($"/device/{uid}.json");
|
}
|
#endregion
|
|
#region ---Sonos音乐播发器---
|
/// <summary>
|
/// 获取所有喜爱文件路径
|
/// </summary>
|
public static List<string> getsonosLikeList ()
|
{
|
|
return GetFileNameList ("/sonos/favorite");
|
}
|
|
/// <summary>
|
/// 获取所有喜爱文件内容
|
/// </summary>
|
public static string getsonosLikeSongs (string patm)
|
{
|
|
return GetFile ($"/sonos/favorite/{patm}.json");
|
}
|
|
|
/// <summary>
|
/// 获取所有我的列表文件路径
|
/// </summary>
|
public static List<string> getsonosMyList ()
|
{
|
|
return GetFileNameList ("/sonos/playLists");
|
}
|
|
/// <summary>
|
/// 获取所有我的列表文件内容
|
/// </summary>
|
public static string getsonosMySongs (string patm)
|
{
|
|
return GetFile ($"/sonos/playLists/{patm}.json");
|
}
|
|
|
/// <summary>
|
/// 获取所有喜爱文件路径
|
/// </summary>
|
public static List<string> getsonosLocalList ()
|
{
|
|
return GetFileNameList ("/sonos/warningTone");
|
}
|
|
/// <summary>
|
/// 获取所有喜爱文件内容
|
/// </summary>
|
public static string getsonosLocalSongs (string patm)
|
{
|
|
return GetFile ($"/sonos/warningTone/{patm}.json");
|
}
|
|
|
#endregion
|
|
|
#region ---场景列表---
|
/// <summary>
|
/// 获取所有场景ID
|
/// </summary>
|
/// <returns>The devices uid list.</returns>
|
public static List<string> getSceneList ()
|
{
|
#if simulation
|
var list = new List<string> { };
|
foreach (var scene in sceneList) {
|
list.Add (scene.uid.ToString ("X"));
|
}
|
return list;
|
#endif
|
return GetFileNameList ("/scene");
|
}
|
|
/// <summary>
|
/// 根据UID来获取场景信息
|
/// </summary>
|
/// <returns>The device by uid.</returns>
|
/// <param name="uid">Uid.</param>
|
public static string getSceneByUID (string uid)
|
{
|
#if simulation
|
var tempScene = sceneList.Find ((s) => s.uid.ToString ("X") == uid);
|
if (tempScene == null) {
|
return null;
|
} else {
|
return Newtonsoft.Json.JsonConvert.SerializeObject (tempScene);
|
}
|
#endif
|
return GetFile ($"/scene/{uid}.json");
|
}
|
/// <summary>
|
/// 添加场景
|
/// </summary>
|
/// <param name="sceneName">Scene name.</param>
|
/// <param name="action">Action.</param>
|
public static void AppendScene (Scene scene, Action<SuperGateWay.Scene> action,string fileName = null)
|
{
|
|
if (scene == null) {
|
return;
|
}
|
|
scene.objects.Clear ();
|
//空调 ,灯光 ,窗帘 ,按键 ,新风 ,地热 ,门锁,干接点 ,传感器,音乐
|
foreach (var common in scene.deviceList)
|
{
|
///空调
|
if (common.Type == DeviceType.HVAC) {
|
|
var uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (common.SubnetID, common.DeviceID));
|
if (common.keyValuePairs.ContainsKey ("Power")) {
|
var oid = SuperCommon.MakeOid (2, 0, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Power"],
|
});
|
}
|
if (common.keyValuePairs.ContainsKey ("Mode")) {
|
var oid = SuperCommon.MakeOid (2, 1, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Mode"],
|
});
|
}
|
if (common.keyValuePairs.ContainsKey ("Speed")) {
|
var oid = SuperCommon.MakeOid (2, 2, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Speed"],
|
});
|
}
|
if (common.keyValuePairs.ContainsKey ("Temperature")) {
|
var oid = SuperCommon.MakeOid (2, 3, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Temperature"],
|
});
|
}
|
if (common.keyValuePairs.ContainsKey ("Scavenging")) {
|
var oid = SuperCommon.MakeOid (2, 4, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Scavenging"],
|
});
|
}
|
if (common.keyValuePairs.ContainsKey ("ambienTtemperature")) {
|
var oid = SuperCommon.MakeOid (2, 5, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["ambienTtemperature"],
|
});
|
}
|
} else if (common.Type == DeviceType.LightSwitch) {
|
var uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (common.SubnetID, common.DeviceID));
|
var oid = SuperCommon.MakeOid (6, 0, common.LoopID);
|
if (common.keyValuePairs.ContainsKey ("Power")) {
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Power"],
|
});
|
}
|
} else if (common.Type == DeviceType.LightDimming) {
|
var uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (common.SubnetID, common.DeviceID));
|
var oid = SuperCommon.MakeOid (6, 1, common.LoopID);
|
if (common.keyValuePairs.ContainsKey ("Brightness")) {
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Brightness"]
|
});
|
}
|
} else if (common.Type == DeviceType.CurtainModel) {
|
var uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (common.SubnetID, common.DeviceID));
|
var oid = SuperCommon.MakeOid (5, 0, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Power"],
|
});
|
} else if (common.Type == DeviceType.CurtainRoller) {
|
var uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (common.SubnetID, common.DeviceID));
|
var oid = SuperCommon.MakeOid (5, 1, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Brightness"],
|
});
|
} else if (common.Type == DeviceType.MechanicalSwitch) {
|
var uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (common.SubnetID, common.DeviceID));
|
var oid = SuperCommon.MakeOid (7, 0, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Power"],
|
});
|
} else if (common.Type == DeviceType.AutomaticSwitch) {
|
var uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (common.SubnetID, common.DeviceID));
|
var oid = SuperCommon.MakeOid (7, 1, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Power"],
|
});
|
} else if (common.Type == DeviceType.FreshAir) {
|
var uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (common.SubnetID, common.DeviceID));
|
if (common.keyValuePairs.ContainsKey ("Power")) {
|
var oid = SuperCommon.MakeOid (4, 0, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Power"],
|
});
|
}
|
if (common.keyValuePairs.ContainsKey ("Mode")) {
|
var oid = SuperCommon.MakeOid (4, 1, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Mode"],
|
});
|
}
|
if (common.keyValuePairs.ContainsKey ("Speed")) {
|
var oid = SuperCommon.MakeOid (4, 2, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Speed"],
|
});
|
}
|
|
} else if (common.Type == DeviceType.FoolHeat) {
|
var uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (common.SubnetID, common.DeviceID));
|
if (common.keyValuePairs.ContainsKey ("Power")) {
|
var oid = SuperCommon.MakeOid (3, 0, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Power"],
|
});
|
}
|
if (common.keyValuePairs.ContainsKey ("Temperature")) {
|
var oid = SuperCommon.MakeOid (3, 1, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Temperature"],
|
});
|
}
|
} else if (common.Type == DeviceType.DoorLock) {
|
var uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (common.SubnetID, common.DeviceID));
|
var oid = SuperCommon.MakeOid (10, 0, common.LoopID);
|
if (common.keyValuePairs.ContainsKey ("Power")) {
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Power"],
|
});
|
}
|
} else if (common.Type == DeviceType.DryContact) {
|
var uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (common.SubnetID, common.DeviceID));
|
var oid = SuperCommon.MakeOid (14, 0, common.LoopID);
|
if (common.keyValuePairs.ContainsKey ("Power")) {
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Power"],
|
});
|
}
|
} else if (common.Type == DeviceType.Sensor) {
|
var uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (common.SubnetID, common.DeviceID));
|
if (common.keyValuePairs.ContainsKey ("Temperature")) {
|
var oid = SuperCommon.MakeOid (9, 0, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Temperature"],
|
});
|
}
|
if (common.keyValuePairs.ContainsKey ("Brightness")) {
|
var oid = SuperCommon.MakeOid (9, 1, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Brightness"],
|
});
|
}
|
} else if (common.Type == DeviceType.MusicModel) {
|
var uid = SuperCommon.MakeUid (1, 1, SuperCommon.MakeDeviceNumber (common.SubnetID, common.DeviceID));
|
if (common.keyValuePairs.ContainsKey ("Status")) {
|
var oid = SuperCommon.MakeOid (8, 0, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Status"],
|
});
|
}
|
if (common.keyValuePairs.ContainsKey ("Volume")) {
|
var oid = SuperCommon.MakeOid (8, 1, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Volume"],
|
});
|
}
|
|
if (common.keyValuePairs.ContainsKey ("Upper")) {
|
var oid = SuperCommon.MakeOid (8, 2, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Upper"],
|
});
|
}
|
|
if (common.keyValuePairs.ContainsKey ("Source")) {
|
var oid = SuperCommon.MakeOid (8, 3, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Source"],
|
});
|
}
|
|
if (common.keyValuePairs.ContainsKey ("Mode")) {
|
var oid = SuperCommon.MakeOid (8, 5, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Mode"],
|
});
|
}
|
|
if (common.keyValuePairs.ContainsKey ("Index")) {
|
var oid = SuperCommon.MakeOid (8, 6, common.LoopID);
|
scene.objects.Add (new Dictionary<string, object> {
|
["sid"] = SuperCommon.MakeSid (uid, oid).ToString ("X"),
|
["value"] = common.keyValuePairs ["Index"],
|
});
|
}
|
}
|
|
//foreach (var device in scene.deviceList) {
|
//var sidValue = SuperCommon.CommonToSidValue (device);
|
//if (sidValue != null) {
|
// scene.objects.Add (sidValue);
|
//}
|
}
|
|
var sceneBytes = System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (scene));
|
{
|
System.Threading.Tasks.Task.Run (() => {
|
var pathBytes = System.Text.Encoding.UTF8.GetBytes ($"/scene/{scene.uid.ToString ("X")}.json");
|
var bytes = new byte [1 + 4 + pathBytes.Length + 1];
|
bytes [0] = 0;
|
bytes [1] = (byte)((sceneBytes.Length >> 24) & 0xFF);
|
bytes [2] = (byte)((sceneBytes.Length >> 16) & 0xFF);
|
bytes [3] = (byte)((sceneBytes.Length >> 8) & 0xFF);
|
bytes [4] = (byte)((sceneBytes.Length >> 0) & 0xFF);
|
Array.Copy (pathBytes, 0, bytes, 5, pathBytes.Length);
|
var packetInfo = new PacketInfo {
|
Command = 0,
|
Buffer = bytes,
|
FileBytes = sceneBytes
|
};
|
addPacketInfo (packetInfo);
|
if (packetInfo.ResponeBytes == null || packetInfo.ResponeBytes [1] != 0xF8) {
|
return;
|
}
|
///新加命令,通知高级网关更新场景列表;
|
var byteup = System.Text.Encoding.UTF8.GetBytes ("update\0");
|
byte [] bytesupdate = new byte [1 + byteup.Length + 1];
|
bytesupdate [0] = 9;
|
Array.Copy (byteup, 0, bytesupdate, 1, byteup.Length);
|
addPacketInfo (new PacketInfo { Command = 9, Buffer = bytesupdate });
|
});
|
action (scene);
|
}
|
}
|
/// <summary>
|
/// 场景控制
|
/// </summary>
|
/// <param name="scene">Scene.</param>
|
public static void ControlScene (Scene scene)
|
{
|
try {
|
if (scene == null || scene.sid == null) {
|
return;
|
}
|
var sidUlong = Convert.ToUInt64 (scene.sid, 16);
|
var zoneId = (byte)((sidUlong >> 40) & 0xFF);
|
var sceneId = (byte)((sidUlong >> 32) & 0xFF);
|
Shared.Control.ControlBytesSend (Command.SetScene, GateWay.Instance.SubnetID, GateWay.Instance.DeviceID, new byte [] { zoneId, sceneId }, SendCount.Zero);
|
|
//通知更新
|
//addPacketInfo (new PacketInfo {
|
// Command = 5,
|
// Buffer = new byte [] { 5 },
|
// ReSendCount = 0
|
//});
|
|
} catch { }
|
}
|
/// <summary>
|
/// 删除场景
|
/// </summary>
|
/// <param name="scene">Scene.</param>
|
public static void DeleteScene (Scene scene)
|
{
|
if (scene == null) {
|
return;
|
}
|
System.Threading.Tasks.Task.Run (() => {
|
{
|
var pathBytes = System.Text.Encoding.UTF8.GetBytes ($"/scene/{scene.uid.ToString ("X")}.json");
|
var bytes = new byte [1 + 4 + pathBytes.Length + 1];
|
bytes [0] = 8;
|
bytes [1] = 0x55;
|
bytes [2] = 0x55;
|
bytes [3] = 0x55;
|
bytes [4] = 0x55;
|
Array.Copy (pathBytes, 0, bytes, 5, pathBytes.Length);
|
var packetInfo = new PacketInfo {
|
Command = 8,
|
Buffer = bytes,
|
};
|
addPacketInfo (packetInfo);
|
if (packetInfo.ResponeBytes == null || packetInfo.ResponeBytes [1] != 0xF8) {
|
return;
|
}
|
}
|
});
|
}
|
/// <summary>
|
/// 场景修改
|
/// </summary>
|
/// <param name="sceneName">Scene name.</param>
|
/// <param name="action">Action.</param>
|
public static void ModifyScene (Scene scene, Action<SuperGateWay.Scene> action)
|
{
|
|
AppendScene (scene, action);
|
|
}
|
#endregion
|
|
#region ---逻辑列表---
|
/// <summary>
|
/// 获取逻辑uid列表
|
/// </summary>
|
/// <returns>The logiclist.</returns>
|
public static List<string> getLogiclist ()
|
{
|
return GetFileNameList ("/logic");
|
}
|
/// <summary>
|
/// 获取逻辑信息
|
/// </summary>
|
/// <returns>The logic by uid.</returns>
|
/// <param name="uid">Uid.</param>
|
public static string getLogicByUID (string uid)
|
{
|
|
return GetFile ($"/logic/{uid}.json");
|
}
|
/// <summary>
|
/// 添加逻辑
|
/// </summary>
|
/// <returns>The appendlogic.</returns>
|
public static void AppendLogic (Logic logic)
|
{
|
|
var logicBytes = System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (logic));
|
{
|
System.Threading.Tasks.Task.Run (() => {
|
var pathBytes = System.Text.Encoding.UTF8.GetBytes ($"/logic/{logic.uid.ToString ("X")}.json");
|
var bytes = new byte [1 + 4 + pathBytes.Length + 1];
|
bytes [0] = 0;
|
bytes [1] = (byte)((logicBytes.Length >> 24) & 0xFF);
|
bytes [2] = (byte)((logicBytes.Length >> 16) & 0xFF);
|
bytes [3] = (byte)((logicBytes.Length >> 8) & 0xFF);
|
bytes [4] = (byte)((logicBytes.Length >> 0) & 0xFF);
|
Array.Copy (pathBytes, 0, bytes, 5, pathBytes.Length);
|
var packetInfo = new PacketInfo {
|
Command = 0,
|
Buffer = bytes,
|
FileBytes = logicBytes
|
};
|
addPacketInfo (packetInfo);
|
if (packetInfo.ResponeBytes == null || packetInfo.ResponeBytes [1] != 0xF8) {
|
return;
|
}
|
|
///新加命令,通知高级网关更新场景列表;
|
var byteup = System.Text.Encoding.UTF8.GetBytes ("update\0");
|
byte [] bytesupdate = new byte [1 + byteup.Length + 1];
|
bytesupdate [0] = 9;
|
Array.Copy (byteup, 0, bytesupdate, 1, byteup.Length);
|
addPacketInfo (new PacketInfo { Command = 9, Buffer = bytesupdate });
|
});
|
//action (logic);
|
}
|
}
|
/// <summary>
|
/// 修改逻辑
|
/// </summary>
|
/// <returns>The modifylogic.</returns>
|
/// <param name="logic">Logic.</param>
|
/// <param name="logicdevice">Logicdevice.</param>
|
public static void modifylogic (Logic logic)
|
{
|
|
var logicBytes = System.Text.Encoding.UTF8.GetBytes (Newtonsoft.Json.JsonConvert.SerializeObject (logic));
|
{
|
System.Threading.Tasks.Task.Run (() => {
|
var pathBytes = System.Text.Encoding.UTF8.GetBytes ($"/logic/{logic.uid.ToString ("X")}.json");
|
var bytes = new byte [1 + 4 + pathBytes.Length + 1];
|
bytes [0] = 0;
|
bytes [1] = (byte)((logicBytes.Length >> 24) & 0xFF);
|
bytes [2] = (byte)((logicBytes.Length >> 16) & 0xFF);
|
bytes [3] = (byte)((logicBytes.Length >> 8) & 0xFF);
|
bytes [4] = (byte)((logicBytes.Length >> 0) & 0xFF);
|
Array.Copy (pathBytes, 0, bytes, 5, pathBytes.Length);
|
var packetInfo = new PacketInfo {
|
Command = 0,
|
Buffer = bytes,
|
FileBytes = logicBytes
|
};
|
addPacketInfo (packetInfo);
|
if (packetInfo.ResponeBytes == null || packetInfo.ResponeBytes [1] != 0xF8) {
|
return;
|
}
|
});
|
//action (logic);
|
}
|
}
|
/// <summary>
|
/// 删除逻辑
|
/// </summary>
|
/// <returns>The logic.</returns>
|
public static void DeleteLogic (Logic logic)
|
{
|
if (logic == null) {
|
return ;
|
}
|
System.Threading.Tasks.Task.Run (() => {
|
{
|
var pathBytes = System.Text.Encoding.UTF8.GetBytes ($"/logic/{logic.uid.ToString ("X")}.json");
|
var bytes = new byte [1 + 4 + pathBytes.Length + 1];
|
bytes [0] = 8;
|
bytes [1] = 0x55;
|
bytes [2] = 0x55;
|
bytes [3] = 0x55;
|
bytes [4] = 0x55;
|
Array.Copy (pathBytes, 0, bytes, 5, pathBytes.Length);
|
var packetInfo = new PacketInfo {
|
Command = 8,
|
Buffer = bytes,
|
};
|
addPacketInfo (packetInfo);
|
if (packetInfo.ResponeBytes == null || packetInfo.ResponeBytes [1] != 0xF8) {
|
return;
|
}
|
}
|
});
|
}
|
|
#endregion
|
|
/// <summary>
|
/// 真正发送数据的地方
|
/// </summary>
|
/// <returns>The data.</returns>
|
/// <param name="packetInfo">Send datas.</param>
|
static void sendData (PacketInfo packetInfo)
|
{
|
SendAndReceiveDatas.CurrentPacketInfo = packetInfo;
|
var startDateTime = DateTime.Now;
|
var haveSendCount = 0;
|
//最多发送3次
|
while (haveSendCount <= packetInfo.ReSendCount && !packetInfo.IsFinish) {
|
switch (haveSendCount) {
|
case 0:
|
if (0 <= (DateTime.Now - startDateTime).TotalMilliseconds) {
|
haveSendCount++;
|
System.Console.WriteLine ("发送第1次");
|
Shared.Control.ControlBytesSend (Command.SuperGatewayAgent, packetInfo.SubnetID, packetInfo.DeviceID, packetInfo.Buffer, Shared.SendCount.Zero);
|
}
|
break;
|
case 1:
|
if (500 <= (DateTime.Now - startDateTime).TotalMilliseconds) {
|
haveSendCount++;
|
System.Console.WriteLine ("发送第2次");
|
Shared.Control.ControlBytesSend (Command.SuperGatewayAgent, packetInfo.SubnetID, packetInfo.DeviceID, packetInfo.Buffer, Shared.SendCount.Zero);
|
}
|
break;
|
case 2:
|
if (1000 <= (DateTime.Now - startDateTime).TotalMilliseconds) {
|
haveSendCount++;
|
System.Console.WriteLine ("发送第3次");
|
Shared.Control.ControlBytesSend (Command.SuperGatewayAgent, packetInfo.SubnetID, packetInfo.DeviceID, packetInfo.Buffer, Shared.SendCount.Zero);
|
}
|
break;
|
case 3:
|
if (1500 <= (DateTime.Now - startDateTime).TotalMilliseconds) {
|
haveSendCount++;
|
System.Console.WriteLine ("发送第4次");
|
Shared.Control.ControlBytesSend (Command.SuperGatewayAgent, packetInfo.SubnetID, packetInfo.DeviceID, packetInfo.Buffer, Shared.SendCount.Zero);
|
}
|
break;
|
}
|
|
Thread.Sleep (10);
|
}
|
if (packetInfo.Command == 0) {
|
var dateTime = DateTime.Now;
|
while (!packetInfo.IsUploadFinish) {
|
System.Threading.Thread.Sleep (500);
|
if (5 < (DateTime.Now - dateTime).TotalSeconds) {
|
break;
|
}
|
}
|
}
|
|
packetInfo.ManualResetEvent.Set ();
|
}
|
|
|
static Thread sendThread;
|
}
|
}
|