Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //New world object!!
- //
- //localstorage if true or not
- var localStorageStatus;
- //This is for local sql
- var sql;
- var syncHasFirstID = localStorage.syncHasFirstID;
- var worker = new Worker('js/worker.js');
- var work = new Object;
- //var xml
- var xml;
- var syncServerCount;
- var syncServerFirstID;
- var syncServerLastID;
- //var json
- //var json;
- var json2;
- var jsonStore = localStorage.jsonStore;
- //var json;
- //var jsonReturn = new Object;
- // time and date
- var localTime = new Date();
- var localTimeCurrent = localTime.getTime();
- //compare server id with local id
- var syncGetIDCompareVar;
- var sync = new Object;
- sync.dev = true;
- sync.lastID;
- //keep updating
- var keepUpdate = false;
- var workerGet = true;
- sync.localtime = new Date().getTime();
- function jsonReturn(jsonReturn)
- {
- json = new Object;
- this.accuracy = json.accuracy;
- this.heading = json.heading;
- this.id = json.id;
- this.ip = json.ip;
- this.latitude = json.latitude;
- this.longitude = json.longitude;
- this.online = json.online;
- this.pointdate = json.pointdate;
- this.serverstamp = json.serverstamp;
- this.speed = json.speed;
- this.timespent = json.timespent;
- return json;
- }
- var jsonReturn = new jsonReturn(this);
- /**********************************/
- /**********************************/
- function gettime(){return new Date().getTime();}
- //var currentTime = new Date().getTime();
- var timestamp = new Date().getTime();
- /***************************************************
- Error loging for the SQL querys
- /***************************************************/
- /***Error code return on query***/
- function sqlQueryError(transaction, error)
- {
- $('#log').prepend('Error was '+error.message+' (Code '+error.code+')<br/>');// loging errors tot he page
- console.log(''+'Oops. Error was '+error.message+' (Code '+error.code+')');// loging errors to the console\
- console.log(error);
- var error_fatal = true;
- if (error_fatal) return true;
- return false;
- }
- /*** When the DB fails this returns the error code ***/
- function sqlDbError(error){// "error" is the error codes 1-7 for local SQL
- if (error == 1) { $('#log').prepend('The statement failed for database reasons not covered by any other error code.<br/>');}
- if (error == 2) {$('#log').prepend('The operation failed because the actual database version was not what it should be.<br/>');}
- if (error == 3) {$('#log').prepend('The statement failed because the data returned from the database was too large.<br/>');}
- if (error == 4) {$('#log').prepend('The statement failed because there was not enough remaining storage space, or the storage quota was reached and the user declined to give more space to the database.<br/>');}
- if (error == 5) {$('#log').prepend('The statement failed because of a syntax error.<br/>');}
- if (error == 6) {$('#log').prepend('An INSERT, UPDATE, or REPLACE statement failed due to a constraint failure.<br/>');}
- if(error == 7){$('#log').prepend('A lock for the transaction could not be obtained in a reasonable time.<br/>');}
- else {/***The error is un-known or error == 0***/console.log(''+'Unknown error '+error);}
- }
- /***Return for NULL local sql query.***/
- function sqlNullHandler(transaction, results) { sync.lastID = true; console.log(transaction);console.log(results); } // should remove this
- //debug info
- function debugJsonReturn() {
- console.log('debugJsonReturn = run');
- $.ajax({
- type: "GET",
- url: "sync.php?json=true",
- dataType: "json",
- data: json,
- success: function(jsonReturn) {
- var jsson = jsonReturn;
- console.debug(jsson.length);
- console.debug(jsson.id);
- console.debug(jsson);
- console.debug(jsson);
- console.debug(jsson);
- console.debug(jsson);
- }
- });
- console.log('debugJsonReturn = end');
- }
- /**********************************
- DB setup
- /**********************************/
- function sqlSetup() {
- console.log('sqlSetup = run');
- try {
- if (!window.openDatabase) {
- //$('#error').prepend('<p>DataBase is not supported.</p>').show().delay(1500).slideUp('slow');
- console.log('DataBase is not supported.');
- }
- else {
- console.log('SQL is about to start');
- var sqlShortName = 'cync';
- var sqlVersion = '1.0';
- var sqlDisplayName = 'jsSync';
- var sqlMaxSize = 65536;
- sql = openDatabase(sqlShortName, sqlVersion, sqlDisplayName, sqlMaxSize);//this is the DB been setup
- console.log('SQL setup is shortName: '+sqlShortName+' | version: '+sqlVersion+' | displayName: '+sqlDisplayName+' | and maxSize: '+sqlMaxSize);
- sqlCreateTables();
- }
- }
- catch(error) {//This is a catch for any errors on the DB setup
- sqlDbError(error);
- }
- console.log('sqlSetup = end');
- }
- sqlSetup();
- /**********************************
- Setup SQL tables
- /**********************************/
- function sqlCreateTables() {
- console.log('sqlCreateTables = run');
- sql.transaction(
- function (transaction) {
- transaction.executeSql('CREATE TABLE IF NOT EXISTS `sync` (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `serverid` text NOT NULL DEFAULT "", `localstamp` text NOT NULL DEFAULT "", `serverstamp` text NOT NULL DEFAULT "", `timestamp` text NOT NULL DEFAULT "",`updatestamp` text NOT NULL DEFAULT "", `ip` text NOT NULL DEFAULT "", `latitude` text NOT NULL DEFAULT "", `longitude` text NOT NULL DEFAULT "", `accuracy` text NOT NULL DEFAULT "", `heading` text NOT NULL DEFAULT "", `speed` text NOT NULL DEFAULT "", `altitudeAccuracy` text NOT NULL DEFAULT "", `online` text NOT NULL DEFAULT "", `pointdate` text NOT NULL DEFAULT "", `pointid` text NOT NULL DEFAULT "", `timespent` text NOT NULL DEFAULT "");',
- [],
- initSync,
- sqlQueryError);
- }
- );
- console.log('Table was inserted into the database');
- console.log('sqlCreateTables = end');
- }
- /**********************************
- /**********************************/
- function initSync(transaction, results)
- {
- console.log('initSync = run');
- if(keepUpdate == false){
- if(workerGet == true){
- }else{
- }
- }else{
- if(workerGet == true){
- }else{
- }
- console.log(sync.lastID);
- sql.transaction(
- function (transaction) {
- transaction.executeSql('SELECT * FROM sync ASC LIMIT 1',
- [],
- sqlFirstID,
- sqlQueryError);
- }
- );
- }
- console.log('initSync = end');
- }
- /**********************************
- /**********************************/
- function sqlFirstID(transaction, results){
- console.log('sqlFirstID = run');
- if(results.rows.length <=0){
- localStorage.syncHasFirstID = false;
- //console.log('syncHasFirstID = false');
- //getServerCount();
- getServerCounta();
- }else{
- sql.transaction(
- function (transaction) {
- transaction.executeSql('SELECT * FROM sync ORDER BY id DESC LIMIT 1',
- [],
- sqlLastID,
- sqlQueryError);
- }
- );
- console.log('else = run');
- }
- //return false;
- console.log('sqlFirstID = end');
- }
- /**********************************
- /**********************************/
- function getServerCounta() {
- console.log(''+'getServerCount = run');
- if(localStorage.jsonStoreid < 0){
- console.log(''+'localStorage.jsonStoreid < 0')
- }else{
- $.ajax({
- type: "GET",
- url: "sync.php?json=true",
- dataType: "json",
- data: json,
- success: function(json) {
- syncUpdateLocala(json);
- }
- });
- }
- console.log(''+'getServerCount = end');
- }
- /**********************************
- /**********************************/
- function sqlLastID(transaction, results){
- for (var i=0; i<results.rows.length; i++) {
- var row = results.rows.item(i);
- json.id = row['id'];
- getServerCountPlusa(json);
- }
- }
- /**********************************
- /**********************************/
- function getServerCountPlusa(json) {
- console.log(''+'getServerCountPlus = run');
- worker.postMessage(json.id);
- console.log(''+'localStorage.jsonStoreid = '+json.id);
- $.ajax({
- type: 'GET',
- url: 'sync.php?json=true&next='+json.id,
- dataType: 'json',
- data: json,
- success: function(json) {
- if(!json){
- console.log('json = false');
- localStorage.jsonStoreid = null;
- localStorage.jsonStoretimestamp = null;
- localStorage.jsonStoreservertime = null;
- localStorage.jsonStoreaboutapp = null;
- }else{
- console.log('getServerCount = run');
- json.content = json.aboutapp;
- console.log(json.id+':'+json.timestamp+':'+json.servertime+':'+json.aboutapp+':'+json.content);
- //console.log(''+'syncServerCount: '+syncServerCount+' syncServerFirstID: '+syncServerFirstID+' syncServerLastID: '+syncServerLastID);
- //syncGetID(syncServerID);
- syncUpdateLocala(json);
- }
- }
- });
- console.log(''+'getServerCountPlus = end');
- }
- function syncUpdateLocala(json){
- console.log(''+'syncUpdateLocal = run');
- sql.transaction(
- function (transaction) {
- transaction.executeSql('INSERT INTO sync (serverid,localstamp,serverstamp,timestamp) VALUES (?,?,?,?);',
- [json.id,sync.localtime,json.serverstamp,json.timespent],
- initSync,
- sqlQueryError);
- }
- );
- console.log(json);
- $('#log').text('someshiot');
- //getServerCountPlus();
- console.log(''+'syncUpdateLocala = end');
- }
- /**********************************
- /**********************************/
- /**********************************
- /**********************************/
- /**********************************
- /**********************************/
- /**********************************
- /**********************************/
- /**********************************
- /**********************************/
- /**********************************
- /**********************************/
- worker.onmessage = function (event) {
- var work = JSON.parse(event.data);
- console.log(work);
- if(keepUpdate == true){
- this.postMessage(sendback);
- }
- if (localStorage.Success) {localStorage.Success=Number(localStorage.Success) +1;}
- else {localStorage.Success=1;}
- //console.log(localStorage.Success);
- };
- /**********************************
- /**********************************/
- $(document).ready(function() {
- $('#getID').click(function() {
- });
- $('#worker #start').click(function() {
- console.log(''+'click = true');
- keepUpdate = true;
- });
- $('#worker #stop').click(function() {
- console.log(''+'click = true');
- keepUpdate = false;
- //syncServer();
- });
- $('#keepupdate #start').click(function() {
- console.log(''+'click = true');
- keepUpdate = true;
- });
- $('#keepupdate #stop').click(function() {
- console.log(''+'click = true');
- keepUpdate = false;
- //syncServer();
- });
- $('#log').ajaxSuccess(function() {
- if (localStorage.ajaxSuccess) {localStorage.ajaxSuccess=Number(localStorage.ajaxSuccess) +1;}
- else {localStorage.ajaxSuccess=1;}
- $(this).text('Triggered ajaxSuccess handler. Count: '+localStorage.ajaxSuccess);
- });
- setInterval((function () {
- if(keepUpdate == true){
- }
- var sendback = '{"getID":"true","lastID":"52"}';
- worker.postMessage(sendback);
- //worker.postMessage(localStorage.Success);
- console.log('10 Seconds');
- return arguments.callee;
- })(), 90000);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement