-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlueFors_Alarm.py
128 lines (103 loc) · 4.73 KB
/
BlueFors_Alarm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# -*- coding: utf-8 -*-
"""
===========================================
Program : LFL_Lab_Maintenance/BlueFors_Alarm.py
===========================================
Summary: Sends out an email when pulse tubes are off
or when the fridge's temperature is >= 50mK
Launches program w/o GUI
"""
__author__ = ["Sadman Ahmed Shanto","Evangelos Vlachos","Clark Miyamoto"]
import csv
import logging
import os
import signal
import time
from BlueFors import BlueFors
from Emailer import Emailer
from Slack_Messenger import Slack_er
logging.getLogger('qcodes.instrument.instrument').setLevel(logging.ERROR)
class BlueForsAlarm:
def __init__(self, folder_path, mc_channel_id=6, temp_threshold=0.05, wait_time=60):
self.folder_path = folder_path
self.mc_channel_id = mc_channel_id
self.temp_threshold = temp_threshold
self.wait_time = wait_time
self.bf = BlueFors('bf_fridge',
folder_path=folder_path,
channel_vacuum_can=1,
channel_pumping_line=2,
channel_compressor_outlet=3,
channel_compressor_inlet=4,
channel_mixture_tank=5,
channel_venting_line=6,
channel_50k_plate=1,
channel_4k_plate=2,
channel_magnet=None,
channel_still=6,
channel_mixing_chamber=5)
self.email_list = self.get_email_list()
self.slack_channel = "sneezy-bluefors"
self.slacker = Slack_er(self.bf)
def get_subject(self, pt_off, mc_temp_high, temp):
subject = []
head_string = "🚨[URGENT]🚨"
separator = ". "
subject.append(head_string)
if pt_off:
subject.append("BlueFors' Pulse Tubes are OFF")
subject.append(separator)
if mc_temp_high:
subject.append("BlueFors is WARM.")
tail_string = " Current temperature is {} K".format(temp)
subject.append(tail_string)
subject = "".join(subject)
return subject
def get_email_list(self):
with open('emails.csv') as f:
reader = csv.reader(f)
email_list = list(reader)[0]
return email_list
def get_content(self, content="This message has been automatically generated by BlueFors_Alarm script from LFL Emailer Bot."):
return content
def send_slack_message(self, message):
slack_message = '<!channel> ' + message
self.slacker.send_message(self.slack_channel, slack_message)
def start_alarm_system(self):
'''
Call this function to start the alarm system.
Returns:
- None
'''
# Register the signal handlers
signal.signal(signal.SIGINT, self.signal_handler) # Handle Control+C
signal.signal(signal.SIGTERM, self.signal_handler) # Handle Control+Z
# Send message that the alarm system is activated
self.send_slack_message('🔔 BlueFors Alarm is now active!')
while True:
pulse_tube_status = self.bf.get_pulse_tube_status()
mc_temp = self.bf.get_temperature(self.mc_channel_id)
print("Pulse Tube Status: {}".format(pulse_tube_status))
print("MC Temperature: {} K".format(mc_temp))
pulse_tube_off = bool(pulse_tube_status == 0)
mc_temp_high = bool(mc_temp >= self.temp_threshold)
if pulse_tube_off or mc_temp_high: # Triggers if something is wrong w/ fridge
# Send out an email
subject = self.get_subject(pulse_tube_off, mc_temp_high, mc_temp)
content = self.get_content()
# Send message to Slack
self.send_slack_message(subject)
#break
print("Waiting for next reading...")
time.sleep(self.wait_time)
def alarm_system_deactivated(self):
page_link = "https://uscedu-my.sharepoint.com/personal/lflab_usc_edu/_layouts/OneNote.aspx?id=%2Fpersonal%2Flflab_usc_edu%2FDocuments%2FLearning%20Guide&wd=target%28%F0%9F%92%BB%20Software%2F0%20Etc%2FFridge%20Monitor.one%7C3C1D6285-FF93-4F2A-9120-534B5293927C%2FBlueFors%20Alarm%7C134AE989-6DF2-714A-9A23-E187A6CFCAFA%2F%29"
self.send_slack_message(f'🔕 BlueFors Alarm is now deactivated!\n\n\nPlease activate it after the issue is resolved. Here is how to do so - {page_link}')
def signal_handler(self, signum, frame):
self.alarm_system_deactivated()
exit(1)
if __name__=='__main__':
folder_path = r'C:\Users\lfl\Bluefors logs'
alarm = BlueForsAlarm(folder_path)
alarm.start_alarm_system()
alarm.alarm_system_deactivated()