Skip to content

Commit

Permalink
Added more mods to the bridge to work serverless
Browse files Browse the repository at this point in the history
  • Loading branch information
Sbef98 authored and Paula-Kli committed Feb 9, 2022
1 parent a9c755f commit ec10d38
Showing 1 changed file with 140 additions and 5 deletions.
145 changes: 140 additions & 5 deletions bridge/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ def sendInitializeMessageToCloud(self):
return self.serverInitialized

def sendToCloud(self, path, json_data):

if not self.serverInitialized: # If not initialized yet i try anotehr initialization
if self.sendInitializeMessageToCloud() == false:
return None

response = requests.post(self.cloud + '/' + path, json=json_data)
return response

Expand All @@ -73,6 +68,136 @@ def loop(self):
finally:
loop.close()

<<<<<<< HEAD
=======
def useData(self, data):
# I have received a line from the serial port. I can use it

self.inbuffer = data

if len(self.inbuffer) < 4: # at least header, flags, sensorid, new sensor datatype, footer
print("Warning: Message is shorter than minimum size")
return False
# split parts
if self.inbuffer[0] != b'\xff': # first byte
print("Warning: Start of sent data is incorrect")
return False

print("Reading flags")

flags = int.from_bytes(self.inbuffer[1], byteorder='little')

if flags & (1 << 6) == 64: # check whether second bit of flags is set
self.debug = True
print("Debug message")

print("Flags: ", flags)

message = ""
for i in range(2, len(self.inbuffer)):
try:
message += self.inbuffer[i].decode("ascii")
except:
print("Print wrong character for: ", i, self.inbuffer[i])
print("Message as text: " + message)

if flags & (1 << 7) == 128: # check whether first bit of flags is set
if flags & (1 << 5) == 32:
self.state = "newActuator"
print("Initialize Actuator")
self.initializeDevice(sensor=False)
else:
self.state = "newSensor"
print("Initialize Sensor")
self.initializeDevice(sensor=True)

else:
self.state = "addValueForSensor"
print("Add Value for Sensor")
self.addValueForSensor()

def initializeDevice(self, sensor):
# read string from inbuffer until fe
# FF Flags sensorid=0 datasize datatype_as_string FE
datasize = int.from_bytes(self.inbuffer[3], byteorder='little')

datatype = ""
for i in range(datasize):
print(self.inbuffer[4 + i])
datatype += self.inbuffer[4 + i].decode("ascii")

data_json = {}
data_json['bridgeid'] = str(self.name)

data_json['sensor'] = "True" if sensor else "False"

data_json['datatype'] = datatype
print("json_data for initilization: ", data_json)

if not self.debug:

if not self.serverInitialized: # If first initialization did not workm, let's try again
if not self.sendInitializeMessageToCloud():
print("Skipping comunication with cloud due to no connection")
print("Assuming Debug Mode, Wanted to initialize sensor:", data_json)
return

response = self.sendToCloud('adddevice', data_json)
device_id = int(response.content) # TODO: answer in a nicer machine readable way

if device_id > 253:
print("Warning: to many devices initialized, device id to high for serial communication")
return

if sensor:
flags = 128
self.sensors.append(device_id)
else:
flags = 32 + 128
self.actuators.append(device_id)
print("Added actuator")

data = createDeviceInitializationMessage(flags, device_id)

self.serialHandler.write(data)
print("Sent device_id to arduino", device_id)

else:
print("Debug: Wanted to initialize sensor:", data_json)

def addValueForSensor(self):
sensorID = self.inbuffer.getDeviceId()
currentData = DataSet(sensorID)

datasize = self.inbuffer.getDataSize() - 1

for i in range(datasize):
try:
val = int.from_bytes(self.inbuffer[4 + i], byteorder='little')
currentData.addValue(val)
strval = "Sensor %d: %d " % (sensorID, val)
print(strval)
except:
print("Datasize not matching: ", datasize, len(self.inbuffer))

# send the read data as json to the cloud
data_json = currentData.getJSON(self.name)

if not self.debug:

if not self.serverInitialized: # If first initialization did not work, let's try again
if not self.sendInitializeMessageToCloud():
print("Skipping comunication with cloud due to no connection")
print("Assuming Debug Mode, Wanted to send the following data to the cloud: ", data_json)
return

response = self.sendToCloud('addvalue', data_json)
if not response.ok:
print("Something went wrong uploading the data. See statuscode " + response.reason)
else:
print("Debug: Wanted to send the following data to the cloud: ", data_json)

>>>>>>> 9058181 (Added more mods to the bridge to work serverless)
def queryForNewActuatorValues(self):
while True:
time.sleep(30)
Expand All @@ -81,6 +206,13 @@ def queryForNewActuatorValues(self):
data_json['bridgeid'] = str(self.name)
data_json['actuator_num'] = str(len(self.actuators))
data_json['actuators'] = [str(actuator) for actuator in self.actuators]

if not self.serverInitialized: # If first initialization did not workm, let's try again
if not self.sendInitializeMessageToCloud():
print("Can't query for new actuators value if no connection is enstablished first")
print("Assuming Debug Mode, Wanted to query actuators:", data_json)
continue # Continues the while

response = self.sendToCloud('getNewValues', data_json)

# expecting json like {'actuator_number' : 'actuator_value'}
Expand All @@ -106,4 +238,7 @@ def queryForNewActuatorValues(self):
=======
hand = SocketHandler(None, 8080, "localhost")
SocketHandler.loop()
<<<<<<< HEAD
>>>>>>> 5a8c296 (Added few line of code to run the bridge without having the cloud up and running)
=======
>>>>>>> 9058181 (Added more mods to the bridge to work serverless)

0 comments on commit ec10d38

Please sign in to comment.