瀏覽代碼

Merge branch 'master' of http://49.235.130.76/LN/LPLocalhostMES

Eson 2 周之前
父節點
當前提交
b5925be275

+ 15 - 3
LocalhostMES/Api/Hosting/WebApiService.cs

@@ -1,6 +1,7 @@
 using LocalhostMES.Core;
 using Microsoft.Owin.Hosting;
 using System;
+using System.Configuration;
 using System.Threading;
 using System.Threading.Tasks;
 
@@ -18,13 +19,24 @@ namespace LocalhostMES.Api.Hosting
 
         public void Init(int port = 8081)
         {
-            _baseUrl = $"http://192.168.1.26:{port}";
+            string url1=ConfigurationManager.AppSettings[ "WebApiUrl" ];
+            string url2= ConfigurationManager.AppSettings[ "WebApiMesUrl" ];
+            _baseUrl = $"{url1}:{port}";
             _startOptions = new StartOptions
             {
-                Urls = { $"http://192.168.1.26:{port}", $"http://10.157.15.51:{port}" }
+                Urls = { $"{url1}:{port}", $"{url2}:{port}" }
+            };
+        }
+        public void InitLocalhost(int port = 8081)
+        {
+            string url1=ConfigurationManager.AppSettings[ "WebApiUrl" ];
+            string url2= ConfigurationManager.AppSettings[ "WebApiMesUrl" ];
+            _baseUrl = $"{url1}:{port}";
+            _startOptions = new StartOptions
+            {
+                Urls = { $"{url1}:{port}"}
             };
         }
-
         public Task StartAsync()
         {
             try

+ 13 - 4
LocalhostMES/Api/Services/MesApiClient.cs

@@ -5,6 +5,7 @@ using Newtonsoft.Json;
 using Newtonsoft.Json.Linq;
 using System;
 using System.Collections.Generic;
+using System.Configuration;
 using System.Net.Http;
 using System.Text;
 using System.Threading.Tasks;
@@ -20,7 +21,7 @@ namespace LocalhostMES.Api.Services
         private readonly string _baseUrl;
         private readonly string _appKey;
         private readonly string _token;
-
+        private MesMomHttpEndpoints mesMom;
         public MesApiClient(string baseUrl, string appKey = "test-app-key", string token = "test-token-123")
         {
             _baseUrl = baseUrl?.TrimEnd('/') ?? "";
@@ -30,6 +31,14 @@ namespace LocalhostMES.Api.Services
             {
                 Timeout = TimeSpan.FromSeconds(60)
             };
+            mesMom=new MesMomHttpEndpoints();
+            mesMom.SetUrl();
+            if ( ConfigurationManager.AppSettings[ "IsMesTest" ] == "1" ) 
+            {
+                mesMom.SetTest();
+            }
+
+           
         }
 
         #region 2.1 厂级MES下发工单信息
@@ -55,7 +64,7 @@ namespace LocalhostMES.Api.Services
         {
             var requestJson = JsonConvert.SerializeObject(request);
             var content = new StringContent(requestJson, Encoding.UTF8, "application/json");
-            var requestMessage = new HttpRequestMessage(HttpMethod.Post, MesMomHttpEndpoints.BatterySnPrint)
+            var requestMessage = new HttpRequestMessage(HttpMethod.Post, mesMom.BatterySnPrint)
             {
                 Content = content
             };
@@ -111,7 +120,7 @@ namespace LocalhostMES.Api.Services
         {
             var requestJson = JsonConvert.SerializeObject(request);
             var content = new StringContent(requestJson, Encoding.UTF8, "application/json");
-            var requestMessage = new HttpRequestMessage(HttpMethod.Post, MesMomHttpEndpoints.ReceiveSnKeyComponent)
+            var requestMessage = new HttpRequestMessage(HttpMethod.Post, mesMom.ReceiveSnKeyComponent)
             {
                 Content = content
             };
@@ -142,7 +151,7 @@ namespace LocalhostMES.Api.Services
             var requestJson = JsonConvert.SerializeObject(request);
             var content = new StringContent(requestJson, Encoding.UTF8, "application/json");
             LogHelper.WriteLogInfo(requestJson);
-            var requestMessage = new HttpRequestMessage(HttpMethod.Post, MesMomHttpEndpoints.ManualSave)
+            var requestMessage = new HttpRequestMessage(HttpMethod.Post, mesMom.ManualSave)
             {
                 Content = content
             };

+ 26 - 6
LocalhostMES/Api/Services/MesMomHttpEndpoints.cs

@@ -1,17 +1,37 @@
+using System.Configuration;
+
 namespace LocalhostMES.Api.Services
 {
     /// <summary>
     /// 零跑 MOM 云端接口地址(与本地 <c>/api/mes/*</c> 区分)。
     /// </summary>
-    internal static class MesMomHttpEndpoints
+    public class MesMomHttpEndpoints
     {
-        internal const string BatterySnPrint =
+        public  string BatterySnPrint =
             "https://mom-jh-annex-api.leapmotor.com/app-mom-mes/api/battery/sn/print";
-
-        internal const string ReceiveSnKeyComponent =
+        public  string ReceiveSnKeyComponent =
             "https://mom-jh-annex-api.leapmotor.com/app-mom-mes/api/task/receive/sn/keyComponent";
-
-        internal const string ManualSave =
+        public  string ManualSave =
             "https://mom-jh-annex-api.leapmotor.com/app-mom-mes/api/task/receive/manualSave";
+
+        public void SetUrl()
+        {
+
+            BatterySnPrint = ConfigurationManager.AppSettings[ "BatterySnPrint" ];
+
+            ReceiveSnKeyComponent = ConfigurationManager.AppSettings[ "ReceiveSnKeyComponent" ];
+
+            ManualSave = ConfigurationManager.AppSettings[ "ManualSave" ];
+        }
+
+        public void SetTest()
+        {
+            BatterySnPrint = ConfigurationManager.AppSettings[ "BatterySnPrintTest" ];
+
+            ReceiveSnKeyComponent = ConfigurationManager.AppSettings[ "ReceiveSnKeyComponentTest" ];
+
+            ManualSave = ConfigurationManager.AppSettings[ "ManualSaveTest" ];
+
+        }
     }
 }

+ 33 - 0
LocalhostMES/App.config

@@ -3,6 +3,39 @@
   <configSections>
     <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
   </configSections>
+  <appSettings>
+    <add key="IsMesTest" value="1" />
+    
+    <add key="BatterySnPrint" value="https://mom-jh-annex-api.leapmotor.com/app-mom-mes/api/battery/sn/print" />
+   
+    <add key="ReceiveSnKeyComponent" value="https://mom-jh-annex-api.leapmotor.com/app-mom-mes/api/task/receive/sn/keyComponent" />
+   
+    <add key="ManualSave" value="https://mom-jh-annex-api.leapmotor.com/app-mom-mes/api/task/receive/manualSave" />
+ 
+    <add key="BatterySnPrintTest" value="https://uat-mom-jh-handle-api.leapmotor.com/app-mom-mes/api/battery/sn/print" />
+
+    <add key="ReceiveSnKeyComponentTest" value="https://uat-mom-jh-annex-api.leapmotor.com/app-mom-mes/api/task/receive/sn/keyComponent" />
+
+    <add key="ManualSaveTest" value="https://uat-mom-jh-annex-api.leapmotor.com/app-mom-mes/api/task/receive/manualSave" />
+
+
+
+    <add key="ApiUrl" value="http://192.168.1.26:5000" />
+
+    <add key="AppKey" value="API_XXM" />
+
+    <add key="Token" value="7c5766afa3ca82f8a21af120f9b1cca6d" />
+
+
+
+    <add key="WebApiUrl" value="http://192.168.1.26" />
+
+
+    <add key="WebApiMesUrl" value="http://10.157.15.51" />
+
+
+
+  </appSettings>
   <startup>
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
   </startup>

+ 11 - 8
LocalhostMES/App.xaml.cs

@@ -52,21 +52,24 @@ namespace LocalhostMES
             try
             {
                 var systemSettingsViewModel = Container.Resolve<SystemSettingsViewModel>();
-                systemSettingsViewModel.SaveSettings();
                 systemSettingsViewModel.StartService();
-
+                systemSettingsViewModel.SaveSettings();
                 Container.Resolve<SystemSettingsViewModel>().StartService();
-                Process.Start(@"D:\HANS LASER\TeamAAS-VP.exe");
+                bool isRunning = Process.GetProcessesByName("TeamAAS-VM").Length > 0;
+                if ( !isRunning )
+                {
+                    Process.Start(@"D:\HANS LASER\TeamAAS-VP.exe");
+                }
             }
-            catch (Exception ex)
+            catch ( Exception ex )
             {
-                MessageBox.Show( $"启动自动初始化系统设置失败: {ex.Message}");
+                MessageBox.Show($"启动自动初始化系统设置失败: {ex.Message}");
             }
         }
         private static System.Threading.Mutex mutex;
         protected override void OnStartup(StartupEventArgs e)
         {
-           
+
 
             mutex = new System.Threading.Mutex(true, "LocalhostMes_CRNS");
             if ( mutex.WaitOne(0, false) )
@@ -79,9 +82,9 @@ namespace LocalhostMES
                 this.Shutdown();
             }
         }
- 
 
-       
+
+
     }
 }
 

+ 53 - 1
LocalhostMES/ViewModels/Tabs/SystemSettingsViewModel.cs

@@ -6,6 +6,7 @@ using Prism.Commands;
 using Prism.Ioc;
 using Prism.Mvvm;
 using System;
+using System.Configuration;
 using System.Windows.Media;
 
 namespace LocalhostMES.ViewModels.Tabs
@@ -21,6 +22,10 @@ namespace LocalhostMES.ViewModels.Tabs
             _container = container;
             ViewLoadedCommand = new DelegateCommand(UpdateServiceStatus);
             _isDarkTheme = ThemeManager.CurrentTheme == ThemeMode.Dark;
+
+            ApiUrl=  ConfigurationManager.AppSettings[ "ApiUrl" ];
+            AppKey = ConfigurationManager.AppSettings[ "AppKey" ];
+            Token = ConfigurationManager.AppSettings[ "Token" ];
         }
 
         public DelegateCommand ViewLoadedCommand { get; }
@@ -48,7 +53,7 @@ namespace LocalhostMES.ViewModels.Tabs
             set => SetProperty(ref _serverPort, value);
         }
 
-        private string _apiUrl = "http://localhost:5000";
+        private string _apiUrl = "http://192.168.1.26:5000";
         public string ApiUrl
         {
             get => _apiUrl;
@@ -101,6 +106,11 @@ namespace LocalhostMES.ViewModels.Tabs
         public DelegateCommand StartServiceCommand =>
             _startServiceCommand ?? (_startServiceCommand = new DelegateCommand(StartService));
 
+
+        private DelegateCommand _startLocalhostServiceCommand;
+        public DelegateCommand StartLocalhostServiceCommand =>
+            _startLocalhostServiceCommand ?? ( _startLocalhostServiceCommand = new DelegateCommand(StartLocalhostService) );
+
         private DelegateCommand _stopServiceCommand;
         public DelegateCommand StopServiceCommand =>
             _stopServiceCommand ?? (_stopServiceCommand = new DelegateCommand(StopService));
@@ -142,6 +152,39 @@ namespace LocalhostMES.ViewModels.Tabs
             }
         }
 
+
+        public async void StartLocalhostService()
+        {
+            try
+            {
+                _workspace.Initialize();
+                if ( ServerPort < 1 || ServerPort > 65535 )
+                {
+                    _workspace.ShowStatus("请输入有效的端口号(1-65535)", true);
+                    return;
+                }
+
+                if ( Management.WebApiService == null )
+                {
+                    Management.WebApiService = _container.Resolve<WebApiService>();
+                }
+
+                if ( Management.WebApiService.IsRunning )
+                {
+                    await Management.WebApiService.StopAsync();
+                }
+
+                Management.WebApiService.InitLocalhost(ServerPort);
+                await Management.WebApiService.StartAsync();
+
+                UpdateServiceStatus();
+                _workspace.ShowStatus($"Web API服务已启动,端口: {ServerPort}", false);
+            }
+            catch ( Exception ex )
+            {
+                _workspace.ShowStatus($"服务启动失败: {ex.Message}", true);
+            }
+        }
         private async void StopService()
         {
             try
@@ -173,8 +216,17 @@ namespace LocalhostMES.ViewModels.Tabs
                     _workspace.ShowStatus("请输入API地址", true);
                     return;
                 }
+                if(Management.ApiClient!= null) { Management.ApiClient.Dispose(); }
 
                 Management.ApiClient = new MesApiClient(apiUrl, appKey, token);
+
+                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
+                config.AppSettings.Settings[ "ApiUrl" ].Value = ApiUrl;
+                config.AppSettings.Settings[ "AppKey" ].Value = AppKey;
+                config.AppSettings.Settings[ "Token" ].Value = Token;
+                config.Save(ConfigurationSaveMode.Modified);
+                ConfigurationManager.RefreshSection("appSettings");
+
                 _workspace.ShowStatus("API客户端配置已保存", false);
             }
             catch (Exception ex)

+ 7 - 2
LocalhostMES/Views/Tabs/SystemSettingsView.xaml

@@ -7,8 +7,8 @@
              xmlns:prism="http://prismlibrary.com/"
              prism:ViewModelLocator.AutoWireViewModel="True"
              mc:Ignorable="d"
-             d:DesignHeight="480"
-             d:DesignWidth="780">
+             d:DesignHeight="1000"
+             d:DesignWidth="1000">
     <Grid Margin="10">
         <b:Interaction.Triggers>
             <b:EventTrigger EventName="Loaded">
@@ -43,6 +43,11 @@
                             Width="90"
                             IsEnabled="{Binding StopServiceEnable}"
                             Command="{Binding StopServiceCommand}" />
+                    <Button Content="仅启动本地服务"
+                            Style="{StaticResource SuccessButtonStyle}"
+                            Width="90"
+                            IsEnabled="{Binding StartServiceEnable}"
+                            Command="{Binding StartLocalhostServiceCommand}" />
                 </StackPanel>
                 <TextBlock Foreground="{Binding Servicebrushes}"
                            Text="{Binding ServiceStatus}"