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