|
| 1 | +// const logFile = require("Storage").open("coin_info_log.txt", "a"); |
| 2 | +const db = require("Storage").readJSON("coin_info.cmc_key.json", 1) || {}; |
| 3 | +const csTokens = db.csTokens.split(','); |
| 4 | +// |
| 5 | +const ciLib = require("coin_info"); |
| 6 | +// |
| 7 | +var ticker = 0; |
| 8 | +var currLoadMsg = "..."; |
| 9 | +var timePeriod = "24h"; |
| 10 | +var tknChrtData = [5,6,5,6,5,6,5,6,5,6,5,6,5,6,]; |
| 11 | +var optSpacing = {}; |
| 12 | +var isPaused = false; |
| 13 | + |
| 14 | + |
| 15 | +// |
| 16 | +Bangle.loadWidgets(); // loading widgets after drawing the layout in `drawMain()` to display the app UI ASAP. |
| 17 | +require("widget_utils").swipeOn(); // hide widgets, make them visible with a swipe |
| 18 | +// Bangle.setUI({ |
| 19 | +// mode: 'custom', |
| 20 | +// back: Bangle.showClock |
| 21 | +// // btn: function() { // Handle button press |
| 22 | +// // console.log("Button pressed"); |
| 23 | +// // } |
| 24 | +// }); |
| 25 | +// |
| 26 | +function swipeHandler(lr, ud) { |
| 27 | + if (lr == 1) { |
| 28 | + ticker = ticker - 1; |
| 29 | + if (ticker < 0) ticker = 0; |
| 30 | + } |
| 31 | + if (lr == -1) { |
| 32 | + ticker = ticker + 1; |
| 33 | + if (ticker > csTokens.length - 1) ticker = csTokens.length - 1; |
| 34 | + } |
| 35 | +} |
| 36 | +Bangle.on("swipe", swipeHandler); |
| 37 | + |
| 38 | + |
| 39 | +// |
| 40 | +function renderGraph(l) { |
| 41 | + const bounds = ciLib.findMinMax(tknChrtData); |
| 42 | + // logFile.write("?. graphy: " + JSON.stringify(bounds) + "\n"); |
| 43 | + require("graph").drawLine(g, tknChrtData, { |
| 44 | + axes: true, |
| 45 | + x: l.x, y: l.y, width: l.w, height: l.h, |
| 46 | + miny: bounds.min, |
| 47 | + maxy: bounds.max, |
| 48 | + // gridy: 5 |
| 49 | + }); |
| 50 | +} |
| 51 | + |
| 52 | +var Layout = require("Layout"); |
| 53 | +var layout = new Layout({ |
| 54 | + type:"v", c: [ |
| 55 | + {type:"h", valign:-1, |
| 56 | + c: [ |
| 57 | + {type:"txt", id:"tknName", font:"6x8:2", label:"", halign:-1, fillx:1}, |
| 58 | + {type:"btn", label:"..", halign:1, cb: d=>showDetails()}, |
| 59 | + {type:"btn", label:"LH", halign:1, cb: d=>showLowHigh()} |
| 60 | + ] |
| 61 | + }, |
| 62 | + {type:"txt", id:"loadMsg", font:"6x8", label:"", fillx:1 }, |
| 63 | + {type:"custom", render:renderGraph, id:"tknGraph", bgCol:g.theme.bg, fillx:1, filly:1 }, |
| 64 | + {type:"h", valign:1, |
| 65 | + c: [ |
| 66 | + {type:"btn", label:"24h", cb: d=>getChart("24h")}, |
| 67 | + {type:"btn", label:"1w", cb: d=>getChart("1w")}, |
| 68 | + {type:"btn", label:"1m", cb: d=>getChart("1m")}, |
| 69 | + {type:"btn", label:"3m", cb: d=>getChart("3m")} |
| 70 | + ] |
| 71 | + } |
| 72 | + ] |
| 73 | + }, |
| 74 | + { lazy:true }); |
| 75 | +layout.update(); |
| 76 | + |
| 77 | + |
| 78 | +// |
| 79 | +var updateTimeout; |
| 80 | +function getChart(period) { |
| 81 | + if (isPaused) { |
| 82 | + if (updateTimeout) clearTimeout(updateTimeout); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // |
| 87 | + timePeriod = period; |
| 88 | + currLoadMsg = `Load... ${period}`; |
| 89 | + // |
| 90 | + // const date = new Date(); |
| 91 | + // logFile.write("Called:" + date.toISOString() + " -- " + timePeriod + " -- " + csTokens[ticker] + "\n"); |
| 92 | + |
| 93 | + const url = `https://openapiv1.coinstats.app/coins/${csTokens[ticker]}/charts?period=${timePeriod}`; |
| 94 | + Bangle |
| 95 | + .http(url, { |
| 96 | + method: 'GET', |
| 97 | + headers: { |
| 98 | + 'X-API-KEY': db.csApiKey |
| 99 | + } |
| 100 | + }) |
| 101 | + .then(data => { |
| 102 | + // logFile.write("HTTP resp:" + JSON.stringify(data)); |
| 103 | + const apiData = JSON.parse(data.resp); |
| 104 | + tknChrtData = apiData.map(innerArray => innerArray[1]); |
| 105 | + // logFile.write("Chart data:" + JSON.stringify(tknChrtData)); |
| 106 | + |
| 107 | + // just not readable |
| 108 | + optSpacing = ciLib.calculateOptimalYAxisSpacing(tknChrtData); |
| 109 | + // |
| 110 | + g.clearRect(layout.tknGraph.x, layout.tknGraph.y, layout.tknGraph.w, layout.tknGraph.h); |
| 111 | + layout.forgetLazyState(); // Force a full re-render |
| 112 | + layout.render(layout.tknGraph); // Render just the graph area |
| 113 | + |
| 114 | + // |
| 115 | + currLoadMsg = ""; |
| 116 | + layout.render(layout.loadMsg); |
| 117 | + }) |
| 118 | + .catch(err => { |
| 119 | + // logFile.write("API Error: " + JSON.stringify(err)); |
| 120 | + tknChrtData = [1,2,3,4,5,6,7,8,9,8,7,6,5,4,]; |
| 121 | + }); |
| 122 | + |
| 123 | + if (updateTimeout) clearTimeout(updateTimeout); |
| 124 | + updateTimeout = setTimeout(function() { |
| 125 | + updateTimeout = undefined; |
| 126 | + getChart(period); |
| 127 | + }, 60000 - (Date.now() % 60000)); |
| 128 | +} |
| 129 | + |
| 130 | +// |
| 131 | +function showLowHigh() { |
| 132 | + const title = `L/H ${csTokens[ticker]}`; |
| 133 | + // |
| 134 | + // logFile.write("OptSpacing:" + JSON.stringify(optSpacing) + "\n"); |
| 135 | + const first = ciLib.formatPriceString(optSpacing.first); |
| 136 | + const last = ciLib.formatPriceString(optSpacing.last); |
| 137 | + const low = ciLib.formatPriceString(optSpacing.rawMin); |
| 138 | + const high = ciLib.formatPriceString(optSpacing.rawMax); |
| 139 | + const msg = ` |
| 140 | + First: ${first} |
| 141 | + Last: ${last} |
| 142 | + Low: ${low} |
| 143 | + High: ${high} |
| 144 | + `; |
| 145 | + isPaused = true; |
| 146 | + E.showAlert(msg, title).then(function() { |
| 147 | + isPaused = false; |
| 148 | + g.clear(); |
| 149 | + layout.forgetLazyState(); |
| 150 | + layout.render(); |
| 151 | + layout.setUI(); |
| 152 | + }); |
| 153 | +} |
| 154 | +function showDetails() { |
| 155 | + const token = csTokens[ticker]; |
| 156 | + const url = `https://openapiv1.coinstats.app/coins/${token}`; |
| 157 | + Bangle.http(url, { |
| 158 | + method: 'GET', |
| 159 | + headers: { |
| 160 | + 'X-API-KEY': db.csApiKey |
| 161 | + } |
| 162 | + }) |
| 163 | + .then(data => { |
| 164 | + const tokenInfo = JSON.parse(data.resp); |
| 165 | + const priceFmt = ciLib.formatPriceString(tokenInfo.price); |
| 166 | + const mCapFmt = ciLib.formatPriceString(tokenInfo.marketCap); |
| 167 | + const title = `Details ${tokenInfo.symbol}`; |
| 168 | + const msg = ` |
| 169 | + Price: ${priceFmt} |
| 170 | + M-Cap: ${mCapFmt} |
| 171 | + 1h:${tokenInfo.priceChange1h} |
| 172 | + 1d:${tokenInfo.priceChange1d} 1w:${tokenInfo.priceChange1w} |
| 173 | + `; |
| 174 | + isPaused = true; |
| 175 | + E.showAlert(msg, title).then(function() { |
| 176 | + isPaused = false; |
| 177 | + g.clear(); |
| 178 | + layout.forgetLazyState(); |
| 179 | + layout.render(); |
| 180 | + layout.setUI(); |
| 181 | + }); |
| 182 | + }) |
| 183 | + .catch(err => { |
| 184 | + const msg = `Failed to fetch details for ${token.toUpperCase()}`; |
| 185 | + E.showAlert(msg, "Error").then(function() { |
| 186 | + // print("Ok pressed"); |
| 187 | + g.clear(); |
| 188 | + layout.forgetLazyState(); |
| 189 | + layout.render(); |
| 190 | + layout.setUI(); |
| 191 | + }); |
| 192 | + }); |
| 193 | +} |
| 194 | + |
| 195 | + |
| 196 | +// timeout used to update every minute |
| 197 | +var drawTimeout; |
| 198 | +// update the screen |
| 199 | +function draw() { |
| 200 | + // |
| 201 | + layout.tknName.label = (csTokens[ticker]).toUpperCase(); |
| 202 | + layout.loadMsg.label = currLoadMsg; |
| 203 | + // |
| 204 | + layout.render(layout.graph); |
| 205 | + // |
| 206 | + layout.render(); |
| 207 | + |
| 208 | + // schedule a draw for the next minute |
| 209 | + if (drawTimeout) clearTimeout(drawTimeout); |
| 210 | + drawTimeout = setTimeout(function() { |
| 211 | + drawTimeout = undefined; |
| 212 | + draw(); |
| 213 | + }, 1000 - (Date.now() % 1000)); |
| 214 | +} |
| 215 | + |
| 216 | +// update time and draw |
| 217 | +g.clear(); |
| 218 | +draw(); |
| 219 | +getChart("24h"); |
0 commit comments