2016-03-16 update 1 FboxConnector bug fix
This commit is contained in:
@@ -27,26 +27,24 @@ namespace Modbus.Net.FBox
|
||||
private OAuth2Client _oauth2;
|
||||
private string _refreshToken;
|
||||
|
||||
private HttpClient _httpClient;
|
||||
private HttpClient _httpClient2;
|
||||
private HubConnection _hubConnection;
|
||||
private SignalRSigninMsg _msg;
|
||||
protected SignalRSigninMsg Msg => _msg;
|
||||
private DMonGroup _dataGroup;
|
||||
private int _state;
|
||||
private string _groupUid;
|
||||
private string _boxUid;
|
||||
private string _boxNo;
|
||||
private int _boxSessionId;
|
||||
private int _connectionState;
|
||||
private Dictionary<string, double> _data;
|
||||
private Dictionary<string, Type> _dataType;
|
||||
private HttpClient _httpClient { get; set; }
|
||||
private HttpClient _httpClient2 { get; set; }
|
||||
private HubConnection _hubConnection { get; set; }
|
||||
protected SignalRSigninMsg Msg { get; }
|
||||
private DMonGroup _dataGroup { get; set; }
|
||||
private string _groupUid { get; set; }
|
||||
private string _boxUid { get; set; }
|
||||
private string _boxNo { get; set; }
|
||||
private int _boxSessionId { get; set; }
|
||||
private int _connectionState { get; set; }
|
||||
private Dictionary<string, double> _data { get; set; }
|
||||
private Dictionary<string, Type> _dataType { get; set; }
|
||||
|
||||
private DateTime _timeStamp = DateTime.MinValue;
|
||||
private DateTime _timeStamp { get; set; } = DateTime.MinValue;
|
||||
|
||||
public override string ConnectionToken { get; }
|
||||
|
||||
private AsyncLock _lock = new AsyncLock();
|
||||
//private AsyncLock _lock = new AsyncLock();
|
||||
|
||||
private Timer _timer;
|
||||
|
||||
@@ -57,20 +55,6 @@ namespace Modbus.Net.FBox
|
||||
private bool _connected;
|
||||
public override bool IsConnected => _connected;
|
||||
|
||||
private bool Connected
|
||||
{
|
||||
get { return _connected; }
|
||||
set
|
||||
{
|
||||
if (value == false)
|
||||
{
|
||||
Disconnect();
|
||||
}
|
||||
_connected = value;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private Constants _constants;
|
||||
private Constants Constants => _constants ?? (_constants = new Constants());
|
||||
|
||||
@@ -79,7 +63,7 @@ namespace Modbus.Net.FBox
|
||||
Constants.SignalRServer = msg.SignalRServer;
|
||||
System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||
ConnectionToken = machineId + "," + localSequence;
|
||||
_msg = msg;
|
||||
Msg = msg;
|
||||
_data = new Dictionary<string, double>();
|
||||
_dataType = new Dictionary<string, Type>();
|
||||
}
|
||||
@@ -88,19 +72,18 @@ namespace Modbus.Net.FBox
|
||||
{
|
||||
try
|
||||
{
|
||||
var tokenResponse = await _oauth2.RequestRefreshTokenAsync(_refreshToken);
|
||||
_refreshToken = tokenResponse.RefreshToken;
|
||||
_httpClient.SetBearerToken(tokenResponse.AccessToken);
|
||||
|
||||
var tokenResponse = await _oauth2.RequestRefreshTokenAsync(_refreshToken);
|
||||
_refreshToken = tokenResponse.RefreshToken;
|
||||
_httpClient.SetBearerToken(tokenResponse.AccessToken);
|
||||
|
||||
_httpClient2.SetBearerToken(tokenResponse.AccessToken);
|
||||
_hubConnection.Stop();
|
||||
_hubConnection.Headers["Authorization"] = "Bearer " + tokenResponse.AccessToken;
|
||||
await _hubConnection.Start();
|
||||
await
|
||||
_httpClient2.PostAsync(
|
||||
"dmon/group/" + _dataGroup.Uid + "/start", null);
|
||||
_httpClient2.SetBearerToken(tokenResponse.AccessToken);
|
||||
|
||||
_hubConnection.Headers["Authorization"] = "Bearer " + tokenResponse.AccessToken;
|
||||
await _hubConnection.Start();
|
||||
await
|
||||
_httpClient2.PostAsync(
|
||||
"dmon/group/" + _dataGroup.Uid + "/start", null);
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -119,224 +102,250 @@ namespace Modbus.Net.FBox
|
||||
try
|
||||
{
|
||||
|
||||
_oauth2 = new OAuth2Client(
|
||||
new Uri(Constants.TokenEndpoint),
|
||||
Msg.ClientId,
|
||||
Msg.ClientSecret
|
||||
);
|
||||
var tokenResponse = await _oauth2.RequestResourceOwnerPasswordAsync
|
||||
(
|
||||
Msg.UserId,
|
||||
Msg.Password,
|
||||
Msg.SigninAdditionalValues
|
||||
);
|
||||
if (tokenResponse != null)
|
||||
_oauth2 = new OAuth2Client(
|
||||
new Uri(Constants.TokenEndpoint),
|
||||
Msg.ClientId,
|
||||
Msg.ClientSecret
|
||||
);
|
||||
var tokenResponse = await _oauth2.RequestResourceOwnerPasswordAsync
|
||||
(
|
||||
Msg.UserId,
|
||||
Msg.Password,
|
||||
Msg.SigninAdditionalValues
|
||||
);
|
||||
if (tokenResponse != null)
|
||||
{
|
||||
_refreshToken = tokenResponse.RefreshToken;
|
||||
if (await CallService(Msg, tokenResponse.AccessToken))
|
||||
{
|
||||
_refreshToken = tokenResponse.RefreshToken;
|
||||
await CallService(Msg, tokenResponse.AccessToken);
|
||||
await
|
||||
_httpClient2.PostAsync(
|
||||
"dmon/group/" + _dataGroup.Uid + "/start", null);
|
||||
_connected = true;
|
||||
_timer = new Timer(ChangeToken, null, 3600*1000*4, 3600*1000*4);
|
||||
Console.WriteLine("SignalR Connected success");
|
||||
return true;
|
||||
}
|
||||
|
||||
await
|
||||
_httpClient2.PostAsync(
|
||||
"dmon/group/" + _dataGroup.Uid + "/start", null);
|
||||
Connected = true;
|
||||
_timer = new Timer(ChangeToken, null, 3600*1000*4, 3600*1000*4);
|
||||
Console.WriteLine("SignalR Connected success");
|
||||
return true;
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_oauth2 = null;
|
||||
Console.WriteLine("SignalR Connected failed");
|
||||
Console.WriteLine("SignalR Connected failed " + e.Message);
|
||||
Clear();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task CallService(SignalRSigninMsg msg, string token)
|
||||
private async Task<bool> CallService(SignalRSigninMsg msg, string token)
|
||||
{
|
||||
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
|
||||
var baseAddress = Constants.AspNetWebApiSampleApi;
|
||||
|
||||
_httpClient = new HttpClient
|
||||
try
|
||||
{
|
||||
BaseAddress = new Uri(baseAddress)
|
||||
};
|
||||
var guid = Guid.NewGuid().ToString();
|
||||
|
||||
_httpClient.SetBearerToken(token);
|
||||
var baseAddress = Constants.AspNetWebApiSampleApi;
|
||||
|
||||
//var response = await _httpClient.GetStringAsync("device/spec");
|
||||
|
||||
//List<DeviceSpecSource> deviceSpecs = JsonConvert.DeserializeObject<List<DeviceSpecSource>>(response);
|
||||
//deviceSpecs = deviceSpecs.OrderBy(p => p.Id).ToList();
|
||||
|
||||
|
||||
var response = await _httpClient.GetStringAsync("boxgroup");
|
||||
|
||||
List<BoxGroup> boxGroups = JsonConvert.DeserializeObject<List<BoxGroup>>(response);
|
||||
|
||||
foreach (var boxGroup in boxGroups)
|
||||
{
|
||||
var boxes = boxGroup.BoxRegs;
|
||||
foreach (var box in boxes)
|
||||
_httpClient = new HttpClient
|
||||
{
|
||||
var sessionId = box.Box.CurrentSessionId;
|
||||
var baseUrl = box.Box.CommServer.ApiBaseUrl;
|
||||
var signalrUrl = box.Box.CommServer.SignalRUrl;
|
||||
var boxUid = box.Box.Uid;
|
||||
var boxNo = box.Box.BoxNo;
|
||||
var connectionState = box.Box.ConnectionState;
|
||||
BaseAddress = new Uri(baseAddress)
|
||||
};
|
||||
|
||||
_httpClient.SetBearerToken(token);
|
||||
|
||||
//var response = await _httpClient.GetStringAsync("device/spec");
|
||||
|
||||
//List<DeviceSpecSource> deviceSpecs = JsonConvert.DeserializeObject<List<DeviceSpecSource>>(response);
|
||||
//deviceSpecs = deviceSpecs.OrderBy(p => p.Id).ToList();
|
||||
|
||||
|
||||
if (boxNo != MachineId) continue;
|
||||
var response = await _httpClient.GetStringAsync("boxgroup");
|
||||
|
||||
_httpClient2 = new HttpClient
|
||||
List<BoxGroup> boxGroups = JsonConvert.DeserializeObject<List<BoxGroup>>(response);
|
||||
if (boxGroups == null) return false;
|
||||
|
||||
foreach (var boxGroup in boxGroups)
|
||||
{
|
||||
var boxes = boxGroup.BoxRegs;
|
||||
if (boxes == null) continue;
|
||||
foreach (var box in boxes)
|
||||
{
|
||||
BaseAddress = new Uri(baseUrl)
|
||||
};
|
||||
_httpClient2.SetBearerToken(token);
|
||||
_httpClient2.DefaultRequestHeaders.Add("X-FBox-ClientId", guid);
|
||||
var sessionId = box.Box.CurrentSessionId;
|
||||
var baseUrl = box.Box.CommServer.ApiBaseUrl;
|
||||
var signalrUrl = box.Box.CommServer.SignalRUrl;
|
||||
var boxUid = box.Box.Uid;
|
||||
var boxNo = box.Box.BoxNo;
|
||||
var connectionState = box.Box.ConnectionState;
|
||||
|
||||
response = await _httpClient2.GetStringAsync("box/" + box.Box.Uid + "/dmon/def/grouped");
|
||||
|
||||
|
||||
List<DMonGroup> dataGroups = JsonConvert.DeserializeObject<List<DMonGroup>>(response);
|
||||
foreach (var dataGroup in dataGroups)
|
||||
{
|
||||
if (dataGroup.Name == LocalSequence)
|
||||
if (boxNo != MachineId) continue;
|
||||
if (connectionState != 1)
|
||||
{
|
||||
_dataGroup = dataGroup;
|
||||
break;
|
||||
_connected = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
_boxSessionId = sessionId;
|
||||
_boxNo = boxNo;
|
||||
_connectionState = connectionState;
|
||||
|
||||
_hubConnection = new HubConnection(signalrUrl);
|
||||
_hubConnection.Headers.Add("Authorization", "Bearer " + token);
|
||||
_hubConnection.Headers.Add("X-FBox-ClientId", guid);
|
||||
_hubConnection.Headers.Add("X-FBox-Session", sessionId.ToString());
|
||||
|
||||
IHubProxy dataHubProxy = _hubConnection.CreateHubProxy("clientHub");
|
||||
dataHubProxy.On<int, List<GetValue>>("dMonUpdateValue",
|
||||
(boxSessionId, values) =>
|
||||
_httpClient2 = new HttpClient
|
||||
{
|
||||
BaseAddress = new Uri(baseUrl)
|
||||
};
|
||||
_httpClient2.SetBearerToken(token);
|
||||
_httpClient2.DefaultRequestHeaders.Add("X-FBox-ClientId", guid);
|
||||
|
||||
response = await _httpClient2.GetStringAsync("box/" + box.Box.Uid + "/dmon/def/grouped");
|
||||
|
||||
|
||||
List<DMonGroup> dataGroups = JsonConvert.DeserializeObject<List<DMonGroup>>(response);
|
||||
foreach (var dataGroup in dataGroups)
|
||||
{
|
||||
if (dataGroup.Name == LocalSequence)
|
||||
{
|
||||
_dataGroup = dataGroup;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_boxSessionId = sessionId;
|
||||
_boxNo = boxNo;
|
||||
_connectionState = connectionState;
|
||||
|
||||
_hubConnection = new HubConnection(signalrUrl);
|
||||
_hubConnection.Headers.Add("Authorization", "Bearer " + token);
|
||||
_hubConnection.Headers.Add("X-FBox-ClientId", guid);
|
||||
_hubConnection.Headers.Add("X-FBox-Session", sessionId.ToString());
|
||||
|
||||
IHubProxy dataHubProxy = _hubConnection.CreateHubProxy("clientHub");
|
||||
dataHubProxy.On<int, List<GetValue>>("dMonUpdateValue",
|
||||
(boxSessionId, values) =>
|
||||
{
|
||||
|
||||
//#if DEBUG
|
||||
//Console.WriteLine($"Box session {boxSessionId} return at {DateTime.Now}");
|
||||
//Console.WriteLine($"Box session {boxSessionId} return at {DateTime.Now}");
|
||||
//#endif
|
||||
|
||||
_timeStamp = DateTime.Now;
|
||||
_timeStamp = DateTime.Now;
|
||||
|
||||
foreach (var value in values)
|
||||
{
|
||||
if (value.Status != 0)
|
||||
foreach (var value in values)
|
||||
{
|
||||
if (value.Status != 0)
|
||||
{
|
||||
lock (_data)
|
||||
{
|
||||
var dMonEntry =
|
||||
_dataGroup.DMonEntries.FirstOrDefault(
|
||||
p => p.Uid == value.Id);
|
||||
if (dMonEntry != null)
|
||||
{
|
||||
if (_data.ContainsKey(dMonEntry.Desc))
|
||||
{
|
||||
_data.Remove(dMonEntry.Desc);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return;
|
||||
}
|
||||
lock (_data)
|
||||
{
|
||||
var dMonEntry =
|
||||
_dataGroup.DMonEntries.FirstOrDefault(
|
||||
p => p.Uid == value.Id);
|
||||
if (dMonEntry != null)
|
||||
if (_dataGroup.DMonEntries.Any(p => p.Uid == value.Id))
|
||||
{
|
||||
if (_data.ContainsKey(dMonEntry.Desc))
|
||||
if (_data == null)
|
||||
{
|
||||
_data.Remove(dMonEntry.Desc);
|
||||
_data = new Dictionary<string, double>();
|
||||
}
|
||||
|
||||
var dMonEntry = _dataGroup.DMonEntries.FirstOrDefault(
|
||||
p => p.Uid == value.Id);
|
||||
|
||||
if (value.Value.HasValue && dMonEntry != null)
|
||||
{
|
||||
if (_data.ContainsKey(dMonEntry.Desc))
|
||||
{
|
||||
_data[dMonEntry.Desc] = value.Value.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
_data.Add(dMonEntry.Desc, value.Value.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
dataHubProxy.On<int, string, int, int>("boxConnectionStateChanged",
|
||||
async (newConnectionToken, getBoxUid, oldStatus, newStatus) =>
|
||||
{
|
||||
#if DEBUG
|
||||
Console.WriteLine(
|
||||
$"Box uid {getBoxUid} change state at {DateTime.Now} new connectionToken {newConnectionToken} newStatus {newStatus}");
|
||||
#endif
|
||||
sessionId = newConnectionToken;
|
||||
_boxSessionId = sessionId;
|
||||
|
||||
_connectionState = newStatus;
|
||||
|
||||
if (!IsConnected || _httpClient2 == null || _hubConnection == null)
|
||||
{
|
||||
Clear();
|
||||
return;
|
||||
}
|
||||
lock (_data)
|
||||
try
|
||||
{
|
||||
if (_dataGroup.DMonEntries.Any(p => p.Uid == value.Id))
|
||||
while (
|
||||
!_httpClient2.DefaultRequestHeaders.TryAddWithoutValidation("X-FBox-Session",
|
||||
sessionId.ToString()))
|
||||
{
|
||||
if (_data == null)
|
||||
{
|
||||
_data = new Dictionary<string, double>();
|
||||
}
|
||||
_httpClient2.DefaultRequestHeaders.Remove("X-FBox-Session");
|
||||
}
|
||||
_httpClient2.DefaultRequestHeaders.Add("X-FBox-Session", sessionId.ToString());
|
||||
|
||||
var dMonEntry = _dataGroup.DMonEntries.FirstOrDefault(
|
||||
p => p.Uid == value.Id);
|
||||
|
||||
_hubConnection.Headers["X-FBox-Session"] = sessionId.ToString();
|
||||
await _hubConnection.Start();
|
||||
|
||||
if (value.Value.HasValue && dMonEntry != null)
|
||||
if (newStatus == 1)
|
||||
{
|
||||
if (IsConnected)
|
||||
{
|
||||
if (_data.ContainsKey(dMonEntry.Desc))
|
||||
{
|
||||
_data[dMonEntry.Desc] = value.Value.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
_data.Add(dMonEntry.Desc, value.Value.Value);
|
||||
}
|
||||
await
|
||||
_httpClient2.PostAsync(
|
||||
"dmon/group/" + _dataGroup.Uid + "/start", null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
dataHubProxy.On<int, string, int, int>("boxConnectionStateChanged",
|
||||
async (newConnectionToken, getBoxUid, oldStatus, newStatus) =>
|
||||
else
|
||||
{
|
||||
lock (_data)
|
||||
{
|
||||
_data.Clear();
|
||||
}
|
||||
//await DisconnectAsync();
|
||||
//_connected = false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("SignalR boxSessionId change error: " + ex.Message);
|
||||
await DisconnectAsync();
|
||||
}
|
||||
});
|
||||
|
||||
_hubConnection.Error += async ex =>
|
||||
{
|
||||
//#if DEBUG
|
||||
//Console.WriteLine(
|
||||
//$"Box uid {getBoxUid} change state at {DateTime.Now} new connectionToken {newConnectionToken} newStatus {newStatus}");
|
||||
//#endif
|
||||
sessionId = newConnectionToken;
|
||||
_boxSessionId = sessionId;
|
||||
Console.WriteLine(@"SignalR error: {0}", ex.Message);
|
||||
await DisconnectAsync();
|
||||
_connected = false;
|
||||
};
|
||||
|
||||
_connectionState = newStatus;
|
||||
|
||||
_httpClient2.DefaultRequestHeaders.Remove("X-FBox-Session");
|
||||
_httpClient2.DefaultRequestHeaders.Add("X-FBox-Session",
|
||||
sessionId.ToString());
|
||||
_hubConnection.Headers["X-FBox-Session"] = sessionId.ToString();
|
||||
_hubConnection.Closed += () =>
|
||||
{
|
||||
_hubConnection.Dispose();
|
||||
_connected = false;
|
||||
};
|
||||
|
||||
if (_hubConnection.State == ConnectionState.Disconnected)
|
||||
{
|
||||
await _hubConnection.Start();
|
||||
}
|
||||
ServicePointManager.DefaultConnectionLimit = 10;
|
||||
|
||||
if (newStatus == 1)
|
||||
{
|
||||
if (Connected)
|
||||
{
|
||||
await
|
||||
_httpClient2.PostAsync(
|
||||
"dmon/group/" + _dataGroup.Uid + "/start", null);
|
||||
}
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
lock (_data)
|
||||
{
|
||||
_data.Clear();
|
||||
}
|
||||
Connected = false;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
_hubConnection.Error += async ex =>
|
||||
{
|
||||
Console.WriteLine(@"SignalR error: {0}", ex.Message);
|
||||
await ConnectRecovery(_hubConnection);
|
||||
};
|
||||
|
||||
_hubConnection.Closed += async () =>
|
||||
{
|
||||
await ConnectRecovery(_hubConnection);
|
||||
};
|
||||
|
||||
ServicePointManager.DefaultConnectionLimit = 10;
|
||||
|
||||
if (_dataGroup == null) return;
|
||||
if (_dataGroup == null) return false;
|
||||
|
||||
var groupUid = _dataGroup.Uid;
|
||||
var groupName = _dataGroup.Name;
|
||||
@@ -351,148 +360,118 @@ namespace Modbus.Net.FBox
|
||||
}
|
||||
|
||||
_dataType = new Dictionary<string, Type>();
|
||||
|
||||
foreach (var dMonEntry in _dataGroup.DMonEntries)
|
||||
{
|
||||
Type type;
|
||||
switch (dMonEntry.DataType)
|
||||
if (_dataGroup.DMonEntries != null)
|
||||
{
|
||||
//位
|
||||
case 0:
|
||||
foreach (var dMonEntry in _dataGroup.DMonEntries)
|
||||
{
|
||||
type = typeof (bool);
|
||||
break;
|
||||
}
|
||||
//16位无符号
|
||||
case 1:
|
||||
{
|
||||
type = typeof (ushort);
|
||||
break;
|
||||
}
|
||||
//16位有符号
|
||||
case 2:
|
||||
{
|
||||
type = typeof (short);
|
||||
break;
|
||||
}
|
||||
//32位无符号
|
||||
case 11:
|
||||
{
|
||||
type = typeof (uint);
|
||||
break;
|
||||
}
|
||||
//32位有符号
|
||||
case 12:
|
||||
{
|
||||
type = typeof (int);
|
||||
break;
|
||||
}
|
||||
//16位BCD
|
||||
case 3:
|
||||
{
|
||||
type = typeof (short);
|
||||
break;
|
||||
}
|
||||
//32位BCD
|
||||
case 13:
|
||||
{
|
||||
type = typeof (int);
|
||||
break;
|
||||
}
|
||||
//浮点数
|
||||
case 16:
|
||||
{
|
||||
type = typeof (float);
|
||||
break;
|
||||
}
|
||||
//16位16进制
|
||||
case 4:
|
||||
{
|
||||
type = typeof (short);
|
||||
break;
|
||||
}
|
||||
//32位16进制
|
||||
case 14:
|
||||
{
|
||||
type = typeof (int);
|
||||
break;
|
||||
}
|
||||
//16位2进制
|
||||
case 5:
|
||||
{
|
||||
type = typeof (short);
|
||||
break;
|
||||
}
|
||||
//32位2进制
|
||||
case 15:
|
||||
{
|
||||
type = typeof (int);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
type = typeof (short);
|
||||
break;
|
||||
Type type;
|
||||
switch (dMonEntry.DataType)
|
||||
{
|
||||
//位
|
||||
case 0:
|
||||
{
|
||||
type = typeof (bool);
|
||||
break;
|
||||
}
|
||||
//16位无符号
|
||||
case 1:
|
||||
{
|
||||
type = typeof (ushort);
|
||||
break;
|
||||
}
|
||||
//16位有符号
|
||||
case 2:
|
||||
{
|
||||
type = typeof (short);
|
||||
break;
|
||||
}
|
||||
//32位无符号
|
||||
case 11:
|
||||
{
|
||||
type = typeof (uint);
|
||||
break;
|
||||
}
|
||||
//32位有符号
|
||||
case 12:
|
||||
{
|
||||
type = typeof (int);
|
||||
break;
|
||||
}
|
||||
//16位BCD
|
||||
case 3:
|
||||
{
|
||||
type = typeof (short);
|
||||
break;
|
||||
}
|
||||
//32位BCD
|
||||
case 13:
|
||||
{
|
||||
type = typeof (int);
|
||||
break;
|
||||
}
|
||||
//浮点数
|
||||
case 16:
|
||||
{
|
||||
type = typeof (float);
|
||||
break;
|
||||
}
|
||||
//16位16进制
|
||||
case 4:
|
||||
{
|
||||
type = typeof (short);
|
||||
break;
|
||||
}
|
||||
//32位16进制
|
||||
case 14:
|
||||
{
|
||||
type = typeof (int);
|
||||
break;
|
||||
}
|
||||
//16位2进制
|
||||
case 5:
|
||||
{
|
||||
type = typeof (short);
|
||||
break;
|
||||
}
|
||||
//32位2进制
|
||||
case 15:
|
||||
{
|
||||
type = typeof (int);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
type = typeof (short);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!_dataType.ContainsKey(dMonEntry.Desc))
|
||||
{
|
||||
_dataType.Add(dMonEntry.Desc, type);
|
||||
}
|
||||
else
|
||||
{
|
||||
_dataType[dMonEntry.Desc] = type;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!_dataType.ContainsKey(dMonEntry.Desc))
|
||||
{
|
||||
_dataType.Add(dMonEntry.Desc, type);
|
||||
}
|
||||
else
|
||||
{
|
||||
_dataType[dMonEntry.Desc] = type;
|
||||
}
|
||||
await _hubConnection.Start();
|
||||
await dataHubProxy.Invoke("updateClientId", guid);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
await _hubConnection.Start();
|
||||
await dataHubProxy.Invoke("updateClientId", guid);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
Clear();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ConnectRecovery(HubConnection hubConnection)
|
||||
{
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
if (hubConnection.State != ConnectionState.Connected)
|
||||
{
|
||||
try
|
||||
{
|
||||
hubConnection.Stop();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored
|
||||
}
|
||||
|
||||
await hubConnection.Start();
|
||||
if (Connected)
|
||||
{
|
||||
await
|
||||
_httpClient2.PostAsync(
|
||||
"dmon/group/" + _dataGroup.Uid + "/start", null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
lock (_data)
|
||||
{
|
||||
_data.Clear();
|
||||
}
|
||||
Connected = false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public override bool Disconnect()
|
||||
{
|
||||
return AsyncHelper.RunSync(DisconnectAsync);
|
||||
@@ -502,22 +481,86 @@ namespace Modbus.Net.FBox
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
if (_httpClient2 != null)
|
||||
{
|
||||
await
|
||||
_httpClient2.PostAsync(
|
||||
"dmon/group/" + _groupUid + "/stop",
|
||||
null);
|
||||
_connected = false;
|
||||
}
|
||||
Clear();
|
||||
Console.WriteLine("SignalR Disconnect success");
|
||||
return true;
|
||||
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine("SignalR Disconnect failed " + e.Message);
|
||||
Clear();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private async void Clear()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_hubConnection != null)
|
||||
{
|
||||
await Task.Run(() => _hubConnection.Stop(TimeSpan.FromSeconds(10)));
|
||||
_hubConnection = null;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
_hubConnection = null;
|
||||
// ignored
|
||||
}
|
||||
try
|
||||
{
|
||||
if (_httpClient != null)
|
||||
{
|
||||
_httpClient.Dispose();
|
||||
_httpClient = null;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
try
|
||||
{
|
||||
if (_httpClient2 != null)
|
||||
{
|
||||
_httpClient2.Dispose();
|
||||
_httpClient2 = null;
|
||||
}
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
if (_data != null)
|
||||
{
|
||||
lock (_data)
|
||||
{
|
||||
_data.Clear();
|
||||
_dataType.Clear();
|
||||
}
|
||||
}
|
||||
_timeStamp = DateTime.MinValue;
|
||||
_connected = false;
|
||||
try
|
||||
{
|
||||
if (_timer != null)
|
||||
{
|
||||
_timer.Dispose();
|
||||
_timer = null;
|
||||
Console.WriteLine("SignalR Disconnect success");
|
||||
return true;
|
||||
|
||||
}
|
||||
}
|
||||
catch
|
||||
catch (Exception)
|
||||
{
|
||||
Console.WriteLine("SignalR Disconnect failed");
|
||||
return false;
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
|
||||
@@ -532,16 +575,22 @@ namespace Modbus.Net.FBox
|
||||
}
|
||||
|
||||
public override byte[] SendMsg(byte[] message)
|
||||
{
|
||||
return AsyncHelper.RunSync(() => SendMsgAsync(message));
|
||||
}
|
||||
|
||||
public override async Task<byte[]> SendMsgAsync(byte[] message)
|
||||
{
|
||||
if (_httpClient == null)
|
||||
{
|
||||
Connected = false;
|
||||
await DisconnectAsync();
|
||||
_connected = false;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (_hubConnection.State == ConnectionState.Disconnected)
|
||||
{
|
||||
_hubConnection.Start();
|
||||
await _hubConnection.Start();
|
||||
}
|
||||
|
||||
var formater = new AddressFormaterFBox();
|
||||
@@ -549,22 +598,28 @@ namespace Modbus.Net.FBox
|
||||
|
||||
byte[] ans;
|
||||
|
||||
if (_connectionState != 1)
|
||||
{
|
||||
Connected = false;
|
||||
Console.WriteLine($"Return Value Rejected with connectionToken {ConnectionToken}");
|
||||
return null;
|
||||
}
|
||||
//if (_connectionState != 1)
|
||||
//{
|
||||
//await DisconnectAsync();
|
||||
//_connected = false;
|
||||
//Console.WriteLine($"Return Value Rejected with connectionToken {ConnectionToken}");
|
||||
//return null;
|
||||
//}
|
||||
|
||||
if (_timeStamp == DateTime.MinValue)
|
||||
{
|
||||
return Encoding.ASCII.GetBytes("NoData");
|
||||
}
|
||||
|
||||
if (DateTime.Now - _timeStamp > TimeSpan.FromMinutes(1))
|
||||
if (DateTime.Now - _timeStamp > TimeSpan.FromMinutes(2))
|
||||
{
|
||||
Connected = false;
|
||||
return null;
|
||||
Console.WriteLine("SignalR Timeout: {0} {1} {2}", _timeStamp, DateTime.Now, ConnectionToken);
|
||||
if (_connectionState != 1)
|
||||
{
|
||||
await DisconnectAsync();
|
||||
_connected = false;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Dictionary<string, double> machineDataValue;
|
||||
@@ -575,7 +630,8 @@ namespace Modbus.Net.FBox
|
||||
var machineDataType = _dataType;
|
||||
if (machineDataType == null || machineDataType.Count == 0)
|
||||
{
|
||||
Connected = false;
|
||||
await DisconnectAsync();
|
||||
_connected = false;
|
||||
Console.WriteLine($"Return Value Rejected with connectionToken {ConnectionToken}");
|
||||
return null;
|
||||
}
|
||||
@@ -597,8 +653,14 @@ namespace Modbus.Net.FBox
|
||||
machineDataValue[formater.FormatAddress(translator.GetAreaName(area), address)],
|
||||
machineDataType[formater.FormatAddress(translator.GetAreaName(area), address)]);
|
||||
}
|
||||
catch (Exception e)
|
||||
catch (Exception)
|
||||
{
|
||||
if (machineDataValue.Count != machineDataType.Count)
|
||||
{
|
||||
await
|
||||
_httpClient2.PostAsync(
|
||||
"dmon/group/" + _dataGroup.Uid + "/start", null);
|
||||
}
|
||||
return Encoding.ASCII.GetBytes("NoData");
|
||||
//dataAns[0] =
|
||||
//Convert.ChangeType(
|
||||
@@ -612,10 +674,5 @@ namespace Modbus.Net.FBox
|
||||
|
||||
return ans;
|
||||
}
|
||||
|
||||
public override Task<byte[]> SendMsgAsync(byte[] message)
|
||||
{
|
||||
return Task.Factory.StartNew(() => SendMsg(message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -75,7 +75,7 @@
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\ModBus.Net\Modbus.Net.csproj">
|
||||
<ProjectReference Include="..\Modbus.Net\Modbus.Net.csproj">
|
||||
<Project>{124ebef2-8960-4447-84cf-1d683b1ef7cc}</Project>
|
||||
<Name>Modbus.Net</Name>
|
||||
</ProjectReference>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<package >
|
||||
<metadata>
|
||||
<id>Modbus.Net.FBox</id>
|
||||
<version>1.0.1</version>
|
||||
<version>1.0.8</version>
|
||||
<title>Modbus.Net.FBox</title>
|
||||
<authors>Chris L.(Luo Sheng)</authors>
|
||||
<owners>Hangzhou Delian Information and Science Technology Co., Ltd.</owners>
|
||||
|
||||
@@ -32,5 +32,5 @@ using System.Runtime.InteropServices;
|
||||
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值,
|
||||
// 方法是按如下所示使用“*”: :
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.1")]
|
||||
[assembly: AssemblyFileVersion("1.0.1")]
|
||||
[assembly: AssemblyVersion("1.0.8")]
|
||||
[assembly: AssemblyFileVersion("1.0.8")]
|
||||
|
||||
Reference in New Issue
Block a user