Przeglądaj źródła

add MainViewModel

chenzk 2 dni temu
rodzic
commit
ce7d6b4e83

+ 4 - 0
FestoDebugger.csproj

@@ -8,4 +8,8 @@
     <UseWPF>true</UseWPF>
   </PropertyGroup>
 
+  <ItemGroup>
+    <Folder Include="Service\" />
+  </ItemGroup>
+
 </Project>

+ 21 - 0
Service/SignalOperationManager.cs

@@ -0,0 +1,21 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace FestoDebugger.Service
+{
+    public class SignalOperationManager : Singleton<SignalOperationManager>
+    {
+        public SignalOperationManager() 
+        { 
+        
+        }
+
+        public void opearte(string moudleName,string itemName,bool value)
+        {
+
+        }
+    }
+}

+ 15 - 0
Service/Singleton.cs

@@ -0,0 +1,15 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace FestoDebugger.Service
+{
+    [Serializable]
+    public class Singleton<T> where T : class, new()
+    {
+        private static readonly Lazy<T> lazy =
+                                        new Lazy<T>(() => new T(), true);
+        public static T Instance => lazy.Value;
+    } 
+}

+ 16 - 14
UserControls/FestoControl.xaml

@@ -5,7 +5,8 @@
              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
              xmlns:local="clr-namespace:FestoDebugger.UserControls"
              mc:Ignorable="d" x:Name="self"
-             d:DesignHeight="200" d:DesignWidth="130">
+             d:DesignHeight="180" d:DesignWidth="100">
+    <Border BorderBrush="Gray" BorderThickness="1">
     <Grid>
         <Grid.RowDefinitions>
             <RowDefinition Height="30"/>
@@ -16,42 +17,42 @@
             <RowDefinition/>
         </Grid.RowDefinitions>
         <Grid.ColumnDefinitions>
-            <ColumnDefinition Width="15"/>
+
             <ColumnDefinition Width="50"/>
             <ColumnDefinition Width="50"/>
 
             <ColumnDefinition/>
         </Grid.ColumnDefinitions>
-        <Label  Grid.Row="0" Grid.Column="1"  HorizontalAlignment="Center" Content="A" FontSize="12" VerticalContentAlignment="Center" VerticalAlignment="Center" />
-        <Label  Grid.Row="0" Grid.Column="2"  HorizontalAlignment="Center" Content="B" FontSize="12" VerticalContentAlignment="Center" VerticalAlignment="Center" />
-
-        <Label  Grid.Row="4" Grid.Column="0"  Grid.ColumnSpan="4" HorizontalAlignment="Center" Content="{Binding ModuleName}" FontSize="12" VerticalContentAlignment="Center" VerticalAlignment="Center" />
-        <Button  Grid.Row="1" Grid.Column="1" Width="40" Height="30" Background="DeepSkyBlue" Content="On" Click="AOn_Click" Canvas.Left="60" Canvas.Top="39"></Button>
-        <Button  Grid.Row="2" Grid.Column="1" Width="40" Height="30" Background="DeepSkyBlue" Content="Off"  Click="AOff_Click" Canvas.Left="101" Canvas.Top="40"></Button>
-        <Button  Grid.Row="1" Grid.Column="2" Width="40" Height="30" Background="DeepSkyBlue" Content="On" Click="BOn_Click" Canvas.Left="60" Canvas.Top="39"></Button>
-        <Button  Grid.Row="2" Grid.Column="2" Width="40" Height="30" Background="DeepSkyBlue" Content="Off"  Click="BOff_Click" Canvas.Left="101" Canvas.Top="40"></Button>
-        <Ellipse Grid.Row="3" Grid.Column="1" Width="16" Height="16"  Fill ="LimeGreen" Stroke="Silver" HorizontalAlignment="Center" VerticalAlignment="Top">
+        <Label  Grid.Row="0" Grid.Column="0"  HorizontalAlignment="Center" Content="A" FontSize="12" VerticalContentAlignment="Center" VerticalAlignment="Center" />
+        <Label  Grid.Row="0" Grid.Column="1"  HorizontalAlignment="Center" Content="B" FontSize="12" VerticalContentAlignment="Center" VerticalAlignment="Center" />
+        <Label  Grid.Row="4" Grid.Column="0"  Grid.ColumnSpan="2" HorizontalAlignment="Center" Content="{Binding ModuleName}" FontSize="12" VerticalContentAlignment="Center" VerticalAlignment="Top" />
+        
+        <Button  Grid.Row="1" Grid.Column="0" Width="40" Height="30" Background="DeepSkyBlue" Content="On" Click="AOn_Click" Canvas.Left="60" Canvas.Top="39"></Button>
+        <Button  Grid.Row="2" Grid.Column="0" Width="40" Height="30" Background="DeepSkyBlue" Content="Off"  Click="AOff_Click" Canvas.Left="101" Canvas.Top="40"></Button>
+        <Button  Grid.Row="1" Grid.Column="1" Width="40" Height="30" Background="DeepSkyBlue" Content="On" Click="BOn_Click" Canvas.Left="60" Canvas.Top="39"></Button>
+        <Button  Grid.Row="2" Grid.Column="1" Width="40" Height="30" Background="DeepSkyBlue" Content="Off"  Click="BOff_Click" Canvas.Left="101" Canvas.Top="40"></Button>
+        <Ellipse Grid.Row="3" Grid.Column="0" Width="16" Height="16" Stroke="Silver" HorizontalAlignment="Center" VerticalAlignment="Top">
             <Ellipse.Style>
                 <Style TargetType="Ellipse">
                     <!-- 默认颜色(SignalOn为false时) -->
                     <Setter Property="Fill" Value="LightGray"/>
                     <Style.Triggers>
                         <!-- 当SignalOn为true时变为绿色 -->
-                        <DataTrigger Binding="{Binding ElementName=self, Path=SignalAOn}" Value="True">
+                        <DataTrigger Binding="{Binding SignalAOn}" Value="True">
                             <Setter Property="Fill" Value="LimeGreen"/>
                         </DataTrigger>
                     </Style.Triggers>
                 </Style>
             </Ellipse.Style>
         </Ellipse>
-        <Ellipse Grid.Row="3" Grid.Column="2" Width="16" Height="16"  Fill ="LimeGreen" Stroke="Silver" HorizontalAlignment="Center" VerticalAlignment="Top">
+        <Ellipse Grid.Row="3" Grid.Column="1" Width="16" Height="16" Stroke="Silver" HorizontalAlignment="Center" VerticalAlignment="Top">
             <Ellipse.Style>
                 <Style TargetType="Ellipse">
                     <!-- 默认颜色(SignalOn为false时) -->
                     <Setter Property="Fill" Value="LightGray"/>
                     <Style.Triggers>
                         <!-- 当SignalOn为true时变为绿色 -->
-                        <DataTrigger Binding="{Binding ElementName=self, Path=SignalBOn}" Value="True">
+                        <DataTrigger Binding="{Binding SignalBOn}" Value="True">
                             <Setter Property="Fill" Value="LimeGreen"/>
                         </DataTrigger>
                     </Style.Triggers>
@@ -59,4 +60,5 @@
             </Ellipse.Style>
         </Ellipse>
     </Grid>
+    </Border>
 </UserControl>

+ 5 - 4
UserControls/FestoControl.xaml.cs

@@ -1,4 +1,5 @@
-using System;
+using FestoDebugger.Service;
+using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
@@ -61,7 +62,7 @@ namespace FestoDebugger.UserControls
         }
 
         public static readonly DependencyProperty SignalBOnProperty = DependencyProperty.Register(
-       "SignalBOn", typeof(bool), typeof(FestoControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
+            "SignalBOn", typeof(bool), typeof(FestoControl), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
         /// <summary>
         /// WaferPresent
         /// </summary>
@@ -79,12 +80,12 @@ namespace FestoDebugger.UserControls
 
         private void AOn_Click(object sender, RoutedEventArgs e)
         {
-
+            SignalOperationManager.Instance.opearte(ModuleName, "A", true);
         }
 
         private void AOff_Click(object sender, RoutedEventArgs e)
         {
-
+            SignalOperationManager.Instance.opearte(ModuleName, "A", false);
         }
 
         private void BOn_Click(object sender, RoutedEventArgs e)

+ 175 - 8
ViewModels/MainViewModel.cs

@@ -2,36 +2,203 @@
 using System;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
+using System.ComponentModel;
+using System.Data;
 using System.Linq;
+using System.Net;
+using System.Net.Sockets;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows.Input;
+using System.Windows.Media;
 
 namespace FestoDebugger.ViewModels
 {
     public class MainViewModel : ViewModelBase
     {
+        #region 内部变量
+        private string _ipAddress = "192.168.1.1";
+
+        private string _port = "502";
+
+        private string _connectionStatus = "未连接";
+        
+        private bool _isConnecting;
+        
+        private bool _isConnected;
+
         private ObservableCollection<SignalModuleData> _signalModuleDatas;
 
+        #endregion
+
+        #region 属性
+        public string IpAddress
+        {
+            get => _ipAddress;
+            set
+            {
+                if (_ipAddress != value)
+                {
+                    _ipAddress = value;
+                    OnPropertyChanged(nameof(IpAddress));
+                }
+            }
+        }
+
+        public string Port
+        {
+            get => _port;
+            set
+            {
+                if (_port != value)
+                {
+                    _port = value;
+                    OnPropertyChanged(nameof(Port));
+                }
+            }
+        }
+
+
+        public string ConnectionStatus
+        {
+            get => _connectionStatus;
+            set
+            {
+                if (_connectionStatus != value)
+                {
+                    _connectionStatus = value;
+                    OnPropertyChanged(nameof(ConnectionStatus));
+                }
+            }
+        }
+
         public ObservableCollection<SignalModuleData> SignalModuleDatas
         {
             get => _signalModuleDatas;
             set => SetProperty(ref _signalModuleDatas, value);
         }
 
+        #endregion
+
+        public bool IsNotConnecting => !_isConnecting;
+
+        public string ConnectButtonText => _isConnected ? "断开连接" : "连接设备";
+
+        public Brush StatusColor => _isConnected ? Brushes.Green : Brushes.Red;
+
+        public ICommand ConnectCommand { get; private set; }
+
         public MainViewModel()
         {
+
+            ConnectCommand = new RelayCommand(async () => await ToggleConnection());
             SignalModuleDatas = new ObservableCollection<SignalModuleData>();
-            SignalModuleData  data = new SignalModuleData();
-            data.ModuleName = "V1";
-            data.SignalAOn = true;
-            data.SignalAOn = false;
-            SignalModuleDatas.Add(data);
+            for (int i = 1; i < 11; i++)
+            {
+                SignalModuleData data = new SignalModuleData();
+                data.ModuleName = $"V{i}";
+                data.SignalAOn = true;
+                data.SignalBOn = false;
+                SignalModuleDatas.Add(data);
+            }
+
             //LoadSignalConfig();
         }
-
+        /// <summary>
+        /// 加载配置文件,初始化数据
+        /// </summary>
         private void LoadSignalConfig()
         {
-            
+
+        }
+
+        private async Task ToggleConnection()
+        {
+            if (_isConnected)
+            {
+                Disconnect();
+                return;
+            }
+
+            await Connect();
+        }
+
+        private async Task Connect()
+        {
+            if (!IPAddress.TryParse(IpAddress, out var ipAddress) || !int.TryParse(Port, out var port))
+            {
+                ConnectionStatus = "IP或端口格式错误";
+                return;
+            }
+
+            _isConnecting = true;
+            OnPropertyChanged(nameof(IsNotConnecting));
+            ConnectionStatus = "连接中...";
+
+            try
+            {
+                using var client = new TcpClient();
+                var connectTask = client.ConnectAsync(ipAddress, port);
+
+                if (await Task.WhenAny(connectTask, Task.Delay(5000)) == connectTask)
+                {
+                    if (client.Connected)
+                    {
+                        _isConnected = true;
+                        ConnectionStatus = "已连接";
+                    }
+                    else
+                    {
+                        ConnectionStatus = "连接失败";
+                    }
+                }
+                else
+                {
+                    ConnectionStatus = "连接超时";
+                }
+            }
+            catch (Exception ex)
+            {
+                ConnectionStatus = $"错误: {ex.Message}";
+            }
+            finally
+            {
+                _isConnecting = false;
+                OnPropertyChanged(nameof(IsNotConnecting));
+                OnPropertyChanged(nameof(ConnectButtonText));
+                OnPropertyChanged(nameof(StatusColor));
+            }
+        }
+
+        private void Disconnect()
+        {
+            _isConnected = false;
+            ConnectionStatus = "已断开";
+            OnPropertyChanged(nameof(ConnectButtonText));
+            OnPropertyChanged(nameof(StatusColor));
+        }
+
+
+    }
+
+    public class RelayCommand : ICommand
+        {
+            private readonly Func<Task> _execute;
+            private readonly Func<bool> _canExecute;
+
+            public event EventHandler CanExecuteChanged;
+
+            public RelayCommand(Func<Task> execute, Func<bool> canExecute = null)
+            {
+                _execute = execute ?? throw new ArgumentNullException(nameof(execute));
+                _canExecute = canExecute;
+            }
+
+            public bool CanExecute(object parameter) => _canExecute?.Invoke() ?? true;
+
+            public async void Execute(object parameter) => await _execute();
+
+            public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
         }
     }
-}
+

+ 52 - 2
Views/MainWindow.xaml

@@ -12,11 +12,60 @@
     <Window.DataContext>
         <local1:MainViewModel />
     </Window.DataContext>
-    <ScrollViewer>
+    <Canvas>
+        
+        <Grid Height="40" Width="700" Margin="10,10,0,0" Panel.ZIndex="1">
+            <Grid.RowDefinitions>
+                <RowDefinition Height="30"/>
+            </Grid.RowDefinitions>
+            <Grid.ColumnDefinitions>
+                <ColumnDefinition Width="50"/>
+                <ColumnDefinition Width="100"/>
+                <ColumnDefinition Width="50"/>
+                <ColumnDefinition Width="50"/>
+                <ColumnDefinition Width="100"/>
+                <ColumnDefinition Width="100"/>
+            </Grid.ColumnDefinitions>
+
+            <!-- IP地址输入 -->
+            <TextBlock Text="IP地址:" 
+                   Grid.Row="0" Grid.Column="0"
+                   VerticalAlignment="Center"/>
+            <TextBox x:Name="IpTextBox"
+                 Grid.Row="0" Grid.Column="1"
+                 Text="{Binding IpAddress, UpdateSourceTrigger=PropertyChanged}"
+                 VerticalContentAlignment="Center" TextChanged="IpTextBox_TextChanged"/>
+
+            <!-- 端口输入 -->
+            <TextBlock Text="端口:" 
+                   Grid.Row="0" Grid.Column="2"
+                   Margin="10,0,0,0"
+                   VerticalAlignment="Center"/>
+            <TextBox x:Name="PortTextBox"
+                 Grid.Row="0" Grid.Column="3"
+                 Margin="0,0,0,0"
+                 Text="{Binding Port, UpdateSourceTrigger=PropertyChanged}"
+                 VerticalContentAlignment="Center"/>
+
+            <!-- 连接按钮 -->
+            <Button Content="Connect" Width="70"
+                Margin="10,0,0,0"
+                Grid.Row="0" Grid.Column="4"
+                Command="{Binding ConnectCommand}"
+                IsEnabled="{Binding IsNotConnecting}"/>
+
+            <TextBlock Text="{Binding ConnectionStatus}"
+                   Grid.Row="0" Grid.Column="5" 
+                   Margin="10,10,0,0"
+                   Foreground="{Binding StatusColor}"
+                   HorizontalAlignment="Center"/>
+
+        </Grid>
+        <ScrollViewer Height="450" Width="800">
         <ItemsControl ItemsSource="{Binding SignalModuleDatas}">
             <ItemsControl.ItemsPanel>
                 <ItemsPanelTemplate>
-                    <WrapPanel Orientation="Horizontal"/>
+                    <WrapPanel Orientation="Horizontal" Margin="20,50,20,0"/>
                 </ItemsPanelTemplate>
             </ItemsControl.ItemsPanel>
             <ItemsControl.ItemTemplate>
@@ -26,4 +75,5 @@
             </ItemsControl.ItemTemplate>
         </ItemsControl>
     </ScrollViewer>
+    </Canvas>
 </Window>

+ 5 - 0
Views/MainWindow.xaml.cs

@@ -20,5 +20,10 @@ namespace FestoDebugger
         {
             InitializeComponent();
         }
+
+        private void IpTextBox_TextChanged(object sender, TextChangedEventArgs e)
+        {
+
+        }
     }
 }