Index.cshtml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. @model Applications.UniversalApi_WebConsole_App.Models.VeederRoot_ATG_Console_Handler_Models.AtgController
  2. @{
  3. ViewData["Title"] = "Auto Tank Gauge System";
  4. Layout = null;
  5. }
  6. <script>
  7. function clearCanvas(canvas) {
  8. var cxt = canvas.getContext("2d");
  9. cxt.clearRect(0, 0, canvas.width, canvas.height);
  10. }
  11. /*
  12. * draw a circle in canvas by specify its parameters.
  13. * @@constructor
  14. * @@param {string} canvas - all things will draw on.
  15. * @@param {string} x - center x coordinate value of the circle.
  16. * @@param {string} y - center y coordinate value of the circle.
  17. * @@param {string} r - radius of the circle.
  18. * @@param {string} productDatas - [{height: int-productHeight, color: string-colorValue, text: string-displayText}].
  19. * @@param {string} horiLineDatas - [{height: int-lineHeightValue, strokeColor: string-colorValue, text: string-displayText}].
  20. */
  21. function drawTank(canvas, x, y, r, productDatas, horiLineDatas) {
  22. var context = canvas.getContext('2d');
  23. /*draw a circle for a tank.*/
  24. context.beginPath();
  25. context.setLineDash([]);
  26. context.arc(x, y, r, 0, 2 * Math.PI, false);
  27. context.closePath();
  28. context.lineWidth = 1;
  29. context.fillStyle = 'white';
  30. context.fill();
  31. context.strokeStyle = '#550000';
  32. context.stroke();
  33. productDatas.forEach(function (data) {
  34. //console.log('height: ' + data.height + ' with color: ' + data.color);
  35. var temp_tri_w = Math.sqrt(Math.pow(r, 2) - Math.pow(r - data.height, 2));
  36. var temp_tri_h = r - data.height;
  37. //console.log('temp_tri_h: '+temp_tri_h+', cos: '+temp_tri_h/r+', acos: '+Math.acos(temp_tri_h/r));
  38. var sectorArc = Math.acos(temp_tri_h / r) * 2;///360 * 2* Math.PI;
  39. //console.log('height: ' + data.height + ', sectorArc: ' + sectorArc);
  40. /*draw the sector area*/
  41. context.beginPath();
  42. context.arc(x, y, r, (Math.PI - sectorArc) / 2, (Math.PI - sectorArc) / 2 + sectorArc, false);
  43. //var compensate_pixel_count = 10;
  44. line_start_x = x - temp_tri_w;//+compensate_pixel_count;
  45. line_end_x = x - temp_tri_w + temp_tri_w * 2;//-compensate_pixel_count;
  46. line_start_y = y + r - data.height;
  47. line_end_y = y + r - data.height;
  48. context.moveTo(line_start_x, line_start_y);
  49. context.lineTo(line_end_x, line_end_y);
  50. //context.strokeStyle = '#a4d3ff';
  51. //context.stroke();
  52. context.closePath();
  53. context.lineWidth = 1;
  54. context.fillStyle = data.color;
  55. context.fill();
  56. context.font = 'Bold 14px Arial';
  57. context.textAlign = 'center';
  58. context.textBaseline = 'middle';
  59. context.fillStyle = 'black'; // a color name or by using rgb/rgba/hex values
  60. context.fillText(data.text, line_start_x - 10, line_start_y); // text and position
  61. // context.strokeStyle = '#550000';
  62. // context.stroke();
  63. });
  64. horiLineDatas.forEach(function (data) {
  65. if (data.height > r * 2) {
  66. console.error('horiLine length should not exceed r*2, but now is ' + data.height);
  67. return;
  68. }
  69. var coordinate_height_y = r * 2 - data.height + y - r;
  70. //圆的标准方程(x-a)²+(y-b)²=r²
  71. var comp = Math.sqrt(Math.pow(r, 2) - Math.pow(coordinate_height_y - y, 2));
  72. line_start_x = -comp + x;
  73. line_end_x = comp + x;
  74. //console.log('horiLineDatas, comp is ' + comp + ', line_start_x is' + line_start_x
  75. // + ', line_end_x is ' + line_end_x);
  76. context.beginPath();
  77. context.lineWidth = 1;
  78. context.setLineDash([3, 2]);/*dashes are 3px and spaces are 2px*/
  79. context.moveTo(line_start_x, coordinate_height_y);
  80. context.lineTo(line_end_x, coordinate_height_y);
  81. context.strokeStyle = data.strokeColor;//'#a4d3ff';
  82. context.stroke();
  83. context.font = 'italic 10px Arial';
  84. context.textAlign = 'center';
  85. context.textBaseline = 'middle';
  86. context.fillStyle = 'gray'; // a color name or by using rgb/rgba/hex values
  87. context.fillText(data.text, line_start_x + (line_end_x - line_start_x) / 2, coordinate_height_y - 4); // text and position
  88. //context.closePath();
  89. });
  90. }
  91. </script>
  92. <script src="~/js/Chart.bundle.js"></script>
  93. <script src="~/js/jquery-3.4.1.min.js"></script>
  94. @*<div class="text-center">
  95. <h3 class="display-4">This is the ATG Console Web App</h3>
  96. </div>*@
  97. @if (Model == null || Model.Tanks == null)
  98. {
  99. <h1>The console is <b>NOT ready</b> for connection, will auto reload once it's done...</h1>
  100. <script>
  101. setInterval(function () { location.reload() }, 3000);
  102. </script>
  103. }
  104. @if (Model.Tanks != null && Model.Tanks.Any())
  105. @foreach (var tank in Model.Tanks)
  106. {
  107. var atgConsoleLengthUnitDisplayStr = "mm";
  108. var atgConsoleVolumeUnitDisplayStr = "L";
  109. if (Model.SystemUnit == Edge.Core.IndustryStandardInterface.ATG.SystemUnit.US)
  110. atgConsoleLengthUnitDisplayStr = "inches";
  111. else if (Model.SystemUnit == Edge.Core.IndustryStandardInterface.ATG.SystemUnit.ImperialGallons)
  112. atgConsoleLengthUnitDisplayStr = "yard";
  113. <div style="border-color:gray;border-style:solid;border-width:thin;padding:4px">
  114. <p style="margin-top:@(10*(tank.TankNumber-1))px">
  115. Tank No.: <b>@tank.TankNumber</b>, has product: <b>@(tank.Product?.ProductLabel ?? "")(code: @(tank.Product?.ProductCode ?? ""))</b>
  116. is in tank state: <b>@(tank.State)</b>, Diameter: @(tank.Diameter ?? -1)@atgConsoleLengthUnitDisplayStr
  117. </p>
  118. <div style="display:flex;">
  119. <!-- Chart.js uses its parent container to update the canvas render and display sizes. However, this method requires the container to be relatively positioned and dedicated to the chart canvas only. -->
  120. <div class="chart-container" style="position: relative;height:@(100/Model.Tanks.Count())vh; width:100vh;">
  121. <canvas id="tank_@(tank.TankNumber)_Height_Water_Chart" aria-label="ThisIsTheCanvas" style="">Your browser does not support the canvas element.</canvas>
  122. </div>
  123. <canvas id="tank_@(tank.TankNumber)_Height_Water_SectionalView" aria-label="ThisIsTheCanvas"
  124. style="height:@(100/Model.Tanks.Count())vh; width:50vh;">
  125. Your browser does not support the canvas element.
  126. </canvas>
  127. </div>
  128. </div>
  129. <script>
  130. var ctx_tank_@(tank.TankNumber)_Height_Water_Chart = document.getElementById('tank_@(tank.TankNumber)_Height_Water_Chart');
  131. var tank_@(tank.TankNumber)_Height_Water_Chart = new Chart(ctx_tank_@(tank.TankNumber)_Height_Water_Chart, {
  132. type: 'line',
  133. data: {
  134. //labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
  135. datasets: [{
  136. label: '油高',
  137. //data: [1761.84, 1661.84, 1561.84, 1461.84, 1361.84, 761.84, 50.84],
  138. backgroundColor: 'rgba(204, 204, 0, 0.2)',
  139. borderColor: [
  140. 'rgba(255, 99, 132, 1)',
  141. 'rgba(54, 162, 235, 1)',
  142. 'rgba(255, 206, 86, 1)',
  143. 'rgba(75, 192, 192, 1)',
  144. 'rgba(153, 102, 255, 1)',
  145. 'rgba(255, 159, 64, 1)'
  146. ],
  147. borderWidth: 1
  148. },
  149. {
  150. label: '水高',
  151. //data: [61.84, 90.15, 61.84, 120.84, 150.84, 200.84, 350.84],
  152. backgroundColor: 'rgba(0, 255, 255, 0.2)',
  153. borderColor: [
  154. 'rgba(255, 99, 132, 1)',
  155. 'rgba(54, 162, 235, 1)',
  156. 'rgba(255, 206, 86, 1)',
  157. 'rgba(75, 192, 192, 1)',
  158. 'rgba(153, 102, 255, 1)',
  159. 'rgba(255, 159, 64, 1)'
  160. ],
  161. borderWidth: 1
  162. }]
  163. },
  164. options: {
  165. maintainAspectRatio: false,
  166. scales: {
  167. yAxes: [{
  168. ticks: {
  169. beginAtZero: true,
  170. // Include a 'mm' sign in the values
  171. callback: function (value, index, values) {
  172. return value + '@atgConsoleLengthUnitDisplayStr';
  173. }
  174. }
  175. }]
  176. }
  177. }
  178. });
  179. //var canvas = document.getElementById('tank_@(tank.TankNumber)_Height_Water_SectionalView');
  180. </script>
  181. }
  182. <script>
  183. var productHeight = 0;
  184. var waterHeight = 0;
  185. var maxChartStackDataCount = 30;
  186. function add_Height_Water_Data(__chart, tankNumber) {
  187. $.getJSON('Details/?tankNumber=' + tankNumber, function (datas) {
  188. if (datas == null) {
  189. console.log('response datas is null'); return;
  190. }
  191. //console.log(datas);
  192. datas.forEach(function (d) {
  193. var chartName = 'tank_' + d.tankNumber + '_Height_Water_Chart';
  194. var chart = window[chartName];
  195. if (chart == null) return;
  196. //console.log(chart);
  197. /*
  198. * purge line chart data
  199. */
  200. chart.data.datasets.forEach((purgeDataset) => {
  201. if (purgeDataset.data.length > maxChartStackDataCount) {
  202. //console.log(chartName + '.data in datasets[' + purgeDataset.label + '] has its length of ' + purgeDataset.data.length + ', will truncate');
  203. purgeDataset.data = purgeDataset.data.slice(purgeDataset.data.length - maxChartStackDataCount, purgeDataset.data.length);
  204. //chart.data.labels = chart.data.labels.slice(chart.data.labels.length - maxChartStackDataCount, chart.data.labels.length);
  205. //console.log(' ' + chartName + ' has truncated and the dataset.data.length now is ' + purgeDataset.data.length);
  206. //for (var i = 0; i < dataset.data.length - maxChartStackDataCount; i++)
  207. //dataset.data.pop();
  208. }
  209. });
  210. if (chart.data.labels.length > maxChartStackDataCount) {
  211. chart.data.labels = chart.data.labels.slice(chart.data.labels.length - maxChartStackDataCount, chart.data.labels.length);
  212. }
  213. //each batch will adding a new x axis value, most likely a time str.
  214. chart.data.labels.push(d.timeStamp);
  215. chart.data.datasets.forEach((dataset) => {
  216. if (dataset.label === "油高") {
  217. dataset.data.push(d.height);
  218. productHeight = d.height;
  219. }
  220. else if (dataset.label === "水高") {
  221. dataset.data.push(d.water);
  222. waterHeight = d.water;
  223. } else return;
  224. chart.update({
  225. duration: 600,
  226. easing: 'easeOutBounce'
  227. });
  228. var sectionalViewCanvas = document.getElementById('tank_' + d.tankNumber + '_Height_Water_SectionalView');
  229. this.clearCanvas(sectionalViewCanvas);
  230. this.drawTank(sectionalViewCanvas, 150, 80, 60, [
  231. { height: productHeight / 1800 * 60 * 2, color: 'orange', text: '92#' },
  232. { height: waterHeight / 1800 * 60 * 2, color: '#00ced1', text: 'water' }],
  233. [{ height: 90, strokeColor: 'red', text: 'fuel high' },
  234. { height: 30, strokeColor: 'blue', text: 'water high' }]);
  235. });
  236. });
  237. })
  238. }
  239. function removeData(chart) {
  240. chart.data.labels.pop();
  241. chart.data.datasets.forEach((dataset) => {
  242. dataset.data.pop();
  243. });
  244. chart.update();
  245. }
  246. setInterval(function () { add_Height_Water_Data(null, null) }, 1500);
  247. </script>