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

View file

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

View file

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