Advertisement
rhessellund

clever-test/2.daily.py

Jan 7th, 2023 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #
  3. import json
  4. import sqlite3
  5. import requests
  6. import sqlite3
  7. from datetime import datetime, timezone
  8.  
  9. config = {}
  10.  
  11. con = sqlite3.connect("/home/azureuser/clever/clever.db")
  12. cur = con.cursor()
  13.  
  14. # Read config from DB
  15. r = cur.execute("SELECT * from Config")
  16. for x, y in r.fetchall():
  17.     config[x] = y
  18.  
  19. print(config)
  20.  
  21. url = (
  22.     "https://mobileapp-backend.clever.dk/api/mobile/customer/loginWithSecretCode?secret="
  23.     + config["userSecret"]
  24.     + "&email="
  25.     + config["email"]
  26. )
  27.  
  28.  
  29. headers = {
  30.     "content-type": "application/json",
  31.     "accept": "*/*",
  32.     "authorization": "Basic bW9iaWxlYXBwOmFwaWtleQ==",
  33.     "app-version": "2.8.0",
  34.     "app-os": "15.7",
  35.     "app-platform": "iOS",
  36.     "app-device": "iPhone9,3",
  37.     "accept-encoding": "gzip, deflate, br",
  38.     "accept-language": "da-DK,da;q=0.9",
  39. }
  40.  
  41. r = requests.get(url, headers=headers)
  42. print(r.json())
  43. dayCode = str(r.json()["data"])
  44. print("dayCode='" + dayCode + "'")
  45. config["dayCode"] = dayCode
  46.  
  47.  
  48. url = "https://mobileapp-backend.clever.dk/api//v3/" + dayCode + "/installations?"
  49. r = requests.get(url, headers=headers)
  50. print(r.json())
  51.  
  52. connectorId = r.json()["data"][0]["connectorId"]
  53. chargeBoxId = r.json()["data"][0]["chargeBoxId"]
  54.  
  55. print("---")
  56. print("connectorId=" + str(connectorId))
  57. print("chargeBoxId='" + chargeBoxId + "'")
  58. config["connectorId"] = str(connectorId)
  59. config["chargeBoxId"] = chargeBoxId
  60.  
  61. print("---")
  62. # Write config to DB
  63. print("Writing config...")
  64. for x in config:
  65.     sql = "UPDATE Config SET value = '" + config[x] + "' WHERE var = '" + x + "';"
  66.     print("   " + sql)
  67.     cur.execute(sql)
  68.  
  69.  
  70. url = (
  71.     "https://mobileapp-backend.clever.dk/api//v2/consumption/"
  72.     + config["dayCode"]
  73.     + "/history?lastUpdatedAt="
  74.     + datetime.now().isoformat()
  75.     + "%2B02:00"
  76. )
  77.  
  78. r = requests.get(url, headers=headers)
  79. # print(r.json())
  80.  
  81. for x in r.json()["data"]["consumptionRecords"]:
  82.     # print(x)
  83.     if x["idTokenSubscriptionLineId"] == None:
  84.         x["idTokenSubscriptionLineId"] = "None"
  85.     sql = (
  86.         "insert into CleverHistory VALUES('"
  87.         + x["chargePointId"]
  88.         + "',"
  89.         + str(x["connectorId"])
  90.         + ","
  91.         + str(x["transactionId"])
  92.         + ",'"
  93.         + x["idTag"]
  94.         + "',"
  95.         + str(x["startTimeLocal"])
  96.         + ","
  97.         + str(x["stopTimeLocal"])
  98.         + ","
  99.         + str(x["startTimeUtc"])
  100.         + ","
  101.         + str(x["stopTimeUtc"])
  102.         + ","
  103.         + str(x["kWh"])
  104.         + ",'"
  105.         + x["idTokenSubscriptionLineId"]
  106.         + "');"
  107.     )
  108.     try:
  109.         cur.execute(sql)
  110.         print("success....: " + sql)
  111.     except:
  112.         print("failed.....: " + sql)
  113.         sql = (
  114.         "UPDATE CleverHistory SET kwh = "
  115.         + str(x["kWh"])
  116.         + " WHERE startTimeUtc = "
  117.         + str(x["startTimeUtc"])
  118.         + ";"
  119.         )
  120.         try:
  121.             cur.execute(sql)
  122.             print("success....: " + sql)
  123.         except:
  124.             print("failed.....: " + sql)
  125.             pass
  126.  
  127. con.commit()
  128. con.close()
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement