Skip to content

Commit 68e35d0

Browse files
committed
Added dump_json.py script
1 parent b7d6630 commit 68e35d0

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

tools/dump_json.py

+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/usr/bin/python3
2+
#
3+
4+
import datetime
5+
import asyncio
6+
import sys
7+
import json
8+
import requests
9+
import logging
10+
11+
logging.basicConfig()
12+
_LOGGER = logging.getLogger(__name__)
13+
_LOGGER.setLevel(level=logging.DEBUG) # Debug hack!
14+
15+
16+
class DaikinCloudController():
17+
"""Daikin Cloud Controller."""
18+
19+
def __init__(self):
20+
"""Initialize a new Daikin Cloud Controller."""
21+
tokenFile = 'tokenset.json';
22+
self.tokenSet = None
23+
with open(tokenFile) as jsonFile:
24+
jsonObject = json.load(jsonFile)
25+
self.tokenSet = jsonObject
26+
jsonFile.close()
27+
28+
configuration = {
29+
'issuer': 'https://cognito-idp.eu-west-1.amazonaws.com/eu-west-1_SLI9qJpc7',
30+
'authorization_endpoint': 'https://daikin-unicloud-prod.auth.eu-west-1.amazoncognito.com/oauth2/authorize',
31+
'userinfo_endpoint': 'userinfo_endpoint',
32+
'token_endpoint': 'https://daikin-unicloud-prod.auth.eu-west-1.amazoncognito.com/oauth2/token',
33+
'token_endpoint_auth_methods_supported': ['none']
34+
}
35+
_LOGGER.info("Initialized Daikin Cloud Controller")
36+
_LOGGER.debug("Daikin Cloud Controller token is [%s]",self.tokenSet['access_token'])
37+
38+
39+
async def doBearerRequest(self, resourceUrl, options=None, refreshed=False):
40+
if self.tokenSet == None:
41+
raise Exception('Please provide a TokenSet or use the Proxy server to Authenticate once')
42+
43+
if not resourceUrl.startswith('http'):
44+
resourceUrl = 'https://api.prod.unicloud.edc.dknadmin.be' + resourceUrl
45+
46+
headers = {
47+
'user-agent': 'Daikin/1.6.1.4681 CFNetwork/1209 Darwin/20.2.0',
48+
'x-api-key': 'xw6gvOtBHq5b1pyceadRp6rujSNSZdjx2AqT03iC',
49+
'Authorization': 'Bearer ' + self.tokenSet["access_token"],
50+
'Content-Type': 'application/json'
51+
}
52+
53+
54+
_LOGGER.debug('BEARER REQUEST URL: %s', resourceUrl)
55+
_LOGGER.debug("BEARER REQUEST HEADERS: %s",headers)
56+
if options != None and 'method' in options and options['method'] == 'PATCH':
57+
_LOGGER.debug("BEARER REQUEST JSON: %s",options['json'])
58+
res = requests.patch(resourceUrl, headers=headers, data=options['json'])
59+
else:
60+
res = requests.get(resourceUrl, headers=headers)
61+
_LOGGER.debug("BEARER RESPONSE CODE: %s",res.status_code)
62+
63+
if res.status_code == 200:
64+
try:
65+
return res.json()
66+
except:
67+
return res.text
68+
if res.status_code == 204:
69+
return True
70+
71+
if not refreshed and res.status_code == 401:
72+
_LOGGER.info("TOKEN EXPIRED: will refresh it (%s)",res.status_code)
73+
await self.refreshAccessToken()
74+
return await self.doBearerRequest(resourceUrl, options,True)
75+
76+
raise Exception('Communication failed! Status: ' + str(res.status_code))
77+
78+
79+
async def refreshAccessToken(self):
80+
"""Attempt to refresh the Access Token."""
81+
url = 'https://cognito-idp.eu-west-1.amazonaws.com'
82+
83+
headers = {
84+
'Content-Type': 'application/x-amz-json-1.1',
85+
'x-amz-target': 'AWSCognitoIdentityProviderService.InitiateAuth',
86+
'x-amz-user-agent': 'aws-amplify/0.1.x react-native',
87+
'User-Agent': 'Daikin/1.6.1.4681 CFNetwork/1220.1 Darwin/20.3.0'
88+
}
89+
ref_json= {
90+
'ClientId': '7rk39602f0ds8lk0h076vvijnb',
91+
'AuthFlow': 'REFRESH_TOKEN_AUTH',
92+
'AuthParameters': {
93+
'REFRESH_TOKEN': self.tokenSet["refresh_token"]
94+
}
95+
}
96+
res = requests.post(url, headers=headers, json=ref_json)
97+
_LOGGER.info("REFRESHACCESSTOKEN RESPONSE CODE: %s",res.status_code)
98+
_LOGGER.debug("REFRESHACCESSTOKEN RESPONSE: %s",res.json())
99+
res_json = res.json()
100+
if res.status_code != 200:
101+
raise Exception('Token refresh was not successful! Status: ' + str(res.status_code))
102+
103+
if res_json['AuthenticationResult'] != None and res_json['AuthenticationResult']['AccessToken'] != None and res_json['AuthenticationResult']['TokenType'] == 'Bearer':
104+
self.tokenSet['access_token'] = res_json['AuthenticationResult']['AccessToken']
105+
self.tokenSet['id_token'] = res_json['AuthenticationResult']['IdToken']
106+
self.tokenSet['expires_at'] = int(datetime.datetime.now().timestamp()) + int(res_json['AuthenticationResult']['ExpiresIn'])
107+
_LOGGER.debug("NEW TOKENSET: %s",self.tokenSet)
108+
with open('tokenset.json', 'w') as outfile:
109+
json.dump(self.tokenSet, outfile)
110+
111+
return self.tokenSet;
112+
raise Exception('Token refresh was not successful! Status: ' + str(res.status_code))
113+
114+
115+
async def getApiInfo(self):
116+
"""Get Daikin API Info."""
117+
return await self.doBearerRequest('/v1/info')
118+
119+
120+
async def getCloudDeviceDetails(self):
121+
"""Get pure Device Data from the Daikin cloud devices."""
122+
return await self.doBearerRequest('/v1/gateway-devices')
123+
124+
async def getCloudDeviceData(self, devId):
125+
"""Get pure Device Data of a single Daikin cloud device."""
126+
return await self.doBearerRequest('/v1/gateway-devices/' + devId)
127+
128+
129+
async def main():
130+
131+
controller = DaikinCloudController()
132+
resp = await controller.getApiInfo()
133+
_LOGGER.info("\nAPI INFO: %s\n",resp)
134+
resp = await controller.getCloudDeviceDetails()
135+
with open("daikin_data.json", 'w') as jsonFile:
136+
_LOGGER.info("Dumping json file daikin_data.json\n")
137+
json.dump(resp, jsonFile, indent=4, sort_keys=True)
138+
139+
asyncio.run(main())
140+
141+
142+
print ("END")
143+
144+
sys.exit(0)
145+

0 commit comments

Comments
 (0)