Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.55 KB | None | 0 0
  1. function onDeviceReady(){
  2. //global variables, bad practice I know
  3. user = 'user';
  4. pass = 'pass';
  5. deviceID = device.uuid;
  6. console.log(deviceID);
  7. db = window.sqlitePlugin.openDatabase({name: 'mood.db', location: 'default'},function(){
  8. db.transaction(function(tx){
  9. tx.executeSql('CREATE TABLE IF NOT EXISTS update_table (uid integer primary key autoincrement, timestamp)',[],function(){},function(e){
  10. console.log(e.message);
  11. });
  12. tx.executeSql('CREATE TABLE IF NOT EXISTS mood_table (uid integer primary key autoincrement, uuid, id, smileyid, textdata, activity, created, last_change, deleted)',[],function(){},function(e){
  13. console.log(e.message);
  14. });
  15. tx.executeSql('SELECT * FROM update_table',[],function(tx, res){
  16. if(res.rows.length === 0){
  17. tx.executeSql('INSERT INTO update_table (timestamp) VALUES (?)',[0],function(){},function(e){
  18. console.log(e.message);
  19. });
  20. }
  21. },function(e){
  22. console.log(e.message);
  23. });
  24. checkForUpdates();
  25. },function(e){
  26. console.log(e.message);
  27. });
  28. },function(e){
  29. console.log(JSON.stringify(e));
  30. });
  31. }
  32.  
  33. function make_base_auth(username, password) {
  34. var tok = username + ':' + password;
  35. var hash = btoa(tok);
  36. return 'Basic ' + hash;
  37. }
  38.  
  39. function checkForUpdates(){
  40. console.log('checkForUpdates');
  41. //internet connection check
  42. if(navigator.connection.type != Connection.NONE) {
  43. db.executeSql('SELECT * FROM update_table WHERE uid = 1',[],function(res){
  44. var data,
  45. new_tsmp,
  46. tsmp = (typeof res.rows.item(0).timestamp === undefined) ? '0' : res.rows.item(0).timestamp;
  47. console.log(tsmp);
  48. $.when(
  49. $.ajax({
  50. url: 'http://mydomain.de/my.php',
  51. crossDomain: true,
  52. timeout: 30000,
  53. method: 'GET',
  54. dataType: 'text',
  55. data: {'command': 'check_update',
  56. 'unix_time': tsmp,
  57. 'uuid': deviceID},
  58. xhrFields: {
  59. withCredentials: true
  60. },
  61. beforeSend: function(xhr){
  62. xhr.setRequestHeader('Authorization', make_base_auth(user, pass));
  63. },
  64. success: function(textdata, status, jqXHR){
  65. data = $.parseJSON(textdata);
  66. //check for updates
  67. console.log(JSON.stringify(data));
  68. console.log(status);
  69. },
  70. error: ajaxErrorCb
  71. })
  72. ).done(function(){
  73. switch(data.case){
  74. case 'update':
  75. //update
  76. new_tsmp = data.timestamp;
  77. $.makeArray(data.notes_id).forEach(function(ele, i){
  78. if(data.deleted[i] === '0'){
  79. getData(ele);
  80. } else {
  81. removeData(data.uuid[i], data.id[i]);
  82. }
  83. });
  84. break;
  85. case 'stay':
  86. //???
  87. new_tsmp = tsmp;
  88. populate();
  89. break;
  90. default:
  91. console.log('something is not right');
  92. }
  93. updateTimestamp(new_tsmp);
  94. });
  95. },function(e){
  96. console.log(e.message);
  97. });
  98. } else {
  99. //popup??
  100. alert('Die Daten können nicht mit dem Server synchronisiert werden, da keine Internetverbindung vorhanden ist.')
  101. populate();
  102. }
  103. }
  104.  
  105. function removeData(uuid, id){
  106. db.executeSql('DELETE FROM mood_table WHERE uuid = ? AND id = ?',[uuid, id],function(){
  107. console.log('removed: '+uuid+', '+id);
  108. },function(e){
  109. console.log(e.message);
  110. })
  111. }
  112.  
  113. function updateTimestamp(tsmp){
  114. db.executeSql('UPDATE update_table SET timestamp = ? WHERE uid = 1',[tsmp],function(){},function(e){
  115. console.log(e.message);
  116. });
  117. }
  118.  
  119. /**
  120. *Downloads data from the server and inserts it into the local database.
  121. *@param {integer} id - The id of the row that will be downloaded
  122. */
  123. function getData(id){
  124. $.ajax({
  125. url: 'http://mydomain.de/my.php',
  126. crossDomain: true,
  127. timeout: 30000,
  128. method: 'GET',
  129. dataType: 'json',
  130. data: {'command': 'get_update_data',
  131. 'id': id},
  132. xhrFields: {
  133. withCredentials: true
  134. },
  135. beforeSend: function(xhr){
  136. xhr.setRequestHeader('Authorization', make_base_auth(user, pass));
  137. },
  138. success: function(data,status,jqXHR){
  139. //insert updated data
  140. console.log(JSON.stringify(data));
  141. var activityArr = new Array(),
  142. smileyimg = getSmileyImg(data.smileyid, data.id),
  143. activityValues = data.activity.split(","),
  144. date = new Date(parseInt(data.created, 10)),
  145. dd = date.getDate(),
  146. mm = date.getMonth()+1, //January is 0!
  147. yyyy = date.getFullYear(),
  148. hh = date.getHours(),
  149. mins = date.getMinutes();
  150. if(dd<10) {
  151. dd='0'+dd;
  152. }
  153. if(mm<10) {
  154. mm='0'+mm;
  155. }
  156. if(mins<10) {
  157. mins='0'+mins;
  158. }
  159. var created = hh+':'+mins+' '+dd+'.'+mm+'.'+yyyy,
  160. activityValues.forEach(function(ele, i){
  161. if(ele == 1){
  162. activityArr.push(getActivity(i));
  163. }
  164. });
  165. //check uuid & id
  166. db.transaction(function(tx){
  167. tx.executeSql('SELECT * FROM mood_table WHERE uuid = ? AND id = ?',[data.uuid, data.id],function(tx, res){
  168. if(res.rows.length != 0){
  169. tx.executeSql('UPDATE mood_table SET smileyid = ?, textdata = ?, activity = ?, last_change = ?, deleted = ? WHERE uuid = ? AND id = ?',[data.smileyid, data.textdata, data.activity, data.last_change, data.deleted, data.uuid, data.id],function(){
  170. console.log('insert success');
  171. },function(e){
  172. console.log(e.message);
  173. });
  174. } else {
  175. tx.executeSql('INSERT INTO mood_table (uuid, id, smileyid, textdata, activity, created, last_change, deleted) VALUES (?,?,?,?,?,?,?,?)',[data.uuid, data.id, data.smileyid, data.textdata, data.activity, created, data.last_change, data.deleted],function(){
  176. console.log('insert succes');
  177. },function(e){
  178. console.log(e.message);
  179. });
  180. }
  181. },function(e){
  182. console.log(e.message);
  183. });
  184. //populate?
  185. $('#mytext').prepend('<div class="mytextarea" id="'+data.id+'"></div>');
  186. if(data.uuid == deviceID){
  187. $('#'+data.id).append('<a href="#popupDialog" data-rel="popup" data-position-to="window" class="ui-btn ui-shadow ui-corner-all ui-nodisc-icon ui-alt-icon ui-icon-bars ui-btn-icon-notext ui-btn-inline delete-button" onclick="openPopup('+data.id+');"></a>');
  188. } else {
  189. $('#'+data.id).append('</br>');
  190. }
  191. $('#'+data.id).append(smileyimg+'<p id="'+data.id+'-text" style="margin-top: 10px;">'+data.textdata+'</p><p><div id="taetigkeiten_'+data.id+'"></div>'+created+'</p>');
  192. $('#taetigkeiten_'+data.id).text('Tätigkeiten: ');
  193. $('#taetigkeiten_'+data.id).append(activityArr.join(', '));
  194. graphMalen(smileyArr);
  195. });
  196. },
  197. error: ajaxErrorCb
  198. });
  199. }
  200.  
  201. /**
  202. *Uploads data to the server.
  203. *@param {integer} id - ID of the div
  204. *@param {integer} smileyid - ID of the smiley
  205. *@param {string} text - Descriptiontext
  206. *@param {integer} activity - ID of the activity
  207. *@param {string} date - Date and time of the creatino
  208. */
  209. function insertData(id, smileyid, text, activity){
  210. var insertObj = {
  211. 'command': 'insert_data',
  212. 'uuid': deviceID,
  213. 'id': id,
  214. 'smileyid': smileyid,
  215. 'text': text,
  216. 'activity': activity.join(',')
  217. };
  218. $.ajax({
  219. url: 'http://mydomain.de/my.php',
  220. crossDomain: true,
  221. timeout: 30000,
  222. method: 'POST',
  223. dataType: 'text',
  224. data: insertObj,
  225. xhrFields: {
  226. withCredentials: true
  227. },
  228. beforeSend : function(xhr){
  229. xhr.setRequestHeader('Authorization', make_base_auth(user, pass));
  230. },
  231. success: function(data,status){
  232. console.log(JSON.stringify(data));
  233. console.log(status);
  234. },
  235. error: ajaxErrorCb
  236. });
  237. }
  238.  
  239. function ajaxErrorCb(obj, text, e){
  240. console.log(JSON.stringify(obj));
  241. console.log(text);
  242. }
  243.  
  244. {'case': string,
  245. 'notes_id': integer,
  246. 'uuid': string,
  247. 'id': integer,
  248. 'deleted': integer,
  249. 'timestamp': integer}
  250.  
  251. {'uuid': string,
  252. 'id': integer,
  253. 'smileyid': integer,
  254. 'textdata': string,
  255. 'activity': string,
  256. 'timedate': integer,
  257. 'created': integer,
  258. 'last_change': integer,
  259. 'deleted': integer}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement