1+ import _thread
2+ import requests
3+ import json
4+
5+ import mpos .apps
6+ import mpos .time
17
28class Wallet :
39
410 # These values could be loading from a cache.json file at __init__
511 last_known_balance = 0
6- last_known_balance_timestamp = 0
12+ # last_known_balance_timestamp = 0
713
814 def __init__ (self ):
915 pass
@@ -22,24 +28,33 @@ def __init__(self, lnbits_url, lnbits_readkey):
2228 self .lnbits_url = lnbits_url
2329 self .lnbits_readkey = lnbits_readkey
2430
25- def fetch_balance_thread ():
31+ def fetch_balance_thread (self , lnbits_url , lnbits_readkey ):
2632 print ("fetch_balance_thread" )
27- walleturl = self . lnbits_url + "/api/v1/wallet"
33+ walleturl = lnbits_url + "/api/v1/wallet"
2834 headers = {
29- "X-Api-Key" : self . lnbits_readkey ,
35+ "X-Api-Key" : lnbits_readkey ,
3036 }
3137 try :
32- response = requests .get (self . lnbits_url , timeout = 10 , headers = headers )
38+ response = requests .get (walleturl , timeout = 10 , headers = headers )
3339 except Exception as e :
3440 print ("GET request failed:" , e )
3541 #lv.async_call(lambda l: please_wait_label.set_text(f"Error downloading app index: {e}"), None)
3642 if response and response .status_code == 200 :
37- print (f"Got response text: { response .text } " )
43+ response_text = response .text
44+ print (f"Got response text: { response_text } " )
3845 response .close ()
46+ try :
47+ balance_reply = json .loads (response_text )
48+ print (f"Got balance: { balance_reply ['balance' ]} " )
49+ balance_msat = balance_reply ['balance' ]
50+ self .last_known_balance = round (balance_msat / 1000 )
51+ #self.last_known_balance_timestamp = mpos.time.epoch_seconds()
52+ except Exception as e :
53+ print (f"Could not parse reponse text '{ response_text } ' as JSON: { e } " )
3954
4055 def start_refresh_balance (self ):
4156 _thread .stack_size (mpos .apps .good_stack_size ())
42- _thread .start_new_thread (self .fetch_balance_thread , ())
57+ _thread .start_new_thread (self .fetch_balance_thread , (self . lnbits_url , self . lnbits_readkey ))
4358
4459class NWCWallet (Wallet ):
4560
0 commit comments