2
0

將寫死改成活讀

This commit is contained in:
DangHome
2023-07-03 01:51:09 +08:00
parent a48d7b8d56
commit a8fd8f3f46
27 changed files with 703 additions and 546 deletions

27
CMMModel/CMMModel.csproj Normal file
View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
<AssemblyName>CMM.Model</AssemblyName>
<RootNamespace>CMM.Model</RootNamespace>
<Product>ControlMyMonitorManagement</Product>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Company>Dang</Company>
<Copyright>Copyright © DangWang $([System.DateTime]::Now.ToString(yyyy))</Copyright>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<Major>1</Major>
<Minor>0</Minor>
<ProjectStartedDate>$([System.DateTime]::op_Subtraction($([System.DateTime]::get_Now().get_Date()),$([System.DateTime]::new(2017,9,17))).get_TotalDays())</ProjectStartedDate>
<DaysSinceProjectStarted>$([System.DateTime]::Now.ToString(Hmm))</DaysSinceProjectStarted>
<DateTimeSuffix>$([System.DateTime]::Now.ToString(yyyyMMdd))</DateTimeSuffix>
<VersionSuffix>$(Major).$(Minor).$(ProjectStartedDate).$(DaysSinceProjectStarted)</VersionSuffix>
<AssemblyVersion Condition=" '$(DateTimeSuffix)' == '' ">0.0.0.1</AssemblyVersion>
<AssemblyVersion Condition=" '$(DateTimeSuffix)' != '' ">$(VersionSuffix)</AssemblyVersion>
<Version Condition=" '$(DateTimeSuffix)' == '' ">0.0.0.1</Version>
<Version Condition=" '$(DateTimeSuffix)' != '' ">$(DateTimeSuffix)</Version>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,17 @@
using System.Text.Json.Serialization;
public class SMonitorModel
{
[JsonPropertyName("VCP Code")]
public string? VCPCode { get; set; }
[JsonPropertyName("VCP Code Name")]
public string? VCPCodeName { get; set; }
[JsonPropertyName("Read-Write")]
public string? ReadWrite { get; set; }
[JsonPropertyName("Current Value")]
public string? CurrentValue { get; set; }
[JsonPropertyName("Maximum Value")]
public string? MaximumValue { get; set; }
[JsonPropertyName("Possible Values")]
public string? PossibleValues { get; set; }
}

View File

@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<AssemblyName>CMM.Service</AssemblyName>
<RootNamespace>CMM.Service</RootNamespace>
<Product>ControlMyMonitorManagement</Product>
<Nullable>enable</Nullable>
<Company>Dang</Company>
<Copyright>Copyright © DangWang $([System.DateTime]::Now.ToString(yyyy))</Copyright>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<Major>1</Major>
<Minor>0</Minor>
<ProjectStartedDate>$([System.DateTime]::op_Subtraction($([System.DateTime]::get_Now().get_Date()),$([System.DateTime]::new(2023,7,2))).get_TotalDays())</ProjectStartedDate>
<DaysSinceProjectStarted>$([System.DateTime]::Now.ToString(Hmm))</DaysSinceProjectStarted>
<DateTimeSuffix>$([System.DateTime]::Now.ToString(yyyyMMdd))</DateTimeSuffix>
<VersionSuffix>$(Major).$(Minor).$(ProjectStartedDate).$(DaysSinceProjectStarted)</VersionSuffix>
<AssemblyVersion Condition=" '$(DateTimeSuffix)' == '' ">0.0.0.1</AssemblyVersion>
<AssemblyVersion Condition=" '$(DateTimeSuffix)' != '' ">$(VersionSuffix)</AssemblyVersion>
<Version Condition=" '$(DateTimeSuffix)' == '' ">0.0.0.1</Version>
<Version Condition=" '$(DateTimeSuffix)' != '' ">$(DateTimeSuffix)</Version>
</PropertyGroup>
<ItemGroup>
<Folder Include="Interface\" />
<Folder Include="Service\" />
</ItemGroup>
</Project>

View File

@@ -11,7 +11,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Language", "Language\Langua
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tester", "Tester\Tester.csproj", "{0D34DD73-3282-40EB-8F59-DF190944BF12}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tester", "Tester\Tester.csproj", "{0D34DD73-3282-40EB-8F59-DF190944BF12}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DellMonitorControl", "DellMonitorControl\DellMonitorControl.csproj", "{64E96610-D431-40B9-A00B-55CE195B4B58}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DellMonitorControl", "DellMonitorControl\DellMonitorControl.csproj", "{64E96610-D431-40B9-A00B-55CE195B4B58}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CMMService", "CMMService\CMMService.csproj", "{FEA2B019-32BC-4704-939F-1CD26F373F55}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CMMModel", "CMMModel\CMMModel.csproj", "{88440737-DB6E-4ACA-B1BC-E40350153F96}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -39,6 +43,14 @@ Global
{64E96610-D431-40B9-A00B-55CE195B4B58}.Debug|Any CPU.Build.0 = Debug|Any CPU {64E96610-D431-40B9-A00B-55CE195B4B58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64E96610-D431-40B9-A00B-55CE195B4B58}.Release|Any CPU.ActiveCfg = Release|Any CPU {64E96610-D431-40B9-A00B-55CE195B4B58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64E96610-D431-40B9-A00B-55CE195B4B58}.Release|Any CPU.Build.0 = Release|Any CPU {64E96610-D431-40B9-A00B-55CE195B4B58}.Release|Any CPU.Build.0 = Release|Any CPU
{FEA2B019-32BC-4704-939F-1CD26F373F55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FEA2B019-32BC-4704-939F-1CD26F373F55}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FEA2B019-32BC-4704-939F-1CD26F373F55}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FEA2B019-32BC-4704-939F-1CD26F373F55}.Release|Any CPU.Build.0 = Release|Any CPU
{88440737-DB6E-4ACA-B1BC-E40350153F96}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{88440737-DB6E-4ACA-B1BC-E40350153F96}.Debug|Any CPU.Build.0 = Debug|Any CPU
{88440737-DB6E-4ACA-B1BC-E40350153F96}.Release|Any CPU.ActiveCfg = Release|Any CPU
{88440737-DB6E-4ACA-B1BC-E40350153F96}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework> <TargetFramework>net7.0-windows</TargetFramework>
<RootNamespace>CMM.Management</RootNamespace> <RootNamespace>CMM.Management</RootNamespace>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<Copyright>Copyright © DangWang $([System.DateTime]::Now.ToString(yyyy))</Copyright> <Copyright>Copyright © DangWang $([System.DateTime]::Now.ToString(yyyy))</Copyright>

View File

@@ -7,21 +7,8 @@
mc:Ignorable="d"> mc:Ignorable="d">
<Grid> <Grid>
<Border Background="{DynamicResource H3}" CornerRadius="10" MouseLeave="Border_MouseLeave"/> <Border Background="{DynamicResource H3}" CornerRadius="10" MouseLeave="Border_MouseLeave"/>
<StackPanel> <StackPanel Name="sp" Margin="10">
<StackPanel Orientation="Vertical" Margin="10,5,5,0">
<TextBlock Text="DELL U3223QE (Center)" Style="{StaticResource TitleStyle}" HorizontalAlignment="Left"/>
<Button Tag="BR974P3" Content="Sleep" Click="ToggleButton_Checked" Style="{StaticResource TextButtonStyle}"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="10,5,5,0">
<TextBlock Text="DELL U3223QE (Right)" Style="{StaticResource TitleStyle}" HorizontalAlignment="Left"/>
<Button Tag="CBBP3P3" Content="Sleep" Click="ToggleButton_Checked" Style="{StaticResource TextButtonStyle}"/>
</StackPanel>
<StackPanel Orientation="Vertical" Margin="10,5,5,0">
<TextBlock Text="DELL UP2716D (Left)" Style="{StaticResource TitleStyle}" HorizontalAlignment="Left"/>
<Button Tag="KV97067ICLCL" Content="Sleep" Click="ToggleButton_Checked" Style="{StaticResource TextButtonStyle}"/>
</StackPanel>
</StackPanel> </StackPanel>
</Grid> </Grid>
</UserControl> </UserControl>

