-
Notifications
You must be signed in to change notification settings - Fork 2
/
Db_Actions.py
189 lines (152 loc) · 6.53 KB
/
Db_Actions.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
import sql as Connection
import helpers as h
from sqlalchemy.sql import func
from datetime import date
def reset_to_no_project():
Session = Connection.sessionmaker(bind=Connection.engine)
sqlSession = Session()
other_settings_query = sqlSession.query(Connection.OtherSettings).filter_by(ID=1).first()
if other_settings_query is not None:
other_settings_query.CurrentProjectID = -1
sqlSession.commit()
sqlSession.close()
def generate_uuid(capacity):
Session = Connection.sessionmaker(bind=Connection.engine)
sqlSession = Session()
other_settings_query = sqlSession.query(Connection.OtherSettings).filter_by(ID=1).first()
uuids = []
if other_settings_query is not None:
if other_settings_query.CurrentProjectID != -1:
proj_id = other_settings_query.CurrentProjectID
project_query = sqlSession.query(Connection.Projects).filter_by(ID=proj_id).first()
if project_query is not None:
project_name = project_query.Name
if project_query.LastCellNumber is None:
# project_query.LastCellNumber = 16
last_cell_id = 0
sqlSession.commit()
else:
last_cell_id = project_query.LastCellNumber
project_query.LastCellNumber += 1
sqlSession.commit()
today = str(date.today())
today = today.replace("-", "")
print("Today's date:", today )
uuid = "D" + today + "-S" + str(last_cell_id + 1).zfill(5) + "-C" + str(capacity)
return uuid
else:
# h.showdialog("No project set", "Create new or open existing project")
return []
sqlSession.close()
else:
return []
def get_current_project_id():
Session = Connection.sessionmaker(bind=Connection.engine)
sqlSession = Session()
other_settings_query = sqlSession.query(Connection.OtherSettings).filter_by(ID=1).first()
project_id = other_settings_query.CurrentProjectID
project_query = sqlSession.query(Connection.Projects).filter_by(ID=project_id).first()
project_name = project_query.Name
if project_id == -1:
h.showdialog("No project set", "Create new or open existing project")
sqlSession.close()
return None
sqlSession.close()
return project_id, project_name
def get_cells_info(min, max):
project_id, project_name = get_current_project_id()
print(project_id)
cells_info = []
if project_id:
Session = Connection.sessionmaker(bind=Connection.engine)
sqlSession = Session()
cells_query = sqlSession.query(Connection.Cells).filter_by(ProjectID=project_id)
cells_info = []
if cells_query is None:
sqlSession.close()
return []
project_query = sqlSession.query(Connection.Projects).filter_by(ID=project_id).first()
for cell in cells_query:
if (cell.Capacity >= min and cell.Capacity <= max) and cell.Available == "YES":
cell_data = dict()
cell_data["ProjectID"] = project_id
cell_data["ID"] = cell.ID
cell_data["ProjectName"] = project_query.Name
cell_data["UUID"] = cell.UUID
cell_data["Voltage"] = cell.Voltage
cell_data["Capacity"] = cell.Capacity
cell_data["ESR"] = cell.ESR
cell_data["CellType"] = cell.CellType
cell_data["AddedDate"] = cell.AddedDate.strftime("%Y-%m-%d %H:%M:%S")
cell_data["Available"] = cell.Available
cell_data["Device"] = cell.Device
cells_info.append(cell_data)
sqlSession.close()
return cells_info
def get_cell_by_capacity(capacity, already_added):
project_id = get_current_project_id()
Session = Connection.sessionmaker(bind=Connection.engine)
sqlSession = Session()
cells_query = sqlSession.query(Connection.Cells).filter_by(ProjectID=project_id, Capacity=capacity).first()
if cells_query is not None:
return cells_query.UUID
else:
return None
#
# for cell in cells_query:
# print("Got in here")
# print(cell.UUID)
# if cell.UUID not in already_added:
# return cell.UUID
# return None
def add_cell_to_db(celltype, projectID, voltage, capacity, esr, status, device):
Session = Connection.sessionmaker(bind=Connection.engine)
sqlSession = Session()
uuid = generate_uuid(capacity)
print(uuid)
cell_register = Connection.Cells(ProjectID=projectID, Voltage=voltage, Capacity=int(capacity),
ESR=esr, AddedDate=func.current_timestamp(), UUID=uuid,
Available="YES", CellType=celltype, Status=status, Device=device)
sqlSession.merge(cell_register)
sqlSession.commit()
sqlSession.close()
def mark_cells_used(uuids):
Session = Connection.sessionmaker(bind=Connection.engine)
sqlSession = Session()
for uuid in uuids:
cell = sqlSession.query(Connection.Cells).filter_by(UUID=uuid).first()
if cell is not None:
cell.Available = "NO"
sqlSession.commit()
sqlSession.close()
def get_gell_data(uuid):
project_id, project_name = get_current_project_id()
print(project_id)
cells_info = []
if project_id:
Session = Connection.sessionmaker(bind=Connection.engine)
sqlSession = Session()
cell = sqlSession.query(Connection.Cells).filter_by(UUID=uuid).first()
project_query = sqlSession.query(Connection.Projects).filter_by(ID=project_id).first()
if cell is not None:
if cell.Available == "YES":
cell_data = dict()
cell_data["ProjectID"] = project_id
cell_data["ID"] = cell.ID
cell_data["ProjectName"] = project_query.Name
cell_data["UUID"] = cell.UUID
cell_data["Voltage"] = cell.Voltage
cell_data["Capacity"] = cell.Capacity
cell_data["ESR"] = cell.ESR
cell_data["CellType"] = cell.CellType
cell_data["AddedDate"] = cell.AddedDate.strftime("%Y-%m-%d %H:%M:%S")
cell_data["Available"] = cell.Available
cell_data["Device"] = cell.Device
cells_info.append(cell_data)
return cell_data
else:
return "NA"
else:
return None
else:
return None