class AutoIncrement(object):
    def __init__(self,plugin,msg,field):
        self.m,self.f,self.obj_plugin = msg,field,plugin
        self.enabled = True
        self.handled = self.obj_plugin.setMessageFieldReserved(self.m,self.f,self.enabled)
        if self.handled is False:
            #raise ValueError("Message or Field not found")
            print("AutoIncrement:: Warning:Message or Field not found")
        else:
            self.range_value = self.obj_plugin.getMessageFieldRange(self.m,self.f)
            period = self.obj_plugin.getMessageUsPeriodTime(self.m)
            self.counter = 0 if period is None else int(period / plugin.us_busclock)
            self.cur_counter = self.counter
            self.val = self.obj_plugin.getMessageFieldValue(self.m,self.f)

    def onClock(self):
        if self.enabled is False or self.handled is False: return False
        if self.cur_counter == 0:
            self.cur_counter = self.counter
            self.val = self.obj_plugin.getMessageFieldValue(self.m,self.f)
            self.val = self.range_value[0] if self.val == self.range_value[1] else self.val + 1
            self.obj_plugin.assignMessageFieldValue(self.m,self.f,self.val)
            self.obj_plugin.commitSingleMessageField(self.m,self.f)
            return True
        else:
            self.cur_counter = self.cur_counter -1
            return False

    def setEnable(self,flag):
        self.enabled = flag
        self.obj_plugin.setMessageFieldReserved(self.m,self.f,self.enabled)
