From efe9b24c5e23fdeadf54637465157e5867173fc6 2017-12-14 01:32:26 From: jespinoza Date: 2017-12-14 01:32:26 Subject: [PATCH] sound, email & popup alarm --- diff --git a/schainpy/admin.py b/schainpy/admin.py index 30212d9..68c99bb 100644 --- a/schainpy/admin.py +++ b/schainpy/admin.py @@ -18,6 +18,7 @@ from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from schainpy.utils import log +from schainpy.model.graphics.jroplot_data import popup def get_path(): ''' @@ -33,27 +34,69 @@ def get_path(): except: log.error('I am sorry, but something is wrong... __file__ not found') -def alarm(level=1, cycle=2): +def alarm(modes=[1], **kwargs): ''' + modes: + 0 - All + 1 - Sound alarm + 2 - Send email + 3 - Popup message + 4 - Send to alarm system TODO ''' - def target(sound, level, cycle): - for __ in range(cycle): - os.system('paplay {}'.format(sound)) - time.sleep(0.5) - - sound = os.path.join(get_path(), 'alarm{}.oga'.format(level)) + def play_sound(): + sound = os.path.join(get_path(), 'alarm1.oga') + if os.path.exists(sound): + for __ in range(2): + os.system('paplay {}'.format(sound)) + time.sleep(0.5) + else: + log.warning('Unable to play alarm, sound file not found', 'ADMIN') - if os.path.exists(sound): - t = Thread(target=target, args=(sound, level, cycle)) - t.start() - else: - log.warning('Unable to play alarm', 'ADMIN') + def send_email(**kwargs): + notifier = SchainNotify() + notifier.notify(**kwargs) + + def show_popup(message='Error'): + popup(message) + + def send_alarm(): + pass + + def get_kwargs(kwargs, keys): + ret = {} + for key in keys: + ret[key] = kwargs[key] + return ret + + tasks = { + 1 : send_email, + 2 : play_sound, + 3 : show_popup, + 4 : send_alarm, + } + + tasks_args = { + 1: ['email', 'message', 'subject', 'subtitle', 'filename'], + 2: [], + 3: ['message'], + 4: [], + } + + for mode in modes: + if mode == 0: + for x in tasks: + t = Thread(target=tasks[x], kwargs=get_kwargs(kwargs, tasks_args[x])) + t.start() + break + else: + t = Thread(target=tasks[mode], kwargs=get_kwargs(kwargs, tasks_args[x])) + t.start() class SchainConfigure(): - __DEFAULT_ADMINISTRATOR_EMAIL = "" + __DEFAULT_ADMINISTRATOR_EMAIL = "juan.espinoza@jro.igp.gob.pe" __DEFAULT_EMAIL_SERVER = "jro-zimbra.igp.gob.pe" __DEFAULT_SENDER_EMAIL = "notifier-schain@jro.igp.gob.pe" __DEFAULT_SENDER_PASS = "" @@ -316,7 +359,10 @@ class SchainNotify: Exceptions: None. """ - print "Notifying to %s ..." %email + if email is None: + email = self.__emailToAddress + + log.success('Notifying to %s ...'.format(email), 'ADMIN') self.sendEmail(email_from=self.__emailFromAddress, email_to=email, @@ -325,7 +371,7 @@ class SchainNotify: subtitle=subtitle, filename=filename) - print "***** Your system administrator has been notified *****" + log.success('Email sent', 'ADMIN') class SchainError(Exception): """SchainError is an exception class that is thrown for all known errors using Schain Py lib. diff --git a/schainpy/controller.py b/schainpy/controller.py index 571f3be..e78f8e0 100644 --- a/schainpy/controller.py +++ b/schainpy/controller.py @@ -908,7 +908,7 @@ class Project(Process): self.id = None self.description = None self.email = None - self.alarm = False + self.alarm = [0] self.plotterQueue = plotter_queue self.procUnitConfObjDict = {} @@ -956,7 +956,7 @@ class Project(Process): self.procUnitConfObjDict = newProcUnitConfObjDict - def setup(self, id, name='', description='', email=None, alarm=False): + def setup(self, id, name='', description='', email=None, alarm=[0]): print print '*' * 60 @@ -1187,21 +1187,13 @@ class Project(Process): err = traceback.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]) - - print '***** Error occurred in %s *****' % (procUnitConfObj.name) - print '***** %s' % err[-1] + + log.error('{}'.format(err[-1]), procUnitConfObj.name) message = ''.join(err) sys.stderr.write(message) - if self.email is None: - log.warning('Email attribute has not been set', self.name) - return - - if self.alarm: - schainpy.admin.alarm(1) - subject = 'SChain v%s: Error running %s\n' % ( schainpy.__version__, procUnitConfObj.name) @@ -1223,8 +1215,8 @@ class Project(Process): subtitle += '[Start time = %s]\n' % readUnitConfObj.startTime subtitle += '[End time = %s]\n' % readUnitConfObj.endTime - adminObj = schainpy.admin.SchainNotify() - adminObj.notify( + schainpy.admin.alarm( + modes=self.alarm, email=self.email, message=message, subject=subject, @@ -1300,6 +1292,7 @@ class Project(Process): try: sts = procUnitConfObj.run() is_ok = is_ok or sts + j except KeyboardInterrupt: is_ok = False break diff --git a/schainpy/model/graphics/jroplot_data.py b/schainpy/model/graphics/jroplot_data.py index 261792b..0fded4d 100644 --- a/schainpy/model/graphics/jroplot_data.py +++ b/schainpy/model/graphics/jroplot_data.py @@ -36,6 +36,13 @@ def figpause(interval): canvas.start_event_loop(interval) return +def popup(message): + fig = plt.figure(figsize=(12, 9), facecolor='r') + fig.text(0.5, 0.5, message, ha='center', va='center', size='20', weight='heavy', color='w') + fig.show() + figpause(1000) + + class PlotData(Operation, Process): '''