Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

#!/usr/bin/env python 

 

""" 

@file ion/agents/instrumentagents/instrument_agent.py 

@author Edward Hunter 

@brief Simple state mahcine for driver and agent classes. 

""" 

 

from twisted.internet import defer 

import ion.util.ionlog 

 

log = ion.util.ionlog.getLogger(__name__) 

 

 

 

class InstrumentFSM(): 

    """ 

    Simple state mahcine for driver and agent classes. 

    """ 

 

 

    def __init__(self, states, events, state_handlers,enter_event,exit_event): 

        """ 

        Initialize states, events, handlers. 

        """ 

        self.states = states 

        self.events = events 

        self.state_handlers = state_handlers 

        self.current_state = None 

        self.previous_state = None 

        self.enter_event = enter_event 

        self.exit_event = exit_event 

 

    def get_current_state(self): 

        """ 

        Return current state. 

        """ 

        return self.current_state 

 

 

    def start(self,state,params=None): 

        """ 

        Start the state machine. Initializes current state and fires the 

        EVENT_ENTER event. 

        """ 

 

        #if state not in self.states: 

        #    return False 

 

        if not self.states.has(state): 

            return False 

 

        self.current_state = state 

        self.state_handlers[self.current_state](self.enter_event,params) 

        return True 

 

    def on_event(self,event,params=None): 

        """ 

        Handle an event. Call the current state handler passing the event 

        and paramters. 

        @param event A string indicating the event that has occurred. 

        @param params Optional parameters to be sent with the event to the 

            handler. 

        @retval Success/fail if the event was handled by the current state. 

        """ 

 

        (success,next_state,result) = self.state_handlers[self.current_state](event,params) 

 

 

        #if next_state in self.states: 

        if self.states.has(next_state): 

            self._on_transition(next_state,params) 

 

        return (success,result) 

 

    @defer.inlineCallbacks 

    def on_event_async(self,event,params=None): 

        """ 

        Handle an event. Call the current state handler passing the event 

        and paramters. 

        @param event A string indicating the event that has occurred. 

        @param params Optional parameters to be sent with the event to the 

            handler. 

        @retval Success/fail if the event was handled by the current state. 

        """ 

        log.debug("Instrument FSM handling async event %s with current state %s", event, self.current_state) 

        (success,next_state,result) = yield self.state_handlers[self.current_state](event,params) 

 

        #if next_state in self.states: 

        if self.states.has(next_state): 

            yield self._on_transition_async(next_state,params) 

 

        defer.returnValue((success,result)) 

 

 

    def _on_transition(self,next_state,params): 

        """ 

        Call the sequence of events to cause a state transition. Called from 

        on_event if the handler causes a transition. 

        @param next_state The state to transition to. 

        @param params Opional parameters passed from on_event 

        """ 

 

        self.state_handlers[self.current_state](self.exit_event,params) 

        self.previous_state = self.current_state 

        self.current_state = next_state 

        self.state_handlers[self.current_state](self.enter_event,params) 

 

 

    @defer.inlineCallbacks 

    def _on_transition_async(self,next_state,params): 

        """ 

        Call the sequence of events to cause a state transition. Called from 

        on_event if the handler causes a transition. 

        @param next_state The state to transition to. 

        @param params Opional parameters passed from on_event 

        """ 

 

        yield self.state_handlers[self.current_state](self.exit_event,params) 

        self.previous_state = self.current_state 

        self.current_state = next_state 

        log.debug("Instrument Driver FSM transitioning from %s to %s", self.previous_state, self.current_state) 

        yield self.state_handlers[self.current_state](self.enter_event,params)