@@ 272-369 (lines=98) @@ | ||
269 | # Get peak types of tariff by energy category |
|
270 | # peak types: toppeak, onpeak, midpeak, offpeak |
|
271 | ######################################################################################################################## |
|
272 | def get_energy_category_peak_types(cost_center_id, energy_category_id, start_datetime_utc, end_datetime_utc): |
|
273 | # todo: validate parameters |
|
274 | if cost_center_id is None: |
|
275 | return dict() |
|
276 | ||
277 | start_datetime_utc = start_datetime_utc.replace(tzinfo=None) |
|
278 | end_datetime_utc = end_datetime_utc.replace(tzinfo=None) |
|
279 | ||
280 | # get timezone offset in minutes, this value will be returned to client |
|
281 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
282 | if config.utc_offset[0] == '-': |
|
283 | timezone_offset = -timezone_offset |
|
284 | ||
285 | tariff_dict = collections.OrderedDict() |
|
286 | ||
287 | cnx = None |
|
288 | cursor = None |
|
289 | try: |
|
290 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
291 | cursor = cnx.cursor() |
|
292 | query_tariffs = (" SELECT t.id, t.valid_from_datetime_utc, t.valid_through_datetime_utc " |
|
293 | " FROM tbl_tariffs t, tbl_cost_centers_tariffs cct " |
|
294 | " WHERE t.energy_category_id = %s AND " |
|
295 | " t.id = cct.tariff_id AND " |
|
296 | " cct.cost_center_id = %s AND " |
|
297 | " t.valid_through_datetime_utc >= %s AND " |
|
298 | " t.valid_from_datetime_utc <= %s " |
|
299 | " ORDER BY t.valid_from_datetime_utc ") |
|
300 | cursor.execute(query_tariffs, (energy_category_id, cost_center_id, start_datetime_utc, end_datetime_utc,)) |
|
301 | rows_tariffs = cursor.fetchall() |
|
302 | except Exception as e: |
|
303 | print(str(e)) |
|
304 | if cnx: |
|
305 | cnx.disconnect() |
|
306 | if cursor: |
|
307 | cursor.close() |
|
308 | return dict() |
|
309 | ||
310 | if rows_tariffs is None or len(rows_tariffs) == 0: |
|
311 | if cursor: |
|
312 | cursor.close() |
|
313 | if cnx: |
|
314 | cnx.disconnect() |
|
315 | return dict() |
|
316 | ||
317 | for row in rows_tariffs: |
|
318 | tariff_dict[row[0]] = {'valid_from_datetime_utc': row[1], |
|
319 | 'valid_through_datetime_utc': row[2], |
|
320 | 'rates': list()} |
|
321 | ||
322 | try: |
|
323 | query_timeofuse_tariffs = (" SELECT tariff_id, start_time_of_day, end_time_of_day, peak_type " |
|
324 | " FROM tbl_tariffs_timeofuses " |
|
325 | " WHERE tariff_id IN ( " + ', '.join(map(str, tariff_dict.keys())) + ")" |
|
326 | " ORDER BY tariff_id, start_time_of_day ") |
|
327 | cursor.execute(query_timeofuse_tariffs, ) |
|
328 | rows_timeofuse_tariffs = cursor.fetchall() |
|
329 | except Exception as e: |
|
330 | print(str(e)) |
|
331 | if cnx: |
|
332 | cnx.disconnect() |
|
333 | if cursor: |
|
334 | cursor.close() |
|
335 | return dict() |
|
336 | ||
337 | if cursor: |
|
338 | cursor.close() |
|
339 | if cnx: |
|
340 | cnx.disconnect() |
|
341 | ||
342 | if rows_timeofuse_tariffs is None or len(rows_timeofuse_tariffs) == 0: |
|
343 | return dict() |
|
344 | ||
345 | for row in rows_timeofuse_tariffs: |
|
346 | tariff_dict[row[0]]['rates'].append({'start_time_of_day': row[1], |
|
347 | 'end_time_of_day': row[2], |
|
348 | 'peak_type': row[3]}) |
|
349 | ||
350 | result = dict() |
|
351 | for tariff_id, tariff_value in tariff_dict.items(): |
|
352 | current_datetime_utc = tariff_value['valid_from_datetime_utc'] |
|
353 | while current_datetime_utc < tariff_value['valid_through_datetime_utc']: |
|
354 | for rate in tariff_value['rates']: |
|
355 | current_datetime_local = current_datetime_utc + timedelta(minutes=timezone_offset) |
|
356 | seconds_since_midnight = (current_datetime_local - |
|
357 | current_datetime_local.replace(hour=0, |
|
358 | second=0, |
|
359 | microsecond=0, |
|
360 | tzinfo=None)).total_seconds() |
|
361 | if rate['start_time_of_day'].total_seconds() <= \ |
|
362 | seconds_since_midnight < rate['end_time_of_day'].total_seconds(): |
|
363 | result[current_datetime_utc] = rate['peak_type'] |
|
364 | break |
|
365 | ||
366 | # start from the next time slot |
|
367 | current_datetime_utc += timedelta(minutes=config.minutes_to_count) |
|
368 | ||
369 | return {k: v for k, v in result.items() if start_datetime_utc <= k <= end_datetime_utc} |
|
370 | ||
371 | ||
372 | ######################################################################################################################## |
|
@@ 168-265 (lines=98) @@ | ||
165 | ######################################################################################################################## |
|
166 | # Get tariffs by energy category |
|
167 | ######################################################################################################################## |
|
168 | def get_energy_category_tariffs(cost_center_id, energy_category_id, start_datetime_utc, end_datetime_utc): |
|
169 | # todo: validate parameters |
|
170 | if cost_center_id is None: |
|
171 | return dict() |
|
172 | ||
173 | start_datetime_utc = start_datetime_utc.replace(tzinfo=None) |
|
174 | end_datetime_utc = end_datetime_utc.replace(tzinfo=None) |
|
175 | ||
176 | # get timezone offset in minutes, this value will be returned to client |
|
177 | timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
|
178 | if config.utc_offset[0] == '-': |
|
179 | timezone_offset = -timezone_offset |
|
180 | ||
181 | tariff_dict = collections.OrderedDict() |
|
182 | ||
183 | cnx = None |
|
184 | cursor = None |
|
185 | try: |
|
186 | cnx = mysql.connector.connect(**config.myems_system_db) |
|
187 | cursor = cnx.cursor() |
|
188 | query_tariffs = (" SELECT t.id, t.valid_from_datetime_utc, t.valid_through_datetime_utc " |
|
189 | " FROM tbl_tariffs t, tbl_cost_centers_tariffs cct " |
|
190 | " WHERE t.energy_category_id = %s AND " |
|
191 | " t.id = cct.tariff_id AND " |
|
192 | " cct.cost_center_id = %s AND " |
|
193 | " t.valid_through_datetime_utc >= %s AND " |
|
194 | " t.valid_from_datetime_utc <= %s " |
|
195 | " ORDER BY t.valid_from_datetime_utc ") |
|
196 | cursor.execute(query_tariffs, (energy_category_id, cost_center_id, start_datetime_utc, end_datetime_utc,)) |
|
197 | rows_tariffs = cursor.fetchall() |
|
198 | except Exception as e: |
|
199 | print(str(e)) |
|
200 | if cnx: |
|
201 | cnx.disconnect() |
|
202 | if cursor: |
|
203 | cursor.close() |
|
204 | return dict() |
|
205 | ||
206 | if rows_tariffs is None or len(rows_tariffs) == 0: |
|
207 | if cursor: |
|
208 | cursor.close() |
|
209 | if cnx: |
|
210 | cnx.disconnect() |
|
211 | return dict() |
|
212 | ||
213 | for row in rows_tariffs: |
|
214 | tariff_dict[row[0]] = {'valid_from_datetime_utc': row[1], |
|
215 | 'valid_through_datetime_utc': row[2], |
|
216 | 'rates': list()} |
|
217 | ||
218 | try: |
|
219 | query_timeofuse_tariffs = (" SELECT tariff_id, start_time_of_day, end_time_of_day, price " |
|
220 | " FROM tbl_tariffs_timeofuses " |
|
221 | " WHERE tariff_id IN ( " + ', '.join(map(str, tariff_dict.keys())) + ")" |
|
222 | " ORDER BY tariff_id, start_time_of_day ") |
|
223 | cursor.execute(query_timeofuse_tariffs, ) |
|
224 | rows_timeofuse_tariffs = cursor.fetchall() |
|
225 | except Exception as e: |
|
226 | print(str(e)) |
|
227 | if cnx: |
|
228 | cnx.disconnect() |
|
229 | if cursor: |
|
230 | cursor.close() |
|
231 | return dict() |
|
232 | ||
233 | if cursor: |
|
234 | cursor.close() |
|
235 | if cnx: |
|
236 | cnx.disconnect() |
|
237 | ||
238 | if rows_timeofuse_tariffs is None or len(rows_timeofuse_tariffs) == 0: |
|
239 | return dict() |
|
240 | ||
241 | for row in rows_timeofuse_tariffs: |
|
242 | tariff_dict[row[0]]['rates'].append({'start_time_of_day': row[1], |
|
243 | 'end_time_of_day': row[2], |
|
244 | 'price': row[3]}) |
|
245 | ||
246 | result = dict() |
|
247 | for tariff_id, tariff_value in tariff_dict.items(): |
|
248 | current_datetime_utc = tariff_value['valid_from_datetime_utc'] |
|
249 | while current_datetime_utc < tariff_value['valid_through_datetime_utc']: |
|
250 | for rate in tariff_value['rates']: |
|
251 | current_datetime_local = current_datetime_utc + timedelta(minutes=timezone_offset) |
|
252 | seconds_since_midnight = (current_datetime_local - |
|
253 | current_datetime_local.replace(hour=0, |
|
254 | second=0, |
|
255 | microsecond=0, |
|
256 | tzinfo=None)).total_seconds() |
|
257 | if rate['start_time_of_day'].total_seconds() <= \ |
|
258 | seconds_since_midnight < rate['end_time_of_day'].total_seconds(): |
|
259 | result[current_datetime_utc] = rate['price'] |
|
260 | break |
|
261 | ||
262 | # start from the next time slot |
|
263 | current_datetime_utc += timedelta(minutes=config.minutes_to_count) |
|
264 | ||
265 | return {k: v for k, v in result.items() if start_datetime_utc <= k <= end_datetime_utc} |
|
266 | ||
267 | ||
268 | ######################################################################################################################## |