tankDetail.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 basicUrl = 'http://localhost:8384';
  7. var href = window.location.href;
  8. var path = window.location.pathname;
  9. let basicUrl = href.slice(0, href.indexOf(path));
  10. //"ws://localhost:8384/mqtt"
  11. let mqttUrl = "ws:" + basicUrl.slice(basicUrl.indexOf("//")) + "/mqtt";
  12. let apis = new Map();
  13. let containerDiv = 'tankcontainer';
  14. let contentDiv = 'content';
  15. let defaultFnav = 'TankOverviewFnav';
  16. let defaultPageDic = new Array();
  17. defaultPageDic['TankOverviewFnav'] = 'TankDevicesOverview';
  18. defaultPageDic['TankDetailFnav'] = 'TankDeviceDetails?index=0';
  19. defaultPageDic['AlarmHistoryFnav'] = 'AlarmHistory';
  20. defaultPageDic['FuelInventoryFnav'] = 'FuelInventoryList';
  21. defaultPageDic['SensorDataFnav'] = 'SensorDataDetails';
  22. defaultPageDic['ListTabFnav'] = 'ListTabPage';
  23. defaultPageDic['ChartInquiryFnav'] = 'ChartInquiryPage';
  24. defaultPageDic['NozzlesFnav'] = 'Nozzles';
  25. let mqttoptions = {
  26. clientId: 'mqttjs_' + (Math.random() * 1000000).toString(),
  27. timeout: 5000,
  28. useSSL: false
  29. };
  30. let mqttclient = mqtt.connect(mqttUrl, mqttoptions);
  31. mqttclient.on('connect',
  32. function (res) {
  33. subscribeEvents();
  34. });
  35. mqttclient.on('error',
  36. function (err) {
  37. console.log(err);
  38. mqttclient.end();
  39. });
  40. mqttclient.on('message',
  41. function (topic, message) {
  42. let jsonObj = JSON.parse(message.toString())
  43. console.log("mqtt client on message:" + typeof (OnReply));
  44. if (jsonObj && typeof (OnReply) === "function") {
  45. OnReply(apis, topic, jsonObj);
  46. } else {
  47. console.log(topic, message.toString());
  48. }
  49. });
  50. function subscribeEvents() {
  51. if (!mqttclient.connected || apis.size === 0) {
  52. console.log("subscribeEvents: mqttclient is not connected or haven't got APIS");
  53. return;
  54. }
  55. mqttclient.subscribe(apis.has("GetTanksAsync_Reply") ? apis.get("GetTanksAsync_Reply") : '/sys/VeederRoot_ATG_Console_Tcp/VeederRoot_ATG_Console.Handler/thing/service/GetTanksAsync_Reply',
  56. function (err) {
  57. if (!err) {
  58. console.log("Subscribe GetTanksAsync_Reply successfully");
  59. };
  60. });
  61. mqttclient.subscribe(apis.has("GetTankReadingAsync_Reply") ? apis.get("GetTankReadingAsync_Reply") : '/sys/VeederRoot_ATG_Console_Tcp/VeederRoot_ATG_Console.Handler/thing/service/GetTankReadingAsync_Reply',
  62. function (err) {
  63. if (!err) {
  64. console.log("Subscribe GetTankReadingAsync_Reply successfully");
  65. };
  66. });
  67. mqttclient.subscribe(apis.has("GetTankAlarmAsync_Reply") ? apis.get("GetTankAlarmAsync_Reply") : '/sys/VeederRoot_ATG_Console_Tcp/VeederRoot_ATG_Console.Handler/thing/service/GetTankAlarmAsync_Reply',
  68. function (err) {
  69. if (!err) {
  70. console.log("Subscribe GetTankAlarmAsync_Reply successfully");
  71. };
  72. });
  73. mqttclient.subscribe(apis.has("GetTankInventoryAsync_Reply") ? apis.get("GetTankInventoryAsync_Reply") : '/sys/VeederRoot_ATG_Console_Tcp/VeederRoot_ATG_Console.Handler/thing/service/GetTankInventoryAsync_Reply',
  74. function (err) {
  75. if (!err) {
  76. console.log("Subscribe GetTankInventoryAsync_Reply successfully");
  77. };
  78. });
  79. mqttclient.subscribe(apis.has("GetTankDeliveryAsync_Reply") ? apis.get("GetTankDeliveryAsync_Reply") : '/sys/VeederRoot_ATG_Console_Tcp/VeederRoot_ATG_Console.Handler/thing/service/GetTankDeliveryAsync_Reply',
  80. function (err) {
  81. if (!err) {
  82. console.log("Subscribe GetTankDeliveryAsync_Reply successfully");
  83. };
  84. });
  85. mqttclient.subscribe(apis.has("GetPressureAsync_Reply") ? apis.get("GetPressureAsync_Reply") : '/sys/VeederRoot_ATG_Console_Tcp/VeederRoot_ATG_Console.Handler/thing/service/GetPressureAsync_Reply',
  86. function (err) {
  87. if (!err) {
  88. console.log("Subscribe GetPressureAsync_Reply successfully");
  89. };
  90. });
  91. mqttclient.subscribe(apis.has("GetVRBoardNozzleTrxFlowDatas_Reply") ? apis.get("GetVRBoardNozzleTrxFlowDatas_Reply") : '/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/service/GetVRBoardNozzleTrxFlowDatas_Reply',
  92. function (err) {
  93. if (!err) {
  94. console.log("Subscribe GetVRBoardNozzleTrxFlowDatas_Reply successfully");
  95. };
  96. });
  97. mqttclient.subscribe(apis.has("GetVaporRecoveryConfig_Reply") ? apis.get("GetVaporRecoveryConfig_Reply") : '/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/service/GetVaporRecoveryConfig_Reply',
  98. function (err) {
  99. if (!err) {
  100. console.log("Subscribe GetVaporRecoveryConfig_Reply successfully");
  101. };
  102. });
  103. }
  104. function Publish1(path, apiname) {
  105. path = apis.has(apiname) ? apis.get(apiname) : (path + apiname)
  106. mqttclient.publish(path, '[]');
  107. }
  108. function Publish2(value, path, apiname) {
  109. path = apis.has(apiname) ? apis.get(apiname) : (path + apiname)
  110. mqttclient.publish(path, value);
  111. }
  112. function Publish(name, value, path) {
  113. let parameters = '{"Parameters": [{"Name": "';
  114. parameters += name;
  115. parameters += '", "Value": "';
  116. parameters += value.replace(new RegExp('"', 'gm'), '\\"') + '"}]}';
  117. //console.log(parameters);
  118. mqttclient.publish(path, parameters);
  119. }
  120. function OnloadIndex(controller, defaultPage = 'TankOverviewFnav') {
  121. defaultFnav = defaultPage;
  122. RenderDiv(controller + defaultFnav, containerDiv, controller + defaultPageDic[defaultFnav], contentDiv);
  123. }
  124. function RenderDiv(containerUrl, containerDiv, contentUrl, contentDiv) {
  125. $.ajax({
  126. url: containerUrl,
  127. success: function (result) {
  128. $('#' + containerDiv).html(result);
  129. if (contentUrl) {
  130. RenderDiv(contentUrl, contentDiv, null, null);
  131. }
  132. },
  133. error: function (msg) {
  134. console.log(msg);
  135. }
  136. })
  137. }
  138. function RenderContainer(ontroller, currentId, fnav) {
  139. RenderDiv(ontroller + currentId, containerDiv, ontroller + defaultPageDic[currentId], contentDiv);
  140. ToggleClass(currentId, fnav);
  141. }
  142. function RenderIndexContainer(ontroller, currentId, fnav, index) {
  143. RenderDiv(ontroller + currentId, containerDiv, ontroller + 'TankDeviceDetails?index=' + index, contentDiv);
  144. ToggleClass(currentId, fnav);
  145. }
  146. function RenderContent(ontroller, currentId, rnav) {
  147. RenderDiv(ontroller + currentId, contentDiv, null, null);
  148. ToggleClass(currentId, rnav);
  149. }
  150. function ToggleClass(currentId, array) {
  151. for (let i = 0; i < array.length; i++) {
  152. let classNames = array[i].className;
  153. let classImage = classNames.split(' ')[0] + '-image';
  154. let id = array[i].id;
  155. if (classNames.length > 1) {
  156. $('#' + id).removeClass(classImage);
  157. }
  158. if (currentId == id) {
  159. $('#' + id).addClass(classImage);
  160. }
  161. }
  162. }
  163. function InvokeHttpGet(apiUrl, callback) {
  164. $.ajax({
  165. url: httpUrl + apiUrl,
  166. contentType: 'application/json',
  167. success: function (res) {
  168. callback(res);
  169. },
  170. error: function (msg) {
  171. console.log(msg)
  172. }
  173. })
  174. }
  175. function InvokeHttpPost(apiUrl, reqdata, callback) {
  176. $.ajax({
  177. url: httpUrl + apiUrl,
  178. contentType: 'application/json',
  179. data: JSON.stringify(reqdata),
  180. type: 'post',
  181. success: function (res) {
  182. callback(res);
  183. },
  184. error: function (msg) {
  185. console.log(msg);
  186. }
  187. })
  188. }
  189. function ShowMeAPIS(parameter, callBack) {
  190. var serviceUrl = basicUrl + "/u/?apitype=service&an=ShowMeApi&pn=ProcessorsDispatcher&en=Edge.Core.Processor.Dispatcher.DefaultDispatcher";
  191. $.ajax({
  192. url: serviceUrl,
  193. datatype: "application/json",
  194. type: 'post',
  195. contentType: "application/json;charset=utf-8;",
  196. data: JSON.stringify(parameter),
  197. beforeSend: function () {
  198. },
  199. success: function (data) {
  200. console.log(data);
  201. data.forEach(function (d) {
  202. if (d.Path === null) return;
  203. if (apis.has(d.ApiName)) {
  204. apis.delete(d.ApiName);
  205. }
  206. apis.set(d.ApiName, d.Path.slice(d.Path.length - 1) === "+" ? d.Path.slice(0, d.Path.length - 1) : d.Path);
  207. });
  208. subscribeEvents();
  209. if (callBack !== null) callBack();
  210. },
  211. error: function (err) {
  212. console.log(err);
  213. if (callBack !== null) callBack();
  214. }
  215. });
  216. }
  217. function GetDataByAPIS(apiName, parameter, method, callBack) {
  218. if (apis.size === 0 || !apis.has(apiName)) return;
  219. $.ajax({
  220. url: basicUrl + apis.get(apiName),
  221. datatype: "application/json",
  222. type: method,
  223. contentType: "application/json;charset=utf-8;",
  224. data: JSON.stringify(parameter),
  225. beforeSend: function () {
  226. },
  227. success: function (data) {
  228. console.log(data);
  229. if (callBack !== null) callBack(data);
  230. },
  231. error: function (err) {
  232. console.log(err);
  233. if (callBack !== null) callBack(err);
  234. }
  235. });
  236. }