Newer
Older
TestStandRepository / Software / Monitoring / Monitoring.py
"""
Author: Federica Lionetto
Date: March 30th, 2015

Description:
Monitoring of the environmental variables.

Parameters:
- <sensor>, the type of sensor;
- <date>, the date according to the yyyymmdd format.

How to run it:
python Monitoring --s <sensor> --d <date>

For example:
python Monitoring --s Hans320 --d 20150330
"""

import argparse
import os
import serial
import sys
import time

parser = argparse.ArgumentParser(description='Define the parameters of the monitoring of the environmental variables.')

parser.add_argument('--s',help='type of sensor',required=True)
parser.add_argument('--d',type=int,help='date according to the yyyymmdd format',required=True)

args = parser.parse_args()

# Configuration.

# Type of sensor.
sensor = args.s
# Date according to the yyyymmdd format.
yyyymmdd = args.d

# Header to be written in the output text file.
header = 'Date&Time T (C) RH (%) DP (C)\n'
# Maximum number of output text files.
nFiles = 0
maxNFiles = 2
# Maximum size of the output text file (number of rows). Once this is reached, a new output text file will be created.
nRows = 0
maxNRows = 10

# Sleep time between two consecutive measurements (s).
wait = 5

# Variables to be monitored.
T = -1.
RH = -1.
DP = -1.

# Connection to Sensirion sensor.
# It should be ttyS4 or ttyS5.
ser = serial.Serial()
ser.port = '/dev/ttyS5'
ser.baudrate = 9600
ser.bytesize = serial.EIGHTBITS
ser.parity = serial.PARITY_NONE
ser.stopbits = serial.STOPBITS_ONE
ser.timeout = 1

ser.open()
print "================================================================================================"
print "Serial port connected to Sensirion sensor: "
print ser.portstr
print "================================================================================================"

if (ser.isOpen()) :
    print "================================================================================================"
    print "Serial port listening."
    print "================================================================================================"

    # ser.write('GO\r')
    # command = ser.read(2)
    # print "Sending the command...", command

    '''
    trash = ser.read(3)
    H = ser.read(5)
    percent = ser.read(1) # Read the % symbol.
    trash = ser.read(4)
    T = ser.read(5)
    degrees = ser.read(1)
    print H
    print percent
    print T
    print degrees
    print "Relative humidity...", H, percent
    print "Temperature...", T, degrees
    '''

    # Loop until the user presses Ctrl+C...
    try :
        while True :
            # Sleep between two consecutive measurements.
            time.sleep(wait)
            # Read current time.
            currentTime = time.ctime()
            # Reset.
            T = -1.
            RH = -1.
            DP = -1.
            # Measure.
            ser.write('GET\r')
            command = ser.read(4)
            # print "Sending the command...", command
            trash1 = ser.read(2)
            # Relative humidity (RH).
            RH = ser.read(5)
            percent = ser.read(1) # Read the % symbol.
            trash2 = ser.read(4)
            # Temperature (T).
            T = ser.read(5)
            degrees = ser.read(1)
            trash3 = ser.read(4)
            # Dew point (DP).
            DP = -1
            # Write row in the output text file.
            if (nRows%maxNRows==0) :
                # Close previous output text file, if exists.
                if (nFiles > 0) :
                    file.close()
                if (nFiles == maxNFiles) :
                    print "================================================================================================"
                    print "Maximum number of output text files reached."
                    print "Terminating monitoring."
                    print "================================================================================================"
                    sys.exit(0)
                # Create and open output text file.
                # Filename (complete path or not) given through a parser.
                path = "/disk/groups/hep/flionett/TestStand/Data/"+sensor+"/Monitoring"
                if not os.path.exists(path) :
                    os.makedirs(path)
                filename = path+"/Monitoring-"+str(yyyymmdd)+"-"+str(int(nRows/maxNRows))+".dat"
                print "================================================================================================"
                print "Opening new output text file: "
                print filename
                print "================================================================================================"

                file = open(filename,"w")
                file.write(header)
                nFiles = nFiles+1

            '''
            print RH
            print percent
            print T
            print degrees
            print "Relative humidity...", RH, percent
            print "Temperature...", T, degrees
            print "Dew point...", DP, degrees
            '''

            row = currentTime + " " + str(T) + " " + str(RH) + " " + str(DP) + "\n"
            print row
            file.write(row)
            nRows = nRows+1
    except KeyboardInterrupt :
        # ser.write('S')
        # command = ser.read(1)
        # print "Sending the command...", command
        sys.exit(0)

    # Close output text file.
    file.close()
    ser.close()