Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dm4rtig4n committed Nov 2, 2021
1 parent fbde995 commit bbfdd2c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
18 changes: 15 additions & 3 deletions app/daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,23 @@ def dailyBeetwen(cur, con, pdl, mode, dateBegin, dateEnded, last_activation_date
for date, date_data in current_data['date'].items():
if not date in new_date:
not_found_data.append(date)
current_date = datetime.strptime(date, '%Y-%m-%d').date()
current_day = datetime.today().date()
if date_data['fail'] == 0 and date_data['value'] == 0:
cur.execute(f"INSERT OR REPLACE INTO {mode}_daily VALUES ('{pdl}','{date}','0','1')")
if current_day == current_date:
fail = 0
else:
fail = 1
cur.execute(f"INSERT OR REPLACE INTO {mode}_daily VALUES ('{pdl}','{date}','0','{fail}')")
# cur.execute(f"INSERT OR REPLACE INTO {mode}_daily VALUES ('{pdl}','{date}','0','1')")
else:
cur.execute(
f"UPDATE {mode}_daily SET fail = {date_data['fail'] + 1} WHERE pdl = '{pdl}' and date = '{date}'")
if current_day != current_date:
fail = 0
else:
fail = date_data['fail']
cur.execute(f"UPDATE {mode}_daily SET fail = {fail + 1} WHERE pdl = '{pdl}' and date = '{date}'")
# cur.execute(f"UPDATE {mode}_daily SET fail = {date_data['fail'] + 1} WHERE pdl = '{pdl}' and date = '{date}'")


if not_found_data != []:
f.log("Data not found :")
Expand Down
1 change: 0 additions & 1 deletion app/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ def getDetail(cur, con, client, mode="consumption", last_activation_date=datetim
query = f"SELECT * FROM consumption_detail WHERE pdl = '{pdl}' ORDER BY date;"
cur.execute(query)
query_result = cur.fetchall()

result = {}
base_vs_offpeak = 0
for data in query_result:
Expand Down
13 changes: 2 additions & 11 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ def init_database(cur):
config = {
"day": datetime.now().strftime('%Y-%m-%d'),
"call_number": 0,
"max_call": 15
"max_call": 200
}
cur.execute(config_query, ["config", json.dumps(config)])

Expand Down Expand Up @@ -600,15 +600,7 @@ def run():
f.logLine()
f.log(f"Consumption data not found on enedis (after {fail_count} retry) :")
# pprint(rows.fetchall())
for row in rows:
f.log(f"{row[0]} => {row[1]}")

query = f"SELECT * FROM production_daily WHERE pdl == '{pdl}' AND fail > {fail_count} ORDER BY date"
rows = con.execute(query)
if rows.fetchone() is not None:
f.logLine()
f.log(f"Production data not found on enedis (after {fail_count} retry) :")
# pprint(rows.fetchall())
# pprint(rows)
for row in rows:
f.log(f"{row[0]} => {row[1]}")

Expand All @@ -635,7 +627,6 @@ def run():
# MQTT
client = f.connect_mqtt()
client.loop_start()
pprint(client)

# INFLUXDB
if influxdb_enable == True:
Expand Down
47 changes: 24 additions & 23 deletions app/myenedis.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def forceRound(x, n):
query = f"SELECT * FROM consumption_daily WHERE pdl = '{pdl}' AND date='{yesterday}';"
cur.execute(query)
query_result = cur.fetchone()
pprint(query_result)
attributes['yesterdayDate'] = yesterday
if query_result != None and query_result[2] != 0:
attributes['yesterday'] = query_result[2] / 1000
Expand All @@ -94,7 +95,7 @@ def forceRound(x, n):
query = f"SELECT * FROM consumption_daily WHERE pdl = '{pdl}' AND date='{yesterday_last_year}';"
cur.execute(query)
query_result = cur.fetchone()

pprint(query_result)
if query_result != None and query_result[2] != 0:
attributes['yesterdayLastYear'] = query_result[2] / 1000
else:
Expand Down Expand Up @@ -149,7 +150,7 @@ def forceRound(x, n):

current_week_last_year = attributes['current_week_last_year']
if attributes['current_week_last_year'] == 0:
attributes['current_week_last_year'] = -1
attributes['current_week_last_year'] = 0
else:
attributes['current_week_last_year'] = str(forceRound(attributes['current_week_last_year'], 2))

Expand All @@ -176,11 +177,11 @@ def forceRound(x, n):

last_month = attributes['last_month']
if attributes['last_month'] == 0:
attributes['last_month'] = -1
attributes['last_month'] = 0

current_month = attributes['current_month']
if attributes['current_month'] == 0:
attributes['current_month'] = -1
attributes['current_month'] = 0
else:
attributes['current_month'] = str(forceRound(attributes['current_month'], 2))

Expand All @@ -201,7 +202,7 @@ def forceRound(x, n):

current_month_last_year = attributes['current_month_last_year']
if attributes['current_month_last_year'] == 0:
attributes['current_month_last_year'] = -1
attributes['current_month_last_year'] = 0
else:
attributes['current_month_last_year'] = str(forceRound(attributes['current_month_last_year'], 2))

Expand All @@ -220,7 +221,7 @@ def forceRound(x, n):

last_month_last_year = attributes['last_month_last_year']
if attributes['last_month_last_year'] == 0:
attributes['last_month_last_year'] = -1
attributes['last_month_last_year'] = 0
else:
attributes['last_month_last_year'] = str(forceRound(last_month_last_year, 2))

Expand All @@ -246,7 +247,7 @@ def forceRound(x, n):

current_year = attributes['current_year']
if attributes['current_year'] == 0:
attributes['current_year'] = -1
attributes['current_year'] = 0
else:
attributes['current_year'] = str(forceRound(attributes['current_year'], 2))

Expand All @@ -264,7 +265,7 @@ def forceRound(x, n):

current_year_last_year = attributes['current_year_last_year']
if attributes['current_year_last_year'] == 0:
attributes['current_year_last_year'] = -1
attributes['current_year_last_year'] = 0
else:
attributes['current_year_last_year'] = str(forceRound(attributes['current_year_last_year'], 2))

Expand All @@ -288,10 +289,10 @@ def forceRound(x, n):
attributes[f'day_{day}_{measure_type}'] = int(0)
if query_result == []:
if measure_type != "BASE":
attributes[f'day_{day}_{measure_type}'] = -1
attributes[f'dailyweek_cost{measure_type}'].append(-1)
attributes[f'dailyweek_{measure_type}'].append(-1)
attributes["dailyweek_cost"][day-1] = -1
attributes[f'day_{day}_{measure_type}'] = 0
attributes[f'dailyweek_cost{measure_type}'].append(0)
attributes[f'dailyweek_{measure_type}'].append(0)
attributes["dailyweek_cost"][day0] = 0
else:
value_wh_total = 0
dailyweek_cost = 0
Expand Down Expand Up @@ -346,32 +347,32 @@ def forceRound(x, n):
if measure_type != "BASE":
attributes['peak_offpeak_percent'] = forceRound(abs(100 * (peak_offpeak_percent['HC'] - peak_offpeak_percent['HP']) / peak_offpeak_percent['HP']), 2)
else:
attributes['peak_offpeak_percent'] = -1
attributes['peak_offpeak_percent'] = 0

def variation(val1, val2):
# print(f"100 * ({val1} - {val2}) / {val2}")
result = 100 * (val1 - val2) / val2
return result

if last_month != -1 and last_month != 0 and last_month_last_year != -1 and last_month_last_year != 0:
if last_month != 0 and last_month != 0 and last_month_last_year != 0 and last_month_last_year != 0:
attributes['monthly_evolution'] = forceRound(variation(last_month, last_month_last_year), 2)
else:
attributes['monthly_evolution'] = -1
attributes['monthly_evolution'] = 0

if current_week != -1 and current_week != 0 and current_week_last_year != -1 and current_week_last_year != 0:
if current_week != 0 and current_week != 0 and current_week_last_year != 0 and current_week_last_year != 0:
attributes['current_week_evolution'] = forceRound(variation(current_week, current_week_last_year), 2)
else:
attributes['current_week_evolution'] = -1
attributes['current_week_evolution'] = 0

if current_month != -1 and current_month != 0 and current_month_last_year != -1 and current_month_last_year != 0:
if current_month != 0 and current_month != 0 and current_month_last_year != 0 and current_month_last_year != 0:
attributes['current_month_evolution'] = forceRound(variation(current_month, current_month_last_year), 2)
else:
attributes['current_month_evolution'] = -1
attributes['current_month_evolution'] = 0

if state != -1 and state != 0 and yesterdayLastYear != -1 and yesterdayLastYear != 0:
if state != 0 and state != 0 and yesterdayLastYear != 0 and yesterdayLastYear != 0:
attributes['yesterday_evolution'] = forceRound(variation(state, yesterdayLastYear), 2)
else:
attributes['yesterday_evolution'] = -1
attributes['yesterday_evolution'] = 0

attributes[f'dailyweek'] = sorted(list(set(attributes[f'dailyweek'])), reverse=True)
attributes[f'friendly_name'] = f"EnedisGateway.{pdl}"
Expand Down Expand Up @@ -402,8 +403,8 @@ def variation(val1, val2):
offpeak_hours.append(current)
attributes[f'offpeak_hours'] = offpeak_hours
else:
attributes[f'offpeak_hours_enedis'] = -1
attributes[f'offpeak_hours'] = -1
attributes[f'offpeak_hours_enedis'] = 0
attributes[f'offpeak_hours'] = 0

query = f"SELECT * FROM config WHERE key = '{pdl}_subscribed_power'"
cur.execute(query)
Expand Down

0 comments on commit bbfdd2c

Please sign in to comment.