예제 #1
0
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label_2.setText(_translate("MainWindow", "Deviation before users are alerted [%]"))
        self.label_3.setText(_translate("MainWindow", "SEC reference [e10]"))
        self.label_4.setText(_translate("MainWindow", "MWPC vertical FWHM refrence"))
        self.label_5.setText(_translate("MainWindow", "MWPC vertical Centre reference [e-1 mm]"))
        self.label_6.setText(_translate("MainWindow", "MWPC horizontal FWHM reference"))
        self.label.setText(_translate("MainWindow", "Down time before users are alerted [min]"))
        self.label_7.setText(_translate("MainWindow", "MWPC horizontal Centre  reference [e-1 mm]"))
        self.pushButton.setText(_translate("MainWindow", "Load Defaults"))
        self.label_8.setText(_translate("MainWindow", "User Name"))
        self.label_10.setText(_translate("MainWindow", "Phone number"))
        self.label_9.setText(_translate("MainWindow", "Email"))
        self.label_13.setText(_translate("MainWindow", "Alert"))

        shifters = get_all_shifters()
        shifters = sorted(shifters, key=str.lower)
        num_shifters = len(shifters)
        n = 0
        e = 1
        p = 2
        for name in shifters:
          shifter_info = self.db_cmd.get_shifter_info(name)
          if len(shifter_info) == 0:
            email = ""
            phone = ""
          else:
            email = shifter_info['email']
            phone = str(shifter_info['phone'])
          self.all_user_boxes[n].setText(_translate("MainWindow", name))
          self.all_user_boxes[e].setText(_translate("MainWindow", email))
          self.all_user_boxes[p].setText(_translate("MainWindow", phone))
          n += 4
          e += 4
          p += 4
