Skip to content

Instantly share code, notes, and snippets.

@bulkan
Last active August 29, 2015 14:14
Show Gist options
  • Save bulkan/a16fbc84a39f0f065609 to your computer and use it in GitHub Desktop.
Save bulkan/a16fbc84a39f0f065609 to your computer and use it in GitHub Desktop.
<!--
To run;
python -m SimpleHTTPServer
http://locahost:8000
-->
<html>
<body>
<h1> POS </h1>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/2.9.4/bluebird.min.js"></script>
<script>
window.onload = function() {
var token = '<REPLACE WITH TOKEN>';
function doshiiRequest(method, uri){
var url = httpRoot + uri;
if (uri.match(/http/)){
url = uri;
}
return Promise.cast($.ajax({
type: method || 'get',
url: url,
dataType: 'json',
contentType: 'application/json',
headers: {
Authorization: token
}
}));
};
var serverRoot = 'beta.doshii.co/pos/api/v1';
var httpRoot = 'https://' + serverRoot;
var socketServer = 'wss://' + serverRoot + '/socket/?token=' + token
socket = new WebSocket(socketServer);
socket.onopen = function(openEvent){
// when socket connection is succesful the POS needs to configure the location mode
// before sending any HTTP requests
$.ajax({
type: 'put',
url: httpRoot + '/config',
data: JSON.stringify({
restaurantMode: 'restaurant',
tableMode: 'selection'
}),
dataType: 'json',
contentType: 'application/json',
headers: {
Authorization: token
},
error: function(err){
console.log(arguments);
}
})
.then(function(){
console.log('connected');
// send heartbeat
setInterval(function(){
socket.send('"primus::ping::'+ (new Date()).getTime() + '"');
}, 2000);
// on connection of socket retrieve all Orders, Consumers and Table Allocations
doshiiRequest('get', '/orders')
.then(function(orders){
console.log(orders)
// get the data for all the Orders
return Promise.all(
_.map(orders, function(order){
return doshiiRequest('get', order.uri);
})
);
})
.then(function(orders){
console.log(orders);
});
doshiiRequest('get', '/consumers')
.then(function(consumers){
console.log(consumers);
});
doshiiRequest('get', '/tables')
.then(function(tables){
console.log(tables);
});
});
};
socket.onmessage = function(event){
if (!event) {
return;
}
var data = JSON.parse(event.data);
if (!data.emit) {
return;
}
var eventName = data.emit[0];
var payload = data.emit[1];
console.log('Received:', eventName);
console.log(payload);
};
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment