You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
balance update works
This commit is contained in:
@@ -9,6 +9,8 @@ from wallet import LNBitsWallet
|
||||
main_screen = None
|
||||
settings_screen = None
|
||||
|
||||
balance_label = None
|
||||
|
||||
# Settings screen implementation
|
||||
class SettingsScreen():
|
||||
def __init__(self):
|
||||
@@ -185,7 +187,7 @@ def settings_button_tap(event):
|
||||
mpos.ui.load_screen(settings_screen)
|
||||
|
||||
def build_main_ui():
|
||||
global main_screen
|
||||
global main_screen, balance_label
|
||||
main_screen = lv.obj()
|
||||
main_screen.set_style_pad_all(10, 0)
|
||||
balance_label = lv.label(main_screen)
|
||||
@@ -209,14 +211,18 @@ def build_main_ui():
|
||||
mpos.ui.load_screen(main_screen)
|
||||
|
||||
|
||||
def redraw_balance_cb(timer):
|
||||
global balance_label
|
||||
if balance_label.get_text() != str(wallet.last_known_balance):
|
||||
balance_label.set_text(str(wallet.last_known_balance))
|
||||
|
||||
def janitor_cb(timer):
|
||||
if lv.screen_active() != main_screen and lv.screen_active() != settings_screen:
|
||||
print("app backgrounded, cleaning up...")
|
||||
janitor.delete()
|
||||
redraw_timer.delete()
|
||||
if settings_screen:
|
||||
settings_screen.delete()
|
||||
|
||||
janitor = lv.timer_create(janitor_cb, 1000, None)
|
||||
|
||||
build_main_ui()
|
||||
|
||||
@@ -236,4 +242,7 @@ elif wallet_type == "nwc":
|
||||
else:
|
||||
print(f"No or unsupported wallet type configured: '{wallet_type}'")
|
||||
|
||||
wallet.start_refresh_balance()
|
||||
redraw_timer = lv.timer_create(redraw_balance_cb, 1000, None)
|
||||
|
||||
janitor = lv.timer_create(janitor_cb, 1000, None)
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
import _thread
|
||||
import requests
|
||||
import json
|
||||
|
||||
import mpos.apps
|
||||
import mpos.time
|
||||
|
||||
class Wallet:
|
||||
|
||||
# These values could be loading from a cache.json file at __init__
|
||||
last_known_balance = 0
|
||||
last_known_balance_timestamp = 0
|
||||
#last_known_balance_timestamp = 0
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
@@ -22,24 +28,33 @@ class LNBitsWallet(Wallet):
|
||||
self.lnbits_url = lnbits_url
|
||||
self.lnbits_readkey = lnbits_readkey
|
||||
|
||||
def fetch_balance_thread():
|
||||
def fetch_balance_thread(self, lnbits_url, lnbits_readkey):
|
||||
print("fetch_balance_thread")
|
||||
walleturl = self.lnbits_url + "/api/v1/wallet"
|
||||
walleturl = lnbits_url + "/api/v1/wallet"
|
||||
headers = {
|
||||
"X-Api-Key": self.lnbits_readkey,
|
||||
"X-Api-Key": lnbits_readkey,
|
||||
}
|
||||
try:
|
||||
response = requests.get(self.lnbits_url, timeout=10, headers=headers)
|
||||
response = requests.get(walleturl, timeout=10, headers=headers)
|
||||
except Exception as e:
|
||||
print("GET request failed:", e)
|
||||
#lv.async_call(lambda l: please_wait_label.set_text(f"Error downloading app index: {e}"), None)
|
||||
if response and response.status_code == 200:
|
||||
print(f"Got response text: {response.text}")
|
||||
response_text = response.text
|
||||
print(f"Got response text: {response_text}")
|
||||
response.close()
|
||||
try:
|
||||
balance_reply = json.loads(response_text)
|
||||
print(f"Got balance: {balance_reply['balance']}")
|
||||
balance_msat = balance_reply['balance']
|
||||
self.last_known_balance = round(balance_msat / 1000)
|
||||
#self.last_known_balance_timestamp = mpos.time.epoch_seconds()
|
||||
except Exception as e:
|
||||
print(f"Could not parse reponse text '{response_text}' as JSON: {e}")
|
||||
|
||||
def start_refresh_balance(self):
|
||||
_thread.stack_size(mpos.apps.good_stack_size())
|
||||
_thread.start_new_thread(self.fetch_balance_thread, ())
|
||||
_thread.start_new_thread(self.fetch_balance_thread, (self.lnbits_url, self.lnbits_readkey))
|
||||
|
||||
class NWCWallet(Wallet):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user