View File

@@ -1,7 +1,8 @@
using CMM.Library.Method; using CMM.Library.Method;
using CMM.Library.ViewModel;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input; using System.Windows.Input;
namespace DellMonitorControl; namespace DellMonitorControl;
@@ -16,25 +17,68 @@ public partial class ControlPanel : UserControl
InitializeComponent(); InitializeComponent();
} }
public async Task Init()
{
await CMMCommand.ScanMonitor();
var monitors = await CMMCommand.ReadMonitorsData();
foreach (var m in monitors)
{
var status = await CMMCommand.GetMonPowerStatus(m.SerialNumber);
var ctrl = CreatControl(m, status);
sp.Children.Add(ctrl);
}
}
private StackPanel CreatControl(XMonitor monitorModel, string powerStatus)
{
var _sp = new StackPanel();
_sp.Orientation = Orientation.Vertical;
_sp.Margin = new Thickness(10, 5, 5, 0);
var tb = new TextBlock
{
Text = monitorModel.MonitorName,
HorizontalAlignment = HorizontalAlignment.Left,
Style = (Style)FindResource("LableStyle")
};
var btn = new Button
{
Tag = monitorModel.SerialNumber,
Content = powerStatus,
Style = (Style)FindResource("TextButtonStyle")
};
btn.Click += async (s, e) => await ToggleButton_Checked(s, e);
_sp.Children.Add(tb);
_sp.Children.Add(btn);
return _sp;
}
private void Border_MouseLeave(object sender, MouseEventArgs e) private void Border_MouseLeave(object sender, MouseEventArgs e)
{ {
} }
private async void ToggleButton_Checked(object sender, RoutedEventArgs e) private async Task ToggleButton_Checked(object sender, RoutedEventArgs e)
{ {
var toggle = sender as Button; var btn = sender as Button;
var tag = toggle?.Tag.ToString(); var tag = btn?.Tag.ToString();
var content = toggle?.Content as string; var content = btn?.Content as string;
if (content == "Sleep") if (content == "Sleep")
{ {
await CMMCommand.Sleep(tag); await CMMCommand.PowerOn(tag);
toggle!.Content = "PowerOn";
} }
else else
{ {
await CMMCommand.PowerOn(tag); await CMMCommand.Sleep(tag);
toggle!.Content = "Sleep";
} }
await Task.Delay(1000);
btn!.Content = await CMMCommand.GetMonPowerStatus(tag);
} }
} }

View File

