code_door.erl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. % Autogenerated with DRAKON Editor 1.26
  2. -module(code_door).
  3. -behaviour(gen_fsm).
  4. -export([locked/2]).
  5. -export([open/2]).
  6. -export([opening/2]).
  7. -export([start_link/2]).
  8. -export([cancel/1, code_change/4, create/1, handle_event/3, handle_info/3, handle_sync_event/4, init/1, key/2, stop/1, terminate/3]).
  9. % stipan.mitkin@gmail.com
  10. % http://drakon-editor.sourceforge.net/
  11. % Copyright 2015 Stepan Mitkin
  12. % This code is PUBLIC DOMAIN.
  13. % The timeout between two button clicks - 10 sec.
  14. -define(TIMEOUT1, 5000).
  15. % The timeout while the door remains open - 30 sec.
  16. -define(TIMEOUT2, 10000).
  17. cancel(Name) ->
  18. % item 107
  19. gen_fsm:send_event(Name, cancel)
  20. .
  21. cancelled() ->
  22. % item 82
  23. io:fwrite("Cancel pressed.~n", [])
  24. .
  25. code_change(_OldVsn, StateName, StateData, _Extra) ->
  26. % item 147
  27. {ok, StateName, StateData}
  28. .
  29. create(Code) ->
  30. % item 94
  31. AlreadyTyped = [],
  32. State = {AlreadyTyped, Code},
  33. % item 95
  34. Options = [],
  35. % item 93
  36. start_link(State, Options)
  37. .
  38. do_lock() ->
  39. % item 78
  40. io:fwrite("The door is locked.~n", [])
  41. .
  42. do_open() ->
  43. % item 74
  44. io:fwrite("The door is opened.~n", [])
  45. .
  46. handle_event(stop, _StateName, StateData) ->
  47. % item 120
  48. {stop, normal, StateData}
  49. .
  50. handle_info(_Info, StateName, StateData) ->
  51. % item 153
  52. {next_state, StateName, StateData}
  53. .
  54. handle_sync_event(_Event, _From, StateName, StateData) ->
  55. % item 159
  56. {next_state, StateName, StateData}
  57. .
  58. init(State) ->
  59. % item 137
  60. io:fwrite("init!~n", []),
  61. % item 136
  62. {ok, locked, State}
  63. .
  64. key(Name, Key) ->
  65. % item 101
  66. gen_fsm:send_event(Name, {button, Key})
  67. .
  68. report_key(Key) ->
  69. % item 70
  70. io:fwrite("key: ~s~n", [Key])
  71. .
  72. stop(Name) ->
  73. % item 126
  74. gen_fsm:send_all_state_event(Name, stop)
  75. .
  76. terminate(normal, _StateName, _StateData) ->
  77. % item 114
  78. io:fwrite("Code door: terminating.~n", []),
  79. % item 113
  80. ok
  81. .
  82. timeout_hit() ->
  83. % item 86
  84. io:fwrite("Timeout reached.~n", [])
  85. .
  86. locked(Message_, State) ->
  87. case Message_ of
  88. {button, Key} ->
  89. % item 64
  90. report_key(Key),
  91. % item 49
  92. {_, Code} = State,
  93. % item 45
  94. {next_state, opening, {Key, Code}, ?TIMEOUT1}
  95. ;
  96. _ ->
  97. % item 46
  98. {next_state, locked, State}
  99. end
  100. .
  101. open(Message_, State) ->
  102. case Message_ of
  103. timeout ->
  104. % item 61
  105. timeout_hit(),
  106. % item 62
  107. do_lock(),
  108. % item 53
  109. {next_state, locked, State}
  110. ;
  111. dummy ->
  112. % item 62
  113. do_lock(),
  114. % item 53
  115. {next_state, locked, State}
  116. ;
  117. _ ->
  118. % item 54
  119. {next_state, open, State}
  120. end
  121. .
  122. opening(Message_, State) ->
  123. case Message_ of
  124. {button, Key} ->
  125. % item 63
  126. report_key(Key),
  127. % item 47
  128. {SoFar, Code} = State,
  129. Typed = SoFar ++ Key,
  130. % item 41
  131. case Typed =:= Code of true ->
  132. % item 57
  133. do_open(),
  134. % item 50
  135. {next_state, open, {[], Code}, ?TIMEOUT2}
  136. ; false ->
  137. % item 48
  138. {next_state, opening, {Typed, Code}, ?TIMEOUT1}
  139. end
  140. ;
  141. cancel ->
  142. % item 58
  143. cancelled(),
  144. % item 59
  145. do_lock(),
  146. % item 51
  147. {next_state, locked, State}
  148. ;
  149. timeout ->
  150. % item 60
  151. timeout_hit(),
  152. % item 59
  153. do_lock(),
  154. % item 51
  155. {next_state, locked, State}
  156. ;
  157. _ ->
  158. throw({invalid_state, "Message not supported by state 'opening'."})
  159. end
  160. .
  161. start_link(State, Options) ->
  162. gen_fsm:start_link(?MODULE, State, Options).