-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPYMONT_Monitoring_v1.0.py
1296 lines (1196 loc) · 53.9 KB
/
PYMONT_Monitoring_v1.0.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 3 11:12:03 2018
@author: serra
v0.1 - Embedding pyqtgraph in the interface, communication tab with each
instrument
v0.2 - Changing layout to add a splitter in order to resize the graph/commands
v0.3 - Add support for second lakeshore controller
Added a configuration file (instrumentation_config.json)
Added dynamical pen, linestyle change with type of instr., color change
for different sensors
corrections
---------
sending commands to lakeshore crashed the program
cause:
in sendLakeshore method
if (self.LakeShoreSend.text()) != '' & (self.dc._STARTED==True):
(self.LakeShoreSend.text()) != ''
replaced by
(self.LakeShoreSend.text() != '')
---------
was not communicating to the vacuum gauge with the serial/USB adapter added
cause:
the adapter port is COM4 not COM1
change :self.vg = vg.TPG262(port='COM4',id='001',type='P')
---------
lakeshore.py Changing input in instrument config file is not dynamic
change:
results = dict()
T_K = [self._send_command('KRDG? '+i) for i in self.inputs]
for idx, i in enumerate(self.inputs):
results[self.inst_id+self._send_command('INNAME? '+i).strip()]=T_K[idx]
return results
---------
v0.3.1 - Pressure Y axis now in log scale
Line width set to 3
Adding a more appropriate message to the About info box
Status bar now display name+version of soft
X grid displayed on plots init
corrections
---------
Two files were created, one on initialization, one on "start" monitoring
the start method of the comm class is to blame for the extra file on
initialization, commenting the line
# self.comm.start()
---------
Couldn't access the menu bar (clicking on it no effect)
No layout to main_widget, self.l end up being on top, preventing menubar to
receive signals
Adding a layout to main_widget which contains the splitter l widget
change:
self.main_widget.layout = QVBoxLayout(self)
self.main_widget.layout.addWidget(self.l)
self.main_widget.setLayout(self.main_widget.layout)
v0.4 - Adding UPS communication
corrections
---------
Timestamp human do have millisecond, never used in anything else.
Creating a common datetime format which should replace any format in prog
change:
self.DATETIME_FMT = '%d-%m-%Y_%H-%M-%S'
---------
Notes and Commands are in one the data columns, the tabulations used for
writing was static (14?).
Changed to dynamic with the size of the header
change:
(len(self.dc.header.keys())+1)*'\t'
---------
Two tabs (\t\t) between datetime and reading #
change:
self.datatime = datetime.datetime.utcnow().strftime(self.comm.df)+'\t'
to
self.datatime = datetime.datetime.utcnow().strftime(self.comm.df)
v0.4.1 - Adding Multimeter rhode&schwarze communication (HMC8012)
Adding Power Supply rhode&schwarze communication (HMP4040)
known issues
---------
Slow application:
1. Too much data to plot over more than a day, significant updating
time for the plot:
possible solution - restrict data displayed to 1 day
2. Line thickness is a limiting factor for speed, initial width of 3
possible solution - reduce line width (1.5 for now)
corrections
---------
v0.4.2 - Adding switching mechanism for choosing which instrument we want to use
known issues
---------
Cannot put more than 2 instrument in each category, will need to create an
iterator over all instrument where the choosen instrument will be selected
by using the qpushbutton and the next() method
v0.4.3 - Adding a last cooldown directory creation in the file menu for support
of the bokeh server
Adding an instruction server which redirect request to instruments
on a separate thread [class InstructionServer():]
host='134.171.36.18', port=4500
Adding oscilloscope Agilent DSO5034A
host='134.171.5.184', port=5025
Adding power supply Rigol DP831A (replacing R&S HMP4040)
serial COMx?
v0.4.4 - Instruction server functions:
- INST|READ: dict. of all instruments as define in _dump_sensors
- INST|LIST: dict. of all instrument tags <TAG> and their resp. class
- INST|<TAG>|<CMD>: raw output of the <CMD> for inst <TAG>
v0.4.4.1 - Rigol library to use TCP instead of serial (dalvarez)
v0.4.5 - Configuration file instrumentation_config_2 json file with proper structure
Communication will loop over every entry and configure the communication to the instrument
Addint timestamp to the dictionnary of the datapoint, switching reading # and time in log files
Only ONE real time process at a time. Either the monitoring from CRIMON and the only data available would be the
last point
Or the instruction server is running and monitoring is not and full real time is available
On a longer time scale, when acquiring data with less than a second DIT will not give enough time to the
instruction server to request a whole new set of data. With this solution the response time is much faster
v0.5 - Dynamical graphical interface from the json file.
# Stop support for windows GUI
v1.0 - Porting software to command line interface (windows and UNIX) using PYVISA for communication
with USB, GPIB, Serial and Ethernet.
Porting to raspberry Pi 3 with custom python environnement
Changing all time stamps to UTC
- datetime.datetime.now() changed to datetime.datetime.utcnow()
Adding a description for values returned by the drivers
Adding a header in the data log file with the descriptions
Adding STOP/START/CLOSE/COMMENT/HELP functionnalities to IS
- START/STOP: starting and stoping the monitoring of instruments
- CLOSE: closing the communication and save file
- COMMENT: add a comment to the log file with a timestamp
- HELP: print instructions that are supported by the IS
- EXIT: close client connection
- INFOS: start/savepath infos
- STATUS: monitoring status
Changing configuration file format
"""
# PYMONT_Monitoring.py --- Simple application for monitoring Cryostats
#
# Copyright (C) 2018 Benoit Serra
#
# This file is a program monitoring the instruments connected to it.
# It may be used and modified with no restriction; raw copies as well
# as modified versions may be distributed without limitation.
# IMPORTS
#########################
from __future__ import unicode_literals
import sys
import os
import time, datetime
import signal
#import matplotlib
import itertools
# Make sure that we are using QT5
#matplotlib.use('Qt5Agg')
#from PyQt5 import QtCore, QtGui, QtWidgets
#from PyQt5.QtWidgets import QWidget, QTabWidget,QVBoxLayout
#from PyQt5.QtCore import pyqtSlot
import numpy as np
#import pyqtgraph as pg
from collections import defaultdict
import drivers.lakeshore.lakeshore as ls
import drivers.pfeiffer.vacuumgauge as vg
import drivers.roheandschwarze.roheandschwarze as rs
import drivers.rigol.rigol as rig
import drivers.agilent.agilent as agi
import drivers.riello.ups_driver as riello
import drivers.sumitomo.sumitomo as sumitomo
import visa
import socket
import threading
import json
import glob
progname = os.path.basename(sys.argv[0])
progversion = "1.1.0"
# METHODS
#########################
def select_instrument(identifier):
""""""
instruments = {'Lakeshore336':ls.LakeShore33x,
'Lakeshore340':ls.LakeShore34x,
'HMC8012':rs.HMC8012,
'DP831A':rig.DP831A,
'TPG262':vg.TPG262,
'DSO5034':agi.DSO5034A,
'DG1062Z':rig.DG1000Z,
'DM3068':rig.DM3068,
'UPS_VSD3x':riello.UPSVSD3xxx,
'COMP_F70':sumitomo.COMP_F70}
print (identifier)
return instruments[identifier]
def stop_communication(CommServ):
""""""
CommServ.close()
class InstructionServer():
'''demonstration class only
- coded for clarity, not efficiency
'''
def __init__(self, monitoring):#CommServ, MonitServ, ip_address, sock=None):
try:
self.socket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
except:
self.socket = None
if self.socket is not None:
self.socket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
# Bind the socket to the port
self.CS = monitoring.comm
self.MS = monitoring
self.ip_address = monitoring.config_file["GLOBAL_CONFIG"]["host"].split(':')
self.thread = threading.Thread(target=self.inst_server)
self.thread.daemon = True
self.thread.start()
self.helper="""
_____________________________
\x1b[44m \x1b[0m
\x1b[44m Instruction server help \x1b[0m
\x1b[44m_____________________________\x1b[0m
Commands:
- HELP: print this help
- SAVEPATH: change the path where the data is saved to the string after the pipe
- START: start the monitoring, creating a [date]_[time].txt log file with
the UTC time.
- INFOS: print the starting time of the monitoring [UTC] and the save file path
- STOP: stop the monitoring, closing properly the log file
- EXIT: close the current connection to the monitoring.
- CLOSE: close every connection and the resource manager from pyvisa
and then stop the python script properly.
- INST: the INST command allows to access the instruments connected through
the communication server.
Syntax:
-------
INST|LIST: Instruments connected and their associated TAG value
(T001, PSU002,...)
INST|READ: Reading of all connected instrument using the dump_sensors
from each driver
INST|[TAG]|[COMMAND]: Send COMMAND to the instrument TAG, careful with
the string formatting, no protection as for v1.0\n
- COMMENT: the COMMENT command allows to enter comments in the logfile
Syntax:
-------
COMMENT|This is a comment: Will put 'This is a comment' in the
Comments columns of the logfile
- SAMPLING: the SAMPLING command allows to chaning the sampling time between
two measures
-------
SAMPLING|value: Will change current sampling to value in seconds
"""
else:
sys.exit('Instruction server failed to initialize')
def handler(self, client):
"""handler for instruction send by the client to the IS
The method parse the client.recv() string to use it for sending
the correct instruction to the communication server
"""
while True:
self.save_file.write(str(client))
client.settimeout(5)
# To avoid getting a timeout error when working in command line
try:
data = client.recv(4096)
# When nothing is received, ignore timeout and continue reading
except socket.timeout:
continue
if data != '':
# client just typed Enter without anything, do nothing
# int some string with basic commands?
print ('Data received:', data)
client_request = data.decode()
if (client_request == 'INST|READ\r\n') & (self.MS._STARTED != True):
response = self.CS._read_setup()
# print ('> Data send:', json.dumps(response))
if json.dumps(response) == json.dumps({'Error':'Timeout'}):
self.CS._stop()
print ('Received timeout response, stopping CS and IS')
break
self.send_client(client, data, json.dumps(response))
elif client_request == '\r\n':
time.sleep(2)
elif client_request == 'DISCONNECT\r\n':
if self.MS._STARTED:
self.send_client(client, data, 'Monitoring is active, cannot disconnect from instruments')
else:
self.CS.stop()
self.send_client(client, data, 'Disconnecting instruments...')
elif client_request == 'CONNECT\r\n':
if self.MS._STARTED:
self.send_client(client, data, 'Monitoring is active, cannot re-connect to instruments')
else:
self.CS = CommunicationServer(self.MS.config_file, self.MS.df)
self.MS.comm = self.CS
self.send_client(client, data, 'Connecting to instruments...')
# Starting EXP|<SCRIPTNAME>
elif client_request[:4] == 'EXP|':
exp = client_request.split('|')[1]
exp_file = glob.glob('./exps/'+self.MS.config_file['GLOBAL_CONFIG']['bench_name']+'*.json')
link_file = open(exp_file[0],'r')
self.config_file = json.load(link_file)
link_file.close()
instructions = self.config_file[exp.replace('\r\n','')]
for instruction in instructions:
self.send_command(client, instruction)
elif client_request[:9] == 'SAMPLING|':
status = {0:'Inactive',1:'Active'}
monitoring_status = status[self.MS._STARTED]
if monitoring_status == 'Active':
self.send_client(client, data, 'Monitoring is active, change sampling while monitoring is stopped')
else:
sampling_rate = client_request.split('|')[1].strip()
self.MS._TIMER = int(sampling_rate)
self.MS.update_infos('sampling', self.MS._TIMER)
self.send_client(client, data, 'Changing sampling rate to: '+sampling_rate+' seconds')
# Adding comment to logfile, comment is the string after the pipe
elif client_request[:8] == 'COMMENT|':
comment = client_request.split('|')[1]
self.MS._add_comment(comment[:-2])
self.send_client(client, data, 'Adding comment to log file: '+comment)
# Print the status of the monitoring
elif client_request == 'STATUS\r\n':
infos = ''
status = {0:'Inactive',1:'Active'}
connection_status = status[self.CS._connected]
monitoring_status = status[self.MS._STARTED]
if monitoring_status == 'Active':
infos = '\n-'+'\n-'.join([i+'\t:'+str(self.MS.infos[i]) for i in self.MS.infos.keys()])+'\n'
print ('Monitoring status: ',infos)
self.send_client(client, data, 'Connection status: '+status[self.CS._connected]+'\nMonitoring status: '+status[self.MS._STARTED]+infos)
# Print infos about the start time and save path
elif client_request == 'INFOS\r\n':
infos = self.MS.infos
print ('Informations: ',infos)
self.send_client(client, data, json.dumps(infos))
# Print help
elif client_request == 'HELP\r\n':
self.send_client(client, data, self.helper)
# Change savepath to the string after the pipe
elif 'SAVEPATH|' in client_request:
savepath = client_request.split('|')[1].replace('\r\n','')
self.MS.DATAPATH = savepath
self.send_client(client, data, 'Changing saving path to: '+savepath)
# Close monitoring, communication and instruction server
# Closing python script at the same time
elif client_request == 'CLOSE\r\n':
print ('Closing monitoring, instruction server closing',client)
self.MS._close()
self.send_client(client, data, 'Monitoring closed, Instruction server will now close.')
client.close()
self.socket.close()
break
# Start the monitoring, savefile in ./ if no SAVEPATH changes
elif client_request == 'START\r\n':
print ('Starting monitoring ',client)
self.MS.start()
self.send_client(client, data, 'Monitoring started.')
# Stop the monitoring, communication still working
elif client_request == 'STOP\r\n':
print ('Stoping monitoring, instruction server ON',client)
self.MS._stop()
self.send_client(client, data, 'Monitoring stopped, Instruction server awaiting instructions...')
# Client exit from the connection to port 4500
elif client_request == 'EXIT\r\n':
print ('Closing client ',client)
client.close()
break
# List all instruments
elif client_request == 'INST|LIST\r\n':
response = dict()
for inst in self.CS.instruments_tag.keys():
response[inst] = str(type(self.CS.instruments_tag[inst]))
self.send_client(client, data, json.dumps(response))
# Print the last read (if started) or the current value (if stopped)
elif (client_request == 'INST|READ\r\n') & (self.MS._STARTED == True):
self.send_client(client, data, json.dumps(self.CS.last_point))
# Send commands to the instruments (by referring to them with their tags)
else:
if ('INST|' in client_request) & (self.CS._connected):
self.send_command(client, client_request)
else:
print ('Request not valid: ', client_request)
self.send_client(client, data, client_request+': is not a valid instruction. Send HELP to the instruction to see the basic commands')
time.sleep(2)
else:
time.sleep(2)
def send_command(self, client, client_request):
"""send_command will format the command and send it to the client
Send a command to the TAG instrument, saving it to the log file at
the same time with a time stamp"""
command = [i.split('|') for i in client_request.split(';')]
for i in command:
if i[1] not in self.CS.instruments_tag.keys():
self.send_client(client, client_request.encode(), i[1]+': is not a valid instrument')
else:
response = None
print (i[1],i[2])
try:
response = self.CS.instruments_tag[i[1]].instrument.query(i[2].replace('\r\n',''))
except visa.VisaIOError as err_string:
response = 'No response from instrument ['+str(err_string)+']'
self.MS._add_comment('Instruction sent - '+client_request[:-2]+' Response - '+response)
# print ('> Data send:', response.encode())
self.send_client(client, client_request.encode(), json.dumps(response))
return None
def send_client(self, client, str_recv, str_send):
"""Send the response to the client, printing it at the same time"""
self.save_file.write(str_recv.decode()+'\n>'+str_send+'\n')
self.save_file.flush()
client.send((str_send+'\r\n').encode())
return None
def inst_server(self):
"""Initialization of the instruction server"""
server_address = (self.ip_address[0], int(self.ip_address[1]))
start_time = datetime.datetime.utcnow().strftime(self.CS.df)
# Open the save file for every interaction with the IS
self.save_file = open('logs/'+start_time+'.txt','w')
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(server_address)
print ('%s:%s listening for client connection' % server_address)
self.socket.listen(5)
self._connected = True
while True:
# Wait for a connection
client, client_adress = self.socket.accept()
self.save_file.write('Connexion on '+'/'+str(client)+'/'+str(client_adress))
# Starting the handler of this client on another thread
client_handler = threading.Thread(target=self.handler,args=(client,))
client_handler.start()
return None
def connect(self, host, port):
""""""
self.socket.connect((host, port))
def _close(self):
"""Close the instruction server"""
self.socket.close()
self.CS._connected = False
class CommunicationServer(object):
"""Class for starting/stopping communication with the instruments
Maybe add a separate communication log?
"""
def __init__(self, config_file, time_fmt):
"""Initialization of the communication server"""
# Time between measures
self.dt = config_file["GLOBAL_CONFIG"]["sampling"]
# Extract the instrument configuration from the config file
dict_config = config_file["Instruments"]
self.filename = time.ctime()
# Starting pyvisa ResourceManager
self.inst_rm = visa.ResourceManager()
self.instruments = defaultdict(list)
self.instruments_tag = dict()
# Config file timeout in seconds, in pyvisa it is in ms
timeout = int(config_file["GLOBAL_CONFIG"]["timeout"])*1000
# For each type of instrument defined in the json file
for instrument_type in dict_config:
# Get its tag
instrument_tag = dict_config[instrument_type]
# For each instrument of this type, connect to it
for idx,protocol in enumerate(instrument_tag['comm']):
instrument = select_instrument(instrument_tag['ident'][idx])
config = instrument_tag['config'][idx]
models = instrument_tag['models']
# gives ID_STR everytime: T001, T002 etc...
id_str = instrument_type+str('{:03d}'.format(idx+1))
conn = instrument(RESOURCE_STRING = protocol,
RESOURCE_MANAGER = self.inst_rm,
RESOURCE_ID = id_str,
RESOURCE_TIMEOUT = timeout,
RESOURCE_CONFIG = config,
RESOURCE_MODEL = models)
if conn.connected == True:
print (id_str, conn, ' Connected')
self.instruments_tag[id_str] = conn
if dict_config[instrument_type]['type'] in self.instruments.keys():
self.instruments[dict_config[instrument_type]['type']].append(self.instruments_tag[id_str])
else:
self.instruments[dict_config[instrument_type]['type']] = [self.instruments_tag[id_str]]
else:
print ('Instrument '+str(protocol)+ ' Failed to connect.')
print (self.instruments_tag)
self.df = time_fmt
self._connected = True
print ('Connexion initialization done.')
def _read_UPSstat(self):
""""""
# battery_status = {**self.ups.dump_sensors()}
return None
def _read_setup(self):
"""Loop over the connected instrument and return the data:
Input: None
Output: dictionary with <tag>_<variable>:(<value>, <comment>)
<tag>: created at the connection, identify the instrument (T001 for ex)
<variable>: variable name as defined in the driver
<value>: value formatted as defined in the driver
<comment>: string to comment what is send as defined in the driver
"""
results = dict()
time = datetime.datetime.utcnow().strftime(self.df)
results.update(Time=(time,'[str] Date with UTC time'))
results.update(Comments=["","[str] Commments on the measure"])
for key in self.instruments_tag.keys():
try:
results.update(self.instruments_tag[key].dump_sensors())
except Exception as e:
# If an intrument takes to much time to respond, timeout
# Send a dictionnary with the error the tag
results['Comments'][0]+='Error instrument - '+str(key)+\
' '+str(self.instruments_tag[key])+\
' '+str(e)
return results
def _save(self):
""""""
self.save_file.flush()
os.fsync(self.save_file)
def start(self, path=''):
try:
start_time = datetime.datetime.utcnow().strftime(self.df)
self.save_file = open(path+\
start_time+'.txt','w')
self.header = self._read_setup().keys()
self.save_file.write('Reading #\t'+\
'\t'.join(self.header)+'\tComments\n')
except:
self.cleanup()
def stop(self):
""""""
self._connected = False
for inst_tag in self.instruments_tag.keys():
print ('Closing connection to ', inst_tag, self.instruments_tag[inst_tag])
self.instruments_tag[inst_tag].instrument.close()
self.inst_rm.close()
def dict_plot(input_name):
'''Method to associate input name with a color'''
colors_dict = dict(H1=(230,25,75),
H2=(0,130,200),
InputA=(60,180,75),
InputB=(255,225,25),
InputC=(245,130,48),
InputD=(128,0,0),
InputD2=(145,30,180),
InputD3=(70,240,240),
InputD4=(240,50,230),
InputD5=(210,245,60),
Gauge1=(230,25,75),
Gauge2=(0,130,200),
SETP1=(0,128,128),
SETP2=(170,110,40))
# Return the full label, but for the color split it with the spaces and
# take only last arg
if 'T001' in input_name:
style = QtCore.Qt.SolidLine
else:
style = QtCore.Qt.DashLine
color = [colors_dict[i]\
for i in colors_dict.keys()\
if input_name.split('_')[-1].replace(' ','') in i]
# Making the pyqtgraph pen
pen = pg.mkPen(color=color[0], width=1.5, style=style)
return dict(pen=pen, name=input_name)
#class TimeAxisItem(pg.AxisItem):
# """Class used for formating x axis labels to dates"""
# def tickStrings(self, values, scale, spacing):
# return [datetime.datetime.fromtimestamp(value).replace(microsecond=0)\
# for value in values]
#
class monitoring(object):
"""Monitoring class that will launch the communication server
and can loop the _read_setup function of the CS to log the data
into a file
"""
def __init__ (self, time_fmt, config_file):
"""Initialization of the monitoring
define the time format, the sampling of the data
"""
self.df = time_fmt
# Open the config file and load it into a dictionary
link_file = open('config/'+config_file,'r')
print (config_file)
self.config_file = json.load(link_file)
link_file.close()
self._TIMER = int(self.config_file["GLOBAL_CONFIG"]["sampling"])
# Start the communication server
self.connect(self.df)
self._STARTED = False
self._STATUS = 'ACTIVE'
# Start the Instruction Server
self.InstructionServ = InstructionServer(self)
# Extract the save path from the config file
self.DATAPATH = self.config_file["GLOBAL_CONFIG"]["savepath"]
# Connecting the SIGINT (ctrl+c) to the close method, so that
# closing the program close the connection properly.
# If it is not closing properly, some instruments might not
# want another connection straight after closing!
# (Yes, I am talking about you TPG262 controller).
signal.signal(signal.SIGINT, self._close)
self.infos = {'start':['Monitoring not started no timestamp available','[str] Starting date YYYY-MM-DD_hh-mm-ss'],
'spath':[self.DATAPATH,'[str] Path for save files'],
'sfile':['Monitoring not started no save file created','[str] Save file full name'],
'sampling':[self._TIMER,'[int] Sampling time in seconds']}
def update_infos(self, keyword, updated_value):
""""""
current_value = self.infos[keyword]
current_value[0] = updated_value
self.infos[keyword] = current_value
def update(self):
"""Update the data in the save file"""
while self._STARTED == True:
# Saving state ON, no closing allowed while reading
self._SAVING = True
# Reading index
self.datacount += 1
# Read the setup instruments
read = self.comm._read_setup()
# Request the current date [UTC]
date_utc = datetime.datetime.utcnow()
# Format it
self.datatime = date_utc.strftime(self.comm.df)
# If the day is not the same as the previous point, create another file
if date_utc.day != self.save_startdate.day:
self.save_file.close()
self.header = read
self._create_savefile(path = self.DATAPATH)
self.save_startdate = date_utc
# If the keys of the read differ from the keys in the header, create another file
if read.keys() != self.header.keys():
self.save_file.close()
self.header = read
self._create_savefile(path = self.DATAPATH)
# If a timeout occur, pass
# I should crash here right?
if 'Error' in read.keys():
self.datapoint = str(self.datacount)+'\t'+\
'\t'.join([val.replace('\n','') for val in read.values()])+'\t'+'\n'
self.comm.last_point = read
self.save_file.write(self.datapoint)
# Every fifth read, flush the data in the file.
# That way you don't miss more than 5*sampling worth of data if crash
if self.datacount%1 == 0:
self._save()
# Create the datapoint and write it to the save
else:
# self.dates = np.append(self.dates, datetime.datetime.utcnow().timestamp())
self.datapoint = str(self.datacount)+'\t'+\
'\t'.join([read[keyw][0].replace('\n','') for keyw in read.keys() if keyw != 'Comments'])+\
'\t'+read['Comments'][0].replace('\n','')+'\n'
self.comm.last_point = read
self.save_file.write(self.datapoint)
# Every fifth read, flush the data in the file.
# That way you don't miss more than 5*sampling worth of data if crash
if self.datacount%1 == 0:
self._save()
# Saving state OFF
self._SAVING = False
# Wait for _TIMER seconds
time.sleep(self._TIMER)
def connect(self, time_fmt):
# Connect to the instruments
self.comm = CommunicationServer(self.config_file, time_fmt)
def start(self, path = ''):
""""""
# Get the header which will give the outputs to read
try:
print (path)
self._create_savefile(path = self.DATAPATH)
except:
self._stop()
self.comm.stop()
sys.exit()
# self.timer.start(self._TIMER)
self._STARTED = True
self.datacount = 0
self.update_infos('start', datetime.datetime.utcnow().strftime(self.comm.df))
self.update_infos('spath', self.DATAPATH)
self.update_infos('sfile', self.save_file.name.replace(self.DATAPATH,''))
self.update_infos('sampling', self._TIMER)
# self.infos = {'start':(datetime.datetime.utcnow().strftime(self.comm.df),'[str] Starting date DD-MM-YYYY_hh-mm-ss'),
# 'spath':(self.DATAPATH,'[str] Path for save files'),
# 'sfile':(self.save_file.name.replace(self.DATAPATH,''),'[str] Save file full name'),
# 'sampling':(self._TIMER,'[int] Sampling time in seconds')}
thread1 = threading.Thread(target=self.update)
thread1.start()
def _create_savefile(self, path = ''):
print (path)
self.save_startdate = datetime.datetime.utcnow()
start_time = self.save_startdate.strftime(self.comm.df)
self.header = self.comm._read_setup()
# Need to move 'Comments' keyword at the end of the data saved
print (self.header)
self.save_file = open(path+\
start_time+'.txt','w')
self.save_file.write('# ___________________________________________\n')
for i in self.header.keys():
print (i, self.header[i])
self.save_file.write('# '+i+'\t'+self.header[i][1]+'\n')
self.save_file.write('# ___________________________________________\n')
self.save_file.write(('Reading\t'+\
'\t'.join(self.header)).replace('Comments\t','')+'\tComments\n')
def _add_comment(self, string):
if self._STARTED == True:
string += '\r\n'
timestamp = datetime.datetime.utcnow().strftime(self.comm.df)
self.save_file.write('\t'+timestamp+'\t'*len(self.header.keys())-1+string)
else:
return None
def _save(self):
""""""
self.save_file.flush()
os.fsync(self.save_file)
def _stop(self):
""""""
self._STARTED = False
while self._SAVING == True:
# If monitoring is still saving, wait 1 second to check again
# TIMEOUT to define?
time.sleep(1)
try:
self.save_file.close()
print (self.save_file, ' Closed')
self._SAVING = 'DONE'
except:
print ('No file to close')
pass
def _close(self, signal=None, frame=None):
""""""
try:
self.save_file.close()
except:
print ('No file to close')
pass
self._STARTED = False
# Closing all conections to instruments
print ('Closing connections ...')
self.comm.stop()
self._STATUS = 'INACTIVE'
def cleanup(self):
""""""
self._stop()
sys.exit(1)
#
#
#
#class MonitoringWidget(pg.GraphicsWindow):
#
# def __init__(self, time_fmt, parent=None, **kargs):
# self.df = time_fmt
# self.connect()
#
# self.header = self.comm._read_setup()
# # Configure the layout of the plot
# pg.setConfigOption('background', 'w')
# pg.setConfigOption('foreground', 'k')
# pg.GraphicsWindow.__init__(self, **kargs)
# self.setParent(parent)
# self.setWindowTitle('pyqtgraph example: Scrolling Plots')
# # Change x_axis label format to dates
# date_axis = TimeAxisItem(orientation='bottom')
# date_axis2 = TimeAxisItem(orientation='bottom')
# # Add a plot
# self.p1 = self.addPlot(0,0,\
# labels = {'left':'Temperature [K]',\
# 'bottom':'Date'},\
# axisItems = {'bottom': date_axis})
# self.p1.setContentsMargins(170,5,150,5)
# self.p2 = self.addPlot(1,0,\
# labels = {'left':'Pressure [mbar]',\
# 'bottom':'Date'},\
# axisItems = {'bottom': date_axis2})
# self.p2.setContentsMargins(150,5,150,5)
# self.p2.setLogMode(y=True)
#
# [p.showGrid(x=True) for p in [self.p1, self.p2]]
# ## create a new ViewBox, link the right axis to its coordinate system
# self.p3 = pg.ViewBox()
# self.p1.showAxis('right')
# self.p1.scene().addItem(self.p3)
# self.p1.getAxis('right').linkToView(self.p3)
# self.p3.setXLink(self.p1)
# self.p1.getAxis('right').setLabel('Heaters %', color='#0000ff')
#
# # Initialization of the data
# self.Xm = np.array([0])
# self.dates = np.array([datetime.datetime.utcnow().timestamp()])
#
# self.monitor_curves = {}
# self.monitor_data = {}
#
# legend_heater = pg.LegendItem()
# legend_temp = pg.LegendItem()
# legend_vacuum = pg.LegendItem()
#
# legends = [legend_heater,
# legend_temp,
# legend_vacuum]
# [legend.setParentItem(self.p1.graphicsItem()) for legend in legends]
# legends[2].setParentItem(self.p2.graphicsItem())
#
# legend_temp.anchor((0,0), (0,0))
# legend_heater.anchor((1,0), (1,0))
# legend_vacuum.anchor((0,0), (0,0))
#
# for keyw in self.header.keys():
# if ('T00' in keyw[:3]) or ('P00' in keyw[:3]):
#
# self.monitor_data[keyw] = np.array([float(self.header[keyw])])
# if 'Gauge' in keyw:
# self.monitor_curves[keyw] = self.p2.plot(self.dates,\
# self.monitor_data[keyw],\
# **dict_plot(keyw))
# legends[2].addItem(self.monitor_curves[keyw],keyw)
#
# elif ('Input' in keyw):
# self.monitor_curves[keyw] = self.p1.plot(self.dates,\
# self.monitor_data[keyw],\
# **dict_plot(keyw))
# legends[1].addItem(self.monitor_curves[keyw],keyw)
#
# elif ('SETP' in keyw):
# self.monitor_curves[keyw] = self.p1.plot(self.dates,\
# self.monitor_data[keyw],\
# **dict_plot(keyw))
# legends[0].addItem(self.monitor_curves[keyw],keyw)
#
# else:
# self.monitor_curves[keyw] = pg.PlotDataItem(self.dates,\
# self.monitor_data[keyw],\
# **dict_plot(keyw))
# self.p3.addItem(self.monitor_curves[keyw])
# legends[0].addItem(self.monitor_curves[keyw],keyw)
#
# # CHANGE THE FONT SIZE AND COLOR OF ALL LEGENDS LABEL
# legendLabelStyle = {'color': '#000',\
# 'size': '6pt',\
# 'bold': True,\
# 'italic': False}
#
# for legend in legends:
# for item in legend.items:
# for single_item in item:
# if isinstance(single_item,\
# pg.graphicsItems.LabelItem.LabelItem):
# single_item.setText(single_item.text,\
# **legendLabelStyle)
#
# self.datacount = 0
# self.ptr = 0
# self._STARTED = False
# self.timer = QtCore.QTimer(self)
# self.timer.timeout.connect(self.update)
#
# def update(self):
# self.datacount += 1
# self.datatime = datetime.datetime.utcnow().strftime(self.comm.df)
## time_ref = time.time()
# read = self.comm._read_setup()
## time_read = time.time()
# if read.keys() != self.header.keys():
# self.save_file.close()
# self.header = read
# self._create_savefile(path = self.DATAPATH)
#
# if read == {'Error':'Timeout'}:
# pass
# else:
# self.dates = np.append(self.dates, datetime.datetime.utcnow().timestamp())
# for keyw in self.header:
# if ('MM' not in keyw) and ('PSU' not in keyw) and ('OS' not in keyw) and ('Time' not in keyw):
# self.monitor_data[keyw] = np.append(self.monitor_data[keyw],\
# float(read[keyw]))
# self.monitor_curves[keyw].setData(self.dates,\
# self.monitor_data[keyw])
# self.p3.setGeometry(self.p1.vb.sceneBoundingRect())
# # self.p3.linkedViewChanged(self.p1.vb, self.p3.XAxis)
# QtGui.QApplication.processEvents() # you MUST process the plot now
# self.datapoint = str(self.datacount)+'\t'+\
# '\t'.join([val.replace('\n','') for val in read.values()])+'\t'+'\n'
#
# self.comm.last_point = read
# self.save_file.write(self.datapoint)
# if self.datacount%10 == 0:
# self._save()
#
# def connect(self):
# # Connect to the instruments
# self.comm = CommunicationServer(self.df)
#
# def start(self, path = ''):
# """"""
# # Get the header which will give the outputs to read
# try:
# self._create_savefile(path)
# except:
# self._stop()
# sys.exit()
#
# self.timer.start(self._TIMER)
#
# self._STARTED = True
#
# def _create_savefile(self, path = ''):
# self.DATAPATH = path
# start_time = datetime.datetime.utcnow().strftime(self.comm.df)
#
# self.save_file = open(path+\
# start_time+'.txt','w')
# self.save_file.write('Reading #\t'+\
# '\t'.join(self.header)+'\tComments\n')
#
#
# def _save(self):
# """"""