@@ -2,12 +2,27 @@
<PropertyGroup> <PropertyGroup>
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework> <TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<AssemblyName>DellMonitorControl</AssemblyName> <AssemblyName>DellMonitorControl</AssemblyName>
<RootNamespace>DellMonitorControl</RootNamespace> <RootNamespace>DellMonitorControl</RootNamespace>
<Product>DellMonitorControl</Product> <Product>ControlMyMonitorManagement</Product>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<Company>Dang</Company>
<Copyright>Copyright © DangWang $([System.DateTime]::Now.ToString(yyyy))</Copyright>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<Major>1</Major>
<Minor>0</Minor>
<ProjectStartedDate>$([System.DateTime]::op_Subtraction($([System.DateTime]::get_Now().get_Date()),$([System.DateTime]::new(2023,7,2))).get_TotalDays())</ProjectStartedDate>
<DaysSinceProjectStarted>$([System.DateTime]::Now.ToString(Hmm))</DaysSinceProjectStarted>
<DateTimeSuffix>$([System.DateTime]::Now.ToString(yyyyMMdd))</DateTimeSuffix>
<VersionSuffix>$(Major).$(Minor).$(ProjectStartedDate).$(DaysSinceProjectStarted)</VersionSuffix>
<AssemblyVersion Condition=" '$(DateTimeSuffix)' == '' ">0.0.0.1</AssemblyVersion>
<AssemblyVersion Condition=" '$(DateTimeSuffix)' != '' ">$(VersionSuffix)</AssemblyVersion>
<Version Condition=" '$(DateTimeSuffix)' == '' ">0.0.0.1</Version>
<Version Condition=" '$(DateTimeSuffix)' != '' ">$(DateTimeSuffix)</Version>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@@ -20,6 +35,12 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Hardcodet.Wpf.TaskbarNotification.Net6" Version="1.0.0" /> <PackageReference Include="Hardcodet.Wpf.TaskbarNotification.Net6" Version="1.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="7.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -6,15 +6,16 @@
xmlns:tb="clr-namespace:Hardcodet.Wpf.TaskbarNotification;assembly=Hardcodet.Wpf.TaskbarNotification.Net6" xmlns:tb="clr-namespace:Hardcodet.Wpf.TaskbarNotification;assembly=Hardcodet.Wpf.TaskbarNotification.Net6"
xmlns:local="clr-namespace:DellMonitorControl" xmlns:local="clr-namespace:DellMonitorControl"
mc:Ignorable="d" mc:Ignorable="d"
Loaded="Window_Loaded"
Title="MainWindow" Height="450" Width="800"> Title="MainWindow" Height="450" Width="800">
<Grid> <Grid>
<tb:TaskbarIcon x:Name="taskbar" <tb:TaskbarIcon x:Name="taskbar"
IconSource="/DellLogo.ico" IconSource="/DellLogo.ico"
ToolTipText="調整效能" ToolTipText="螢幕開關"
PopupActivation="LeftOrDoubleClick"> PopupActivation="LeftOrDoubleClick">
<tb:TaskbarIcon.TrayPopup> <tb:TaskbarIcon.TrayPopup>
<local:ControlPanel/> <local:ControlPanel x:Name="comtrolPanel"/>
</tb:TaskbarIcon.TrayPopup> </tb:TaskbarIcon.TrayPopup>
</tb:TaskbarIcon> </tb:TaskbarIcon>
</Grid> </Grid>

View File

@@ -10,6 +10,11 @@ public partial class MainWindow : Window
public MainWindow() public MainWindow()
{ {
InitializeComponent(); InitializeComponent();
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Hide(); this.Hide();
await comtrolPanel.Init();
} }
} }

View File

@@ -15,7 +15,6 @@
</Style.Triggers> </Style.Triggers>
</Style> </Style>
<Style x:Key="TextBorderStyle" TargetType="{x:Type Border}"> <Style x:Key="TextBorderStyle" TargetType="{x:Type Border}">
<Setter Property="Background" Value="{StaticResource F3}"/> <Setter Property="Background" Value="{StaticResource F3}"/>
<Setter Property="BorderBrush" Value="#C1272C"/> <Setter Property="BorderBrush" Value="#C1272C"/>

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework> <TargetFramework>net7.0-windows</TargetFramework>
<RootNamespace>CMM.Language</RootNamespace> <RootNamespace>CMM.Language</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>

View File

@@ -1,16 +1,14 @@
using System; using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized; using System.Collections.Specialized;
namespace CMM.Library.Base namespace CMM.Library.Base;
/// <summary>
/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
/// </summary>
/// <typeparam name="T"></typeparam>
public class ObservableRangeCollection<T> : ObservableCollection<T>
{ {
/// <summary>
/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
/// </summary>
/// <typeparam name="T"></typeparam>
public class ObservableRangeCollection<T> : ObservableCollection<T>
{
/// <summary> /// <summary>
/// Adds the elements of the specified collection to the end of the ObservableCollection(Of T). /// Adds the elements of the specified collection to the end of the ObservableCollection(Of T).
/// </summary> /// </summary>
@@ -66,5 +64,4 @@ namespace CMM.Library.Base
/// <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception> /// <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception>
public ObservableRangeCollection(IEnumerable<T> collection) public ObservableRangeCollection(IEnumerable<T> collection)
: base(collection) { } : base(collection) { }
}
} }

View File

@@ -1,10 +1,10 @@
using System.ComponentModel; using System.ComponentModel;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace CMM.Library.Base namespace CMM.Library.Base;
public class PropertyBase : INotifyPropertyChanged
{ {
public class PropertyBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;
virtual internal protected void OnPropertyChanged(string propertyName) virtual internal protected void OnPropertyChanged(string propertyName)
@@ -27,5 +27,4 @@ namespace CMM.Library.Base
storage = value; storage = value;
this.OnPropertyChanged(propertyName); this.OnPropertyChanged(propertyName);
} }
}
} }

View File

@@ -2,20 +2,15 @@
using CMM.Library.Base; using CMM.Library.Base;
using CMM.Library.Helpers; using CMM.Library.Helpers;
using CMM.Library.Method; using CMM.Library.Method;
using System;
using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization; using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System.Windows; using System.Windows;
namespace CMM.Library.Config namespace CMM.Library.Config;
public class XConfig : PropertyBase
{ {
public class XConfig : PropertyBase
{
[JsonIgnore] [JsonIgnore]
public string Version { get; private set; } public string Version { get; private set; }
@@ -72,5 +67,4 @@ namespace CMM.Library.Config
MessageBox.Show($"{Lang.Find("SaveConfigErr")}{ex.Message}", "failed", MessageBoxButton.OK, MessageBoxImage.Error); MessageBox.Show($"{Lang.Find("SaveConfigErr")}{ex.Message}", "failed", MessageBoxButton.OK, MessageBoxImage.Error);
} }
} }
}
} }

View File

@@ -1,14 +1,9 @@
using System; using System.Diagnostics;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMM.Library.Helpers namespace CMM.Library.Helpers;
internal class ConsoleHelper
{ {
internal class ConsoleHelper
{
const string cmdFileName = "cmd.exe"; const string cmdFileName = "cmd.exe";
private static Process CreatProcess(string fileName) => private static Process CreatProcess(string fileName) =>
new Process() new Process()
@@ -24,6 +19,20 @@ namespace CMM.Library.Helpers
} }
}; };
public static async Task<string> ExecuteCommand(string command)
{
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = command;
p.Start();
var output = await p.StandardOutput.ReadToEndAsync();
await p.WaitForExitAsync();
return output;
}
public static async Task<string> CmdCommandAsync(params string[] cmds) => public static async Task<string> CmdCommandAsync(params string[] cmds) =>
await CommandAsync(cmdFileName, cmds); await CommandAsync(cmdFileName, cmds);
@@ -67,5 +76,4 @@ namespace CMM.Library.Helpers
Debug.WriteLine(result); Debug.WriteLine(result);
return result; return result;
} }
}
} }

View File

@@ -1,17 +1,13 @@
using System; using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Text.Json; using System.Text.Json;
using System.Threading.Tasks;
namespace CMM.Library.Helpers namespace CMM.Library.Helpers;
static class JsonSerializerExtensions
{ {
static class JsonSerializerExtensions
{
public static JsonSerializerOptions defaultSettings = new JsonSerializerOptions() public static JsonSerializerOptions defaultSettings = new JsonSerializerOptions()
{ {
WriteIndented = true, WriteIndented = true,
@@ -19,10 +15,10 @@ namespace CMM.Library.Helpers
PropertyNamingPolicy = null, PropertyNamingPolicy = null,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
}; };
} }
internal static class JsonHelper public static class JsonHelper
{ {
/// <summary> /// <summary>
/// 複製整個obj全部結構 /// 複製整個obj全部結構
/// </summary> /// </summary>
@@ -127,5 +123,4 @@ namespace CMM.Library.Helpers
catch (IOException) { throw; } catch (IOException) { throw; }
catch (Exception) { throw; } catch (Exception) { throw; }
} }
}
} }

View File

@@ -1,10 +1,9 @@
using System; using System.Security.Principal;
using System.Security.Principal;
namespace CMM.Library.Helpers namespace CMM.Library.Helpers;
public class UAC
{ {
public class UAC
{
public static void Check() public static void Check()
{ {
var identity = WindowsIdentity.GetCurrent(); var identity = WindowsIdentity.GetCurrent();
@@ -14,5 +13,4 @@ namespace CMM.Library.Helpers
"You likely need to run this application 'as administrator' even if you are using an administrator account."); "You likely need to run this application 'as administrator' even if you are using an administrator account.");
} }
}
} }

View File

@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework> <TargetFramework>net7.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>CMM.Library</RootNamespace> <RootNamespace>CMM.Library</RootNamespace>
<AssemblyName>CMM.Library</AssemblyName>
<Product>ControlMyMonitorManagement</Product> <Product>ControlMyMonitorManagement</Product>
<UseWPF>true</UseWPF> <UseWPF>true</UseWPF>
<Company>Dang</Company> <Company>Dang</Company>
@@ -34,7 +35,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\CMMModel\CMMModel.csproj" />
<ProjectReference Include="..\Language\Language.csproj" /> <ProjectReference Include="..\Language\Language.csproj" />
<ProjectReference Include="..\Tester\Tester.csproj" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@@ -1,14 +1,9 @@
using System; using System.Diagnostics;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMM.Library.Method namespace CMM.Library.Method;
public static class AssemblyData
{ {
public static class AssemblyData
{
/// <summary> /// <summary>
/// 當下Assembly名稱 /// 當下Assembly名稱
/// </summary> /// </summary>
@@ -44,5 +39,4 @@ namespace CMM.Library.Method
$"{fvi.FileBuildPart}." + $"{fvi.FileBuildPart}." +
$"{fvi.FilePrivatePart}"; $"{fvi.FilePrivatePart}";
} }
}
} }

View File

