增加时间同步功能,并编写相应的前端页面展示功能

This commit is contained in:
MoYue 2024-06-13 18:17:25 +08:00
parent ac2f40337c
commit f348af3c82
6 changed files with 215 additions and 22 deletions

View File

@ -1,6 +1,7 @@
using InSituLaboratory.Entities; using InSituLaboratory.Entities;
using InSituLaboratory.Entities.ExperimentalStationEntities; using InSituLaboratory.Entities.ExperimentalStationEntities;
using InSituLaboratory.Entities.Sensor; using InSituLaboratory.Entities.Sensor;
using InSituLaboratory.Entities.SqlSugar;
using InSituLaboratory.Entities.SysData; using InSituLaboratory.Entities.SysData;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -196,6 +197,13 @@ namespace InSituLaboratory.IService.Sensor
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
IEnumerable<SysSensorModel> GeSysSensor(); IEnumerable<SysSensorModel> GeSysSensor();
/// <summary>
/// 获取当前运行时序状态
/// </summary>
/// <returns></returns>
IEnumerable<CurrentSequentiual> GetCurrentSequentiual();
#endregion #endregion
} }

View File

@ -1,6 +1,7 @@
using InSituLaboratory.Entities; using InSituLaboratory.Entities;
using InSituLaboratory.Entities.ExperimentalStationEntities; using InSituLaboratory.Entities.ExperimentalStationEntities;
using InSituLaboratory.Entities.Sensor; using InSituLaboratory.Entities.Sensor;
using InSituLaboratory.Entities.SqlSugar;
using InSituLaboratory.Entities.SysData; using InSituLaboratory.Entities.SysData;
using InSituLaboratory.IService; using InSituLaboratory.IService;
using InSituLaboratory.IService.Sensor; using InSituLaboratory.IService.Sensor;
@ -286,6 +287,15 @@ namespace InSituLaboratory.Service.Sensor
{ {
return this.Query<SysSensorModel>(m => true).OrderByDescending(n => n.CreateTime).AsNoTracking(); return this.Query<SysSensorModel>(m => true).OrderByDescending(n => n.CreateTime).AsNoTracking();
} }
/// <summary>
/// 获取当前运行时序状态
/// </summary>
/// <returns></returns>
public IEnumerable<CurrentSequentiual> GetCurrentSequentiual()
{
return this.Query<CurrentSequentiual>(m => true).OrderByDescending(n => n.CreateTime).AsNoTracking();
}
#endregion #endregion
} }

View File

