-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
189 lines (174 loc) · 6.9 KB
/
Copy pathlambda_function.py
File metadata and controls
189 lines (174 loc) · 6.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import json
import requests
import boto3
from datetime import datetime
from datetime import time
from decimal import Decimal
import time as time_
import os
import base64
def lambda_handler(event, context):
MELCLOUD_USER = KMSDecrypt(os.environ["MELCLOUD_USER"])
MELCLOUD_PASS = KMSDecrypt(os.environ["MELCLOUD_PASS"])
StructureName = event['StructureName']
DeviceName = event['DeviceName']
Action = event.get('Action','SAVE')
Token = doLogin (MELCLOUD_USER, MELCLOUD_PASS)
if Action == 'GET':
Result = getDeviceByID(Token)
if Action == 'SET':
Device = getDeviceByID(Token)
ConsignTemp = event.get('Temperature',None)
if ConsignTemp is not None:
Device['SetTemperature'] = ConsignTemp
Result = SetAta(Token, Device)
if Action == 'SET_FAN':
Device = getDeviceByID(Token)
ConsignFanSpeed = event.get('FanSpeed',None)
if ConsignFanSpeed is not None:
Device['SetFanSpeed'] = ConsignFanSpeed
Result = SetAta(Token, Device)
if Action == 'SET_ON':
Device = getDeviceByID(Token)
Device['Power'] = True
Device['EffectiveFlags']= 1
Result = SetAta(Token, Device)
if Action == 'SET_ON_AT':
Device = getDeviceByID(Token)
Device['Power'] = True
Device['EffectiveFlags']= 1
Device['SetTemperature'] = ConsignTemp
Result = SetAta(Token, Device)
if Action == 'SET_ON_INC':
Device = getDeviceByID(Token)
Device['Power'] = True
Device['EffectiveFlags']= 1
Device['SetTemperature'] = Device['SetTemperature'] + ConsignTemp
Result = SetAta(Token, Device)
if Action == 'SET_OFF':
Device = getDeviceByID(Token)
Device['EffectiveFlags']= 1
Device['Power'] = False
#Device['EffectiveFlags']= 0
Result = SetAta(Token, Device)
if Action == 'SAVE':
TableDB = 'MelcsDevice'
Device = getDeviceByID(Token)
putDB(Device,TableDB)
return Result
def doLogin(User,Password):
url = 'https://app.melcloud.com/Mitsubishi.Wifi.Client/Login/ClientLogin'
body = {
"AppVersion": "1.18.5.1",
"CaptchaResponse": None,
"Email": User,
"Language": 6,
"Password": Password,
"Persist": False
}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(body), headers=headers)
response = response.json()
LoginData = response.get('LoginData')
Token = LoginData.get('ContextKey')
return Token
def getDeviceByID(Token):
response = requests.get(
'https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/Get?id=134314&buildingID=77048',
params={'q': 'requests+language:python'},
headers={'Host': 'app.melcloud.com',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:69.0) Gecko/20100101 Firefox/69.0',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate, br',
'X-MitsContextKey': Token,
'X-Requested-With': 'XMLHttpRequest',
'DNT': '1',
'Connection': 'keep-alive',
'Referer': 'https://app.melcloud.com/',
'Cookie': 'policyaccepted=true',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache'},
)
json_response = response.json()
return json_response
def getListDevices(Token):
response = requests.get(
'https://app.melcloud.com/Mitsubishi.Wifi.Client/User/ListDevices',
params={'q': 'requests+language:python'},
headers={'Host': 'app.melcloud.com',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:69.0) Gecko/20100101 Firefox/69.0',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate, br',
'X-MitsContextKey': Token,
'X-Requested-With': 'XMLHttpRequest',
'DNT': '1',
'Connection': 'keep-alive',
'Referer': 'https://app.melcloud.com/',
'Cookie': 'policyaccepted=true',
'Pragma': 'no-cache',
'Cache-Control': 'no-cache'},
)
json_response = response.json()
return json_response
def getDevices(Token, StructureName):
Structures = getListDevices(Token)
for Structure in Structures:
if Structure['Name'] == StructureName:
return Structure['Structure']['Devices']
def getDevice (Token, StructureName, DeviceName):
Devices = getDevices(Token, StructureName)
for Device in Devices:
return Device
if Device['DeviceName'] == DeviceName:
return Device
def SetAta (Token, Values):
url = 'https://app.melcloud.com/Mitsubishi.Wifi.Client/Device/SetAta'
body = Values
headers = { 'Host': 'app.melcloud.com',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:69.0) Gecko/20100101 Firefox/69.0',
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Language': 'es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3',
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'application/json; charset=utf-8',
'X-MitsContextKey': Token,
'X-Requested-With': 'XMLHttpRequest',
'DNT': '1',
'Connection': 'keep-alive',
'Referer': 'https://app.melcloud.com/',
'Cookie': 'policyaccepted=true'}
dt = datetime.utcfromtimestamp((millis()+60000)/1000)
body['HasPendingCommand'] = True
dtstring = dt.isoformat()+'Z'
body['NextCommunication'] = dtstring.replace('000Z','')
print(json.dumps(body))
response = requests.post(url, data=json.dumps(body), headers=headers)
#response = response.json()
#type(response)
return response.status_code
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
if abs(o) % 1 > 0:
return float(o)
else:
return int(o)
return super(DecimalEncoder, self).default(o)
def millis():
return int(round(time_.time() * 1000))
def putDB(item,tableDB):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table(tableDB)
ymd = datetime.today().strftime('%Y%m%d')
timestamp = datetime.now().microsecond
item['ymd']= ymd
item['timestamp']= millis()
response = table.put_item(Item=json.loads(json.dumps(item), parse_float=Decimal))
print('PutItem succeeded:')
print(json.dumps(response, indent=4, cls=DecimalEncoder))
def KMSDecrypt(encryptedVar):
cipherTextBlob = base64.b64decode(encryptedVar)
decrytedVar = boto3.client('kms').decrypt(CiphertextBlob=cipherTextBlob)['Plaintext']
return str(decrytedVar, 'utf-8')