-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
219 lines (183 loc) · 5.99 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
################
### Web page ###
################
*/
const { CLIENT_RENEG_LIMIT } = require('tls');
window.console = {
log: function (str) {
var node = document.createElement("div");
node.appendChild(document.createTextNode(str));
document.getElementById("myLog").appendChild(node);
}
}
//selecting all required elements
const dropArea = document.querySelector(".drag-area"),
dragText = dropArea.querySelector("header"),
button = dropArea.querySelector("button"),
input = dropArea.querySelector("input");
let file; //this is a global variable and we'll use it inside multiple functions
const buttonsArea = document.querySelector(".bottons-area"),
startbutton = buttonsArea.querySelector(".start");
startbutton.onclick = () => {
if (document.getElementById("myFile").textContent != "No file") {
exec();
} else {
alert("No BPMN file loaded!");
}
}
//if user click on the button then the input also clicked
button.onclick = () => {
input.click();
}
//getting user select file and [0] this means if user select multiple files then we'll select only the first one
input.addEventListener("change", function () {
file = this.files[0];
document.getElementById("myFile").textContent = file.name;
dropArea.classList.add("active");
});
//If user Drag File Over DropArea
dropArea.addEventListener("dragover", (event) => {
event.preventDefault(); //preventing from default behaviour
dropArea.classList.add("active");
dragText.textContent = "Release to Upload File";
});
//If user leave dragged File from DropArea
dropArea.addEventListener("dragleave", () => {
dropArea.classList.remove("active");
dragText.textContent = "Drag & Drop to Upload File";
});
//If user drop File on DropArea
dropArea.addEventListener("drop", (event) => {
event.preventDefault(); //preventing from default behaviour
//getting user select file and [0] this means if user select multiple files then we'll select only the first one
file = event.dataTransfer.files[0];
document.getElementById("myFile").textContent = file.name;
});
/*
######################
### Ros connection ###
######################
*/
var ros = new ROSLIB.Ros();
// If there is an error on the backend, an 'error' emit will be emitted.
ros.on('error', function (error) {
console.log(error);
});
// Find out exactly when we made a connection.
ros.on('connection', function () {
console.log('Connection made!');
});
// Find out exactly when a connection is closed.
ros.on('close', function () {
console.log('Connection closed.');
});
// Create a connection to the rosbridge WebSocket server.
ros.connect('ws://192.168.1.235:9090')
/*
##############
### Engine ###
##############
*/
//Validation of BPMN files
function exec() {
const extension = file.name.split('.').pop();
let validExtensions = ["bpmn"]; //adding some valid extensions in array
if (validExtensions.includes(extension)) { //if user selected file is an image file
console.log("### [ " + JSON.stringify(file.name) + " ] ###");
const filereader = new FileReader();
filereader.onload = function (event) {
executeXmlFile(event.target.result);
};
const t = filereader.readAsText(file, "UTF-8");
} else {
alert("This is not a BPMN File!");
dropArea.classList.remove("active");
dragText.textContent = "Drag & Drop to Upload File";
}
}
//Execution of BPMN files
function executeXmlFile(source) {
'use strict';
const { Engine } = require('bpmn-engine');
const { EventEmitter } = require('events');
const listener = new EventEmitter();
var tstart = 0;
var tfinish = 0;
//Variables
const engine = Engine({
name: 'BPMN engine',
//Insert external variables here
variables: {
},
//Insert external functions here
services: {
addVariable,
makeTopic,
publish
},
source
});
//Freccie
listener.on('flow.take', (flow) => {
console.log(`flow.take <${flow.id}> was taken`);
});
//Inizio attivita'
listener.on('activity.start', (activity) => {
if (tstart == 0) tstart = activity.messageProperties.timestamp;
console.log(`activity.start <${activity.id}> was taken`);
});
//Fine attivita'
listener.on('activity.end', (activity) => {
tfinish = activity.messageProperties.timestamp;
console.log(`activity.end <${activity.id}> was released`);
});
//User task
listener.on('activity.wait', (wait) => {
console.log(`wait <${wait.id}> was taken`);
});
listener.on('activity.throw', (throwev) => {
console.log(`throw <${throwev.id}> was taken`);
});
listener.on('activity.error', (errorev) => {
console.log(`error <${errorev.id}> was taken`);
});
engine.on('end', (execution) => {
console.log("### Execution completed in " + JSON.stringify(tfinish - tstart) + "ms ###");
});
engine.on('stop', (execution) => {
console.log('stopped');
});
engine.on('error', (execution) => {
console.log('error');
});
engine.execute({
listener
}, (err) => {
if (err) throw err;
});
// Makes a new variable
function addVariable(local, varName, varValue) {
local.environment.output[varName] = varValue;
}
// Makes a new topic
/*
String
type: std_msgs/String
format: { data: '<String>' }
Movement
type: geometry_msgs/msg/Twist
format: { linear: { x: 1.0, y: 0.0, z: 0.0 }, angular: { x: 0.0, y: 0.0, z: 1.0 } }
*/
function makeTopic(local, topicName, topicType) {
local.environment.output[topicName] = new ROSLIB.Topic({
ros: ros,
name: '/' + topicName,
messageType: topicType
});
}
// Publish data in a specific topic
function publish(local, topicName, message) {
local.environment.output[topicName].publish(message);
}
}