tankDetail.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
  2. // for details on configuring this project to bundle and minify static web assets.
  3. // Write your JavaScript code.
  4. let httpUrl = 'http://127.0.0.1:6688';
  5. let mqttUrl = 'ws://localhost:8384/mqtt';
  6. let url = '/TankDetails/';
  7. let containerDiv = 'tankcontainer';
  8. let contentDiv = 'content';
  9. let defaultFnav = 'TankOverviewFnav';
  10. let defaultPageDic = new Array();
  11. defaultPageDic['TankOverviewFnav'] = 'TankDevicesOverview';
  12. defaultPageDic['TankDetailFnav'] = 'TankDeviceDetails?index=0';
  13. defaultPageDic['AlarmHistoryFnav'] = 'AlarmHistory';
  14. defaultPageDic['FuelInventoryFnav'] = 'FuelInventoryList';
  15. let mqttoptions = {
  16. clientId: 'mqttjs_' + (Math.random() * 1000000).toString(),
  17. timeout: 5000,
  18. useSSL: false
  19. };
  20. let mqttclient = mqtt.connect(mqttUrl, mqttoptions);
  21. mqttclient.on('connect',
  22. function (res) {
  23. mqttclient.subscribe('/sys/VeederRoot_ATG_Console_Tcp/VeederRoot_ATG_Console.Handler/thing/service/GetTanksAsync_reply',
  24. function (err) {
  25. if (err) {
  26. console.log(err);
  27. }
  28. });
  29. mqttclient.subscribe('/sys/VeederRoot_ATG_Console_Tcp/VeederRoot_ATG_Console.Handler/thing/service/GetTankReadingAsync_reply',
  30. function (err) {
  31. if (err) {
  32. console.log(err);
  33. }
  34. });
  35. mqttclient.subscribe('/sys/VeederRoot_ATG_Console_Tcp/VeederRoot_ATG_Console.Handler/thing/service/GetTankAlarmAsync_reply',
  36. function (err) {
  37. if (err) {
  38. console.log(err);
  39. }
  40. });
  41. mqttclient.subscribe('/sys/VeederRoot_ATG_Console_Tcp/VeederRoot_ATG_Console.Handler/thing/service/GetTankInventoryAsync_reply',
  42. function (err) {
  43. if (err) {
  44. console.log(err);
  45. }
  46. });
  47. mqttclient.subscribe('/sys/VeederRoot_ATG_Console_Tcp/VeederRoot_ATG_Console.Handler/thing/service/GetTankDeliveryAsync_reply',
  48. function (err) {
  49. if (err) {
  50. console.log(err);
  51. }
  52. });
  53. });
  54. mqttclient.on('error',
  55. function (err) {
  56. console.log(err);
  57. mqttclient.end();
  58. });
  59. mqttclient.on('message',
  60. function (topic, message) {
  61. let jsonObj = JSON.parse(message.toString())
  62. if (jsonObj) {
  63. OnReply(topic, jsonObj);
  64. } else {
  65. console.log(topic, message.toString());
  66. }
  67. });
  68. function Publish1(path) {
  69. mqttclient.publish(path, '[]');
  70. }
  71. function Publish2(value, path) {
  72. mqttclient.publish(path, value);
  73. }
  74. function Publish(name, value, path) {
  75. let parameters = '{"Parameters": [{"Name": "';
  76. parameters += name;
  77. parameters += '", "Value": "';
  78. parameters += value.replace(new RegExp('"', 'gm'), '\\"') + '"}]}';
  79. //console.log(parameters);
  80. mqttclient.publish(path, parameters);
  81. }
  82. function OnloadIndex() {
  83. RenderDiv(url + defaultFnav, containerDiv, url + defaultPageDic[defaultFnav], contentDiv);
  84. }
  85. function RenderDiv(containerUrl, containerDiv, contentUrl, contentDiv) {
  86. $.ajax({
  87. url: containerUrl,
  88. success: function (result) {
  89. $('#' + containerDiv).html(result);
  90. if (contentUrl) {
  91. RenderDiv(contentUrl, contentDiv, null, null);
  92. }
  93. },
  94. error: function (msg) {
  95. console.log(msg);
  96. }
  97. })
  98. }
  99. function RenderContainer(currentId, fnav) {
  100. RenderDiv(url + currentId, containerDiv, url + defaultPageDic[currentId], contentDiv);
  101. ToggleClass(currentId, fnav);
  102. }
  103. function RenderIndexContainer(currentId, fnav, index) {
  104. RenderDiv(url + currentId, containerDiv, '/TankDetails/TankDeviceDetails?index=' + index, contentDiv);
  105. ToggleClass(currentId, fnav);
  106. }
  107. function RenderContent(currentId, rnav) {
  108. RenderDiv(url + currentId, contentDiv, null, null);
  109. ToggleClass(currentId, rnav);
  110. }
  111. function ToggleClass(currentId, array) {
  112. for (let i = 0; i < array.length; i++) {
  113. let classNames = array[i].className;
  114. let classImage = classNames.split(' ')[0] + '-image';
  115. let id = array[i].id;
  116. if (classNames.length > 1) {
  117. $('#' + id).removeClass(classImage);
  118. }
  119. if (currentId == id) {
  120. $('#' + id).addClass(classImage);
  121. }
  122. }
  123. }
  124. function InvokeHttpGet(apiUrl, callback) {
  125. $.ajax({
  126. url: httpUrl + apiUrl,
  127. contentType: 'application/json',
  128. success: function (res) {
  129. callback(res);
  130. },
  131. error: function (msg) {
  132. console.log(msg)
  133. }
  134. })
  135. }
  136. function InvokeHttpPost(apiUrl, reqdata, callback) {
  137. $.ajax({
  138. url: httpUrl + apiUrl,
  139. contentType: 'application/json',
  140. data: JSON.stringify(reqdata),
  141. type: 'post',
  142. success: function (res) {
  143. callback(res);
  144. },
  145. error: function (msg) {
  146. console.log(msg);
  147. }
  148. })
  149. }