import time class BLOCK_FUNCTION(object): #this is the shell name = "Generic" def __init__(self): pass def __name__(self): return self.name def on(self, *args): return True def off(self, *args): return True def properties(self): return self.__doc__ class func_countup(BLOCK_FUNCTION): '''updates a variable with the current elapsed time in 1/10ths of a second. var1 is an int, var2 is a boolean which serves as a reset line ''' name = "CountUp" def __init__(self): self.settime = None self.delta = 0 def on(self, *args): if args: if len(args)>1: if args[1]: self.reset() t = 0 if self.settime is None: self.settime = time.time() else: t = self.calc() t += self.delta args[0].set(t) def off(self, *args): if self.settime is not None: t = self.calc() + self.delta self.delta = t self.settime = None args[0].set(t) elif args: t = self.delta if len(args)>1: if args[1]: self.settime = None self.delta = 0 t = 0 args[0].set(t) def reset(self): self.settime = time.time() self.delta = 0 def calc(self): if self.settime is not None: return int((time.time()-self.settime)*10) else: return 0 class func_countdown(BLOCK_FUNCTION): '''when activated, counts down till it gets to the set level. var1 is the time to count to, var 2 is reset, and var 3 is output ''' name = "CountDown" def __init__(self): self.settime = None self.delta = 0 def on(self, *args): if args: if len(args)>1: if args[1]: self.reset() t = 0 if self.settime is None: self.settime = time.time() else: t = self.calc() t += self.delta if len(args)>2: if t > args[0]: args[2].set(True) else: args[2].set(False) def off(self, *args): if self.settime is not None: t = self.calc() + self.delta self.delta = t self.settime = None if len(args)>1: if args[1]: self.settime = None self.delta = 0 if len(args)>2: t = self.delta if t > args[0]: args[2].set(True) else: args[2].set(False) def reset(self): self.settime = time.time() self.delta = 0 def calc(self): if self.settime is not None: return int((time.time()-self.settime)*10) else: return 0 def properties(self): s = self.__doc__ s += "\n\n" t = self.calc() + self.delta s += "current count value:" + str(t) return s class func_and(BLOCK_FUNCTION): '''True if every parameter is true -- contacts in series''' name = "And" def on(self,*args): if all(args): return True else: return False class func_or(BLOCK_FUNCTION): '''true if any of the parameters are true -- contacts in parallell''' name = "Or" def on(self,*args): if any(args): return True else: return False class func_not(BLOCK_FUNCTION): '''returns the opposite of the first term, ignoring the rest''' name = "Not" def on(self,*args): if args: return not args[0] else: return False class func_add(BLOCK_FUNCTION): '''val3 = val1 + val2''' name = "+" def on(self,*args): if args: if len(args) > 2: val = int(args[0]) val += int(args[1]) args[2].set(val) class func_gt(BLOCK_FUNCTION): '''true if val1 is greater than val2''' name = ">" def on(self,*args): if len(args)>1: if args[0] > args[1]: return True else: return False else: return False class func_set(BLOCK_FUNCTION): '''sets val1 to be the same as val2''' name = "Set" def on(self,*args): if args: if len(args) > 1: args[0].set(args[1]) return args[0] class func_close(BLOCK_FUNCTION): '''sets val1 true if powered, sets false if not. A normally open relay contact a.k.a. closes when powered''' name = "Close" def on(self,*args): if args: args[0].set(True) def off(self,*args): if args: args[0].set(False) class func_fault(BLOCK_FUNCTION): '''creates a fault if powered with whatever parameters are passed to this block as the message''' name = 'Fault' def on(self, *args): raise Exception('Fault Block -- ' + str(args)) BLOCK_FUNCTION_LIST = [func_and, func_or, func_not, func_add, func_gt, func_set, func_close,func_countup,func_countdown,func_fault] BLOCK_FUNCTION_DICT = dict(zip(map(lambda x:x.name,BLOCK_FUNCTION_LIST),BLOCK_FUNCTION_LIST)) DEFAULT = func_close