from xml.etree import cElementTree as eTree

from msgbus import (
    MBEasyContext,
    TK_POS_GETMODEL,
    FM_PARAM,
    TK_SYS_ACK,
)


class PosModelRetriever(object):
    def __init__(
        self,
        mb_context,  # type: MBEasyContext
    ):
        self.mb_context = mb_context

    def get_model(
        self,
        pos_id,  # type: int
    ):
        reply = self.mb_context.MB_EasySendMessage(
            dest_name="POS{}".format(pos_id),
            token=TK_POS_GETMODEL,
            format=FM_PARAM,
            data="{}".format(pos_id),
        )
        if reply.token != TK_SYS_ACK:
            raise Exception("Could not retrieve state of pos '{}'".format(pos_id))
        return eTree.XML(reply.data)
