Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. # !/usr/bin/env python
  2.  
  3. # HOW TO USE
  4. #
  5. # ROBOT:
  6. # - Poll the URL http://veemon:5000/getinstructions to receive instructions that have been sent by the app. After an
  7. # instruction is received by the robot it is reset, so the next time the URL is accessed it will return
  8. # INSTRUCTION_RESET_VALUE
  9. # - Add notifications to the notification list with http://veemon:5000/addnotification?not=x where x is the notification
  10. # string to be received by the app.
  11. #
  12. # APP:
  13. # - Send instructions to the robot by accessing http://veemon:5000/setinstructions?inst=x where x is the instruction to
  14. # be received by the robot
  15. # - Receive notifications from the robot by polling http://veemon:5000/getnotifications. Notifications will be returned
  16. # as a string, where (if there is more than one notification) notifications are separated by semicolons. After a list of
  17. # notifications has been received, the list will be reset, so the next time the URL is accessed it will return the empty
  18. # string. (The empty string means there are no notifications.)
  19.  
  20. from flask import Flask
  21. from flask import request
  22. import time
  23. import serial
  24.  
  25. app = Flask(__name__)
  26. INSTRUCTION_RESET_VALUE = "0"
  27. INITIAL_INSTRUCTION = "0"
  28.  
  29. @app.route('/test')
  30. def home():
  31. return "Server is working!"
  32.  
  33. instructions = INITIAL_INSTRUCTION
  34. notifications = []
  35.  
  36. # URL accessed by robot to receive instructions from the app
  37. # Instructions are reset to RESET_VALUE after being accessed
  38. @app.route('/getinstructions')
  39. def get_instructions():
  40. global instructions
  41. ret = instructions
  42. instructions = INSTRUCTION_RESET_VALUE
  43. return ret
  44.  
  45. # URL accessed by app to send instructions to server
  46. @app.route('/setinstructions')
  47. def send_instructions():
  48. global instructions
  49. instructions = request.args['inst']
  50. return "Instructions set to " + instructions
  51.  
  52. @app.route('/addnotification')
  53. def add_notification():
  54. global notifications
  55. notification = request.args['not']
  56. notifications.append(notification)
  57. return "Succesfully appended notification \"" + notification + "\" to the list"
  58.  
  59. @app.route('/getnotifications')
  60. def get_notifications():
  61. global notifications
  62. ret = ""
  63. for notification in notifications:
  64. ret = ret + notification + ";"
  65. ret = ret[0:-1] # cuts off last semicolon
  66. notifications = []
  67. return ret
  68.  
  69. # Start the server without the dictionary function
  70. def run():
  71. app.run(host='0.0.0.0')
  72.  
  73.  
  74. if __name__ == "__main__":
  75. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement