statepoint.py 6.4 KB

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