Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. import sys
  2. import cv2
  3. from sys import argv
  4. import datetime
  5. from logging import basicConfig, getLogger, FileHandler, StreamHandler, DEBUG, INFO, ERROR, Formatter
  6. import time
  7. import database
  8. import asyncio
  9. import raidnearby
  10. import findfort
  11. import devicecontroller
  12. import crop
  13. import os
  14. import concurrent.futures
  15. from multiprocessing import Process
  16. from pathlib import Path
  17. from shapely.geometry import Polygon, Point
  18. from downloadfortimg import download_img
  19. from devicecontroller import DBFort
  20. import importlib
  21.  
  22. LOG = getLogger('')
  23.  
  24. class RaidScan:
  25.  
  26. def __init__(self):
  27.  
  28. if len(argv) >= 2:
  29. self.config = importlib.import_module(str(argv[1]))
  30. else:
  31. self.config = importlib.import_module('config')
  32.  
  33. self.all_forts_inside = []
  34.  
  35. if self.config.SCAN_AREA is not None:
  36.  
  37. LOG.info('Scan-Area is set! Getting Forts...')
  38. session = database.Session()
  39. all_forts = database.get_forts(session)
  40.  
  41. all_forts_to_download = []
  42.  
  43. session2 = database.Session()
  44.  
  45. for fort in all_forts:
  46. if fort.lat is not None and fort.lon is not None and self.config.SCAN_AREA.contains(Point(fort.lat, fort.lon)):
  47. self.all_forts_inside.append(DBFort(fort.id, fort.lat, fort.lon))
  48.  
  49. if fort.id not in all_forts_to_download:
  50. all_forts_to_download.append(fort.id)
  51.  
  52. nearby_ids = database.get_fort_ids_within_range(session2, all_forts, 800, fort.lat, fort.lon)
  53. for fort_id in nearby_ids:
  54. if fort_id not in all_forts_to_download:
  55. all_forts_to_download.append(fort_id)
  56.  
  57. session2.close()
  58.  
  59. LOG.info('Found {} Gyms Gyms in Scan-Area'.format(len(self.all_forts_inside)))
  60.  
  61. for fort in all_forts:
  62. if fort.id in all_forts_to_download:
  63. image_file = Path(os.getcwd() + '/url_img/' + str(fort.id) + '.jpg')
  64. if not os.path.isfile(image_file) and fort.url is not None:
  65. LOG.info(
  66. 'Found gym in Scan-Area without stored image! Downloading image for {}'.format(fort.id))
  67. download_img(str(fort.url), str(image_file))
  68.  
  69. if self.config.DEVICE_LIST is None:
  70. LOG.error('SCAN_AREA set but DEVICE_LIST is empty! Skipping')
  71.  
  72. session.commit()
  73. session.close()
  74. time.sleep(1)
  75.  
  76. if self.config.ENABLE_NEARBY:
  77. for i in range(self.config.NEARBY_PROCESSES):
  78. self.restart_nearby(i)
  79. if self.config.ENABLE_CROP:
  80. for i in range(self.config.CROP_PROCESSES):
  81. self.restart_crop(i)
  82. if self.config.ENABLE_FINDFORT:
  83. for i in range(self.config.FINDFORT_PROCESSES):
  84. self.restart_findfort(i)
  85. if self.config.ENABLE_CONTROL and self.config.SCAN_AREA is not None and self.config.DEVICE_LIST is not None:
  86. self.restart_devicecontroller()
  87.  
  88. def restart_crop(self, id):
  89. time.sleep(1)
  90. try:
  91. crop_obj = crop.Crop()
  92. except KeyboardInterrupt:
  93. sys.exit(1)
  94. except Exception as e:
  95. LOG.error('Failed to init Crop: {}'.format(e))
  96. self.restart_crop(id)
  97. return
  98. crop_process = Process(target=crop_obj.crop_task, args=(self,id,))
  99. crop_process.start()
  100.  
  101. def restart_nearby(self, id):
  102. time.sleep(1)
  103. try:
  104. raid_nearby = raidnearby.RaidNearby()
  105. except KeyboardInterrupt:
  106. sys.exit(1)
  107. except Exception as e:
  108. LOG.error('Failed to init RaidNearby: {}'.format(e))
  109. self.restart_nearby(id)
  110. return
  111. rn_process = Process(target=raid_nearby.main, args=(self,id,))
  112. rn_process.start()
  113.  
  114. def restart_findfort(self, id):
  115. time.sleep(1)
  116. try:
  117. find_fort = findfort.FindFort()
  118. except KeyboardInterrupt:
  119. sys.exit(1)
  120. except Exception as e:
  121. LOG.error('Failed to init FindFort: {}'.format(e))
  122. self.restart_findfort(id)
  123. return
  124. ff_process = Process(target=find_fort.findfort_main, args=(self,id,))
  125. ff_process.start()
  126.  
  127. def restart_devicecontroller(self):
  128. time.sleep(1)
  129. try:
  130. device_controller = devicecontroller.DeviceController(self.all_forts_inside, self.config.DEVICE_LIST)
  131. except KeyboardInterrupt:
  132. sys.exit(1)
  133. except Exception as e:
  134. LOG.error('Failed to init DeviceController: {}'.format(e))
  135. self.restart_devicecontroller()
  136. return
  137. dc_process = Process(target=device_controller.devicecontroller_main, args=(self,))
  138. dc_process.start()
  139.  
  140. if __name__ == '__main__':
  141. cv2.setNumThreads(0)
  142. main = RaidScan()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement