-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.04ccde355aa667d98846.js
More file actions
1 lines (1 loc) · 10.2 KB
/
scripts.04ccde355aa667d98846.js
File metadata and controls
1 lines (1 loc) · 10.2 KB
1
$.ajaxSetup({timeout:6e5,cache:!1});class Tmpo{constructor(t,e,s=!1){this.uid=t,this.token=e,this.debug=s,this.dbPromise=idb.open("flukso",1,t=>{switch(t.oldVersion){case 0:t.createObjectStore("device"),t.createObjectStore("sensor"),t.createObjectStore("tmpo")}}),this.sync_completed=!1,this.progress=this._progress_state_init("completed"),this.progress_cb=(()=>{})}sync(t=(()=>{})){this.sync_completed=!1,this.progress=this._progress_state_init("waiting"),this.progress_cb=t,this._device_sync()}sync_sensors(t,e=(()=>{})){this.sync_completed=!1,this.progress=this._progress_state_init("waiting"),this.progress_cb=e,this.progress.device.state="completed",this.dbPromise.then(async e=>{for(let s in t){this.progress.sensor.state="running",this.progress.sensor.todo++;const e=await this._last_block(t[s]);this._tmpo_sensor_sync(t[s],e)}"completed"==this.progress.device.state&&"running"!=this.progress.sensor.state&&(this.progress.all.state="completed"),this._progress_cb_handler()})}sync_devices(t,e=(()=>{})){this.sync_completed=!1,this.progress=this._progress_state_init("waiting"),this.progress_cb=e;for(let s in t)this.progress.device.state="running",this.progress.device.todo++,this._sensor_sync(t[s]);"waiting"==this.progress.device.state&&(this.progress.all.state="completed"),this._progress_cb_handler()}_device_sync(){$.getJSON(`https://www.flukso.net/api/user/${this.uid}/device`,{version:"1.0",token:this.token}).done(t=>{this._log("device",`sync call for uid ${this.uid} successful`),this.dbPromise.then(e=>{e.transaction("device","readwrite").objectStore("device").put(t,this.uid);for(let s in t)this.progress.device.state="running",this.progress.device.todo++,this._sensor_sync(t[s].device);"waiting"==this.progress.device.state&&(this.progress.all.state="completed"),this._progress_cb_handler()})})}_sensor_sync(t){$.getJSON(`https://www.flukso.net/api/device/${t}/sensor`,{version:"1.0",token:this.token}).done(e=>{this._log("sensor",`sync call for device ${t} successful`),0==--this.progress.device.todo&&(this.progress.device.state="completed"),this.dbPromise.then(async s=>{s.transaction("sensor","readwrite").objectStore("sensor").put(e,t);for(let t in e){this.progress.sensor.state="running",this.progress.sensor.todo++;const s=await this._last_block(e[t].sensor);this._tmpo_sensor_sync(e[t].sensor,s)}"completed"==this.progress.device.state&&"running"!=this.progress.sensor.state&&(this.progress.all.state="completed"),this._progress_cb_handler()})})}_tmpo_sensor_sync(t,e={rid:0,lvl:0,bid:0}){$.getJSON(`https://www.flukso.net/api/sensor/${t}/tmpo/sync`,{version:"1.0",token:this.token,rid:e.rid,lvl:e.lvl,bid:e.bid}).done(e=>{this._log("tmpo",`sync call for sensor ${t} successful`),0==--this.progress.sensor.todo&&(this.progress.sensor.state="completed");for(let s in e)this.progress.block.state="running",this.progress.block.todo++,this._tmpo_block_sync(t,e[s]);"completed"==this.progress.device.state&&"completed"==this.progress.sensor.state&&"running"!=this.progress.block.state&&(this.progress.all.state="completed"),this._progress_cb_handler()})}_tmpo_block_sync(t,e){const s=e.rid,o=e.lvl,r=e.bid;$.getJSON(`https://www.flukso.net/api/sensor/${t}/tmpo/${s}/${o}/${r}`,{version:"1.0",token:this.token}).done(n=>{const i=this._bid2key(t,s,o,r);this._log("tmpo",`sync call for block ${i} successful`),0==--this.progress.block.todo&&(this.progress.block.state="completed"),this.dbPromise.then(s=>{s.transaction("tmpo","readwrite").objectStore("tmpo").put(n,i),this._tmpo_clean(t,e)})})}_tmpo_clean(t,e){const s=IDBKeyRange.bound(t,`${t}~`),o=this;let r=[];this.dbPromise.then(t=>t.transaction("tmpo","readonly").objectStore("tmpo").openKeyCursor(s)).then(function t(s){if(8==e.lvl)return;if(!s)return;const n=o._key2bid(s.key);return n.rid==e.rid&&n.lvl<e.lvl&&n.bid<e.bid+Math.pow(2,e.lvl)&&r.push(s.key),s.continue().then(t)}).then(()=>{let e=r.toString();this._log("clean",`cleaning sensor ${t} blocks [${e}]`);for(let t in r)this.progress.clean.state="running",this.progress.clean.todo++,this._tmpo_delete_block(r[t]);"completed"==this.progress.device.state&&"completed"==this.progress.sensor.state&&"completed"==this.progress.block.state&&"running"!=this.progress.clean.state&&(this.progress.all.state="completed"),this._progress_cb_handler()})}_tmpo_delete_block(t){this.dbPromise.then(e=>{const s=e.transaction("tmpo","readwrite");return s.objectStore("tmpo").delete(t),s.complete}).then(()=>{this._log("clean",`block ${t} deleted`),0==--this.progress.clean.todo&&(this.progress.clean.state="completed",this.progress.all.state="completed"),this._progress_cb_handler()})}async series(t,{rid:e=null,head:s=0,tail:o=Number.POSITIVE_INFINITY,subsample:r=0}={}){null==e&&({rid:e}=await this._last_block(t));const n=await this._blocklist(t,e,s,o),i=JSON.stringify(n);this._log("series",`sensor ${t} using blocks: ${i}`);const c=await Promise.all(n.map(async e=>{const n=await this._block_content(t,e.rid,e.lvl,e.bid);return this._block2series(n,s,o,r)}));return this._serieslist_concat(c)}async _last_block(t){const e=IDBKeyRange.bound(t,`${t}~`),s=this;let o={rid:0,lvl:0,bid:0};const r=(await this.dbPromise).transaction("tmpo","readonly").objectStore("tmpo").openKeyCursor(e);return await r.then(function t(e){if(!e)return;const r=s._key2bid(e.key);return r.bid>o.bid&&(o=r),e.continue().then(t)}).then(()=>{const e=s._bid2key(t,o.rid,o.lvl,o.bid);return s._log("last",`last block: ${e}`),o})}async _blocklist(t,e,s,o){const r=this,n=IDBKeyRange.bound(`${t}-${e}`,`${t}-${e}~`),i=(await this.dbPromise).transaction("tmpo","readonly").objectStore("tmpo").openKeyCursor(n);let c=[];return await i.then(function t(e){if(!e)return;const n=r._key2bid(e.key);return s<r._blocktail(n.lvl,n.bid)&&o>n.bid&&c.push(n),e.continue().then(t)}).then(()=>c.sort((t,e)=>t.bid-e.bid))}_block2series(t,e,s,o=0){let[r,n]=t.h.head,i=0,c=[],a=[];for(let l in t.t)n+=t.v[l],(r+=t.t[l])>=e&&r<=s&&r>=i+o&&(c.push(r),a.push(n),i=r);return{t:c,v:a}}_serieslist_concat(t){let e={t:[],v:[]};for(let{t:s,v:o}of t)e.t=e.t.concat(s),e.v=e.v.concat(o);return e}async _block_content(t,e,s,o){const r=await this.dbPromise,n=this._bid2key(t,e,s,o);return r.transaction("tmpo").objectStore("tmpo").get(n)}_blocktail(t,e){return e+Math.pow(2,t)}_bid2key(t,e,s,o){return`${t}-${e}-${s}-${o}`}_key2bid(t){const e=t.split("-");return{rid:Number(e[1]),lvl:Number(e[2]),bid:Number(e[3])}}_progress_state_init(t="waiting"){return{device:{state:t,todo:0},sensor:{state:t,todo:0},block:{state:t,todo:0},clean:{state:t,todo:0},all:{state:t,start:Date.now(),runtime:0}}}_progress_cb_handler(){this.sync_completed||(this.progress.all.runtime=Date.now()-this.progress.all.start,this.progress_cb(this.progress)),"completed"==this.progress.all.state&&(this.sync_completed=!0)}_log(t,e){if(this.debug){let s=Date.now();console.log(`${s} [${t}] ${e}`)}}}$(function(){function t(t){return new Promise(function(e,s){t.onsuccess=function(){e(t.result)},t.onerror=function(){s(t.error)}})}function e(e,s,o){var r,n=new Promise(function(n,i){t(r=e[s].apply(e,o)).then(n,i)});return n.request=r,n}function s(t,e,s){s.forEach(function(s){Object.defineProperty(t.prototype,s,{get:function(){return this[e][s]},set:function(t){this[e][s]=t}})})}function o(t,s,o,r){r.forEach(function(r){r in o.prototype&&(t.prototype[r]=function(){return e(this[s],r,arguments)})})}function r(t,e,s,o){o.forEach(function(o){o in s.prototype&&(t.prototype[o]=function(){return this[e][o].apply(this[e],arguments)})})}function n(t,s,o,r){r.forEach(function(r){r in o.prototype&&(t.prototype[r]=function(){return(t=e(this[s],r,arguments)).then(function(e){if(e)return new c(e,t.request)});var t})})}function i(t){this._index=t}function c(t,e){this._cursor=t,this._request=e}function a(t){this._store=t}function l(t){this._tx=t,this.complete=new Promise(function(e,s){t.oncomplete=function(){e()},t.onerror=function(){s(t.error)},t.onabort=function(){s(t.error)}})}function p(t,e,s){this._db=t,this.oldVersion=e,this.transaction=new l(s)}function h(t){this._db=t}s(i,"_index",["name","keyPath","multiEntry","unique"]),o(i,"_index",IDBIndex,["get","getKey","getAll","getAllKeys","count"]),n(i,"_index",IDBIndex,["openCursor","openKeyCursor"]),s(c,"_cursor",["direction","key","primaryKey","value"]),o(c,"_cursor",IDBCursor,["update","delete"]),["advance","continue","continuePrimaryKey"].forEach(function(e){e in IDBCursor.prototype&&(c.prototype[e]=function(){var s=this,o=arguments;return Promise.resolve().then(function(){return s._cursor[e].apply(s._cursor,o),t(s._request).then(function(t){if(t)return new c(t,s._request)})})})}),a.prototype.createIndex=function(){return new i(this._store.createIndex.apply(this._store,arguments))},a.prototype.index=function(){return new i(this._store.index.apply(this._store,arguments))},s(a,"_store",["name","keyPath","indexNames","autoIncrement"]),o(a,"_store",IDBObjectStore,["put","add","delete","clear","get","getAll","getKey","getAllKeys","count"]),n(a,"_store",IDBObjectStore,["openCursor","openKeyCursor"]),r(a,"_store",IDBObjectStore,["deleteIndex"]),l.prototype.objectStore=function(){return new a(this._tx.objectStore.apply(this._tx,arguments))},s(l,"_tx",["objectStoreNames","mode"]),r(l,"_tx",IDBTransaction,["abort"]),p.prototype.createObjectStore=function(){return new a(this._db.createObjectStore.apply(this._db,arguments))},s(p,"_db",["name","version","objectStoreNames"]),r(p,"_db",IDBDatabase,["deleteObjectStore","close"]),h.prototype.transaction=function(){return new l(this._db.transaction.apply(this._db,arguments))},s(h,"_db",["name","version","objectStoreNames"]),r(h,"_db",IDBDatabase,["close"]),["openCursor","openKeyCursor"].forEach(function(t){[a,i].forEach(function(e){t in e.prototype&&(e.prototype[t.replace("open","iterate")]=function(){var e=Array.prototype.slice.call(arguments),s=e[e.length-1],o=this._store||this._index,r=o[t].apply(o,e.slice(0,-1));r.onsuccess=function(){s(r.result)}})})}),[i,a].forEach(function(t){t.prototype.getAll||(t.prototype.getAll=function(t,e){var s=this,o=[];return new Promise(function(r){s.iterateCursor(t,function(t){t?(o.push(t.value),void 0===e||o.length!=e?t.continue():r(o)):r(o)})})})}),window.idb={open:function(t,s,o){var r=e(indexedDB,"open",[t,s]),n=r.request;return n&&(n.onupgradeneeded=function(t){o&&o(new p(n.result,t.oldVersion,n.transaction))}),r.then(function(t){return new h(t)})},delete:function(t){return e(indexedDB,"deleteDatabase",[t])}}});