ui.js~ 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. var lines;
  2. var subscription;
  3. var ws;
  4. var acts = {};
  5. var transitions = [];
  6. var lastpos;
  7. var sub = 'topic' + '=' + 'activity' + '&' +// {{{
  8. 'events' + '=' + 'calling' + '&' +
  9. 'topic' + '=' + 'condition' + '&' +
  10. 'events' + '=' + 'eval' + '&' +
  11. 'topic' + '=' + 'dataelements' + '&' +
  12. 'events' + '=' + 'change' + '&' +
  13. 'topic' + '=' + 'position' + '&' +
  14. 'events' + '=' + 'change';// }}}
  15. function transdraw() {
  16. var buff = [];
  17. while (transitions.length > 0) {
  18. var trans = transitions.shift();
  19. if (acts[trans.unmark] && acts[trans.at]) {
  20. $(lines).each(function(k,val){
  21. if (val['From'] == acts[trans.unmark] && val['To'] == acts[trans.at]) {
  22. changelines('green','red');
  23. drawline(val['From'],val['To'],val['Between'],'green');
  24. }
  25. });
  26. } else {
  27. buff.push(trans);
  28. }
  29. }
  30. transitions = buff;
  31. }
  32. function transdraw2(station) {
  33. if (lastpos) {
  34. $(lines).each(function(k,val){
  35. if (val['From'] == lastpos && val['To'] == station) {
  36. changelines('green','red');
  37. drawline(val['From'],val['To'],val['Between'],'green');
  38. //console.log(lastpos);
  39. }
  40. });
  41. }
  42. lastpos = station;
  43. }
  44. function websocket() { //{{{
  45. var url = $('body').attr('current-instance');
  46. var Socket = "MozWebSocket" in window ? MozWebSocket : WebSocket;
  47. if (ws) ws.close();
  48. ws = new Socket(url.replace(/http/,'ws') + "/notifications/subscriptions/" + subscription + "/ws/");
  49. ws.onopen = function() {
  50. console.log("monitoring", "opened", "");
  51. };
  52. ws.onmessage = function(e) {
  53. data = $.parseXML(e.data);
  54. if ($('event > topic',data).length > 0) {
  55. switch($('event > topic',data).text()) {
  56. case 'condition':
  57. var data = JSON.parse($('event > notification',data).text());
  58. if (data.condition == "true") {
  59. $('#detailcolumn .stations').append($('<div class="station-condition">' + data.code + '</div>'));
  60. }
  61. case 'dataelements':
  62. var data = JSON.parse($('event > notification',data).text());
  63. var schrott = data.values.schrott;
  64. var schrott_value = (JSON.parse(schrott)["schrott"]);
  65. var schrott_percent = schrott_value*100;
  66. console.log(data);
  67. if (data.label == 'Identifikation 110'){
  68. $('#detailcolumn .instance').append($('<br> <div class="station-id"> Predicted Scrap Probability: ' + schrott_percent.toFixed(2) + ' %</div>'));
  69. }
  70. case 'position':
  71. // var data = JSON.parse($('event > notification',data).text());
  72. // if (data.unmark && data.at) {
  73. // transitions.push({unmark: data.unmark[0], at: data.at[0]});
  74. // transdraw();
  75. // }
  76. break;
  77. case 'activity':
  78. var data = JSON.parse($('event > notification',data).text());
  79. var activity = data.activity;
  80. var label = data.label;
  81. var station = label.match(/ ([\d\/-]+)$/)[1];
  82. if (station.split('/').length > 1) {
  83. station = station.split('/')[0];
  84. $(station.split('/')).each(function(k,val){
  85. acts[activity] = val;
  86. });
  87. } else {
  88. acts[activity] = station;
  89. }
  90. $('g.node[data-id=' + station + ']').addClass('active');
  91. transdraw2(station);
  92. $('#detailcolumn .stations').append($('<div class="station-id">' + station + '</div>'));
  93. break;
  94. }
  95. }
  96. if ($('vote > topic',data).length > 0) {
  97. }
  98. };
  99. ws.onclose = function() {
  100. console.log("monitoring", "closed", "server down i assume.");
  101. };
  102. } //}}}
  103. $(document).ready(function(){
  104. // $("#graphcolumn .draw").on('click','g.node',getnode);
  105. // $("#graphcolumn .draw").click('click',getpos);
  106. $.get("nodes.yaml", function(res) {
  107. var nodes = jsyaml.load(res);
  108. $(nodes).each(function(k,val){
  109. drawnode(val['ID'],val['X'],val['Y'],val['Name']);
  110. });
  111. });
  112. $.get("lines.yaml", function(res) {
  113. lines = jsyaml.load(res);
  114. });
  115. $('#run').click(function(){
  116. $('#detailcolumn .stations').empty();
  117. $('#graphcolumn .draw .lines').empty();
  118. $('#graphcolumn .draw .texts g.node.active').removeClass('active');
  119. $.ajax({
  120. type: "POST",
  121. url: "https://centurio.work/flow/start/url/",
  122. data: { info: "OEBB Sim", behavior: "fork_ready", url: "https://centurio.work/customers/oebb/processes/Fulltest_3.6.xml" },
  123. success: function(url){
  124. url = url['CPEE-INSTANCE-URL'];
  125. $('#detailcolumn .instance').show();
  126. $('#detailcolumn .instance a').text(url);
  127. $('#detailcolumn .instance a').attr('href','https://centurio.work/customers/oebb/?monitor=' + url);
  128. $('body').attr('current-instance',url);
  129. $.ajax({
  130. type: "POST",
  131. url: url + "/notifications/subscriptions/",
  132. data: sub,
  133. success: function(res){
  134. res = res.unserialize();
  135. $.each(res,function(a,b){
  136. if (b[0] == 'key') {
  137. subscription = b[1];
  138. }
  139. });
  140. websocket();
  141. }
  142. });
  143. }
  144. });
  145. });
  146. });