Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. ## This seeks out the bicycle counts for 49 (Frognerveien) and 55 (Bygdoy)
  2. ## from the most recent scrape in /scrapes
  3. ##
  4. ##
  5. ## Ideally in the future it will query a db where the scrapes are collected
  6. ##
  7.  
  8. import json
  9. import os
  10. import glob
  11. import sys
  12. import serial
  13. import urllib2
  14. import time
  15. import datetime
  16. import dateutil.relativedelta
  17. from time import sleep
  18.  
  19. ## Initialize serial target
  20.  
  21.  
  22. # Copied print and logging class from KpaBap's script
  23.  
  24. class simpleLogger():
  25.  
  26. def __init__(self,logfile):
  27. self.logfile = logfile
  28. open(logfile,"w").write("") ##clear out any previous contents
  29.  
  30. def write(self,logtext):
  31. logfile = open(self.logfile,"a")
  32. logfile.write(logtext)
  33. logfile.flush()
  34. logfile.close()
  35. return 0
  36.  
  37. def flush(self):
  38. return 0
  39.  
  40. def mainloop():
  41. sys.stdout = simpleLogger("/home/pi/bike/lcd.log")
  42. sys.stderr = simpleLogger("/home/pi/bike/lcd-err.log")
  43. ser = serial.Serial('/dev/ttyACM0', 115200)
  44.  
  45.  
  46. try:
  47. url11 = "http://reis.trafikanten.no/reisrest/realtime/getrealtimedata/3010511"
  48. url17 = "http://reis.trafikanten.no/reisrest/realtime/getrealtimedata/3010531"
  49.  
  50. url11data = json.load(urllib2.urlopen(url11))
  51. url17data = json.load(urllib2.urlopen(url17))
  52. i = 0
  53. j = 0
  54.  
  55. nybruatimes = list()
  56. heimdalstimes = list()
  57.  
  58. while i < len(url11data):
  59. if (url11data[i][u'DirectionRef'] == '2' and url11data[i][u'LineRef'] == '11'):
  60. nybruatimes.append(url11data[i][u'ExpectedArrivalTime'])
  61. i = i + 1
  62. while j < len(url17data):
  63. if (url17data[j][u'DirectionRef'] == '2' and url17data[j][u'LineRef'] == '17'):
  64. heimdalstimes.append(url17data[j][u'ExpectedArrivalTime'])
  65. j = j + 1
  66. except:
  67. ser.write(b'\xFE')
  68. ser.write(b'\x58')
  69. ser.write("Connect Error")
  70. #sys.exit()
  71.  
  72. ## Set current time
  73. current = datetime.datetime.fromtimestamp(int(time.time())) ## Current time
  74.  
  75. ## Wipe and Start First Line -- with catchall exception
  76. try:
  77.  
  78. if len(heimdalstimes) == 1:
  79. top1 = datetime.datetime.fromtimestamp(int(heimdalstimes[0][6:16]))
  80. top1tuple = dateutil.relativedelta.relativedelta (top1, current) ## First diff
  81. topcombo = str(top1tuple.minutes) + 'm '
  82. else:
  83. top2 = datetime.datetime.fromtimestamp(int(heimdalstimes[1][6:16]))
  84. top2tuple = dateutil.relativedelta.relativedelta (top2, current)
  85. topcombo = str(top1tuple.minutes) + 'm ' + str(top2tuple.minutes) + 'm'
  86.  
  87. ser.write(b'\xFE')
  88. ser.write(b'\x58')
  89. ser.write("17 Vest: %s" % topcombo)
  90. except:
  91. ser.write(b'\xFE')
  92. ser.write(b'\x58')
  93. ser.write("17 Vest: N/A")
  94.  
  95.  
  96. ## Second Line -- with late night N/A exception
  97. try:
  98.  
  99. if len(nybruatimes) == 1:
  100. bottom1 = datetime.datetime.fromtimestamp(int(nybruatimes[0][6:16])) ## First sentrum time tuple
  101. bottom1tuple = dateutil.relativedelta.relativedelta (bottom1, current) ## First diff
  102. bottomcombo = str(bottom1tuple.minutes) + 'm '
  103. else:
  104. bottom2 = datetime.datetime.fromtimestamp(int(nybruatimes[1][6:16]))
  105. bottom2tuple = dateutil.relativedelta.relativedelta (bottom2, current)
  106. bottomcombo = str(bottom1tuple.minutes) + 'm ' + str(bottom2tuple.minutes) + 'm'
  107.  
  108. ser.write(b'\xFE')
  109. ser.write(b'\x47')
  110. ser.write("\x01")
  111. ser.write("\x02")
  112. except:
  113. ser.write(b'\xFE')
  114. ser.write(b'\x47')
  115. ser.write("\x01")
  116. ser.write("\x02")
  117. ser.write("11 Vest: N/A")
  118.  
  119. time.sleep(15)
  120.  
  121. while(1):
  122. mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement