-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebaseCommunicator.py
More file actions
133 lines (114 loc) · 5.54 KB
/
firebaseCommunicator.py
File metadata and controls
133 lines (114 loc) · 5.54 KB
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
#Last Updated: 12/13/17
import utils
import json
import datetime
import numpy as np
import pyrebase
class PyrebaseCommunicator(object)
'''docstring for PyrebaseCommunicator'''
def __init__(self):
super(PyrebaseCommunicator, self).__init__()
self.JSONmatches = []
self.JSONteams = []
self.url = 'error-server'
config = {
'apiKey': 'mykey',
'authDomain': self.url + '.firebaseapp.com',
'databaseURL': 'https://' + self.url + '.firebaseio.com/',
'storageBucket': self.url + '.appspot.com'
}
app = pyrebase.initialize_app(config)
self.firebase = app.database()
self.fbStorage = app.storage()
#Turns inputted team (class) object into dict and puts on firebase
def updateFirebaseWithTeam(self, team):
print(str(team.number) + ',',)
teamDict = utils.makeDictFromTeam(team)
self.firebase.child('Teams').child(team.number).set(teamDict)
#Turns inputted match (class) object into dict, condenses team numbers, and puts on firebase
def updateFirebaseWithMatch(self, match):
print(str(match.number) + ',',)
matchDict = utils.makeDictFromMatch(match)
matchDict['blueAllianceTeamNumbers'] = map(lambda n: int(n.replace('frc', '')), matchDict['blueAllianceTeamNumbers'])
matchDict['redAllianceTeamNumbers'] = map(lambda n: int(n.replace('frc', '')), matchDict['redAllianceTeamNumbers'])
self.firebase.child('Matches').child(match.number).set(matchDict)
#Turns inputted TIMD (class) object into dict and puts on firebase
def updateFirebaseWithTIMD(self, timd):
timdDict = utils.makeDictFromTIMD(timd)
print(str(timd.teamNumber) + 'Q' + str(timd.matchNumber) + ',' ,)
self.firebase.child('TeamInMatchDatas').child(str(timd.teamNumber) + 'Q' + str(timd.matchNumber)).set(timdDict)
#Turns inputted CalculatedTeamData (class) object into dict and puts on firebase
def addCalculatedTeamDataToFirebase(self, team):
calculatedTeamDataDict = utils.makeDictFromCalculatedData(team.calculatedData)
FBLocation = str(team.number) + '/calculatedData/'
return {FBLocation : calculatedTeamDataDict}
#Turns inputted Calculated TIMD (class) object into dict and puts on firebase
def addCalculatedTIMDataToFirebase(self, timd):
calculatedTIMDataDict = utils.makeDictFromCalculatedData(timd.calculatedData)
FBLocation = str(timd.teamNumber) + 'Q' + str(timd.matchNumber) + '/calculatedData/'
return {FBLocation : calculatedTIMDataDict}
#Turns inputted CalculatedMatchData (class) object into dict and puts on firebase
def addCalculatedMatchDataToFirebase(self, match):
calculatedMatchDataDict = utils.makeDictFromCalculatedData(match.calculatedData)
FBLocation = str(match.number) + '/calculatedData/'
return {FBLocation : calculatedMatchDataDict}
#Adds calculated data for each inputted team to firebase
def addCalculatedTeamDatasToFirebase(self, teams):
firebaseDict = {}
[firebaseDict.update(self.addCalculatedTeamDataToFirebase(team)) for team in teams]
print('> Uploading Teams to Firebase...')
self.firebase.child('Teams').update(firebaseDict)
#Adds calculated data for each inputted match to firebase
def addCalculatedMatchDatasToFirebase(self, matches):
firebaseDict = {}
[firebaseDict.update(self.addCalculatedMatchDataToFirebase(match)) for match in matches]
print('> Uploading Matches to Firebase...')
self.firebase.child('Matches').update(firebaseDict)
#Adds calculated data for each inputted TIMD to firebase
def addCalculatedTIMDatasToFirebase(self, timds):
firebaseDict = {}
[firebaseDict.update(self.addCalculatedTIMDataToFirebase(timd)) for timd in timds]
print('> Uploading TIMDs to Firebase...')
self.firebase.child('TeamInMatchDatas').update(firebaseDict)
#Puts all teams from local JSON list (probably from TBA) onto firebase
def addTeamsToFirebase(self):
print('\nDoing Teams...')
map(lambda t: self.updateFirebaseWithTeam(utils.setDataForTeam(t)), self.JSONteams)
#Puts all qual matches from local JSON list (probably from TBA) onto firebase
def addMatchesToFirebase(self):
print('\nDoing Matches...')
matches = filter(lambda m: m['comp_level'] == 'qm', self.JSONmatches)
map(lambda m: self.updateFirebaseWithMatch(utils.setDataForMatch(m)), matches)
def addTIMDsToFirebase(self, matches):
#gets all team numbers in a given match and updates firebase with the corresponding TIMD
print('\nDoing TIMDs...')
timdFunc = lambda t, m: self.updateFirebaseWithTIMD(utils.makeTIMDFromTeamNumberAndMatchNumber(t, m.number))
addTIMD = lambda m: map(lambda t: timdFunc(t, m), m.redAllianceTeamNumbers + m.blueAllianceTeamNumbers)
map(addTIMD, matches)
#Puts all of firebase onto a local JSON
def cacheFirebase(self):
try:
data = dict(self.firebase.get().val())
now = str(datetime.datetime.now())
with open('./CachedFirebases/' + now + '.json', 'w+') as f:
json.dump(data, f)
except:
pass
def addCompInfoToFirebase(self):
#Doing these keys manually so less clicking in firebase is better and because just easier
self.firebase.child('code').set('cama')
#Empties everything from firebase
def wipeDatabase(self):
map(utils.printWarningForSeconds, range(10, 0, -1))
print('\nWARNING: Wiping Firebase...')
self.firebase.remove()
def getPythonObjectForFirebaseDataAtLocation(self, location):
return utils.makeASCIIFromJSON(self.firebase.child(location).get().val())
#Stores inputted file (data export) on firebase
def sendExport(self, fileName):
now = str(datetime.datetime.now())
filePath = './' + fileName
self.fbStorage.child('Exports').child(fileName).put(filePath)
#Puts current match number on firebase as 1
def addCurrentMatchToFirebase(self):
self.firebase.child('currentMatchNum').set(1)