@smartface/smartface.emulator
Advanced tools
Comparing version
91
main.js
@@ -30,5 +30,13 @@ var SMFC_WS; | ||
var dispatcherProcess = null; | ||
var qrCodeUrl = ""; | ||
var STATUS = { | ||
CONNECTED: 1, | ||
DISCONNECTED: 2 | ||
}; | ||
var wsConnectionStatus = STATUS.DISCONNECTED; | ||
var CONSTANTS = { | ||
"hostname": c9.hostname || window.location.hostname, | ||
"logToConsole": (window.location.search.indexOf('logToConsole=1') > -1) ? true : false, | ||
"ws_port": 8081, | ||
@@ -47,4 +55,5 @@ "ws_protocol": (c9.hostname) ? 'wss' : 'ws' | ||
plugin.on("unload", function() { | ||
if (!loaded) | ||
if (!loaded) { | ||
return; | ||
} | ||
$(".emulatorMain").remove(); | ||
@@ -73,3 +82,8 @@ loaded = false; | ||
function draw() { | ||
if (drawn) return; | ||
if (drawn) { | ||
// currently for every draw ws connection reconnects again, if current structure | ||
// changes below code need to change to -> if already connected dont show processing | ||
changeQRImageToProcessing(); | ||
return; | ||
} | ||
drawn = true; | ||
@@ -124,6 +138,5 @@ var markup = parseHtmlForStaticPrefix(require("text!./emulator.html")); | ||
IS_QRCODE_GENERATED = true; | ||
img.setAttribute('src', response.dataURL); | ||
img.setAttribute('width', '150'); | ||
img.setAttribute('height', '150'); | ||
img.classList.remove('absolute-both-center'); | ||
qrCodeUrl = response.dataURL; | ||
if (wsConnectionStatus === STATUS.CONNECTED) | ||
changeQRImageToUrl(qrCodeUrl); | ||
} | ||
@@ -135,3 +148,5 @@ else { | ||
process.stderr.on("data", function(chunk) { | ||
console.log(chunk); | ||
if (CONSTANTS.logToConsole) { | ||
console.log(chunk); | ||
} | ||
dialogError(chunk); | ||
@@ -143,2 +158,19 @@ }); | ||
function changeQRImageToProcessing() { | ||
var element = document.getElementById("smf-emulator-qrcode"); | ||
element.setAttribute("src", staticPrefix + '/icons/processing.gif'); | ||
// removing width & height to set it automatically to gif size | ||
element.removeAttribute('width'); | ||
element.removeAttribute('height'); | ||
element.classList.add('absolute-both-center'); | ||
} | ||
function changeQRImageToUrl(url) { | ||
var element = document.getElementById("smf-emulator-qrcode"); | ||
element.setAttribute("src", url); | ||
element.setAttribute('width', '200'); | ||
element.setAttribute('height', '200'); | ||
element.classList.remove('absolute-both-center'); | ||
} | ||
plugin.freezePublicAPI({ | ||
@@ -180,3 +212,5 @@ draw: draw, | ||
process.stdout.on("data", function(chunk) { | ||
console.log('data, from node', chunk); | ||
if (CONSTANTS.logToConsole) { | ||
console.log('data, from node', chunk); | ||
} | ||
setTimeout(function() { | ||
@@ -204,10 +238,15 @@ process.stdin.write('received message from Node.js, sending this back'); | ||
} | ||
proc.spawn("smartface-cloud-emulator-dispatcher", function(err, process) { | ||
var args_str = (CONSTANTS.logToConsole) ? ' --logToConsole=1' : ''; | ||
proc.spawn("smartface-cloud-emulator-dispatcher" + args_str, function(err, process) { | ||
if (err) { | ||
throw err; | ||
} | ||
console.log('[process]:smartface-cloud-emulator-dispatcher:', process); | ||
if (CONSTANTS.logToConsole) { | ||
console.log('[process]:smartface-cloud-emulator-dispatcher:', process); | ||
} | ||
dispatcherProcess = process; | ||
process.stdout.on("data", function(chunk) { | ||
console.log(chunk.toString()); | ||
if (CONSTANTS.logToConsole) { | ||
console.log(chunk.toString()); | ||
} | ||
if (chunk.toString().trim() === "ready") { | ||
@@ -291,3 +330,5 @@ // dont know why but without setTimeout websockets could not connect | ||
ws.onopen = function() { | ||
console.log('ws connected'); | ||
if (CONSTANTS.logToConsole) { | ||
console.log('ws connected'); | ||
} | ||
SMFC_WS = ws; | ||
@@ -297,2 +338,5 @@ timer = setInterval(function() { | ||
}, 15000); | ||
if (qrCodeUrl !== "") // checking if qr code generated or not | ||
changeQRImageToUrl(qrCodeUrl); | ||
wsConnectionStatus = STATUS.CONNECTED; | ||
}; | ||
@@ -303,13 +347,22 @@ ws.onerror = function(e) { | ||
ws.onclose = function(e) { | ||
if (timer) | ||
if (timer) { | ||
clearInterval(timer); | ||
console.log('ws closed', e); | ||
} | ||
if (CONSTANTS.logToConsole) { | ||
console.log('ws closed', e); | ||
} | ||
wsConnectionStatus = STATUS.DISCONNECTED; | ||
}; | ||
ws.onmessage = function(msgevent) { | ||
console.log('msgevent', msgevent); | ||
var json; | ||
if (CONSTANTS.logToConsole) { | ||
console.log('msgevent', msgevent); | ||
} | ||
try { | ||
var json = JSON.parse(msgevent.data); | ||
json = JSON.parse(msgevent.data); | ||
} | ||
catch (e) { | ||
console.log('not json'); | ||
if (CONSTANTS.logToConsole) { | ||
console.log('not json'); | ||
} | ||
return; | ||
@@ -326,3 +379,5 @@ } | ||
else { | ||
console.log('command is not implemented', command); | ||
if (CONSTANTS.logToConsole) { | ||
console.log('command is not implemented', command); | ||
} | ||
} | ||
@@ -329,0 +384,0 @@ } |
{ | ||
"name": "@smartface/smartface.emulator", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "SmartfaceCloud Emulator", | ||
@@ -5,0 +5,0 @@ "main": "main.js", |
Sorry, the diff of this file is not supported yet
83043
2.13%444
13.27%