ui (copy).js 4.5 KB

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