vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
6.4 KiB

9 years ago
  1. 'use strict';
  2. function AgentGenerator(id) {
  3. // execute super constructor
  4. eve.Agent.call(this, id);
  5. this.rpc = this.loadModule('rpc', this.rpcFunctions);
  6. var me = this;
  7. this.amountOfEvents = 0;
  8. this.eventNumber = 0;
  9. this.eventsToFire = 0;
  10. this.lastEndOfDayTime = null;
  11. this.events = [];
  12. conn = this.connect(eve.system.transports.getAll());
  13. // connect to websocket if not online only
  14. if (conn[0].connect !== undefined) {
  15. conn[0].connect(EVENTS_AGENT_ADDRESS)
  16. .then(function () {
  17. console.log('Connected to ', EVENTS_AGENT_ADDRESS);
  18. me.rpc.request(EVENTS_AGENT_ADDRESS, {
  19. method: "loadEvents",
  20. params: {filename: "events.csv", actuallySend: true}
  21. }).done(function (reply) {
  22. me.amountOfEvents = reply;
  23. me.getEvents(AMOUNT_OF_INITIAL_EVENTS);
  24. });
  25. })
  26. .catch(function (err) {
  27. console.log('Error: Failed to connect to the conductor agent');
  28. console.log(err);
  29. // keep trying until the conductor agent is online
  30. setTimeout(connect, RECONNECT_DELAY);
  31. });
  32. }
  33. //use local connection
  34. else {
  35. EVENTS_AGENT_ADDRESS = 'eventGenerator';
  36. setTimeout(function() {
  37. me.rpc.request(EVENTS_AGENT_ADDRESS, {
  38. method: "loadEvents",
  39. params: {filename: "events.csv", actuallySend: true}
  40. }).done(function (reply) {
  41. me.amountOfEvents = reply;
  42. me.getEvents(AMOUNT_OF_INITIAL_EVENTS);
  43. });
  44. },40);
  45. }
  46. }
  47. // extend the eve.Agent prototype
  48. AgentGenerator.prototype = Object.create(eve.Agent.prototype);
  49. AgentGenerator.prototype.constructor = AgentGenerator;
  50. // define RPC functions, preferably in a separated object to clearly distinct
  51. // exposed functions from local functions.
  52. AgentGenerator.prototype.rpcFunctions = {};
  53. AgentGenerator.prototype.rpcFunctions.receiveEvent = function(params) {
  54. // setup timeline
  55. this.events.push(JSON.stringify(params));
  56. if (params.performedBy == "global") {
  57. this.imposeWorkingHours(params);
  58. }
  59. else {
  60. if (agentList[params.performedBy] === undefined) {
  61. agentList[params.performedBy] = new GenericAgent(params.performedBy, params.type);
  62. }
  63. this.rpc.request(params.performedBy, {method: "newEvent", params: params});
  64. }
  65. // check if we need to get another event, its done here to avoid raceconditions
  66. if (this.eventsToFire != 0) {
  67. var me = this;
  68. setTimeout(function() {
  69. me.eventNumber += 1;
  70. eventCounter.innerHTML = me.eventNumber +""; // make string so it works
  71. me.rpc.request(EVENTS_AGENT_ADDRESS, {method:'nextEvent', params:{}}).done();
  72. me.eventsToFire -= 1;
  73. },EVENT_DELAY);
  74. if (INCREASE_SPEED == true) {
  75. EVENT_DELAY = Math.max(0, 1000 - (1000 * (me.eventNumber / 205)));
  76. }
  77. }
  78. };
  79. AgentGenerator.prototype.getEvents = function (count) {
  80. if (this.eventNumber + count > this.amountOfEvents) {
  81. count = this.amountOfEvents - this.eventNumber;
  82. }
  83. if (count != 0) {
  84. this.eventsToFire = count - 1;
  85. this.rpc.request(EVENTS_AGENT_ADDRESS, {method: 'nextEvent', params: {}}).done();
  86. this.eventNumber += 1;
  87. eventCounter.innerHTML = this.eventNumber + ""; // make string so it works
  88. }
  89. };
  90. AgentGenerator.prototype.rpcFunctions.updateOpenJobs = function(params) {
  91. var skipJob = params.jobId;
  92. var time = params.time;
  93. this.moveTimeline(params);
  94. for (var agentId in agentList) {
  95. if (agentList.hasOwnProperty(agentId)) {
  96. agentList[agentId].jobs.updateJobs(time, skipJob);
  97. }
  98. }
  99. };
  100. AgentGenerator.prototype.moveTimeline = function(params) {
  101. timeline.setCustomTime(params.time);
  102. var range = timeline.getWindow();
  103. var duration = range.end - range.start;
  104. var hiddenDates = timeline.body.hiddenDates;
  105. var DateUtil = vis.timeline.DateUtil;
  106. var hiddenDuration = DateUtil.getHiddenDurationBetween(hiddenDates, range.start, range.end);
  107. var visibleDuration = duration - hiddenDuration;
  108. var fraction = 0.15;
  109. var requiredStartDuration = (1-fraction) * visibleDuration;
  110. var requiredEndDuration = fraction * visibleDuration;
  111. var convertedTime = new Date(params.time).getTime();
  112. var newStart;
  113. var newEnd;
  114. var elapsedDuration = 0;
  115. var previousPoint = convertedTime;
  116. for (var i = hiddenDates.length-1; i > 0; i--) {
  117. var startDate = hiddenDates[i].start;
  118. var endDate = hiddenDates[i].end;
  119. // if time after the cutout, and the
  120. if (endDate <= convertedTime) {
  121. elapsedDuration += previousPoint - endDate;
  122. previousPoint = startDate;
  123. if (elapsedDuration >= requiredStartDuration) {
  124. newStart = endDate + (elapsedDuration - requiredStartDuration);
  125. break;
  126. }
  127. }
  128. }
  129. if (newStart === undefined) {
  130. newStart = endDate - (requiredStartDuration - elapsedDuration);
  131. }
  132. elapsedDuration = 0;
  133. previousPoint = convertedTime;
  134. for (var i = 0; i < hiddenDates.length; i++) {
  135. var startDate = hiddenDates[i].start;
  136. var endDate = hiddenDates[i].end;
  137. // if time after the cutout, and the
  138. if (startDate >= convertedTime) {
  139. elapsedDuration += startDate - previousPoint;
  140. previousPoint = endDate;
  141. if (elapsedDuration >= requiredEndDuration) {
  142. newEnd = startDate - (elapsedDuration - requiredEndDuration);
  143. break;
  144. }
  145. }
  146. }
  147. if (newEnd === undefined) {
  148. newEnd = endDate + (requiredEndDuration - elapsedDuration);
  149. }
  150. timeline.setWindow(newStart, newEnd, {animate:Math.min(100,EVENT_DELAY)});
  151. };
  152. AgentGenerator.prototype.imposeWorkingHours = function(params) {
  153. var time = params.time;
  154. var operation = params.operation;
  155. for (var agentId in agentList) {
  156. if (agentList.hasOwnProperty(agentId)) {
  157. var agent = agentList[agentId];
  158. for (var jobId in agent.jobs.openJobs) {
  159. if (agent.jobs.openJobs.hasOwnProperty(jobId)) {
  160. var job = agent.jobs.openJobs[jobId];
  161. agent.updateAssignment(jobId, job.type, time, operation);
  162. }
  163. }
  164. }
  165. }
  166. if (operation == 'endOfDay') {
  167. this.lastEndOfDayTime = time;
  168. }
  169. else {
  170. if (this.lastEndOfDayTime !== null) {
  171. timelineItems.update({
  172. id: 'night' + uuid(),
  173. start: this.lastEndOfDayTime,
  174. end: time,
  175. type: 'background',
  176. className: 'night'
  177. });
  178. this.lastEndOfDayTime = null;
  179. }
  180. }
  181. };
  182. AgentGenerator.prototype.printEvents = function() {
  183. var str = "";
  184. str += "[";
  185. for (var i = 0; i < this.events.length; i++) {
  186. str += this.events[i];
  187. if (i < this.events.length - 1) {
  188. str += ","
  189. }
  190. }
  191. str += "]";
  192. console.log(str);
  193. }