Index.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. class Queue {
  2. constructor() {
  3. this.count = 0;
  4. this.lowestCount = 0;
  5. this.items = {};
  6. }
  7. enqueue(ele) {
  8. this.items[this.count] = ele;
  9. this.count++;
  10. }
  11. dequeue() {
  12. if (this.isEmpty()) {
  13. return undefined;
  14. }
  15. const result = this.items[this.lowestCount];
  16. delete this.items[this.lowestCount];
  17. this.lowestCount++;
  18. return result;
  19. }
  20. peek() {
  21. if (this.isEmpty()) {
  22. return undefined;
  23. }
  24. return this.items[this.lowestCount];
  25. }
  26. isEmpty() {
  27. return this.count - this.lowestCount === 0;
  28. }
  29. size() {
  30. return this.count - this.lowestCount;
  31. }
  32. clear() {
  33. this.count = 0;
  34. this.lowestCount = 0;
  35. this.items = {};
  36. }
  37. toString() {
  38. if (this.isEmpty()) {
  39. return "";
  40. }
  41. let objString = `${this.items[this.lowestCount]}`;
  42. for (let i = this.lowestCount + 1; i < this.count; i++) {
  43. objString = `${objString}, ${this.items[i]}`;
  44. }
  45. return objString;
  46. }
  47. }
  48. Date.prototype.Format = function (fmt) { // author: meizz
  49. var o = {
  50. "M+": this.getMonth() + 1, // 月份
  51. "d+": this.getDate(), // 日
  52. "h+": this.getHours(), // 小时
  53. "m+": this.getMinutes(), // 分
  54. "s+": this.getSeconds(), // 秒
  55. "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
  56. "S": this.getMilliseconds() // 毫秒
  57. };
  58. if (/(y+)/.test(fmt))
  59. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  60. for (var k in o)
  61. if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  62. return fmt;
  63. }
  64. const sleep = (time) => new Promise(resolve => setTimeout(resolve, time));
  65. var refreshGirdData; // 更新数据
  66. var selectedRow;
  67. var rangeoninput;
  68. var israngedrag = false;
  69. var clearPlayIntervalStart;
  70. var clearGetDataInterval;
  71. var clearGetDequeueDataInterval;
  72. var PlayStartframe;
  73. var time1;
  74. var time2;
  75. var Play = 1;
  76. var StartFrame;
  77. var StartFrameTicks;
  78. var Frame;
  79. var FrameTicks;
  80. var Starting = false;
  81. var Reading = false;
  82. var replayFlag = false;
  83. var Index = 0;
  84. var lastvalue = 0;
  85. var changed = false;
  86. var SliderTime;
  87. var SliderTimeTickts;
  88. var DataPack = {};
  89. const Datas = new Queue();
  90. var PlayIntervalStartEnd = false;
  91. var GetLastDatasEnd = false;
  92. var GetDataEnd = false;
  93. var FaultInfosTime;
  94. var bootstrap = function ($, learun) {
  95. "use strict";
  96. var page = {
  97. init: function () {
  98. //page.GetDeviceData();
  99. page.bind();
  100. page.Start();
  101. page.PlayRealTime();
  102. page.OnGui();
  103. page.SetFaultInfos();
  104. page.GetWarehouseData();
  105. },
  106. bind: function () {
  107. //var srmlist = $("[wcssrm = 'wcssrm']");
  108. // 新增 $('#lr_add')
  109. $(".left [srm]").on('click', function () {
  110. let name = $(this).attr('srm');
  111. learun.layerForm({
  112. id: 'form',
  113. btn: null,//['关闭'],
  114. title: '堆垛机',
  115. url: top.$.rootUrl + '/DeviceMonitorManager/HJDeviceMonitor/DDJDetail?keyValue=' + name,
  116. width: 860,
  117. height: 745,
  118. callBack: function (id) {
  119. // return top[id].acceptClick(refreshGirdData);
  120. }
  121. });
  122. });
  123. //$(".left .station").on('click', function () {
  124. // let name = $(this).find('span').text();
  125. // learun.layerForm({
  126. // id: 'form',
  127. // btn: null,//['关闭'],
  128. // title: 'DB详情',
  129. // url: top.$.rootUrl + '/DeviceMonitorManager/HJDeviceMonitor/StationDetail?keyValue=' + name,
  130. // width: 960,
  131. // height: 745,
  132. // callBack: function (id) {
  133. // // return top[id].acceptClick(refreshGirdData);
  134. // }
  135. // });
  136. //});
  137. $("#PlayerPlay").on('click', function () {
  138. Play = Play == 1 ? 0 : 1;
  139. if (Play == 1)
  140. $("#imgPlayerPlay").attr("src", "/Content/images/DeviceMonitor/PlayerPlay.png");
  141. else {
  142. $("#imgPlayerPlay").attr("src", "/Content/images/DeviceMonitor/PlayerPause.png");
  143. }
  144. });
  145. $("#PlayerRealTime").on('click', function () {
  146. page.PlayRealTime();
  147. });
  148. $('#slider').on('input propertychange', function () {
  149. changed = true;
  150. })
  151. $("#slider").on('change', function () {
  152. // alert(this.value);
  153. israngedrag = false;
  154. changed = true;
  155. SliderTimeTickts = this.value.replace('Date', '').replace('(', '').replace(')', '').replace(/\//g, '');
  156. page.PlayBack();
  157. //learun.httpAsyncGet(top.$.rootUrl + '/DeviceMonitorManager/HJDeviceMonitor/GetDatetime?timestamp=' + timeSpan, function (data) {
  158. // if (data.IsSucc) {
  159. // alert(data.data);
  160. // }
  161. // else {
  162. // let msg = data.info;
  163. // }
  164. // var sd = Datas.dequeue();
  165. //});
  166. //var ticks = Math.floor(timeSpan / 10000) - 62135596800000; // 将时间戳转换为Ticks
  167. //var date = new Date(ticks);//.toLocaleString('zh-CN'); // 将Ticks转换为DateTime格式
  168. //date.setHours(date.getHours() - 8);
  169. ////alert(date.Format("yyyy-MM-dd hh:mm:ss"))
  170. });
  171. setInterval(page.GetWarehouseData, 10000);
  172. //setInterval(page.SetFaultInfos, 4000);
  173. setTimeout(() => {
  174. let oneBottomLeft = document.getElementById('equipmentStatus');
  175. let mt_value = 0;
  176. let speed = 100;
  177. let height = oneBottomLeft.clientHeight;
  178. if (height > 285) {
  179. function settimer1() {
  180. if (mt_value >= height) {
  181. mt_value = 0;
  182. page.SetFaultInfos();
  183. } else {
  184. mt_value = mt_value + 1;
  185. }
  186. oneBottomLeft.style.marginTop = '-' + mt_value + 'px';
  187. }
  188. time1 = setInterval(settimer1, speed);
  189. }
  190. // setInterval(timer2,speed)
  191. }, 5000);
  192. },
  193. //加载
  194. GetDeviceData: function () {
  195. var deferred = $.Deferred();
  196. learun.httpAsyncPost(top.$.rootUrl + '/DeviceMonitorManager/HJDeviceMonitor/GetData', {}, function (data) {
  197. if (data.IsSucc) {
  198. Datas.enqueue(data.data);
  199. learun.SetDeviceMonitor.SetDeviceData(data.data);
  200. }
  201. else {
  202. //let msg = data.info;
  203. }
  204. //var sd = Datas.dequeue();
  205. });
  206. //return deferred.promise();
  207. },
  208. search: function (param) {
  209. },
  210. PlayRealTime: function () {
  211. Play = 1;
  212. Starting = true;
  213. Reading = false;
  214. page.GetLastDatas();// PlayStart(LastFrameTicks);
  215. },
  216. PlayBack: function () {
  217. Starting = false;
  218. Reading = false;
  219. page.PlayStart(SliderTimeTickts);
  220. },
  221. GetLastDatas: function () {
  222. DataPack = {};
  223. if (!GetLastDatasEnd) {
  224. GetLastDatasEnd = true;
  225. sleep(10).then(() => {
  226. if (PlayStartframe < StartFrameTicks)
  227. PlayStartframe = StartFrameTicks;
  228. else if (PlayStartframe > LastFrameTicks)
  229. PlayStartframe = LastFrameTicks;
  230. // lock(Datas)
  231. Datas.clear();
  232. replayFlag = Play == 1;
  233. //Index = 0;
  234. learun.httpAsyncGet(top.$.rootUrl + '/DeviceMonitorManager/HJDeviceMonitor/GetLastDatas', function (data) {
  235. if (data.IsSucc) {
  236. DataPack = data.data;
  237. learun.SetDeviceMonitor.SetDeviceData(data.data);
  238. Index = data.data.Index;
  239. Frame = data.data.Frame;
  240. FrameTicks = data.data.FrameTicks;
  241. //SRMStations
  242. //WCSAGVs
  243. //WCSSRMs
  244. //WCSStations
  245. GetLastDatasEnd = false;
  246. Starting = false;
  247. //clearGetDataInterval = setInterval(page.GetData, 500);
  248. console.log("PlayIntervalStart : " + Starting);
  249. }
  250. else {
  251. }
  252. });
  253. })
  254. }
  255. },
  256. PlayStart: function (frame) {
  257. DataPack = {};
  258. PlayStartframe = frame;
  259. //console.log("PlayStart : " + Reading);
  260. // !Reading 等待
  261. //if (!Reading) {
  262. // if (clearPlayIntervalStart)
  263. // clearInterval(clearPlayIntervalStart);
  264. // sleep(5000);
  265. //}
  266. //else {
  267. // page.PlayIntervalStart();
  268. // //clearPlayIntervalStart= setInterval(page.PlayIntervalStart,2000)
  269. //}
  270. if (!Reading) {
  271. //console.log("PlayIntervalStart : " + Reading );
  272. page.PlayIntervalStart();
  273. }
  274. },
  275. PlayIntervalStart: function () {
  276. if (!PlayIntervalStartEnd) {
  277. PlayIntervalStartEnd = true;
  278. sleep(10).then(() => {
  279. if (PlayStartframe < StartFrameTicks)
  280. PlayStartframe = StartFrameTicks;
  281. else if (PlayStartframe > LastFrameTicks)
  282. PlayStartframe = LastFrameTicks;
  283. // lock(Datas)
  284. Datas.clear();
  285. replayFlag = Play == 1;
  286. //Index = 0;
  287. learun.httpAsyncGet(top.$.rootUrl + '/DeviceMonitorManager/HJDeviceMonitor/GetStartDatas?timestamp=' + PlayStartframe + '&len=' + Len, function (data) {
  288. if (data.IsSucc) {
  289. DataPack = data.data;
  290. learun.SetDeviceMonitor.SetDeviceData(data.data);
  291. Index = data.data.Index;
  292. Frame = data.data.Frame;
  293. FrameTicks = data.data.FrameTicks;
  294. //SRMStations
  295. //WCSAGVs
  296. //WCSSRMs
  297. //WCSStations
  298. PlayIntervalStartEnd = false;
  299. Starting = false;
  300. //clearGetDataInterval = setInterval(page.GetData, 500);
  301. console.log("PlayIntervalStart : " + Starting);
  302. }
  303. else {
  304. }
  305. });
  306. })
  307. }
  308. },
  309. Start: function () {
  310. clearGetDataInterval = setInterval(page.GetData, 500);
  311. clearGetDequeueDataInterval = setInterval(page.GetDequeueData, 300);
  312. },
  313. GetDequeueData: function () {
  314. if (Play == 0) {
  315. return;
  316. }
  317. if (Datas.size() == 0) {
  318. return
  319. }
  320. else {
  321. var pack = Datas.dequeue();
  322. DataPack = pack;
  323. Frame = pack.Frame;
  324. FrameTicks = pack.FrameTicks;
  325. }
  326. },
  327. GetData: function () {
  328. if (!GetDataEnd) {
  329. console.log("GetData GetData : " + Starting);
  330. if (Starting) {
  331. Reading = false;
  332. // clearInterval(clearGetDataInterval);
  333. return;
  334. }
  335. else {
  336. Reading = true;
  337. if (Index <= 0) {
  338. return;
  339. }
  340. var qty = 0;
  341. qty = Datas.size();
  342. if (qty < 600 * Play + 1) {
  343. learun.httpAsyncGet(top.$.rootUrl + '/DeviceMonitorManager/HJDeviceMonitor/GetLastFrame', function (data) {
  344. GetDataEnd = true;
  345. if (data.IsSucc) {
  346. var Length = parseInt(data.data.Len);
  347. LastFrameTicks = data.data.FrameTicks;
  348. LastFrame = data.data.Frame;
  349. if (parseInt(Index) >= parseInt(Length))
  350. Index = Length - 1;
  351. var end = parseInt(Index) + 30;
  352. end = end < parseInt(Length) ? end : Length - 1;
  353. end = parseInt(end) > 0 ? end : 0;
  354. console.log("GetRangeData: " + Index + " end: " + end);
  355. learun.httpAsyncGet(top.$.rootUrl + '/DeviceMonitorManager/HJDeviceMonitor/GetRangeData?start=' + Index + '&end=' + end, function (data) {
  356. if (data.IsSucc) {
  357. for (var i = 0; i < parseInt(data.data.length); i++) {
  358. Datas.enqueue(data.data[i]);
  359. }
  360. Index = parseInt(Index) + parseInt(data.data.length);
  361. // console.log("Datas: "+ Datas.size() + " index: " + Index);
  362. }
  363. else {
  364. }
  365. GetDataEnd = false;
  366. });
  367. }
  368. else {
  369. }
  370. });
  371. }
  372. }
  373. }
  374. },
  375. OnGui: function () {
  376. // PlayButton.image.sprite = Play == 1 ? sPlay : sPause;
  377. //Slider.minValue = DataSystem.StartFrame.Ticks - MinTime.Ticks;
  378. //Slider.maxValue = DataSystem.LastFrame.Ticks - MinTime.Ticks;
  379. //SliderTime = DateTime.FromBinary((long)Slider.value + MinTime.Ticks);
  380. setInterval(page.SetOnGui, 300)
  381. },
  382. SetOnGui: function () {
  383. if (israngedrag)
  384. return;
  385. $('#slider').attr('min', StartFrameTicks);
  386. $('#slider').attr('max', LastFrameTicks);
  387. var timeSpan = $('#slider').val().replace('Date', '').replace('(', '').replace(')', '').replace(/\//g, '');
  388. SliderTimeTickts = timeSpan;
  389. var ticks = Math.floor(timeSpan / 10000) - 62135596800000; // 将时间戳转换为Ticks
  390. var date = new Date(ticks); // 将Ticks转换为DateTime格式
  391. date.setHours(date.getHours() - 8);
  392. $('#Frame').text(date.Format("yyyy-MM-dd hh:mm:ss"));
  393. learun.SetDeviceMonitor.SetDeviceData(DataPack);
  394. if (lastvalue != timeSpan && changed) {
  395. FrameTicks = timeSpan;
  396. Frame = date;
  397. changed = false;
  398. lastvalue = timeSpan;
  399. page.PlayBack();
  400. }
  401. else {
  402. $('#slider').val(FrameTicks);
  403. $('#Frame').text(Frame);
  404. }
  405. },
  406. GetWarehouseData: function () {
  407. learun.httpAsyncGet(top.$.rootUrl + '/DeviceMonitorManager/HJDeviceMonitor/GetWarehouseLocationInfo', function (data) {
  408. if (data.IsSucc) {
  409. $("#emptywarecellnum").text( data.data.EmptySpaces)
  410. $("#agvtasknum").text(data.data.TotalAGVs);
  411. $("#tasknum").text(data.data.TotalTasks);
  412. }
  413. });
  414. },
  415. SetFaultInfos: function () {
  416. if (DataPack != null && DataPack.FaultInfos && DataPack.FaultInfos.length > 0) {
  417. var equipmentStatus = $("#equipmentStatus");
  418. var htmls = '';
  419. for (var i = 0; i < DataPack.FaultInfos.length; i++) {
  420. htmls += ' <div class="InfoItem"> <span class="titleS">故障</span> '
  421. htmls += '<div class="InfoBlock"> <span>区域:</span> <label>合金库</label> </div> ';
  422. htmls += '<div class="InfoBlock"> <span>交互点:</span> <label>' + DataPack.FaultInfos[i].DeviceCode + '</label> </div> ';
  423. htmls += '<div class="InfoBlock"> <span>故障原因:</span> <label>' + DataPack.FaultInfos[i].FaultInfos + '</label> </div> </div>';
  424. }
  425. equipmentStatus.empty();
  426. equipmentStatus.html(htmls);
  427. }
  428. },
  429. SetFaultMove: function () {
  430. setTimeout(() => {
  431. let oneBottomLeft = document.getElementById('equipmentStatus');
  432. let mt_value = 0;
  433. let speed = 100;
  434. let height = oneBottomLeft.clientHeight;
  435. if (height > 285) {
  436. clearInterval(FaultInfosTime);
  437. function settimer1() {
  438. if (mt_value >= height) {
  439. mt_value = 0;
  440. page.SetFaultInfos();
  441. //alert('获取新的数据');
  442. } else {
  443. mt_value = mt_value + 1;
  444. }
  445. oneBottomLeft.style.marginTop = '-' + mt_value + 'px';
  446. }
  447. time1 = setInterval(settimer1, speed);
  448. }
  449. else {
  450. FaultInfosTime= setInterval(page.SetFaultInfos, 500);
  451. }
  452. // setInterval(timer2,speed)
  453. }, 300);
  454. },
  455. };
  456. // 保存数据后回调刷新
  457. refreshGirdData = function () {
  458. page.search();
  459. }
  460. page.init();
  461. rangeoninput = function () {
  462. israngedrag = true;
  463. changed = true;
  464. console.log(Play);
  465. }
  466. //获取promise
  467. // var deferred = page.GetDeviceData();
  468. //setInterval(page.GetDeviceData,1000);
  469. }
  470. // 回调函数
  471. var master = function () {
  472. console.log('你是最棒的')
  473. return asyn()
  474. // 成功时 继续执行异步任务 并且返回promise
  475. }
  476. var good = function () {
  477. console.log('还不错')
  478. return asyn()
  479. // 进行时 继续执行异步任务 并且返回promise
  480. }
  481. var loser = function () {
  482. console.log('太差劲了')
  483. return asyn()
  484. // 失败时 继续执行异步任务 并且返回promise
  485. }
  486. var asyn = function () {
  487. // 声明deferred对象
  488. var deferred = $.Deferred()
  489. // 通过定时器异步任务,调用不同的回调函数
  490. setInterval(function () {
  491. var num = Math.random() * 100
  492. if (num > 85) {
  493. // 成功时候调用
  494. deferred.resolve()
  495. } else if (num < 60) {
  496. // 失败调用
  497. deferred.reject()
  498. } else {
  499. // 进行中调用
  500. deferred.notify()
  501. }
  502. }, 1000)
  503. return deferred.promise()
  504. }
  505. //获取promise
  506. var deferred = asyn()
  507. deferred.then(master, loser, good).then(master, loser, good)
  508. //执行了两次回调