@@ -2,6 +2,8 @@
using CMM.Library.Helpers; using CMM.Library.Helpers;
using CMM.Library.ViewModel; using CMM.Library.ViewModel;
using System.IO; using System.IO;
using System.Net.NetworkInformation;
using System.Threading;
namespace CMM.Library.Method; namespace CMM.Library.Method;
@@ -13,7 +15,6 @@ public static class CMMCommand
static readonly string CMMTmpFolder = Path.Combine(Path.GetTempPath(), $"CMM"); static readonly string CMMTmpFolder = Path.Combine(Path.GetTempPath(), $"CMM");
static readonly string CMMexe = Path.Combine(CMMTmpFolder, "ControlMyMonitor.exe"); static readonly string CMMexe = Path.Combine(CMMTmpFolder, "ControlMyMonitor.exe");
static readonly string CMMsMonitors = Path.Combine(CMMTmpFolder, "smonitors.tmp"); static readonly string CMMsMonitors = Path.Combine(CMMTmpFolder, "smonitors.tmp");
static readonly string CMMcfg = Path.Combine(CMMTmpFolder, "ControlMyMonitor.cfg");
public static async Task ScanMonitor() public static async Task ScanMonitor()
{ {
@@ -21,80 +22,92 @@ public static class CMMCommand
await ConsoleHelper.CmdCommandAsync($"{CMMexe} /smonitors {CMMsMonitors}"); await ConsoleHelper.CmdCommandAsync($"{CMMexe} /smonitors {CMMsMonitors}");
} }
public static async Task PowerOn(string MonitorSn) public static Task PowerOn(string monitorSN)
{ {
await ConsoleHelper.CmdCommandAsync($"{CMMexe} /SetValue {MonitorSn} D6 1"); return ConsoleHelper.CmdCommandAsync($"{CMMexe} /SetValue {monitorSN} D6 1");
} }
public static async Task Sleep(string MonitorSn) public static Task Sleep(string monitorSN)
{ {
await ConsoleHelper.CmdCommandAsync($"{CMMexe} /SetValue {MonitorSn} D6 4"); return ConsoleHelper.CmdCommandAsync($"{CMMexe} /SetValue {monitorSN} D6 4");
} }
public static async Task ScanMonitorInterfaces(IEnumerable<XMonitor> monitors) private static async Task<string> GetMonitorValue(string monitorSN)
{ {
var taskList = new List<Task>(); var cmdFileName = Path.Combine(CMMTmpFolder, $"{Guid.NewGuid()}.bat");
foreach (var mon in monitors) var cmd = $"{CMMexe} /GetValue {monitorSN} D6\r\n" +
{ $"echo %errorlevel%";
taskList.Add(Task.Run(async () => await File.WriteAllText(cmdFileName, cmd);
ScanMonitorInterfaces($"{CMMTmpFolder}\\{mon.SerialNumber}.tmp", mon))); var values = await ConsoleHelper.ExecuteCommand(cmdFileName);
File.Delete(cmdFileName);
return values.Split("\r\n", StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
} }
await Task.WhenAll(taskList.ToArray()); public static async Task<string> GetMonPowerStatus(string monitorSN)
{
var status = await GetMonitorValue(monitorSN);
return status switch
{
"1" => "PowerOn",
"4" => "Sleep",
"5" => "PowerOff",
_ => string.Empty
};
} }
static async Task ScanMonitorInterfaces(string savePath, XMonitor mon) public static async Task ScanMonitorStatus(IEnumerable<XMonitor> monitors)
{ {
await ConsoleHelper.CmdCommandAsync($"{CMMexe} /scomma {savePath} {mon.MonitorID}"); var taskList = monitors.Select(x =>
await mon.ReadMonitorStatus(savePath); {
return ScanMonitorStatus($"{CMMTmpFolder}\\{x.SerialNumber}.tmp", x);
});
await Task.WhenAll(taskList);
}
static async Task ScanMonitorStatus(string savePath, XMonitor mon)
{
await ConsoleHelper.CmdCommandAsync($"{CMMexe} /sjson {savePath} {mon.MonitorID}");
var monitorModel = JsonHelper.JsonFormFile<IEnumerable<SMonitorModel>>(savePath);
var status = monitorModel.ReadMonitorStatus();
mon.Status = new ObservableRangeCollection<XMonitorStatus>(status);
} }
/// <summary> /// <summary>
/// 取得螢幕狀態 /// 取得螢幕狀態
/// </summary> /// </summary>
public static async Task ReadMonitorStatus(this XMonitor @this, string filePath) public static IEnumerable<XMonitorStatus> ReadMonitorStatus(this IEnumerable<SMonitorModel> monitorModel)
{ {
var statusColle = new ObservableRangeCollection<XMonitorStatus>(); foreach (var m in monitorModel)
if (!File.Exists(filePath)) return;
foreach (var line in await File.ReadAllLinesAsync(filePath))
{ {
var sp = line.Split(","); yield return new XMonitorStatus
if (sp.Length < 6) continue;
statusColle.Add(new XMonitorStatus
{ {
VCP_Code = StrTrim(sp[0]), VCP_Code = m.VCPCode,
VCPCodeName = StrTrim(sp[1]), VCPCodeName = m.VCPCodeName,
Read_Write = StrTrim(sp[2]), Read_Write = m.ReadWrite,
CurrentValue = TryGetInt(sp[3]), CurrentValue = TryGetInt(m.CurrentValue),
MaximumValue = TryGetInt(sp[4]), MaximumValue = TryGetInt(m.MaximumValue),
PossibleValues = TryGetArrStr(sp), PossibleValues = TryGetArrStr(m.PossibleValues),
}); };
} }
@this.Status = statusColle; IEnumerable<int> TryGetArrStr(string str)
string StrTrim(string str)
{ {
if (string.IsNullOrEmpty(str)) return null; return str.Split(",", StringSplitOptions.RemoveEmptyEntries)
return str; .Select(x => TryGetInt(x))
} .Where(x => x != null)
.Select(x => (int)x)
string TryGetArrStr(string[] strArr) .ToList();
{
if (strArr.Length < 7) return null;
var outStr = string.Join(",", strArr[5..]);
outStr = outStr.Substring(1, outStr.Length - 2);
return outStr;
} }
int? TryGetInt(string str) int? TryGetInt(string str)
{ {
if (int.TryParse(str, out var value)) return value; return int.TryParse(str, out var value)
return null; ? value
: null;
} }
} }

View File

@@ -1,15 +1,10 @@
using CMM.Library.Base; using CMM.Library.Base;
using CMM.Library.ViewModel; using CMM.Library.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMM.Library.Method namespace CMM.Library.Method;
public class CMMMgr : PropertyBase
{ {
public class CMMMgr : PropertyBase
{
public ObservableRangeCollection<XMonitor> Monitors public ObservableRangeCollection<XMonitor> Monitors
{ {
get => _Monitors; get => _Monitors;
@@ -23,7 +18,6 @@ namespace CMM.Library.Method
var monColle = new ObservableRangeCollection<XMonitor>(); var monColle = new ObservableRangeCollection<XMonitor>();
monColle.AddRange(await CMMCommand.ReadMonitorsData()); monColle.AddRange(await CMMCommand.ReadMonitorsData());
Monitors = monColle; Monitors = monColle;
await CMMCommand.ScanMonitorInterfaces(monColle); await CMMCommand.ScanMonitorStatus(monColle);
}
} }
} }

View File

@@ -1,14 +1,9 @@
using CMM.Library.Base; using CMM.Library.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMM.Library.ViewModel namespace CMM.Library.ViewModel;
public class XMonitor : PropertyBase
{ {
public class XMonitor : PropertyBase
{
/// <summary> /// <summary>
/// 裝置路徑 /// 裝置路徑
/// </summary> /// </summary>
@@ -68,5 +63,4 @@ namespace CMM.Library.ViewModel
set { SetProperty(ref _Status, value); } set { SetProperty(ref _Status, value); }
} }
ObservableRangeCollection<XMonitorStatus> _Status = new(); ObservableRangeCollection<XMonitorStatus> _Status = new();
}
} }

