Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. //
  4. //New world object!!
  5. //
  6.  
  7.  
  8.  
  9.  
  10. //localstorage if true or not
  11. var localStorageStatus;
  12. //This is for local sql
  13. var sql;
  14.  
  15. var syncHasFirstID = localStorage.syncHasFirstID;
  16.  
  17.  
  18. var worker = new Worker('js/worker.js');
  19. var work = new Object;
  20.  
  21. //var xml
  22. var xml;
  23.  
  24. var syncServerCount;
  25. var syncServerFirstID;
  26. var syncServerLastID;
  27.  
  28. //var json
  29. //var json;
  30. var json2;
  31. var jsonStore = localStorage.jsonStore;
  32. //var json;
  33. //var jsonReturn =  new Object;
  34.  
  35. // time and date
  36. var localTime = new Date();
  37. var localTimeCurrent = localTime.getTime();
  38.  
  39. //compare server id with local id
  40. var syncGetIDCompareVar;
  41.  
  42.  
  43.  
  44.  
  45. var sync =  new Object;
  46. sync.dev = true;
  47. sync.lastID;
  48.  
  49.  
  50. //keep updating
  51. var keepUpdate = false;
  52. var workerGet = true;
  53.  
  54.  
  55.  
  56. sync.localtime = new Date().getTime();
  57.  
  58.  
  59. function jsonReturn(jsonReturn)
  60. {
  61.    
  62.     json =  new Object;
  63.     this.accuracy = json.accuracy;
  64.     this.heading = json.heading;
  65.     this.id = json.id;
  66.     this.ip = json.ip;
  67.     this.latitude = json.latitude;
  68.     this.longitude = json.longitude;
  69.     this.online = json.online;
  70.     this.pointdate = json.pointdate;
  71.     this.serverstamp = json.serverstamp;
  72.     this.speed = json.speed;
  73.     this.timespent = json.timespent;
  74.     return json;
  75. }
  76.  
  77. var jsonReturn = new jsonReturn(this);
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. /**********************************/
  86.  
  87. /**********************************/
  88.  
  89.  
  90. function gettime(){return new Date().getTime();}
  91. //var currentTime = new Date().getTime();
  92. var timestamp = new Date().getTime();
  93.  
  94.  
  95. /***************************************************
  96. Error loging for the SQL querys
  97. /***************************************************/
  98.  
  99. /***Error code return on query***/
  100. function sqlQueryError(transaction, error)
  101. {
  102.     $('#log').prepend('Error was '+error.message+' (Code '+error.code+')<br/>');// loging errors tot he page
  103.     console.log(''+'Oops.  Error was '+error.message+' (Code '+error.code+')');// loging errors to the console\
  104.     console.log(error);
  105.     var error_fatal = true;
  106.     if (error_fatal) return true;
  107.     return false;
  108. }
  109. /***  When the DB fails this returns the error code  ***/
  110. function sqlDbError(error){// "error" is the error codes 1-7 for local SQL
  111.     if (error == 1) { $('#log').prepend('The statement failed for database reasons not covered by any other error code.<br/>');}
  112.     if (error == 2) {$('#log').prepend('The operation failed because the actual database version was not what it should be.<br/>');}
  113.     if (error == 3) {$('#log').prepend('The statement failed because the data returned from the database was too large.<br/>');}
  114.     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/>');}
  115.     if (error == 5) {$('#log').prepend('The statement failed because of a syntax error.<br/>');}
  116.     if (error == 6) {$('#log').prepend('An INSERT, UPDATE, or REPLACE statement failed due to a constraint failure.<br/>');}
  117.     if(error == 7){$('#log').prepend('A lock for the transaction could not be obtained in a reasonable time.<br/>');}
  118.     else {/***The error is un-known or error == 0***/console.log(''+'Unknown error '+error);}
  119. }
  120. /***Return for NULL local sql query.***/
  121. function sqlNullHandler(transaction, results) { sync.lastID = true; console.log(transaction);console.log(results); } // should remove this
  122.  
  123. //debug info
  124. function debugJsonReturn() {
  125. console.log('debugJsonReturn = run');
  126.     $.ajax({
  127.             type: "GET",
  128.             url: "sync.php?json=true",
  129.             dataType: "json",
  130.             data: json,
  131.             success: function(jsonReturn) {
  132.                 var jsson = jsonReturn;
  133.                 console.debug(jsson.length);
  134.                 console.debug(jsson.id);
  135.                 console.debug(jsson);
  136.                 console.debug(jsson);
  137.                 console.debug(jsson);
  138.                 console.debug(jsson);
  139.             }
  140.     });
  141. console.log('debugJsonReturn = end');
  142. }
  143.  
  144.  
  145. /**********************************
  146.     DB setup   
  147. /**********************************/
  148. function sqlSetup() {
  149. console.log('sqlSetup = run');
  150.     try {
  151.         if (!window.openDatabase) {
  152.             //$('#error').prepend('<p>DataBase is not supported.</p>').show().delay(1500).slideUp('slow');
  153.             console.log('DataBase is not supported.');
  154.             }
  155.             else {
  156.                 console.log('SQL is about to start');
  157.                 var sqlShortName = 'cync';
  158.                 var sqlVersion = '1.0';
  159.                 var sqlDisplayName = 'jsSync';
  160.                 var sqlMaxSize = 65536;
  161.                
  162.                 sql = openDatabase(sqlShortName, sqlVersion, sqlDisplayName, sqlMaxSize);//this is the DB been setup
  163.                
  164.                 console.log('SQL setup is shortName: '+sqlShortName+' | version: '+sqlVersion+' | displayName: '+sqlDisplayName+' | and maxSize: '+sqlMaxSize);
  165.                 sqlCreateTables();
  166.         }
  167.     }
  168.     catch(error) {//This is a catch for any errors on the DB setup
  169.         sqlDbError(error);
  170.     }  
  171. console.log('sqlSetup = end');
  172. }
  173.  
  174. sqlSetup();
  175.  
  176.  
  177. /**********************************
  178.     Setup SQL tables   
  179. /**********************************/
  180.  
  181.  
  182. function sqlCreateTables() {
  183. console.log('sqlCreateTables = run');  
  184.     sql.transaction(
  185.         function (transaction) {
  186.             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  "");',
  187.             [],
  188.             initSync,
  189.             sqlQueryError);
  190.         }
  191.     );
  192.     console.log('Table was inserted into the database');
  193.    
  194. console.log('sqlCreateTables = end');
  195. }
  196.  
  197.  
  198. /**********************************
  199.        
  200. /**********************************/
  201. function initSync(transaction, results)
  202. {
  203.     console.log('initSync = run');
  204.    
  205. if(keepUpdate == false){
  206. if(workerGet == true){
  207.    
  208. }else{
  209.  
  210. }
  211.  
  212. }else{
  213. if(workerGet == true){
  214.  
  215. }else{
  216.  
  217. }
  218.         console.log(sync.lastID);
  219.         sql.transaction(
  220.         function (transaction) {
  221.             transaction.executeSql('SELECT * FROM sync ASC LIMIT 1',
  222.                 [],
  223.                 sqlFirstID,
  224.                 sqlQueryError);
  225.             }
  226.         );
  227. }
  228.     console.log('initSync = end');
  229. }
  230.  
  231.  
  232.  
  233.  
  234. /**********************************
  235.        
  236. /**********************************/
  237. function sqlFirstID(transaction, results){
  238.     console.log('sqlFirstID = run');
  239.     if(results.rows.length <=0){
  240.    
  241.         localStorage.syncHasFirstID = false;
  242.         //console.log('syncHasFirstID = false');
  243.         //getServerCount();
  244.    
  245.    
  246.        
  247.         getServerCounta();
  248.        
  249.     }else{
  250.    
  251.     sql.transaction(
  252.         function (transaction) {
  253.             transaction.executeSql('SELECT * FROM sync ORDER BY id DESC LIMIT 1',
  254.             [],
  255.             sqlLastID,
  256.             sqlQueryError);
  257.         }
  258.     );
  259.        
  260.         console.log('else = run');
  261.     }
  262.     //return false;
  263. console.log('sqlFirstID = end');
  264. }
  265. /**********************************
  266.        
  267. /**********************************/
  268. function getServerCounta() {
  269. console.log(''+'getServerCount = run');
  270.     if(localStorage.jsonStoreid < 0){
  271.         console.log(''+'localStorage.jsonStoreid < 0')
  272.     }else{
  273.    
  274.         $.ajax({
  275.             type: "GET",
  276.             url: "sync.php?json=true",
  277.             dataType: "json",
  278.             data: json,
  279.             success: function(json) {
  280.                
  281.  
  282.                 syncUpdateLocala(json);
  283.                
  284.             }
  285.            
  286.         });
  287.        
  288.     }
  289. console.log(''+'getServerCount = end');
  290. }
  291. /**********************************
  292.        
  293. /**********************************/
  294. function sqlLastID(transaction, results){
  295.  
  296.     for (var i=0; i<results.rows.length; i++) {
  297.             var row = results.rows.item(i);
  298.            
  299.             json.id = row['id'];
  300.            
  301.             getServerCountPlusa(json);
  302.     }
  303.    
  304. }
  305. /**********************************
  306.        
  307. /**********************************/
  308.  
  309. function getServerCountPlusa(json) {
  310.     console.log(''+'getServerCountPlus = run');
  311.    
  312.     worker.postMessage(json.id);
  313.    
  314.     console.log(''+'localStorage.jsonStoreid = '+json.id);
  315.     $.ajax({
  316.             type: 'GET',
  317.             url: 'sync.php?json=true&next='+json.id,
  318.             dataType: 'json',
  319.             data: json,
  320.             success: function(json) {
  321.                
  322.                 if(!json){
  323.                     console.log('json = false');
  324.                     localStorage.jsonStoreid = null;
  325.                     localStorage.jsonStoretimestamp = null;
  326.                     localStorage.jsonStoreservertime = null;
  327.                     localStorage.jsonStoreaboutapp = null;
  328.                 }else{
  329.                
  330.                     console.log('getServerCount = run');
  331.                     json.content = json.aboutapp;
  332.                     console.log(json.id+':'+json.timestamp+':'+json.servertime+':'+json.aboutapp+':'+json.content);
  333.    
  334.  
  335.                    
  336.                     //console.log(''+'syncServerCount: '+syncServerCount+' syncServerFirstID: '+syncServerFirstID+' syncServerLastID: '+syncServerLastID);
  337.                    
  338.                     //syncGetID(syncServerID);
  339.                     syncUpdateLocala(json);
  340.                 }
  341.             }
  342.            
  343.         });
  344. console.log(''+'getServerCountPlus = end');
  345. }
  346. function syncUpdateLocala(json){
  347. console.log(''+'syncUpdateLocal = run');
  348.    
  349.  
  350.     sql.transaction(
  351.         function (transaction) {
  352.             transaction.executeSql('INSERT INTO sync (serverid,localstamp,serverstamp,timestamp) VALUES (?,?,?,?);',
  353.                                             [json.id,sync.localtime,json.serverstamp,json.timespent],
  354.             initSync,
  355.             sqlQueryError);
  356.         }
  357.     );
  358.     console.log(json);
  359.     $('#log').text('someshiot');
  360.     //getServerCountPlus();
  361. console.log(''+'syncUpdateLocala = end');
  362. }
  363.  
  364. /**********************************
  365.        
  366. /**********************************/
  367.  
  368. /**********************************
  369.        
  370. /**********************************/
  371.  
  372. /**********************************
  373.        
  374. /**********************************/
  375.  
  376. /**********************************
  377.        
  378. /**********************************/
  379.  
  380. /**********************************
  381.        
  382. /**********************************/
  383.  
  384.  
  385. /**********************************
  386.        
  387. /**********************************/
  388. worker.onmessage = function (event) {
  389.    
  390.     var work = JSON.parse(event.data);
  391.     console.log(work);
  392.    
  393.     if(keepUpdate == true){
  394.  
  395.    
  396.    
  397.    
  398.    
  399.     this.postMessage(sendback);
  400.    
  401.    
  402.     }
  403.    
  404.  
  405.    
  406.     if (localStorage.Success) {localStorage.Success=Number(localStorage.Success) +1;}
  407.     else {localStorage.Success=1;}
  408.    
  409.     //console.log(localStorage.Success);
  410.    
  411.  
  412.  
  413. };
  414.  
  415.  
  416. /**********************************
  417.        
  418. /**********************************/
  419. $(document).ready(function() {
  420.    
  421.     $('#getID').click(function() {
  422.  
  423.        
  424.     });
  425.     $('#worker #start').click(function() {
  426.         console.log(''+'click = true');
  427.         keepUpdate = true;
  428.        
  429.     });
  430.     $('#worker #stop').click(function() {
  431.         console.log(''+'click = true');
  432.         keepUpdate = false;
  433.         //syncServer();
  434.     });
  435.         $('#keepupdate #start').click(function() {
  436.         console.log(''+'click = true');
  437.         keepUpdate = true;
  438.        
  439.     });
  440.     $('#keepupdate #stop').click(function() {
  441.         console.log(''+'click = true');
  442.         keepUpdate = false;
  443.         //syncServer();
  444.     });
  445.     $('#log').ajaxSuccess(function() {
  446.         if (localStorage.ajaxSuccess) {localStorage.ajaxSuccess=Number(localStorage.ajaxSuccess) +1;}
  447.         else {localStorage.ajaxSuccess=1;}
  448.       $(this).text('Triggered ajaxSuccess handler. Count: '+localStorage.ajaxSuccess);
  449.     });
  450.    
  451.      
  452.        
  453.     setInterval((function () {
  454.        
  455.         if(keepUpdate == true){
  456.        
  457.         }
  458.         var sendback = '{"getID":"true","lastID":"52"}';
  459.         worker.postMessage(sendback);
  460.          //worker.postMessage(localStorage.Success);
  461.         console.log('10 Seconds');
  462.         return arguments.callee;
  463.     })(), 90000);
  464.    
  465. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement