Generic, low security traffic light indicator for monitors
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ampel.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python3
  2. import bottle
  3. import json
  4. from bottle import route, post, run, template, static_file, request, response, redirect
  5. import sqlite3
  6. @route("/")
  7. def index():
  8. return template("display")
  9. @route("/login")
  10. def login():
  11. global employees
  12. if request.get_cookie("employeeId") is None:
  13. return template("login", employees=employees)
  14. #return template("login", employees=sorted(employees.items(), key=lambda entry: entry[0]))
  15. else:
  16. redirect("/manage")
  17. @post("/login")
  18. def loginUser():
  19. global employees
  20. employeeId = request.forms.get("employeeId")
  21. response.set_cookie("employeeId", employeeId , max_age=60*60*24*365*10)
  22. redirect("/manage")
  23. def loggedIn():
  24. try:
  25. employeeId = int(request.get_cookie("employeeId"))
  26. except ValueError:
  27. employeeId = None
  28. if employeeId is None or employeeId > len(employees) or employeeId < 0:
  29. response.set_cookie("employeeId", "") #clear cookie
  30. redirect("/login")
  31. else:
  32. return employeeId
  33. @post("/logout")
  34. def logout():
  35. employeeId = loggedIn()
  36. if employeeId >= 0:
  37. global employees
  38. loggedOut = list(employees[employeeId])
  39. loggedOut[2] = False #sets status to not available
  40. employees[employeeId] = loggedOut
  41. response.set_cookie("employeeId", "") #clear cookie
  42. redirect("/login")
  43. @route("/manage")
  44. def manage():
  45. global employees
  46. employeeId = loggedIn()
  47. if employeeId >= 0:
  48. return template("manage", employeeData=employees[employeeId])
  49. @post("/setStatus")
  50. def setStatus():
  51. employeeId = loggedIn()
  52. if employeeId >= 0:
  53. global employees
  54. newStatus = list(employees[employeeId])
  55. newStatus[2] = not newStatus[2] #toggle availability
  56. employees[employeeId] = newStatus
  57. redirect("/manage")
  58. @post("/getStatus")
  59. def getStatus():
  60. response.content_type = 'application/json'
  61. global employees
  62. employeeId = request.get_cookie("employeeId")
  63. resp = json.dumps(employees)
  64. return resp
  65. @route("/assets/static/<filename>")
  66. def staticContent(filename):
  67. return static_file(filename, root="./assets/static")
  68. @route("/assets/js/<filename>")
  69. def staticContent(filename):
  70. return static_file(filename, root="./assets/js")
  71. @route("/assets/css/<filename>")
  72. def staticContent(filename):
  73. return static_file(filename, root="./assets/css")
  74. def initTool():
  75. global employees
  76. employees = []
  77. with open("bearbeiter.txt", "r") as f:
  78. for line in f.readlines():
  79. name, room = line.strip().split("|")
  80. status = False
  81. employeeId = name.strip().replace(" ", "_")
  82. employees.append((name, room, status))
  83. pass
  84. bottle.TEMPLATE_PATH += ["./assets/templates/"]
  85. initTool()
  86. run(host='0.0.0.0', port=8080, debug=False)