-
-
Save MrCrambo/685eee191817e7508cacc590245ba021 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onload = function () { | |
// add eventListener for tizenhwkey | |
document.addEventListener('tizenhwkey', function(e) { | |
if(e.keyName == "back") | |
try { | |
//tizen.application.getCurrentApplication().exit(); | |
} catch (ignore) { } | |
}); | |
function successcallback(device) | |
{ | |
document.getElementById('textbox').innerHTML = device.address+'<br/>'+device.rssi+' dBm'; | |
if (device.address in target_devices) { | |
console.log(device.address + " _ " + device.rssi); | |
if (device.rssi > -95) | |
target_devices[device.address].push(device.rssi); | |
} | |
}; | |
var adapter = tizen.bluetooth.getLEAdapter(); | |
adapter.startScan(successcallback); | |
function makeTag(length) { | |
var result = ''; | |
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
var charactersLength = characters.length; | |
for ( var i = 0; i < length; i++ ) { | |
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | |
} | |
return result; | |
} | |
/* Open Tizen File System */ | |
var user_events = []; | |
var fileName = "logs" | |
var fileDir, logFile; | |
var hasPermission = false; | |
tizen.ppm.requestPermission("http://tizen.org/privilege/mediastorage", onsuccessPermission, null); | |
function onsuccessPermission(){ | |
tizen.filesystem.resolve('documents', onResolveSuccess, null, 'rw'); | |
} | |
function onResolveSuccess(dir) { | |
hasPermission = true; | |
fileDir = dir; | |
try { | |
fileDir.createFile(fileName); | |
} catch (IOError) { | |
console.log("File Already Exist"); | |
} | |
try { | |
logFile = fileDir.resolve(fileName); | |
logFile.openStream('r', readFromStream, null, "UTF-8"); | |
function readFromStream(fileStream) { | |
if (logFile.fileSize > 0){ | |
var text = fileStream.read(logFile.fileSize); | |
var text_events = text.split(';'); | |
for (var j = 0; j < text_events.length - 1; j++){ | |
var eventFields = text_events[j].split(":"); | |
user_events.push({deviceId:cur_device.replace(/:/g, ""), locationId:3, tag:eventFields[0], timestamp:eventFields[1], content:eventFields[2]}); | |
} | |
fileStream.close(); | |
} | |
} | |
} catch (exc) { | |
hasPermission = false; | |
return; | |
} | |
} | |
function myMethod( ) | |
{ | |
Object.keys(target_devices).forEach(function(key) { | |
console.log(key.replace(/:/g, "") + " - " + eval(target_devices[key].join('+'))/target_devices[key].length); | |
if (eval(target_devices[key].join('+'))/target_devices[key].length > -80) // think about avg | |
user_events.push({deviceId:cur_device.replace(/:/g, ""), locationId:3, tag:makeTag(10) + "_" + eval(target_devices[key].join('+'))/target_devices[key].length, timestamp:new Date().valueOf(), content:key.replace(/:/g, "")}); | |
target_devices[key] = []; | |
}); | |
var temp_events = user_events; | |
if (hasPermission){ | |
logFile.openStream('a', appendToStream, null, "UTF-8"); | |
function appendToStream(fileStream) { | |
var es = ""; | |
try { | |
for (var j = 0; j < temp_events.length; j++) | |
{ | |
es += temp_events[j].tag + ":" + temp_events[j].timestamp + ":" + temp_events[j].content; | |
es += ";"; | |
} | |
fileStream.write(es); | |
fileStream.close(); | |
} catch (exc) { | |
console.log('Could not write to file: ' + exc.message); | |
} | |
} | |
} | |
if (user_events.length != 0) | |
{ | |
var data = JSON.stringify({events : user_events, userHash : "010E-0F55-D634-76D6"}).replace(/\\\"/g, ""); | |
console.log(data); | |
jQuery.ajax ({ | |
url: 'https://api.stage.navigine.com/mobile/user-events/add', | |
type: "POST", | |
data: data, | |
dataType: "json", | |
contentType: "application/json", | |
success: function(){ | |
console.log('posted'); | |
user_events = []; | |
if (hasPermission){ | |
logFile.openStream('w', clearStream, null, "UTF-8"); | |
function clearStream(fileStream) { | |
try { | |
fileStream.write(""); | |
fileStream.close(); | |
} catch (exc) { | |
console.log('Could not clear file: ' + exc.message); | |
} | |
} | |
} | |
}, | |
error: function(error){ | |
console.log(error); | |
} | |
}); | |
} | |
} | |
setInterval(myMethod, 30000); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment