import time

from .lcm_patterns import Subscriber
from .channels import app_control_t, environmental_t


def msg_handler(channel_name, data):   # message callback 
    if channel_name == "ENVIRONMENTAL":
        msg = environmental_t.decode(data)
        print("   t = {0} , T = {1}, P = {2}".format(msg.timestamp, msg.temp, msg.pressure))
    elif channel_name == "APP_CONTROL":
        msg = app_control_t.decode(data)
        print("   run flag = ",msg.run_flag)
    else:
        print("unknown msg")

channels = ["ENVIRONMENTAL", "APP_CONTROL"]
with Subscriber(channels, msg_handler) as lcm:

    for n in range(200):
        rc = lcm.get()     # non-blocking read.  False = no messages
        time.sleep(0.1)    # 

print("done!")
