From 483130bc149565195b74e391836f9c63efececce Mon Sep 17 00:00:00 2001 From: "parallelbgls@outlook.com" Date: Tue, 5 Jan 2016 17:14:00 +0800 Subject: [PATCH] 2016-01-05 update 1 continue fix bugs in fboxconnection --- .../ModBus.Net/FBox/SignalRConnector.cs | 205 ++++++++++-------- Modbus.Net/ModBus.Net/TaskManager.cs | 2 +- 2 files changed, 110 insertions(+), 97 deletions(-) diff --git a/Modbus.Net/ModBus.Net/FBox/SignalRConnector.cs b/Modbus.Net/ModBus.Net/FBox/SignalRConnector.cs index 9cf856e..4b08a91 100644 --- a/Modbus.Net/ModBus.Net/FBox/SignalRConnector.cs +++ b/Modbus.Net/ModBus.Net/FBox/SignalRConnector.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; +using System.Threading; using System.Threading.Tasks; using DelinRemoteControlBoxTest; using Microsoft.AspNet.SignalR.Client; @@ -24,11 +25,12 @@ namespace ModBus.Net.FBox public class SignalRConnector : BaseConnector { private static Dictionary _oauth2; + private static Dictionary _refreshToken; private static Dictionary _httpClient; private static Dictionary _httpClient2; private static Dictionary _hubConnections; - + private static Dictionary _boxUidMsg; private static Dictionary _boxUidSessionId; private static Dictionary> _boxUidDataGroups; private static Dictionary _connectionTokenState; @@ -40,6 +42,8 @@ namespace ModBus.Net.FBox public override string ConnectionToken { get; } + private Timer _timer; + private string MachineId { get @@ -57,28 +61,6 @@ namespace ModBus.Net.FBox private static AsyncLock _lock = new AsyncLock(); - private static bool _retokenFlag; - private static bool RetokenFlag - { - get - { - return _retokenFlag; - } - set - { - _retokenFlag = value; - if (_retokenFlag) - { - System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(RetokenReset), null, 300000, System.Threading.Timeout.Infinite); - } - } - } - - private static void RetokenReset(object state) - { - RetokenFlag = false; - } - private bool _connected; public override bool IsConnected { get { return _connected; } } private SignalRSigninMsg Msg { get; set;} @@ -95,7 +77,6 @@ namespace ModBus.Net.FBox public SignalRConnector(string machineId, string localSequence, SignalRSigninMsg msg) { - RetokenFlag = false; Constants.SignalRServer = msg.SignalRServer; System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; }; ConnectionToken = machineId + "," + localSequence; @@ -103,6 +84,8 @@ namespace ModBus.Net.FBox { _httpClient = new Dictionary(); _oauth2 = new Dictionary(); + _refreshToken = new Dictionary(); + _boxUidMsg = new Dictionary(); _hubConnections = new Dictionary(); _httpClient2 = new Dictionary(); _machineData = new Dictionary>(); @@ -112,11 +95,45 @@ namespace ModBus.Net.FBox _groupNameUid = new Dictionary(); _groupNameBoxUid = new Dictionary(); _boxUidDataGroups = new Dictionary>(); - _boxUidBoxNo = new Dictionary(); + _boxUidBoxNo = new Dictionary(); + _timer = new Timer(ChangeToken, null, 3600 * 1000 * 4, 3600 * 1000 * 4); } Msg = msg; } + private async void ChangeToken(object sender) + { + try + { + var tokenResponse = await _oauth2[Msg].RequestRefreshTokenAsync(_refreshToken[Msg]); + _refreshToken[Msg] = tokenResponse.RefreshToken; + _httpClient[Msg].SetBearerToken(tokenResponse.AccessToken); + foreach (var boxUidMsg in _boxUidMsg) + { + if (boxUidMsg.Value.Equals(Msg)) + { + if (_httpClient2.ContainsKey(boxUidMsg.Key) && _hubConnections.ContainsKey(boxUidMsg.Key)) + _httpClient2[boxUidMsg.Key].SetBearerToken(tokenResponse.AccessToken); + _hubConnections[boxUidMsg.Key].Stop(); + _hubConnections[boxUidMsg.Key].Headers["Authorization"] = "Bearer " + tokenResponse.AccessToken; + await _hubConnections[boxUidMsg.Key].Start(); + var localDataGroups = _boxUidDataGroups[boxUidMsg.Key]; + foreach (var localDataGroup in localDataGroups) + { + await + _httpClient2[boxUidMsg.Key].PostAsync( + "dmon/group/" + localDataGroup.Uid + "/start", null); + } + } + } + } + catch (Exception e) + { + Console.WriteLine("Retoken failed." + e.Message); + } + Console.WriteLine("Retoken success."); + } + public override bool Connect() { return AsyncHelper.RunSync(ConnectAsync); @@ -135,15 +152,15 @@ namespace ModBus.Net.FBox Msg.ClientId, Msg.ClientSecret )); - var tokenResponse = _oauth2[Msg].RequestResourceOwnerPasswordAsync + var tokenResponse = await _oauth2[Msg].RequestResourceOwnerPasswordAsync ( Msg.UserId, Msg.Password, Msg.SigninAdditionalValues - ).Result; + ); if (tokenResponse != null) { - RetokenFlag = true; + _refreshToken.Add(Msg, tokenResponse.RefreshToken); AsyncHelper.RunSync(()=> CallService(Msg, tokenResponse.AccessToken)); } } @@ -237,6 +254,7 @@ namespace ModBus.Net.FBox _boxUidDataGroups.Add(boxUid, dataGroups); _boxUidSessionId.Add(boxUid, sessionId); _boxUidBoxNo.Add(boxUid, boxNo); + _boxUidMsg.Add(boxUid, Msg); var hubConnection = new HubConnection(signalrUrl); _hubConnections.Add(boxUid, hubConnection); @@ -269,7 +287,7 @@ namespace ModBus.Net.FBox var dMonEntry = dataGroupInner.DMonEntries.FirstOrDefault( p => p.Uid == value.Id); - if (dMonEntry != null) + if (dMonEntry != null && _machineData.ContainsKey(localBoxNo + "," + dataGroupInner.Name)) { if (_machineData[localBoxNo + "," + dataGroupInner.Name] .ContainsKey(dMonEntry.Desc)) @@ -385,75 +403,15 @@ namespace ModBus.Net.FBox } }); - hubConnection.Error += ex => Console.WriteLine(@"SignalR error: {0}", ex.Message); + hubConnection.Error += async ex => + { + Console.WriteLine(@"SignalR error: {0}", ex.Message); + await ConnectRecovery(hubConnection); + }; hubConnection.Closed += async () => { - try - { - string getBoxUid; - lock (_boxUidSessionId) - { - getBoxUid = - _boxUidSessionId.FirstOrDefault( - p => p.Value == int.Parse(hubConnection.Headers["X-FBox-Session"])).Key; - } - if (hubConnection.State != ConnectionState.Connected) - { - - await hubConnection.Start(); - if (IsConnected) - { - var localDataGroups = _boxUidDataGroups[getBoxUid]; - foreach (var localDataGroup in localDataGroups) - { - await - _httpClient2[getBoxUid].PostAsync( - "dmon/group/" + localDataGroup.Uid + "/start", null); - } - } - } - } - catch (HttpClientException e) - { - using (await _lock.LockAsync()) - { - if (!RetokenFlag) - { - foreach(var httpClient in _httpClient) - { - httpClient.Value.Dispose(); - } - foreach(var httpClient in _httpClient2) - { - httpClient.Value.Dispose(); - } - foreach(var hubConnectiont in _hubConnections) - { - //hubConnectiont.Value.Stop(); - hubConnectiont.Value.Dispose(); - } - _oauth2.Clear(); - _httpClient.Clear(); - _httpClient2.Clear(); - _hubConnections.Clear(); - - _boxUidSessionId.Clear(); - _boxUidDataGroups.Clear(); - _connectionTokenState.Clear(); - _groupNameUid.Clear(); - _groupNameBoxUid.Clear(); - _machineData.Clear(); - _machineDataType.Clear(); - _boxUidBoxNo.Clear(); - } - } - _connected = false; - } - catch - { - _connected = false; - } + await ConnectRecovery(hubConnection); }; ServicePointManager.DefaultConnectionLimit = 10; @@ -586,7 +544,62 @@ namespace ModBus.Net.FBox } } } - + + private async Task ConnectRecovery(HubConnection hubConnection) + { + string getBoxUid; + lock (_boxUidSessionId) + { + getBoxUid = + _boxUidSessionId.FirstOrDefault( + p => p.Value == int.Parse(hubConnection.Headers["X-FBox-Session"])).Key; + } + try + { + if (hubConnection.State != ConnectionState.Connected) + { + try + { + hubConnection.Stop(); + } + catch + { + // ignored + } + + await hubConnection.Start(); + if (IsConnected) + { + var localDataGroups = _boxUidDataGroups[getBoxUid]; + foreach (var localDataGroup in localDataGroups) + { + await + _httpClient2[getBoxUid].PostAsync( + "dmon/group/" + localDataGroup.Uid + "/start", null); + } + } + } + } + catch + { + if (_boxUidBoxNo.ContainsKey(getBoxUid)) + { + var localBoxNo = _boxUidBoxNo[getBoxUid]; + lock (_machineData) + { + foreach (var machineDataUnit in _machineData) + { + if (machineDataUnit.Key.Contains(localBoxNo)) + { + _machineData.Remove(machineDataUnit.Key); + } + } + } + } + _connected = false; + } + } + public override bool Disconnect() { return AsyncHelper.RunSync(DisconnectAsync); diff --git a/Modbus.Net/ModBus.Net/TaskManager.cs b/Modbus.Net/ModBus.Net/TaskManager.cs index 12fd3a5..11be60b 100644 --- a/Modbus.Net/ModBus.Net/TaskManager.cs +++ b/Modbus.Net/ModBus.Net/TaskManager.cs @@ -238,7 +238,7 @@ namespace ModBus.Net _getCycle = value; } _timer = new Timer(MaintainTasks, null, 0, _getCycle * 1000); - _timer2 = new Timer(MaintainTasks2, null, _getCycle * 6000, _getCycle * 6000); + _timer2 = new Timer(MaintainTasks2, null, _getCycle * 2000, _getCycle * 2000); //MaintainTasks(null); } }