OeeDebugPageViewModel.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using HandyControl.Controls;
  2. using HandyControl.Tools.Extension;
  3. using LogoForceTestApp.Modules.MainModule.Models;
  4. using Newtonsoft.Json;
  5. using Prism.Commands;
  6. using Prism.Mvvm;
  7. using Prism.Regions;
  8. using Serilog;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Linq;
  13. using System.Net.Http;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. using System.Windows.Input;
  17. namespace LogoForceTestApp.Modules.MainModule.ViewModels
  18. {
  19. internal class OeeDebugPageViewModel: BindableBase
  20. {
  21. private readonly IRegionManager _regionManager;
  22. private readonly ILogger _logger;
  23. private readonly IHttpClientFactory _httpClientFactory;
  24. public string Op_Id { get; set; } = "C10088888";
  25. public string OeeIp { get; set; }
  26. //[OnChangedMethod("SetChanged")]
  27. public int OeePort { get; set; }
  28. //[OnChangedMethod("SetChanged")]
  29. public int WaitPartDuration { get; set; } = 1;
  30. public string Site { get; set; }
  31. public int StopDuration { get; set; } = 5;
  32. public string Floor { get; set; }
  33. public string Product { get; set; }
  34. public string Line { get; set; }
  35. public string ProcessOee { get; set; }
  36. public string TerminalId { get; set; }
  37. public string MachineId { get; set; }
  38. public string MacAddr { get; set; }
  39. public string IpAddr { get; set; }
  40. //public List<string> AddressSource { get; set; }
  41. public string Status { get; set; }
  42. public string OeePath { get; set; }
  43. public OeeDebugPageViewModel(IRegionManager regionManager,ILogger logger,IHttpClientFactory httpClientFactory)
  44. {
  45. var oeePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "oee.json");
  46. if (File.Exists(oeePath))
  47. {
  48. var streamOee = new StreamReader(oeePath);
  49. var json = streamOee.ReadToEnd();
  50. var oeeConfig = JsonConvert.DeserializeObject<OeeConfig>(json);
  51. OeeIp = oeeConfig.Ip;
  52. OeePort = oeeConfig.Port;
  53. WaitPartDuration = oeeConfig.WaitPartDuration;
  54. StopDuration = oeeConfig.StopDuration;
  55. Site = oeeConfig.Site;
  56. Floor = oeeConfig.Floor;
  57. Product = oeeConfig.Product;
  58. Line = oeeConfig.Line;
  59. ProcessOee = oeeConfig.Process;
  60. TerminalId = oeeConfig.TerminalId;
  61. MachineId = oeeConfig.MachineId;
  62. MacAddr = oeeConfig.Macaddr;
  63. IpAddr = oeeConfig.Ipaddr;
  64. OeePath = oeeConfig.OeePath;
  65. }
  66. _regionManager = regionManager;
  67. _logger = logger;
  68. _httpClientFactory = httpClientFactory;
  69. }
  70. private DelegateCommand uploadCommand;
  71. public ICommand UploadCommand => uploadCommand ??= new DelegateCommand(Upload);
  72. private async void Upload()
  73. {
  74. using var httpClient = _httpClientFactory.CreateClient();
  75. var oeeLogger = new OeeLogger
  76. {
  77. site = Site,
  78. floor = Floor,
  79. product = Product,
  80. line = Line,
  81. vendor = "Team",
  82. process = ProcessOee,
  83. terminalId = TerminalId,
  84. machineId = MachineId,
  85. status = Status,
  86. op_id = Op_Id,
  87. startTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  88. ipaddr = IpAddr,
  89. macaddr = MacAddr,
  90. warningCodeList = new Warningcodelist[1]
  91. };
  92. oeeLogger.warningCodeList[0] = new Warningcodelist
  93. {
  94. warningCode = "100002",
  95. remark = "待料"
  96. };
  97. await UploadOeeAsync(oeeLogger);
  98. }
  99. private async Task UploadOeeAsync(OeeLogger oeeLogger)
  100. {
  101. using var client = _httpClientFactory.CreateClient();
  102. //var url = _appSttings.Value.OeeUrl;
  103. var loggingURI = $"http://{OeeIp}:{OeePort}/device/upload/logging/v1";
  104. try
  105. {
  106. var json = JsonConvert.SerializeObject(oeeLogger, Formatting.Indented);
  107. WriteToLocal(json);
  108. _logger.Information(json);
  109. _logger.Information($"{loggingURI}");
  110. client.BaseAddress = new Uri(loggingURI);
  111. var content = new StringContent(json, Encoding.UTF8, "application/json");
  112. var response= await client.PostAsync(loggingURI, content);
  113. var s = await response.Content.ReadAsStringAsync();
  114. MessageBox.Show($"状态码:{response.StatusCode}:" + s, "服务器响应");
  115. }
  116. catch (Exception e)
  117. {
  118. //WriteLog($"oee上传失败,{e.Message}", LogType.Error);
  119. MessageBox.Show($"oee上传失败,{e.Message}", "提示");
  120. }
  121. }
  122. private void WriteToLocal(string json)
  123. {
  124. var name = DateTime.Now.ToString("yyyy-mm-dd-HH-mm-ss")+"_oee" + ".json";
  125. var path = Path.Combine(OeePath, name);
  126. if (!Directory.Exists(OeePath))
  127. {
  128. MessageBox.Show("oee路径不存在", "提示");
  129. return;
  130. }
  131. using var stream = new StreamWriter(path);
  132. stream.Write(json);
  133. }
  134. }
  135. }