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>
|
/// 通讯端口
|
/// </summary>
|
public int port = 0;
|
|
/// <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 static void ReceiveRepeatManager(string receiveFlag,byte []usefulBytes)
|
{
|
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场景
|
/// todo
|
/// </summary>
|
public void ControlBusScenes(Scene scene)
|
{
|
if (Control.Ins.IsRemote)
|
{
|
foreach (var f in scene.functions)
|
{
|
var count = 0;
|
List<ApiAlinkControlActionObj> actionObjs = new List<ApiAlinkControlActionObj>();
|
Dictionary<string, string> d = new Dictionary<string, string>();
|
var pm = new DAL.Server.HttpServerRequest();
|
foreach (var temp in f.status)
|
{
|
if (f.localFunction == null)
|
{
|
continue;
|
}
|
d.Add(temp.key, temp.value);
|
var apiControlData = f.localFunction.GetApiControlData(d);
|
actionObjs.Add(apiControlData);
|
count++;
|
if (count > 9)
|
{
|
var result = pm.ControlDevice(actionObjs);
|
actionObjs = new List<ApiAlinkControlActionObj>();
|
count = 0;
|
}
|
}
|
var pack = pm.ControlDevice(actionObjs);
|
}
|
}
|
else
|
{
|
foreach (var f in scene.functions)
|
{
|
if (f.localFunction == null)
|
{
|
continue;
|
}
|
switch (f.localFunction.spk)
|
{
|
case SPK.CurtainSwitch:
|
foreach (var attr in f.status)
|
{
|
if (attr.key == FunctionAttributeKey.OnOff)
|
{
|
byte curtainState = 0;
|
switch (attr.value)
|
{
|
case "on":
|
curtainState = 1;
|
break;
|
case "off":
|
curtainState = 2;
|
break;
|
}
|
ControlBytesSend(Command.SetCurtainModelStutas, f.localFunction.bus.SubnetID, f.localFunction.bus.DeviceID,
|
new byte[] { f.localFunction.bus.LoopId, curtainState });
|
break;
|
}
|
}
|
break;
|
case SPK.AcStandard:
|
AC tempAc = new AC();
|
foreach (var attr in f.status)
|
{
|
byte modeKey = 4;
|
switch (attr.key)
|
{
|
case FunctionAttributeKey.OnOff:
|
tempAc.trait_on_off.curValue = attr.value;
|
break;
|
case FunctionAttributeKey.Mode:
|
tempAc.trait_mode.curValue = attr.value;
|
break;
|
case FunctionAttributeKey.FanSpeed:
|
tempAc.trait_fan.curValue = attr.value;
|
break;
|
case FunctionAttributeKey.SetTemp:
|
tempAc.trait_temp.curValue = attr.value;
|
switch (tempAc.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;
|
}
|
break;
|
}
|
ControlBytesSend(Command.InstructionPanelKey, f.localFunction.bus.SubnetID, f.localFunction.bus.DeviceID, new byte[] { 3, tempAc.trait_on_off.curValue.ToString() == "on" ? (byte)1 : (byte)0, f.localFunction.bus.LoopId });
|
ControlBytesSend(Command.InstructionPanelKey, f.localFunction.bus.SubnetID, f.localFunction.bus.DeviceID, new byte[] { 6, tempAc.curModeIndex, f.localFunction.bus.LoopId });
|
ControlBytesSend(Command.InstructionPanelKey, f.localFunction.bus.SubnetID, f.localFunction.bus.DeviceID, new byte[] { 5, tempAc.curFanIndex, f.localFunction.bus.LoopId });
|
ControlBytesSend(Command.InstructionPanelKey, f.localFunction.bus.SubnetID, f.localFunction.bus.DeviceID, new byte[] { modeKey, Convert.ToByte(tempAc.trait_temp.curValue), f.localFunction.bus.LoopId });
|
}
|
break;
|
case SPK.LightSwitch:
|
case SPK.LightDimming:
|
byte brightness = 0;
|
foreach (var attr in f.status)
|
{
|
switch (attr.key)
|
{
|
case FunctionAttributeKey.OnOff:
|
switch (attr.value)
|
{
|
case "on":
|
brightness = 100;
|
break;
|
case "off":
|
brightness = 0;
|
break;
|
}
|
break;
|
case FunctionAttributeKey.Brightness:
|
brightness = Convert.ToByte(attr.value);
|
break;
|
}
|
}
|
ControlBytesSend(Command.SetSingleLight, f.localFunction.bus.SubnetID, f.localFunction.bus.DeviceID,
|
new byte[] { f.localFunction.bus.LoopId, brightness, (byte)(f.localFunction.delay / 255), (byte)(f.localFunction.delay % 255) });
|
break;
|
case SPK.LightRGB:
|
break;
|
case SPK.FloorHeatStandard:
|
var tempFh = new FloorHeating();
|
foreach (var attr in f.status)
|
{
|
switch (attr.key)
|
{
|
case FunctionAttributeKey.OnOff:
|
tempFh.trait_on_off.curValue = attr.value;
|
break;
|
case FunctionAttributeKey.SetTemp:
|
tempFh.trait_temp.curValue = attr.value;
|
break;
|
case FunctionAttributeKey.Mode:
|
tempFh.trait_mode.curValue = attr.value;
|
break;
|
}
|
}
|
byte b1 = tempFh.trait_on_off.curValue.ToString() == "on" ? (byte)1 : (byte)0;
|
ControlBytesSend(Command.SetFloorHeat, f.localFunction.bus.SubnetID, f.localFunction.bus.DeviceID, new byte[] {
|
f. localFunction.bus.LoopId, b1, 0, tempFh.curModeIndex, Convert.ToByte( tempFh.trait_temp.curValue), Convert.ToByte( tempFh.trait_temp.curValue), Convert.ToByte( tempFh.trait_temp.curValue), Convert.ToByte( tempFh.trait_temp.curValue), 0, 0 });
|
break;
|
case SPK.ElectricSocket:
|
foreach (var attr in f.status)
|
{
|
if (attr.key == FunctionAttributeKey.OnOff)
|
{
|
byte onOffValue = 0;
|
switch (attr.value)
|
{
|
case "on":
|
onOffValue = 1;
|
break;
|
case "off":
|
onOffValue = 2;
|
break;
|
}
|
ControlBytesSend(Command.SetSingleLight, f.localFunction.bus.SubnetID, f.localFunction.bus.DeviceID,
|
new byte[] { f.localFunction.bus.LoopId, onOffValue });
|
break;
|
}
|
}
|
break;
|
}
|
}
|
}
|
}
|
|
/// <summary>
|
/// 控制bus设备
|
/// </summary>
|
/// <param name="commandString"></param>
|
/// <returns></returns>
|
public void WriteBusData(Function function, Dictionary<string, string> commandDictionary)
|
{
|
try
|
{
|
var subnetId = function.bus.SubnetID;
|
var deviceId = function.bus.DeviceID;
|
var loopId = function.bus.LoopId;
|
|
switch (function.Spk_Prefix)
|
{
|
case FunctionCategory.Light:
|
var light = function as Light;
|
switch (light.spk)
|
{
|
case SPK.LightCCT:
|
byte b0 = 100;//开关操作依据on_off字段,实际开关值依据brightness,当on_off为打开,brightness不能为0
|
if (light.trait_on_off.curValue.ToString() == "off")
|
{
|
b0 = 0;
|
}
|
else
|
{
|
b0 = (byte)light.brightness == 0 ? (byte)100 : (byte)light.brightness;
|
}
|
var bytes0 = new byte[] { function.bus.LoopId,
|
b0,
|
254, 0,Convert.ToByte(light.fadeTime) ,2,
|
(byte)((int)light.Attr_CCT.curValue/256),(byte)((int)light.Attr_CCT.curValue%256),
|
(byte)light.BlueColor,
|
0,0};
|
ControlBytesSend(Command.SetLogicLoopColor, subnetId, deviceId, bytes0, 1);
|
break;
|
case SPK.LightRGB:
|
byte b = 100;//开关操作依据on_off字段,实际开关值依据brightness,当on_off为打开,brightness不能为0
|
if (light.trait_on_off.curValue.ToString() == "off")
|
{
|
b = 0;
|
}
|
else
|
{
|
b = (byte)light.brightness == 0 ? (byte)100 : (byte)light.brightness;
|
}
|
var bytes = new byte[] { function.bus.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 SPK.LightDimming:
|
byte b1 = 100;
|
if (light.trait_on_off.curValue.ToString() == "off")
|
{
|
b1 = 0;
|
}
|
else
|
{
|
b1 = (byte)light.brightness == 0 ? (byte)100 : (byte)light.brightness;
|
}
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] {
|
function.bus.LoopId,
|
b1,
|
0, Convert.ToByte(light.fadeTime) });
|
//DimmerPage.UpdataStates(light);
|
break;
|
case SPK.LightSwitch:
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { function.bus.LoopId, light.trait_on_off.curValue.ToString() == "on" ? (byte)100 : (byte)0, 0, 0 });
|
//RelayPage.UpdataState(light);
|
break;
|
}
|
break;
|
case FunctionCategory.Curtain:
|
var curtain = function as Curtain;
|
switch (curtain.spk)
|
{
|
case SPK.CurtainSwitch:
|
byte b1 = 0;
|
if (curtain.trait_on_off.curValue.ToString() == "stop")
|
{
|
b1 = 0;
|
}
|
else if (curtain.trait_on_off.curValue.ToString() == "on")
|
{
|
b1 = 1;
|
}
|
else
|
{
|
b1 = 2;
|
}
|
ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { curtain.bus.LoopId, b1 });
|
break;
|
case SPK.CurtainTrietex:
|
case SPK.CurtainRoller:
|
//if (commandDictionary.Count > 0)
|
//{
|
if (curtain.trait_on_off.curValue.ToString() == "stop")
|
{
|
ControlBytesSend(Command.SetCurtainModelStutas, subnetId, deviceId, new byte[] { curtain.bus.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.AC:
|
switch (function.spk)
|
{
|
case SPK.AcStandard:
|
var aC = function as AC;
|
ControlBytesSend(Command.SetACMode, subnetId, deviceId, new byte[] { aC.bus.LoopId, (byte)aC.curTempType, 32, 32, 32, 32, 32, 0, aC.trait_on_off.curValue.ToString() == "on" ? (byte)1 : (byte)0, aC.curModeIndex, aC.curFanIndex, Convert.ToByte(aC.trait_temp.curValue), 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.SubnetID, aC.bus.DeviceID, new byte[] { 3, aC.trait_on_off.curValue.ToString() == "on" ? (byte)1 : (byte)0, aC.bus.LoopId });
|
break;
|
case "mode":
|
ControlBytesSend(Command.InstructionPanelKey, aC.bus.SubnetID, aC.bus.DeviceID, new byte[] { 6, aC.curModeIndex, aC.bus.LoopId });
|
break;
|
case "fan":
|
ControlBytesSend(Command.InstructionPanelKey, aC.bus.SubnetID, aC.bus.DeviceID, new byte[] { 5, aC.curFanIndex, aC.bus.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.SubnetID, aC.bus.DeviceID, new byte[] { modeKey, Convert.ToByte(aC.trait_temp.curValue), aC.bus.LoopId });
|
break;
|
default:
|
MainPage.Log($"功能未支持 : {dic.Key}");
|
break;
|
}
|
}
|
|
break;
|
}
|
break;
|
case FunctionCategory.FloorHeat:
|
switch (function.spk) {
|
case SPK.FloorHeatStandard:
|
var fh = function as FloorHeating;
|
if (fh.modeTemp.Count == 4)
|
{
|
byte b1 = fh.trait_on_off.curValue.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.curValue)
|
{
|
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.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.Electric:
|
switch (function.spk)
|
{
|
case SPK.ElectricFan:
|
var fan = function as Fan;
|
if (fan.trait_on_off.curValue.ToString() == "on")
|
{
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { fan.bus.LoopId, (byte)fan.openLevel });
|
}
|
{
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { fan.bus.LoopId, 0 });
|
}
|
break;
|
case SPK.ElectricSocket:
|
var s = function as SwitchSocket;
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { s.bus.LoopId, s.trait_on_off.curValue.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.SubnetID;
|
var deviceId = function.bus.DeviceID;
|
var loopId = function.bus.LoopId;
|
|
switch (function.Spk_Prefix)
|
{
|
case FunctionCategory.Light:
|
switch (function.spk)
|
{
|
case SPK.LightSwitch:
|
case SPK.LightDimming:
|
ControlBytesSend(Command.ReadLightAllLoopBrightness, subnetId, deviceId, new byte[] { });
|
break;
|
case SPK.LightRGB:
|
ControlBytesSend(Command.ReadLogicLoopColor, subnetId, deviceId, new byte[] { function.bus.LoopId});
|
break;
|
case SPK.LightCCT:
|
ControlBytesSend(Command.ReadLogicLoopColor, subnetId, deviceId, new byte[] { function.bus.LoopId });
|
break;
|
}
|
break;
|
case FunctionCategory.Curtain:
|
switch (function.spk)
|
{
|
case SPK.CurtainSwitch:
|
case SPK.CurtainTrietex:
|
case SPK.CurtainRoller:
|
ControlBytesSend(Command.ReadCurtainStatus, subnetId, deviceId, new byte[] { function.bus.LoopId });
|
break;
|
}
|
break;
|
case FunctionCategory.AC:
|
switch (function.spk)
|
{
|
case SPK.AcStandard:
|
ControlBytesSend(Command.ReadACMode, subnetId, deviceId, new byte[] { function.bus.LoopId });
|
break;
|
}
|
break;
|
case FunctionCategory.FloorHeat:
|
switch (function.spk)
|
{
|
case SPK.FloorHeatStandard:
|
ControlBytesSend(Command.ReadFloorHeat, subnetId, deviceId, new byte[] { function.bus.LoopId });
|
break;
|
}
|
break;
|
case FunctionCategory.Electric:
|
switch (function.spk)
|
{
|
case SPK.ElectricFan:
|
var fan = function as Fan;
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { fan.bus.LoopId });
|
break;
|
case SPK.ElectricSocket:
|
var s = function as SwitchSocket;
|
ControlBytesSend(Command.SetSingleLight, subnetId, deviceId, new byte[] { s.bus.LoopId });
|
break;
|
}
|
break;
|
case FunctionCategory.Sensor:
|
var sensor = function as Sensor;
|
byte sensorType = 0;
|
switch (function.spk)
|
{
|
case SPK.SensorTemperature:
|
sensorType = 2;
|
break;
|
case SPK.SensorHumidity:
|
sensorType = 3;
|
break;
|
case SPK.SensorTVOC:
|
sensorType = 5;
|
break;
|
case SPK.SensorPm25:
|
sensorType = 6;
|
break;
|
case SPK.SensorCO2:
|
sensorType = 7;
|
break;
|
}
|
ControlBytesSend(Command.ReadDeviceLoopInfo, subnetId, deviceId, new byte[] { 5, sensorType, sensor.bus.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(UdpSocket._BusSocket.Port);
|
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(300);
|
}
|
}
|
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;
|
}
|
|
}
|
|
//private void signAlinkPacket()
|
//{
|
//}
|
|
/// <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", Control.Ins.msg_id.ToString() }, { "time_stamp", (DateTime.Now.Ticks / 100000).ToString() } };
|
var bodyString = JsonConvert.SerializeObject(sendJob);
|
|
var sendBytes = Control.Ins.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}");
|
}
|
}
|
|
|
/// <summary>
|
/// 发送udp A协议数据
|
/// </summary>
|
public void SendLocalHdlLinkData(byte[] sendBytes,string id, bool isWait=false)
|
{
|
packet = new Packet(sendBytes, new System.Net.IPEndPoint(System.Net.IPAddress.Parse(Control.Ins.reportIp), 8585));
|
sendFlag = id;
|
System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(managerSendCount));
|
thread.IsBackground = true;
|
thread.Start(packet);
|
if (isWait)
|
{
|
wait();
|
}
|
MainPage.Log($"发送Hdl-Link数据,IP:{Control.Ins.reportIp}:8585");
|
}
|
}
|
|
}
|