pumps.cshtml 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. @*
  2. For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
  3. *@
  4. @{
  5. Layout = null;
  6. }
  7. @model Edge.WebHost.Models.OnlineMonitor.OnlineMonitorInfo
  8. @{
  9. int maxPumpNumberInOnePage = 8;
  10. int screenWidth = 1280;
  11. int mainviewHeight = 570;
  12. string pumpViewImgPrePath = "images/onlineMonitor/pumpView/";
  13. }
  14. <script>
  15. var currentPage = 1;
  16. var totalPage = 1;
  17. var warningNozzles = new Array();
  18. var alarmNozzles = new Array();
  19. function GeneratePumpView(message) {
  20. if (message == null || message.data == null) {
  21. console.log("Invalid message got in pump view");
  22. return;
  23. }
  24. var pumpsDiv = document.getElementById("Pumps");
  25. if (pumpsDiv == null) {
  26. console.log("pumps div is null");
  27. return;
  28. }
  29. if (pumpsDiv.childNodes.length > 0) {
  30. var pl = pumpsDiv.childNodes.length;
  31. for (var cp = 0; cp < pl; cp++) {
  32. var c = pumpsDiv.childNodes[0];
  33. pumpsDiv.removeChild(c);
  34. }
  35. }
  36. if (warningNozzles.length > 0) {
  37. warningNozzles.splice(0,warningNozzles.length);
  38. }
  39. if (alarmNozzles.length > 0) {
  40. alarmNozzles.splice(0,alarmNozzles.length);
  41. }
  42. var data = message.data;
  43. var pumpMap = new Map();
  44. var currentDispenserId = 0;
  45. var nozzlesArray = new Array();
  46. data.forEach(function (d) {
  47. if (currentDispenserId === 0) {
  48. currentDispenserId = d.SiteLevelDispenserId;
  49. nozzlesArray.push(d);
  50. } else if(currentDispenserId !== d.SiteLevelDispenserId) {
  51. pumpMap.set(currentDispenserId, new Array().concat(nozzlesArray));
  52. currentDispenserId = d.SiteLevelDispenserId;
  53. nozzlesArray.splice(0,nozzlesArray.length);
  54. nozzlesArray.push(d);
  55. } else {
  56. currentDispenserId = d.SiteLevelDispenserId;
  57. //nozzlesArray.clear();
  58. nozzlesArray.push(d);
  59. }
  60. if (d.VrState === "WARNING") {
  61. warningNozzles.push(d.SiteLevelDispenserId);
  62. } else if (d.VrState === "ALARM") {
  63. alarmNozzles.push(d.SiteLevelDispenserId);
  64. }
  65. });
  66. if (nozzlesArray.length > 0) {
  67. pumpMap.set(currentDispenserId, new Array().concat(nozzlesArray));
  68. nozzlesArray.splice(0,nozzlesArray.length);
  69. }
  70. totalPage = Math.ceil(pumpMap.size/@maxPumpNumberInOnePage);
  71. var dataLength =
  72. (pumpMap.size - @maxPumpNumberInOnePage * (currentPage - 1)) > @maxPumpNumberInOnePage
  73. ? @maxPumpNumberInOnePage
  74. : (pumpMap.size - @maxPumpNumberInOnePage * (currentPage - 1));
  75. var showedPumpCount = 0;
  76. var currentPumpMapIndex = 0;
  77. pumpMap.forEach(function (value, key) {
  78. if (!(showedPumpCount < dataLength)) {
  79. return;
  80. }
  81. currentPumpMapIndex++;
  82. if (@maxPumpNumberInOnePage*(currentPage-1)> (currentPumpMapIndex-1)) {
  83. return;
  84. }
  85. showedPumpCount++;
  86. var dispenserId = key;
  87. var nozzleCount = value.length;
  88. //如果只有单数个油枪时,左侧比右侧多一个,所以在生成页面时注意不能越界了
  89. var nozzleShouldBeDisplayed = Math.ceil(nozzleCount / 2);
  90. var currentIndex = 0;
  91. //pump block div
  92. var pumpBlockDiv = document.createElement("div");
  93. pumpBlockDiv.id = "Pump" + dispenserId + "_block";
  94. pumpBlockDiv.style = "position: relative; margin-left: 42px; margin-top: 42px; float: left; width: 265px; height: 222px;";
  95. //
  96. for (var cl = 0; cl < 3; cl++) {
  97. if (cl === 1) {
  98. var pumpDiv = document.createElement("div");
  99. pumpDiv.id = "pump" + dispenserId;
  100. pumpDiv.style =
  101. "position: relative;margin: 0px;float: left;width: 46%;height: 100%;text-align: center;";
  102. var imgPump = document.createElement("img");
  103. imgPump.id = "pump" + dispenserId + "_img";
  104. imgPump.style = "position: relative; width: 113px; height: 222px; visibility: visible";
  105. imgPump.src = "@(pumpViewImgPrePath)pump.png";
  106. var pumpIdSpan = document.createElement("span");
  107. pumpIdSpan.id = "pump_id_" + dispenserId;
  108. pumpIdSpan.style = "position: absolute; left: 0px; top: 30%; width: 100%; text-align: center; font-size: 22px;";
  109. pumpIdSpan.innerHTML = dispenserId;
  110. pumpDiv.appendChild(imgPump);
  111. pumpDiv.appendChild(pumpIdSpan);
  112. pumpBlockDiv.appendChild(pumpDiv);
  113. } else {
  114. var nozzleBlockDiv = document.createElement("div");
  115. nozzleBlockDiv.id = "pump" + dispenserId + "_Nozzle_block" + cl;
  116. nozzleBlockDiv.style = "position: relative; margin: 0px; float: left; width: 27%; height: 100%;";
  117. for (var n = 0; n < nozzleShouldBeDisplayed; n++) {
  118. if (currentIndex+1 > value.length) {
  119. break;
  120. }
  121. var itemNormalVisibility = (value[currentIndex].VrState === "NORMAL") ? "visible" : "hidden";
  122. var itemWarningVisibility = (value[currentIndex].VrState === "WARNING") ? "visible" : "hidden";
  123. //var itemNormalVisibility = "visible";
  124. //var itemWarningVisibility = "visible";
  125. var itemAlarmVisibility = (value[currentIndex].VrState === "ALARM") ? "visible" : "hidden";
  126. var itemDisconnectVisibility = (value[currentIndex].VrState === "DISCONNETED" || value[currentIndex].VrState === "NO_VR") ? "visible" : "hidden";
  127. //var itemAlarmVisibility = "visible";
  128. var idPreStr = "pump" + dispenserId + "_nozzle" + value[currentIndex].SiteLevelNozzleId;
  129. var nozzleDiv = document.createElement("div");
  130. nozzleDiv.id = idPreStr;
  131. nozzleDiv.style = "position: relative; width: 100%; height:" + 100 / nozzleShouldBeDisplayed + "%;";
  132. if (cl === 0) {
  133. var nozzleNumberSpan = document.createElement("span");
  134. nozzleNumberSpan.style = "float: left; width: 35%; height: 100%; font-size: 22px; text-align: right; line-height:" + 222 / nozzleShouldBeDisplayed + "px;";
  135. nozzleNumberSpan.innerHTML = value[currentIndex].SiteLevelNozzleId;
  136. var imgSpan = document.createElement("span");
  137. imgSpan.style = "float: left; width: 65%; height: 100%;";
  138. var imgNormal = document.createElement("img");
  139. imgNormal.id = idPreStr + "_normal";
  140. imgNormal.src = "@(pumpViewImgPrePath)normal.png";
  141. imgNormal.style = "position: absolute; top:" + ((222 / nozzleShouldBeDisplayed - 38) / 2) + "px; right: 0px;visibility:" + itemNormalVisibility + ";";
  142. var imgWarning = document.createElement("img");
  143. imgWarning.id = idPreStr + "_warning";
  144. imgWarning.src = "@(pumpViewImgPrePath)warning.png";
  145. imgWarning.style = "position: absolute; top:" + ((222 / nozzleShouldBeDisplayed - 38) / 2) + "px; right: 0px;visibility:" + itemWarningVisibility + ";";
  146. var imgAlarm = document.createElement("img");
  147. imgAlarm.id = idPreStr + "_alarm";
  148. imgAlarm.src = "@(pumpViewImgPrePath)alarm.png";
  149. imgAlarm.style = "position: absolute; top:" + ((222 / nozzleShouldBeDisplayed - 38) / 2) + "px; right: 0px;visibility:" + itemAlarmVisibility + ";";
  150. var imgDisconnect = document.createElement("img");
  151. imgDisconnect.id = idPreStr + "_disconnected";
  152. imgDisconnect.src = "@(pumpViewImgPrePath)PumpViewDisconnected.png";
  153. imgDisconnect.style = "position: absolute; top:" + ((222 / nozzleShouldBeDisplayed - 38) / 2) + "px; right: 0px;visibility:" + itemDisconnectVisibility + ";";
  154. imgSpan.appendChild(imgNormal);
  155. imgSpan.appendChild(imgWarning);
  156. imgSpan.appendChild(imgAlarm);
  157. imgSpan.appendChild(imgDisconnect);
  158. nozzleDiv.appendChild(nozzleNumberSpan);
  159. nozzleDiv.appendChild(imgSpan);
  160. }
  161. else if (cl === 2) {
  162. var imgSpan2 = document.createElement("span");
  163. imgSpan2.style = "float: left; width: 65%; height: 100%;";
  164. var imgNormal2 = document.createElement("img");
  165. imgNormal2.id = idPreStr + "_normal";
  166. imgNormal2.src = "@(pumpViewImgPrePath)normal.png";
  167. imgNormal2.style = "position: absolute; top:" + ((222 / nozzleShouldBeDisplayed - 38) / 2) + "px;visibility:" + itemNormalVisibility + ";";
  168. var imgWarning2 = document.createElement("img");
  169. imgWarning2.id = idPreStr + "_warning";
  170. imgWarning2.src = "@(pumpViewImgPrePath)warning.png";
  171. imgWarning2.style = "position: absolute; top:" + ((222 / nozzleShouldBeDisplayed - 38) / 2) + "px;visibility:" + itemWarningVisibility + ";";
  172. var imgAlarm2 = document.createElement("img");
  173. imgAlarm2.id = idPreStr + "_alarm";
  174. imgAlarm2.src = "@(pumpViewImgPrePath)alarm.png";
  175. imgAlarm2.style = "position: absolute; top:" + ((222 / nozzleShouldBeDisplayed - 38) / 2) + "px;visibility:" + itemAlarmVisibility + ";";
  176. var imgDisconnect2 = document.createElement("img");
  177. imgDisconnect2.id = idPreStr + "_disconnected";
  178. imgDisconnect2.src = "@(pumpViewImgPrePath)PumpViewDisconnected.png";
  179. imgDisconnect2.style = "position: absolute; top:" + ((222 / nozzleShouldBeDisplayed - 38) / 2) + "px;visibility:" + itemDisconnectVisibility + ";";
  180. imgSpan2.appendChild(imgNormal2);
  181. imgSpan2.appendChild(imgWarning2);
  182. imgSpan2.appendChild(imgAlarm2);
  183. imgSpan2.appendChild(imgDisconnect2);
  184. nozzleDiv.appendChild(imgSpan2);
  185. var nozzleNumberSpan2 = document.createElement("span");
  186. nozzleNumberSpan2.style = "float: left; width: 35%; height: 100%;font-size: 22px; text-align:left;line-height:" + 222 / nozzleShouldBeDisplayed + "px;";
  187. nozzleNumberSpan2.innerHTML = value[currentIndex].SiteLevelNozzleId;
  188. nozzleDiv.appendChild(nozzleNumberSpan2);
  189. }
  190. nozzleBlockDiv.appendChild(nozzleDiv);
  191. currentIndex++;
  192. }
  193. pumpBlockDiv.appendChild(nozzleBlockDiv);
  194. }
  195. }
  196. pumpsDiv.appendChild(pumpBlockDiv);
  197. });
  198. if (document.getElementById("PumpsPage_Bottom_pages") != null) {
  199. for (var p = 1; p < 6; p++) {
  200. var imgCurrentPageIdStr = "PumpsPage_Bottom_pages_img_"+p;
  201. var imgCurrentPageNotShowedIdStr = "PumpsPage_Bottom_pages_notshowed_img_"+p;
  202. if (p > totalPage) {
  203. if (document.getElementById(imgCurrentPageIdStr) != null)
  204. document.getElementById(imgCurrentPageIdStr).style.visibility = "hidden";
  205. if (document.getElementById(imgCurrentPageNotShowedIdStr) != null)
  206. document.getElementById(imgCurrentPageNotShowedIdStr).style.visibility = "hidden";
  207. } else if (p === currentPage || (p === 5 && totalPage > p)) {
  208. if (document.getElementById(imgCurrentPageIdStr) != null)
  209. document.getElementById(imgCurrentPageIdStr).style.visibility = "visible";
  210. if (document.getElementById(imgCurrentPageNotShowedIdStr) != null)
  211. document.getElementById(imgCurrentPageNotShowedIdStr).style.visibility = "hidden";
  212. }else {
  213. if (document.getElementById(imgCurrentPageIdStr) != null)
  214. document.getElementById(imgCurrentPageIdStr).style.visibility = "hidden";
  215. if (document.getElementById(imgCurrentPageNotShowedIdStr) != null)
  216. document.getElementById(imgCurrentPageNotShowedIdStr).style.visibility = "visible";
  217. }
  218. }
  219. }
  220. }
  221. //process vr state change event
  222. function UpdateVrState(message) {
  223. console.log("message received:"+message);
  224. if (message != null && message.NozzleStatus != null && message.NozzleStatus.data != null) {
  225. message.NozzleStatus.data.forEach(
  226. function(d) {
  227. var nozzleId = d.SiteLevelNozzleId;
  228. var dispenserId = d.SiteLevelDispenserId;
  229. var nozzleDiv = document.getElementById("pump" + dispenserId + "_nozzle" + nozzleId);
  230. if (!(warningNozzles.indexOf(nozzleId) < 0)) {
  231. if (d.VrState !== "WARNING")
  232. warningNozzles.splice(warningNozzles.indexOf(nozzleId), 1);
  233. } else if (!(alarmNozzles.indexOf(nozzleId) < 0)) {
  234. if (d.VrState !== "ALARM")
  235. alarmNozzles.splice(alarmNozzles.indexOf(nozzleId), 1);
  236. }
  237. if (d.VrState === "WARNING" && warningNozzles.indexOf(nozzleId)<0) warningNozzles.push(nozzleId);
  238. else if (d.VrState === "ALARM" && alarmNozzles.indexOf(nozzleId)<0) alarmNozzles.push(nozzleId);
  239. if (nozzleDiv != null) {
  240. var itemNormalVisibility = (d.VrState === "NORMAL") ? "visible" : "hidden";
  241. var itemWarningVisibility = (d.VrState === "WARNING") ? "visible" : "hidden";
  242. var itemAlarmVisibility = (d.VrState === "ALARM") ? "visible" : "hidden";
  243. var itemDisconnectVisibility = (d.VrState === "DISCONNETED") ? "visible" : "hidden";
  244. var preIdStr = "pump" + dispenserId + "_nozzle" + nozzleId;
  245. var imgNormal = document.getElementById(preIdStr + "_normal");
  246. var imgWarning = document.getElementById(preIdStr + "_warning");
  247. var imgAlarm = document.getElementById(preIdStr + "_alarm");
  248. var imgDisconnected = document.getElementById(preIdStr + "_disconnected");
  249. if (imgNormal != null) imgNormal.style.visibility = itemNormalVisibility;
  250. if (imgWarning != null) imgWarning.style.visibility = itemWarningVisibility;
  251. if (imgAlarm != null) imgAlarm.style.visibility = itemAlarmVisibility;
  252. if (imgDisconnected != null) imgDisconnected.style.visibility = itemDisconnectVisibility;
  253. } else {
  254. console.log("nozzle " + nozzleId + "is not exist on UI");
  255. }
  256. });
  257. } else {
  258. console.log("Invalid vr state change event");
  259. }
  260. UpdateHeadMessage();
  261. }
  262. //Update the message on header
  263. function UpdateHeadMessage() {
  264. var pumpsHead = document.getElementById("PumpsPage_Head");
  265. var normalHead = document.getElementById("PumpsPage_Head_Status_normal");
  266. var warningHead = document.getElementById("PumpsPage_Head_Status_warning");
  267. var alarmHead = document.getElementById("PumpsPage_Head_Status_alarm");
  268. var textSpan = document.getElementById("head_left_text");
  269. if (pumpsHead == null || normalHead == null || warningHead == null || alarmHead == null || textSpan ==null) {
  270. return;
  271. }
  272. var message = "";
  273. if (alarmNozzles.length > 0) {
  274. alarmNozzles.forEach(function(d) {
  275. message = message+d + ",";
  276. });
  277. normalHead.style.visibility = "hidden";
  278. warningHead.style.visibility = "hidden";
  279. alarmHead.style.visibility = "visible";
  280. message = message + "号枪处于报警状态";
  281. textSpan.style.textAlign = "left";
  282. textSpan.innerHTML = message;
  283. return;
  284. }
  285. if (warningNozzles.length > 0) {
  286. warningNozzles.forEach(function(d) {
  287. message = message+d + ",";
  288. });
  289. normalHead.style.visibility = "hidden";
  290. warningHead.style.visibility = "visible";
  291. alarmHead.style.visibility = "hidden";
  292. message = message + "号枪处于预警状态";
  293. textSpan.style.textAlign = "left";
  294. textSpan.innerHTML = message;
  295. return;
  296. }
  297. }
  298. </script>
  299. <div id="Pumps" style="position: relative; margin: 0 auto; width: (@screenWidth)px; height: (@mainviewHeight)px;">
  300. </div>
  301. <script src="~/js/mqtt.min.js"></script>
  302. <script type="text/javascript">
  303. var hostname = 'localhost', //'192.168.1.2',
  304. port = 8384,
  305. clientId = 'mqttjs_' + (Math.random() * 1000000).toString(),
  306. timeout = 5000,
  307. keepAlive = 100,
  308. cleanSession = false,
  309. mqttVersion = 3,
  310. ssl = false;
  311. var options = {
  312. //mqtt客户端的id,这里面应该还可以加上其他参数,具体看官方文档
  313. clientId: 'mqttjs_' + (Math.random() * 1000000).toString(),
  314. //invocationContext: {
  315. // host: hostname,
  316. // port: port,
  317. // //path: client.path,
  318. // mqttVersion:mqttVersion,
  319. // clientId: clientId
  320. //},
  321. //mqttVersion:mqttVersion,
  322. timeout: timeout,
  323. //keepAliveInterval: keepAlive,
  324. //cleanSession: cleanSession,
  325. useSSL: false
  326. //onSuccess: onConnect,
  327. //onFailure: onError
  328. }
  329. //浏览器采用websocket协议,host主机地址为192.168.0.200,端口为9001,路径为/mqtt
  330. var client = mqtt.connect("@Model.mqttConnectionString", options); // you add a ws:// url here
  331. //建立连接
  332. client.on('connect',
  333. function () {
  334. var a = document.getElementById("testmqqt");
  335. if (a != null) a.innerHTML = "connected";
  336. //订阅主题 presence
  337. client.subscribe('/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/service/GetConfigAsync_reply',
  338. function (err) {
  339. if (!err) {
  340. if (a != null) a.innerHTML = "subscribe GetConfigAsync_reply successfully";
  341. client.publish('/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/service/GetConfigAsync', 'Hello mqtt ');
  342. } else {
  343. //打印错误
  344. if (a != null) a.innerHTML = err;
  345. }
  346. });
  347. client.subscribe('/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/service/GetNozzlesData_reply',
  348. function (err) {
  349. if (!err) {
  350. client.publish('/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/service/GetNozzlesData', "Hello mqtt");
  351. };
  352. });
  353. client.subscribe('/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/event/OnVaporRecoveryStateChange/post',
  354. function (err) {
  355. if (!err) {
  356. console.log("subscribe vr state change event successfully");
  357. } else {
  358. console.log("subscribe vr state change event failed"+err);
  359. }
  360. });
  361. client.subscribe('/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/service/GetNozzlesFlowData_reply',
  362. function (err) {
  363. if (!err) {
  364. if (a != null) a.innerHTML = "subscribe nozzleFlowData successfully";
  365. } else {
  366. //打印错误
  367. if (a != null) a.innerHTML = err;
  368. }
  369. });
  370. });
  371. //如果连接错误,打印错误
  372. client.on('error',
  373. function (err) {
  374. var a = document.getElementById("testmqqt");
  375. if (a != null) a.innerHTML = err;
  376. client.end();
  377. });
  378. ////如果client订阅主题成功,那么这里就是当接收到自己订阅主题的处理逻辑
  379. client.on('message',
  380. function (topic, message) {
  381. // message is Buffer,此处就是打印消息的具体内容
  382. //console.log('-> ' + message.toString());
  383. var a = document.getElementById("testmqqt");
  384. if (a != null) a.innerHTML = topic;
  385. if (topic === "/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/service/GetConfigAsync_reply") {
  386. processConfigData(JSON.parse(message.toString()));
  387. return;
  388. }
  389. if (topic === "/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/service/GetNozzlesData_reply") {
  390. GeneratePumpView(JSON.parse(message.toString()));
  391. return;
  392. }
  393. if (topic === '/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/event/OnVaporRecoveryStateChange/post') {
  394. UpdateVrState(JSON.parse(message.toString()));
  395. return;
  396. }
  397. });
  398. function processConfigData(message) {
  399. var WarningValueMin = message.onlineWatchConfig.WarningValueMin;
  400. var WarningValueMax = message.onlineWatchConfig.WarningValueMax;
  401. console.log(message);
  402. //var a = document.getElementById("testmqqt");
  403. //if (a != null) a.innerHTML = WarningValueMin;
  404. }
  405. //// 用户程序点击事件
  406. function Onmqtttest() {
  407. message = "message from browser with websocket"; // 消息内容
  408. //发布主题presence,消息内容为Hello mqtt,订阅与推送一样自发自收
  409. client.publish('/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/service/GetConfigAsync', 'Hello mqtt ' + message);
  410. var a = document.getElementById("testmqqt");
  411. if (a != null) a.innerHTML = message;
  412. }
  413. //显示前一页
  414. function ShowPreviousPage() {
  415. if (currentPage === 1) {
  416. alert("已经是第一页!");
  417. return;
  418. }
  419. currentPage--;
  420. client.publish('/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/service/GetNozzlesData');
  421. }
  422. //显示下一页
  423. function ShowNextPage() {
  424. if (currentPage === totalPage) {
  425. alert("已经是最后一页!");
  426. return;
  427. }
  428. currentPage++;
  429. client.publish('/sys/Vapor_Recovery_App/Application.VaporRecoveryApp.App/thing/service/GetNozzlesData');
  430. }
  431. </script>