add aria2 websocket in run.py

improve the way to open a warehouse
slow down the reflash freq of download progress in download_process_page.py
This commit is contained in:
cuu 2019-12-17 13:40:16 +00:00
parent 42fd86bce6
commit 31f117d65f
3 changed files with 191 additions and 31 deletions

View File

@ -57,6 +57,131 @@ class RPCStack:
def Length(self):
return len(self.stack)
class LoadHousePage(Page):
_FootMsg = ["Nav.","","","Back","Cancel"]
_DownloaderTimer = -1
_Value = 0
_URL = None
_ListFontObj = MyLangManager.TrFont("varela18")
_URLColor = MySkinManager.GiveColor('URL')
_TextColor = MySkinManager.GiveColor('Text')
_Caller=None
_img = None
_Downloader=None
_DownloaderTimer=-1
def __init__(self):
Page.__init__(self)
self._Icons = {}
self._CanvasHWND = None
def Init(self):
self._PosX = self._Index * self._Screen._Width
self._Width = self._Screen._Width
self._Height = self._Screen._Height
self._CanvasHWND = self._Screen._CanvasHWND
self._LoadingLabel = Label()
self._LoadingLabel.SetCanvasHWND(self._CanvasHWND)
self._LoadingLabel.Init("Loading",self._ListFontObj)
self._LoadingLabel.SetColor(self._TextColor )
def OnLoadCb(self):
if self._URL is None:
return
self._img = None
self.ClearCanvas()
self._Screen.Draw()
self._Screen.SwapAndShow()
filename = self._URL.split("/")[-1].strip()
local_dir = self._URL.split("raw.githubusercontent.com")
if len(local_dir) >1:
menu_file = local_dir[1]
local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'),menu_file )
if FileExists(local_menu_file):
#load json
with open(local_menu_file) as json_file:
try:
local_menu_json = json.load(json_file)
self._Caller._MyStack.Push(local_menu_json["list"])
except:
pass
self.Leave()
else:
self._Downloader = Download(self._URL,"/tmp",None)
self._Downloader.start()
self._DownloaderTimer = gobject.timeout_add(400, self.GObjectUpdateProcessInterval)
def GObjectUpdateProcessInterval(self):
ret = True
if self._Screen.CurPage() == self:
if self._Downloader._stop == True:
ret = False
dst_filename = self._Downloader.get_dest()
if self._Downloader.isFinished():
if self._Downloader.isSuccessful():
filename = self._URL.split("/")[-1].strip()
local_dir = self._URL.split("raw.githubusercontent.com")
menu_file = local_dir[1]
local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'),menu_file )
dl_file = os.path.join("/tmp",filename)
if not os.path.exists(os.path.dirname(local_menu_file)):
os.makedirs(os.path.dirname(local_menu_file))
copyfile(dl_file, local_menu_file)
with open(local_menu_file) as json_file:
try:
local_menu_json = json.load(json_file)
self._Caller._MyStack.Push(local_menu_json["list"])
except:
pass
ret = False
self.Leave()
return ret
else:
return False
def Leave(self):
if self._DownloaderTimer != -1:
gobject.source_remove(self._DownloaderTimer)
self._DownloaderTimer = -1
if self._Downloader != None:
try:
self._Downloader.stop()
except:
print("user canceled ")
self.ReturnToUpLevelPage()
self._Screen.Draw()
self._Screen.SwapAndShow()
self._URL = None
def KeyDown(self,event):
if IsKeyMenuOrB(event.key):
self.Leave()
def Draw(self):
self.ClearCanvas()
self._LoadingLabel.NewCoord( (Width-self._LoadingLabel._Width)/2,(Height-44)/2)
self._LoadingLabel.Draw()
if self._img is not None:
self._CanvasHWND.blit(self._img,midRect(Width/2,
(Height-44)/2,
pygame.Surface.get_width(self._img),pygame.Surface.get_height(self._img),Width,Height-44))
class ImageDownloadProcessPage(Page):
_FootMsg = ["Nav.","","","Back","Cancel"]
_DownloaderTimer = -1
@ -67,7 +192,7 @@ class ImageDownloadProcessPage(Page):
_TextColor = MySkinManager.GiveColor('Text')
_img = None
_Downloader=None
_DownloaderTimer=-1
def __init__(self):
Page.__init__(self)
self._Icons = {}
@ -563,6 +688,12 @@ class GameStorePage(Page):
self._PreviewPage._Name = "preview"
self._PreviewPage.Init()
self._LoadHousePage = LoadHousePage()
self._LoadHousePage._Screen = self._Screen
self._LoadHousePage._Name = "Warehouse"
self._LoadHousePage._Caller = self
self._LoadHousePage.Init()
def ResetHouse(self):
if self._PsIndex > len(self._MyList) -1:
@ -585,6 +716,18 @@ class GameStorePage(Page):
self._Screen._MsgBox.Draw()
self._Screen.SwapAndShow()
def LoadHouse(self):
if self._PsIndex > len(self._MyList) -1:
return
cur_li = self._MyList[self._PsIndex]
if cur_li._Value["type"] == "source" or cur_li._Value["type"] == "dir":
self._LoadHousePage._URL = cur_li._Value["file"]
self._Screen.PushPage(self._LoadHousePage)
self._Screen.Draw()
self._Screen.SwapAndShow()
def PreviewGame(self):
if self._PsIndex > len(self._MyList) -1:
return
@ -660,33 +803,23 @@ class GameStorePage(Page):
local_menu_file = "%s/aria2download%s" % (os.path.expanduser('~'),menu_file )
print(local_menu_file)
if FileExists( local_menu_file ) == False:
print(local_menu_file, "non-existed")
gid,ret = config.RPC.urlDownloading(remote_file_url)
if ret == False:
gid = config.RPC.addUri( remote_file_url, options={"out": menu_file})
self._Downloading = remote_file_url
self._Screen._MsgBox.SetText("Loading")
self._Screen._MsgBox.Draw()
self._Screen.SwapAndShow()
pygame.time.delay(800)
else:
print(gid," url is downloading")
self._Screen._MsgBox.SetText("Loading")
self._Screen._MsgBox.Draw()
self._Screen.SwapAndShow()
pygame.time.delay(400)
self.LoadHouse()
else:
#read the local_menu_file, push into stack,display menu
self._Downloading = None
with open(local_menu_file) as json_file:
local_menu_json = json.load(json_file)
print(local_menu_json)
self._MyStack.Push(local_menu_json["list"])
try:
with open(local_menu_file) as json_file:
local_menu_json = json.load(json_file)
print(local_menu_json)
self._MyStack.Push(local_menu_json["list"])
self.SyncList()
self._Screen.Draw()
self.SyncList()
self._Screen.Draw()
self._Screen.SwapAndShow()
except Exception as ex:
print(ex)
self._Screen._MsgBox.SetText("Open house failed ")
self._Screen._MsgBox.Draw()
self._Screen.SwapAndShow()
elif cur_li._Value["type"] == "add_house":
@ -704,7 +837,7 @@ class GameStorePage(Page):
gid,ret = config.RPC.urlDownloading(remote_file_url)
if ret == False:
gid = config.RPC.addUri( remote_file_url, options={"out": menu_file})
self._Downloading = remote_file_url
self._Downloading = gid
print("stack length ",self._MyStack.Length())
if self._MyStack.Length() > 1:## not on the top list page
try:
@ -748,6 +881,9 @@ class GameStorePage(Page):
game_sh = "/home/cpi/apps/Menu/51_TIC-80/TIC-80.sh"
self._Screen.RunEXE(game_sh)
def OnAria2CompleteCb(self,gid):
print("OnAria2CompleteCb ", gid)
def raw_github_com(self,_url):#eg: github.com/clockworkpi/warehouse
if _url.startswith("github.com")== False:
return False

