@@ -18,6 +18,7 from email.mime.application import MIMEApplication | |||
|
18 | 18 | from email.mime.multipart import MIMEMultipart |
|
19 | 19 | |
|
20 | 20 | from schainpy.utils import log |
|
21 | from schainpy.model.graphics.jroplot_data import popup | |
|
21 | 22 | |
|
22 | 23 | def get_path(): |
|
23 | 24 | ''' |
@@ -33,27 +34,69 def get_path(): | |||
|
33 | 34 | except: |
|
34 | 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): | |
|
41 | for __ in range(cycle): | |
|
47 | def play_sound(): | |
|
48 | sound = os.path.join(get_path(), 'alarm1.oga') | |
|
49 | if os.path.exists(sound): | |
|
50 | for __ in range(2): | |
|
42 | 51 | os.system('paplay {}'.format(sound)) |
|
43 | 52 | time.sleep(0.5) |
|
44 | ||
|
45 | sound = os.path.join(get_path(), 'alarm{}.oga'.format(level)) | |
|
46 | ||
|
47 | if os.path.exists(sound): | |
|
48 | t = Thread(target=target, args=(sound, level, cycle)) | |
|
53 | else: | |
|
54 | log.warning('Unable to play alarm, sound file not found', 'ADMIN') | |
|
55 | ||
|
56 | def send_email(**kwargs): | |
|
57 | notifier = SchainNotify() | |
|
58 | notifier.notify(**kwargs) | |
|
59 | ||
|
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])) | |
|
49 | 90 | t.start() |
|
91 | break | |
|
50 | 92 | else: |
|
51 | log.warning('Unable to play alarm', 'ADMIN') | |
|
93 | t = Thread(target=tasks[mode], kwargs=get_kwargs(kwargs, tasks_args[x])) | |
|
94 | t.start() | |
|
52 | 95 | |
|
53 | 96 | |
|
54 | 97 | class SchainConfigure(): |
|
55 | 98 | |
|
56 | __DEFAULT_ADMINISTRATOR_EMAIL = "" | |
|
99 | __DEFAULT_ADMINISTRATOR_EMAIL = "juan.espinoza@jro.igp.gob.pe" | |
|
57 | 100 | __DEFAULT_EMAIL_SERVER = "jro-zimbra.igp.gob.pe" |
|
58 | 101 | __DEFAULT_SENDER_EMAIL = "notifier-schain@jro.igp.gob.pe" |
|
59 | 102 | __DEFAULT_SENDER_PASS = "" |
@@ -316,7 +359,10 class SchainNotify: | |||
|
316 | 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 | 367 | self.sendEmail(email_from=self.__emailFromAddress, |
|
322 | 368 | email_to=email, |
@@ -325,7 +371,7 class SchainNotify: | |||
|
325 | 371 | subtitle=subtitle, |
|
326 | 372 | filename=filename) |
|
327 | 373 | |
|
328 | print "***** Your system administrator has been notified *****" | |
|
374 | log.success('Email sent', 'ADMIN') | |
|
329 | 375 | |
|
330 | 376 | class SchainError(Exception): |
|
331 | 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 | 908 | self.id = None |
|
909 | 909 | self.description = None |
|
910 | 910 | self.email = None |
|
911 |
self.alarm = |
|
|
911 | self.alarm = [0] | |
|
912 | 912 | self.plotterQueue = plotter_queue |
|
913 | 913 | self.procUnitConfObjDict = {} |
|
914 | 914 | |
@@ -956,7 +956,7 class Project(Process): | |||
|
956 | 956 | |
|
957 | 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 | 962 | print '*' * 60 |
@@ -1188,20 +1188,12 class Project(Process): | |||
|
1188 | 1188 | sys.exc_info()[1], |
|
1189 | 1189 | sys.exc_info()[2]) |
|
1190 | 1190 | |
|
1191 | print '***** Error occurred in %s *****' % (procUnitConfObj.name) | |
|
1192 | print '***** %s' % err[-1] | |
|
1191 | log.error('{}'.format(err[-1]), procUnitConfObj.name) | |
|
1193 | 1192 | |
|
1194 | 1193 | message = ''.join(err) |
|
1195 | 1194 | |
|
1196 | 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 | 1197 | subject = 'SChain v%s: Error running %s\n' % ( |
|
1206 | 1198 | schainpy.__version__, procUnitConfObj.name) |
|
1207 | 1199 | |
@@ -1223,8 +1215,8 class Project(Process): | |||
|
1223 | 1215 | subtitle += '[Start time = %s]\n' % readUnitConfObj.startTime |
|
1224 | 1216 | subtitle += '[End time = %s]\n' % readUnitConfObj.endTime |
|
1225 | 1217 | |
|
1226 |
|
|
|
1227 | adminObj.notify( | |
|
1218 | schainpy.admin.alarm( | |
|
1219 | modes=self.alarm, | |
|
1228 | 1220 | email=self.email, |
|
1229 | 1221 | message=message, |
|
1230 | 1222 | subject=subject, |
@@ -1300,6 +1292,7 class Project(Process): | |||
|
1300 | 1292 | try: |
|
1301 | 1293 | sts = procUnitConfObj.run() |
|
1302 | 1294 | is_ok = is_ok or sts |
|
1295 | j | |
|
1303 | 1296 | except KeyboardInterrupt: |
|
1304 | 1297 | is_ok = False |
|
1305 | 1298 | break |
@@ -36,6 +36,13 def figpause(interval): | |||
|
36 | 36 | canvas.start_event_loop(interval) |
|
37 | 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 | 47 | class PlotData(Operation, Process): |
|
41 | 48 | ''' |
General Comments 0
You need to be logged in to leave comments.
Login now