forked from mzsmakr/PGSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raidscan.py
171 lines (144 loc) · 5.86 KB
/
raidscan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import sys
import cv2
from sys import argv
import datetime
from logging import basicConfig, getLogger, FileHandler, StreamHandler, DEBUG, INFO, ERROR, Formatter
import time
import database
import asyncio
import raidnearby
import findfort
import devicecontroller
import crop
import os
import healthcheck
import concurrent.futures
from multiprocessing import Process
from pathlib import Path
from shapely.geometry import Polygon, Point
from downloadfortimg import download_img
from devicecontroller import DBFort
import importlib
import signal
LOG = getLogger('')
class RaidScan:
def __init__(self):
if len(argv) >= 2:
self.config = importlib.import_module(str(argv[1]))
else:
self.config = importlib.import_module('config')
self.all_forts_inside = []
if self.config.SCAN_AREA is not None:
LOG.info('Scan-Area is set! Getting Forts...')
session = database.Session()
all_forts = database.get_forts(session)
all_forts_to_download = []
session2 = database.Session()
for fort in all_forts:
if fort.lat is not None and fort.lon is not None and self.config.SCAN_AREA == 'All':
all_forts_to_download.append(fort.id)
self.all_forts_inside.append(DBFort(fort.id, fort.lat, fort.lon, 0))
continue
if fort.lat is not None and fort.lon is not None and self.config.SCAN_AREA.contains(Point(fort.lat, fort.lon)):
self.all_forts_inside.append(DBFort(fort.id, fort.lat, fort.lon, 0))
if fort.id not in all_forts_to_download:
all_forts_to_download.append(fort.id)
nearby_ids = database.get_fort_ids_within_range(session2, all_forts, 800, fort.lat, fort.lon)
for fort_id in nearby_ids:
if fort_id not in all_forts_to_download:
all_forts_to_download.append(fort_id)
session2.close()
LOG.info('Found {} Gyms Gyms in Scan-Area'.format(len(self.all_forts_inside)))
for fort in all_forts:
if fort.id in all_forts_to_download:
image_file = Path(os.getcwd() + '/url_img/' + str(fort.id) + '.jpg')
if not os.path.isfile(image_file) and fort.url is not None:
LOG.info(
'Found gym in Scan-Area without stored image! Downloading image for {}'.format(fort.id))
download_img(str(fort.url), str(image_file))
if self.config.DEVICE_LIST is None:
LOG.error('SCAN_AREA set but DEVICE_LIST is empty! Skipping')
session.commit()
session.close()
time.sleep(1)
if self.config.ENABLE_NEARBY:
for i in range(self.config.NEARBY_PROCESSES):
self.restart_nearby(i)
if self.config.ENABLE_CROP:
for i in range(self.config.CROP_PROCESSES):
self.restart_crop(i)
if self.config.ENABLE_FINDFORT:
for i in range(self.config.FINDFORT_PROCESSES):
self.restart_findfort(i)
if self.config.ENABLE_CONTROL and self.config.SCAN_AREA is not None and self.config.DEVICE_LIST is not None:
self.restart_devicecontroller()
self.restart_healthcheck()
def restart_crop(self, id):
time.sleep(0.1)
try:
crop_obj = crop.Crop()
except KeyboardInterrupt:
os.killpg(0, signal.SIGINT)
sys.exit(1)
except Exception as e:
LOG.error('Failed to init Crop: {}'.format(e))
self.restart_crop(id)
return
crop_process = Process(target=crop_obj.crop_task, args=(self,id,))
crop_process.start()
def restart_nearby(self, id):
time.sleep(0.1)
try:
raid_nearby = raidnearby.RaidNearby(id)
except KeyboardInterrupt:
os.killpg(0, signal.SIGINT)
sys.exit(1)
except Exception as e:
LOG.error('Failed to init RaidNearby: {}'.format(e))
self.restart_nearby(id)
return
rn_process = Process(target=raid_nearby.main, args=(self,id,))
rn_process.start()
def restart_findfort(self, id):
time.sleep(0.1)
try:
find_fort = findfort.FindFort()
except KeyboardInterrupt:
os.killpg(0, signal.SIGINT)
sys.exit(1)
except Exception as e:
LOG.error('Failed to init FindFort: {}'.format(e))
self.restart_findfort(id)
return
ff_process = Process(target=find_fort.findfort_main, args=(self,id,))
ff_process.start()
def restart_devicecontroller(self):
time.sleep(0.1)
try:
device_controller = devicecontroller.DeviceController(self.all_forts_inside, self.config.DEVICE_LIST)
except KeyboardInterrupt:
os.killpg(0, signal.SIGINT)
sys.exit(1)
except Exception as e:
LOG.error('Failed to init DeviceController: {}'.format(e))
self.restart_devicecontroller()
return
dc_process = Process(target=device_controller.devicecontroller_main, args=(self,))
dc_process.start()
def restart_healthcheck(self):
time.sleep(0.1)
try:
health_check = healthcheck.HealthCheck()
except KeyboardInterrupt:
os.killpg(0, signal.SIGINT)
sys.exit(1)
except Exception as e:
LOG.error('Failed to init HealthCheck: {}'.format(e))
self.restart_healthcheck()
return
hc_process = Process(target=health_check.healthcheck_main, args=(self,))
hc_process.start()
if __name__ == '__main__':
os.setpgrp()
cv2.setNumThreads(0)
main = RaidScan()