Skip to content

Commit

Permalink
feat: first version
Browse files Browse the repository at this point in the history
  • Loading branch information
rokam committed Feb 15, 2023
1 parent c0bf8d2 commit 8be124f
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# sunweg
Python lib for WEG solar platform
Python lib for WEG solar energy platform

22 changes: 22 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/python
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name='sunweg',
version='0.0.1',
author="rokam",
author_email="[email protected]",
description="A library to retrieve data from sunweg.net",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/rokam/sunweg",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)
Empty file added sunweg/__init__.py
Empty file.
65 changes: 65 additions & 0 deletions sunweg/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from datetime import datetime
import json
import requests

from sunweg.const import SUNWEG_LOGIN_PATH, SUNWEG_PLANT_DETAIL_PATH, SUNWEG_PLANT_LIST_PATH, SUNWEG_URL
from sunweg.plant import Plant
from sunweg.util import SingletonMeta

class APIHelper(metaclass=SingletonMeta):
def __init__(self, username:str, password:str) -> None:
self._token = None
self._username = username
self._password = password

def authenticate(self)->bool:
userdata = json.dumps({"usuario":self._username,"senha":self._password}, default=lambda o: o.__dict__,
sort_keys=True, indent=4)
response = json.loads(requests.post(SUNWEG_URL+SUNWEG_LOGIN_PATH, userdata).text)
self._token = response["token"]
return response["success"]

def __headers(self):
return {'X-Auth-Token-Update' : self._token}


def listPlants(self)->list:
req = requests.get(SUNWEG_URL+SUNWEG_PLANT_LIST_PATH, headers=self.__headers())
if req.status_code == 401:
self.authenticate()
req = requests.get(SUNWEG_URL+SUNWEG_PLANT_LIST_PATH, headers=self.__headers())

if req.status_code != 200:
return []

response = json.loads(req.text)
if not response["success"]:
return []

ret_list = []
for usina in response["usinas"]:
ret_list.append(self.plant(usina["id"]))
return ret_list

def plant(self, id:int)->Plant:
req = requests.get(SUNWEG_URL+SUNWEG_PLANT_DETAIL_PATH+str(id), headers=self.__headers())
if req.status_code == 401:
self.authenticate()
req = requests.get(SUNWEG_URL+SUNWEG_PLANT_DETAIL_PATH+str(id), headers=self.__headers())

if req.status_code != 200:
return None
response = json.loads(req.text)
if not response["success"]:
return None

return Plant(id,
response["usinas"]["nome"],
float(str(response["AcumuladoPotencia"]).replace(" kW","").replace(",",".")),
float(str(response["KWHporkWp"]).replace(",",".")),
response["taxaPerformance"],
response["economia"],
float(str(response["energiaGeradaHoje"]).replace(" kWh","").replace(",",".")),
float(response["energiaacumuladanumber"]),
response["reduz_carbono_total_number"],
datetime.strptime(response["ultimaAtualizacao"],"%Y-%m-%d %H:%M:%S"))
4 changes: 4 additions & 0 deletions sunweg/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SUNWEG_URL = 'https://api.sun.weg.net/'
SUNWEG_LOGIN_PATH = 'login/autenticacao'
SUNWEG_PLANT_LIST_PATH = 'usinas/getpaineloperacao?procurar=&integrador=&franqueado=&portal=&alarme=&planos=%5B%220%22,%221%22,%222%22,%223%22,%224%22%5D&status=%5B%221%22,%222%22,%223%22,%224%22,%225%22%5D'
SUNWEG_PLANT_DETAIL_PATH = 'usinas/view?idusina='
15 changes: 15 additions & 0 deletions sunweg/plant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from datetime import datetime

class Plant:
def __init__(self, id:int, name:str, total_power:float, kwh_per_kwp:float, performance_rate:float, saving:float,
today_energy:float, total_energy:float, total_carbon_saving:float, last_update:datetime):
self.id = id
self.name = name
self.total_power = total_power
self.kwh_per_kwp = kwh_per_kwp
self.performance_rate = performance_rate
self.saving = saving
self.today_energy = today_energy
self.total_energy = total_energy
self.total_carbon_saving = total_carbon_saving
self.last_update = last_update
9 changes: 9 additions & 0 deletions sunweg/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class SingletonMeta(type):

_instances = {}

def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
instance = super().__call__(*args, **kwargs)
cls._instances[cls] = instance
return cls._instances[cls]

0 comments on commit 8be124f

Please sign in to comment.