Skip to content

Commit

Permalink
manage erreur lors du call contract
Browse files Browse the repository at this point in the history
meilleure gestion des reprises sur erreur
  • Loading branch information
saniho committed Jun 3, 2021
1 parent d4db63f commit 7fcfdff
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion custom_components/apiEnedis/const.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" Constants """

__VERSION__ = "1.2.0.4rc1" # attention updater aussi manifest.json
__VERSION__ = "1.2.0.4rc2" # attention updater aussi manifest.json
__name__ = "myEnedis"

try:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/apiEnedis/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "myEnedis sensor",
"documentation": "https://github.com/saniho/apiEnedis/",
"config_flow": true,
"version": "1.2.0.4rc1",
"version": "1.2.0.4rc2",
"requirements": [
],
"dependencies": [],
Expand Down
2 changes: 1 addition & 1 deletion custom_components/apiEnedis/myCall.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ def post_and_get_json(self, url, params=None, data=None, headers=None):
import json, requests
session = requests.Session()
session.verify = True
#print("ici", params, headers, data)
response = session.post(url, params=params, data=json.dumps(data), headers=headers, timeout=30)
response.raise_for_status()
dataAnswer = response.json()
self.setLastAnswer(dataAnswer)
#print("ici", params, headers, data)
#raise(requests.exceptions.Timeout) # pour raiser un timeout de test ;)
return dataAnswer
except requests.exceptions.Timeout as error:
Expand Down
58 changes: 27 additions & 31 deletions custom_components/apiEnedis/myClientEnedis.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,13 +543,6 @@ def getCallPossible(self, currentDateTime = datetime.datetime.now()):
log.info("myEnedis ...new update self.getTimeLastCall() : %s ??" %self.getTimeLastCall())
log.info("myEnedis ...new update self.getStatusLastCall() : %s??" %self.getStatusLastCall())
log.info("myEnedis ...new update self.getDelaiIsGoodAfterError() : %s??" %self.getDelaiIsGoodAfterError(currentDateTime))
#print("myEnedis ...new update self.getTimeLastCall() : %s ??" %self.getTimeLastCall())
#print("myEnedis ...new update self.getAppelAEffectuer() : %s ??" %self.getAppelAEffectuer())
#print("myEnedis ...new update self.getStatusLastCall() : %s??" %self.getStatusLastCall())
#print("myEnedis ...new update self.getDelaiIsGoodAfterError() : %s??" %self.getDelaiIsGoodAfterError())
callpossible = ( self.getHorairePossible() and self.getLastCallHier()) or \
((self.getTimeLastCall() == None) or
(self.getStatusLastCall() == False and self.getDelaiIsGoodAfterError(currentDateTime)))
# new callpossible ???
callpossible = ( self.getHorairePossible() and
( self.getLastCallHier() or (self.getTimeLastCall() == None) or
Expand All @@ -574,11 +567,11 @@ def update(self):
self.updateErrorLastCall("")
self.updateLastMethodCall("")
self.setUpdateRealise(True)
if (self.isConsommation()):
self._niemeAppel += 1
if (self.getStatusLastCall() or self.getLastMethodCallError() == "updateYesterday"):
self.updateYesterday()
try:
try:
if (self.isConsommation()):
self._niemeAppel += 1
if (self.getStatusLastCall() or self.getLastMethodCallError() == "updateYesterday"):
self.updateYesterday()
if (self.getStatusLastCall() or self.getLastMethodCallError() == "updateCurrentWeek"):
self.updateCurrentWeek()
if (self.getStatusLastCall() or self.getLastMethodCallError() == "updateLastWeek"):
Expand Down Expand Up @@ -611,25 +604,28 @@ def update(self):
self.updateTimeLastCall()
self.updateStatusLastCall(True)
log.info("mise à jour effectuee")
except Exception as inst:
if (inst.args[:2] == ("call", "error")): # gestion que c'est pas une erreur de contrat trop recent ?
log.error("%s - Erreur call ERROR %s" % (self.getContract().get_PDL_ID(), inst))
# Erreur lors du call...
self.updateTimeLastCall()
self.updateStatusLastCall(False)
self.updateErrorLastCall(
"%s - %s" % (messages.getMessage(inst.args[2]), myCalli.getLastAnswer()))
log.error("%s - last call : %s" % (self.getContract().get_PDL_ID(), self.getLastMethodCall()))
else:
raise Exception(inst)

if (self.isProduction()):
if (self.getStatusLastCall() or self.getLastMethodCallError() == "updateProductionYesterday"):
self.updateYesterdayProduction()
self.updateTimeLastCall()
self.updateStatusLastCall(True)
log.error("myEnedis ...%s update termine, status courant : %s, lastCall :%s" \
% (self.getContract().get_PDL_ID(), self.getStatusLastCall(), self.getLastMethodCallError()))

if (self.isProduction()):
if (self.getStatusLastCall() or self.getLastMethodCallError() == "updateProductionYesterday"):
self.updateYesterdayProduction()
self.updateTimeLastCall()
self.updateStatusLastCall(True)
log.error("myEnedis ...%s update termine, status courant : %s, lastCall :%s" \
% (self.getContract().get_PDL_ID(), self.getStatusLastCall(), self.getLastMethodCallError()))

except Exception as inst:
if (inst.args[:2] == (
"call", "error")): # gestion que c'est pas une erreur de contrat trop recent ?
log.error("%s - Erreur call ERROR %s" % (self.getContract().get_PDL_ID(), inst))
# Erreur lors du call...
self.updateTimeLastCall()
self.updateStatusLastCall(False)
self.updateErrorLastCall(
"%s - %s" % (messages.getMessage(inst.args[2]), myCalli.getLastAnswer()))
log.error(
"%s - last call : %s" % (self.getContract().get_PDL_ID(), self.getLastMethodCall()))
else:
raise Exception(inst)

except Exception as inst:
if (inst.args == ("call", None)):
Expand Down
14 changes: 10 additions & 4 deletions testEnedis.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
_production,
)

dateRepertoire = "20210527"
dateRepertoire = "20210603"
def writeDataJson( myDataEnedis ):
directory = "../myCredential/%s/" %(dateRepertoire)
for clef in myDataEnedis.getDataJsonKey():
Expand Down Expand Up @@ -43,9 +43,9 @@ def testMulti():
heuresCreusesON = True

# Lecture fichier Json de sortie
dataJson = readDataJson()
#dataJson = readDataJson()
dataJson = {}
myDataEnedis = myClientEnedis.myClientEnedis( token=token, PDL_ID=PDL_ID, delai=3600,
myDataEnedis = myClientEnedis.myClientEnedis( token=token, PDL_ID=PDL_ID, delai=7200,
heuresCreuses=heureCreusesCh, heuresCreusesCost=0.0797, heuresPleinesCost=0.1175,
version = __version__, heuresCreusesON=heuresCreusesON )
myDataEnedis.setDataJsonDefault( dataJsonDefault = dataJson)
Expand All @@ -55,6 +55,12 @@ def testMulti():
print("possible ? %s "%(callPossible))
print("** on tente une maj ??")
myDataEnedis.getData()
currentDateTime = (datetime.datetime.now() + datetime.timedelta(hours=1))
callPossible = myDataEnedis.getCallPossible(currentDateTime)
print("possible %s ? %s "%(currentDateTime, callPossible))
currentDateTime = (datetime.datetime.now() + datetime.timedelta(hours=5))
callPossible = myDataEnedis.getCallPossible(currentDateTime)
print("possible %s ? %s "%(currentDateTime, callPossible))
currentDateTime = (datetime.datetime.now() + datetime.timedelta(days=1))
callPossible = myDataEnedis.getCallPossible(currentDateTime)
print("possible %s ? %s "%(currentDateTime, callPossible))
Expand All @@ -67,7 +73,7 @@ def testMulti():
#print("consommation : %s" %myDataEnedis.getYesterday().getValue() )

# SORTIE OUTPUT
#writeDataJson( myDataEnedis )
writeDataJson( myDataEnedis )

# ***********************************
# ***********************************
Expand Down

0 comments on commit 7fcfdff

Please sign in to comment.