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.

173 lines
4.9 KiB

6 years ago
  1. #!/usr/bin/env python3
  2. '''Hello to the world from ev3dev.org'''
  3. import os
  4. import sys
  5. import time
  6. import json
  7. import datetime
  8. import urllib3
  9. import requests
  10. from ev3dev2.motor import LargeMotor, OUTPUT_A, OUTPUT_B, SpeedPercent, MoveTank
  11. from ev3dev2.sensor import INPUT_1,INPUT_2
  12. from ev3dev2.sensor.lego import TouchSensor
  13. from ev3dev2.sensor.lego import UltrasonicSensor
  14. from ev3dev2.sound import Sound
  15. from ev3dev2.button import Button
  16. ON = True
  17. OFF = False
  18. def debug_print(*args, **kwargs):
  19. print(*args, **kwargs, file=sys.stderr)
  20. def reset_console():
  21. print('\x1Bc', end='')
  22. def set_cursor(state):
  23. if state:
  24. print('\x1B[?25h', end='')
  25. else:
  26. print('\x1B[?25l', end='')
  27. def set_font(name):
  28. '''Sets the console font
  29. A full list of fonts can be found with `ls /usr/share/consolefonts`
  30. '''
  31. os.system('setfont ' + name)
  32. def raise_ramp():
  33. print("Raising ramp")
  34. def get_closest_time(times, time):
  35. closest = 0
  36. times = [datetime.datetime.strptime(i,"%H:%M") for i in times]
  37. time = datetime.datetime.strptime(time,"%H:%M")
  38. for i in times:
  39. if i.hour < time.hour or (i.hour == time.hour and i.minute < time.minute):
  40. continue
  41. else:
  42. if closest == 0:
  43. closest = i
  44. continue
  45. if abs(i.hour - time.hour) < abs(closest.hour - time.hour):
  46. closest = i
  47. continue
  48. elif abs(i.hour - time.hour) == abs(closest.hour - time.hour):
  49. if i.minute - time.minute < closest.minute - time.minute:
  50. closest = i
  51. return closest.strftime("%H:%M")
  52. def time_diff(t1, t2):
  53. time1 = datetime.datetime.strptime(t1, "%H:%M")
  54. time2 = datetime.datetime.strptime(t2, "%H:%M")
  55. response = ""
  56. if abs(time2.hour - time1.hour) < 10:
  57. response += "0"
  58. if time2.minute - time1.minute >= 0:
  59. response += str(abs(time2.hour - time1.hour))
  60. else:
  61. response += str(abs(time2.hour - time1.hour) - 1)
  62. response += ":"
  63. if abs(time2.minute - time1.minute) < 10:
  64. response += "0"
  65. response += str(abs(time2.minute - time1.minute))
  66. return response
  67. def raise_ramp(motor1):
  68. motor1.on_for_rotations(SpeedPercent(75), 5)
  69. def main():
  70. start = time.time()
  71. file = open("times.json","r")
  72. time_data = json.loads(file.read())
  73. stop_data = [i for i in time_data.keys()]
  74. debug_print(stop_data)
  75. file.close()
  76. time_start = "10:02"
  77. debug_print('Started Code')
  78. reset_console()
  79. set_cursor(OFF)
  80. set_font('Lat15-Terminus24x12')
  81. alarms = [0,0,0]
  82. stop = "11222"
  83. urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
  84. print('AKILLI DURAK')
  85. sound = Sound()
  86. us = UltrasonicSensor(INPUT_2)
  87. us.MODE_US_DIST_CM
  88. btn = Button()
  89. m1 = LargeMotor(OUTPUT_A)
  90. ts = TouchSensor(INPUT_1)
  91. while 1:
  92. if us.distance_centimeters < 15:
  93. sound.speak("Please select a stop")
  94. i = 0
  95. reset_console()
  96. print(stop_data[i%len(stop_data)])
  97. while not btn.enter:
  98. if btn.up:
  99. i += 1
  100. reset_console()
  101. print(stop_data[i%len(stop_data)])
  102. time.sleep(0.1)
  103. if btn.down:
  104. i -= 1
  105. reset_console()
  106. print(stop_data[i%len(stop_data)])
  107. time.sleep(0.1)
  108. closest = datetime.datetime.strptime(time_diff(time_start,get_closest_time(time_data[stop_data[i%len(stop_data)]]["weekday"][:],time_start)),"%H:%M")
  109. difference = datetime.timedelta(hours=closest.hour,minutes=closest.minute).total_seconds()+start
  110. if not difference in alarms:
  111. alarms[i%len(stop_data)] = difference
  112. r = requests.get("https://192.168.44.50:5000/bus?line={}&stop={}".format(stop_data[i%len(stop_data)],stop), verify=False)
  113. print(difference-time.time())
  114. time.sleep(1)
  115. reset_console()
  116. print("AKILLI DURAK")
  117. for i in range(len(alarms)):
  118. if alarms[i] == 0:
  119. continue
  120. else:
  121. if alarms[i]-time.time()<0:
  122. reset_console()
  123. print("Bus no {} has arrived".format(stop_data[i%len(stop_data)]))
  124. sound.speak("Bus no {} has arrived".format(stop_data[i%len(stop_data)]))
  125. sound.speak("Would you like a ramp?")
  126. waiter = time.time()
  127. pressed = True
  128. while time.time()-waiter < 10 and pressed:
  129. if ts.is_pressed:
  130. pressed = False
  131. raise_ramp(m1)
  132. reset_console()
  133. print("AKILLI DURAK")
  134. alarms[i] = 0
  135. if __name__ == '__main__':
  136. main()