using HandyControl.Controls; using HandyControl.Tools.Extension; using LogoForceTestApp.Modules.MainModule.Models; using Newtonsoft.Json; using Prism.Commands; using Prism.Mvvm; using Prism.Regions; using Serilog; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; using System.Windows.Input; namespace LogoForceTestApp.Modules.MainModule.ViewModels { internal class OeeDebugPageViewModel: BindableBase { private readonly IRegionManager _regionManager; private readonly ILogger _logger; private readonly IHttpClientFactory _httpClientFactory; public string Op_Id { get; set; } = "C10088888"; public string OeeIp { get; set; } //[OnChangedMethod("SetChanged")] public int OeePort { get; set; } //[OnChangedMethod("SetChanged")] public int WaitPartDuration { get; set; } = 1; public string Site { get; set; } public int StopDuration { get; set; } = 5; public string Floor { get; set; } public string Product { get; set; } public string Line { get; set; } public string ProcessOee { get; set; } public string TerminalId { get; set; } public string MachineId { get; set; } public string MacAddr { get; set; } public string IpAddr { get; set; } //public List AddressSource { get; set; } public string Status { get; set; } public string OeePath { get; set; } public OeeDebugPageViewModel(IRegionManager regionManager,ILogger logger,IHttpClientFactory httpClientFactory) { var oeePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "oee.json"); if (File.Exists(oeePath)) { var streamOee = new StreamReader(oeePath); var json = streamOee.ReadToEnd(); var oeeConfig = JsonConvert.DeserializeObject(json); OeeIp = oeeConfig.Ip; OeePort = oeeConfig.Port; WaitPartDuration = oeeConfig.WaitPartDuration; StopDuration = oeeConfig.StopDuration; Site = oeeConfig.Site; Floor = oeeConfig.Floor; Product = oeeConfig.Product; Line = oeeConfig.Line; ProcessOee = oeeConfig.Process; TerminalId = oeeConfig.TerminalId; MachineId = oeeConfig.MachineId; MacAddr = oeeConfig.Macaddr; IpAddr = oeeConfig.Ipaddr; OeePath = oeeConfig.OeePath; } _regionManager = regionManager; _logger = logger; _httpClientFactory = httpClientFactory; } private DelegateCommand uploadCommand; public ICommand UploadCommand => uploadCommand ??= new DelegateCommand(Upload); private async void Upload() { using var httpClient = _httpClientFactory.CreateClient(); var oeeLogger = new OeeLogger { site = Site, floor = Floor, product = Product, line = Line, vendor = "Team", process = ProcessOee, terminalId = TerminalId, machineId = MachineId, status = Status, op_id = Op_Id, startTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ipaddr = IpAddr, macaddr = MacAddr, warningCodeList = new Warningcodelist[1] }; oeeLogger.warningCodeList[0] = new Warningcodelist { warningCode = "100002", remark = "待料" }; await UploadOeeAsync(oeeLogger); } private async Task UploadOeeAsync(OeeLogger oeeLogger) { using var client = _httpClientFactory.CreateClient(); //var url = _appSttings.Value.OeeUrl; var loggingURI = $"http://{OeeIp}:{OeePort}/device/upload/logging/v1"; try { var json = JsonConvert.SerializeObject(oeeLogger, Formatting.Indented); WriteToLocal(json); _logger.Information(json); _logger.Information($"{loggingURI}"); client.BaseAddress = new Uri(loggingURI); var content = new StringContent(json, Encoding.UTF8, "application/json"); var response= await client.PostAsync(loggingURI, content); var s = await response.Content.ReadAsStringAsync(); MessageBox.Show($"状态码:{response.StatusCode}:" + s, "服务器响应"); } catch (Exception e) { //WriteLog($"oee上传失败,{e.Message}", LogType.Error); MessageBox.Show($"oee上传失败,{e.Message}", "提示"); } } private void WriteToLocal(string json) { var name = DateTime.Now.ToString("yyyy-mm-dd-HH-mm-ss")+"_oee" + ".json"; var path = Path.Combine(OeePath, name); if (!Directory.Exists(OeePath)) { MessageBox.Show("oee路径不存在", "提示"); return; } using var stream = new StreamWriter(path); stream.Write(json); } } }