statepoint.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import threading
  2. class Statepoint:
  3. def __init__(self, initvalue = False, initstate = False):
  4. self.data = initvalue
  5. self.state = initstate
  6. self.lock = threading.Lock()
  7. self.permitted_update = True
  8. self.__private_permitted_update = True
  9. self.converter = lambda data: bool(data)
  10. self.do_excite = lambda: None
  11. self.do_reset = lambda: None
  12. self.keep_time = 1000
  13. self.pre_reset = False
  14. def inject(self, data):
  15. if self.data == data:
  16. return None
  17. #数据更新
  18. self.data = data
  19. #状态更新
  20. if self.permitted_update and self.__private_permitted_update:
  21. self.__async_update_state()
  22. #self.__update_state()
  23. def excite(self):
  24. #logger.info('excite to next')
  25. self.do_excite()
  26. def reset(self):
  27. self.do_reset()
  28. def __update_state(self):
  29. self.lock.acquire()
  30. last_state = self.state
  31. self.state = self.converter(self.data)
  32. if last_state == False and self.state == True:
  33. self.pre_reset = False
  34. self.excite()
  35. elif last_state == True and self.state == False:
  36. if self.keep_time <= 0:
  37. self.reset()
  38. elif self.pre_reset:
  39. self.pre_reset = False
  40. self.reset()
  41. else:
  42. self.state = True
  43. self.__private_allow_update(False)
  44. self.pre_reset = True
  45. timer = threading.Timer(self.keep_time/1000, lambda: self.__private_allow_update())
  46. timer.start()
  47. elif last_state == True and self.state == True:
  48. self.pre_reset = False
  49. else:
  50. self.pre_reset = False
  51. self.lock.release()
  52. def __async_update_state(self):
  53. threading.Thread(target=self.__update_state).start()
  54. def allow_update(self, enable: bool = True):
  55. self.permitted_update = enable
  56. if enable and self.__private_permitted_update:
  57. self.__async_update_state()
  58. #self.__update_state()
  59. def __private_allow_update(self, enable: bool = True):
  60. self.__private_permitted_update = enable
  61. if enable:
  62. self.__async_update_state()
  63. #self.__update_state()
  64. def set_convertor(self, func = lambda data: bool(data)):
  65. if callable(func):
  66. self.converter = func
  67. else:
  68. raise TypeError('The parameter func can only be a function')
  69. def set_excite_action(self, func = lambda: None):
  70. if callable(func):
  71. self.do_excite = func
  72. else:
  73. raise TypeError('The parameter func can only be a function')
  74. def set_reset_action(self, func = lambda: None):
  75. if callable(func):
  76. self.do_reset = func
  77. else:
  78. raise TypeError('The parameter func can only be a function')
  79. def set_keep_time(self, keeptime):
  80. self.keep_time = keeptime
  81. def set_state(self, state):
  82. self.state = state
  83. class Through_state_continues3(Statepoint):
  84. def __init__(self, p1, p2, p3):
  85. super().__init__()
  86. self.point1 = p1
  87. self.point2 = p2
  88. self.point3 = p3
  89. self.point1.allow_update(False)
  90. self.point2.allow_update(False)
  91. self.point3.allow_update(False)
  92. self.point1.set_excite_action(lambda: self.point2.allow_update())
  93. self.point2.set_excite_action(lambda: self.point3.allow_update())
  94. self.point3.set_excite_action(lambda: self.inject(True))
  95. self.point1.set_reset_action(lambda: (None if self.point2.state else self.point2.allow_update(False),
  96. None if self.point2.state or self.point3.state else self.inject(False)))
  97. self.point2.set_reset_action(lambda: (None if self.point3.state else self.point3.allow_update(False),
  98. None if self.point1.state else self.inject(False)))
  99. self.point3.set_reset_action(lambda: None if self.point2.state else self.inject(False))
  100. # self.set_excite_action(lambda: logger.info('经过点已触发'))
  101. # self.set_reset_action(lambda: logger.info('已前往推钢区域'))
  102. def reset(self):
  103. self.point2.allow_update(False)
  104. self.point3.allow_update(False)
  105. self.point3.state = False
  106. super().reset()
  107. if self.point1.state:
  108. self.point1.excite()
  109. def allow_update(self, enable: bool = True):
  110. if enable:
  111. self.point1.allow_update(enable)
  112. else:
  113. self.permitted_update = False
  114. self.point1.allow_update(False)
  115. self.point2.allow_update(False)
  116. self.point3.allow_update(False)
  117. self.point1.state = False
  118. self.point2.state = False
  119. self.point3.state = False
  120. self.permitted_update = True
  121. class Through_state_separation2(Statepoint):
  122. def __init__(self, p1, p2):
  123. super().__init__()
  124. self.point1 = p1
  125. self.point2 = p2
  126. self.point1.allow_update(False)
  127. self.point2.allow_update(False)
  128. #self.point1.set_keep_time(3000)
  129. self.point1.set_excite_action(lambda: self.point2.allow_update())
  130. self.point2.set_excite_action(lambda: self.inject(True))
  131. self.point1.set_reset_action(lambda: None if self.point2.state else self.point2.allow_update(False))
  132. self.point2.set_reset_action(lambda: self.inject(False))
  133. # self.set_excite_action(lambda: logger.debug('推钢机已经推动钢坯'))
  134. def reset(self):
  135. self.point1.allow_update(False)
  136. self.point2.allow_update(False)
  137. self.point1.state = False
  138. self.point2.state = False
  139. #logger.debug('推钢机刚刚经过')
  140. super().reset()
  141. def allow_update(self, enable: bool = True):
  142. if enable:
  143. # logger.debug('open')
  144. self.point1.allow_update()
  145. else:
  146. # logger.debug('close')
  147. self.permitted_update = False
  148. self.point1.allow_update(False)
  149. self.point2.allow_update(False)
  150. self.point1.state = False
  151. self.point2.state = False
  152. self.permitted_update = True