Skip to content

Commit

Permalink
feat: log results and start on launch.
Browse files Browse the repository at this point in the history
  • Loading branch information
am009 committed Nov 21, 2024
1 parent fa97e8f commit cd734dd
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/TMSpeech.Core/ConfigTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ public static class GeneralConfigTypes
public const string LaunchOnStartup = "general.LaunchOnStartup";
public const string StartOnLaunch = "general.StartOnLaunch";
public const string AutoUpdate = "general.AutoUpdate";
public const string ResultLogPath = "general.ResultLogPath";


private static Dictionary<string, object> _defaultConfig => new()
{
{ Language, "zh-cn" },
{ LaunchOnStartup, false },
{ StartOnLaunch, false },
{ AutoUpdate, true }
{ StartOnLaunch, true },
{ AutoUpdate, true },
{ ResultLogPath, Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "TMSpeechLogs") }
};

public static Dictionary<string, object> DefaultConfig => _defaultConfig;
Expand Down
18 changes: 18 additions & 0 deletions src/TMSpeech.Core/JobManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TMSpeech.Core.Plugins;
Expand Down Expand Up @@ -65,6 +66,7 @@ internal JobManagerImpl()
private IRecognizer? _recognizer;
private HashSet<string> _sensitiveWords;
private bool _disableInThisSentence = false;
private string logFile;

private void InitAudioSource()
{
Expand Down Expand Up @@ -111,6 +113,12 @@ private void InitRecognizer()

private void OnRecognizerOnSentenceDone(object? sender, SpeechEventArgs args)
{
// Save the sentense to log
if (logFile != null && logFile.Length > 0)
{
File.AppendAllText(logFile, string.Format("{0:T}: {1}\n", DateTime.Now, args.Text.Text));
}

_disableInThisSentence = false;
OnSentenceDone(args);
}
Expand Down Expand Up @@ -167,6 +175,16 @@ private void StartRecognize()
return;
}

var logPath = ConfigManagerFactory.Instance.Get<string>(GeneralConfigTypes.ResultLogPath).Trim();
if (logPath.Length > 0)
{
Directory.CreateDirectory(logPath);
logFile = Path.Combine(logPath, string.Format("{0:yy-MM-dd-HH-mm-ss}.txt", DateTime.Now));
} else
{
logFile = "";
}

if (Status == JobStatus.Stopped) RunningSeconds = 0;

Status = JobStatus.Running;
Expand Down
10 changes: 10 additions & 0 deletions src/TMSpeech.GUI/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Linq;
using System.Reactive.Concurrency;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using ReactiveUI;
using TMSpeech.Core;
using TMSpeech.GUI.ViewModels;
using TMSpeech.GUI.Views;
Expand Down Expand Up @@ -49,6 +53,12 @@ public override void OnFrameworkInitializationCompleted()
if (!Design.IsDesignMode)
{
Core.Plugins.PluginManagerFactory.GetInstance().LoadPlugins();

// Run recognizer if config is set.
if (ConfigManagerFactory.Instance.Get<bool>(GeneralConfigTypes.StartOnLaunch))
{
Dispatcher.UIThread.Post(() => { _mainWindow.ViewModel.PlayCommand.Execute(); });
}
}
}
}
4 changes: 4 additions & 0 deletions src/TMSpeech.GUI/ViewModels/ConfigViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ public class GeneralSectionConfigViewModel : SectionConfigViewModelBase
//[ConfigJsonValue]
//public string UserDir { get; set; } = "D:\\TMSpeech";

[Reactive]
[ConfigJsonValue]
public string ResultLogPath { get; set; }

[Reactive]
[ConfigJsonValue]
public bool LaunchOnStartup { get; set; }
Expand Down
19 changes: 11 additions & 8 deletions src/TMSpeech.GUI/Views/ConfigWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@
</Style>
</TabControl.Styles>

<TabItem IsVisible="False">
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock>通用</TextBlock>
</StackPanel>
</TabItem.Header>
<controls:AutoGrid RowCount="100" ColumnDefinitions="100,*" DataContext="{Binding GeneralSectionConfig}">
<Label>语言</Label>
<ComboBox SelectedValueBinding="{Binding Key}" SelectedValue="{Binding Language,Mode=TwoWay}"
<controls:AutoGrid RowCount="100" ColumnDefinitions="115,*" DataContext="{Binding GeneralSectionConfig}">
<Label IsVisible="False">语言</Label>
<ComboBox IsVisible="False" SelectedValueBinding="{Binding Key}" SelectedValue="{Binding Language,Mode=TwoWay}"
Name="comboLanguage" ItemsSource="{Binding LanguagesAvailable}">
<ComboBox.ItemTemplate>
<DataTemplate>
Expand All @@ -58,13 +58,16 @@
<Label>用户数据目录</Label>
<controls:FilePicker Text="{Binding UserDir,Mode=TwoWay}" ExtendedOptions="True" Type="Folder" />
-->
<Label>开机启动</Label>
<CheckBox IsChecked="{Binding LaunchOnStartup,Mode=TwoWay}" />
<Label IsVisible="False">开机启动</Label>
<CheckBox IsVisible="False" IsChecked="{Binding LaunchOnStartup,Mode=TwoWay}" />
<Label>启动开始识别</Label>
<CheckBox IsChecked="{Binding StartOnLaunch,Mode=TwoWay}" />
<Label>自动更新</Label>
<CheckBox IsChecked="{Binding AutoUpdate,Mode=TwoWay}" />
<Label>识别日志文件夹</Label>
<TextBox Text="{Binding ResultLogPath,Mode=TwoWay}"></TextBox>
<Label IsVisible="False">自动更新</Label>
<CheckBox IsVisible="False" IsChecked="{Binding AutoUpdate,Mode=TwoWay}" />
</controls:AutoGrid>


</TabItem>
<TabItem>
Expand Down

0 comments on commit cd734dd

Please sign in to comment.