Scanning bluetooth web API multiple times - javascript

I am creating a web app that uses the BLE web api. I made it so that when you press the scan button it scans for bluetooth devices and filters out all the ones that are not ESP's. Then it calculates the distance and pushes it into an array. I am able to make so that it can scan for the bluetooth devices, calculate distance and calculate your position. However it only works once. I think this is because of the eventlistener because it only scans when the advertisements are received. However I can not seem to figure out how to make it so that i can just scan multiple times and recalculate the data.
import * as trilateration from './trilateration.js';
import * as Utils from './utils.js';
import context from './context.js';
const beaconMapping = {
'ESP32_1': 0,
'ESP32_2': 1,
'ESP32_3': 2,
}
const DISTANCE_SCALAR = 1;
console.log("Init");
const devices = [];
const ids = [];
const beaconuuid = [];
const distances = [];
const SCAN_TIME = 10000;
//add beacons to trilatertion;
trilateration.addBeacon(0, trilateration.vector(10.1, 7.0));
trilateration.addBeacon(1, trilateration.vector(0, 0));
trilateration.addBeacon(2, trilateration.vector(10.1, 0));
document.getElementById("scan").onclick = scan;
async function scan() {
console.log("Scanning...");
let options = {
acceptAllAdvertisements: true,
acceptAllDevices: true,
};
try {
log("Requesting Bluetooth Scan with options: " + JSON.stringify(options));
const scan = await navigator.bluetooth.requestLEScan(options);
// set the length to 0
distances.length = 0;
log("Scan started with:");
log(" acceptAllAdvertisements: " + scan.acceptAllAdvertisements);
log(" active: " + scan.active);
log(" keepRepeatedDevices: " + scan.keepRepeatedDevices);
log(" filters: " + JSON.stringify(scan.filters));
navigator.bluetooth.addEventListener("advertisementreceived", (event) => {
let name = event.device.name;
if (name && name.startsWith("ESP32") && !ids.includes(event.device.id)) {
console.log("Found");
//console.log(event);
//add to device list if name starts with ESP32
console.log("adding");
const obj = {
device: event.device,
uuids: event.uuids,
rssi: event.rssi,
tx: event.txPower,
distance: calculateDistance(event.rssi, event.txPower) * DISTANCE_SCALAR,
};
console.log(obj);
console.log(obj.tx, obj.rssi, obj.distance, name);
devices.push(obj);
ids.push(event.device.id);
const beaconIndex = beaconMapping[event.device.name];
if (beaconIndex !== undefined) {
distances.push(obj.distance);
trilateration.setDistance(beaconIndex, obj.distance);
} else {
console.error("Name not found in mapping");
}
if (distances.length > 2) {
var pos = trilateration.calculatePosition();
console.log(pos);
console.log("Distqnces qrr length", distances.length)
console.log("X: " + pos.x + "; Y: " + pos.y);
context.fillRect(pos.x * 100, pos.y * 100, 20, 20);
}
showBeaconInfo(obj);
}
/* log('Advertisement received.');
log(' Device Name: ' + event.device.name);
log(' Device ID: ' + event.device.id);
log(' RSSI: ' + event.rssi);
log(' TX Power: ' + event.txPower);
log(' UUIDs: ' + event.uuids); */
});
setTimeout(stopScan, SCAN_TIME);
function stopScan() {
console.log("List of all devices detected:");
console.log(devices);
console.log(ids)
console.log(beaconuuid);
console.log(distances)
log("Stopping scan...");
scan.stop();
log("Stopped. scan.active = " + scan.active);
}
} catch (error) {
log("Argh! " + error);
}
}
function showBeaconInfo(obj) {
log(" Device Name: " + obj.device.name);
log(" Device ID: " + obj.device.id);
log(" RSSI: " + obj.rssi);
log(" TX Power: " + obj.tx);
log(" UUIDs: " + obj.uuids);
}
function log(c) {
console.log(c);
}
function calculateDistance(rssi, txPower) {
//original method
//Math.pow(10, (event.txPower - event.rssi) / (10 * 2))
//overwrite txPower
txPower = -59;
if (rssi == 0) {
return -1.0;
}
const ratio = rssi * 1.0 / txPower;
if (ratio < 1.0) {
return Math.pow(ratio, 10);
} else {
const distance = (0.89976) * Math.pow(ratio, 7.7095) + 0.111;
return distance;
}
}
/*trilateration.addBeacon(0, trilateration.vector(25, 63));
trilateration.addBeacon(1, trilateration.vector(0, 0));
trilateration.addBeacon(2, trilateration.vector(25, 31));
trilateration.setDistance(0, distances[0]);
trilateration.setDistance(1, distances[1]);
trilateration.setDistance(2, distances[2]);
var pos = trilateration.calculatePosition();
console.log("X: " + pos.x + "; Y: " + pos.y);*/
context.strokeRect(0, 0, 1300, 700);
drawBeacons()
function drawBeacons() {
context.fillStyle = "red"
Utils.fillCircle(1001, 700, 50)
Utils.fillCircle(0, 0, 50)
Utils.fillCircle(1001, 0, 50)
}

