44using System . Diagnostics ;
55using System . Linq ;
66using System . Timers ;
7- using _1RM . Model . DAO ;
87using _1RM . Model . Protocol . Base ;
98using _1RM . Service ;
109using _1RM . Service . DataSource ;
10+ using _1RM . Service . DataSource . DAO ;
1111using _1RM . Service . DataSource . Model ;
12+ using _1RM . Utils ;
1213using _1RM . View ;
1314using _1RM . View . Launcher ;
1415using Shawn . Utils ;
16+ using Stylet ;
1517using ServerListPageViewModel = _1RM . View . ServerList . ServerListPageViewModel ;
1618
1719namespace _1RM . Model
@@ -34,7 +36,7 @@ public GlobalData(ConfigurationService configurationService)
3436 _timer . Start ( ) ;
3537 }
3638
37- private DataSourceService _sourceService ;
39+ private DataSourceService ? _sourceService ;
3840 private readonly ConfigurationService _configurationService ;
3941
4042 public void SetDataSourceService ( DataSourceService sourceService )
@@ -112,13 +114,17 @@ public bool ReloadServerList(bool focus = false)
112114 foreach ( var additionalSource in _sourceService . AdditionalSources )
113115 {
114116 // 对于断线的数据源,隔一段时间后尝试重连
115- if ( additionalSource . Value . Status == EnumDatabaseStatus . LostConnection )
117+ // TODO try to reconnect when network is available
118+ if ( additionalSource . Value . Status != EnumDatabaseStatus . OK )
116119 {
120+ #if DEBUG
121+ if ( additionalSource . Value . StatueTime . AddMinutes ( 1 ) < DateTime . Now )
122+ #else
117123 if ( additionalSource . Value . StatueTime . AddMinutes ( 10 ) < DateTime . Now )
124+ #endif
118125 {
119126 additionalSource . Value . Database_SelfCheck ( ) ;
120127 }
121- continue ;
122128 }
123129
124130 if ( needRead == false )
@@ -287,53 +293,6 @@ public Result UpdateServer(IEnumerable<ProtocolBase> protocolServers)
287293 }
288294 }
289295
290- public Result DeleteServer ( ProtocolBase protocolServer )
291- {
292- StopTick ( ) ;
293- try
294- {
295- Debug . Assert ( protocolServer . IsTmpSession ( ) == false ) ;
296-
297- var source = protocolServer . GetDataSource ( ) ;
298- var needReload = source ? . NeedRead ( ) ?? false ;
299- if ( source == null || source . IsWritable == false )
300- {
301- // 正常情况下,不应该执行到这个地方
302- return Result . Fail ( $ "TXT: We can not delete on `{ protocolServer . DataSourceName } ` because `{ protocolServer . DataSourceName } ` is readonly for you") ;
303- } ;
304-
305- var ret = source . Database_DeleteServer ( protocolServer . Id ) ;
306- if ( ret . IsSuccess )
307- {
308- if ( needReload == false )
309- {
310- var old = GetItemById ( source . DataSourceName , protocolServer . Id ) ;
311- if ( old != null )
312- {
313- VmItemList . Remove ( old ) ;
314- IoC . Get < ServerListPageViewModel > ( ) ? . VmServerList ? . Remove ( old ) ; // invoke main list ui change
315- IoC . Get < ServerSelectionsViewModel > ( ) ? . VmServerList ? . Remove ( old ) ; // invoke launcher ui change
316- }
317- }
318-
319- if ( needReload )
320- {
321- ReloadServerList ( ) ;
322- }
323- else
324- {
325- ReadTagsFromServers ( ) ;
326- IoC . Get < ServerListPageViewModel > ( ) . ClearSelection ( ) ;
327- }
328- }
329- return ret ;
330- }
331- finally
332- {
333- StartTick ( ) ;
334- }
335- }
336-
337296 public Result DeleteServer ( IEnumerable < ProtocolBase > protocolServers )
338297 {
339298 StopTick ( ) ;
@@ -362,8 +321,13 @@ public Result DeleteServer(IEnumerable<ProtocolBase> protocolServers)
362321 if ( old != null )
363322 {
364323 VmItemList . Remove ( old ) ;
365- IoC . Get < ServerListPageViewModel > ( ) . VmServerList . Remove ( old ) ; // invoke main list ui change
366- IoC . Get < ServerSelectionsViewModel > ( ) . VmServerList . Remove ( old ) ; // invoke launcher ui change
324+ Execute . OnUIThread ( ( ) =>
325+ {
326+ if ( IoC . Get < ServerListPageViewModel > ( ) . VmServerList . Contains ( old ) )
327+ IoC . Get < ServerListPageViewModel > ( ) . VmServerList . Remove ( old ) ; // invoke main list ui change
328+ if ( IoC . Get < ServerSelectionsViewModel > ( ) . VmServerList . Contains ( old ) )
329+ IoC . Get < ServerSelectionsViewModel > ( ) . VmServerList . Remove ( old ) ; // invoke launcher ui change
330+ } ) ;
367331 }
368332 }
369333 }
@@ -398,6 +362,11 @@ public Result DeleteServer(IEnumerable<ProtocolBase> protocolServers)
398362 return Result . Success ( ) ;
399363 }
400364 }
365+ catch ( Exception e )
366+ {
367+ MsAppCenterHelper . Error ( e ) ;
368+ throw ;
369+ }
401370 finally
402371 {
403372 StartTick ( ) ;
@@ -444,13 +413,18 @@ private void TimerOnElapsed(object? sender, ElapsedEventArgs e)
444413
445414 if ( ReloadServerList ( ) )
446415 {
447- SimpleLogHelper . Debug ( "check database update - reload data" + _timer . GetHashCode ( ) ) ;
416+ SimpleLogHelper . Debug ( "check database update - reload data by timer " + _timer . GetHashCode ( ) ) ;
448417 }
449418 else
450419 {
451- SimpleLogHelper . Debug ( "check database update - no need reload" + _timer . GetHashCode ( ) ) ;
420+ SimpleLogHelper . Debug ( "check database update - no need reload by timer " + _timer . GetHashCode ( ) ) ;
452421 }
453422 }
423+ catch ( Exception ex )
424+ {
425+ MsAppCenterHelper . Error ( ex ) ;
426+ throw ;
427+ }
454428 finally
455429 {
456430 lock ( this )
0 commit comments