from datetime import datetime


st1 = Stream("st1", dispatcher)

#Adding channels to that stream
s1=st1.addScalar("Int8Scalar", "Int8Scalar", 1, 0)
#s2=st1.addScalar("Float64Scalar", "Float64Scalar", 1, 0)
#w1=st1.addWaveform("Int32Waveform", "Int32Waveform", 1, 0)
#mt1=st1.addMatrix("Int16Waveform", "Int16Waveform", 10, 0, 64, 32)
st1.initialize()
st1.start()



def after(rec, scan):    
    path = get_exec_pars().group+"/TimeIOC"
    pathpc = get_exec_pars().group+"/TimePC"
    if rec.index==0:
        create_dataset(path, 's')
        create_dataset(pathpc, 's')
    nanos = st1.timestampNanos
    dt = datetime.fromtimestamp(nanos/1e9)
    time_str = str(dt).ljust(26,'0') + str(int(nanos % 1000)).zfill(3)
    append_dataset(path, time_str , type = 's')

    append_dataset(pathpc, str(datetime.fromtimestamp(time.time())) , type = 's')    

#bscan (st1, 10)    #Sampling 10 elements

#tscan (st1.getReadables(), 10 , 0.2)



mscan (st1, st1.getReadables(), -1, 3.0, after_read = after)

st1.close()