statepoint.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import threading, time
  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. if self.permitted_update:
  74. self.allow_update(0)
  75. self.set_state(False)
  76. self.do_excite = func
  77. self.allow_update()
  78. else:
  79. self.do_excite = func
  80. else:
  81. raise TypeError('The parameter func can only be a function')
  82. def set_reset_action(self, func = lambda: None):
  83. if callable(func):
  84. self.do_reset = func
  85. else:
  86. raise TypeError('The parameter func can only be a function')
  87. def set_keep_time(self, keeptime):
  88. self.keep_time = keeptime
  89. def set_state(self, state):
  90. self.state = state
  91. class Through_state_continues3(Statepoint):
  92. def __init__(self, p1, p2, p3):
  93. super().__init__()
  94. self.point1 = p1
  95. self.point2 = p2
  96. self.point3 = p3
  97. self.point1.allow_update(False)
  98. self.point2.allow_update(False)
  99. self.point3.allow_update(False)
  100. self.point1.set_excite_action(lambda: self.point2.allow_update())
  101. self.point2.set_excite_action(lambda: self.point3.allow_update())
  102. self.point3.set_excite_action(lambda: self.inject(True))
  103. self.point1.set_reset_action(lambda: (None if self.point2.state else self.point2.allow_update(False),
  104. None if self.point2.state or self.point3.state else self.inject(False)))
  105. self.point2.set_reset_action(lambda: (None if self.point3.state else self.point3.allow_update(False),
  106. None if self.point1.state else self.inject(False)))
  107. self.point3.set_reset_action(lambda: None if self.point2.state else self.inject(False))
  108. # self.set_excite_action(lambda: logger.info('经过点已触发'))
  109. # self.set_reset_action(lambda: logger.info('已前往推钢区域'))
  110. def reset(self):
  111. self.point2.allow_update(False)
  112. self.point3.allow_update(False)
  113. self.point3.state = False
  114. super().reset()
  115. if self.point1.state:
  116. self.point1.excite()
  117. def allow_update(self, enable: bool = True):
  118. if enable:
  119. self.point1.allow_update(enable)
  120. else:
  121. self.permitted_update = False
  122. self.point1.allow_update(False)
  123. self.point2.allow_update(False)
  124. self.point3.allow_update(False)
  125. self.point1.state = False
  126. self.point2.state = False
  127. self.point3.state = False
  128. self.permitted_update = True
  129. class Through_state_separation2(Statepoint):
  130. def __init__(self, p1, p2):
  131. super().__init__()
  132. self.point1 = p1
  133. self.point2 = p2
  134. self.point1.allow_update(False)
  135. self.point2.allow_update(False)
  136. #self.point1.set_keep_time(3000)
  137. self.point1.set_excite_action(lambda: self.point2.allow_update())
  138. self.point2.set_excite_action(lambda: self.inject(True))
  139. self.point1.set_reset_action(lambda: None if self.point2.state else self.point2.allow_update(False))
  140. self.point2.set_reset_action(lambda: self.inject(False))
  141. # self.set_excite_action(lambda: logger.debug('推钢机已经推动钢坯'))
  142. def reset(self):
  143. self.point1.allow_update(False)
  144. self.point2.allow_update(False)
  145. self.point1.state = False
  146. self.point2.state = False
  147. #logger.debug('推钢机刚刚经过')
  148. super().reset()
  149. self.point1.allow_update()
  150. def allow_update(self, enable: bool = True):
  151. if enable:
  152. # logger.debug('open')
  153. self.point1.allow_update()
  154. else:
  155. # logger.debug('close')
  156. self.permitted_update = False
  157. self.point1.allow_update(False)
  158. self.point2.allow_update(False)
  159. self.point1.state = False
  160. self.point2.state = False
  161. self.permitted_update = True
  162. class Integration_speed_mpmin(Statepoint):
  163. def __init__(self, *args):
  164. super().__init__(*args)
  165. self.last_inject_time = time.time()
  166. self.last_data = 0
  167. self.last_data_time = self.last_inject_time
  168. def inject(self, data):
  169. current_inject_time = time.time()
  170. current_data = data
  171. current_data_time = self.last_inject_time + (current_inject_time - self.last_inject_time) / 2
  172. data = self.data + (current_data_time - self.last_data_time) * (self.last_data + current_data) / 120
  173. self.last_inject_time = current_inject_time
  174. self.last_data = current_data
  175. self.last_data_time = current_data_time
  176. return super().inject(data)