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