instrument_manager.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. initial_state = dict(
  2. instrument_list={},
  3. connection_list={},
  4. )
  5. class ActionTypes(object):
  6. # Initiate update of instrument list. For every instrument, ADD_INSTRUMENT is triggered
  7. UPDATE_LIST = 'instrument_manager/UPDATE_LIST'
  8. # Add an instrument to the instrument list.
  9. ADD_INSTRUMENT = 'instrument_manager/ADD_INSTRUMENT'
  10. # Try to connect an instrument. On success, trigger ADD_CONNECTION.
  11. CONNECT = 'instrument_manager/CONNECT'
  12. # Add an instrument from the instrument list to the connection list.
  13. ADD_CONNECTION = 'instrument_manager/ADD_CONNECTION'
  14. # Try to disconnect an instrument. On success, trigger REMOVE_CONNECTION.
  15. DISCONNECT = 'instrument_manager/DISCONNECT'
  16. # Remove a disconnected instrument from the connection list.
  17. REMOVE_CONNECTION = 'instrument_manager/REMOVE_CONNECTION'
  18. class ActionCreators(object):
  19. @staticmethod
  20. def update_list():
  21. return dict(
  22. type=ActionTypes.UPDATE_LIST
  23. )
  24. @staticmethod
  25. def add_instrument(instrument):
  26. return dict(
  27. type=ActionTypes.ADD_INSTRUMENT,
  28. instrument=instrument
  29. )
  30. @staticmethod
  31. def connect(instrument):
  32. return dict(
  33. type=ActionTypes.CONNECT,
  34. id=instrument
  35. )
  36. @staticmethod
  37. def add_connection(instrument):
  38. return dict(
  39. type=ActionTypes.ADD_CONNECTION,
  40. instrument=instrument
  41. )
  42. @staticmethod
  43. def disconnect(instrument):
  44. return dict(
  45. type=ActionTypes.DISCONNECT,
  46. id=instrument
  47. )
  48. @staticmethod
  49. def remove_connection(instrument):
  50. return dict(
  51. type=ActionTypes.REMOVE_CONNECTION,
  52. id=instrument
  53. )
  54. def reducer(state=None, action=None):
  55. if state is None:
  56. state = initial_state
  57. if not (isinstance(action, dict) and 'type' in action):
  58. return state
  59. # if action['type'] == ActionTypes.UPDATE_LIST:
  60. # state = action['value']
  61. # elif action['type'] == ActionTypes.CONNECT:
  62. # state += 1
  63. # elif action['type'] == ActionTypes.DISCONNECT:
  64. # state -= 1
  65. return state
  66. if __name__ == '__main__':
  67. import unittest
  68. unittest.main()