-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstations.py
277 lines (247 loc) · 10.3 KB
/
stations.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from hd import HD
from os import path
from math import isclose
from time import sleep, time
from glob import glob
from adb import Template
import numpy as np
from logger import MyLogger,logging
class Stations(dict):
def __init__(self, device, tasklist):
self.log=MyLogger('Stations', LOG_LEVEL=logging.INFO)
self.device=device
self.tasklist=tasklist
self.data=[]
self.settings=[]
self.icons=[ [],[], #location of recipes in relation to info-button
[[-543, -13],[-702, 90] ], #2
[[-560, -95],[-715, 15],[-785, 165] ], #3
[[-500,-145],[-665,-77],[-800, 50],[-845,205] ], #4
[[-565, -15],[-700,100],[-605,-170],[-800,-75],[-915,110] ] ] #5
self.checkListData()
def checkListData(self):
try:
settings=HD.loadJSON('stations')
resources=HD.loadJSON('resources')
if not (len(resources) and "stations" in resources and len(settings)):
raise Exception("Something is wrong with the loading of JSONs")
if resources['stations']==self.data and settings==self.settings:
return
self.log.debug("updating stations")
self.data=resources['stations']
self.settings=settings
self.updateListData()
except Exception as e:
self.log.error(e)
def updateListData(self):
try:
count={}
for station in self.values():
station.enabled=False
for newstation in self.data:
station=newstation['station']
self.log.debug(station)
if station not in count:
count[station]=0
count[station]+=1
name=f"{station} [{count[station]}]"
if station in self.settings:
data=self.settings[station]
products=data['recipes'].keys()
num_recipes=len(data['recipes'])
icons=self.icons[num_recipes]
data['icons']=dict(zip(products,icons))
if name not in self:
self[name]=Station(self.device, self.tasklist, station)
data['log']=MyLogger(name)
data['name']=name
data['enabled']=True
data['position']=HD.getPos(newstation['location'])
data['templates']=HD.loadTemplates('stations',station)
data['update']=self.checkListData
for key,value in data.items():
setattr(self[name], key, value)
self[name].setRecipes()
except Exception as e:
self.log.error(e)
def getList(self):
data=[]
for name,station in self.items():
tasks=self.tasklist.find(name)
totaltime=self.tasklist.printtime(int(station.getTotalTime()*60))
data.append({"name":name, "jobs": len(station.jobs), "time":totaltime, "queue":station.queue, "tasks":tasks})
return data
class Station(HD):
def __init__(self, device, tasklist, station):
HD.__init__(self, device, tasklist, station)
self.base=[-490,305]
self.products={}
self.queue=[]
self.jobs=[]
@staticmethod
def remove_lowest(list):
if len(list):
lowest=min(list)
for idx, value in enumerate(list):
if value==lowest:
break
list.pop(idx)
def setRecipes(self):
self.log.debug(f"set recipes ({len(self.recipes)})")
for product,recipe in self.recipes.items():
self.products[product]=Recipe(self,product,recipe)
#get time of last job in minutes
def getJobTime(self):
self.log.debug(f"\n {self.name}: getjobtime")
self.log.debug(f"jobs: {self.jobs}")
if len(self.jobs):
waittime=max(self.jobs)-int(time())
self.log.debug(f"waittime: {waittime}")
if waittime > 0:
wait=int(waittime/60)
self.log.debug(f"wait: {wait}")
return wait
return 0
def getTotalTime(self):
if not self.enabled:
return False
self.log.debug(f"\n {self.name}: gettotaltime")
waittime=self.getWaitTime()+self.getJobTime()+0.1
self.log.debug(f"{self.name}: waittime: {waittime}")
for product in self.queue:
waittime+=self.products[product].cooktime
self.log.debug(f"{self.name}: total waittime: {waittime}")
return waittime
def orderJobs(self):
self.log.debug("\n ordering")
result={}
for product in self.queue:
result[product]=self.products[product].cooktime
self.queue=sorted(result, key=result.get)
self.log.debug(self.queue)
def checkJobs(self):
self.log.debug(f"\n checking jobs for {self.name}")
wait=self.getWaitTime()+0.25
self.log.debug(f"queue: {self.queue}")
if len(self.queue) and len(self.jobs)<2:
self.log.debug('adding task')
product=self.queue.pop(0)
self.log.debug(f"{self.name} new job: {product} - jobs: {self.queue}")
self.setWaittime(wait)
self.tasklist.addtask(wait, f"{self.name} - create: {product}", self.image, self.products[product].create)
self.jobs.append(wait*60+int(time()))
self.log.debug(self.jobs)
for job in self.jobs:
self.log.debug(f"time in seconds: {job-int(time())}\n")
def collect(self,product,amount=1):
x,y=[800,450]
try:
self.log.debug(f"{self.name} collect {product}")
if not self.reset_screen():
raise Exception("Error on resetting Screen")
self.move_to()
if self.check_plus():
self.click_green()
location=self.device.locate_item(self.templates, self.threshold, one=True)
if not len(location):
raise Exception("Could not locate station")
x,y=location
self.device.tap(x,y)
sleep(.2)
self.check_cross()
self.tasklist.removeSchedule(product,amount)
self.remove_lowest(self.jobs)
self.orderJobs()
self.checkJobs()
except Exception as e:
self.log.error(f"Could not collect {product}")
self.log.error(e)
info=f"{self.name} - retry to collect: {product}"
self.log.debug(info)
self.tasklist.addtask(1,info, self.image, self.products[product].start_collect)
finally:
self.exit(x,y)
def start(self,product,cooktime):
try:
self.log.debug(f"{self.name} start {product}")
icon=self.icons[product]
recipe=self.products[product]
x,y=[0,0]
if not self.reset_screen():
raise Exception("Error on resetting Screen")
self.move_to()
if self.check_diamond():
self.click_green()
self.check_moved()
location=self.device.locate_item(self.templates, self.threshold, one=True)
if not len(location):
raise Exception(f"Could not find {self.name}")
self.log.debug(f'found: {self.name}')
x,y=location
self.device.tap(x,y)
sleep(.5)
self.log.debug(f"should be open now")
info_button=self.device.locate_item(self.info, 0.85, one=True)
if not len(info_button):
raise Exception(f"Could open {self.name}")
bas=np.add(info_button,self.base)
ico=np.add(info_button, icon)
self.device.swipe(ico[0],ico[1],bas[0],bas[1],300)
self.remove_lowest(self.jobs)
sleep(.1)
if self.check_cross(): #could not find ingredients, wait 2 minutes
self.log.debug('not enough ingredients')
list=[]
for ingredient in recipe.ingredients:
if self.onscreen(ingredient):
list.append(ingredient)
if not len(list):
list=recipe.ingredients.keys()
for product in list:
self.log.debug(f"Request: {product}")
self.tasklist.checkWish(product, recipe.ingredients[product])
self.setWaittime(2)
recipe.addJob(error=True)
return #exit function to reorder next item
self.setWaittime(0)
jobtime=self.getJobTime()+cooktime
self.log.debug(f"new jobtime: {jobtime}")
info=f'{self.name} - collect: {product}'
self.log.debug(info)
self.tasklist.addtask(jobtime+0.5, info, self.image, recipe.start_collect)
self.jobs.append(jobtime*60+int(time()))
self.log.debug(f"jobs: {self.jobs}")
except Exception as e:
#something went wrong, try again in one minute
self.log.error('something went wrong')
self.log.error(e)
self.log.debug('retry in 1 minute')
self.tasklist.addtask(1, f'{self.name} - create: {product}', self.image, recipe.create)
finally:
self.exit(x,y)
def exit(self,x=800,y=450):
self.device.tap(x-80,y-40)
self.move_from()
class Recipe():
def __init__(self, station, product, recipe):
self.station=station
self.product=product
for key,value in recipe.items():
setattr(self, key, value)
station.tasklist.addProduct(product, self.addJob, station.getTotalTime)
def addJob(self,error=False):
if not self.station.enabled:
return 0
self.station.queue.append(self.product)
if not error:
self.requestIngredients()
self.station.orderJobs()
self.station.checkJobs()
return self.amount
def requestIngredients(self):
for ingredient,amount in self.ingredients.items():
self.station.tasklist.addWish(ingredient, amount)
def create(self):
self.station.start(self.product, self.cooktime)
def start_collect(self):
self.station.collect(self.product, self.amount)