statepoint.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. with self.lock:
  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. def __async_update_state(self):
  52. threading.Thread(target=self.__update_state).start()
  53. def allow_update(self, enable: bool = True):
  54. self.permitted_update = enable
  55. if enable and self.__private_permitted_update:
  56. self.__async_update_state()
  57. #self.__update_state()
  58. def __private_allow_update(self, enable: bool = True):
  59. self.__private_permitted_update = enable
  60. if enable:
  61. self.__async_update_state()
  62. #self.__update_state()
  63. def set_convertor(self, func = lambda data: bool(data)):
  64. if callable(func):
  65. self.converter = func
  66. else:
  67. raise TypeError('The parameter func can only be a function')
  68. def set_excite_action(self, func = lambda: None):
  69. if callable(func):
  70. self.do_excite = func
  71. else:
  72. raise TypeError('The parameter func can only be a function')
  73. def set_reset_action(self, func = lambda: None):
  74. if callable(func):
  75. self.do_reset = func
  76. else:
  77. raise TypeError('The parameter func can only be a function')
  78. def set_keep_time(self, keeptime):
  79. self.keep_time = keeptime
  80. def set_state(self, state):
  81. self.state = state
  82. class Through_state_continues3(Statepoint):
  83. def __init__(self, p1, p2, p3):
  84. super().__init__()
  85. self.point1 = p1
  86. self.point2 = p2
  87. self.point3 = p3
  88. self.point1.allow_update(False)
  89. self.point2.allow_update(False)
  90. self.point3.allow_update(False)
  91. self.point1.set_excite_action(lambda: self.point2.allow_update())
  92. self.point2.set_excite_action(lambda: self.point3.allow_update())
  93. self.point3.set_excite_action(lambda: self.inject(True))
  94. self.point1.set_reset_action(lambda: (None if self.point2.state else self.point2.allow_update(False),
  95. None if self.point2.state or self.point3.state else self.inject(False)))
  96. self.point2.set_reset_action(lambda: (None if self.point3.state else self.point3.allow_update(False),
  97. None if self.point1.state else self.inject(False)))
  98. self.point3.set_reset_action(lambda: None if self.point2.state else self.inject(False))
  99. # self.set_excite_action(lambda: logger.info('经过点已触发'))
  100. # self.set_reset_action(lambda: logger.info('已前往推钢区域'))
  101. def reset(self):
  102. self.point2.allow_update(False)
  103. self.point3.allow_update(False)
  104. self.point3.state = False
  105. super().reset()
  106. if self.point1.state:
  107. self.point1.excite()
  108. def allow_update(self, enable: bool = True):
  109. if enable:
  110. self.point1.allow_update(enable)
  111. else:
  112. self.permitted_update = False
  113. self.point1.allow_update(False)
  114. self.point2.allow_update(False)
  115. self.point3.allow_update(False)
  116. self.point1.state = False
  117. self.point2.state = False
  118. self.point3.state = False
  119. self.permitted_update = True
  120. class Through_state_separation2(Statepoint):
  121. def __init__(self, p1, p2):
  122. super().__init__()
  123. self.point1 = p1
  124. self.point2 = p2
  125. self.point1.allow_update(False)
  126. self.point2.allow_update(False)
  127. #self.point1.set_keep_time(3000)
  128. self.point1.set_excite_action(lambda: self.point2.allow_update())
  129. self.point2.set_excite_action(lambda: self.inject(True))
  130. self.point1.set_reset_action(lambda: None if self.point2.state else self.point2.allow_update(False))
  131. self.point2.set_reset_action(lambda: self.inject(False))
  132. # self.set_excite_action(lambda: logger.debug('推钢机已经推动钢坯'))
  133. def reset(self):
  134. self.point1.allow_update(False)
  135. self.point2.allow_update(False)
  136. self.point1.state = False
  137. self.point2.state = False
  138. #logger.debug('推钢机刚刚经过')
  139. super().reset()
  140. def allow_update(self, enable: bool = True):
  141. if enable:
  142. # logger.debug('open')
  143. self.point1.allow_update()
  144. else:
  145. # logger.debug('close')
  146. self.permitted_update = False
  147. self.point1.allow_update(False)
  148. self.point2.allow_update(False)
  149. self.point1.state = False
  150. self.point2.state = False
  151. self.permitted_update = True