@ -42,7 +42,7 @@ namespace InSituLaboratory.Common
string ResponID = ""; string ResponID = "";
//通用应答消息ID //通用应答消息ID
string MessageID = "1000"; string MessageID = "0000";
//crc //crc
static byte nr_crc = 0; static byte nr_crc = 0;
@ -209,10 +209,10 @@ namespace InSituLaboratory.Common
currentSequentiual.DeviceNumber += "气相色谱仪 "; currentSequentiual.DeviceNumber += "气相色谱仪 ";
break; break;
case 0x08: case 0x08:
currentSequentiual.DeviceNumber += "甲烷传感器 "; currentSequentiual.DeviceNumber += "CH4 ";
break; break;
case 0x09: case 0x09:
currentSequentiual.DeviceNumber += "二氧化碳同位素分析仪 "; currentSequentiual.DeviceNumber += "CO2 ";
break; break;
case 0x0a: case 0x0a:
currentSequentiual.DeviceNumber += "备用网口设备 "; currentSequentiual.DeviceNumber += "备用网口设备 ";
@ -221,7 +221,7 @@ namespace InSituLaboratory.Common
currentSequentiual.DeviceNumber += "ICL "; currentSequentiual.DeviceNumber += "ICL ";
break; break;
case 0x0c: case 0x0c:
currentSequentiual.DeviceNumber += "显微拉曼分析仪 "; currentSequentiual.DeviceNumber += "显微拉曼 ";
break; break;
case 0x0d: case 0x0d:
currentSequentiual.DeviceNumber += "质谱仪 "; currentSequentiual.DeviceNumber += "质谱仪 ";
@ -250,7 +250,7 @@ namespace InSituLaboratory.Common
//通用应答 消息ID0x1000 //通用应答 消息ID0x0000
if (MessageID == dataNew[1].ToString("X2") + dataNew[2].ToString("X2")) if (MessageID == dataNew[1].ToString("X2") + dataNew[2].ToString("X2"))
{ {
dataParsingModel.CreateTime = System.DateTime.Now; dataParsingModel.CreateTime = System.DateTime.Now;

View File

@ -3,6 +3,7 @@ using InSituLaboratory.Common;
using InSituLaboratory.Controls; using InSituLaboratory.Controls;
using InSituLaboratory.Entities; using InSituLaboratory.Entities;
using InSituLaboratory.Entities.Sensor; using InSituLaboratory.Entities.Sensor;
using InSituLaboratory.Entities.SqlSugar;
using InSituLaboratory.IService; using InSituLaboratory.IService;
using InSituLaboratory.IService.Sensor; using InSituLaboratory.IService.Sensor;
using InSituLaboratory.Models.Sendsor; using InSituLaboratory.Models.Sendsor;
@ -20,6 +21,7 @@ using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Forms; using System.Windows.Forms;
using System.Windows.Markup; using System.Windows.Markup;
using System.Windows.Media.Animation;
using static System.Windows.Forms.VisualStyles.VisualStyleElement; using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace InSituLaboratory.ViewModels.Pages namespace InSituLaboratory.ViewModels.Pages
@ -30,12 +32,13 @@ namespace InSituLaboratory.ViewModels.Pages
public class RelayViewModel : ViewModelBase public class RelayViewModel : ViewModelBase
{ {
#region Entity #region Entity
public ClientModel clientModel { get; set; } = new ClientModel(); public ClientModel clientModel { get; set; } = new ClientModel();//Socket 通信类
public SysSensorModel sysSensorModel { get; set; } = new SysSensorModel(); public SysSensorModel sysSensorModel { get; set; } = new SysSensorModel(); // 传感器表
public SysSensorModels sysSensorModels { get; set; } = new SysSensorModels(); public CurrentSequentiual CurrentSequentiual { get; set; } = new CurrentSequentiual(); //当前运行时序状态表
public LogModel logModel { get; set; } public SysSensorModels sysSensorModels { get; set; } = new SysSensorModels(); //传感器表
public LogModel logModel { get; set; } //日志记录表
public ObservableCollection<LogModel> loglist { get; set; } = new ObservableCollection<LogModel>(); public ObservableCollection<LogModel> loglist { get; set; } = new ObservableCollection<LogModel>();
public PaginationModel PaginationModel { get; set; } = new PaginationModel(); public PaginationModel PaginationModel { get; set; } = new PaginationModel(); //分页组件
public static SocketInfo SocketInfo { get; set; } = new SocketInfo() public static SocketInfo SocketInfo { get; set; } = new SocketInfo()
{ {
IP = tools.GetAppSetting("IP"), IP = tools.GetAppSetting("IP"),
@ -73,10 +76,17 @@ namespace InSituLaboratory.ViewModels.Pages
public override void Refresh() public override void Refresh()
{ {
loglist.Clear(); loglist.Clear();
//获取日志记录数据
var Loglist = _logService.GetLogData(SearchKey, PaginationModel.PageSize, PaginationModel.PageIndex, out int totalCount); var Loglist = _logService.GetLogData(SearchKey, PaginationModel.PageSize, PaginationModel.PageIndex, out int totalCount);
//获取传感器电源开关数据
var SysSensorlist = _sensorService.GeSysSensor(); var SysSensorlist = _sensorService.GeSysSensor();
///列表清单数据 //获取当前运行时序状态表
var CurrentSequentiualList = _sensorService.GetCurrentSequentiual();
//日志列表清单数据
int index = 0; int index = 0;
foreach (var item in Loglist) foreach (var item in Loglist)
{ {
@ -89,7 +99,7 @@ namespace InSituLaboratory.ViewModels.Pages
}); });
} }
///状态监控 //传感器电源开关数据
if (SysSensorlist.Count() != 0) if (SysSensorlist.Count() != 0)
{ {
var data = _sensorService.GeSysSensor().FirstOrDefault(); var data = _sensorService.GeSysSensor().FirstOrDefault();
@ -115,6 +125,16 @@ namespace InSituLaboratory.ViewModels.Pages
sysSensorModel.ElectricSwitch2 = sysSensorModels.ElectricSwitch2 = data.ElectricSwitch2; sysSensorModel.ElectricSwitch2 = sysSensorModels.ElectricSwitch2 = data.ElectricSwitch2;
} }
//当前运行时序状态数据
if (CurrentSequentiualList.Count() != 0)
{
var data = _sensorService.GetCurrentSequentiual().FirstOrDefault();
CurrentSequentiual.Number = data.Number;
CurrentSequentiual.DeviceNumber = data.DeviceNumber;
CurrentSequentiual.Status = data.Status;
}
// 刷新分页组件的页码 // 刷新分页组件的页码
PaginationModel.FillPageNumbers(totalCount); PaginationModel.FillPageNumbers(totalCount);
} }
@ -931,7 +951,7 @@ namespace InSituLaboratory.ViewModels.Pages
opendata += data[i]; opendata += data[i];
opendata = opendata + "、"; opendata = opendata + "、";
} }
if (System.Windows.Forms.MessageBox.Show("当前处于打开状态的设备有" + opendata +"确认需要一键还原吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) if (System.Windows.Forms.MessageBox.Show("当前处于打开状态的设备有" + opendata + "确认需要一键还原吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{ {
if (System.Windows.Forms.MessageBox.Show("再次确定需要一键还原吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) if (System.Windows.Forms.MessageBox.Show("再次确定需要一键还原吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
{ {
@ -1097,5 +1117,112 @@ namespace InSituLaboratory.ViewModels.Pages
} }
} }
/// <summary>
/// 时间同步
/// </summary>
public override void DoSynchronization()
{
ulong senddate;
ulong year;
ulong month;
ulong day;
ulong hour;
ulong min;
ulong sec;
ulong minsec;
//定义消息体数组
byte[] byteaq = new byte[16];
DateTime dateTime = DateTime.Now;
year = (ulong)dateTime.Year * 10000000000000UL;
month = (ulong)dateTime.Month * 100000000000;
day = (ulong)dateTime.Day * 1000000000L;
hour = (ulong)dateTime.Hour * 10000000;
min = (ulong)dateTime.Minute * 100000;
sec = (ulong)dateTime.Second * 1000;
minsec = (ulong)dateTime.Millisecond;
senddate = year + month + day + hour + min + sec + minsec;
byteaq[0] = 0x90;
byteaq[1] = 0x02;
byteaq[2] = 0x10;
byteaq[3] = 0x01;
byteaq[4] = 0x00;
byteaq[5] = 0x00;
byteaq[6] = 0x00;
byteaq[7] = 0x00;
byteaq[8] = (byte)((senddate >> 56) & 0xff);
byteaq[9] = (byte)((senddate >> 48) & 0xff);
byteaq[10] = (byte)((senddate >> 40) & 0xff);
byteaq[11] = (byte)((senddate >> 32) & 0xff);
byteaq[12] = (byte)((senddate >> 24) & 0xff);
byteaq[13] = (byte)((senddate >> 16) & 0xff);
byteaq[14] = (byte)((senddate >> 8) & 0xff);
byteaq[15] = (byte)(senddate & 0xff);
//标志位
byte head = 0x7f;
//crc
byte nr_crc = 0;
nr_crc = tools.CRC(byteaq.ToArray(), 0, byteaq.Length);
///转义
int j = 0;
List<byte> tBuffer = byteaq.ToList();
tBuffer.Add(nr_crc);
int length = tBuffer.ToArray().Length;
byte[] newAnswer = tBuffer.ToArray();
for (int i = 0; i < length; i++)
{
if (newAnswer[i] == 0x7e || newAnswer[i] == 0x7f)
{
j++;
}
}
byte[] newSendBuffer = new byte[length + j];
for (int i = 0; i < length; i++)
{
newSendBuffer[i] = newAnswer[i];
}
for (int i = 0; i < length + j; i++)
{
if (newSendBuffer[i] == 0x7e)
{
for (int k = length + j - 1; k > i + 1; k--)
{
newSendBuffer[k] = newSendBuffer[k - 1];
}
newSendBuffer[i + 1] = 0x01;
}
if (newSendBuffer[i] == 0x7f)
{
newSendBuffer[i] = 0x7e;
for (int k = length + j - 1; k > i + 1; k--)
{
newSendBuffer[k] = newSendBuffer[k - 1];
}
newSendBuffer[i + 1] = 0x02;
}
}
List<byte> buffer = new List<byte>();
buffer.Add(head);
for (int i = 0; i < newSendBuffer.Length; i++)
{
buffer.Add(newSendBuffer[i]);
}
buffer.Add(head);
string list = tools.byteToHexStr(buffer.ToArray());
byte[] bytea = tools.ConvertHexStringToBytes(list);
clientModel.SendData(bytea);
logModel = new LogModel();
logModel.CreateTime = DateTime.Now;
logModel.Remark = "时间同步指令已下发";
_logService.Insert<LogModel>(logModel);
this.Refresh();
}
} }
} }

