-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
141 lines (127 loc) · 5.49 KB
/
main.py
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
# Copyright 2016 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import webapp2
from google.appengine.ext import blobstore
from google.appengine.ext.webapp import blobstore_handlers
from internal.datastore import Datastore
from internal.iot import IOT
from internal.util import decode64, get_region_from_user, validate_region, get_region_id
class Data(webapp2.RequestHandler):
def get(self):
"""Fetches telemetry data for a paticular device.
curl -X GET "[URL]/data?registry=[REGISTRY_NAME]&states=[NUMBER_OF_STATES]&device=[DEVICE_NAME]" """
dev = self.request.get('device')
reg = self.request.get('registry')
states = self.request.get('states')
self.response.headers['Content-Type'] = 'text/plain'
if (not dev) or len(dev)==0:
self.response.write('parameter dev not found')
elif (not reg) or len(reg)==0:
self.response.write('parameter reg not found')
elif (not states) or states <= 0:
self.response.write('invalid or no states found')
else:
# Get user account
ds = Datastore()
user = ds.get_registry(reg)
if len(user) == 0:
self.response.write("Registry does not exist")
else:
region = get_region_from_user(user)
# Add Device on IOT Core
iot = IOT()
success, message = iot.get_data(dev, reg, states, region)
if success:
try:
for msg in message['deviceStates']:
msg['binaryData'] = decode64(msg['binaryData'])
self.response.write(message)
except:
self.response.write([])
else:
self.response.write(message)
class Accounts(webapp2.RequestHandler):
def get(self):
"""Checks if a registry exists.
curl -X GET [URL]/accounts?registry=[REGISTRY_NAME] """
reg = self.request.get('registry')
if reg and len(reg) > 0:
ds = Datastore()
self.response.headers['Content-Type'] = 'text/plain'
try:
check = ds.check_registry_exists(reg)
if check:
self.response.write("Registry Account Exists")
else:
self.response.write("Registry Account Not Found")
except Exception as error:
self.response.write(error.message)
else:
self.response.write('parameter reg not found')
def post(self):
"""Creates a new registry
curl -X POST -d "registry=[REGISTRY_NAME]®ion=['us' or 'eu']" [URL]/accounts """
reg = self.request.get('registry')
region_name = self.request.get('region')
if reg and len(reg) > 0 and reg.isalnum() and validate_region(region_name):
region = get_region_id(region_name)
# Create Registry on IOT Core
iot = IOT()
success, message = iot.create_registry(region,reg)
if success:
# Add registry to Datastore
ds = Datastore()
status = ds.add_registry(reg, region_name)
self.response.headers['Content-Type'] = 'text/plain'
if status:
self.response.write('Registry Added')
else:
self.response.write('Registry already exists')
else:
self.response.write(message)
else:
self.response.write('invalid parameters: ' + reg + " " + region_name )
def put(self):
"""Creates a new device.
curl -X PUT -F data=@[PATH_TO_PUBLIC_KEY] -F registry=[REGISTRY_NAME] -F device=[DEVICE_NAME] [URL]/accounts """
dev = self.request.get('device')
reg = self.request.get('registry')
uploaded_file = self.request.POST.get('data')
data = uploaded_file.file.read()
self.response.headers['Content-Type'] = 'text/plain'
if (not dev) and len(dev)==0:
self.response.write('parameter device not found')
elif (not reg) and len(reg)==0:
self.response.write('parameter registry not found')
elif (not data) and len(data)==0:
self.response.write('invalid or no key file found')
else:
# Get user account
ds = Datastore()
user = ds.get_registry(reg)
if len(user) == 0:
self.response.write("Registry does not exist")
else:
region = get_region_from_user(user)
# Add Device on IOT Core
iot = IOT()
success, message = iot.create_device(dev, reg, data, region)
if success:
self.response.write('Device Added')
else:
self.response.write(message)
app = webapp2.WSGIApplication([
('/accounts', Accounts),
('/data', Data)
], debug=True)