Repositório do ISTTOK
Bernardo Carvalho
2020-01-27 d2045469e5678e608aabc799bf3d92f8f66fec20
commit | author | age
9af01e 1 #!/usr/bin/env python2
B 2 # -*- coding: utf-8 -*-
3 """
4 Created on Thu Feb  8 16:34:35 2018
5
6 @author: joao_loureiro
7 """
8
9 import epics
10 import time
11 import os
12
13 import matplotlib
14 matplotlib.use('Agg')
15 import matplotlib.pyplot as plt
16 import matplotlib.ticker as mtick
17
18 import mpld3
19 from mpld3 import plugins, utils
20
21 import numpy as np
22 import MySQLdb
23
24 os.environ['EPICS_CA_ADDR_LIST']= '192.168.1.110 192.168.1.152'
25 os.environ['EPICS_CA_AUTO_ADDR_LIST']= 'NO'
26
27 class HelloWorld(mpld3.plugins.PluginBase):  # inherit from PluginBase
28     """Hello World plugin"""
29     
30     JAVASCRIPT = """
31     mpld3.register_plugin("helloworld", HelloWorld);
32     HelloWorld.prototype = Object.create(mpld3.Plugin.prototype);
33     HelloWorld.prototype.constructor = HelloWorld;
34     function HelloWorld(fig, props){
35         mpld3.Plugin.call(this, fig, props);
36     };
37     
38     HelloWorld.prototype.draw = function(){
39         // FIXME: this is a very brittle way to select the y-axis element
40         var ax = this.fig.axes[0].elements[1];
41
42         // see https://github.com/mbostock/d3/wiki/Formatting#d3_format
43         // for d3js formating documentation
44         ax.axis.tickFormat(d3.format(",.3e"));
45
46         // TODO: use a function for tick values that
47         // updates when values pan and zoom
48         //ax.axis.tickValues([1,100,1000]);
49
50         // HACK: use reset to redraw figure
51         this.fig.reset(); 
52     }
53     """
54     def __init__(self):
55         self.dict_ = {"type": "helloworld"}
56
57 style_tooltip = """
58 <style>
59 .tooltip {
60     position: relative;
61     display: inline-block;
62     border-bottom: 1px dotted black;
63 }
64
65 .tooltip .tooltiptext {
66     visibility: hidden;
67     width: 800px;
68     background-color: #555;
69     color: #fff;
70     text-align: center;
71     border-radius: 6px;
72     padding: 5px 0;
73     position: absolute;
74     z-index: 1;
75     bottom: 125%;
76     left: 50%;
77     margin-left: -60px;
78     opacity: 0;
79     transition: opacity 1s;
80 }
81
82 .tooltip .tooltiptext::after {
83     content: "";
84     position: absolute;
85     top: 100%;
86     left: 50%;
87     margin-left: -5px;
88     border-width: 5px;
89     border-style: solid;
90     border-color: #555 transparent transparent transparent;
91 }
92
93 .tooltip:hover .tooltiptext {
94     visibility: visible;
95     opacity: 1;
96 }
97 </style>
98 """
99
100 def text_with_tooltip(text, tooltip):
101     html = '<div class="tooltip">'+text+'\n'\
102            '  <span class="tooltiptext">'+tooltip+'</span></div>'
103     return html
104
105
106 def application(environ,start_response):
107     status = '200 OK'
108     valuePrimary1  = epics.caget('ISTTOK:central:RPump1-Pressure')
109     valuePrimary2  = epics.caget('ISTTOK:central:RPump2-Pressure')
110     valueChamber1  = epics.caget('ISTTOK:central:VVessel-Pressure')
111     valueTMPadmission  = epics.caget('ISTTOK:central:TMPump1-PressureAdmission')
112     now = time.ctime()
113     # Open database connection
114     db = MySQLdb.connect(host = "192.168.1.152",user = "report", passwd="$report", db = "archive")
115     # prepare a cursor object using cursor() method
116     cursor = db.cursor()
117     
118     # 19 | ISTTOK:central:VVessel-Pressure
119     sql_chamber ="SELECT `smpl_time`, `float_val` FROM `sample` WHERE `channel_id` = 19 ORDER BY `smpl_time` DESC LIMIT 250;"
120     # 21 | ISTTOK:central:RPump1-Pressure
121     sql_primary ="SELECT `smpl_time`, `float_val` FROM `sample` WHERE `channel_id` = 21 ORDER BY `smpl_time` DESC LIMIT 250;"
122     # Execute the SQL command
123     cursor.execute(sql_chamber)
124     # Fetch all the rows in a list of lists.
125     results = cursor.fetchall()
126     
127     data_chamber = []
128     for row in results:
129         data_chamber.append([row[0],row[1]])
130         
131     # Execute the SQL command
132     cursor.execute(sql_primary)
133     # Fetch all the rows in a list of lists.
134     results = cursor.fetchall()
135     
136     data_primary = []
137     for row in results:
138         data_primary.append([row[0],row[1]])
139     
140     # disconnect from server
141     db.close()
142     #
143     data_chamber = np.array(data_chamber)
144     data_primary = np.array(data_primary)
145     fig,axr = plt.subplots(1, sharex=True)
146     
147     axr.plot(data_chamber[:,0],data_chamber[:,1])
148     axr.plot(data_primary[:,0],data_primary[:,1])
149     axr.legend(['Tokamak Chamber', 'Primary pump'], loc =2)
150     axr.set_xlabel('Time', fontsize = 15)
151     axr.set_ylabel('Pressure [mbar]', fontsize = 15)
152     axr.yaxis.set_major_formatter(mtick.FormatStrFormatter('%.3e'))
153     fig.set_size_inches(12,5, forward=True)
154     fig.tight_layout()
155     ax_fmt = HelloWorld()
156     mpld3.plugins.connect(fig, ax_fmt)
157     html2 = mpld3.fig_to_html(fig)
158     html = '<html>\n' \
159            '<head>' \
160            '<title>ISTTOK EPICS WSGI Page</title>' \
161            '</head>\n' \
162            '<body>\n' \
163            '<div>'+style_tooltip+'</div>\n'\
164            '<div style="width: 100%; font-size: 40px; font-weight: bold; text-align: center;">\n' \
165            'mod_wsgi EPICS ISTTOK info Page\n' \
166            '</div>\n' \
167            '<div style="width: 100%; font-size: 30px; font-weight: bold; text-align: center;">\n' \
168            ' '+text_with_tooltip('Primary pump 1 pressure: '+ '{0:.3e}'.format(valuePrimary1)+ ' mBar','ISTTOK:vacuum:Pressure_Primary1')+'\n' \
169            '</div>\n' \
170            '<div style="width: 100%; font-size: 30px; font-weight: bold; text-align: center;">\n' \
171            ' '+text_with_tooltip('Turbopump 1 admission pressure: '+ '{0:.3e}'.format(valueTMPadmission)+ ' mBar','ISTTOK:vacuum:Pressure_TMP_admission')+'\n' \
172            '</div>\n' \
173            '<div style="width: 100%; font-size: 30px; font-weight: bold; text-align: center;">\n' \
174            ' '+text_with_tooltip('Tokamak Vessel pressure: '+ '{0:.3e}'.format(valueChamber1)+ ' mBar','ISTTOK:vacuum:Pressure_Chamber1')+'\n' \
175            '</div>\n' \
176            '<div style="width: 100%; font-size: 30px; font-weight: bold; text-align: center;">\n' \
177             'Current Time: ' +now+ '\n' \
178            '</div><center>\n'+html2 +'</center>\n' \
179            '</body>\n' \
180            '</html>\n'
181     response_header = [('Content-type','text/html')]
182     start_response(status,response_header)
183     html = bytes(html, encoding= 'utf-8')
184     return [html]
185
186 #'</div><center>\n'+html2.encode('utf8')+'</center>\n' \