Beispiel #1
0
    def handle_service_commands(self, service_sock):
        while True:
            service_req = sock_util.recv_msg(service_sock)
            if service_req == None:
                break

            if not isinstance(service_req, str):
                service_req = service_req.decode()

            req = json.loads(service_req)
            #print ("SENSOR_MANAGER received service request:", service_req)
            opcode, service_id, sensor_name, input_rate = req["opcode"], req["service_id"], req["sensor_name"], req["input_rate"]
            if opcode == "SERVICE_REGISTER":
                if service_id not in self.service_input_timers:
                    if input_rate == "default_rate":
                        timer = RepeatedTimer(float(self.sensor_rates[sensor_name]), self.send_input_to_service, service_sock, service_id, sensor_name)
                    else:
                        timer = RepeatedTimer(float(input_rate), self.send_input_to_service, service_sock, service_id, sensor_name)
                    timer.start()
                    self.service_input_timers[service_id] = timer
            elif opcode == "SERVICE_UNREGISTER":
                if service_id in service_input_timers:
                    self.service_input_timers[service_id].stop()
                    self.service_input_timers.pop(service_id, None)
            else:
                print ("Shouldn't reach here!!")
                pass
Beispiel #2
0
    def input_stream(self, sensor_sock):
        while True:
            sensor_input = sock_util.recv_msg(sensor_sock)
            if sensor_input == None:
                break

            if not isinstance(sensor_input, str):
                sensor_input = sensor_input.decode()

            sensor_input = json.loads(sensor_input)
            sensor_name, sensor_type, sensor_rate, sensor_data = sensor_input["name"], sensor_input["sensor_type"], sensor_input["sensor_rate"], sensor_input["sensor_data"]
            if sensor_name not in self.supported_sensors:
                print (f"Registering Sensor Name: {sensor_name} --> data: {sensor_data}")
                self.sensor_rates[sensor_name] = sensor_rate
                self.supported_sensors[sensor_name] = sensor_sock
                self.sensor_buffer[sensor_name] = []
                self.sensor_buffer_lock[sensor_name] = threading.Lock()

                with self.sensor_buffer_lock[sensor_name]:
                    self.sensor_buffer[sensor_name].append(sensor_data)

                #if sensor_type == "two-way":
                if False:
                    sensor_IP = "127.0.0.1"
                    sensor_output_port = int(sensor_input["output_port"])
                    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                    sock.connect((sensor_IP, sensor_output_port))
                    self.output_sensor_sock[sensor_name] = sock


                # TODO: write this new sensor to the sensor config file
            else:
                with self.sensor_buffer_lock[sensor_name]:
                    self.sensor_buffer[sensor_name][0] = sensor_data
 def init_output_stream(self):
     print("calling")
     service_gateway_output_listen = socket.socket()
     service_gateway_output_listen.bind(('', 5002))
     service_gateway_output_listen.listen(15)
     print("called")
     while True:
         print("getting")
         service_socket, addr = service_gateway_output_listen.accept()
         data = sock_util.recv_msg(service_socket)
         self.handle_service_output(data)
 def recv_service_requests(self):
     service_to_gateway_listen = socket.socket()
     service_to_gateway_listen.bind(('', 5001))
     service_to_gateway_listen.listen(15)
     while True:
         service_socket, addr = service_to_gateway_listen.accept()
         data = sock_util.recv_msg(service_socket)
         req = json.loads(data)
         if str(req["service_id"]) not in self.service_sockets:
             self.service_sockets[str(req["service_id"])] = service_socket
         self.handle_service_requests(data)
Beispiel #5
0
    def handle_output_content(self, output_sock):
        while True:
            output_content = sock_util.recv_msg(output_sock)
            if output_content == None:
                print("[SensorManager] Output connection with platform lost!!")
                break

            if not isinstance(output_content, str):
                output_content = output_content.decode()

            output_dict = json.loads(output_content)

            print ("[SensorManager] receiving output --> ", output_dict)
            sock_util.send_msg(self.supported_sensors[output_dict["sensor_name"]], output_content)
    def handle_input_stream(self, gateway_sock):
        while True:
            input_data = sock_util.recv_msg(gateway_sock)
            if input_data is None:
                print("Connection with gateway lost!!")
                break

            if not isinstance(input_data, str):
                input_data = input_data.decode()

            input_data = json.loads(input_data)
            service_id, sensor_name, content = input_data[
                "service_id"], input_data["sensor_name"], input_data["content"]
            # queue_name = self.description + "_" + str(service_id)
            data = {}
            data["sensor_name"] = sensor_name
            data["content"] = content
            json_data = json.dumps(data)

            sock_util.send_msg(self.service_sockets[str(service_id)],
                               json_data)
 def service_recv_input_request(self, service_id, func):
     while True:
         # print (self.service_to_gatway_socket)
         data = sock_util.recv_msg(self.service_to_gatway_socket)
         func(None, None, None, data)