Revision 2ea7698ff7e59d1867a9ea1375c95e75e00ae1a6

HW/Radiation/Monitoring/Fluke451P/SWdevelop/OG/main.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
#!/usr/bin/env/ python

import socket
from subprocess import call
from time import sleep, time
from contextlib import contextmanager
import sys


# PARAMETERS
gnome232_address = ("192.168.2.249", 10001)
units = {
        'm': 1e-3,
        'MICRO': 1e-6,
        }


# COMMUNICATION CLASS

class Fluke451PSocket(object):

    def __init__(self, address, timeout=1):
        self.socket = socket.create_connection(gnome232_address)
        self.socket.settimeout(timeout)
        self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

    def __del__(self):
        self.socket.shutdown(socket.SHUT_RDWR)
        self.socket.close()

    @contextmanager
    def different_timeout(self, timeout):
        old_timeout = self.socket.gettimeout()
        self.socket.settimeout(timeout)
        yield
        self.socket.settimeout(old_timeout)


    def get(self, termination_str=None, buflen=1024):
        message = ''
        while True:
            try:
                packet = self.socket.recv(buflen)
            except socket.timeout:
                break
            message += packet
            if termination_str and termination_str in message:
                break
        return message

    def put(self, command):
        self.socket.sendall(command)

    def activate_measurement(self):
        self.put('T')

    def terminate_measurement(self, plus_chars=5, repeat=1):
        with self.different_timeout(0.05):
            while True:
                self.put(' ')
                packet = self.get(buflen=1024)
                if '?' in  packet:
                    break

    def get_integrated(self):
        self.put('F')
        # not interested if ends with Sv/h
        with self.different_timeout(5):
            message = self.get()
        line = ''
        for line in message.splitlines():
            if 'integrated' in line:
                break
        try:
            value, unit_mod, unit, status = line.split()
            return float(value) * units[unit_mod]
        except (TypeError, ValueError, KeyError):
            raise

    def save_integrated(self, fname):
        self.terminate_measurement()
        value = self.get_integrated()
        self.activate_measurement()
        save_const(fname, value)
        return value



# UTILITY FUNCTIONS
def make_icon(text):
    return call(['convert', '-size', '150x120', '-gravity', 'center',
                'label:' + text, 'icon.png'])

def save_const(fname, value):
    with open(fname, 'w') as fobj:
        fobj.write(str(value))

def load_const(fname):
    with open(fname, 'r') as fobj:
        return float(fobj.read())



if __name__ == '__main__':
    try:
        action = sys.argv[1]
    except IndexError:
        action = ''
    fluke = Fluke451PSocket(gnome232_address)
    if action == 'arming':
        #value = fluke.save_integrated('pre_Sv')
        pass
    elif action == 'acquisition':
        value = fluke.save_integrated('dose_since_last_measurement_Sv')
        try:
            old_value = load_const('/tmp/cumulative_dose_Sv')
        except IOError:
            old_value = 0
        value += old_value
        save_const('/tmp/cumulative_dose_Sv', value)
        make_icon('%.2g Sv' % value)
        #value = fluke.save_integrated('post_Sv')
        #value_pre = load_const('pre_Sv')
        #diff = value - value_pre
        #save_const('diff_Sv', diff)
        #make_icon('%.2g Sv' % diff)