Skip to content

Commit

Permalink
添加 Sqlite 持久化存储
Browse files Browse the repository at this point in the history
  • Loading branch information
chao committed Sep 12, 2022
1 parent ce137db commit 384ed25
Show file tree
Hide file tree
Showing 17 changed files with 438 additions and 113 deletions.
14 changes: 11 additions & 3 deletions SuperCom/Config/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ namespace SuperCom.Config
{
public static class ConfigManager
{
public static Main Main = Main.CreateInstance();
public const string SQLITE_DATA_PATH = "user_data.sqlite";

public static Main Main { get; set; }


static ConfigManager()
{
Main = Main.CreateInstance();
}

public static void InitConfig()
{
System.Reflection.FieldInfo[] fieldInfos = typeof(ConfigManager).GetFields();
foreach (var item in fieldInfos)
System.Reflection.PropertyInfo[] propertyInfos = typeof(ConfigManager).GetProperties();
foreach (var item in propertyInfos)
{
AbstractConfig config = item.GetValue(null) as AbstractConfig;
if (config == null)
Expand Down
3 changes: 2 additions & 1 deletion SuperCom/Config/WindowConfig/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace SuperCom.Config.WindowConfig
{
public class Main : AbstractConfig
{
private Main() : base("user_data.sqlite", $"WindowConfig.Main")
private Main() : base(ConfigManager.SQLITE_DATA_PATH, $"WindowConfig.Main")
{
Width = SystemParameters.WorkArea.Width * 0.8;
Height = SystemParameters.WorkArea.Height * 0.8;
Expand All @@ -32,6 +32,7 @@ public static Main CreateInstance()

public bool FirstRun { get; set; }
public string SendHistory { get; set; }
public string OpeningPorts { get; set; }

}
}
4 changes: 2 additions & 2 deletions SuperCom/CustomWindows/About.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@
FontSize="14"
Foreground="{DynamicResource Window.Foreground}"
Style="{StaticResource ReadOnlyTextBox}"
Text="版本:1.1" />
Text="版本:1.2" />
<TextBox
Margin="10"
FontSize="11"
Foreground="{DynamicResource Window.Sub.Foreground}"
Style="{StaticResource ReadOnlyTextBox}"
Text="发行日期:2022-09-09" />
Text="发行日期:2022-09-13" />

<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<StackPanel Margin="20,0" Orientation="Horizontal">
Expand Down
59 changes: 59 additions & 0 deletions SuperCom/Entity/ComSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using SuperUtils.Framework.ORM.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SuperCom.Entity
{

[Table(tableName: "com_settings")]
public class ComSettings
{
[TableId(IdType.AUTO)]
public int Id { get; set; }
public string PortName { get; set; }
public bool Connected { get; set; }
public bool AddTimeStamp { get; set; }
public bool AddNewLineWhenWrite { get; set; }
public string PortSetting { get; set; } // json 格式
public string WriteData { get; set; }

public string CreateDate { get; set; }
public string UpdateDate { get; set; }


// 必须要有无参构造器
public ComSettings()
{

}

public override bool Equals(object obj)
{
if (obj is ComSettings com)
{
if (PortName == null && com.PortName == null) return true;
else if (PortName != null)
return PortName.Equals(com.PortName);
}
return false;
}

public override int GetHashCode()
{
return this.PortName.GetHashCode();
}

// SQLite 中建表语句
public static class SqliteTable
{
public static Dictionary<string, string> Table = new Dictionary<string, string>()
{
{"com_settings","create table if not exists com_settings( Id INTEGER PRIMARY KEY autoincrement, PortName VARCHAR(50), Connected INT DEFAULT 0, AddTimeStamp INT DEFAULT 0, AddNewLineWhenWrite INT DEFAULT 0, PortSetting VARCHAR(1000), WriteData VARCHAR(5000), CreateDate VARCHAR(30) DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%S', 'NOW', 'localtime')), UpdateDate VARCHAR(30) DEFAULT(STRFTIME('%Y-%m-%d %H:%M:%S', 'NOW', 'localtime')), unique(PortName) );" }
};

}
}
}
37 changes: 37 additions & 0 deletions SuperCom/Entity/CustomSerialPort.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using DynamicData.Annotations;
using SuperControls.Style;
using SuperUtils.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -38,13 +40,48 @@ public void RefreshSetting()
this.Encoding = GetEncoding();
this.StopBits = GetStopBits();
this.Parity = GetParity();

// 保存
SettingJson = PortSettingToJson();
}
catch (Exception ex)
{
MessageCard.Error(ex.Message);
}
}

public string SettingJson { get; set; }

private string PortSettingToJson()
{
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("BaudRate", this.BaudRate);
dic.Add("DataBits", this.DataBits);
dic.Add("Encoding", this.Encoding.HeaderName);
dic.Add("StopBits", this.StopBitsString);
dic.Add("Parity", this.ParityString);
return JsonUtils.TrySerializeObject(dic);
}

public void SetPortSettingByJson(string json)
{
Dictionary<string, object> dict = JsonUtils.TryDeserializeObject<Dictionary<string, object>>(json);
if (dict != null)
{
int baudRate = PortSetting.DEFAULT_BAUDRATE;
int.TryParse(dict["BaudRate"].ToString(), out baudRate);
this.BaudRate = baudRate;

int dataBits = PortSetting.DEFAULT_DATABITS;
int.TryParse(dict["DataBits"].ToString(), out dataBits);
this.DataBits = dataBits;

this.PortEncoding = dict["Encoding"].ToString();
this.ParityString = dict["Parity"].ToString();
this.StopBitsString = dict["StopBits"].ToString();
}
}

public Encoding GetEncoding()
{
try
Expand Down
9 changes: 7 additions & 2 deletions SuperCom/Entity/PortTabItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ public string WriteData
private StringBuilder Builder { get; set; }
public TextBox TextBox { get; set; }

public bool AddTimeStamp { get; set; }
private bool _AddTimeStamp = true;
public bool AddTimeStamp
{
get { return _AddTimeStamp; }
set { _AddTimeStamp = value; OnPropertyChanged(); }
}



Expand Down Expand Up @@ -148,7 +153,7 @@ public PortTabItem(string name, bool connected)
Connected = connected;
Setting = new PortSetting();
Builder = new StringBuilder();
AddTimeStamp = true;
//AddTimeStamp = true;
}

public event PropertyChangedEventHandler PropertyChanged;
Expand Down
21 changes: 14 additions & 7 deletions SuperCom/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -1300,12 +1300,12 @@
</Button>
<CheckBox
Margin="10,0"
Click="AddTimeStamp"
Content="加时间戳"
FontSize="15"
IsChecked="True"
IsChecked="{Binding AddTimeStamp, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource NormalCheckBox}" />
<Button Click="SaveToNewFile" Content="存到新文件" />

</StackPanel>
</Border>
</Grid>
Expand Down Expand Up @@ -1351,7 +1351,7 @@
Width="100"
SelectedIndex="0"
Style="{StaticResource BorderComboBox}"
Text="{Binding SerialPort.ParityString, Mode=TwoWay}">
Text="{Binding SerialPort.ParityString, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<ComboBoxItem>None</ComboBoxItem>
<ComboBoxItem>Odd</ComboBoxItem>
<ComboBoxItem>Even</ComboBoxItem>
Expand Down Expand Up @@ -1400,7 +1400,6 @@
</Grid.RowDefinitions>
<Border
Grid.Row="0"
Margin="5,0"
Padding="5,0"
Background="{DynamicResource Window.Title.Background}"
CornerRadius="5">
Expand All @@ -1427,7 +1426,14 @@
</ComboBox>


<Button Click="ShowAdvancedOptions" Content="高级设置" />
<Button
Margin="5,0"
Click="ShowAdvancedOptions"
Content="高级设置" />
<Button
Margin="5,0"
Click="OpenSendPanel"
Content="发送队列" />


<!--<TextBlock Style="{StaticResource BaseTextBlock}" Text="Flow Controls" />
Expand Down Expand Up @@ -1461,7 +1467,7 @@
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
<Border
Margin="5"
Margin="0,5"
Padding="10"
Background="{DynamicResource Window.Side.Background}"
CornerRadius="5">
Expand All @@ -1476,7 +1482,7 @@
Foreground="{DynamicResource Window.Foreground}"
PreviewKeyDown="SendTextBox_PreviewKeyDown"
ScrollViewer.VerticalScrollBarVisibility="Auto"
Text="{Binding WriteData, Mode=TwoWay}"
Text="{Binding WriteData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
TextChanged="SendTextBox_TextChanged" />
</Border>

Expand Down Expand Up @@ -1590,6 +1596,7 @@
<Path Data="M896 160C896 177.664 881.664 192 864 192l-768 0C78.336 192 64 177.664 64 160l0 0C64 142.336 78.336 128 96 128l768 0C881.664 128 896 142.336 896 160L896 160zM896 352C896 334.336 881.664 320 864 320l-768 0C78.336 320 64 334.336 64 352l0 0C64 369.664 78.336 384 96 384l768 0C881.664 384 896 369.664 896 352L896 352zM384 736C384 718.336 369.664 704 352 704l-256 0C78.336 704 64 718.336 64 736l0 0C64 753.664 78.336 768 96 768l256 0C369.664 768 384 753.664 384 736L384 736zM735.168 512 732.8 512 599.232 512 96 512C78.336 512 64 526.336 64 544S78.336 576 96 576l503.232 0 133.568 0 2.368 0c52.928 0 96 43.072 96 96s-43.072 96-96 96L568.96 768l0-2.112L568.576 765.888l64.128-64.128c12.8-12.8 12.8-33.664 0-46.528-12.864-12.864-33.6-12.864-46.528 0l-120.192 120.192c-6.208 6.208-9.6 14.592-9.6 23.296 0 8.704 3.456 17.024 9.6 23.232l120.192 120.128c12.864 12.736 33.6 12.736 46.528 0 12.864-12.864 12.864-33.6 0-46.592L569.024 832l166.208 0c88.256 0 160-71.744 160-160S823.424 512 735.168 512z" Fill="{Binding Foreground, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" />
</Viewbox>
</ToggleButton>

<Button
Margin="5,5,7,5"
Padding="10"
Expand Down
Loading

0 comments on commit 384ed25

Please sign in to comment.