main.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. import json
  4. import logging
  5. import logging.handlers
  6. import timing
  7. import threading
  8. import upload
  9. import config
  10. def format_logger(log_path):
  11. logger = logging.getLogger()
  12. rf = logging.handlers.TimedRotatingFileHandler(filename=log_path, when='S', interval=1, backupCount=3)
  13. rf.setFormatter(logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'))
  14. handler = logging.StreamHandler()
  15. handler.setLevel(logging.DEBUG)
  16. handler.setFormatter(logging.Formatter('%(asctime)s %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'))
  17. logger.addHandler(rf)
  18. logger.addHandler(handler)
  19. logging.root = logger
  20. if __name__ == '__main__':
  21. # read local config
  22. config_path = 'config.json'
  23. conf = config.Config(config_path)
  24. local_config = conf.read_local_config()
  25. remote_config = conf.read_redis_config()
  26. # init logger
  27. log_path = local_config.get('log_path', 'elec_monitor.log')
  28. format_logger(log_path)
  29. logging.info('[MAIN] local config: %s', local_config)
  30. logging.info('[MAIN] elec monitor init logger successful')
  31. # timing server start
  32. timing_tread = threading.Thread(target=timing.run, name='elec_timing_server')
  33. timing_tread.setDaemon(True)
  34. timing_tread.start()
  35. logging.info('[MAIN] elec monitor init timing server successful')
  36. # load redis config to init upload server
  37. remote_host_config = conf.load_remote_host_config()
  38. if remote_host_config is None:
  39. logging.info('[MAIN] elec monitor load remote host successful but None')
  40. exit(0)
  41. logging.info('[MAIN] elec monitor load remote host successful: %s', remote_host_config)
  42. # upload server start
  43. logging.info('[MAIN] elec monitor will listen to upload server')
  44. upload.defaultHandler = upload.Handle(
  45. local_config['upload_max_length'],
  46. local_config['upload_temp_file'],
  47. local_config['upload_official_dir'],
  48. local_config['upload_rubbish'],
  49. remote_host_config['host'],
  50. remote_host_config['port'],
  51. remote_host_config['username'],
  52. remote_host_config['password'],
  53. remote_host_config['file_path'],
  54. local_config['upload_interval'],
  55. )
  56. upload.listen()