Related

How to decode Javascript _0x Hex

I tried to decode this code but I did not succeed:
var _0x147bb5 = _0xe0c4, _0x41852d = _0xe0c4, _0x39ac2c = _0xe0c4;
(function (_0x3c89e2, _0x4b3ebe) {
var _0x331330 = _0xe0c4, _0x279707 = _0xe0c4, _0x50917c = _0xe0c4, _0x489044 = _0x3c89e2();
while (!![]) {
try {
var _0x36e386 = -parseInt(_0x331330(0x133)) / 0x1 + -parseInt(_0x279707('0x12a')) / 0x2 + -parseInt(_0x331330(0x111)) / 0x3 + -parseInt(_0x331330(0x12f)) / 0x4 + parseInt(_0x279707('0x10a')) / 0x5 * (-parseInt(_0x279707('0xf1')) / 0x6) + -parseInt(_0x331330('0x109')) / 0x7 + parseInt(_0x331330(0xee)) / 0x8 * (parseInt(_0x279707(0x11a)) / 0x9);
if (_0x36e386 === _0x4b3ebe) break; else _0x489044['push'](_0x489044['shift']());
} catch (_0x108487) {
_0x489044['push'](_0x489044['shift']());
}
}
}(_0x5a9e, 0x7f5a2));
var s = _0x147bb5('0x12b') + _0x147bb5(0xdc) + _0x39ac2c('0x118') + '91', f = 0xb, l = lf(), message = _0x39ac2c('0x117') + _0x39ac2c('0xe7') + _0x39ac2c('0x108') + _0x147bb5('0xd4') + 'ot\x20' + _0x147bb5('0x104') + _0x41852d(0xd3) + _0x39ac2c('0x122') + _0x39ac2c('0xef') + _0x41852d(0x127) + _0x41852d('0x102') + _0x41852d('0xfb') + _0x147bb5(0xe2) + _0x39ac2c(0xf2) + _0x147bb5(0xe5) + _0x41852d(0xd6) + _0x39ac2c(0x125) + _0x41852d(0xff) + _0x147bb5(0xd7) + _0x41852d('0x110') + _0x39ac2c('0x11e') + _0x39ac2c(0xdd) + _0x41852d(0x107) + 'ea)';
function rtclickcheck(_0xf37bcd) {
var _0x5b07b6 = _0x39ac2c, _0x189d42 = _0x41852d, _0x158d5e = _0x147bb5;
if (navigator[_0x5b07b6(0xfd) + _0x189d42(0xf5) + 'e'] == _0x158d5e('0xe4') + _0x189d42(0xde) + 'pe' && _0xf37bcd[_0x5b07b6('0xfc') + 'ch'] == 0x3) return alert(message), ![];
if (navigator[_0x5b07b6(0xfd) + _0x189d42(0xf8) + _0x5b07b6('0x11d') + 'n'][_0x5b07b6('0xec') + _0x158d5e(0x128) + 'f'](_0x158d5e(0xe6) + 'E') != -0x1 && event[_0x189d42('0x126') + _0x158d5e('0x132')] == 0x2) return alert(message), ![];
} function _0x5a9e() {
var _0x521b96 = ['Thi', '7109872DszjuD', '4405xemFJj', 'deA', '//r', 'cre', 'hre', 'h=/', 're.', '1823784ZeLCWd', ';\x20p', 'etc', 'cap', 'cha', 'crt', 'Ooh', '794', 'TCS', '14494869UZdEDp', 'onm', 'erH', 'sio', 'com', 'ute', 'dli', 'men', 'py\x20', '_y=', 'tBy', 'uth', 'but', 'rot', 'exO', 'inn', '603236FmaGTN', '-11', 'exp', 'ous', 'toU', '2882436fEtPRL', 'dit', 'kie', 'ton', '1000745kgQiEE', '\x20Co', 's\x20N', 'hos', '//y', 'sto', 'len', 'pat', 'tri', '.ly', '350', '\x20(y', 'sca', 'gth', 'lin', 'rib', 'by\x20', 'Att', 'Net', 'ps:', 'MSI', '!!\x20', 'edo', 'ide', 'tna', '10;', 'ind', '/yu', '24DoydZk', '-\x20P', 'rCo', '4518TXMdnf', 'htt', 'N_f', 'Ele', 'Nam', 'and', 'get', 'Ver', 'ire', 'ati', 'ed\x20', 'whi', 'app', 'coo', 'eme', 'TML', 'ath', 'ect', 'sli', 'For', 'Tim', 'set', 'uid'];
_0x5a9e = function () {
return _0x521b96;
};
return _0x5a9e();
}
document[_0x147bb5(0x11b) + _0x147bb5('0x12d') + _0x147bb5('0xe8') + 'wn'] = rtclickcheck;
if (l == s && s[_0x147bb5(0xd8) + _0x147bb5('0xdf')] == f) {
var ceditf = document[_0x147bb5(0xf7) + _0x41852d(0xf4) + _0x39ac2c('0x121') + _0x147bb5(0x124) + 'Id'](_0x39ac2c(0x10d) + _0x147bb5('0x130') + _0x147bb5('0xe0') + 'k'), citf = document[_0x39ac2c(0xf7) + _0x147bb5('0xf4') + _0x39ac2c('0x121') + _0x39ac2c('0x124') + 'Id'](_0x39ac2c('0x116') + _0x39ac2c(0xe0) + 'k');
o();
var citdf = document[_0x41852d('0xf7') + _0x147bb5('0xf4') + _0x41852d(0x121) + _0x41852d('0x124') + 'Id'](_0x41852d('0x116') + _0x147bb5('0x120') + 'nk');
function checkLIC() {
var _0x4072d7 = _0x41852d, _0x264ab4 = _0x41852d, _0x1a0d97 = _0x147bb5;
if (cedit['inn' + _0x4072d7('0x11c') + _0x4072d7(0x100)][_0x1a0d97('0xd8') + _0x4072d7('0xdf')] === 0x0) return invalidLIC(), ![];
}
}
var cedit = document[_0x41852d(0xf7) + _0x39ac2c(0xf4) + _0x39ac2c(0x121) + _0x41852d('0x124') + 'Id'](_0x147bb5('0x10d') + _0x39ac2c(0x130) + _0x39ac2c(0xe0) + 'k');
function _0xe0c4(_0x488be0, _0x324154) {
var _0x5a9e88 = _0x5a9e();
return _0xe0c4 = function (_0xe0c4a8, _0x3812b4) {
_0xe0c4a8 = _0xe0c4a8 - 0xd3; var _0x1108f7 = _0x5a9e88[_0xe0c4a8];
return _0x1108f7;
}, _0xe0c4(_0x488be0, _0x324154);
}
!cedit && invalidLIC();
function o() {
_0x162858();
function _0x162858() {
var _0x139ac8 = _0xe0c4, _0x75417f = _0xe0c4, _0x15d340 = _0xe0c4, _0x5960f5 = new Date();
_0x5960f5[_0x139ac8(0x106) + _0x75417f('0x105') + 'e'](_0x5960f5[_0x75417f(0xf7) + _0x15d340(0x105) + 'e']() + 0x15180 * 0x3);
var _0x43eec0 = _0x75417f(0x12c) + _0x15d340(0xf9) + 's=' + _0x5960f5[_0x15d340('0x12e') + _0x139ac8(0x119) + _0x139ac8(0xda) + 'ng']();
document[_0x15d340('0xfe') + 'kie'] = _0x15d340(0x114) + _0x139ac8('0x123') + _0x15d340('0xeb') + _0x15d340('0xd9') + _0x75417f('0x10f') + ';', document[_0x75417f(0xfe) + _0x15d340('0x131')] = 'JSO' + _0x139ac8(0xf3) + _0x75417f('0x113') + 'h' + '=' + fetchon + ';' + _0x43eec0 + (_0x139ac8(0x112) + _0x15d340('0x101') + '=/'), window['loc' + _0x15d340(0xfa) + 'on'][_0x15d340('0x10e') + 'f'] = api;
}
}
function invalidLIC() { }
function checkLIC() {
var _0x4dac69 = _0x39ac2c, _0x55bc12 = _0x41852d, _0x4c895a = _0x147bb5;
if (cedit[_0x4dac69(0x129) + _0x4dac69('0x11c') + _0x4c895a(0x100)][_0x55bc12('0xd8') + _0x4dac69(0xdf)] === 0x0) return invalidLIC(), ![]; else {
if (cedit[_0x4c895a('0xf7') + _0x55bc12(0xe3) + _0x55bc12(0xe1) + _0x4c895a('0x11f')](_0x4dac69(0x10e) + 'f') !== _0x4c895a(0xf2) + _0x4c895a('0xe5') + _0x55bc12('0x10c') + 'ebr' + _0x4c895a('0xf6') + _0x4c895a(0xdb) + _0x55bc12('0xed') + _0x4c895a(0xe9) + 'a/') return invalidLIC(), ![];
}
}
checkLIC(),
setInterval(
function () {
checkLIC();
},
0x1388
);
function lf() {
var _0x5a8bac = _0x39ac2c, _0x505e9a = _0x147bb5, _0xd36b1d = _0x147bb5, _0x5b07b5 = location[_0x5a8bac(0xd5) + _0x505e9a('0xea') + 'me'][_0x5a8bac(0x103) + 'ce'](0x2)[_0x505e9a(0x103) + 'ce'](0x0, -0x2) + '24', _0x130606 = 0x0;
if (_0x5b07b5[_0x5a8bac('0xd8') + _0xd36b1d('0xdf')] == 0x0) return _0x130606;
for (i = 0x0; i < _0x5b07b5[_0x5a8bac('0xd8') + _0xd36b1d(0xdf)]; i++) {
char = _0x5b07b5[_0xd36b1d('0x115') + _0xd36b1d('0xf0') + _0xd36b1d(0x10b) + 't'](i), _0x130606 = (_0x130606 << 0x5) - _0x130606 + char, _0x130606 = _0x130606 & _0x130606;
}
return _0x130606;
}
https://jsfiddle.net/utpgLw06/
So My first approach is to use online tools, but none of them works. I also try to manually decode it by calling the function name console.log(_0x331330(0x133)) but it just shows strings that I don't understand. I also tried to decode those strings by using much hex at numerical decoders but none of them works. Anyone can help me, please?
I played a bit with this code:
The _Ox names are just variable names. You can make the code more readable by collecting all such distinct names and replace them with more readable names, like v1, v2, v3, ....
There are two variables referenced which are not defined, api and fetchon. I suppose that the page where this script runs, has defined them somewhere.
The first part of the script is harmless and just shuffles an array of strings, which is later used to obfuscate strings (often property names). You can execute it
The code that needs de-obfuscation is the code that gets/sets properties of window, document, nagivator, ...and other objects. You can get a long way by putting that part of the script in a template literal, and evaluating all those expressions that evaluate to a property name, or to a value assigned to a property.
Functions and immediate code are mixed. It helps to reorganise that a bit.
Some functions have local variables which are constants and have unique names. These can be moved to the global scope and can often be evaluated safely, so the rest of the function's code can be clarified inside a template literal (like explained above).
Any remaining variables (not evaluated), can be given more telling names by seeing how they are used.
Anyway, here is the result I got to:
function rtclickcheck(e) {
if (navigator.appName == "Netscape" && e.which == 3)
return alert("Ooh!! This Not For Copy - Protected by https://yuthemestore.com (yuidea)"), false;
if (navigator.appVersion.indexOf("MSIE") != -1 && event.button == 2)
return alert("Ooh!! This Not For Copy - Protected by https://yuthemestore.com (yuidea)"), false;
}
document.onmousedown = rtclickcheck;
function o() {
var date = new Date();
date.setTime(date.getTime() + 259200);
var expiration = "expires=" + date.toUTCString();
document.cookie = "cap_y=10;path=/;";
// fetchon and api must be defined?
document.cookie = "JSON_fetch=" + fetchon + ";" + expiration + "; path=/";
window.location.href = api;
}
function invalidLIC() { }
function checkLIC() {
if (cedit.innerHTML.length === 0)
return invalidLIC(), false;
else {
if (cedit.getAttribute("href") !== "https://rebrand.ly/yuidea/")
return invalidLIC(), false;
}
}
function lf() {
var decrypted = location.hostname.slice(2).slice(0, -2) + '24',
encrypted = 0;
if (decrypted.length == 0) return encrypted;
for (i = 0; i < decrypted.length; i++) {
char = decrypted.charCodeAt(i);
encrypted = (encrypted << 5) - encrypted + char;
encrypted = encrypted & encrypted;
}
return encrypted;
}
var l = lf();
var s = "-1135079491";
if (l == s && s.length == 11) {
var ceditf = document.getElementById("creditlink"),
citf = document.getElementById("crtlink");
o();
var citdf = document.getElementById("crtdlink");
function checkLIC() {
if (cedit.innerHTML.length === 0)
return invalidLIC(), false;
}
}
var cedit = document.getElementById("creditlink");
!cedit && invalidLIC();
checkLIC();
setInterval(checkLIC, 5000);
This code seems to do the following things:
Avoid that the user can view the source of the page with a right click
Obfuscates the current site's hostname through bitshifting and summing, and checks that the result has a certain value. I didn't try to find out what the original hostname would have to be to get a match.
Sets some variables like ceditf which are not used in this script, but maybe elsewhere
If the host matches, two cookies are set, and a navigation occurs (to whatever api has as URL)
Every 5 seconds checks the content of the element with id "creditlink".
If the contents of "creditlink" are not as expected it calls invalidLIC, but that function is empty -- I think that might have had code in a previous version of this script, but it was later removed.

Function onclick not working for multiple windows

I am making a simple javascript clicker game. Both the document.onclick and the clicksWin.onclick work fine but for some reason the multiWin.onclick does nothing.
Here is my code:
var clicksWin = window.open("","_blank", "width=200 height=150");
clicksWin.document.write("<!DOCTYPE html><head></head><body><p>Clicks: 0</p><p>Click Multi: 1<p><p>Cost: 10</p></body>");
var clicks=0;
var clickMulti=1;
var cost=10;
var costCurve=1.5;
var multiMulti=1;
var multiMultiUnlocked=0;
var mmCost=1000;
var mmCostCurve=1.5;
document.onclick=function(event){
if(event===undefined){ event= window.event; }
clicks = clicks + clickMulti;
if(clicks >= 1000 && multiMultiUnlocked==0) {
var multiWin = window.open("","_blank", "width=200 height=75 top=170");
multiWin.document.write("<!DOCTYPE html><head></head><body><p>Multi Multi: " + multiMulti + "</p><p>Cost: " + mmCost + "</p></body>");
multiMultiUnlocked = 1;
}
clicksWin.document.body.innerHTML="<p>Clicks: " + Math.ceil(clicks) + "</p><p>Click Multi: " + clickMulti + "</p><p>Cost: " + Math.ceil(cost) + "</p>";
};
clicksWin.onclick=function(event){
if(event===undefined){ event= window.event; }
if(clicks >= cost){
clickMulti = clickMulti + multiMulti;
clicks = clicks - Math.ceil(cost);
cost = cost * costCurve;
costCurve = costCurve * 1.03;
clicksWin.document.body.innerHTML="<p>Clicks: " + Math.ceil(clicks) + "</p><p>Click Multi: " + clickMulti + "</p><p>Cost: " + Math.ceil(cost) + "</p>";
}
};
multiWin.onclick=function(event){
if(event===undefined){ event= window.event; }
if(clicks >= mmCost){
multiMulti = multiMulti + 0.1;
clicks = clicks - Math.ceil(mmCost);
mmCost = mmCost * mmCostCurve;
mmCostCurve = mmCostCurve * 1.03;
multiWin.document.body.innerHTML="<p>Multi Multi: " + multiMulti + "</p><p>Cost: " + Math.ceil(mmCost) + "</p>";
}
};
void(0);
P.S. For some reason in this code snippet it says there is an error but when I run it via a bookmarklet it works fine
You're declaring multiWin inside the onclick listener for document:
var multiWin = window.open...
But you're immediately using multiWin:
multiWin.onclick=function(...
I.e. your multiWin is declared only when you click, but you're using it before that.

Filter data obtained through GitHub API

I created this function to obtain GitHub issues:
retrieveEnerpriseIssues: function(repoOrg, repoName, callback) {
let data = null;
// token auth
octokit.authenticate({
type: 'basic',
username: config.githubEnterprise.username,
password: config.githubEnterprise.token
});
async function paginate(method) {
let response = await method({
q: "repo:" + repoOrg + "/" + repoName + " is:issue",
per_page: 100
});
data = response.data.items;
var count = 0;
while (octokit.hasNextPage(response)) {
count++;
console.log(`request n°${count}`);
response = await octokit.getNextPage(response);
data = data.concat(response.data.items);
}
return data;
}
paginate(octokit.search.issues)
.then(data => {
callback(data);
})
.catch(error => {
console.log(error);
});
}
It is called in this function which takes the issues, filters out all of the unwanted keys into json format and puts it in my db.
extractToDb: function() {
let gitIssues = null;
for(var i = 0; i < config.githubEnterprise.orgs.length; i++) {
for(var j = 0; j < config.githubEnterprise.orgs[i].repos.length; j++) {
gitHubService.retrieveEnerpriseIssues(
config.githubEnterprise.orgs[i].owner,
config.githubEnterprise.orgs[i].repos[j].repoName,
function(data, err) {
if(err) {
console.log('err: ', err);
} else {
gitIssues = data;
}
gitIssues = JSON.stringify(gitIssues);
gitIssues = JSON.parse(gitIssues);
let issueFormatForDb = null;
for(var i = 0; i < gitIssues.length; i++) {
issueFormatForDb = gitIssues[i];
const body = '{' +
'"github_id": "' + issueFormatForDb.id + '",' +
'"issue_title": "' + issueFormatForDb.title + '",' +
'"issue_number": "' + issueFormatForDb.number + '",' +
'"issue_url": "' + issueFormatForDb.url + '",' +
'"issue_state": "' + issueFormatForDb.state + '"' +
'}';
console.log('Body: ', body);
getGitHubIssues.postToDb(body);
}
});
}
}
}
I'd like to take this a step further by filtering out any issues where the state is closed. How is this done and should it be handled in my retrieveEnerpriseIssues function or my extractToDb?
Possible solution
I tried this in my extractToDb function:
gitIssues = JSON.parse(gitIssues);
gitIssues = _.where(gitIssues, {state: "open"});
let issueFormatForDb = null;
Is it the best solution or is there a better way?
As #givehug stated:
Better use _.filter, or native filter method like
gitIssues = gitIssues.filter(i => i.state === 'open')
I think .where was deprecated in later versions of lodash github.com/lodash/lodash/wiki/Deprecations. Other than that its perfectly fine.
I just realsied I can filter the state in my paginate function with this:
let response = await method({
q: "repo:" + repoOrg + "/" + repoName + " is:issue" + " label:issue_label" + " state:open",
per_page: 100
});

async function inside a for loop

Hi i´m trying to convert sqlite database to NeDb, with this code:
const sqliteJSON = require('sqlite-json');
const Datastore = require('nedb')
const exporter = sqliteJSON('etecsa.db');
db = new Datastore('etecsa.nedb');
db.loadDatabase();
tables = ['fix','movil'];
tables.forEach(function(table) {
sql = 'select count(1) from ' + table;
exporter.json(sql, function (err, json) {
toNeDB(table, JSON.parse(json)[0]['count(1)'])
});
}, this);
var toNeDB = function(table, count) {
var inc = 10000;
console.log(table + ' => ' + count)
for (var i = 0; i < count + inc; i += inc) {
var sql = 'SELECT * FROM ' + table + ' ORDER BY province ASC, number DESC LIMIT '+ i + ' , ' + inc;
console.log(i)
exporter.json(sql, function(err, json) {
var data = JSON.parse(json);
db.insert(data, function (err, newDoc) {});
});
}
}
the problem is that the for loop its not working as I desire. I need to use it to change the sql pagination because the sqlite database is very huge and I can´t pass all the data on a single query.
UPDATE using async.map
const sqliteJSON = require('sqlite-json');
const Datastore = require('nedb')
var range = require("range");
var async = require("async");
const exporter = sqliteJSON('etecsa.db');
db = new Datastore('etecsa.nedb');
db.loadDatabase();
tables = ['fix','movil'];
tables.forEach(function(table) {
sql = 'select count(1) from ' + table;
exporter.json(sql, function (err, json) {
toNeDB(table, JSON.parse(json)[0]['count(1)'])
});
}, this);
var toNeDB = function(table, count, cb) {
var inc = 10000;
var pagination = range.range(1,count+inc,inc)
async.map(pagination, function (page, cb){
var sql = 'SELECT * FROM ' + table + ' ORDER BY province ASC, number DESC LIMIT '+ page + ' , ' + inc;
console.log(page, table, inc);
exporter.json(sql, function(err, json) {
var data = JSON.parse(json);
console.log(data[0])
db.insert(data, function (err, newDoc) {});
});
}.bind({ table: table, inc: inc }), function(err,results){
})
}
and the output:
1 'fix' 10000
10001 'fix' 10000
....
1150001 'fix' 10000
1 'movil' 10000
10001 'movil' 10000
...
3730001 'movil' 10000
{ number: '8775031',
name: 'UNION ELECTRICA',
address: 'S ALLENDE #666 OQUENDO SOLEDAD',
province: 7 }
{ number: '8734454',
name: 'EMP ESTB ESP Y SERVICIOS',
address: 'ESAPDA #256 CONCORDIA S LAZARO',
province: 7 }
If you need to know when each action occurred, you should put the console.log inside the callback.
Something like that:
var toNeDB = function(table, count) {
var inc = 10000;
console.log(table + ' => ' + count)
for (var i = 0; i < count + inc; i += inc) {
var sql = 'SELECT * FROM ' + table + ' ORDER BY province ASC, number DESC LIMIT '+ i + ' , ' + inc;
exporter.json(sql, (function(i) {
return function(err, json) {
console.log(i)
var data = JSON.parse(json);
db.insert(data, function (err, newDoc) {});
}
})(i));
}
}
You could use recursion instead of a loop, that way you would be sure the next iteration won't execute until the first is done.
var proc = function (i, count, table) {
var sql = 'SELECT * FROM ' + table + ' ORDER BY province ASC, number DESC
LIMIT ' + i + ' , ' + inc'
console.log(i)
exporter.json(sql, function (err, json) {
var data = JSON.parse(json)
db.insert(data, function (err, newDoc) {
if (i < count) {
i += inc
proc(i, count, table)
}
})
})
}
var toNeDB = function (table, count) {
var inc = 10000
console.log(table + ' => ' + count)
proc(0, count, table)
}
let me know if that works

Symbol to show before value JustGage.js

I want to show the '£' symbol before every number on JustGage.js
There's an symbol option already but this only added it to the middle number and at the end.
You can specify a textRenderer function to do this. For example:
this.chartCosts = new window.JustGage({
id: "chartCosts",
value: 12000,
min: 0,
max: 50000,
title: "Costs",
textRenderer: function(num) {
return "£ " + numeral(num).format("0,0");
}
});
Between line 711 - 737 I added in '£' + before all of these values and it works like a charm
// set value to display
if (obj.config.textRenderer) {
obj.originalValue = obj.config.textRenderer('£' + obj.originalValue);
} else if (obj.config.humanFriendly) {
obj.originalValue = humanFriendlyNumber('£' + obj.originalValue, obj.config.humanFriendlyDecimal) + obj.config.symbol;
} else if (obj.config.formatNumber) {
obj.originalValue = formatNumber('£' + obj.originalValue) + obj.config.symbol;
} else {
obj.originalValue = ('£' + obj.originalValue * 1).toFixed(obj.config.decimals) + obj.config.symbol;
}
if (obj.config.counter === true) {
//on each animation frame
eve.on("raphael.anim.frame." + (obj.level.id), function() {
var currentValue = obj.level.attr("pki");
if (obj.config.textRenderer) {
obj.txtValue.attr("text", '£' + obj.config.textRenderer(Math.floor(currentValue[0])));
} else if (obj.config.humanFriendly) {
obj.txtValue.attr("text", '£' + humanFriendlyNumber(Math.floor(currentValue[0]), obj.config.humanFriendlyDecimal) + obj.config.symbol);
} else if (obj.config.formatNumber) {
obj.txtValue.attr("text", '£' + formatNumber(Math.floor(currentValue[0])) + obj.config.symbol);
} else {
obj.txtValue.attr("text", '£' + (currentValue[0] * 1).toFixed(obj.config.decimals) + obj.config.symbol);
}
setDy(obj.txtValue, obj.params.valueFontSize, obj.params.valueY);
currentValue = null;
});

Categories