import logging; logger = logging.getLogger("morse." + __name__)
import pymoos.MOOSCommClient
from morse.middleware.moos import AbstractMOOS

class ModemActuatorReader(AbstractMOOS):
    def initialize(self):
        AbstractMOOS.initialize(self)

        # register for control variables from the database
        self.m.Register("BOX_SELF")
        self.m.Register("BOAT_X")
        self.m.Register("BOAT_Y")
        self.m.Register("BOAT_HEADING")
        self.m.Register("BOAT_HEADING_DOT")
        self.m.Register("BOAT_SPEED")
        self.m.Register("NAME")
        self.m.Register("PRESSURE_Z")

    def default(self, ci='unused'):
        current_time = pymoos.MOOSCommClient.MOOSTime()
        # get latest mail from the MOOS comm client
        messages = self.getRecentMail() #self.m.FetchRecentMail()

        # look for command messages: MODEM_MSG
        for message in messages:
            if (message.GetKey() == "BOX_SELF") and (message.IsString()):
                self.data['BOX_BUDDY'] = message.GetString()
            if (message.GetKey() == "BOAT_X") and (message.IsDouble()):
                #logger.info("boat receiving %d BOAT_X" % message.GetDouble())
                self.data['BOAT_X'] = message.GetDouble()
            if (message.GetKey() == "BOAT_Y") and (message.IsDouble()):
                self.data['BOAT_Y'] = message.GetDouble()
            if (message.GetKey() == "BOAT_HEADING") and (message.IsDouble()):
                self.data['BOAT_HEADING'] = message.GetDouble()
            if (message.GetKey() == "BOAT_HEADING_DOT") and (message.IsDouble()):
                self.data['BOAT_HEADING_DOT'] = message.GetDouble()
            if (message.GetKey() == "BOAT_SPEED") and (message.IsDouble()):
                self.data['BOAT_SPEED'] = message.GetDouble()
            if (message.GetKey() == "NAME") and (message.IsString()):
                self.data['NAME'] = message.GetString()
            if (message.GetKey() == "PRESSURE_Z") and (message.IsString()):
                self.data['PRESSURE_Z'] = message.GetString()