def main(): while True: now = roundTime(datetime.now(), roundTo=60*60) t1 = (now-timedelta(hours=2)).strftime(tf) t2 = (now).strftime(tf) output = get_pot(t1, t2) pot_df = analyse_pot() try: last_spill = pot_df.index[-1] warning = '{} - PROBLEM! NO BEAM SINCE {}!'.format(datetime.now(), last_spill) except IndexError: last_spill = datetime.strptime(t1, tf) warning = '{} - PROBLEM! NO BEAM FOR OVER 2 HOURS!'.format(datetime.now()) d = (now-last_spill) if d.seconds>(45.0*60): print(warning) alert('CHARM No Beam!', warning) else: print('{} - BEAM CHECK OK!'.format(now)) time.sleep(60*60)
def running(): # If the shifter changes we want to send an email to the new shifter. prev_shifter = '' while True: xmsg = "" ymsg = "" centre_msg = "" fwhm_msg = "" sec_msg = "" beam_status = 1 centre_status = 1 fwhm_status = 1 subject = "Warning " warn_email = False warn_centre_email = False warn_fwhm_email = False dbc = db_commands() last_msg = dbc.get_last_msg() response = dbc.get_response() # In order for the user and administrator intefaces to see all the shifters, they need to be stored in the database # To make sure that all shifters are stored in the data base we call get_all_shifters on both the database and the google sheet # Then we add all the shifters to the database that are in the google sheet but not yet in the database database_shifters = dbc.get_all_shifters() sheet_shifters = get_all_shifters() for name in sheet_shifters: if name not in database_shifters: dbc.insert_shifter({'name':name, 'email':'', 'phone':0, 'current':0, 'alert':0}) shifter = get_shifter() alertees = dbc.get_alerts() recipients = [] if shifter not in alertees: shifter_info = dbc.get_shifter_info(shifter) recipients.append(shifter_info['email']) sms = phone2email(shifter_info['phone']) recipients.append(sms) for name in alertees: shifter_info = dbc.get_shifter_info(name) recipients.append(shifter_info['email']) sms = phone2email(shifter_info['phone']) recipients.append(sms) # Send email if there is a new shifter if prev_shifter != shifter: prev_shifter = shifter dbc.set_current_shifter(shifter) shifttime=1400 date,tomorrow = get_date(shifttime) shifter_msg = new_shifter_msg.format(shifter=shifter, date=date, tomorrow=tomorrow, shifttime=shifttime) print(shifter_msg) alert('New CHARM shifter {:s}'.format(shifter), shifter_msg, '*****@*****.**', recipients) #check only SEC for intensity sec_msg = check_SEC() if sec_msg != '': warn_email = True beam_status = 0 centre_status = 0 fwhm_status = 0 subject += 'BEAM DOWN! ' xmsg, ymsg, xcentre, xfwhm, ycentre, yfwhm, bpm_error = check_BPM() # Make sure the centre/fwhm is acutally off by also comparing to the SEC if (xfwhm or xcentre or ycentre or yfwhm or bpm_error): centre_msg, fwhm_msg = check_MWPC() if centre_msg != '': warn_centre_email = True subject += 'Beam off centre! ' if fwhm_msg != '': warn_fwhm_email = True subject += 'Beam fwhm too wide ' t_now = (datetime.now()).strftime(tf) # Only send message if we haven't already alert_msg = whole_msg.format(sec=sec_msg, x=xmsg, y=ymsg, centre=centre_msg, fwhm=fwhm_msg) if last_msg is None: if warn_email or warn_fwhm_email or warn_centre_email: alert(subject, alert_msg, '*****@*****.**', recipients) dbc.insert_msg((t_now, alert_msg, 1*warn_email, 1*warn_fwhm_email, 1*warn_centre_email)) dbc.respond(0) # Send the alerts if there is something wrong or if th ebeam is up again. else: if last_msg[-3] == 1 and warn_email: print(1) # If there is a warn_email, everything is down alert(subject, alert_msg, '*****@*****.**', recipients) dbc.insert_msg((t_now, alert_msg, 0, 0, 0)) dbc.respond(0) elif last_msg[-3] == 0 and warn_email == False: print(2) # Beam is now up again alert("Notice CHARM beam is up again", "Beam up again at " + t_now, '*****@*****.**', recipients) dbc.insert_msg((t_now, "Beam up again.", 1,1*warn_fwhm_email,1*warn_centre_email)) dbc.respond(1) elif (warn_fwhm_email == False and last_msg[-2] == 0) and (warn_centre_email == False and last_msg[-1] == 0): print(3) alert('Notic Beam Centered and FWHM Normal', alert_msg, '*****@*****.**', recipients) dbc.insert_msg((t_now, alert_msg, 1, 1, 1)) dbc.respond(1) elif last_msg[-2] == 1 or last_msg[-1] == 1: print(4) # FWHM too large or Centre off if (warn_fwhm_email and last_msg[-2] == 1) or (warn_centre_email and last_msg[-1] == 1): alert(subject, alert_msg, '*****@*****.**', recipients) dbc.insert_msg((t_now, alert_msg, 1, 1*(not warn_fwhm_email), 1*(not warn_centre_email))) dbc.respond(0) # FWHM and Centre back to normal elif ((last_msg[-3] == 0 or last_msg[-2] == 0 or last_msg[-1] == 0) and response == (0,)): print(5) #Keep sending messages until user responds alert('Resending ' + subject, alert_msg, '*****@*****.**', recipients) elif ((last_msg[-3] == 0 or last_msg[-2] == 0 or last_msg[-1] == 0) and response == (0,)): print(5) #Keep sending messages until user responds alert('Resending ' + subject, alert_msg, '*****@*****.**', recipients) del dbc print(alert_msg) time.sleep(600)
def running(): # If the shifter changes we want to send an email to the new shifter. while True: xmsg = "" ymsg = "" centre_msg = "" fwhm_msg = "" sec_msg = "" beam_status = 1 centre_status = 1 fwhm_status = 1 subject = "Warning " warn_email = False warn_centre_email = False warn_fwhm_email = False dbc = db_commands() last_msg = dbc.get_last_msg() response = dbc.get_response() downtime = dbc.get_setting('downtime') prev_shifter = dbc.get_current_shifter() try: downtime = int(downtime) except ValueError: #downtime was not a string; wait time 10 minutes by default downtime = 10 print( 'ERROR: downtime in database was not a string, sleeping for defualt 10 min' ) downtime *= 60 #multiply by 60 to get seconds # In order for the user and administrator intefaces to see all the shifters, they need to be stored in the database # To make sure that all shifters are stored in the data base we call get_all_shifters on both the database and the google sheet # Then we add all the shifters to the database that are in the google sheet but not yet in the database database_shifters = dbc.get_all_shifters() sheet_shifters = get_all_shifters() for name in sheet_shifters: if name not in database_shifters: dbc.insert_shifter({ 'name': name, 'email': '', 'phone': 0, 'current': 0, 'alert': 0 }) shifter = get_shifter() alertees = dbc.get_alerts() recipients = [] if shifter not in alertees: shifter_info = dbc.get_shifter_info(shifter) recipients.append(shifter_info['email']) sms = phone2email(shifter_info['phone']) recipients.append(sms) for name in alertees: shifter_info = dbc.get_shifter_info(name) recipients.append(shifter_info['email']) sms = phone2email(shifter_info['phone']) recipients.append(sms) # Send email if there is a new shifter if prev_shifter != shifter: if shifter == '': #Something went wrong when getting shifter from google sheet, use prev shifter shifter = prev_shifter else: prev_shifter = shifter dbc.set_current_shifter(shifter) shifttime = 1400 date, tomorrow = get_date(shifttime) shifter_msg = new_shifter_msg.format(shifter=shifter, date=date, tomorrow=tomorrow, shifttime=shifttime) print(shifter_msg) alert('New CHARM shifter {:s}'.format(shifter), shifter_msg, '*****@*****.**', recipients) #check only SEC for intensity sec_msg = check_SEC() if sec_msg != '': warn_email = True beam_status = 0 centre_status = 0 fwhm_status = 0 subject += 'BEAM DOWN! ' xmsg, ymsg, xcentre, xfwhm, ycentre, yfwhm, bpm_error = check_BPM() # Make sure the centre/fwhm is acutally off by also comparing to the SEC if (xfwhm or xcentre or ycentre or yfwhm or bpm_error): centre_msg, fwhm_msg = check_MWPC() if centre_msg != '': warn_centre_email = True subject += 'Beam off centre! ' if fwhm_msg != '': warn_fwhm_email = True subject += 'Beam fwhm too wide ' t_now = (datetime.now()).strftime(tf) # Only send message if we haven't already alert_msg = whole_msg.format(sec=sec_msg, x=xmsg, y=ymsg, centre=centre_msg, fwhm=fwhm_msg) if last_msg is None: if warn_email or warn_fwhm_email or warn_centre_email: alert(subject, alert_msg, '*****@*****.**', recipients) dbc.insert_msg((t_now, alert_msg, 1 * warn_email, 1 * warn_fwhm_email, 1 * warn_centre_email)) dbc.respond(0) # Send the alerts if there is something wrong or if th ebeam is up again. else: if last_msg[-3] == 1 and warn_email: print(1) # If there is a warn_email, everything is down alert(subject, alert_msg, '*****@*****.**', recipients) dbc.insert_msg((t_now, alert_msg, 0, 0, 0)) dbc.respond(0) elif last_msg[-3] == 0 and warn_email == False: print(2) # Beam is now up again alert("Notice CHARM beam is up again", "Beam up again at " + t_now, '*****@*****.**', recipients) dbc.insert_msg((t_now, "Beam up again.", 1, 1 * warn_fwhm_email, 1 * warn_centre_email)) dbc.respond(1) elif (warn_fwhm_email == False and last_msg[-2] == 0) and (warn_centre_email == False and last_msg[-1] == 0): print(3) alert('Notic Beam Centered and FWHM Normal', alert_msg, '*****@*****.**', recipients) dbc.insert_msg((t_now, alert_msg, 1, 1, 1)) dbc.respond(1) elif last_msg[-2] == 1 or last_msg[-1] == 1: print(4) # FWHM too large or Centre off if (warn_fwhm_email and last_msg[-2] == 1) or (warn_centre_email and last_msg[-1] == 1): alert(subject, alert_msg, '*****@*****.**', recipients) dbc.insert_msg( (t_now, alert_msg, 1, 1 * (not warn_fwhm_email), 1 * (not warn_centre_email))) dbc.respond(0) # FWHM and Centre back to normal elif ((last_msg[-3] == 0 or last_msg[-2] == 0 or last_msg[-1] == 0) and response == (0, )): print(5) #Keep sending messages until user responds alert('Resending ' + subject, alert_msg, '*****@*****.**', recipients) elif ((last_msg[-3] == 0 or last_msg[-2] == 0 or last_msg[-1] == 0) and response == (0, )): print(5) #Keep sending messages until user responds alert('Resending ' + subject, alert_msg, '*****@*****.**', recipients) del dbc print(alert_msg) time.sleep(downtime)