Guest User

Untitled

a guest
Feb 23rd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # Remember to either chmod +x this file and call it like this:
  3. # */15 * * * * /path/to/foo.py >/dev/null 2>&1
  4. # Or invoke with python3 like this:
  5. # */15 * * * * python3 /path/to/foo.py >/dev/null 2>&1
  6.  
  7. from subprocess import run
  8.  
  9. username = 'User'
  10. password = 'Pass'
  11.  
  12. omxplayer_start_command = """screen -dmS ipc{} sh -c 'omxplayer --live --refresh --fps {} --win "{}" rtsp://{}:{}@{}/h264/ch1/sub/av_stream'"""
  13.  
  14. # The below could easily be in a JSON configuration file. Then you could just do this and have the script be super short
  15. # import json
  16. # with open('config_file_path') as file_descriptor:
  17. # cameras = json.load(file_descriptor)
  18. # As things are, I've baked the config into the script.
  19.  
  20.  
  21. cameras = {
  22. '1': {
  23. 'fps': '30',
  24. 'window_size': '0 0 1008 1050',
  25. 'ip_address': '192.168.1.50',
  26. 'status': {
  27. '0': {'kill': ['1'], 'start': '7'},
  28. '1': {'kill': ['7', '4', '5'], 'start': '1'}
  29. }
  30. },
  31. '2': {
  32. 'fps': '15',
  33. 'window_size': '1008 0 1680 350',
  34. 'ip_address': '192.168.1.51',
  35. 'status': {
  36. '0': {'kill': ['2'], 'start': '8'},
  37. '1': {'kill': ['8'], 'start': '2'}
  38. }
  39. },
  40. '3': {
  41. 'fps': '15',
  42. 'window_size': '1008 350 1680 700',
  43. 'ip_address': '192.168.1.52',
  44. 'status': {
  45. '0': {'kill': ['3'], 'start': '9'},
  46. '1': {'kill': ['9'], 'start': '3'}
  47. }
  48. },
  49. '4': {
  50. 'fps': '15',
  51. 'window_size': '840 0 1680 350',
  52. 'ip_address': '192.168.1.53',
  53. 'status': {
  54. '0': {'kill': [], 'start': None},
  55. '1': {'kill': [], 'start': '4'}
  56. }
  57. },
  58. '5': {
  59. 'fps': '15',
  60. 'window_size': '840 350 1680 700',
  61. 'ip_address': '192.168.1.54',
  62. 'status': {
  63. '0': {'kill': [], 'start': None},
  64. '1': {'kill': [], 'start': '5'}
  65. }
  66. },
  67. '6': {
  68. 'fps': '15',
  69. 'window_size': '1008 700 1680 1050',
  70. 'ip_address': '192.168.1.55',
  71. 'status': {
  72. '0': {'kill': ['6'], 'start': '10'},
  73. '1': {'kill': ['10'], 'start': '6'}
  74. }
  75. },
  76. '7': {
  77. 'fps': '15',
  78. 'window_size': '0 0 840 350',
  79. 'ip_address': '192.168.1.50',
  80. },
  81. '8': {
  82. 'fps': '15',
  83. 'window_size': '0 350 840 700',
  84. 'ip_address': '192.168.1.51',
  85. },
  86. '9': {
  87. 'fps': '15',
  88. 'window_size': '0 700 840 1050',
  89. 'ip_address': '192.168.1.52',
  90. },
  91. '10': {
  92. 'fps': '15',
  93. 'window_size': '840 700 1680 1050',
  94. 'ip_address': '192.168.1.55',
  95. }
  96. }
  97.  
  98. # Whew! Alright. Your script actually has pretty complex logic. There might be ways to simplify that, but it's not
  99. # really my place to do so if there are.
  100. # Previously, you had the relationship between cameras codified in your if statements and what you did in them.
  101. # I tend to prefer more declarative style config, so cameras 1-6 have a status key in them.
  102. # If the grep for those camera's pids returns a 0 (which means grep found something) then I perform one set of actions,
  103. # and if it's 1 (the other value grep will return, which means it couldn't find anything) then I'll perform a different
  104. # set of actions. That way, there's only one piece of actual code, and the config dictates the relationships between
  105. # all of the cameras, namely which ones to kill and which one to start. The kill key is a list since you kill multiple
  106. # cameras if st1 is not running.
  107.  
  108. for camera_number in range(1, 7):
  109. # For stupid techincal reasons, all of the integer keys in our dict above are strings. Namely, JSON doesn't like
  110. # keys that aren't strings. In order to make it easy to turn this into something that reads from a JSON config file,
  111. # I'm going to pretend that I'm already reading from one and cast our camera_number variable to a string.
  112. camera_number = str(camera_number)
  113. # Grep for a specific camera running. I dunno if the cut statements are necessary here, since we're just checking
  114. # the return value now. I've left them out for simplicities sake.
  115. # See that {} thingy there? That's how you do string interpolation in Python. In Bash you can just do this:
  116. # ps -ef | grep omxplayer | grep ipc$some_variable
  117. # Python doesn't do that. It's actually a much better design, but that means that you need to do the {} thingy and
  118. # put .format at the end of your string.
  119. result = run('''ps -ef | grep omxplayer | grep ipc{}'''.format(camera_number), shell=True)
  120. # Again, casting this to a string because JSON is kinda dumb sometimes. YAML is way better but isn't in the stdlib.
  121. camera_is_running = str(result.returncode)
  122. # Pull out what to do with this specific camera based on whether is was running or not.
  123. camera_info = cameras[camera_number]['status'][camera_is_running]
  124. cameras_to_kill = camera_info['kill']
  125. camera_to_start = camera_info['start']
  126. for camera_to_kill in cameras_to_kill:
  127. # Using the old method to kill cameras because I can't test against the actual environment. I'd recommend
  128. # changing this to use pkill instead.
  129. run('''kill $(ps aux | grep ipc{} | awk '{{print $2}}')'''.format(camera_to_kill), shell=True)
  130. if camera_to_start is None:
  131. # In the cases of cameras 4 and 5, you don't start anything if the camera is already running.
  132. continue
  133. camera = cameras[camera_to_start]
  134. camera_fps, camera_window_size, camera_ip_address = camera['fps'], camera['window_size'], camera['ip_address']
  135. run(omxplayer_start_command.format(
  136. camera_to_start, camera_fps, camera_window_size, username, password, camera_ip_address), shell=True)
Add Comment
Please, Sign In to add comment