Skip to content

Commit

Permalink
fix: minor corrections to PR #39
Browse files Browse the repository at this point in the history
~ Changes in logging/print of API request success/error
~ Fixes in formatting
  • Loading branch information
fetzu committed Oct 20, 2023
1 parent c7373e3 commit bc57749
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions teslamate_mqtt2abrp.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import os
import paho.mqtt.client as mqtt
import requests
import json
from time import sleep
from docopt import docopt

Expand Down Expand Up @@ -149,13 +148,13 @@ def on_message(client, userdata, message):
data["speed"] = int(payload)
elif topic_postfix == "power":
data["power"] = float(payload)
if(data["is_charging"]==True and int(payload)<-11):
data["is_dcfc"]=True
if(data["is_charging"] == True and int(payload)<-11):
data["is_dcfc"] = True
elif topic_postfix == "charger_power":
if(payload!='' and int(payload)!=0):
data["is_charging"]=True
data["is_charging"] = True
if int(payload)>11:
data["is_dcfc"]=True
data["is_dcfc"] = True
elif topic_postfix == "heading":
data["heading"] = int(payload)
elif topic_postfix == "outside_temp":
Expand All @@ -180,27 +179,27 @@ def on_message(client, userdata, message):
del data["voltage"]
elif topic_postfix == "shift_state":
if payload == "P":
data["is_parked"]=True
data["is_parked"] = True
elif(payload == "D" or payload == "R"):
data["is_parked"]=False
data["is_parked"] = False
elif topic_postfix == "state":
state = payload
if payload=="driving":
data["is_parked"]=False
data["is_charging"]=False
data["is_dcfc"]=False
elif payload=="charging":
data["is_parked"]=True
data["is_charging"]=True
data["is_dcfc"]=False
elif payload=="supercharging":
data["is_parked"]=True
data["is_charging"]=True
data["is_dcfc"]=True
elif(payload=="online" or payload=="suspended" or payload=="asleep"):
data["is_parked"]=True
data["is_charging"]=False
data["is_dcfc"]=False
if payload == "driving":
data["is_parked"] = False
data["is_charging"] = False
data["is_dcfc"] = False
elif payload == "charging":
data["is_parked"] = True
data["is_charging"] = True
data["is_dcfc"] = False
elif payload == "supercharging":
data["is_parked"] = True
data["is_charging"] = True
data["is_dcfc"] = True
elif(payload == "online" or payload == "suspended" or payload == "asleep"):
data["is_parked"] = True
data["is_charging"] = False
data["is_dcfc"] = False
elif topic_postfix == "usable_battery_level": #State of Charge of the vehicle (what's displayed on the dashboard of the vehicle is preferred)
data["soc"] = int(payload)
elif topic_postfix == "charge_energy_added":
Expand All @@ -216,7 +215,7 @@ def on_message(client, userdata, message):
#print("Unneeded topic:", message.topic, payload)

# Calculate acurrate power on AC charging
if data["is_charging"]==True and data["is_dcfc"]==False and "voltage" in data and "current" in data:
if data["is_charging"] == True and data["is_dcfc"] == False and "voltage" in data and "current" in data:
data["power"] = float(data["current"] * data["voltage"] * charger_phases) / 1000.0 * -1

return
Expand Down Expand Up @@ -280,18 +279,15 @@ def updateABRP():
global APIKEY
global USERTOKEN

msgDetails = "Data object to send:"
print(msgDetails, data)

try:
headers = {"Authorization": "APIKEY "+APIKEY}
body = {"tlm": data}
response = requests.post("https://api.iternio.com/1/tlm/send?token="+USERTOKEN, headers=headers, json=body)
resp = response.json()
if resp["status"] != "ok":
print("Response from ABRP:", response.text)
print("Error, response from the ABRP API:", response.text)
else:
print("Response from ABRP:", resp["status"])
print("Data object successfully sent:", data)
except Exception as ex:
print("Unexpected exception while calling ABRP API:", sys.exc_info()[0])
print(ex)
Expand All @@ -311,7 +307,7 @@ def updateABRP():
str_now = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
msg = str_now + ": Car is " + state
if(state == "parked" or state == "online" or state == "suspended" or state=="asleep" or state=="offline"): #if parked, update every 30 cylces/seconds
if data["power"] != 0: #Sometimes after charging it keeps the last Power and not refresh any more until new drive or charge.
if data["power"] != 0: #sometimes after charging the last power value is kept and not refreshed until the next drive or charge session.
data["power"] = 0.0
if "kwh_charged" in data:
del data["kwh_charged"]
Expand Down

0 comments on commit bc57749

Please sign in to comment.