-
Notifications
You must be signed in to change notification settings - Fork 1
/
tr.py
executable file
·545 lines (515 loc) · 12.4 KB
/
tr.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#!/usr/bin/python
import re,sys,redis,json,getopt,random,urllib2
import time,uuid
MPH=1.60934
WAKEINTERVAL=3
alreadyPicked=[]
liveScheds=[]
maps=[]
segMaps=[]
projectDir=""
segmentsDir="segments/"
foundSch=False
conf={}
#scheduleFile=""
redisDB=0
try:
opts, args = getopt.getopt(sys.argv[1:], "h:m", ["help", "instance="])
except getopt.GetoptError as err:
print(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
for o, a in opts:
if o in ("--instance"):
redisDB = int(a)
# elif o in ("--schedule"):
# foundSch=True
# scheduleFile = a
else:
assert False, "option unknown"
sys.exit(2)
#if foundSch==False:
# print "ERROR. Schedule needed."
# sys.exit(2)
r=redis.StrictRedis(host='localhost', port=6379, db=redisDB)
try:
answ=r.client_list()
except redis.ConnectionError:
print "FATAL: cannot connect to redis."
sys.exit()
routeName=r.get("routeName")
projectDir=routeName+'/'
def getOffset(seg,name):
global unitsFactor
f=open(projectDir+"segments/"+seg+"/SIGs.txt","r")
ssf=f.readlines()
ss=[]
f.close()
cnt=0
for s in ssf:
s=s.split(" ")
if s[1]==name:
return float(s[0])*1000.0*unitsFactor
return None
def getSegLenAndType(seg):
global unitsFactor
f=open(projectDir+"segments/"+seg+"/SIGs.txt","r")
ssf=f.readlines()
ss=[]
f.close()
cnt=0
for s in ssf:
if cnt==0:
s=s.split(" ")
if len(s)>2:
firstS=s[2]
else:
firstS='1'
firstN=s[1]
if len(s)>3:
firstB=s[3]
cnt=cnt+1
s=s.split(" ")
if len(s)>2:
lastS=s[2]
else:
lastS='1'
lastN=s[1]
# print seg+" "+str(firstS)+"->"+str(lastS)
lnt={}
lnt['length']=1000.0*float(s[0])*unitsFactor
typeFound=False
if firstS=='2':
if lastS=='2':
typeFound=True
lnt['type']='Main'
lnt['offset']=0.0
elif lastS=='4C':
typeFound=True
lnt['type']='BranchConv'
segMain=s[3].split(":")
if segMain[0]=='Main':
lnt['offset']=getOffset(segMain[1],lastN)
else:
print "FATAL: syntax error in signal "+lastS+":"+lastN
sys.exit()
elif firstS=='4D':
if lastS=='2':
typeFound=True
lnt['type']='BranchDiv'
segMain=firstB.split(":")
if segMain[0]=='Main':
lnt['offset']=getOffset(segMain[1],firstN)
else:
print "FATAL: syntax error in signal "+firstS+":"+firstN
sys.exit()
elif lastS=='1':
typeFound=True
lnt['type']='GarageExit'
lnt['offset']=0.0
elif lastS=='4C':
typeFound=True
lnt['type']='Siding'
lnt['offset']=0.0
elif firstS=='1':
if lastS=='4C':
typeFound=True
lnt['type']='GarageFeed'
lnt['offset']=0.0
elif lastS=='1':
typeFound=True
lnt['type']='OneOff'
lnt['offset']=0.0
if 'type' in lnt:
return lnt
print "FATAL: unknown segment type: "+seg+" firstS:"+firstS+" lastS:"+lastS
sys.exit()
return None
def confMAPs():
f=open(projectDir+"maps.txt","r")
ssf=f.readlines()
ss=[]
f.close()
cnt=0
aL=[]
for s in ssf:
if s[0]!='#':
s=s.rstrip().split(":")
aS={}
ss=s[1].split(",")
aS[s[0]]=ss
aL.append(aS)
return aL
def initSEGmap(sm):
global segMaps
global segmentsList
global liveScheds
schml=[]
schm={}
for ll in liveScheds:
if ll['segment']==sm:
lli={}
lli['x']=ll['x']
lli['name']=ll['name']
schml.append(lli)
schm['points']=schml
lnt=getSegLenAndType(sm)
schm['length']=lnt['length']
schm['type']=lnt['type']
schm['offset']=lnt['offset']
# print schm
return schm
def initMAPs():
global maps
global segMaps
global segmentsList
# stage 1: identify maps
mapsconf=confMAPs()
# print mapsconf
# stage 2: generate segment maps
for se in segmentsList:
found=False
for sm in segMaps:
if sm['segment']==se:
found=True
if found==False:
smp={}
smp['map']=initSEGmap(se)
smp['segment']=se
segMaps.append(smp)
# print segMaps
#stage 3: generate maps
lmaps=[]
for m in mapsconf:
hasMain=False
keys=m.keys()
if keys[0] is not None:
cnt=0
amap={}
amap['map']=keys[0]
amap['points']=[]
for mm in m[keys[0]]:
for sm in segMaps:
if sm['segment']==mm:
if ((hasMain==False) and (sm['map']['type']=='Main')):
hasMain=True
# print "MAIN"
for p in sm['map']['points']:
amap['points'].append(p)
# print amap['points']
# print "/MAIN"
elif ((hasMain==True) and (sm['map']['type']=='Main')):
print "FATAL: map has two main branches"
sys.exit()
elif sm['map']['type']=='BranchDiv':
# print "BDIV"
for p in sm['map']['points']:
p1={}
p1['x']=p['x']+sm['map']['offset']
p1['name']=p['name']
amap['points'].append(p1)
# print "/BDIV"
elif sm['map']['type']=='BranchConv':
# print "BCONV"
for p in sm['map']['points']:
p1={}
p1['x']=p['x']+sm['map']['offset']-sm['map']['length']
p1['name']=p['name']
amap['points'].append(p1)
# print "/BCONV"
# print 'before lambda:'
# print amap['points']
amap['points'].sort(key=lambda l: l['x'])
# print 'after lambda:'
# print amap['points']
lmaps.append(amap)
# print "LMAPS:"
# print lmaps
# print "/LMAPS"
return lmaps
def distances(m):
global alreadyPicked
oldp={}
dl=[]
for p in m['points']:
if 'name' in oldp:
found=False
for a in alreadyPicked:
if a==oldp['name']:
found=True
if found==False:
d={}
d['distance']=p['x']-oldp['x']
d['from']=oldp['name']
d['to']=p['name']
# print d
dl.append(d)
oldp=p
dl.sort(key=lambda l: l['distance'])
return dl
def initSRVs(s_type):
f=open(projectDir+"services.txt","r")
ssf=f.readlines()
ss=[]
f.close()
cnt=0
if s_type=='exit':
s_char='-'
elif s_type=='standard':
s_char='+'
else:
return []
a=[]
for s in ssf:
aS={}
if (s[0]==s_char):
s=s.rstrip().split(" ")
aS['name']=s[1]
aDivs=[]
if s_char=='+':
ss=s[2].split(":")
if ss[0]!='TpH':
print "FATAL: unknown service verb"
sys.exit()
ss=ss[1].split("-")
aS['min']=int(ss[0])
aS['max']=int(ss[1])
elif s_char=='-':
if s[2]!='OneOff':
print "FATAL: unknown service verb"
sys.exit()
for i in range(3,len(s)):
# print "s"+str(i)+": "+s[i]
ss=s[i].split(":")
aSS={}
aSS[ss[0]]=ss[1]
aDivs.append(aSS)
aS['divs']=aDivs
cnt=cnt+1
a.append(aS)
return a
def initSEGs():
f=open(projectDir+"segments.txt","r")
ssf=f.readlines()
ss=[]
f.close()
cnt=0
for s in ssf:
if (s[0]!='#'):
s=s.rstrip()
ss.append(s)
cnt=cnt+1
return ss
def initConfig():
f=open(projectDir+"routeConfig.txt","r")
ssf=f.readlines()
ss=[]
f.close()
cnt=0
for s in ssf:
if (s[0]!='#'):
s=s.rstrip().split(" ")
ss.append(s)
cnt=cnt+1
return ss
def initSchedules(seg,sFile):
global segs
f=open(projectDir+"/schedules/"+sFile,"r")
ssf=f.readlines()
ss=[]
f.close()
cnt=0
for s in ssf:
if (s[0]!='#'):
s=s.rstrip().split(" ")
ss.append(s[0])
cnt=cnt+1
return ss
def initSTAs(seg):
global segs
f=open(projectDir+segmentsDir+seg+"/STAs.txt","r")
ssf=f.readlines()
ss=[]
f.close()
cnt=0
for s in ssf:
if (s[0]!='#'):
s=s.rstrip().split(" ")
ss.append(s)
cnt=cnt+1
return ss
def initSIGs(seg):
global segs
f=open(projectDir+segmentsDir+seg+"/SIGs.txt","r")
ssf=f.readlines()
ss=[]
f.close()
cnt=0
for s in ssf:
if (s[0]!='#'):
s=s.rstrip().split(" ")
ss.append(s)
cnt=cnt+1
return ss
def getState():
global liveScheds
ll=[]
rs=urllib2.urlopen('http://127.0.0.1:4999/v1/list/schedules')
sched=json.loads(rs.read())
for v in sched:
state=r.hgetall('state:'+v)
state=dict(state)
s1={}
if 'segment' in state:
s1['name']=v
s1['inSta']=state['inSta']
s1['atSig']=state['atSig']
s1['coasting']=state['coasting']
s1['sigBrake']=state['sigBrake']
s1['staBrake']=state['staBrake']
s1['segment']=state['segment']
s1['nextSTA']=state['nextSTA']
s1['nextSIG']=state['nextSIG']
s1['nextTIV']=state['nextTIV']
s1['service']=state['service']
s1['advSIGcol']=state['advSIGcol']
s1['x']=float(state['x'])
s1['v']=float(state['v'])
s1['pax']=float(state['pax'])
s1['maxPax']=float(state['maxPax'])
s1['maxVk']=float(state['maxVk'])
s1['units']=state['units']
ll.append(s1)
return ll
def getSegExits(seg,sig):
sis=initSIGs(seg)
exits=[]
cntEx=0
exPos=-1
for si in sis:
if len(si)>4:
if si[2]=='4D':
br=si[4].split(":")
if br[0]=='Branch':
chk=getSegLenAndType(br[1])
if 'type' in chk:
if chk['type']=='GarageExit':
order={}
order['type']=si[2]
order['name']=si[1]
bro=si[5].split(":")
if bro[0]=='BranchOrientation':
order['direction']=bro[1]
exPos=cntEx
cnt=0
sigPos=-1
for si2 in sis:
if si2[1]==sig:
sigPos=cnt
cnt=cnt+1
if sigPos>0:
# print "sch sig pos is:"+str(sigPos)
# print "div sig pos is:"+str(exPos)
if ((sigPos>exPos) or (sigPos<(exPos-2))): #train must not be too close to div
exits.append(order)
cntEx=cntEx+1
return exits
def getReverseSeg(seg):
chk=getSegLenAndType(seg)
if 'type' in chk:
if chk['type']=='Main':
sis=initSIGs(seg)
si=sis[-1][3].split(":")
if si[0]=='Reverse':
return si[1]
return None
def getExitsForSched(sname):
global liveScheds
for l in liveScheds:
if l['name']==sname:
break
if l is not None:
sseg=l['segment']
# print sname+" is in seg: "+sseg
exits=getSegExits(sseg,l['nextSIG'])
if len(exits)>0:
return exits
else:
altSeg=getReverseSeg(sseg)
if altSeg is not None:
exits=getSegExits(altSeg,l['nextSIG'])
if len(exits)>0:
return exits
return None
segmentsList=initSEGs()
confraw=initConfig()
unitsFactor=1.0
for aa in confraw:
if (aa[0]!="#"):
if (aa[0]=='units'):
conf['units']=aa[1]
if conf['units']=='imperial':
unitsFactor=MPH
downScaleQty=4
candidatesQty=downScaleQty*2
liveScheds=getState()
candidatesQty=min(candidatesQty,len(liveScheds))
candidatesQty=max(candidatesQty,8)
selected=[]
maps=initMAPs()
simID=r.get("simID")
#exits=r.lrange(simID+":exits",0,-1)
loopPick=True
cnt=0
scalingEvent={}
scalingEvent['type']='downScale'
scalingEvent['desired']=downScaleQty
scalingEvent['id']=str(uuid.uuid4())
scalingEvent['time']=r.get("elapsed")
scalingEvent['simID']=simID
while loopPick==True:
# print "ITERATION "+str(cnt)
for m in maps:
dl=distances(m)
# print dl
if candidatesQty>0:
alreadyPicked.append(dl[0]['from'])
candidatesQty=candidatesQty-1
else:
loopPick=False
cnt=cnt+1
print alreadyPicked
cnt=0
for a in alreadyPicked:
exits=getExitsForSched(a)
if exits is not None:
cnt=cnt+1
candidate={}
candidate['name']=a
# candidate['order']=exits[0]['name']+':'+exits[0]['direction']
candidate['sigName']=exits[0]['name']
candidate['sigDir']=exits[0]['direction']
if cnt<=downScaleQty:
selected.append(candidate)
print selected
scalingEvent['set']=len(selected)
print scalingEvent
for e in selected:
rs=urllib2.urlopen('http://127.0.0.1:4999/v1/update/schedule/'+e['name']+'/'+e['sigName']+'/'+e['sigDir']+'/'+scalingEvent['time'])
sched=json.loads(rs.read())
print "RES:"
print sched
def mainLoop():
global liveScheds
global elapsed
global srvs
while True:
elapsed=r.get("elapsed")
if elapsed is not None:
elapsed=int(elapsed)
liveScheds=getState()
print elapsed
else:
print "lost connection to redis...retrying..."
time.sleep(WAKEINTERVAL)
#mainLoop()