From 3b7924d1229257ab1c08a5b84441cda4f6f258a4 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Tue, 25 Aug 2020 17:28:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=88=E6=B7=BB=E5=8A=A0=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=94=A8=E4=BA=8E=E6=9B=B4=E6=96=B0IoTNode?= =?UTF-8?q?=EF=BC=8C=E5=AE=8C=E6=88=90=E5=90=8E=E5=86=8D=E5=9C=A8IoTNode?= =?UTF-8?q?=E4=B8=AD=E6=B7=BB=E5=8A=A0=E6=89=8B=E5=8A=A8=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E7=9A=84=E6=9B=B4=E6=96=B0"=E6=9B=B4=E6=96=B0=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F"=E7=9A=84=E6=96=B9=E6=B3=95=20=E6=9B=B4=E6=96=B0"?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=A8=8B=E5=BA=8F"=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E7=A1=AE=E4=BF=9DIoTNode=E7=9A=84=E7=89=88=E6=9C=AC=E5=92=8C?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E7=AB=AF=E4=B8=8Aiotnode.xml=E9=87=8C?= =?UTF-8?q?=E7=9A=84=E7=89=88=E6=9C=AC=E5=8F=B7=E6=98=AF=E4=B8=80=E8=87=B4?= =?UTF-8?q?=E7=9A=84=EF=BC=8C=E8=BF=99=E6=A0=B7=E5=8F=AF=E4=BB=A5=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E5=86=B2=E7=AA=81=EF=BC=9B=E5=BC=80=E5=A7=8B=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E6=9B=B4=E6=96=B0=E6=97=B6=E5=B0=B1=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E7=A8=8B=E5=BA=8F=EF=BC=8C=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=BB=93=E6=9D=9F=E5=90=8E=E5=86=8D=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 67d6137cc0ac6b1a46f4b72201daf8b5e84e8c41 Former-commit-id: 7c752e3a9b1cd1775d43acd8e1dc17ee356b3afa --- projects/IoTCenter/wwwroot/dameon.temp.xml | 5 --- projects/IoTCenter/wwwroot/iotnode.xml | 5 +++ .../IoTDameon/Controllers/HomeController.cs | 34 +-------------- .../Controllers/ISupervisorService.cs | 19 -------- .../ISupervisorService.cs | 2 +- projects/IoTDameon/Startup.cs | 2 +- .../UpdateIoTNodeService.cs} | 43 +++++++++++-------- .../IoTNode/Controllers/HomeController.cs | 21 +++++++-- 8 files changed, 50 insertions(+), 81 deletions(-) delete mode 100644 projects/IoTCenter/wwwroot/dameon.temp.xml create mode 100644 projects/IoTCenter/wwwroot/iotnode.xml delete mode 100644 projects/IoTDameon/Controllers/ISupervisorService.cs rename projects/{IoTNode/Services => IoTDameon}/ISupervisorService.cs (94%) rename projects/{IoTNode/Services/UpdateDameonService.cs => IoTDameon/UpdateIoTNodeService.cs} (80%) diff --git a/projects/IoTCenter/wwwroot/dameon.temp.xml b/projects/IoTCenter/wwwroot/dameon.temp.xml deleted file mode 100644 index b0bee291..00000000 --- a/projects/IoTCenter/wwwroot/dameon.temp.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - 1.0.0.20082401 - - \ No newline at end of file diff --git a/projects/IoTCenter/wwwroot/iotnode.xml b/projects/IoTCenter/wwwroot/iotnode.xml new file mode 100644 index 00000000..6e0cd4d1 --- /dev/null +++ b/projects/IoTCenter/wwwroot/iotnode.xml @@ -0,0 +1,5 @@ + + + 1.0.0.20082501 + b67172948390b8594382cec333498b0905bbdf0fa70f32c3882d7dbc07d745ed899be55bf90f460d63caf12d3924d7384bb0722a264f7b2184e167b1b63397c0 + \ No newline at end of file diff --git a/projects/IoTDameon/Controllers/HomeController.cs b/projects/IoTDameon/Controllers/HomeController.cs index ac05a95b..5c53ae44 100644 --- a/projects/IoTDameon/Controllers/HomeController.cs +++ b/projects/IoTDameon/Controllers/HomeController.cs @@ -1,43 +1,13 @@ -using CookComputing.XmlRpc; -using Microsoft.AspNetCore.Mvc; -using System.Net; -using System.Net.Http; +using Microsoft.AspNetCore.Mvc; using System.Reflection; namespace IoTDameon.Controllers { public class HomeController : Controller { - private IHttpClientFactory _httpClientFactory; - - public HomeController(IHttpClientFactory httpClientFactory) - { - _httpClientFactory = httpClientFactory; - } - public IActionResult Index() { - this.Process(); - return Content(""); - } - - private void Process(bool stop = false) - { - try - { - var proxy = XmlRpcProxyGen.Create(); - proxy.Url = "http://192.168.1.3:9001/RPC2"; - proxy.Credentials = new NetworkCredential("usr", "pwd"); - var result = stop ? proxy.stopProcess("iotnode") : proxy.startProcess("iotnode"); - } - catch (XmlRpcFaultException ex) - { - //start:60 stop:70 - if (ex.FaultCode != 60 && ex.FaultCode != 70) - { - throw ex; - } - } + return GetVersion(); } public IActionResult GetVersion() diff --git a/projects/IoTDameon/Controllers/ISupervisorService.cs b/projects/IoTDameon/Controllers/ISupervisorService.cs deleted file mode 100644 index 5cfe8edc..00000000 --- a/projects/IoTDameon/Controllers/ISupervisorService.cs +++ /dev/null @@ -1,19 +0,0 @@ -using CookComputing.XmlRpc; - -namespace IoTDameon.Controllers -{ - public interface ISupervisorService : IXmlRpcProxy - { - [XmlRpcMethod("supervisor.getAllProcessInfo")] - XmlRpcStruct getAllProcessInfo(); - - [XmlRpcMethod("supervisor.getProcessInfo")]//result["statename"]: "RUNNING" or "STOPPED" - XmlRpcStruct getProcessInfo(string name); - - [XmlRpcMethod("supervisor.startProcess")] - bool startProcess(string name); - - [XmlRpcMethod("supervisor.stopProcess")] - bool stopProcess(string name); - } -} \ No newline at end of file diff --git a/projects/IoTNode/Services/ISupervisorService.cs b/projects/IoTDameon/ISupervisorService.cs similarity index 94% rename from projects/IoTNode/Services/ISupervisorService.cs rename to projects/IoTDameon/ISupervisorService.cs index 687f2296..f41c52b9 100644 --- a/projects/IoTNode/Services/ISupervisorService.cs +++ b/projects/IoTDameon/ISupervisorService.cs @@ -1,6 +1,6 @@ using CookComputing.XmlRpc; -namespace IoTNode.Services +namespace IoTDameon { public interface ISupervisorService : IXmlRpcProxy { diff --git a/projects/IoTDameon/Startup.cs b/projects/IoTDameon/Startup.cs index f1dc3516..d03bc5be 100644 --- a/projects/IoTDameon/Startup.cs +++ b/projects/IoTDameon/Startup.cs @@ -25,7 +25,7 @@ namespace IoTDameon builder.SetIsOriginAllowed(o => true).AllowAnyMethod().AllowAnyHeader().AllowCredentials(); }) ); - + services.AddHostedService(); services.AddHttpClient(); services.AddControllersWithViews(); diff --git a/projects/IoTNode/Services/UpdateDameonService.cs b/projects/IoTDameon/UpdateIoTNodeService.cs similarity index 80% rename from projects/IoTNode/Services/UpdateDameonService.cs rename to projects/IoTDameon/UpdateIoTNodeService.cs index f12b0177..a4a93b01 100644 --- a/projects/IoTNode/Services/UpdateDameonService.cs +++ b/projects/IoTDameon/UpdateIoTNodeService.cs @@ -15,17 +15,18 @@ using System.Threading; using System.Threading.Tasks; using System.Xml; -namespace IoTNode.Services +namespace IoTDameon { - public class UpdateDameonService : BackgroundService + public class UpdateIoTNodeService : BackgroundService { - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly IWebHostEnvironment _env; private readonly IConfiguration _cfg; private readonly IHttpClientFactory _httpClientFactory; - private bool _isUpdating; - public UpdateDameonService(ILogger logger, IWebHostEnvironment env, IConfiguration cfg, IHttpClientFactory httpClientFactory) + public bool IsUpdating { get; set; } + + public UpdateIoTNodeService(ILogger logger, IWebHostEnvironment env, IConfiguration cfg, IHttpClientFactory httpClientFactory) { this._logger = logger; this._env = env; @@ -47,13 +48,13 @@ namespace IoTNode.Services private void Update() { - if (this._isUpdating) + if (this.IsUpdating) { return; } else { - this._isUpdating = true; + this.IsUpdating = true; } try { @@ -63,23 +64,26 @@ namespace IoTNode.Services { this._logger.LogError(ex, "update error"); } - this._isUpdating = false; + this.IsUpdating = false; } private void UpdateInternal() { + var processName = "iotnode"; + var appFolder = "IoTNode"; + var port = 8002; var proxyUrl = "http://localhost:9001/RPC2"; - var processName = "iotdaemon"; var root = Directory.GetParent(_env.ContentRootPath).FullName; - var appPath = Path.Combine(root, "IoTDameon"); - var name = "dameon.zip"; - var backupName = Path.Combine(root, "iotdameon.bk.zip"); + var appPath = Path.Combine(root, appFolder); + var name = $"{appFolder}.zip"; + var backupName = Path.Combine(root, $"{appFolder}.bk.zip"); var file = Path.Combine(root, name); var currentCheckSum = string.Empty; //检查是否有更新 - this._logger.LogInformation("check dameon version"); - var currentVersion = this._httpClientFactory.CreateClient().GetAsync("http://localhost:8003/Home/GetVersion").Result.Content.ReadAsStringAsync().Result; - var info = this._httpClientFactory.CreateClient().GetAsync($"{this._cfg["notify:host"]}/dameon.xml").Result.Content.ReadAsStringAsync().Result; + this._logger.LogInformation("check version"); + var currentVersion = this._httpClientFactory.CreateClient().GetAsync($"http://localhost:{port}/Home/GetVersion").Result.Content.ReadAsStringAsync().Result; + var server = this._httpClientFactory.CreateClient().GetAsync($"http://localhost:{port}/Home/GetServer").Result.Content.ReadAsStringAsync().Result; + var info = this._httpClientFactory.CreateClient().GetAsync($"{server}/{processName}.xml").Result.Content.ReadAsStringAsync().Result; var doc = new XmlDocument(); doc.LoadXml(info); var lastVersion = doc.GetElementsByTagName("version")[0].InnerText.Trim(); @@ -131,15 +135,14 @@ namespace IoTNode.Services try { ZipFile.CreateFromDirectory(appPath, backupName); - Directory.Delete(appPath, true); - Directory.CreateDirectory(appPath); } catch (Exception ex) { this._logger.LogError(ex, ex.Message); throw new Exception("备份程序失败", ex); } - + Directory.Delete(appPath, true); + Directory.CreateDirectory(appPath); //更新程序 try { @@ -154,7 +157,9 @@ namespace IoTNode.Services //设置权限 if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { - Process.Start("/bin/bash", $"-c \"chmod 755 {Path.Combine(appPath, "IoTDameon")}\""); + var updateScript = Path.Combine(appPath, "update.sh"); + Process.Start("/bin/bash", $"-c \"chmod 755 {file}\""); + Process.Start(updateScript); } //启动更新程序 proxy.startProcess(processName); diff --git a/projects/IoTNode/Controllers/HomeController.cs b/projects/IoTNode/Controllers/HomeController.cs index d4956606..1ebabc36 100644 --- a/projects/IoTNode/Controllers/HomeController.cs +++ b/projects/IoTNode/Controllers/HomeController.cs @@ -1,24 +1,27 @@ using Application.Domain.Entities; +using Infrastructure.Application.Services.Settings; using Infrastructure.Data; using Infrastructure.Web; using IoT.Shared.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; -using System; using System.Linq; +using System.Reflection; namespace IoTNode.Controllers { //[Device] public class HomeController : BaseController { + private readonly ISettingService _settingService; private readonly IRepository _nodeRepo; private readonly IRepository _sceneRepo; private readonly IoTNodeClient _ioTNodeClient; - public HomeController(IRepository nodeRepo, IRepository sceneRepo,IoTNodeClient ioTNodeClient) + public HomeController(ISettingService settingService, IRepository nodeRepo, IRepository sceneRepo, IoTNodeClient ioTNodeClient) { + this._settingService = settingService; this._nodeRepo = nodeRepo; this._sceneRepo = sceneRepo; this._ioTNodeClient = ioTNodeClient; @@ -36,8 +39,8 @@ namespace IoTNode.Controllers var model = this._sceneRepo.ReadOnlyTable() .Include(o => o.SceneTimers) .Include(o => o.SceneTiggers) - .Include(o => o.SceneCommands).ThenInclude(o=>o.Command).ThenInclude(o=>o.Device) - .Include(o=>o.SceneCommands).ThenInclude(o=>o.Command).ThenInclude(o=>o.Api) + .Include(o => o.SceneCommands).ThenInclude(o => o.Command).ThenInclude(o => o.Device) + .Include(o => o.SceneCommands).ThenInclude(o => o.Command).ThenInclude(o => o.Api) .ToList(); return View(model); } @@ -47,5 +50,15 @@ namespace IoTNode.Controllers this._ioTNodeClient.OnConnected(); return RedirectTo(); } + + public IActionResult GetVersion() + { + return Content(Assembly.GetEntryAssembly().GetCustomAttribute().InformationalVersion); + } + + public IActionResult GetServer() + { + return Content(this._settingService.GetSetting("notify:host").Value?.Trim()); + } } } \ No newline at end of file