using System;
|
using System.Collections.Generic;
|
using System.Text;
|
using HDL_ON.DriverLayer;
|
using HDL_ON.Entity;
|
using Newtonsoft.Json;
|
using Newtonsoft.Json.Linq;
|
using Shared.Net;
|
|
namespace HDL_ON.DriverLayer
|
{
|
public class Control_Udp
|
{
|
|
public Control_Udp()
|
{
|
MainPage.Log($"control bus udp 被创建,时间:{DateTime.Now}");
|
}
|
/// <summary>
|
/// 通讯ID
|
/// </summary>
|
public const int msg_id = 1;
|
|
/// <summary>
|
/// 控制失败次数
|
/// </summary>
|
public int controlLostCount = 0;
|
|
/// <summary>
|
/// 所有对一端口的控制都会放到这个集合里
|
/// </summary>
|
static List<Control_Udp> controlList = new List<Control_Udp>(50);
|
|
public System.Net.IPEndPoint EndPoint
|
{
|
get
|
{
|
try
|
{
|
return new System.Net.IPEndPoint(System.Net.IPAddress.Parse(new NetWiFi().BroadcastIpAddress.ToString()),6000);
|
}
|
catch
|
{
|
//防止异常导致程序退出
|
return new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 6000);
|
}
|
}
|
}
|
|
/// <summary>
|
/// 发送数据,不需要等待回复
|
/// </summary>
|
public void ControlBytesSend(Command command, byte subnetID, byte deviceID, byte[] gatewayBytes, int sendCount = 3)
|
{
|
var control = new Control_Udp();
|
control.Send(new Target()
|
{
|
IPEndPoint = EndPoint ,
|
Command = command,
|
SubnetID = subnetID,
|
DeviceID = deviceID,
|
AddData = gatewayBytes,
|
}, sendCount, false);
|
}
|
|
/// <summary>
|
/// 重发验证
|
/// </summary>
|
public void ReceiveRepeatManager(string receiveFlag)
|
{
|
for (int i = 0; i < controlList.Count; i++)
|
{
|
try
|
{
|
var control = controlList[i];
|
if (control.SendFlag == receiveFlag)
|
{
|
control.UsefulBytes = usefulBytes;//
|
control.run();
|
}
|
}
|
catch (Exception ex)
|
{
|
Console.WriteLine($"control error : {ex.Message}");
|
}
|
}
|
}
|
|
/// <summary>
|
/// 000E 搜索回复
|
/// </summary>
|
public void ReceiveReadRemark(byte[] usefullBytes)
|
{
|
try
|
{
|
//账号没登录不回复
|
if ( UserInfo.Current == null || ! UserInfo.Current.IsLogin)
|
{
|
return;
|
}
|
|
Control.Ins.OpenTcpServer();
|
var sendStr = UserInfo.Current.AccountString;
|
if (usefullBytes.Length == 0)
|
{
|
SendRemark(sendStr);
|
}
|
else
|
{
|
bool isExit = false;
|
for (int i = 0, len = usefullBytes.Length; i < len; i++)
|
{
|
if (i % 2 == 0)
|
{
|
if ((usefullBytes[i] & 0xFF) == 252 && (usefullBytes[i + 1] & 0xFF) == 252)
|
{
|
isExit = true;
|
break;
|
}
|
}
|
}
|
if (!isExit)
|
{
|
//不存在,代表没收到本机的发送,继续回复
|
SendRemark(sendStr);
|
}
|
}
|
}
|
catch (Exception ex) {
|
Console.WriteLine($"回复bus搜索异常 : {ex.Message}");
|
}
|
}
|
/// <summary>
|
/// 000F回复备注
|
/// </summary>
|
void SendRemark(string sendStr)
|
{
|
byte[] sendBytes = new byte[20];
|
byte[] b1 = Encoding.GetEncoding("gb2312").GetBytes(sendStr);
|
Array.Copy(b1, 0, sendBytes, 0, 20 < b1.Length ? 20 : b1.Length);
|
|
var control = new Control_Udp();
|
control.Send(new Target()
|
{
|
IPEndPoint = EndPoint,
|
Command = Command.ReadRemarkACK,
|
SubnetID = 0xFF,
|
DeviceID = 0xFF,
|
AddData = sendBytes,
|
}, 0, false); //设置当前发送指令方式为:任何情况下本地发送、不限制、不加密明文发送
|
}
|
|
/// <summary>
|
/// 控制bus场景
|
/// </summary>
|
public void ControlBusScenes(Scene scene)
|
{
|
foreach (var function in scene.sceneFunctionList)
|
{
|
switch (function.functionType)
|
{
|
case FunctionType.Curtain:
|
byte curtainState = 0;
|
if (function.trait_on_off.value.ToString() == "on")
|
{
|
curtainState = 1;
|
}
|
else if (function.trait_on_off.value.ToString() == "off")
|
{
|
curtainState = 2;
|
}
|
ControlBytesSend(Command.SetCurtainModelStutas, function.bus_Data.SubnetID, function.bus_Data.DeviceID,
|
new byte[] {
|
function.bus_Data.LoopID,
|
curtainState,
|
});
|
break;
|
case FunctionType.AC:
|
var aC = Newtonsoft.Json.JsonConvert.DeserializeObject<AC>(Newtonsoft.Json.JsonConvert.SerializeObject(function));//as AC;
|
ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 3, aC.trait_on_off.value.ToString() == "on" ? (byte)1 : (byte)0, aC.bus_Data.LoopID });
|
ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 6, aC.curModeIndex, aC.bus_Data.LoopID });
|
ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 5, aC.curFanIndex, aC.bus_Data.LoopID });
|
byte modeKey = 4;
|
switch (aC.curModeIndex)
|
{
|
case 3:
|
modeKey = 8;
|
break;
|
case 0:
|
modeKey = 4;
|
break;
|
case 1:
|
modeKey = 7;
|
break;
|
case 4:
|
modeKey = 19;
|
break;
|
case 2:
|
modeKey = 2;
|
break;
|
}
|
ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { modeKey, Convert.ToByte(aC.trait_temp.value), aC.bus_Data.LoopID });
|
|
break;
|
case FunctionType.Relay:
|
case FunctionType.Dimmer:
|
byte brightness = 0;
|
if (function.trait_on_off.value.ToString() == "on")
|
{
|
var brightnessTrait = function.function.Find((obj) => obj.name == "brightness");
|
if (brightnessTrait != null)
|
{
|
brightness = Convert.ToByte(brightnessTrait.value);
|
}
|
}
|
ControlBytesSend(Command.SetSingleLight, function.bus_Data.SubnetID, function.bus_Data.DeviceID,
|
new byte[] {
|
function.bus_Data.LoopID,brightness
|
});
|
break;
|
case FunctionType.RGB:
|
//ControlBytesSend(Command.SetLogicLoopColor, function.bus_Data.SubnetID, function.bus_Data.DeviceID,
|
// new byte[] {
|
// });
|
break;
|
case FunctionType.FloorHeating:
|
var fh = Newtonsoft.Json.JsonConvert.DeserializeObject<FloorHeating>(Newtonsoft.Json.JsonConvert.SerializeObject(function));//as AC;
|
var fhModeTempTrait = function.function.Find((obj) => obj.name == "mode");
|
if (fhModeTempTrait.value_key.Count == 4)
|
{
|
byte b1 = function.trait_on_off.value.ToString() == "on" ? (byte)1 : (byte)0;
|
if (fh.workMode > 0)
|
{
|
b1 += (byte)(16 + fh.workMode);
|
}
|
ControlBytesSend(Command.SetFloorHeat, function.bus_Data.SubnetID, function.bus_Data.DeviceID, new byte[] { fh.bus_Data.LoopID, b1, (byte)fh.curTempType, fh.curModeIndex, fh.modeTemp["normal"], fh.modeTemp["day"], fh.modeTemp["night"], fh.modeTemp["away"], 0, 0 });
|
}
|
break;
|
case FunctionType.Socket:
|
//var s = function as SwitchSocket;
|
ControlBytesSend(Command.SetSingleLight, function.bus_Data.SubnetID, function.bus_Data.DeviceID, new byte[] { function.bus_Data.LoopID, function.trait_on_off.value.ToString() == "on" ? (byte)100 : (byte)0 });
|
break;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 控制bus设备
|
/// </summary>
|
/// <param name="commandString"></param>
|
/// <returns></returns>
|
public void WriteBusData(Function function, Dictionary<string, string> commandDictionary)
|
{
|
try
|
{
|
var subnetId = function.bus_Data.SubnetID;
|
var deviceId = function.bus_Data.DeviceID;
|
var loopId = function.bus_Data.LoopID;
|
|
switch (function.functionCategory)
|
{
|
case FunctionCategory.Light:
|
var light = function as Light;
|
switch (light.functionType)
|
{
|
case FunctionType.RGB:
|
MainPage.Log($"rgb level : {light.brightness}");
|
byte b = 100;//开关操作依据on_off字段,实际开关值依据brightness,当on_off为打开,brightness不能为0
|
if (light.trait_on_off.value.ToString() == "off")
|
{
|
b = 0;
|
}
|
else
|
{
|
b = (byte)light.brightness == 0 ? (byte)100 : (byte)light.brightness;
|
}
|
var bytes = new byte[] { function.bus_Data.LoopID,
|
b,
|
254, 0,Convert.ToByte(light.fadeTime) ,3,
|
(byte)light.redColor,(byte)light.greenColor,
|
(byte)light.blueColor,
|
0,0};
|
ControlBytesSend(Command.SetLogicLoopColor, subnetId, deviceId, bytes, 1);
|
break;
|
case FunctionType.Dimmer:
|
byte b1 = 100;
|
if (light.trait_on_off.value.ToString() == "off")
|
{
|
b1 = 0;
|
}
|
else
|
{
|
b1 = (byte)light.brightness == 0 ? (byte)100 : (byte)light.brightness;
|
}
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] {
|
function.bus_Data.LoopID,
|
b1,
|
0, Convert.ToByte(light.fadeTime) });
|
//DimmerPage.UpdataStates(light);
|
break;
|
case FunctionType.Relay:
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { function.bus_Data.LoopID, light.trait_on_off.value.ToString() == "on" ? (byte)100 : (byte)0, 0, 0 });
|
//RelayPage.UpdataState(light);
|
break;
|
}
|
break;
|
case FunctionCategory.Curtain:
|
var curtain = function as Curtain;
|
switch (curtain.functionType)
|
{
|
case FunctionType.Curtain:
|
byte b1 = 0;
|
if (curtain.trait_on_off.value.ToString() == "stop")
|
{
|
b1 = 0;
|
}
|
else if (curtain.trait_on_off.value.ToString() == "on")
|
{
|
b1 = 1;
|
}
|
else
|
{
|
b1 = 2;
|
}
|
ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { curtain.bus_Data.LoopID, b1 });
|
break;
|
case FunctionType.MotorCurtain:
|
case FunctionType.RollingShutter:
|
//if (commandDictionary.Count > 0)
|
//{
|
if (curtain.trait_on_off.value.ToString() == "stop")
|
{
|
ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { curtain.bus_Data.LoopID, 0 });
|
}
|
else
|
{
|
ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { 17, Convert.ToByte(curtain.percent) });
|
|
}
|
//}
|
//else
|
//{
|
// if (curtain.trait_on_off.value.ToString() == "stop")
|
// {
|
// ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { curtain.bus_Data.LoopID, 0 });
|
// }
|
// else if (curtain.trait_on_off.value.ToString() == "on")
|
// {
|
// ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { 17, 100 });
|
// }
|
// else if (curtain.trait_on_off.value.ToString() == "off")
|
// {
|
// ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { 17, 0 });
|
// }
|
//}
|
break;
|
}
|
break;
|
case FunctionCategory.Thermostat:
|
switch (function.functionType)
|
{
|
case FunctionType.AC:
|
var aC = function as AC;
|
ControlBytesSend(Command.SetACMode, subnetId, deviceId, new byte[] { aC.bus_Data.LoopID, (byte)aC.curTempType, 32, 32, 32, 32, 32, 0, aC.trait_on_off.value.ToString() == "on" ? (byte)1 : (byte)0, aC.curModeIndex, aC.curFanIndex, Convert.ToByte(aC.trait_temp.value), 0 });
|
//ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 3, aC.on_off == "on" ? (byte)1 : (byte)0, aC.bus_Data.LoopID });
|
foreach (var dic in commandDictionary)
|
{
|
switch (dic.Key)
|
{
|
case "on_off":
|
ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 3, aC.trait_on_off.value.ToString() == "on" ? (byte)1 : (byte)0, aC.bus_Data.LoopID });
|
break;
|
case "mode":
|
ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 6, aC.curModeIndex, aC.bus_Data.LoopID });
|
break;
|
case "fan":
|
ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { 5, aC.curFanIndex, aC.bus_Data.LoopID });
|
break;
|
case "temp":
|
byte modeKey = 4;
|
switch (aC.curModeIndex)
|
{
|
case 3:
|
modeKey = 8;
|
break;
|
case 0:
|
modeKey = 4;
|
break;
|
case 1:
|
modeKey = 7;
|
break;
|
case 4:
|
modeKey = 19;
|
break;
|
case 2:
|
modeKey = 2;
|
break;
|
}
|
ControlBytesSend(Command.InstructionPanelKey, aC.bus_Data.SubnetID, aC.bus_Data.DeviceID, new byte[] { modeKey, Convert.ToByte(aC.trait_temp.value), aC.bus_Data.LoopID });
|
break;
|
default:
|
MainPage.Log($"功能未支持 : {dic.Key}");
|
break;
|
}
|
}
|
|
break;
|
case FunctionType.FloorHeating:
|
var fh = function as FloorHeating;
|
if (fh.modeTemp.Count == 4)
|
{
|
byte b1 = fh.trait_on_off.value.ToString() == "on" ? (byte)1 : (byte)0;
|
if (fh.workMode > 0)
|
{
|
b1 += (byte)(16 + fh.workMode);
|
}
|
if (commandDictionary.ContainsKey("temp"))
|
{
|
var dicTempString = "";
|
commandDictionary.TryGetValue("temp", out dicTempString);
|
var dicTemp = Convert.ToByte(dicTempString);
|
switch (fh.trait_mode.value)
|
{
|
case "day":
|
fh.modeTemp["day"] = dicTemp;
|
break;
|
case "night":
|
fh.modeTemp["night"] = dicTemp;
|
break;
|
case "away":
|
fh.modeTemp["away"] = dicTemp;
|
break;
|
case "normal":
|
fh.modeTemp["normal"] = dicTemp;
|
break;
|
}
|
}
|
ControlBytesSend(Command.SetFloorHeat, subnetId, deviceId, new byte[] { fh.bus_Data.LoopID, b1, (byte)fh.curTempType, fh.curModeIndex, fh.modeTemp["normal"], fh.modeTemp["day"], fh.modeTemp["night"], fh.modeTemp["away"], 0, 0 });
|
}
|
break;
|
}
|
break;
|
case FunctionCategory.Electrical:
|
switch (function.functionType)
|
{
|
case FunctionType.Fan:
|
var fan = function as Fan;
|
if (fan.trait_on_off.value.ToString() == "on")
|
{
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { fan.bus_Data.LoopID, (byte)fan.openLevel });
|
}
|
{
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { fan.bus_Data.LoopID, 0 });
|
}
|
break;
|
}
|
break;
|
case FunctionCategory.SwitchDevice:
|
switch (function.functionType)
|
{
|
case FunctionType.Socket:
|
var s = function as SwitchSocket;
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { s.bus_Data.LoopID, s.trait_on_off.value.ToString() == "on" ? (byte)100 : (byte)0 });
|
break;
|
}
|
break;
|
}
|
#region 发送命令立即更新UI
|
//HomePage.UpdataFunctionStates(function);
|
//RoomPage.UpdataStates(function);
|
//FunctionPage.UpdataStates(function);
|
//ClassificationPage.UpdataInfo(function);
|
#endregion
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"DataConversion_Bus Erorr : {ex.Message}");
|
}
|
}
|
|
/// <summary>
|
/// 读取bus设备数据
|
/// </summary>
|
public void ReadBusData(Function function)
|
{
|
try
|
{
|
var subnetId = function.bus_Data.SubnetID;
|
var deviceId = function.bus_Data.DeviceID;
|
var loopId = function.bus_Data.LoopID;
|
|
switch (function.functionCategory)
|
{
|
case FunctionCategory.Light:
|
switch (function.functionType)
|
{
|
case FunctionType.Relay:
|
case FunctionType.Dimmer:
|
ControlBytesSend(Command.ReadLightAllLoopBrightness, subnetId, deviceId, new byte[] { });
|
break;
|
case FunctionType.RGB:
|
ControlBytesSend(Command.ReadLogicLoopColor, subnetId, deviceId, new byte[] { function.bus_Data.LoopID });
|
break;
|
}
|
break;
|
case FunctionCategory.Curtain:
|
switch (function.functionType)
|
{
|
case FunctionType.Curtain:
|
case FunctionType.MotorCurtain:
|
case FunctionType.RollingShutter:
|
ControlBytesSend(Command.ReadCurtainStatus, subnetId, deviceId, new byte[] { function.bus_Data.LoopID });
|
break;
|
}
|
break;
|
case FunctionCategory.Thermostat:
|
switch (function.functionType)
|
{
|
case FunctionType.AC:
|
ControlBytesSend(Command.ReadACMode, subnetId, deviceId, new byte[] { function.bus_Data.LoopID });
|
break;
|
case FunctionType.FloorHeating:
|
ControlBytesSend(Command.ReadFloorHeat, subnetId, deviceId, new byte[] { function.bus_Data.LoopID });
|
break;
|
}
|
break;
|
case FunctionCategory.Electrical:
|
switch (function.functionType)
|
{
|
case FunctionType.Fan:
|
var fan = function as Fan;
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { fan.bus_Data.LoopID });
|
break;
|
}
|
break;
|
case FunctionCategory.SwitchDevice:
|
switch (function.functionType)
|
{
|
case FunctionType.Socket:
|
var s = function as SwitchSocket;
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { s.bus_Data.LoopID });
|
break;
|
}
|
break;
|
case FunctionCategory.Sensor:
|
var sensor = function as Sensor;
|
ControlBytesSend(Command.ReadDeviceLoopInfo, subnetId, deviceId, new byte[] { (byte)5,
|
(byte)((int)sensor.functionType % 256), sensor.bus_Data.LoopID });
|
break;
|
}
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"Read DataConversion_Bus Erorr : {ex.Message}");
|
}
|
}
|
|
/// <summary>
|
/// 添加到内存数组里面
|
/// </summary>
|
void add()
|
{
|
/// <summary>
|
/// 达到50条数据后就清理一下
|
/// </summary>
|
if (50 < controlList.Count)
|
{
|
lock (controlList)
|
{
|
for (int i = 0; i < controlList.Count;)
|
{
|
if (controlList[i] == null || 3 <= controlList[i].Packet.HaveSendCount)
|
{
|
controlList.RemoveAt(i);
|
}
|
else
|
{
|
i++;
|
}
|
}
|
|
}
|
}
|
controlList.Add(this);
|
}
|
|
//当前数据的关键数据
|
string sendFlag = string.Empty;
|
protected string SendFlag
|
{
|
get
|
{
|
return sendFlag;
|
}
|
set
|
{
|
sendFlag = value;
|
usefulBytes = null;
|
}
|
}
|
|
private byte[] usefulBytes;
|
/// <summary>
|
/// 获取回来的有用信息,如果获取回来的数据为null,就会抛出异常信息
|
/// </summary>
|
public byte[] UsefulBytes
|
{
|
get
|
{
|
if (null == usefulBytes)
|
{
|
// throw new Exception("不好意思,网络不稳定或者远程设备不在线,请稍候再试!");
|
}
|
|
return this.usefulBytes;
|
}
|
set
|
{
|
usefulBytes = value;
|
}
|
}
|
|
//发送数据了之后当前线程等待或者运行的信号
|
System.Threading.ManualResetEvent allDone = new System.Threading.ManualResetEvent(false);
|
|
/// <summary>
|
/// 发送了数据后,线程就是等待状态,直到接收到反馈或者超时后退出
|
/// </summary>
|
void wait()
|
{
|
allDone.Reset();
|
allDone.WaitOne();
|
}
|
|
/// <summary>
|
/// 让当前线程继续执行
|
/// </summary>
|
void run()
|
{
|
allDone.Set();
|
Packet.HaveSendCount = 4;
|
}
|
|
//数据发送处理
|
void managerSendCount(object o)
|
{
|
add();
|
//Bus socket无法控制,重启机制
|
if (controlLostCount > 10)
|
{
|
UdpSocket._BusSocket.Stop();
|
new System.Threading.Thread(() =>
|
{
|
System.Threading.Thread.Sleep(1000);
|
UdpSocket._BusSocket.Start();
|
controlLostCount = 0;
|
})
|
{ IsBackground = true }.Start();
|
}
|
|
try
|
{
|
MainPage.Log("发送数据:" + SendFlag);
|
UdpSocket._BusSocket.AsyncBeginSend(Packet);
|
Packet.HaveSendCount--;
|
|
//这里是重发两次
|
while (Packet.HaveSendCount < 3)
|
{
|
if (Packet.FlagDateTime.AddMilliseconds(1000).Ticks <= DateTime.Now.Ticks)
|
{
|
MainPage.Log("重发数据:" + SendFlag);
|
UdpSocket._BusSocket.AsyncBeginSend(Packet);
|
controlLostCount++;
|
}
|
System.Threading.Thread.Sleep(100);
|
}
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log("managerSendCount:" + ex.ToString());
|
}
|
finally
|
{
|
allDone.Set();
|
}
|
}
|
|
/// <summary>
|
/// 当前数据包
|
/// </summary>
|
Packet Packet;
|
/// <summary>
|
/// 记录发送数据包
|
/// </summary>
|
/// <param name="target"></param>
|
private void signPacket(Target target)
|
{
|
this.SendFlag = string.Format("{0},{1},{2},", target.SubnetID, target.DeviceID, (int)target.Command + 1);
|
|
switch (target.Command)
|
{
|
case Command.SetSingleLight:
|
case Command.ReadLogicLoopColor:
|
case Command.ReadACMode:
|
case Command.SetACMode:
|
case Command.ReadFloorHeat:
|
case Command.SetFloorHeat:
|
this.sendFlag += string.Format("{0}", target.AddData[0]);
|
break;
|
case Command.SetLogicLoopColor:
|
this.sendFlag += string.Format("{0},{1},{2}", target.AddData[0], target.AddData[1], target.AddData[2]);
|
break;
|
case Command.ReadLightAllLoopBrightness:
|
this.SendFlag += "";
|
break;
|
case Command.ReadGateway:
|
this.SendFlag = string.Format("{0},{1}", target.AddData[0], target.AddData[1]);
|
break;
|
case Command.ReadDeviceLoopInfo:
|
if (target.AddData[0] == 1)//特殊处理灯光类,DMX模块每一个回路不固定小类,根据具体设置来回复
|
this.sendFlag += string.Format("{0},{1}", target.AddData[0], target.AddData[2]);
|
else
|
this.sendFlag += string.Format("{0},{1},{2}", target.AddData[0], target.AddData[1], target.AddData[2]);
|
break;
|
case Command.InstructionPanelKey:
|
case Command.ReadInstructionPanelKey:
|
this.sendFlag += string.Format("{0},{1}", target.AddData[0], target.AddData[1]);
|
break;
|
default:
|
break;
|
}
|
|
}
|
|
/// <summary>
|
/// 发送数据
|
/// </summary>
|
/// <param name="target">发送对象</param>
|
/// <param name="sendCount">重发次数</param>
|
private void Send(Target target, int sendCount, bool isWait)
|
{
|
try
|
{
|
Packet = new Packet(target.SendBytes, target.IPEndPoint);
|
Packet.HaveSendCount = 3 - sendCount;
|
|
signPacket(target);
|
|
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
|
thread.IsBackground = true;
|
thread.Start(Packet);
|
|
if (isWait)
|
{
|
this.wait();
|
}
|
}
|
catch(Exception ex)
|
{
|
MainPage.Log($"Send bus data error {ex.Message}");
|
}
|
}
|
|
/// <summary>
|
/// 读取网关IP
|
/// </summary>
|
public void SearchLocalGateway()
|
{
|
try
|
{
|
var sendJob = new JObject { { "id", msg_id.ToString() }, { "time_stamp", (DateTime.Now.Ticks / 10000).ToString() } };
|
var bodyString = JsonConvert.SerializeObject(sendJob);
|
var bodyBytes = Encoding.ASCII.GetBytes(bodyString);
|
var sendBytes = A_Protocal_Common.apc.ConvertSendBodyData(CommunicationTopic.SearchLoaclGateway, bodyString);
|
//组播发送
|
Packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("239.0.168.188"), 8585));
|
Packet.HaveSendCount = 4;//ps:没有重发,后期有时间可以增加重发判断
|
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
|
thread.IsBackground = true;
|
thread.Start(Packet);
|
|
wait();
|
}
|
catch (Exception ex)
|
{
|
MainPage.Log($"Send bus data error {ex.Message}");
|
}
|
}
|
|
|
}
|
}
|