add asynch function
This commit is contained in:
parent
5369e2c57c
commit
6be0b47f9e
@ -2,19 +2,24 @@ class Flower {
|
|||||||
constructor(type) {
|
constructor(type) {
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.maturity = 0;
|
this.maturity = 0;
|
||||||
this.maxMaturity;
|
this.maxMaturity = 0;
|
||||||
this.grown = false;
|
this.grown = false;
|
||||||
|
this.ordered = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
grow() {
|
grow() {
|
||||||
if(!this.isReady()) {
|
if(!this.isReady()) {
|
||||||
this.maturity++;
|
this.maturity++;
|
||||||
if(this.grown == false && this.maturity == this.maxMaturity) {
|
if(this.grown == false && this.maturity >= this.maxMaturity) {
|
||||||
this.grown == true;
|
this.grown = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isOrdered() {
|
||||||
|
return this.ordered;
|
||||||
|
}
|
||||||
|
|
||||||
isReady() {
|
isReady() {
|
||||||
return this.grown;
|
return this.grown;
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ class Forklift {
|
|||||||
this.currentSection = 0;
|
this.currentSection = 0;
|
||||||
this.cargo = null;
|
this.cargo = null;
|
||||||
this.queue = new PriorityQueue();
|
this.queue = new PriorityQueue();
|
||||||
|
this.busy = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
draw() {
|
draw() {
|
||||||
@ -32,7 +33,7 @@ class Forklift {
|
|||||||
} else {
|
} else {
|
||||||
// Final destination reached
|
// Final destination reached
|
||||||
magazineState[this.currentTarget].push(this.cargo);
|
magazineState[this.currentTarget].push(this.cargo);
|
||||||
this.removeCargo();
|
//this.removeCargo();
|
||||||
this.end = true;
|
this.end = true;
|
||||||
this.targetId = 0;
|
this.targetId = 0;
|
||||||
|
|
||||||
@ -62,6 +63,65 @@ class Forklift {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async findPath(targetSection) {
|
||||||
|
let data = {
|
||||||
|
graph: magazineToGraph(),
|
||||||
|
start_node: forklift.currentSection,
|
||||||
|
dest_node: int(targetSection),
|
||||||
|
};
|
||||||
|
httpPost(
|
||||||
|
serverUrl + '/shortestPath',
|
||||||
|
data,
|
||||||
|
response => {
|
||||||
|
let path = response.split('').map(Number);
|
||||||
|
this.currentTarget = path[0];
|
||||||
|
this.setPath(path);
|
||||||
|
going = true;
|
||||||
|
},
|
||||||
|
error => {
|
||||||
|
console.log(error);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
completeOrder(order) {
|
||||||
|
this.waitUntilJobIsDone()
|
||||||
|
.then(() => this.findPath(order.from))
|
||||||
|
.then(() => this.waitUntilJobIsDone())
|
||||||
|
.then(() => {
|
||||||
|
this.setCargo(order.what)
|
||||||
|
magazineState[order.from] = magazineState[order.from].filter((flower) => {
|
||||||
|
return flower != order.what;
|
||||||
|
})
|
||||||
|
this.findPath(order.to);
|
||||||
|
})
|
||||||
|
.then(() => this.waitUntilJobIsDone())
|
||||||
|
.then(() => {
|
||||||
|
magazineState[order.to].push(order.what);
|
||||||
|
order.what.setMaxMaturity(5 + 2*order.to);
|
||||||
|
console.log(order.message);
|
||||||
|
this.busy = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
waitUntilJobIsDone() {
|
||||||
|
return new Promise(resolve => {
|
||||||
|
let interval = setInterval(() => {
|
||||||
|
if (!going) {
|
||||||
|
clearInterval(interval);
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
}, 50);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
completeOrders() {
|
||||||
|
while(!this.queue.isEmpty()) {
|
||||||
|
let order = queue.dequeue();
|
||||||
|
this.completeOrder(order);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setCargo(cargo) {
|
setCargo(cargo) {
|
||||||
this.cargo = cargo;
|
this.cargo = cargo;
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ class PriorityQueue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
enqueue(element) {
|
enqueue(element) {
|
||||||
if(this.collection.isEmpty()) {
|
if(this.isEmpty()) {
|
||||||
this.collection.push(element);
|
this.collection.push(element);
|
||||||
}else {
|
}else {
|
||||||
let added = false;
|
let added = false;
|
||||||
|
@ -4,7 +4,7 @@ let roads;
|
|||||||
let packageClaim;
|
let packageClaim;
|
||||||
let going = false;
|
let going = false;
|
||||||
let forklift;
|
let forklift;
|
||||||
let magazineState = { 1: [], 2: [], 3: [], 4: [], 5: [], 6: [] };
|
let magazineState = {0: [], 1: [], 2: [], 3: [], 4: [], 5: [], 6: [] };
|
||||||
|
|
||||||
// This runs once at start
|
// This runs once at start
|
||||||
function setup() {
|
function setup() {
|
||||||
@ -14,7 +14,7 @@ function setup() {
|
|||||||
|
|
||||||
createMagazineLayout();
|
createMagazineLayout();
|
||||||
|
|
||||||
select('#button').mousePressed(getIrisType);
|
select('#button').mousePressed(addOrder());
|
||||||
sepalWidth = select('#sepalWidth');
|
sepalWidth = select('#sepalWidth');
|
||||||
sepalLength = select('#sepalLength');
|
sepalLength = select('#sepalLength');
|
||||||
petalWidth = select('#petalWidth');
|
petalWidth = select('#petalWidth');
|
||||||
@ -23,6 +23,8 @@ function setup() {
|
|||||||
// Create a forklift instance
|
// Create a forklift instance
|
||||||
forklift = new Forklift(sections[0].x, sections[0].y);
|
forklift = new Forklift(sections[0].x, sections[0].y);
|
||||||
setInterval(ageFlowers, 5000)
|
setInterval(ageFlowers, 5000)
|
||||||
|
|
||||||
|
select('#completeOrdersButton').mousePressed(forklift.completeOrders());
|
||||||
}
|
}
|
||||||
|
|
||||||
// This runs every frame
|
// This runs every frame
|
||||||
@ -68,7 +70,7 @@ function drawMagazine() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getIrisType() {
|
function addOrder() {
|
||||||
let sw = select('#sepalWidth').value();
|
let sw = select('#sepalWidth').value();
|
||||||
let sl = select('#sepalLength').value();
|
let sl = select('#sepalLength').value();
|
||||||
let pw = select('#petalWidth').value();
|
let pw = select('#petalWidth').value();
|
||||||
@ -79,34 +81,13 @@ function getIrisType() {
|
|||||||
petalWidth: pw,
|
petalWidth: pw,
|
||||||
petalLength: pl,
|
petalLength: pl,
|
||||||
};
|
};
|
||||||
|
console.log(data)
|
||||||
httpPost(serverUrl + '/classify', data, response => {
|
httpPost(serverUrl + '/classify', data, response => {
|
||||||
forklift.setCargo(new Flower(response))
|
magazineState[0].push(new Flower(response));
|
||||||
deliver(response);
|
forklift.completeOrder(new Order(0, magazineState[0][0], magazineState[0][0].type, 'cycki'));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function deliver(targetSection) {
|
|
||||||
let data = {
|
|
||||||
graph: magazineToGraph(),
|
|
||||||
start_node: forklift.currentSection,
|
|
||||||
dest_node: int(targetSection),
|
|
||||||
};
|
|
||||||
console.log(data);
|
|
||||||
httpPost(
|
|
||||||
serverUrl + '/shortestPath',
|
|
||||||
data,
|
|
||||||
response => {
|
|
||||||
path = response.split('').map(Number);
|
|
||||||
forklift.currentTarget = path[0];
|
|
||||||
forklift.setPath(path);
|
|
||||||
going = true;
|
|
||||||
},
|
|
||||||
error => {
|
|
||||||
console.log(error);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function createMagazineLayout() {
|
function createMagazineLayout() {
|
||||||
unit = width / 6;
|
unit = width / 6;
|
||||||
|
|
||||||
@ -156,7 +137,7 @@ function ageFlowers() {
|
|||||||
for (let key of Object.keys(magazineState)) {
|
for (let key of Object.keys(magazineState)) {
|
||||||
for (let flower of magazineState[key]) {
|
for (let flower of magazineState[key]) {
|
||||||
flower.grow();
|
flower.grow();
|
||||||
if(flower.isReady()) {
|
if(flower.isReady() && !flower.isOrdered()) {
|
||||||
let order;
|
let order;
|
||||||
let priority;
|
let priority;
|
||||||
if(int(key) == 0) {
|
if(int(key) == 0) {
|
||||||
@ -170,6 +151,7 @@ function ageFlowers() {
|
|||||||
priority = 2;
|
priority = 2;
|
||||||
}
|
}
|
||||||
forklift.queue.enqueue([order, priority]);
|
forklift.queue.enqueue([order, priority]);
|
||||||
|
flower.ordered = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
<script src="{% static 'magazine/flower.js' %}"></script>
|
<script src="{% static 'magazine/flower.js' %}"></script>
|
||||||
<script src="{% static 'magazine/sketch.js' %}"></script>
|
<script src="{% static 'magazine/sketch.js' %}"></script>
|
||||||
<script src="{% static 'magazine/priorityQueue.js' %}"></script>
|
<script src="{% static 'magazine/priorityQueue.js' %}"></script>
|
||||||
|
<script src="{% static 'magazine/order.js' %}"></script>
|
||||||
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
|
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
@ -44,14 +45,15 @@
|
|||||||
<div class="package">
|
<div class="package">
|
||||||
<h3 style="margin-top: 0;">Package description</h1>
|
<h3 style="margin-top: 0;">Package description</h1>
|
||||||
<label >Sepal Width</label>
|
<label >Sepal Width</label>
|
||||||
<input type="number" id="sepalWidth">
|
<input type="number" id="sepalWidth" value="1">
|
||||||
<label >Sepal Length</label>
|
<label >Sepal Length</label>
|
||||||
<input type="number" id="sepalLength">
|
<input type="number" id="sepalLength" value="1">
|
||||||
<label >Petal Width</label>
|
<label >Petal Width</label>
|
||||||
<input type="number" id="petalWidth">
|
<input type="number" id="petalWidth" value="1">
|
||||||
<label >Petal Length</label>
|
<label >Petal Length</label>
|
||||||
<input type="number" id="petalLength">
|
<input type="number" id="petalLength" value="1">
|
||||||
<button id="button">Send Package</button>
|
<button id="button">Send Package</button>
|
||||||
|
<button id="completeOrdersButton">Complete Queued Orders</button>
|
||||||
</div>
|
</div>
|
||||||
<div id="canvas" style="margin: 10px;"></div>
|
<div id="canvas" style="margin: 10px;"></div>
|
||||||
<div class="legend">
|
<div class="legend">
|
||||||
|
Loading…
Reference in New Issue
Block a user