View File

@@ -1,9 +1,4 @@
using CMM.Library.Base; using CMM.Library.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CMM.Library.ViewModel namespace CMM.Library.ViewModel
{ {
@@ -44,11 +39,11 @@ namespace CMM.Library.ViewModel
} }
int? _MaximumValue; int? _MaximumValue;
public string PossibleValues public IEnumerable<int> PossibleValues
{ {
get => _PossibleValues; get => _PossibleValues;
set { SetProperty(ref _PossibleValues, value); } set { SetProperty(ref _PossibleValues, value); }
} }
string _PossibleValues; IEnumerable<int> _PossibleValues;
} }
} }

38
Tester/CommnadTest.cs Normal file
View File

@@ -0,0 +1,38 @@
using CMM.Library.Base;
using CMM.Library.Helpers;
using CMM.Library.Method;
using CMM.Library.ViewModel;
namespace CMM.Tester;
public class CommnadTest
{
[SetUp]
public void Setup()
{
}
[Test]
public async Task Test1()
{
await CMMCommand.ScanMonitor();
var monColle = new ObservableRangeCollection<XMonitor>();
monColle.AddRange(await CMMCommand.ReadMonitorsData());
await CMMCommand.ScanMonitorStatus(monColle);
}
[Test]
public void JsonParserTest()
{
var path = @"C:\Users\shoop\AppData\Local\Temp\CMM\KV97067ICLCL.tmp";
var monitorModel = JsonHelper.JsonFormFile<IEnumerable<SMonitorModel>>(path);
}
[Test]
public async Task GetMonPowerStatus()
{
var status = await CMMCommand.GetMonPowerStatus("CBBP3P3");
}
}

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net7.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>CMM.Tester</RootNamespace> <RootNamespace>CMM.Tester</RootNamespace>
<Product>ControlMyMonitorManagement</Product> <Product>ControlMyMonitorManagement</Product>
@@ -18,4 +18,12 @@
<PackageReference Include="coverlet.collector" Version="3.1.2" /> <PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CMMModel\CMMModel.csproj" />
<ProjectReference Include="..\CMMService\CMMService.csproj" />
<ProjectReference Include="..\Language\Language.csproj" />
<ProjectReference Include="..\Library\Library.csproj" />
</ItemGroup>
</Project> </Project>

View File

@@ -1,16 +0,0 @@
namespace CMM.Tester
{
public class Tests
{
[SetUp]
public void Setup()
{
}
[Test]
public void Test1()
{
Assert.Pass();
}
}
}