|
@@ 343-496 (lines=154) @@
|
| 340 |
|
# Get peak types of tariff by energy category |
| 341 |
|
# peak types: toppeak, onpeak, midpeak, offpeak, deep |
| 342 |
|
######################################################################################################################## |
| 343 |
|
def get_energy_category_peak_types(cost_center_id, energy_category_id, start_datetime_utc, end_datetime_utc): |
| 344 |
|
# todo: validate parameters |
| 345 |
|
if cost_center_id is None: |
| 346 |
|
return dict() |
| 347 |
|
|
| 348 |
|
start_datetime_utc = start_datetime_utc.replace(tzinfo=None) |
| 349 |
|
end_datetime_utc = end_datetime_utc.replace(tzinfo=None) |
| 350 |
|
|
| 351 |
|
# get timezone offset in minutes, this value will be returned to client |
| 352 |
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
| 353 |
|
if config.utc_offset[0] == '-': |
| 354 |
|
timezone_offset = -timezone_offset |
| 355 |
|
|
| 356 |
|
tariff_dict = collections.OrderedDict() |
| 357 |
|
|
| 358 |
|
cnx = None |
| 359 |
|
cursor = None |
| 360 |
|
try: |
| 361 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 362 |
|
cursor = cnx.cursor() |
| 363 |
|
query_tariffs = (" SELECT t.id, t.valid_from_datetime_utc, t.valid_through_datetime_utc " |
| 364 |
|
" FROM tbl_tariffs t, tbl_cost_centers_tariffs cct " |
| 365 |
|
" WHERE t.energy_category_id = %s AND " |
| 366 |
|
" t.id = cct.tariff_id AND " |
| 367 |
|
" cct.cost_center_id = %s AND " |
| 368 |
|
" t.valid_through_datetime_utc >= %s AND " |
| 369 |
|
" t.valid_from_datetime_utc <= %s " |
| 370 |
|
" ORDER BY t.valid_from_datetime_utc ") |
| 371 |
|
cursor.execute(query_tariffs, (energy_category_id, cost_center_id, start_datetime_utc, end_datetime_utc,)) |
| 372 |
|
rows_tariffs = cursor.fetchall() |
| 373 |
|
except InterfaceError as ex: |
| 374 |
|
print("Failed to connect request") |
| 375 |
|
if cnx: |
| 376 |
|
cnx.close() |
| 377 |
|
if cursor: |
| 378 |
|
cursor.close() |
| 379 |
|
return dict() |
| 380 |
|
except OperationalError as ex: |
| 381 |
|
print("Failed to operate request") |
| 382 |
|
if cnx: |
| 383 |
|
cnx.close() |
| 384 |
|
if cursor: |
| 385 |
|
cursor.close() |
| 386 |
|
return dict() |
| 387 |
|
except ProgrammingError as ex: |
| 388 |
|
print("Failed to SQL request") |
| 389 |
|
if cnx: |
| 390 |
|
cnx.close() |
| 391 |
|
if cursor: |
| 392 |
|
cursor.close() |
| 393 |
|
return dict() |
| 394 |
|
except DataError as ex: |
| 395 |
|
print("Failed to SQL Data request") |
| 396 |
|
if cnx: |
| 397 |
|
cnx.close() |
| 398 |
|
if cursor: |
| 399 |
|
cursor.close() |
| 400 |
|
return dict() |
| 401 |
|
except Exception as ex: |
| 402 |
|
print('write_log:' + str(ex)) |
| 403 |
|
if cnx: |
| 404 |
|
cnx.close() |
| 405 |
|
if cursor: |
| 406 |
|
cursor.close() |
| 407 |
|
return dict() |
| 408 |
|
|
| 409 |
|
if rows_tariffs is None or len(rows_tariffs) == 0: |
| 410 |
|
if cursor: |
| 411 |
|
cursor.close() |
| 412 |
|
if cnx: |
| 413 |
|
cnx.close() |
| 414 |
|
return dict() |
| 415 |
|
|
| 416 |
|
for row in rows_tariffs: |
| 417 |
|
tariff_dict[row[0]] = {'valid_from_datetime_utc': row[1], |
| 418 |
|
'valid_through_datetime_utc': row[2], |
| 419 |
|
'rates': list()} |
| 420 |
|
|
| 421 |
|
try: |
| 422 |
|
query_timeofuse_tariffs = (" SELECT tariff_id, start_time_of_day, end_time_of_day, peak_type " |
| 423 |
|
" FROM tbl_tariffs_timeofuses " |
| 424 |
|
" WHERE tariff_id IN ( " + ', '.join(map(str, tariff_dict.keys())) + ")" |
| 425 |
|
" ORDER BY tariff_id, start_time_of_day ") |
| 426 |
|
cursor.execute(query_timeofuse_tariffs, ) |
| 427 |
|
rows_timeofuse_tariffs = cursor.fetchall() |
| 428 |
|
except InterfaceError as ex: |
| 429 |
|
print("Failed to connect request") |
| 430 |
|
if cnx: |
| 431 |
|
cnx.close() |
| 432 |
|
if cursor: |
| 433 |
|
cursor.close() |
| 434 |
|
return dict() |
| 435 |
|
except OperationalError as ex: |
| 436 |
|
print("Failed to operate request") |
| 437 |
|
if cnx: |
| 438 |
|
cnx.close() |
| 439 |
|
if cursor: |
| 440 |
|
cursor.close() |
| 441 |
|
return dict() |
| 442 |
|
except ProgrammingError as ex: |
| 443 |
|
print("Failed to SQL request") |
| 444 |
|
if cnx: |
| 445 |
|
cnx.close() |
| 446 |
|
if cursor: |
| 447 |
|
cursor.close() |
| 448 |
|
return dict() |
| 449 |
|
except DataError as ex: |
| 450 |
|
print("Failed to SQL Data request") |
| 451 |
|
if cnx: |
| 452 |
|
cnx.close() |
| 453 |
|
if cursor: |
| 454 |
|
cursor.close() |
| 455 |
|
return dict() |
| 456 |
|
except Exception as ex: |
| 457 |
|
print('write_log:' + str(ex)) |
| 458 |
|
if cnx: |
| 459 |
|
cnx.close() |
| 460 |
|
if cursor: |
| 461 |
|
cursor.close() |
| 462 |
|
return dict() |
| 463 |
|
|
| 464 |
|
if cursor: |
| 465 |
|
cursor.close() |
| 466 |
|
if cnx: |
| 467 |
|
cnx.close() |
| 468 |
|
|
| 469 |
|
if rows_timeofuse_tariffs is None or len(rows_timeofuse_tariffs) == 0: |
| 470 |
|
return dict() |
| 471 |
|
|
| 472 |
|
for row in rows_timeofuse_tariffs: |
| 473 |
|
tariff_dict[row[0]]['rates'].append({'start_time_of_day': row[1], |
| 474 |
|
'end_time_of_day': row[2], |
| 475 |
|
'peak_type': row[3]}) |
| 476 |
|
|
| 477 |
|
result = dict() |
| 478 |
|
for tariff_id, tariff_value in tariff_dict.items(): |
| 479 |
|
current_datetime_utc = tariff_value['valid_from_datetime_utc'] |
| 480 |
|
while current_datetime_utc < tariff_value['valid_through_datetime_utc']: |
| 481 |
|
for rate in tariff_value['rates']: |
| 482 |
|
current_datetime_local = current_datetime_utc + timedelta(minutes=timezone_offset) |
| 483 |
|
seconds_since_midnight = (current_datetime_local - |
| 484 |
|
current_datetime_local.replace(hour=0, |
| 485 |
|
second=0, |
| 486 |
|
microsecond=0, |
| 487 |
|
tzinfo=None)).total_seconds() |
| 488 |
|
if rate['start_time_of_day'].total_seconds() <= \ |
| 489 |
|
seconds_since_midnight < rate['end_time_of_day'].total_seconds(): |
| 490 |
|
result[current_datetime_utc] = rate['peak_type'] |
| 491 |
|
break |
| 492 |
|
|
| 493 |
|
# start from the next time slot |
| 494 |
|
current_datetime_utc += timedelta(minutes=config.minutes_to_count) |
| 495 |
|
|
| 496 |
|
return {k: v for k, v in result.items() if start_datetime_utc <= k <= end_datetime_utc} |
| 497 |
|
|
| 498 |
|
|
| 499 |
|
######################################################################################################################## |
|
@@ 183-336 (lines=154) @@
|
| 180 |
|
######################################################################################################################## |
| 181 |
|
# Get tariffs by energy category |
| 182 |
|
######################################################################################################################## |
| 183 |
|
def get_energy_category_tariffs(cost_center_id, energy_category_id, start_datetime_utc, end_datetime_utc): |
| 184 |
|
# todo: validate parameters |
| 185 |
|
if cost_center_id is None: |
| 186 |
|
return dict() |
| 187 |
|
|
| 188 |
|
start_datetime_utc = start_datetime_utc.replace(tzinfo=None) |
| 189 |
|
end_datetime_utc = end_datetime_utc.replace(tzinfo=None) |
| 190 |
|
|
| 191 |
|
# get timezone offset in minutes, this value will be returned to client |
| 192 |
|
timezone_offset = int(config.utc_offset[1:3]) * 60 + int(config.utc_offset[4:6]) |
| 193 |
|
if config.utc_offset[0] == '-': |
| 194 |
|
timezone_offset = -timezone_offset |
| 195 |
|
|
| 196 |
|
tariff_dict = collections.OrderedDict() |
| 197 |
|
|
| 198 |
|
cnx = None |
| 199 |
|
cursor = None |
| 200 |
|
try: |
| 201 |
|
cnx = mysql.connector.connect(**config.myems_system_db) |
| 202 |
|
cursor = cnx.cursor() |
| 203 |
|
query_tariffs = (" SELECT t.id, t.valid_from_datetime_utc, t.valid_through_datetime_utc " |
| 204 |
|
" FROM tbl_tariffs t, tbl_cost_centers_tariffs cct " |
| 205 |
|
" WHERE t.energy_category_id = %s AND " |
| 206 |
|
" t.id = cct.tariff_id AND " |
| 207 |
|
" cct.cost_center_id = %s AND " |
| 208 |
|
" t.valid_through_datetime_utc >= %s AND " |
| 209 |
|
" t.valid_from_datetime_utc <= %s " |
| 210 |
|
" ORDER BY t.valid_from_datetime_utc ") |
| 211 |
|
cursor.execute(query_tariffs, (energy_category_id, cost_center_id, start_datetime_utc, end_datetime_utc,)) |
| 212 |
|
rows_tariffs = cursor.fetchall() |
| 213 |
|
except InterfaceError as ex: |
| 214 |
|
print("Failed to connect request") |
| 215 |
|
if cnx: |
| 216 |
|
cnx.close() |
| 217 |
|
if cursor: |
| 218 |
|
cursor.close() |
| 219 |
|
return dict() |
| 220 |
|
except OperationalError as ex: |
| 221 |
|
print("Failed to operate request") |
| 222 |
|
if cnx: |
| 223 |
|
cnx.close() |
| 224 |
|
if cursor: |
| 225 |
|
cursor.close() |
| 226 |
|
return dict() |
| 227 |
|
except ProgrammingError as ex: |
| 228 |
|
print("Failed to SQL request") |
| 229 |
|
if cnx: |
| 230 |
|
cnx.close() |
| 231 |
|
if cursor: |
| 232 |
|
cursor.close() |
| 233 |
|
return dict() |
| 234 |
|
except DataError as ex: |
| 235 |
|
print("Failed to SQL Data request") |
| 236 |
|
if cnx: |
| 237 |
|
cnx.close() |
| 238 |
|
if cursor: |
| 239 |
|
cursor.close() |
| 240 |
|
return dict() |
| 241 |
|
except Exception as ex: |
| 242 |
|
print('write_log:' + str(ex)) |
| 243 |
|
if cnx: |
| 244 |
|
cnx.close() |
| 245 |
|
if cursor: |
| 246 |
|
cursor.close() |
| 247 |
|
return dict() |
| 248 |
|
|
| 249 |
|
if rows_tariffs is None or len(rows_tariffs) == 0: |
| 250 |
|
if cursor: |
| 251 |
|
cursor.close() |
| 252 |
|
if cnx: |
| 253 |
|
cnx.close() |
| 254 |
|
return dict() |
| 255 |
|
|
| 256 |
|
for row in rows_tariffs: |
| 257 |
|
tariff_dict[row[0]] = {'valid_from_datetime_utc': row[1], |
| 258 |
|
'valid_through_datetime_utc': row[2], |
| 259 |
|
'rates': list()} |
| 260 |
|
|
| 261 |
|
try: |
| 262 |
|
query_timeofuse_tariffs = (" SELECT tariff_id, start_time_of_day, end_time_of_day, price " |
| 263 |
|
" FROM tbl_tariffs_timeofuses " |
| 264 |
|
" WHERE tariff_id IN ( " + ', '.join(map(str, tariff_dict.keys())) + ")" |
| 265 |
|
" ORDER BY tariff_id, start_time_of_day ") |
| 266 |
|
cursor.execute(query_timeofuse_tariffs, ) |
| 267 |
|
rows_timeofuse_tariffs = cursor.fetchall() |
| 268 |
|
except InterfaceError as ex: |
| 269 |
|
print("Failed to connect request") |
| 270 |
|
if cnx: |
| 271 |
|
cnx.close() |
| 272 |
|
if cursor: |
| 273 |
|
cursor.close() |
| 274 |
|
return dict() |
| 275 |
|
except OperationalError as ex: |
| 276 |
|
print("Failed to operate request") |
| 277 |
|
if cnx: |
| 278 |
|
cnx.close() |
| 279 |
|
if cursor: |
| 280 |
|
cursor.close() |
| 281 |
|
return dict() |
| 282 |
|
except ProgrammingError as ex: |
| 283 |
|
print("Failed to SQL request") |
| 284 |
|
if cnx: |
| 285 |
|
cnx.close() |
| 286 |
|
if cursor: |
| 287 |
|
cursor.close() |
| 288 |
|
return dict() |
| 289 |
|
except DataError as ex: |
| 290 |
|
print("Failed to SQL Data request") |
| 291 |
|
if cnx: |
| 292 |
|
cnx.close() |
| 293 |
|
if cursor: |
| 294 |
|
cursor.close() |
| 295 |
|
return dict() |
| 296 |
|
except Exception as ex: |
| 297 |
|
print('write_log:' + str(ex)) |
| 298 |
|
if cnx: |
| 299 |
|
cnx.close() |
| 300 |
|
if cursor: |
| 301 |
|
cursor.close() |
| 302 |
|
return dict() |
| 303 |
|
|
| 304 |
|
if cursor: |
| 305 |
|
cursor.close() |
| 306 |
|
if cnx: |
| 307 |
|
cnx.close() |
| 308 |
|
|
| 309 |
|
if rows_timeofuse_tariffs is None or len(rows_timeofuse_tariffs) == 0: |
| 310 |
|
return dict() |
| 311 |
|
|
| 312 |
|
for row in rows_timeofuse_tariffs: |
| 313 |
|
tariff_dict[row[0]]['rates'].append({'start_time_of_day': row[1], |
| 314 |
|
'end_time_of_day': row[2], |
| 315 |
|
'price': row[3]}) |
| 316 |
|
|
| 317 |
|
result = dict() |
| 318 |
|
for tariff_id, tariff_value in tariff_dict.items(): |
| 319 |
|
current_datetime_utc = tariff_value['valid_from_datetime_utc'] |
| 320 |
|
while current_datetime_utc < tariff_value['valid_through_datetime_utc']: |
| 321 |
|
for rate in tariff_value['rates']: |
| 322 |
|
current_datetime_local = current_datetime_utc + timedelta(minutes=timezone_offset) |
| 323 |
|
seconds_since_midnight = (current_datetime_local - |
| 324 |
|
current_datetime_local.replace(hour=0, |
| 325 |
|
second=0, |
| 326 |
|
microsecond=0, |
| 327 |
|
tzinfo=None)).total_seconds() |
| 328 |
|
if rate['start_time_of_day'].total_seconds() <= \ |
| 329 |
|
seconds_since_midnight < rate['end_time_of_day'].total_seconds(): |
| 330 |
|
result[current_datetime_utc] = rate['price'] |
| 331 |
|
break |
| 332 |
|
|
| 333 |
|
# start from the next time slot |
| 334 |
|
current_datetime_utc += timedelta(minutes=config.minutes_to_count) |
| 335 |
|
|
| 336 |
|
return {k: v for k, v in result.items() if start_datetime_utc <= k <= end_datetime_utc} |
| 337 |
|
|
| 338 |
|
|
| 339 |
|
######################################################################################################################## |