@ -7,7 +7,6 @@ using hass_workstation_service.Communication.NamedPipe;
using JKang.IpcServiceFramework.Client ;
using Microsoft.Extensions.DependencyInjection ;
using System ;
using System.Dynamic ;
using System.Linq ;
using System.Text.Json ;
using UserInterface.Util ;
@ -17,16 +16,25 @@ namespace UserInterface.Views
{
public class AddCommandDialog : Window
{
private readonly IIpcClient < IServiceContractInterfaces > client ;
public ComboBox comboBox { get ; set ; }
public ComboBox detectionModecomboBox { get ; set ; }
private readonly IIpcClient < IServiceContractInterfaces > _client ;
public ComboBox ComboBox { get ; set ; }
public ComboBox DetectionModecomboBox { get ; set ; }
public Guid CommandId { get ; }
public AddCommandDialog ( Guid commandId ) : this ( )
{
CommandId = commandId ;
GetCommandInfo ( CommandId ) ;
Title = "Edit command" ;
}
public AddCommandDialog ( )
{
this . InitializeComponent ( ) ;
InitializeComponent ( ) ;
DataContext = new AddCommandViewModel ( ) ;
this . comboBox = this . FindControl < ComboBox > ( "ComboBox" ) ;
this . comboBox . Items = Enum . GetValues ( typeof ( AvailableCommands ) ) . Cast < AvailableCommands > ( ) . OrderBy ( v = > v . ToString ( ) ) ;
this . comboBox . SelectedIndex = 0 ;
C omboBox = this . FindControl < ComboBox > ( "ComboBox" ) ;
C omboBox. Items = Enum . GetValues ( typeof ( AvailableCommands ) ) . Cast < AvailableCommands > ( ) . OrderBy ( v = > v . ToString ( ) ) ;
C omboBox. SelectedIndex = 0 ;
// register IPC clients
ServiceProvider serviceProvider = new ServiceCollection ( )
@ -38,22 +46,53 @@ namespace UserInterface.Views
. GetRequiredService < IIpcClientFactory < IServiceContractInterfaces > > ( ) ;
// create client
this . client = clientFactory . CreateClient ( "addCommand" ) ;
_client = clientFactory . CreateClient ( "addCommand" ) ;
Title = "Add sensor" ;
}
private void InitializeComponent ( )
{
AvaloniaXamlLoader . Load ( this ) ;
}
private async void GetCommandInfo ( Guid commandId )
{
var command = await _client . InvokeAsync ( x = > x . GetConfiguredCommand ( commandId ) ) ;
ComboBox . SelectedItem = command . Type ;
FillDefaultValues ( ) ;
ComboBox . IsEnabled = false ;
var item = ( AddCommandViewModel ) DataContext ;
item . SelectedType = command . Type ;
item . Name = command . Name ;
//item.UpdateInterval = command.UpdateInterval;
//item.WindowName =
//item.Query =
}
public async void Save ( object sender , RoutedEventArgs args )
{
var item = ( ( AddCommandViewModel ) this . DataContext ) ;
dynamic model = new { item . Name , item . Command , item . Key } ;
var item = ( AddCommandViewModel ) DataContext ;
dynamic model = new { item . Name , item . Command , item . Key } ;
string json = JsonSerializer . Serialize ( model ) ;
await this . client . InvokeAsync ( x = > x . AddCommand ( item . SelectedType , json ) ) ;
if ( CommandId = = Guid . Empty )
await _client . InvokeAsync ( x = > x . AddCommand ( item . SelectedType , json ) ) ;
else
await _client . InvokeAsync ( x = > x . UpdateCommandById ( CommandId , json ) ) ;
Close ( ) ;
}
public void ComboBoxClosed ( object sender , SelectionChangedEventArgs args )
{
var item = ( ( AddCommandViewModel ) this . DataContext ) ;
switch ( this . comboBox . SelectedItem )
FillDefaultValues ( ) ;
}
private void FillDefaultValues ( )
{
var item = ( AddCommandViewModel ) DataContext ;
switch ( ComboBox . SelectedItem )
{
case AvailableCommands . CustomCommand :
item . Description = "This command lets you execute any command you want. It will run in a Windows Command Prompt silently. " ;
@ -129,28 +168,26 @@ namespace UserInterface.Views
break ;
}
}
public void OpenInfo ( object sender , RoutedEventArgs args )
{
var item = ( ( AddCommandViewModel ) this . DataContext ) ;
var item = ( AddCommandViewModel ) DataContext ;
BrowserUtil . OpenBrowser ( item . MoreInfoLink ) ;
}
public void Test ( object sender , RoutedEventArgs args )
{
var item = ( ( AddCommandViewModel ) this . DataContext ) ;
var item = ( AddCommandViewModel ) DataContext ;
System . Diagnostics . Process process = new System . Diagnostics . Process ( ) ;
System . Diagnostics . ProcessStartInfo startInfo = new System . Diagnostics . ProcessStartInfo ( ) ;
startInfo . WindowStyle = System . Diagnostics . ProcessWindowStyle . Normal ;
startInfo . FileName = "cmd.exe" ;
startInfo . Arguments = $"/k {" echo You won ' t see this window normally . & & " + item.Command}" ;
var process = new System . Diagnostics . Process ( ) ;
var startInfo = new System . Diagnostics . ProcessStartInfo
{
WindowStyle = System . Diagnostics . ProcessWindowStyle . Normal ,
FileName = "cmd.exe" ,
Arguments = $"/k {" echo You won ' t see this window normally . & & " + item.Command}"
} ;
process . StartInfo = startInfo ;
process . Start ( ) ;
}
private void InitializeComponent ( )
{
AvaloniaXamlLoader . Load ( this ) ;
}
}
}