예제 #2
0
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)
예제 #3
0
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1024, 908)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.plainTextEdit = QtWidgets.QPlainTextEdit(self.centralwidget)
        self.plainTextEdit.setGeometry(QtCore.QRect(590, 0, 431, 541))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.plainTextEdit.sizePolicy().hasHeightForWidth())
        self.plainTextEdit.setSizePolicy(sizePolicy)
        self.plainTextEdit.setTextInteractionFlags(QtCore.Qt.TextSelectableByKeyboard|QtCore.Qt.TextSelectableByMouse)
        self.plainTextEdit.setObjectName("plainTextEdit")
        self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 0, 591, 541))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.mwpcVCentreRefBox = QtWidgets.QSpinBox(self.gridLayoutWidget)
        self.mwpcVCentreRefBox.setMaximum(999)
        self.mwpcVCentreRefBox.setObjectName("mwpcVCentreRefBox")
        self.gridLayout.addWidget(self.mwpcVCentreRefBox, 4, 1, 1, 1)
        self.mwpcVFwhmRefBox = QtWidgets.QSpinBox(self.gridLayoutWidget)
        self.mwpcVFwhmRefBox.setMaximum(999)
        self.mwpcVFwhmRefBox.setObjectName("mwpcVFwhmRefBox")
        self.gridLayout.addWidget(self.mwpcVFwhmRefBox, 3, 1, 1, 1)
        self.mwpcHCentreRefBox = QtWidgets.QSpinBox(self.gridLayoutWidget)
        self.mwpcHCentreRefBox.setMaximum(999)
        self.mwpcHCentreRefBox.setObjectName("mwpcHCentreRefBox")
        self.gridLayout.addWidget(self.mwpcHCentreRefBox, 6, 1, 1, 1)
        self.secRefBox = QtWidgets.QSpinBox(self.gridLayoutWidget)
        self.secRefBox.setMaximum(999)
        self.secRefBox.setObjectName("secRefBox")
        self.gridLayout.addWidget(self.secRefBox, 2, 1, 1, 1)
        self.downTimeBox = QtWidgets.QSpinBox(self.gridLayoutWidget)
        self.downTimeBox.setMaximum(120)
        self.downTimeBox.setObjectName("downTimeBox")
        self.gridLayout.addWidget(self.downTimeBox, 0, 1, 1, 1)
        self.label_2 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_2.setObjectName("label_2")
        self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
        self.label_3 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_3.setObjectName("label_3")
        self.gridLayout.addWidget(self.label_3, 2, 0, 1, 1)
        self.label_4 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_4.setObjectName("label_4")
        self.gridLayout.addWidget(self.label_4, 3, 0, 1, 1)
        self.label_5 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_5.setObjectName("label_5")
        self.gridLayout.addWidget(self.label_5, 4, 0, 1, 1)
        self.mwpcHFwhmRefBox = QtWidgets.QSpinBox(self.gridLayoutWidget)
        self.mwpcHFwhmRefBox.setMaximum(999)
        self.mwpcHFwhmRefBox.setObjectName("mwpcHFwhmRefBox")
        self.gridLayout.addWidget(self.mwpcHFwhmRefBox, 5, 1, 1, 1)
        self.label_6 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_6.setObjectName("label_6")
        self.gridLayout.addWidget(self.label_6, 5, 0, 1, 1)
        self.deviationBox = QtWidgets.QSpinBox(self.gridLayoutWidget)
        self.deviationBox.setMaximum(999)
        self.deviationBox.setObjectName("deviationBox")
        self.gridLayout.addWidget(self.deviationBox, 1, 1, 1, 1)
        self.label = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label.setObjectName("label")
        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
        self.label_7 = QtWidgets.QLabel(self.gridLayoutWidget)
        self.label_7.setObjectName("label_7")
        self.gridLayout.addWidget(self.label_7, 6, 0, 1, 1)
        self.pushButton = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.pushButton.setObjectName("pushButton")
        self.gridLayout.addWidget(self.pushButton, 7, 0, 1, 1)
        self.gridLayoutWidget_2 = QtWidgets.QWidget(self.centralwidget)
        self.gridLayoutWidget_2.setGeometry(QtCore.QRect(0, 540, 1021, 281))
        self.gridLayoutWidget_2.setObjectName("gridLayoutWidget_2")
        self.gridLayout_4 = QtWidgets.QGridLayout(self.gridLayoutWidget_2)
        self.gridLayout_4.setContentsMargins(0, 0, 0, 0)
        self.gridLayout_4.setObjectName("gridLayout_4")
        self.label_8 = QtWidgets.QLabel(self.gridLayoutWidget_2)
        self.label_8.setAlignment(QtCore.Qt.AlignCenter)
        self.label_8.setObjectName("label_8")
        self.gridLayout_4.addWidget(self.label_8, 0, 0, 1, 1)
        self.label_13 = QtWidgets.QLabel(self.gridLayoutWidget_2)
        self.label_13.setAlignment(QtCore.Qt.AlignCenter)
        self.label_13.setObjectName("label_13")
        self.gridLayout_4.addWidget(self.label_13, 0, 3, 1, 1)

        self.all_user_boxes = []
        self.emailEdits = []
        self.phoneEdits = []
        self.usernames = []
        self.alert_checkboxes = []
        num_shifters = len(get_all_shifters())
        for i in range(num_shifters):
          myLabel = QtWidgets.QLabel(self.gridLayoutWidget_2) 
          myLabel.setObjectName("user_label_"+str(i))
          self.all_user_boxes.append(myLabel)
          self.usernames.append(myLabel)
          for j in range(2):
            myLineEdit = QtWidgets.QLineEdit(self.gridLayoutWidget_2)
            myLineEdit.setObjectName("lineEdit_"+str(i+j))
            self.all_user_boxes.append(myLineEdit)
            if j == 0:
              self.emailEdits.append(myLineEdit)
            else:
              self.phoneEdits.append(myLineEdit)
          checkBox = QtWidgets.QCheckBox(self.gridLayoutWidget_2)
          checkBox.setText("")
          checkBox.setObjectName("checkBox_"+str(i))
          self.all_user_boxes.append(checkBox)
          self.alert_checkboxes.append(checkBox)

        i = 0
        for j in range(2,num_shifters+2):
          for k in range(0,4):
            self.gridLayout_4.addWidget(self.all_user_boxes[i], j, k, 1, 1)
            i += 1

        self.label_10 = QtWidgets.QLabel(self.gridLayoutWidget_2)
        self.label_10.setAlignment(QtCore.Qt.AlignCenter)
        self.label_10.setObjectName("label_10")
        self.gridLayout_4.addWidget(self.label_10, 0, 2, 1, 1)
        self.label_9 = QtWidgets.QLabel(self.gridLayoutWidget_2)
        self.label_9.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.label_9.setAlignment(QtCore.Qt.AlignCenter)
        self.label_9.setObjectName("label_9")
        self.gridLayout_4.addWidget(self.label_9, 0, 1, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1024, 38))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.deviationBox.valueChanged['int'].connect(MainWindow.deviation_changed)
        self.pushButton.clicked.connect(MainWindow.load_defaults)
        self.mwpcVCentreRefBox.valueChanged['int'].connect(MainWindow.mwpc_V_centre_changed)
        self.mwpcVFwhmRefBox.valueChanged['int'].connect(MainWindow.mwpc_V_FWHM_changed)
        self.secRefBox.valueChanged['int'].connect(MainWindow.sec_ref_changed)
        self.mwpcHFwhmRefBox.valueChanged['int'].connect(MainWindow.mwpc_H_FWHM_changed)
        self.downTimeBox.valueChanged['int'].connect(MainWindow.downtime_changed)
        self.mwpcHCentreRefBox.valueChanged['int'].connect(MainWindow.mwpc_H_centre_changed)
        self.pushButton.clicked.connect(self.mwpcHCentreRefBox.update)
        self.pushButton.clicked.connect(self.mwpcHFwhmRefBox.update)
        for line in self.emailEdits:
          line.editingFinished.connect(self.user_email_changed)
        for line in self.phoneEdits:
          line.editingFinished.connect(self.user_phone_changed)
        for box in self.alert_checkboxes:
          box.clicked['bool'].connect(self.alert_checked)

        QtCore.QMetaObject.connectSlotsByName(MainWindow)
예제 #4
0
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)