#!/APSshare/epd/rh6-x86_64/bin/python

import time
import epics

opened_at = {}
integrated_time = {}

def onChanges(pvname, value=None, char_value=None, **kws):
    if value == 0: # shutter closed
        print ' %s closed ' % pvname
        if opened_at[pvname] > 0:
            integrated_time[pvname] += time.time() - opened_at[pvname]
    else:  # shutter opened
        opened_at[pvname] = time.time()
        print ' %s opened ' % pvname
        
pvs = []
for i in range(16):
    pvname = "id%2.2i:shutter" % (i+1)
    opened_at[pvname]     = -1
    integrated_time[pvname] = 0.0
    pvs.append( epics.PV(pvname,  callback=onChanges) )


start_time = time.time()
while (time.time() - start_time ) < 60:
    time.sleep(0.001)

print 'Done collecting data!'

for i in range(16):
    pvname = "id%2.2i:shutter" % (i+1)
    print "%s: %.3f sec" % (pvname, integrated_time[pvname])

