@ -6,6 +6,7 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging ;
using Models ;
using SlnMesnac.Config ;
using SlnMesnac.Model.domain ;
using SlnMesnac.Repository ;
using SlnMesnac.Rfid ;
using SlnMesnac.TouchSocket ;
@ -55,6 +56,7 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage
LoadDeviceInfo ( ) ;
LoadSenorInfo ( ) ;
StartCheckStatus ( ) ;
CollectState ( ) ;
}
/// <summary>
/// 日志信息
@ -83,7 +85,7 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage
LogMessages . Add ( $"{DateTime.Now}: {msg}" ) ;
while ( LogMessages . Count > 10 )
{
LogMessages . RemoveAt( 0 ) ;
LogMessages . Clear( ) ;
}
var orderedList = LogMessages . OrderByDescending ( x = > x ) ; // 排序后转为 List
LogMessages = new ObservableCollection < string > ( orderedList ) ;
@ -131,7 +133,7 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage
private void LoadDeviceInfo ( )
{
List < Base_device_info > DeviceInfos = sqlSugarClient . Queryable < Base_device_info > ( ) . Where ( expression : x = > x . Collectid = = appConfig . StationCode ) . ToList ( ) ;
List < Base_device_info > DeviceInfos = sqlSugarClient . Queryable < Base_device_info > ( ) . Where ( expression : x = > x . Collectid = = appConfig . StationCode & & x . Deleteflag = = 0 ) . ToList ( ) ;
App . Current . Dispatcher . Invoke ( ( ) = >
{
Deviceinfo . Clear ( ) ;
@ -152,7 +154,7 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage
}
private void LoadSenorInfo ( )
{
List < Base_sensor_info > SenorInfos = sqlSugarClient . Queryable < Base_sensor_info > ( ) . ToList( ) ;
List < Base_sensor_info > SenorInfos = sqlSugarClient . Queryable < Base_sensor_info > ( ) . Where( x = > x . Deleteflag = = 0 ) . ToList( ) ;
Senorinfo . Clear ( ) ;
foreach ( var item in Deviceinfo )
{
@ -205,27 +207,83 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage
status = await device . ConnectAsync ( device . ip , device . port , device . ConfigKey ) ;
if ( ! status )
{
Deviceinfo . FirstOrDefault ( x = > x . Connectstr = = $"{device.ip}:{device.port}" ) . IsOnline = "未连接" ;
Deviceinfo . FirstOrDefault ( x = > x . Deviceid = = device . deviceid ) . IsOnline = "未连接" ;
}
else
{
Deviceinfo . FirstOrDefault ( x = > x . Connectstr = = $"{device.ip}:{device.port}" ) . IsOnline = "已连接" ;
Deviceinfo . FirstOrDefault ( x = > x . Deviceid = = device . deviceid ) . IsOnline = "已连接" ;
}
}
else
{
//更新界面状态
Deviceinfo . FirstOrDefault ( x = > x . Connectstr = = $"{device.ip}:{device.port}" ) . IsOnline = "已连接" ;
Deviceinfo . FirstOrDefault ( x = > x . Deviceid = = device . deviceid ) . IsOnline = "已连接" ;
}
//存库,如果没有则插入如果存在则更新
lock ( string . Empty )
{
try
{
List < Base_device_state > Base_device_state = sqlSugarClient . Ado . Context . CopyNew ( ) . Queryable < Base_device_state > ( ) . Where ( x = > x . DeviceId = = int . Parse ( device . deviceid ) ) . ToList ( ) ;
if ( Base_device_state ! = null & & Base_device_state . Count > 0 )
{
//只更新
Base_device_state [ 0 ] . DeviceState = status ? 1 : 0 ;
Base_device_state [ 0 ] . DeviceTime = DateTime . Now ;
sqlSugarClient . Ado . Context . CopyNew ( ) . Updateable ( Base_device_state ) . UpdateColumns ( it = > new { it . DeviceState , it . DeviceTime } ) . ExecuteCommand ( ) ;
}
else
{
Base_device_state base_device_state = new Base_device_state ( )
{
DeviceId = int . Parse ( device . deviceid ) ,
DeviceState = status ? 1 : 0 ,
DeviceTime = DateTime . Now ,
} ;
var a = sqlSugarClient . Ado . Context . CopyNew ( ) . Insertable < Base_device_state > ( base_device_state ) . ExecuteCommand ( ) ;
}
}
catch ( Exception ex )
{
_logger . LogError ( $"更新RFID状态异常:{ex.Message}" , ex ) ;
}
}
}
catch ( Exception ex )
{
_logger . LogError ( $"更新RFID状态异常:{ex.Message}" , ex ) ;
}
}
private void CollectState ( )
{
Task . Run ( async ( ) = >
{
while ( true )
{
try
{
#region 汇集软件状态
Base_collect_state base_Collect_State = new Base_collect_state ( )
{
CollectId = appConfig . StationCode ,
CollectState = 1 ,
CollectTime = DateTime . Now ,
} ;
sqlSugarClient . Ado . Context . CopyNew ( ) . Insertable < Base_collect_state > ( base_Collect_State ) . ExecuteCommand ( ) ;
#endregion 汇集软件状态
}
catch ( Exception ex )
{
_logger . LogError ( $"更新汇集软件状态异常:{ex.Message}" ) ;
}
await Task . Delay ( 1000 * 60 * 10 ) ;
}
} ) ;
}
private void StartCheckStatus ( )
{
Task . Run ( async ( ) = >
@ -253,36 +311,11 @@ namespace SlnMesnac.WPF.ViewModel.IndexPage
await Task . Delay ( 10 ) ;
}
}
//foreach (var rfid in rfidList)
//{
// if (rfid.ConfigKey == "C031040701")
// {
// }
// bool status = rfid.GetOnlineStatus();
// if (!status)
// {
// status = await rfid.ConnectAsync(rfid.ip, rfid.port);
// if (!status)
// {
// Deviceinfo.FirstOrDefault(x => x.Connectstr == $"{rfid.ip}:{rfid.port}").IsOnline = "未连接";
// }
// else
// {
// Deviceinfo.FirstOrDefault(x => x.Connectstr == $"{rfid.ip}:{rfid.port}").IsOnline = "已连接";
// }
// }
// else
// {
// //更新界面状态
// Deviceinfo.FirstOrDefault(x => x.Connectstr == $"{rfid.ip}:{rfid.port}").IsOnline = "已连接";
// }
//}
#endregion RFID状态
}
catch ( Exception ex )
{
//_logger.Error($"监听设备状态异常:{ex.Message}") ;
_logger . LogError ( $"监听设备状态异常:{ex.Message}" ) ;
}
await Task . Delay ( 1000 * 30 ) ;
}