Skip to content

Commit

Permalink
maj
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dm4rtig4n committed Oct 4, 2022
1 parent 1ecc4e6 commit 74057b8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 42 deletions.
3 changes: 0 additions & 3 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,13 @@
if __name__ == '__main__':

# while True:
print(CONFIG.get('myelectricaldata'))
for usage_point_id, config in CONFIG.get('myelectricaldata').items():

myelectricaldata = MyElectricalData(
cache=CACHE,
url=url,
usage_point_id=usage_point_id,
config=config
)

logSep()
log("Récupération du contrat :")
myelectricaldata.contract()
Expand Down
2 changes: 1 addition & 1 deletion app/models/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def get_contract(self, usage_point_id):
query_result = self.cursor.fetchone()
return query_result

def insert_contract(self, usage_point_id, contract, count):
def insert_contract(self, usage_point_id, contract, count=0):
query = f"INSERT OR REPLACE INTO contracts VALUES (?,?,?)"
debug(query)
self.cursor.execute(query, [usage_point_id, json.dumps(contract), count])
Expand Down
63 changes: 26 additions & 37 deletions app/models/myelectricaldata.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import json
import sys

from . import MQTT
from models.config import get_version
from models.query import Query
from models.log import log, debug
from models.log import log, debug, critical


class MyElectricalData:
Expand All @@ -20,7 +21,6 @@ def __init__(self, cache, url, usage_point_id, config):

self.usage_point_id = usage_point_id
self.config = config
self.contrat = {}

def contract(self):
name = "contracts"
Expand All @@ -32,48 +32,37 @@ def contract(self):
if current_cache is None:
# No cache
log(f" => No cache : {target}")
self.contrat = Query(endpoint=target, headers=self.headers).get(headers=self.headers)
self.cache.insert_contract(usage_point_id=self.usage_point_id, contract=self.contract(), count=0)
response = Query(endpoint=target, headers=self.headers).get(headers=self.headers)
if response.status_code == 200:
self.cache.insert_contract(
usage_point_id=self.usage_point_id,
contract=response.text,
)
else:
critical("Erreur lors de la récupération du contrat.")
else:
if "refresh_contract" in self.config and self.config["refresh_contract"] == True:
# Refresh cache
if "refresh_contract" in self.config and self.config["refresh_contract"]:
log(f" => Refresh Cache : {target}")
self.contrat = Query(endpoint=target, headers=self.headers).get(headers=self.headers)
self.cache.insert_contract(usage_point_id=self.usage_point_id, contract=self.contract(), count=0)
response = Query(endpoint=target, headers=self.headers).get(headers=self.headers)
if response.status_code == 200:
self.cache.insert_contract(
usage_point_id=self.usage_point_id,
contract=response.text,
)
else:
critical("Erreur lors de la récupération du contrat.")
else:
# Get data in cache
log(f" => Query Cache")
contract = json.loads(query_result[1])
query = f"INSERT OR REPLACE INTO contracts VALUES (?,?,?)"
cur.execute(query, [pdl, json.dumps(contract), 0])
con.commit()
contract = json.loads(current_cache[1])
self.cache.insert_contract(
usage_point_id=self.usage_point_id,
contract=contract,
)

# def queryApi(url, headers, data, count=0):
# contract = f.apiRequest(cur, con, pdl, type="POST", url=f"{url}", headers=headers, data=json.dumps(data))
# if not "error_code" in contract:
# query = f"INSERT OR REPLACE INTO contracts VALUES (?,?,?)"
# cur.execute(query, [pdl, json.dumps(contract), count])
# con.commit()
# return contract
#
# url = main.url
#
# ha_discovery = {pdl: {}}
#
#

# if query_result is None:
# f.log(" => Query API")
# contract = queryApi(url, headers, data)
# else:
# if "refresh_contract" in pdl_config and pdl_config["refresh_contract"] == True:
# f.log(" => Query API (Refresh Cache)")
# contract = queryApi(url, headers, data, 0)
# else:
# f.log(f" => Query Cache")
# contract = json.loads(query_result[1])
# query = f"INSERT OR REPLACE INTO contracts VALUES (?,?,?)"
# cur.execute(query, [pdl, json.dumps(contract), 0])
# con.commit()
#
# if "error_code" in contract:
# f.log(contract["description"])
# ha_discovery = {"error_code": True, "detail": {"message": contract["description"]}}
Expand Down
2 changes: 1 addition & 1 deletion app/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ def get(self, headers=None, params=None):
response = requests.request('GET', headers=headers, params=params, url=self.endpoint)
debug(f"[RESPONSE] : status_code {response.status_code}")
debug(f" => {response.text}")
return response
except Exception as e:
error(e)
print(str(e))
return response

def post(self, headers=None, params=None, data=None):
if headers is None:
Expand Down

0 comments on commit 74057b8

Please sign in to comment.