View File

@ -187,7 +187,7 @@ class DownloadProcessPage(Page):
self._Downloader = Download(url,dst_dir,None)
self._Downloader.start()
self._DownloaderTimer = gobject.timeout_add(100, self.GObjectUpdateProcessInterval)
self._DownloaderTimer = gobject.timeout_add(200, self.GObjectUpdateProcessInterval)
def KeyDown(self,event):
if IsKeyMenuOrB(event.key):

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
import os
import dbus
import dbus.service
import sys
@ -8,6 +8,7 @@ import logging
import errno
from wicd import misc
import libs.websocket as websocket
##misc.to_bool
##misc.misc.noneToString
##misc.to_unicode
@ -22,7 +23,6 @@ import gobject
import socket
import pygame
from sys import exit
import os
#from beeprint import pp
########
@ -478,6 +478,29 @@ def gobject_pygame_event_timer(main_screen):
return True
@misc.threaded
def aria2_ws(main_screen):
def on_message(ws, message):
print(message)
def on_error(ws, error):
print(error)
def on_close(ws):
print("### closed ###")
websocket.enableTrace(True)
try:
ws = websocket.WebSocketApp("ws://localhost:6800/jsonrpc",
on_message = on_message,
on_error = on_error,
on_close = on_close)
# ws.on_open = on_open
ws.run_forever()
except:
return
@misc.threaded
def socket_thread(main_screen):
@ -588,6 +611,7 @@ def big_loop():
socket_thread(main_screen)
aria2_ws(main_screen)
gobject_loop()