VeederRoot_ATG_Console_Handler_Controller.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Http;
  5. using System.Net.Http.Headers;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using Applications.UniversalApi_WebConsole_App.Models.VeederRoot_ATG_Console_Handler_Models;
  9. using Edge.Core.IndustryStandardInterface.ATG;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.AspNetCore.Mvc;
  12. namespace Applications.UniversalApi_WebConsole_App.Controllers
  13. {
  14. public class VeederRoot_ATG_Console_Handler_Controller : Controller
  15. {
  16. private static MockAutoTankGaugeController mockAtgController;
  17. static VeederRoot_ATG_Console_Handler_Controller()
  18. {
  19. var tank1 = new Tank()
  20. {
  21. TankNumber = 1,
  22. Diameter = 1800,
  23. Product = new Product()
  24. {
  25. ProductCode = "02",
  26. ProductLabel = "92#"
  27. },
  28. Limit = new TankLimit() { },
  29. Probe = new Probe()
  30. {
  31. ProbeLength = 1800,
  32. State = "",
  33. }
  34. };
  35. var tank2 = new Tank()
  36. {
  37. TankNumber = 2,
  38. Diameter = 1800,
  39. Product = new Product()
  40. {
  41. ProductCode = "05",
  42. ProductLabel = "95#"
  43. },
  44. Limit = new TankLimit()
  45. {
  46. },
  47. Probe = new Probe()
  48. {
  49. ProbeLength = 2800,
  50. State = "",
  51. }
  52. };
  53. var tank3 = new Tank()
  54. {
  55. TankNumber = 3,
  56. Diameter = 1800,
  57. Product = new Product()
  58. {
  59. ProductCode = "08",
  60. ProductLabel = "98#"
  61. },
  62. Limit = new TankLimit()
  63. {
  64. },
  65. Probe = new Probe()
  66. {
  67. ProbeLength = 1800,
  68. State = "",
  69. }
  70. };
  71. var tank4 = new Tank()
  72. {
  73. TankNumber = 4,
  74. Diameter = 1800,
  75. Product = new Product()
  76. {
  77. ProductCode = "09",
  78. ProductLabel = "0#"
  79. },
  80. Limit = new TankLimit()
  81. {
  82. },
  83. Probe = new Probe()
  84. {
  85. ProbeLength = 1800,
  86. State = "",
  87. }
  88. };
  89. var tank5 = new Tank()
  90. {
  91. TankNumber = 5,
  92. Diameter = 1800,
  93. Product = new Product()
  94. {
  95. ProductCode = "11",
  96. ProductLabel = "-35#"
  97. },
  98. Limit = new TankLimit()
  99. {
  100. },
  101. Probe = new Probe()
  102. {
  103. ProbeLength = 1800,
  104. State = "",
  105. }
  106. };
  107. mockAtgController = new MockAutoTankGaugeController(
  108. new List<Tank>()
  109. {
  110. tank1,tank2,tank3,tank4,tank5
  111. }, SystemUnit.Metric);
  112. }
  113. // GET: VeederRoot_ATG_Console_Handler_
  114. public async Task<ActionResult> Index(int? tankNumber)
  115. {
  116. HttpClient client = new HttpClient();
  117. client.DefaultRequestHeaders.Accept.Clear();
  118. client.DefaultRequestHeaders.Accept.Add(
  119. new MediaTypeWithQualityHeaderValue("application/json"));
  120. client.DefaultRequestHeaders.Add("User-Agent", "UniversalWebConsoleApp");
  121. var streamTask = client.GetStreamAsync("http://localhost:8384/u/?mn=GetAutoTankGaugeControllerAsync&pn=VeederRoot_ATG_Console_Tcp");
  122. var atgController = await JsonSerializer.DeserializeAsync<IAutoTankGaugeController>(await streamTask);
  123. return View("/Views/VeederRoot_ATG_Console_Handler/Index.cshtml", atgController);
  124. }
  125. public async Task<ActionResult> IndexMock(int? tankNumber)
  126. {
  127. var model = new AtgController()
  128. {
  129. Name = mockAtgController.MetaConfigName,
  130. DeviceId = mockAtgController.DeviceId,
  131. SystemUnit = mockAtgController.SystemUnit
  132. };
  133. if (tankNumber.HasValue)
  134. model.Tanks = mockAtgController.Tanks.Where(t => t.TankNumber == tankNumber.Value);
  135. else
  136. model.Tanks = mockAtgController.Tanks;
  137. return View("/Views/VeederRoot_ATG_Console_Handler/Index.cshtml", model);
  138. }
  139. //public async Task<IActionResult> TankProbeReadingsData(int? tankNumber)
  140. //{
  141. // var readings = await App.AutoTankGaugeController.GetTankProbeReadingsAsync(tankNumber);
  142. // if (readings == null || !readings.Any()) return NoContent();
  143. // //DataPointWithStringX data = new DataPointWithStringX(DateTime.Now.ToLongTimeString(), 1999);
  144. // var datas = DataPointProbeReading.From(readings.Select(r => r.Item2));
  145. // return Content(JsonConvert.SerializeObject(datas, _jsonSetting), "application/json");
  146. //}
  147. // GET: VeederRoot_ATG_Console_Handler_/Details/5
  148. public async Task<ActionResult> Details(int? tankNumber)
  149. {
  150. if (tankNumber.HasValue)
  151. {
  152. var reading = await mockAtgController.GetTankReadingAsync(tankNumber.Value);
  153. if (reading == null) return NotFound();
  154. return Ok(new[]
  155. {
  156. new TankReadingDto(){
  157. tankNumber= tankNumber.Value,
  158. Height= reading.Height,
  159. Water= reading.Water,
  160. Temperature= reading.Temperature,
  161. Volume= reading.Volume,
  162. WaterVolume= reading.WaterVolume,
  163. TimeStamp = DateTime.Now.ToString("HH:mm:ss"),
  164. }
  165. });
  166. }
  167. else
  168. {
  169. List<TankReadingDto> tankReadings = new List<TankReadingDto>();
  170. foreach (var t in mockAtgController.Tanks)
  171. {
  172. var reading = await mockAtgController.GetTankReadingAsync(t.TankNumber);
  173. if (reading == null) continue;
  174. var dto = new TankReadingDto()
  175. {
  176. tankNumber = t.TankNumber,
  177. Height = reading.Height,
  178. Water = reading.Water,
  179. Temperature = reading.Temperature,
  180. Volume = reading.Volume,
  181. WaterVolume = reading.WaterVolume,
  182. TimeStamp = DateTime.Now.ToString("HH:mm:ss"),
  183. };
  184. tankReadings.Add(dto);
  185. }
  186. return Ok(tankReadings);
  187. }
  188. }
  189. // GET: VeederRoot_ATG_Console_Handler_/Create
  190. public ActionResult Create()
  191. {
  192. return View();
  193. }
  194. // POST: VeederRoot_ATG_Console_Handler_/Create
  195. [HttpPost]
  196. [ValidateAntiForgeryToken]
  197. public ActionResult Create(IFormCollection collection)
  198. {
  199. try
  200. {
  201. // TODO: Add insert logic here
  202. return RedirectToAction(nameof(Index));
  203. }
  204. catch
  205. {
  206. return View();
  207. }
  208. }
  209. // GET: VeederRoot_ATG_Console_Handler_/Edit/5
  210. public ActionResult Edit(int id)
  211. {
  212. return View();
  213. }
  214. // POST: VeederRoot_ATG_Console_Handler_/Edit/5
  215. [HttpPost]
  216. [ValidateAntiForgeryToken]
  217. public ActionResult Edit(int id, IFormCollection collection)
  218. {
  219. try
  220. {
  221. // TODO: Add update logic here
  222. return RedirectToAction(nameof(Index));
  223. }
  224. catch
  225. {
  226. return View();
  227. }
  228. }
  229. // GET: VeederRoot_ATG_Console_Handler_/Delete/5
  230. public ActionResult Delete(int id)
  231. {
  232. return View();
  233. }
  234. // POST: VeederRoot_ATG_Console_Handler_/Delete/5
  235. [HttpPost]
  236. [ValidateAntiForgeryToken]
  237. public ActionResult Delete(int id, IFormCollection collection)
  238. {
  239. try
  240. {
  241. // TODO: Add delete logic here
  242. return RedirectToAction(nameof(Index));
  243. }
  244. catch
  245. {
  246. return View();
  247. }
  248. }
  249. }
  250. }