-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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=' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |