Skip to content

Commit

Permalink
maj
Browse files Browse the repository at this point in the history
  • Loading branch information
m4dm4rtig4n committed Oct 19, 2022
1 parent 89f9363 commit 84e51c3
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 48 deletions.
24 changes: 1 addition & 23 deletions app/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -397,28 +397,6 @@ <h2 class="menu_title">Menu</h2>
</div>
<div id="body" style="padding-bottom: 100px;">{{ body }}</div>
</div>
<div id="bottom_menu" class="fixed-action-btn horizontal" style="bottom: 20px; right: 25px;">
<a id="menu" class="btn-floating btn-large red" >
<i class="large material-icons">menu</i>
</a>
<ul>
<li>
<a id="delete_data" class="btn-floating" title="Supprimer le cache">
<i class="material-icons">delete</i>
</a>
</li>
<li>
<a id="config_data" class="btn-floating" title="Configuration">
<i class="material-icons">settings_applications</i>
</a>
</li>
<li>
<a id="import_data" class="btn-floating" title="Importer les données depuis Enedis">
<i class="material-icons">file_download</i>
</a>
</li>
</ul>
</div>
<div id="dialog" title="Dialog Title">I'm in a dialog</div>
{{ bottom_menu }}
</body>
</html>
10 changes: 5 additions & 5 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@
class FetchAllDataScheduler(object):
JOBS = [
{
"id": f"fetch_data_boot",
"func": get_data,
}, {
# "id": f"fetch_data_boot",
# "func": get_data,
# }, {
"id": f"fetch_data",
"func": get_data,
"trigger": "interval",
Expand All @@ -98,7 +98,6 @@ class FetchAllDataScheduler(object):
scheduler = APScheduler()
scheduler.init_app(APP)
scheduler.start()
APP.run(host="0.0.0.0", port=5000, debug=False, use_reloader=True)


# ------------------------------------------------------------------------------------------------------------------
Expand All @@ -109,7 +108,6 @@ class FetchAllDataScheduler(object):
@APP.route("/status/")
@APP.route("/ping")
@APP.route("/ping/")
@APP.route("/ping/d")
def status():
return "ok"

Expand Down Expand Up @@ -168,3 +166,5 @@ def whitelist_data(usage_point_id, target, date):
@APP.route("/usage_point_id/<usage_point_id>/<target>/import/<date>/")
def fetch_data(usage_point_id, target, date):
return Ajax(usage_point_id).fetch(target, date)

APP.run(host="0.0.0.0", port=5000, debug=False, use_reloader=True)
8 changes: 5 additions & 3 deletions app/models/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def __init__(self, path="/data"):
self.db_path = f"{self.path}/{self.db_name}"
self.uri = f'sqlite:///{self.db_path}?check_same_thread=False'

self.engine = db.create_engine(self.uri)
# self.engine = db.create_engine(self.uri, echo=True)
self.engine = db.create_engine(self.uri, echo=False)
self.connection = self.engine.connect()
self.metadata = db.MetaData()

Expand Down Expand Up @@ -318,7 +319,7 @@ def get_consumption_daily_cache_data(self, usage_point_id, date):
.where(self.db_consumption_daily.c.date == date)
).fetchone()

def get_consumption_daily_cache_state(self, date, usage_point_id):
def get_consumption_daily_cache_state(self, usage_point_id, date):
current_data = self.connection.execute(
self.db_consumption_daily.select()
.where(self.db_consumption_daily.c.pdl == usage_point_id)
Expand Down Expand Up @@ -401,6 +402,7 @@ def get_consumption_daily(self, usage_point_id, begin, end):
"value": consumption
}
result["missing_data"] = True
app.LOG.show(result)
self.consumption_daily_fail_increment(usage_point_id, checkDate)
else:
# SUCCESS
Expand All @@ -413,7 +415,7 @@ def get_consumption_daily(self, usage_point_id, begin, end):

def insert_consumption_daily(self, usage_point_id, date, value, blacklist=0):
result = self.get_consumption_daily_cache_state(usage_point_id, date)
app.LOG.show(result)
# app.LOG.show(result)
if not result:
return self.connection.execute(
self.db_consumption_daily.insert()
Expand Down
3 changes: 3 additions & 0 deletions app/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,6 @@ def usage_point_id_config(self, usage_point_id):
return self.config["myelectricaldata"][usage_point_id]
else:
return False

def list_usage_point(self):
return self.config["myelectricaldata"]
43 changes: 36 additions & 7 deletions app/models/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ def __init__(self, usage_point_id=None):
self.usage_point_id = usage_point_id
if self.usage_point_id is not None:
self.config = self.cache.get_config(self.usage_point_id)
self.headers = {
'Content-Type': 'application/json',
'Authorization': self.config['token'],
'call-service': "myelectricaldata",
'version': get_version()
}
self.headers = {
'Content-Type': 'application/json',
'Authorization': self.config['token'],
'call-service': "myelectricaldata",
'version': get_version()
}
self.list_usage_points_id = ""
self.max_history = 4
self.max_history_chart = 6
Expand Down Expand Up @@ -55,7 +55,7 @@ def html_return(self, body, head="", usage_point_id=0):
with open(f'{self.application_path}/html/index.html') as file_:
index_template = Template(file_.read())

config = self.cache.get_config(usage_point_id)
config = app.CONFIG.list_usage_point()
key_mapping = {
"token": "Token",
"cache": "Cache ?",
Expand All @@ -71,6 +71,7 @@ def html_return(self, body, head="", usage_point_id=0):
"offpeak_hours": "Forcer les heures HC/HP",
}
configuration = "<table>"
print(config)
for key, value in config.items():
if key in key_mapping:
key_name = key_mapping[key]
Expand All @@ -86,6 +87,33 @@ def html_return(self, body, head="", usage_point_id=0):
configuration += f'<tr><td>{key_name}</td><td><input type="text" id="configuration_{key}" name="{key}" value="{value}"></td></tr>'
configuration += "</table>"

bottom_menu = ""
if usage_point_id != 0:
bottom_menu = """
<div id="bottom_menu" class="fixed-action-btn horizontal" style="bottom: 20px; right: 25px;">
<a id="menu" class="btn-floating btn-large red" >
<i class="large material-icons">menu</i>
</a>
<ul>
<li>
<a id="delete_data" class="btn-floating" title="Supprimer le cache">
<i class="material-icons">delete</i>
</a>
</li>
<li>
<a id="config_data" class="btn-floating" title="Configuration">
<i class="material-icons">settings_applications</i>
</a>
</li>
<li>
<a id="import_data" class="btn-floating" title="Importer les données depuis Enedis">
<i class="material-icons">file_download</i>
</a>
</li>
</ul>
</div>
"""

html = index_template.render(
head=head,
body=body,
Expand All @@ -100,6 +128,7 @@ def html_return(self, body, head="", usage_point_id=0):
faq_url=f"{URL}/faq/",
code_url=f"{URL}/error_code/",
doc_url=f"{URL}/documentation/",
bottom_menu=bottom_menu,
)
return html

Expand Down
24 changes: 15 additions & 9 deletions app/models/query_consumption_daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ def __init__(self, headers, usage_point_id, config, activation_date=False):
self.cache = app.CACHE
self.url = URL
self.max_daily = 36
self.date_format = '%Y-%m-%d'

self.headers = headers
self.usage_point_id = usage_point_id
self.config = config

if activation_date is not None and activation_date:
self.activation_date = datetime.datetime.strptime(activation_date, "%Y-%m-%d%z").replace(tzinfo=None)
self.activation_date = datetime.datetime.strptime(activation_date, f"{self.date_format}%z").replace(tzinfo=None)
else:
self.activation_date = activation_date

Expand All @@ -32,8 +33,8 @@ def __init__(self, headers, usage_point_id, config, activation_date=False):
self.base_price = self.config['consumption_price_base']

def run(self, begin, end, cache=True):
begin = begin.strftime('%Y-%m-%d')
end = end.strftime('%Y-%m-%d')
begin = begin.strftime(self.date_format)
end = end.strftime(self.date_format)
app.LOG.log(f"Récupération des données : {begin} => {end}")
endpoint = f"daily_consumption/{self.usage_point_id}/start/{begin}/end/{end}"
if "cache" in self.config and self.config["cache"] and cache:
Expand All @@ -53,13 +54,19 @@ def run(self, begin, end, cache=True):
if hasattr(data, "status_code"):
if data.status_code == 200:
meter_reading = json.loads(data.text)['meter_reading']
next_date = meter_reading["interval_reading"][0]["date"]
for interval_reading in meter_reading["interval_reading"]:
value = interval_reading["value"]
date = interval_reading["date"]
app.LOG.show(date)
if str(date) != str(next_date):
while str(date) != str(next_date):
app.LOG.show(f"|{date}| != |{next_date.strftime(self.date_format)}|")
date = datetime.datetime.strptime(date, self.date_format) + relativedelta(days=1)
date = date.strftime(self.date_format)
self.cache.insert_consumption_daily(usage_point_id=self.usage_point_id, date=date,
value=value,
blacklist=blacklist)
next_date = datetime.datetime.strptime(date, self.date_format) + relativedelta(days=1)
return meter_reading["interval_reading"]
else:
return {
Expand Down Expand Up @@ -116,7 +123,7 @@ def get(self):
error = [
"Echec de la récupération des données.",
f' => {response["description"]}',
f" => {begin.strftime('%Y-%m-%d')} -> {end.strftime('%Y-%m-%d')}",
f" => {begin.strftime(self.date_format)} -> {end.strftime(self.date_format)}",
]
app.LOG.error(error)

Expand All @@ -128,9 +135,8 @@ def reset(self, date=None):
return True

def fetch(self, date):
result = self.run(datetime.datetime.strptime(date, "%Y-%m-%d") - relativedelta(days=1),
datetime.datetime.strptime(date, "%Y-%m-%d") + relativedelta(days=1), True)
print(result)
result = self.run(datetime.datetime.strptime(date, self.date_format) - relativedelta(days=1),
datetime.datetime.strptime(date, self.date_format) + relativedelta(days=1), False)
if "error" in result and result["error"]:
return {
"error": True,
Expand All @@ -145,5 +151,5 @@ def fetch(self, date):
}

def blacklist(self, date, action):
app.LOG.show(self.cache.blacklist_consumption_daily(self.usage_point_id, date, action))
self.cache.blacklist_consumption_daily(self.usage_point_id, date, action)
return True
2 changes: 1 addition & 1 deletion app/models/query_production_daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,5 @@ def fetch(self, date):
}

def blacklist(self, date, action):
app.LOG.show(self.cache.blacklist_production_daily(self.usage_point_id, date, action))
self.cache.blacklist_production_daily(self.usage_point_id, date, action)
return True

0 comments on commit 84e51c3

Please sign in to comment.