def __init__(self): self.state = StateMachine() self.blockchain = Blockchain() self.transaction_pool = [] self.executed_transactions = [] self.nodes = [] self.lock = Lock()
def __init__(self, channels): """ :param channels: a list of Channel object """ self._channels = channels self._statemachine = {} self.__savestartdate = {} self.__saveenddate = {} self._currentstate = {} self._timer = {} for ch in self._channels: self._statemachine[ch.nb] = StateMachine() self._statemachine[ch.nb].register("NotRunning", self._not_running, [ch]) self._statemachine[ch.nb].register("Running", self._running, [ch]) self._statemachine[ch.nb].register("ManualOn", self._manual_on, [ch]) self._statemachine[ch.nb].register("ManualOff", self._manual_off, [ch]) self._statemachine[ch.nb].setState("NotRunning") self._currentstate[ch.nb] = {'nb': ch.nb, 'state': "NotRunning"} self._timer[ch.nb] = timer.Timer() self._oldstate = self._currentstate.copy() self._sched = Scheduler(self.run) self._logger = logging.getLogger('sprinkler') self._event_new_state = asyncio.Event()
def getStates(self): # add states to the game self.states = StateMachine() self.states.addState('mainScreen', MainScreen(self)) self.states.addState('gameScreen', GameScreen(self)) self.states.addState('pauseScreen', PauseMenu(self)) self.states.change('mainScreen')
def sm_eval(self, plan, input_state): def be_task(prim_name, fn, api, **kwargs): def wrapped(input_state, **outer_args): kwargs.update(outer_args) return self.backend_wrapper(input_state, prim_name, fn, api, **kwargs) return wrapped self.logger.info('building state machine for %s' % plan) sm = StateMachine(input_state) sm.set_state('state_0') for state_index, state in enumerate(plan): parameters = state['ns'] primitive = state['primitive'] timeout = state.get('timeout', 30) self.logger.debug('padding globals %s' % self.adventure_globals) parameters['globals'] = self.adventure_globals parameters['globals']['parent_task_id'] = self.parent_task_id self.logger.debug('Wrapping %s primitive' % primitive) if '.' in primitive: # this primitive comes from backends backend_fn = opencenter.backends.primitive_by_name(primitive) if not backend_fn: msg = 'cannot find backend primitive "%s"' % primitive self.logger.debug(msg) input_state['fails'] = input_state['nodes'] input_state['nodes'] = [] return ({ 'result_code': 1, 'result_str': msg, 'result_data': {} }, input_state) fn = be_task(primitive, backend_fn, self.api, **parameters) else: # this primitive comes from node tasks. parameters['timeout'] = timeout run_task = 'agent.run_task' backend_fn = opencenter.backends.primitive_by_name(run_task) fn = be_task('agent.run_task', backend_fn, self.api, **{ 'action': primitive, 'payload': parameters }) # we have the backend fn, now wrap it up. # solver plans are linear, so we'll jump to next step always # plus, we'll assume that failure goes to default failure case next_state = state_index + 1 success_state = 'state_%d' % next_state if (next_state) == len(plan): success_state = 'success' sm.add_state( 'state_%d' % state_index, StateMachineState(advance=fn, on_success=success_state)) result_data, end_state = sm.run_to_completion() return (result_data, end_state)
from state import StateMachine import config import pyaudio import time machine = StateMachine() p = pyaudio.PyAudio() def callback(data, frame_count, time_info, status): out = machine.process(data) return (out, pyaudio.paContinue) stream = p.open(format=p.get_format_from_width(config.WIDTH), channels=config.CHANNELS, rate=config.RATE, frames_per_buffer=config.CHUNK, input=True, output=True, stream_callback=callback) stream.start_stream() while True: time.sleep(0.1) stream.stop_stream() stream.close() p.terminate()