@@ -18,6 +18,7 from email.mime.application import MIMEApplication | |||||
18 | from email.mime.multipart import MIMEMultipart |
|
18 | from email.mime.multipart import MIMEMultipart | |
19 |
|
19 | |||
20 | from schainpy.utils import log |
|
20 | from schainpy.utils import log | |
|
21 | from schainpy.model.graphics.jroplot_data import popup | |||
21 |
|
22 | |||
22 | def get_path(): |
|
23 | def get_path(): | |
23 | ''' |
|
24 | ''' | |
@@ -33,27 +34,69 def get_path(): | |||||
33 | except: |
|
34 | except: | |
34 | log.error('I am sorry, but something is wrong... __file__ not found') |
|
35 | log.error('I am sorry, but something is wrong... __file__ not found') | |
35 |
|
36 | |||
36 | def alarm(level=1, cycle=2): |
|
37 | def alarm(modes=[1], **kwargs): | |
37 | ''' |
|
38 | ''' | |
|
39 | modes: | |||
|
40 | 0 - All | |||
|
41 | 1 - Sound alarm | |||
|
42 | 2 - Send email | |||
|
43 | 3 - Popup message | |||
|
44 | 4 - Send to alarm system TODO | |||
38 | ''' |
|
45 | ''' | |
39 |
|
46 | |||
40 | def target(sound, level, cycle): |
|
47 | def play_sound(): | |
41 | for __ in range(cycle): |
|
48 | sound = os.path.join(get_path(), 'alarm1.oga') | |
42 | os.system('paplay {}'.format(sound)) |
|
49 | if os.path.exists(sound): | |
43 | time.sleep(0.5) |
|
50 | for __ in range(2): | |
44 |
|
51 | os.system('paplay {}'.format(sound)) | ||
45 | sound = os.path.join(get_path(), 'alarm{}.oga'.format(level)) |
|
52 | time.sleep(0.5) | |
|
53 | else: | |||
|
54 | log.warning('Unable to play alarm, sound file not found', 'ADMIN') | |||
46 |
|
55 | |||
47 | if os.path.exists(sound): |
|
56 | def send_email(**kwargs): | |
48 | t = Thread(target=target, args=(sound, level, cycle)) |
|
57 | notifier = SchainNotify() | |
49 | t.start() |
|
58 | notifier.notify(**kwargs) | |
50 | else: |
|
59 | ||
51 | log.warning('Unable to play alarm', 'ADMIN') |
|
60 | def show_popup(message='Error'): | |
|
61 | popup(message) | |||
|
62 | ||||
|
63 | def send_alarm(): | |||
|
64 | pass | |||
|
65 | ||||
|
66 | def get_kwargs(kwargs, keys): | |||
|
67 | ret = {} | |||
|
68 | for key in keys: | |||
|
69 | ret[key] = kwargs[key] | |||
|
70 | return ret | |||
|
71 | ||||
|
72 | tasks = { | |||
|
73 | 1 : send_email, | |||
|
74 | 2 : play_sound, | |||
|
75 | 3 : show_popup, | |||
|
76 | 4 : send_alarm, | |||
|
77 | } | |||
|
78 | ||||
|
79 | tasks_args = { | |||
|
80 | 1: ['email', 'message', 'subject', 'subtitle', 'filename'], | |||
|
81 | 2: [], | |||
|
82 | 3: ['message'], | |||
|
83 | 4: [], | |||
|
84 | } | |||
|
85 | ||||
|
86 | for mode in modes: | |||
|
87 | if mode == 0: | |||
|
88 | for x in tasks: | |||
|
89 | t = Thread(target=tasks[x], kwargs=get_kwargs(kwargs, tasks_args[x])) | |||
|
90 | t.start() | |||
|
91 | break | |||
|
92 | else: | |||
|
93 | t = Thread(target=tasks[mode], kwargs=get_kwargs(kwargs, tasks_args[x])) | |||
|
94 | t.start() | |||
52 |
|
95 | |||
53 |
|
96 | |||
54 | class SchainConfigure(): |
|
97 | class SchainConfigure(): | |
55 |
|
98 | |||
56 | __DEFAULT_ADMINISTRATOR_EMAIL = "" |
|
99 | __DEFAULT_ADMINISTRATOR_EMAIL = "juan.espinoza@jro.igp.gob.pe" | |
57 | __DEFAULT_EMAIL_SERVER = "jro-zimbra.igp.gob.pe" |
|
100 | __DEFAULT_EMAIL_SERVER = "jro-zimbra.igp.gob.pe" | |
58 | __DEFAULT_SENDER_EMAIL = "notifier-schain@jro.igp.gob.pe" |
|
101 | __DEFAULT_SENDER_EMAIL = "notifier-schain@jro.igp.gob.pe" | |
59 | __DEFAULT_SENDER_PASS = "" |
|
102 | __DEFAULT_SENDER_PASS = "" | |
@@ -316,7 +359,10 class SchainNotify: | |||||
316 | Exceptions: None. |
|
359 | Exceptions: None. | |
317 | """ |
|
360 | """ | |
318 |
|
361 | |||
319 | print "Notifying to %s ..." %email |
|
362 | if email is None: | |
|
363 | email = self.__emailToAddress | |||
|
364 | ||||
|
365 | log.success('Notifying to %s ...'.format(email), 'ADMIN') | |||
320 |
|
366 | |||
321 | self.sendEmail(email_from=self.__emailFromAddress, |
|
367 | self.sendEmail(email_from=self.__emailFromAddress, | |
322 | email_to=email, |
|
368 | email_to=email, | |
@@ -325,7 +371,7 class SchainNotify: | |||||
325 | subtitle=subtitle, |
|
371 | subtitle=subtitle, | |
326 | filename=filename) |
|
372 | filename=filename) | |
327 |
|
373 | |||
328 | print "***** Your system administrator has been notified *****" |
|
374 | log.success('Email sent', 'ADMIN') | |
329 |
|
375 | |||
330 | class SchainError(Exception): |
|
376 | class SchainError(Exception): | |
331 | """SchainError is an exception class that is thrown for all known errors using Schain Py lib. |
|
377 | """SchainError is an exception class that is thrown for all known errors using Schain Py lib. |
@@ -908,7 +908,7 class Project(Process): | |||||
908 | self.id = None |
|
908 | self.id = None | |
909 | self.description = None |
|
909 | self.description = None | |
910 | self.email = None |
|
910 | self.email = None | |
911 |
self.alarm = |
|
911 | self.alarm = [0] | |
912 | self.plotterQueue = plotter_queue |
|
912 | self.plotterQueue = plotter_queue | |
913 | self.procUnitConfObjDict = {} |
|
913 | self.procUnitConfObjDict = {} | |
914 |
|
914 | |||
@@ -956,7 +956,7 class Project(Process): | |||||
956 |
|
956 | |||
957 | self.procUnitConfObjDict = newProcUnitConfObjDict |
|
957 | self.procUnitConfObjDict = newProcUnitConfObjDict | |
958 |
|
958 | |||
959 |
def setup(self, id, name='', description='', email=None, alarm= |
|
959 | def setup(self, id, name='', description='', email=None, alarm=[0]): | |
960 |
|
960 | |||
961 |
|
961 | |||
962 | print '*' * 60 |
|
962 | print '*' * 60 | |
@@ -1187,21 +1187,13 class Project(Process): | |||||
1187 | err = traceback.format_exception(sys.exc_info()[0], |
|
1187 | err = traceback.format_exception(sys.exc_info()[0], | |
1188 | sys.exc_info()[1], |
|
1188 | sys.exc_info()[1], | |
1189 | sys.exc_info()[2]) |
|
1189 | sys.exc_info()[2]) | |
1190 |
|
1190 | |||
1191 | print '***** Error occurred in %s *****' % (procUnitConfObj.name) |
|
1191 | log.error('{}'.format(err[-1]), procUnitConfObj.name) | |
1192 | print '***** %s' % err[-1] |
|
|||
1193 |
|
1192 | |||
1194 | message = ''.join(err) |
|
1193 | message = ''.join(err) | |
1195 |
|
1194 | |||
1196 | sys.stderr.write(message) |
|
1195 | sys.stderr.write(message) | |
1197 |
|
1196 | |||
1198 | if self.email is None: |
|
|||
1199 | log.warning('Email attribute has not been set', self.name) |
|
|||
1200 | return |
|
|||
1201 |
|
||||
1202 | if self.alarm: |
|
|||
1203 | schainpy.admin.alarm(1) |
|
|||
1204 |
|
||||
1205 | subject = 'SChain v%s: Error running %s\n' % ( |
|
1197 | subject = 'SChain v%s: Error running %s\n' % ( | |
1206 | schainpy.__version__, procUnitConfObj.name) |
|
1198 | schainpy.__version__, procUnitConfObj.name) | |
1207 |
|
1199 | |||
@@ -1223,8 +1215,8 class Project(Process): | |||||
1223 | subtitle += '[Start time = %s]\n' % readUnitConfObj.startTime |
|
1215 | subtitle += '[Start time = %s]\n' % readUnitConfObj.startTime | |
1224 | subtitle += '[End time = %s]\n' % readUnitConfObj.endTime |
|
1216 | subtitle += '[End time = %s]\n' % readUnitConfObj.endTime | |
1225 |
|
1217 | |||
1226 |
|
|
1218 | schainpy.admin.alarm( | |
1227 | adminObj.notify( |
|
1219 | modes=self.alarm, | |
1228 | email=self.email, |
|
1220 | email=self.email, | |
1229 | message=message, |
|
1221 | message=message, | |
1230 | subject=subject, |
|
1222 | subject=subject, | |
@@ -1300,6 +1292,7 class Project(Process): | |||||
1300 | try: |
|
1292 | try: | |
1301 | sts = procUnitConfObj.run() |
|
1293 | sts = procUnitConfObj.run() | |
1302 | is_ok = is_ok or sts |
|
1294 | is_ok = is_ok or sts | |
|
1295 | j | |||
1303 | except KeyboardInterrupt: |
|
1296 | except KeyboardInterrupt: | |
1304 | is_ok = False |
|
1297 | is_ok = False | |
1305 | break |
|
1298 | break |
@@ -36,6 +36,13 def figpause(interval): | |||||
36 | canvas.start_event_loop(interval) |
|
36 | canvas.start_event_loop(interval) | |
37 | return |
|
37 | return | |
38 |
|
38 | |||
|
39 | def popup(message): | |||
|
40 | fig = plt.figure(figsize=(12, 9), facecolor='r') | |||
|
41 | fig.text(0.5, 0.5, message, ha='center', va='center', size='20', weight='heavy', color='w') | |||
|
42 | fig.show() | |||
|
43 | figpause(1000) | |||
|
44 | ||||
|
45 | ||||
39 |
|
46 | |||
40 | class PlotData(Operation, Process): |
|
47 | class PlotData(Operation, Process): | |
41 | ''' |
|
48 | ''' |
General Comments 0
You need to be logged in to leave comments.
Login now