import json import os.path as op from subprocess import call from time import strftime, sleep basepath = '/dev/shm/golem/0' def write2file(fname, value): with open(fname, 'w') as f: f.write(str(value)) def catfile(fname_rel2basepath, default=None): fname = op.join(basepath, fname_rel2basepath) try: with open(fname) as f: content = f.read() except IOError: return default try: return int(content) except ValueError: try: return float(content) except ValueError: return content.strip() def main(): while op.exists(basepath): call('scp golem@vacuum:/dev/shm/pressure_chamber_mPa /dev/shm') current_data = dict( time=strftime("%H:%M:%S"), U_Bt=catfile('UBt')), U_CD=catfile('UCD')), p_ch=catfile('pressure_chamber_mPa'), tokamak_state=catfile('tokamak_state', default='idle'), ) if current_data['tokamak_state'] != 'idle': current_data['discharge_params'] = dict( UBt=catfile('target_UBt'), Ucd=catfile('target_UCD'), pressure=catfile('target_pressure'), comment=catfile('precomment'), gas=catfile('working_gas'), preionization=catfile('preion'), shot_no=catfile('ShotNo'), ) json_str = json.dumps(current_data) write2file(op.join(basepath, 'golem_status.json'), json_str) sleep(1)