|
|
|
@ -30,7 +30,6 @@ namespace FBeeService
|
|
|
|
|
private readonly IServiceProvider _applicationServices;
|
|
|
|
|
private CancellationTokenSource _tokenSource;
|
|
|
|
|
private static readonly object lockObject = new Object();
|
|
|
|
|
private readonly TcpClient _tcpClient;
|
|
|
|
|
private string _tcpIp;
|
|
|
|
|
private int _tcpPort;
|
|
|
|
|
private byte[] _address;
|
|
|
|
@ -45,7 +44,6 @@ namespace FBeeService
|
|
|
|
|
this._configuration = configuration;
|
|
|
|
|
this._httpClientFactory = httpClientFactory;
|
|
|
|
|
this._tokenSource = new CancellationTokenSource();
|
|
|
|
|
this._tcpClient = new TcpClient();
|
|
|
|
|
this._tcpIp = this._configuration["tcp.ip"];
|
|
|
|
|
this._tcpPort = this._configuration.GetValue<int>("tcp.port");
|
|
|
|
|
//this.Connect();
|
|
|
|
@ -117,64 +115,12 @@ namespace FBeeService
|
|
|
|
|
list.Add(0x01);
|
|
|
|
|
list.AddRange(new byte[] { 0x00, 0x00 });
|
|
|
|
|
list.Add(status);
|
|
|
|
|
this.Clients[sn].Client.GetStream().Write(list.ToArray());
|
|
|
|
|
var client = this.Clients[sn];
|
|
|
|
|
this.Write(client, list.ToArray());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Switch2AllOn(string id)
|
|
|
|
|
{
|
|
|
|
|
this.Command16(new byte[] { this.GetNumber(id), 0x0f, 0x00, 0x00, 0x00, 0x03, 0x01, 0x07 });
|
|
|
|
|
this.SearchSwitchStatus(this.GetNumber(id), 0x02);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Switch2AllOff(string id)
|
|
|
|
|
{
|
|
|
|
|
this.Command16(new byte[] { this.GetNumber(id), 0x0f, 0x00, 0x00, 0x00, 0x03, 0x01, 0x00 });
|
|
|
|
|
this.SearchSwitchStatus(this.GetNumber(id), 0x02);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Switch2L1On(string id)
|
|
|
|
|
{
|
|
|
|
|
this.Command16(new byte[] { this.GetNumber(id), 0x05, 0x00, 0x00, 0xff, 0x00 });
|
|
|
|
|
this.SearchSwitchStatus(this.GetNumber(id), 0x02);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Switch2L1Off(string id)
|
|
|
|
|
{
|
|
|
|
|
this.Command16(new byte[] { this.GetNumber(id), 0x05, 0x00, 0x00, 0x00, 0x00 });
|
|
|
|
|
this.SearchSwitchStatus(this.GetNumber(id), 0x02);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Switch2L2On(string id)
|
|
|
|
|
{
|
|
|
|
|
this.Command16(new byte[] { this.GetNumber(id), 0x05, 0x00, 0x01, 0xff, 0x00 });
|
|
|
|
|
this.SearchSwitchStatus(this.GetNumber(id), 0x02);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Switch2L2Off(string id)
|
|
|
|
|
{
|
|
|
|
|
this.Command16(new byte[] { this.GetNumber(id), 0x05, 0x00, 0x01, 0x00, 0x00 });
|
|
|
|
|
this.SearchSwitchStatus(this.GetNumber(id), 0x02);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SocketOn(string id)
|
|
|
|
|
{
|
|
|
|
|
this.Command16(new byte[] { this.GetNumber(id), 0x05, 0x00, 0x00, 0xff, 0x00 });
|
|
|
|
|
this.SearchSwitchStatus(this.GetNumber(id), 0x10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SocketOff(string id)
|
|
|
|
|
{
|
|
|
|
|
this.Command16(new byte[] { this.GetNumber(id), 0x05, 0x00, 0x00, 0x00, 0x00 });
|
|
|
|
|
this.SearchSwitchStatus(this.GetNumber(id), 0x10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private byte GetNumber(string id)
|
|
|
|
|
{
|
|
|
|
|
return Convert.ToByte(id.Split("-")[1]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckConnection()
|
|
|
|
|
{
|
|
|
|
|
var ips = NetworkInterface.GetAllNetworkInterfaces()
|
|
|
|
@ -254,25 +200,21 @@ namespace FBeeService
|
|
|
|
|
Clients.TryGetValue(gateway.Sn, out TcpClientWrapper client);
|
|
|
|
|
if (client.Ip != gateway.Ip)
|
|
|
|
|
{
|
|
|
|
|
if (client.Client.Connected)
|
|
|
|
|
{
|
|
|
|
|
client.Client.Close();
|
|
|
|
|
}
|
|
|
|
|
this.Connect(gateway.Sn, client);
|
|
|
|
|
this.Connect(client);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!client.Client.Connected)
|
|
|
|
|
{
|
|
|
|
|
this.Connect(gateway.Sn, client);
|
|
|
|
|
this.Connect(client);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var client = new TcpClientWrapper { Ip = gateway.Ip, Client = new TcpClient() };
|
|
|
|
|
var client = new TcpClientWrapper { Sn = gateway.Sn, Ip = gateway.Ip, Client = new TcpClient() };
|
|
|
|
|
Clients.TryAdd(gateway.Sn, client);
|
|
|
|
|
this.Connect(gateway.Sn, client);
|
|
|
|
|
this.Connect(client);
|
|
|
|
|
}
|
|
|
|
|
this.Refresh(gateway.Sn);
|
|
|
|
|
}
|
|
|
|
@ -291,17 +233,23 @@ namespace FBeeService
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Connect(string sn, TcpClientWrapper client)
|
|
|
|
|
private void Connect(TcpClientWrapper client)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (client != null)
|
|
|
|
|
{
|
|
|
|
|
client.Client.Dispose();
|
|
|
|
|
client.Client = new TcpClient();
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine($"connect {client.Ip} for {client.Sn}");
|
|
|
|
|
client.Client.Connect(client.Ip, 8001);
|
|
|
|
|
new Thread(() =>
|
|
|
|
|
{
|
|
|
|
|
var stream = client.Client.GetStream();
|
|
|
|
|
while (client.Client.Connected)
|
|
|
|
|
{
|
|
|
|
|
if (stream.CanRead)
|
|
|
|
|
if (stream.DataAvailable)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
@ -309,13 +257,11 @@ namespace FBeeService
|
|
|
|
|
var length = stream.Read(buffer);
|
|
|
|
|
var data = buffer.ToList().Take(length).ToArray();
|
|
|
|
|
Console.WriteLine($"response:{BitConverter.ToString(data)}");
|
|
|
|
|
var test = new byte[] {
|
|
|
|
|
0x16,0x00, 0x34, 0xF5, 0x41, 0x11, 0xFE, 0x82, 0x0D, 0x02, 0x42 ,0x20 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x00 ,0x01 ,0x00 ,0x00 ,0x00 };
|
|
|
|
|
this.Handle(sn, data);
|
|
|
|
|
this.Handle(client.Sn, data);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ex.PrintStack();
|
|
|
|
|
ex.PrintStack("read stream error");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -335,14 +281,7 @@ namespace FBeeService
|
|
|
|
|
list.AddRange(sn.HexToBytes().Reverse());
|
|
|
|
|
list.Add(0xfe);
|
|
|
|
|
list.Add(0x81);
|
|
|
|
|
this.Write(client.Client, list.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SearchAddress()
|
|
|
|
|
{
|
|
|
|
|
var list = new List<byte>() { 0x0c };
|
|
|
|
|
var command = Command(list.ToArray());
|
|
|
|
|
this.Write(command);
|
|
|
|
|
this.Write(client, list.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Handle(string sn, byte[] data)
|
|
|
|
@ -459,51 +398,6 @@ namespace FBeeService
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SearchNodeStatus()
|
|
|
|
|
{
|
|
|
|
|
var list = new List<byte>() { 0x15 };
|
|
|
|
|
list.AddRange(this._address);
|
|
|
|
|
list.Add(0x00);
|
|
|
|
|
var command = Command(list.ToArray());
|
|
|
|
|
this.Write(command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SearchNodeType(int i)
|
|
|
|
|
{
|
|
|
|
|
var list = new List<byte>() { 0x16 };
|
|
|
|
|
list.AddRange(this._address);
|
|
|
|
|
list.Add(0x00);
|
|
|
|
|
var modbusdata = new List<byte> {
|
|
|
|
|
(byte)i,0x03,0x00,0x0f,0x00,0x01
|
|
|
|
|
}.ToArray();
|
|
|
|
|
var modbus = new List<byte>();
|
|
|
|
|
modbus.AddRange(modbusdata);
|
|
|
|
|
var crc = Crc16.ComputeChecksum(modbusdata);
|
|
|
|
|
modbus.Add(crc[1]);
|
|
|
|
|
modbus.Add(crc[0]);
|
|
|
|
|
list.AddRange(modbus);
|
|
|
|
|
var command = Data(list.ToArray());
|
|
|
|
|
this.Write(command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SearchSwitchStatus(int i, byte cmd)
|
|
|
|
|
{
|
|
|
|
|
var list = new List<byte>() { 0x16 };
|
|
|
|
|
list.AddRange(this._address);
|
|
|
|
|
list.Add(0x00);
|
|
|
|
|
var modbusdata = new List<byte> {
|
|
|
|
|
(byte)i, 0x01,0x00,0x00,0x00,cmd
|
|
|
|
|
}.ToArray();
|
|
|
|
|
var modbus = new List<byte>();
|
|
|
|
|
modbus.AddRange(modbusdata);
|
|
|
|
|
var crc = Crc16.ComputeChecksum(modbusdata);
|
|
|
|
|
modbus.Add(crc[1]);
|
|
|
|
|
modbus.Add(crc[0]);
|
|
|
|
|
list.AddRange(modbus);
|
|
|
|
|
var command = Data(list.ToArray());
|
|
|
|
|
this.Write(command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private byte[] Command(byte[] data)
|
|
|
|
|
{
|
|
|
|
|
var command = new List<byte> { 0xfe, 0xa5, 0x00 };
|
|
|
|
@ -531,40 +425,14 @@ namespace FBeeService
|
|
|
|
|
return command.ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Command16(byte[] command)
|
|
|
|
|
{
|
|
|
|
|
var list = new List<byte>() { 0x16 };
|
|
|
|
|
list.AddRange(this._address);
|
|
|
|
|
list.Add(0x00);
|
|
|
|
|
var modbusdata = command;
|
|
|
|
|
var modbus = new List<byte>();
|
|
|
|
|
modbus.AddRange(modbusdata);
|
|
|
|
|
var crc = Crc16.ComputeChecksum(modbusdata);
|
|
|
|
|
modbus.Add(crc[1]);
|
|
|
|
|
modbus.Add(crc[0]);
|
|
|
|
|
list.AddRange(modbus);
|
|
|
|
|
var data = Data(list.ToArray());
|
|
|
|
|
this.Write(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Write(byte[] command)
|
|
|
|
|
private void Write(TcpClientWrapper clientWrapper, byte[] command)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(500);
|
|
|
|
|
Console.WriteLine($"write:{BitConverter.ToString(command).Replace("-", " ")}");
|
|
|
|
|
this._tcpClient.GetStream().Write(command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Write(TcpClient client, byte[] command)
|
|
|
|
|
{
|
|
|
|
|
if (client.Connected)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"write:{BitConverter.ToString(command).Replace("-", " ")}");
|
|
|
|
|
client.GetStream().Write(command);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
if (!clientWrapper.Client.Connected)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine($"can not write:{BitConverter.ToString(command).Replace("-", " ")}");
|
|
|
|
|
this.Connect(clientWrapper);
|
|
|
|
|
}
|
|
|
|
|
Console.WriteLine($"write:{BitConverter.ToString(command).Replace("-", " ")}");
|
|
|
|
|
clientWrapper.Client.GetStream().Write(command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private NotifyModel CreateModel(string name, string number, string path, string icon)
|
|
|
|
@ -604,12 +472,15 @@ namespace FBeeService
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("LiChuangService dispose...");
|
|
|
|
|
if (this._tcpClient.Connected)
|
|
|
|
|
Console.WriteLine("dispose...");
|
|
|
|
|
this._tokenSource.Cancel();
|
|
|
|
|
foreach (var item in Clients)
|
|
|
|
|
{
|
|
|
|
|
this._tcpClient.Close();
|
|
|
|
|
if (item.Value.Client != null)
|
|
|
|
|
{
|
|
|
|
|
item.Value.Client.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this._tokenSource.Cancel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|