View File

@ -62,6 +62,7 @@ namespace InSituLaboratory.ViewModels.Pages
public DelegateCommand<object> ConnectionCommand { get; set; } public DelegateCommand<object> ConnectionCommand { get; set; }
public DelegateCommand ReductionCommand { get; set; } //一键还原 public DelegateCommand ReductionCommand { get; set; } //一键还原
public DelegateCommand StartCommand { get; set; }//一键启动 public DelegateCommand StartCommand { get; set; }//一键启动
public DelegateCommand SynchronizationCommand { get; set; }//时间同步
#region 1-5 / #region 1-5 /
@ -107,6 +108,7 @@ namespace InSituLaboratory.ViewModels.Pages
ConnectionCommand = new DelegateCommand<object>(DoConnection); ConnectionCommand = new DelegateCommand<object>(DoConnection);
ReductionCommand = new DelegateCommand(DoReduction); ReductionCommand = new DelegateCommand(DoReduction);
StartCommand = new DelegateCommand(DoStart); StartCommand = new DelegateCommand(DoStart);
SynchronizationCommand = new DelegateCommand(DoSynchronization);
#region 1-5 / #region 1-5 /
@ -155,6 +157,7 @@ namespace InSituLaboratory.ViewModels.Pages
public virtual void DoConnection(object model) { } public virtual void DoConnection(object model) { }
public virtual void DoReduction() { } public virtual void DoReduction() { }
public virtual void DoStart() { } public virtual void DoStart() { }
public virtual void DoSynchronization() { }
private string PageName { get; set; } private string PageName { get; set; }

