Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dm4rtig4n committed Sep 29, 2021
1 parent 9debdbb commit 471e67b
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea
/data
/data
/app/__pycache__/
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# EnedisGateway2MQTT

## Links
## IMPORTANT !
**The tool is still under development.
It is possible that various functions disappear or be modified**

# Links

Github Repository : https://github.com/m4dm4rtig4n/enedisgateway2mqtt

Expand Down
Binary file modified app/__pycache__/addresses.cpython-39.pyc
100644 → 100755
Binary file not shown.
Binary file modified app/__pycache__/contract.cpython-39.pyc
100644 → 100755
Binary file not shown.
Binary file modified app/__pycache__/daily_consumption.cpython-39.pyc
100644 → 100755
Binary file not shown.
Binary file modified app/__pycache__/daily_production.cpython-39.pyc
100644 → 100755
Binary file not shown.
Binary file modified app/__pycache__/function.cpython-39.pyc
100644 → 100755
Binary file not shown.
Binary file modified app/__pycache__/home_assistant.cpython-39.pyc
100644 → 100755
Binary file not shown.
Binary file modified app/__pycache__/main.cpython-39.pyc
100644 → 100755
Binary file not shown.
Empty file modified app/addresses.py
100644 → 100755
Empty file.
3 changes: 2 additions & 1 deletion app/contract.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def getContract(client):
f.publish(client, f"{pdl}/details/usage_points/contracts/{contracts_key}", str(contracts_data))
if contracts_key == "last_activation_date":
f.publish(client, f"{pdl}/activation_date", str(contracts_data))
last_activation_date = contracts_data
if contracts_key == "subscribed_power":
f.publish(client, f"{pdl}/subscribed_power", str(contracts_data.split()[0]))
ha_discovery[pdl].update({
Expand All @@ -52,7 +53,7 @@ def getContract(client):
})
retour = {
"ha_discovery": ha_discovery,
"last_activation_date": contracts_data
"last_activation_date": last_activation_date
}
else:
retour = {
Expand Down
23 changes: 18 additions & 5 deletions app/daily_consumption.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def dailyConsumption(cur, client, last_activation_date):
current_year = current_year + 1
return ha_discovery


def checkHistoryConsumptionDaily(cur, dateBegin, dateEnded):
pdl = main.pdl
dateBegin = datetime.strptime(dateBegin, '%Y-%m-%d')
dateEnded = datetime.strptime(dateEnded, '%Y-%m-%d')
delta = dateEnded - dateBegin
Expand All @@ -98,7 +98,7 @@ def checkHistoryConsumptionDaily(cur, dateBegin, dateEnded):
for i in range(delta.days + 1):
checkDate = dateBegin + timedelta(days=i)
checkDate = checkDate.strftime('%Y-%m-%d')
query = f"SELECT * FROM consumption_daily WHERE pdl = '{main.pdl}' AND date = '{checkDate}'"
query = f"SELECT * FROM consumption_daily WHERE pdl = '{pdl}' AND date = '{checkDate}'"
cur.execute(query)
if cur.fetchone() is None:
main.api_no_result.append(checkDate)
Expand Down Expand Up @@ -127,25 +127,38 @@ def dailyConsumptionBeetwen(cur, pdl, dateBegin, dateEnded, last_activation_date
}

try:
new_date = []
current_data = checkHistoryConsumptionDaily(cur, dateBegin, dateEnded)
if current_data['status'] == True:
f.log(f"All data loading beetween {dateBegin} / {dateEnded}")
f.log(f" => Skip API Call")
else:
f.log(f"{current_data['count']} lost date beetween {dateBegin} / {dateEnded} :")
for lost_data in current_data['date']:
f.log(f" - {lost_data}")
f.log(f"Data is missing between {dateBegin} / {dateEnded}")

daily_consumption = requests.request("POST", url=f"{main.url}", headers=main.headers, data=json.dumps(data)).json()
meter_reading = daily_consumption['meter_reading']
mesures = {}
f.log("Import data :")
log_import = []
for interval_reading in meter_reading["interval_reading"]:
date = interval_reading['date']
value = interval_reading['value']
cur.execute(f"INSERT OR REPLACE INTO consumption_daily VALUES ('{pdl}','{interval_reading['date']}','{interval_reading['value']}')")
new_date.append(interval_reading['date'])
mesures[date] = value
list_date = list(reversed(sorted(mesures.keys())))

f.splitLog(new_date)

not_found_data = []
for current_date in current_data['date']:
if not current_date in new_date:
not_found_data.append(current_date)

if not_found_data != []:
f.log("Data not found :")
f.splitLog(not_found_data)

dateEnded = datetime.strptime(dateEnded, '%Y-%m-%d')

dateWeek = dateEnded + relativedelta(days=-7)
Expand Down
20 changes: 17 additions & 3 deletions app/daily_production.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def dailyProduction(cur, client, last_activation_date):
return ha_discovery

def checkHistoryProductionDaily(cur, dateBegin, dateEnded):
pdl = main.pdl
dateBegin = datetime.strptime(dateBegin, '%Y-%m-%d')
dateEnded = datetime.strptime(dateEnded, '%Y-%m-%d')
delta = dateEnded - dateBegin
Expand Down Expand Up @@ -126,25 +127,38 @@ def dailyProductionBeetwen(cur, pdl, dateBegin, dateEnded, last_activation_date)
}

try:
new_date = []
current_data = checkHistoryProductionDaily(cur, dateBegin, dateEnded)
if current_data['status'] == True:
f.log(f"All data loading beetween {dateBegin} / {dateEnded}")
f.log(f" => Skip API Call")
else:
f.log(f"{current_data['count']} lost date beetween {dateBegin} / {dateEnded} :")
for lost_data in current_data['date']:
f.log(f" - {lost_data}")
f.log(f"Data is missing between {dateBegin} / {dateEnded}")

daily_production = requests.request("POST", url=f"{main.url}", headers=main.headers, data=json.dumps(data)).json()
meter_reading = daily_production['meter_reading']
mesures = {}
f.log("Import data :")
log_import = []
for interval_reading in meter_reading["interval_reading"]:
date = interval_reading['date']
value = interval_reading['value']
cur.execute(f"INSERT OR REPLACE INTO production_daily VALUES ('{pdl}','{interval_reading['date']}','{interval_reading['value']}')")
new_date.append(interval_reading['date'])
mesures[date] = value
list_date = list(reversed(sorted(mesures.keys())))

f.splitLog(new_date)

not_found_data = []
for current_date in current_data['date']:
if not current_date in new_date:
not_found_data.append(current_date)

if not_found_data != []:
f.log("Data not found :")
f.splitLog(not_found_data)

dateEnded = datetime.strptime(dateEnded, '%Y-%m-%d')

dateWeek = dateEnded + relativedelta(days=-7)
Expand Down
29 changes: 25 additions & 4 deletions app/function.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,37 @@ def on_connect(client, userdata, flags, rc):
client.connect(main.broker, main.port)
return client

def publish(client, topic, msg, current_prefix="enedis_gateway"):
def publish(client, topic, msg):
prefix = main.prefix
msg_count = 0
result = client.publish(f'{current_prefix}/{topic}', str(msg), qos=main.qos, retain=int(main.retain))
result = client.publish(f'{prefix}/{topic}', str(msg), qos=main.qos, retain=int(main.retain))
status = result[0]
if status == 0:
log(f" MQTT Send : {current_prefix}/{topic} => {msg}")
log(f" MQTT Send : {prefix}/{topic} => {msg}")
else:
log(f" - Failed to send message to topic {current_prefix}/{topic}")
log(f" - Failed to send message to topic {prefix}/{topic}")
msg_count += 1

def log(msg):
now = datetime.now()
print(f"{now} : {msg}")

def splitLog(msg):
format_log = ""
i = 1
nb_col = 12
msg_length = len(msg)
cur_length = 1
for log_msg in msg:
format_log += f" | {log_msg}"
if i == nb_col:
i = 1
format_log += f" |"
log(format_log)
format_log = ""
elif cur_length == msg_length:
format_log += f" |"
log(format_log)
else:
i = i + 1
cur_length = cur_length + 1
Empty file modified app/home_assistant.py
100644 → 100755
Empty file.

0 comments on commit 471e67b

Please sign in to comment.