-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_manager.py
More file actions
executable file
·220 lines (173 loc) · 8.19 KB
/
Copy pathapi_manager.py
File metadata and controls
executable file
·220 lines (173 loc) · 8.19 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python
#Author : Kevin Murphy
#Date : 16 - Dec - 14
#
#API class handling all http requests
import requests
import json
import time
import os
from threading import Timer
from configurable import Configurable
import constants as CONSTS
from sensor_factory import SensorFactory
from sensor_manager import SensorManager
from camera_manager import CameraManager
class APIManager(Configurable):
DEBUG = True
LOGTAG = "APIManager"
__sensorManager = None
__configurationManager = None
__polling = True
#Configurables
__systemConfigRequestRate = CONSTS.REQUEST_RATE_SYSTEM_CONFIG
__sensorValueUploadRate = CONSTS.REQUEST_RATE_UPLOAD_SENSOR_VALUES
__cameraImageUploadRate = CONSTS.REQUEST_RATE_UPLOAD_CAMERA_IMAGE
def __init__(self, sensorManager):
super(APIManager, self).__init__(CONSTS.JSON_KEY_API_CONFIG)
if self.DEBUG:
print self.LOGTAG, " :: Created"
if sensorManager is not None:
self.__sensorManager = sensorManager
self.schedule_UploadSensorValues()
self.schedule_UploadCameraStill()
self.schedule_SysConfigCheck()
self.schedule_UploadCameraVideo()
def configure(self, config):
if self.DEBUG:
print self.LOGTAG, ":: Configuring"
if config is not None:
try:
apiConfig = config[self.getJsonConfigKey()]
self.setSensorValueUploadRate(apiConfig[CONSTS.JSON_KEY_API_SENSOR_VALUE_UPLOAD_RATE])
#self.setSystemConfigRequestRate(apiConfig[CONSTS.JSON_KEY_API_SYSTEM_CONFIG_REQUEST_RATE])
self.setCameraImageUploadRate(apiConfig[CONSTS.JSON_KEY_API_CAMERA_IMAGE_UPLOAD_RATE])
except KeyError:
if self.DEBUG:
print self.LOGTAG, " :: Config not present"
#---------API CALLS-----------------------------
def getSystemConfig(self):
configResponse = self.sendRequest(service=CONSTS.JSON_VALUE_REQUEST_SERVICE_GET_CONFIG, payload=None, filez=None)
if self.__polling:
self.schedule_SysConfigCheck()
if configResponse is not None and self.getConfigManager() is not None:
self.getConfigManager().reconfigure(configResponse)
def uploadSensorValues(self):
self.sendRequest(service=CONSTS.JSON_VALUE_REQUEST_SERVICE_UPLOAD_SENSOR_VALUES, payload=self.__sensorManager.getSensorValues(), filez=None)
if self.__polling:
self.schedule_UploadSensorValues()
def uploadImage(self):
#Should select latest image dynamically
images = os.listdir(CONSTS.DIR_CAMERA_STILL)
if len(images) > 0:
camera_image = {CONSTS.JSON_KEY_CAMERA_STILL : (images[0], open(CONSTS.DIR_CAMERA_STILL + images[0], 'rb'), 'image/png')}
self.sendRequest(service=None, payload=None, filez=camera_image)
if self.__polling:
self.schedule_UploadCameraStill()
def uploadVideo(self):
videos = os.listdir(CONSTS.DIR_CAMERA_VIDEO)
if len(videos) > 0:
if self.DEBUG:
print self.LOGTAG, " :: Uploading Video -> ", videos[0]
camera_video = {CONSTS.JSON_KEY_CAMERA_STILL : (videos[0], open(CONSTS.DIR_CAMERA_VIDEO + videos[0], 'rb'), 'video/mp4')}
self.sendRequest(service=None, payload=None, filez=camera_video)
if self.__polling:
self.schedule_UploadCameraVideo()
def getPNRegIDs(self):
return self.sendRequest(service=CONSTS.JSON_VALUE_REQUEST_SERVICE_GET_REG_IDS, payload=None, filez=None)
#Purely for testing server side api
def getLatestSensorValues(self):
return self.sendRequest(service=CONSTS.JSON_VALUE_REQUEST_SERVICE_GET_SENSOR_VALUES, payload=None, filez=None)
#Purely for testing server side api
def updateSystemConfig(self):
file = open('config/test_config.json', 'r')
return self.sendRequest(service=CONSTS.JSON_VALUE_REQUEST_SERVICE_UPDATE_CONFIG, payload=file.read(), filez=None)
def sendRequest(self, service, payload, filez):
url = CONSTS.API_URL_CS1 + CONSTS.API_URL_MANAGER
headers = CONSTS.REQUEST_DEFAULT_HEADERS
if service is not None:
headers[CONSTS.JSON_KEY_REQUEST_SERVICE] = service
else:
service = "file_uploading"
if self.DEBUG:
print self.LOGTAG, " :: Service -> ", service
if self.DEBUG and payload is not None:
print self.LOGTAG, " :: Uploading -> " , payload
try:
if payload is not None:
response = requests.post(url, headers=headers, data=json.dumps(payload))
elif filez is not None:
response = requests.post(url, headers=None, files=filez)
else:
response = requests.post(url, headers=headers)
if self.DEBUG and response.text != "" and response.text != None:
print self.LOGTAG, " :: Response Content -> ", response.text, "\n"
if self.DEBUG and response.status_code == requests.codes.ok:
print self.LOGTAG, " :: ", service, " -> Completed Successfully"
else:
print self.LOGTAG, " :: ERROR: ",service, " -> status_code:", sendResponse.status_code
self.parseRequestMetaData(response.content)
return response.content
except requests.ConnectionError:
if self.DEBUG:
print self.LOGTAG, ":: ConnectionError Thrown"
def parseRequestMetaData(self, data):
responseObj = json.loads(data)
try:
if responseObj[CONSTS.JSON_KEY_REQUESTING_VIDEO_STREAM] == "1":
if self.DEBUG:
print self.LOGTAG, " :: Starting Video Stream"
CameraManager.startStream()
elif responseObj[CONSTS.JSON_KEY_REQUESTING_VIDEO_STREAM] == "2":
if self.DEBUG:
print self.LOGTAG, " :: Capturing Image Stream"
CameraManager.takeStill()
except KeyError:
if self.DEBUG:
print self.LOGTAG, " :: MetaData KeyError"
#-------Polling Calls---------------------------
def schedule_SysConfigCheck(self):
timer = Timer(self.getSystemConfigRequestRate(), self.getSystemConfig,())
timer.start()
def schedule_UploadSensorValues(self):
timer = Timer(self.getSensorValueUploadRate(), self.uploadSensorValues,())
timer.start()
def schedule_UploadCameraStill(self):
timer = Timer(self.getCameraImageUploadRate(), self.uploadImage,())
timer.start()
def schedule_UploadCameraVideo(self):
timer = Timer(self.getCameraImageUploadRate(), self.uploadVideo,())
timer.start()
#------Getters and Setters-------------------------
def getSensorValueUploadRate(self):
return self.__sensorValueUploadRate
def setSensorValueUploadRate(self, newRate):
self.__sensorValueUploadRate = newRate
def getSystemConfigRequestRate(self):
return self.__systemConfigRequestRate
def setSystemConfigRequestRate(self, newRate):
self.__systemConfigRequestRate = newRate
def getCameraImageUploadRate(self):
return self.__cameraImageUploadRate
def setCameraImageUploadRate(self, newRate):
self.__cameraImageUploadRate = newRate
def getConfigManager(self):
return self.__configurationManager
def setConfigManager(self, configurationManager):
self.__configurationManager = configurationManager
def toString(self):
data = { CONSTS.JSON_KEY_API_SENSOR_VALUE_UPLOAD_RATE : self.getSensorValueUploadRate(),
CONSTS.JSON_KEY_API_SYSTEM_CONFIG_REQUEST_RATE : self.getSystemConfigRequestRate(),
CONSTS.JSON_KEY_API_CAMERA_IMAGE_UPLOAD_RATE : self.getCameraImageUploadRate()}
if self.DEBUG:
print self.LOGTAG , json.dumps(data)
return data
'''
sensorFactory = SensorFactory()
sensorManager = SensorManager(sensorFactory.getSensors(), None)
apiManager = APIManager(sensorManager=sensorManager)
apiManager.getSystemConfig()
apiManager.updateSystemConfig()
apiManager.uploadSensorValues()
apiManager.uploadImage()
'''