View File

@ -87,12 +87,13 @@
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="120"/>
<RowDefinition Height="120"/> <RowDefinition Height="120"/>
<RowDefinition/> <RowDefinition/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<Grid> <Grid>
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="2.5*"/> <ColumnDefinition Width="2.0*"/>
<ColumnDefinition/> <ColumnDefinition/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<GroupBox Header="通信连接" Margin="15"> <GroupBox Header="通信连接" Margin="15">
@ -108,7 +109,7 @@
<ColumnDefinition/> <ColumnDefinition/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<TextBlock Text="IP地址:" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="17"/> <TextBlock Text="IP地址:" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="17"/>
<TextBox Grid.Column="1" Width="150" VerticalAlignment="Center" FontSize="16" Text="{Binding SocketInfo.IP }"/> <TextBox Grid.Column="1" Width="150" VerticalAlignment="Center" FontSize="16" Text="{Binding SocketInfo.IP }" Foreground="#CD9B9B"/>
</Grid> </Grid>
<Grid Grid.Column="1"> <Grid Grid.Column="1">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
@ -116,7 +117,7 @@
<ColumnDefinition/> <ColumnDefinition/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<TextBlock Text="端口号:" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="17"/> <TextBlock Text="端口号:" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="17"/>
<TextBox Grid.Column="1" Width="150" VerticalAlignment="Center" FontSize="16" Text="{Binding SocketInfo.Port}" Margin="10,0" /> <TextBox Grid.Column="1" Width="150" VerticalAlignment="Center" FontSize="16" Text="{Binding SocketInfo.Port}" Margin="10,0" Foreground="#CD9B9B" />
</Grid> </Grid>
<Button Grid.Column="2" Content="{Binding SocketInfo.ConnectionMessage }" Style="{StaticResource NormalButtonStyle}" Width="120" Height="30" Command="{Binding ConnectionCommand}"/> <Button Grid.Column="2" Content="{Binding SocketInfo.ConnectionMessage }" Style="{StaticResource NormalButtonStyle}" Width="120" Height="30" Command="{Binding ConnectionCommand}"/>
</Grid> </Grid>
@ -126,13 +127,57 @@
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition/> <ColumnDefinition/>
<ColumnDefinition/> <ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Button Content="一键还原" Style="{StaticResource NormalButtonStyle}" Width="100" Height="30" Command="{Binding ReductionCommand}"/> <Button Content="一键还原" Style="{StaticResource NormalButtonStyle}" Width="80" Height="30" Command="{Binding ReductionCommand}"/>
<Button Grid.Column="1" Content="一键启动" Style="{StaticResource NormalButtonStyle}" Width="100" Height="30" Command="{Binding StartCommand}"/> <Button Grid.Column="1" Content="一键启动" Style="{StaticResource NormalButtonStyle}" Width="80" Height="30" Command="{Binding StartCommand}"/>
<Button Grid.Column="2" Content="时间同步" Style="{StaticResource NormalButtonStyle}" Width="80" Height="30" Command="{Binding SynchronizationCommand}"/>
</Grid> </Grid>
</GroupBox> </GroupBox>
</Grid> </Grid>
<GroupBox Header="电源开关控制" Margin="15,1,15,5" Grid.Row="1">
<GroupBox Header="当前运行时序" Margin="15,1,15,10" Grid.Row="1">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="35"/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="时序序号:" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="17"/>
<TextBlock Grid.Column="1" Text="{Binding CurrentSequentiual.Number}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="17" Foreground="#CD9B9B"/>
</Grid>
<Grid Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="时序运行状态:" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="17"/>
<TextBlock Grid.Column="1" Text="{Binding CurrentSequentiual.Status}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="17" Foreground="#CD9B9B" />
</Grid>
</Grid>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Text="运行的设备编号:" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="17" Margin="47,0,0,0"/>
<TextBlock Grid.Column="1" Text="{Binding CurrentSequentiual.DeviceNumber}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="17" Foreground="#CD9B9B" Margin="-246,0,0,0"/>
</Grid>
</Grid>
</GroupBox>
<GroupBox Header="电源开关控制" Margin="15,1,15,5" Grid.Row="2">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="35"/> <RowDefinition Height="35"/>
@ -477,7 +522,7 @@
</Button.CommandParameter> </Button.CommandParameter>
</Button> </Button>
</Grid> </Grid>
<Grid Grid.Column="1"> <Grid Grid.Column="1">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition/> <ColumnDefinition/>
@ -508,7 +553,7 @@
</Button.CommandParameter> </Button.CommandParameter>
</Button> </Button>
</Grid> </Grid>
<Grid Grid.Column="2"> <Grid Grid.Column="2">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
@ -679,7 +724,7 @@
</Button.CommandParameter> </Button.CommandParameter>
</Button> </Button>
</Grid> </Grid>
<Grid Grid.Column="1"> <Grid Grid.Column="1">
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition/> <ColumnDefinition/>