import lcm

from test.TestUtils import *


def test_publish_lcm(lc=lcm.LCM()):

    msg_pub_rate_sec = 1.0

    pub = LcmPublisher(lc)
    fluo_data = read_file(fluo_file)
    ubat_data = read_file(test_file)
    fluo_data.pop(0)  # drop first line
    ubat_data.pop(0)  # drop first line

    channel_name = 'WetLabsBB2FL'
    var_name = 'mass_concentration_of_chlorophyll_in_sea_water'

    print("\n")
    print("publishing Flou msgs at {0} Hz:".format(1 / msg_pub_rate_sec))
    for i, l in enumerate(fluo_data):
        try:
            t = time.time()
            print("publishing fluo msg # {}".format(pub.seq_number))

            fluo = float(fluo_data[i].strip().split(',')[-1])

            pub.clear_msg()
            pub.add_variable(var_name, fluo, 'ug/l')
            pub.timestamp()
            pub.publish(channel_name)
            time.sleep(max(0.0, t + msg_pub_rate_sec - time.time()))
        except IndexError:
            print("Reached EOF.")
        except KeyboardInterrupt:
            print("Aborting.")
            break


if __name__ == "__main__":

    test_publish_lcm()
