/scripts/repeater.py

https://github.com/batt/StratoSpera
Python | 99 lines | 79 code | 16 blank | 4 comment | 25 complexity | a6a61e3bccaa895803102d2d73a212f3 MD5 | raw file
  1. #!/usr/bin/env python
  2. import config
  3. import os
  4. import time
  5. import sys
  6. import thread
  7. import glob
  8. from datetime import datetime
  9. import utils
  10. def uploader_thread():
  11. try:
  12. while True:
  13. time.sleep(0.1)
  14. os.system("python uploader.py")
  15. except:
  16. thread.interrupt_main()
  17. def downloader_thread():
  18. try:
  19. while True:
  20. time.sleep(30)
  21. os.system("python downloader.py")
  22. except:
  23. thread.interrupt_main()
  24. def parse_loop():
  25. logfile = config.logdir + "/aprs.log"
  26. os.system("mkdir -p " + config.logdir)
  27. os.system("touch " + logfile)
  28. #start aprs_decoder
  29. os.system("python ax25.py >%s&" % logfile)
  30. try:
  31. #start web updaters
  32. thread.start_new_thread(uploader_thread, ())
  33. thread.start_new_thread(downloader_thread, ())
  34. wait_start = True
  35. where = 0
  36. while 1:
  37. file = open(logfile,'r')
  38. file.seek(where)
  39. d = file.readline()
  40. where = file.tell()
  41. file.close()
  42. if not d:
  43. time.sleep(0.1)
  44. else:
  45. if wait_start:
  46. #Check for correct config.sender address
  47. if d.startswith("AFSK1200: fm %s" % config.sender.upper()):
  48. wait_start = False
  49. else:
  50. wait_start = True
  51. if d[1:7].isdigit() and d[7] == 'h':
  52. name = d[1:7]
  53. elif config.log_all_messages:
  54. now = datetime.utcnow()
  55. name = "%02d%02d%02d" % (now.hour, now.minute, now.second)
  56. else:
  57. print "Unhandled message:", d.strip()
  58. continue
  59. msg_name = config.logdir + "/" + name
  60. found = False
  61. for msg in glob.glob(config.logdir + "/" + "[0-9]" * 6 + "*"):
  62. if msg.startswith(msg_name):
  63. print "Message", msg, "already present"
  64. found = True
  65. break
  66. if not found:
  67. utils.write_file(config.logdir + "/" + name, d)
  68. utils.write_file(config.logdir + "/" + name + ".unsent", d)
  69. utils.update_index(config.logdir)
  70. except KeyboardInterrupt:
  71. print "\nCTRL-C pressed, exit"
  72. finally:
  73. os.system("kill `ps ax | grep ax25.py | head -n1 | cut -f2 -d' '`")
  74. def auth_test(start):
  75. min = start
  76. for i in range(60):
  77. updater.send_server(">%04d00zTest %d" % (min, i))
  78. min += 1
  79. if __name__ == "__main__":
  80. if len(sys.argv) > 1 and sys.argv[1].isdigit():
  81. auth_test(int(sys.argv[1]))
  82. else:
  83. parse_loop()