link kivyapp to the logqueue and implement permanent consuming of it

This commit is contained in:
Daniel afx 2020-10-26 16:06:25 +02:00
parent 494535800d
commit e297854e45
3 changed files with 34 additions and 22 deletions

View file

@ -16,10 +16,23 @@ def get_random_string(length):
return result_str
class ForestBot(pydle.Client):
verbose = True
async def on_connect(self):
await super().on_connect()
await self.join(CNC_CHANNEL)
await self.message(CNC_CHANNEL, MOTD)
#await self.message(CNC_CHANNEL, MOTD)
await self.dumplog(CNC_CHANNEL)
async def dumplog(self, target):
sleep_time = 1
while True:
msg = self.queue.get()
if self.verbose:
await asyncio.sleep(sleep_time)
await self.message(target, msg)
else:
print(msg)
async def is_admin(self, nickname):
"""
@ -44,7 +57,8 @@ class ForestBot(pydle.Client):
await self.message(target, '] cnc usage:')
await self.message(target, '] help - displays help')
await self.message(target, '] id - cnc user info')
await self.message(target, '] dump - dump cnc log')
await self.message(target, '] log on - dump cnc log')
await self.message(target, '] log off - stop cnc log dump')
# Show user info
if message.startswith('id {}'.format(self.nickname)):
@ -54,21 +68,19 @@ class ForestBot(pydle.Client):
else:
await self.message(target, '] You are NOT an administrator. :('.format(source))
if message.startswith('dump {}'.format(self.nickname)):
if message.startswith('log on {}'.format(self.nickname)):
admin = await self.is_admin(source)
if admin:
await self.dumplog(target)
self.verbose = True
else:
await self.message(target, '] You are NOT an administrator. :('.format(source))
async def dumplog(self, target):
sleep_time = 1
while True:
msg = self.queue.get()
await asyncio.sleep(sleep_time)
await self.message(target, msg)
if len(msg) == 0:
break
if message.startswith('log off {}'.format(self.nickname)):
admin = await self.is_admin(source)
if admin:
self.verbose = False
else:
await self.message(target, '] You are NOT an administrator. :('.format(source))
def run(self, *args, **kwargs):
self.queue = kwargs['extqueue']

View file

@ -12,8 +12,7 @@ class ForestMapView(MapView):
image_ext="png", attribution="@ afx")
def print_current_zoom(self, *args):
print("] Zoom level: " + str(self.zoom))
#pqueue.put("] Zoom level: " + str(self.zoom))
App.get_running_app().logqueue.put("] Zoom level: " + str(self.zoom))
def start_get_fov_trees(self):
# After one second get the trees in field of view

View file

@ -69,7 +69,7 @@ class MainApp(MDApp):
def on_start(self):
# Welcome
print(MOTD)
queue.put('] Framework started.')
self.logqueue.put('] Framework started.')
self.theme_cls.primary_palette = 'BlueGray'
@ -85,12 +85,13 @@ class MainApp(MDApp):
if __name__ == '__main__':
# Start the Command and Control bot. Mainly for reporting.
queue = Queue()
cnc = Process(target=CnCApp, args=((queue),))
logqueue = Queue()
cnc = Process(target=CnCApp, args=((logqueue),))
cnc.start()
# Start the Kivy Framework main loop
kivyapp = MainApp()
kivyapp.logqueue = logqueue
kivyapp.run()
# Cleanup subprocesses