def send_data(self, data, output_port = -1):
        if self.debug == "yes":
            print (f"Sending data: {data}")

        data_dict = {}
        data_dict["sensor_type"] = self.sensor_type
        data_dict["name"] = self.name
        data_dict["sensor_rate"] = self.rate
        data_dict["sensor_data"] = data
        data_dict["output_port"] = output_port
        json_data_str = json.dumps(data_dict)

        if self.sock:
            sock_util.send_msg(self.sock, json_data_str.encode())
            return

        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((self.dest_IP, self.dest_port))
        sock_util.send_msg(sock, json_data_str.encode())

        if self.rate < 5:
            self.sock = sock
        else:
            sock.close()
            self.sock = None
    def service_output_send(self, service_id, content):
        output = {}
        output["service_id"] = service_id
        output["content"] = content
        json_output_str = json.dumps(output)

        send_to_gateway_output_socket = socket.socket(socket.AF_INET,
                                                      socket.SOCK_STREAM)
        send_to_gateway_output_socket.connect(
            ("127.0.0.1", 5002))  # Need to pass actual IP
        sock_util.send_msg(send_to_gateway_output_socket, json_output_str)
Example #3
0
    def send_input_to_service(self, service_sock, service_id, sensor_name):
        with self.sensor_buffer_lock[sensor_name]:
            sensor_data = self.sensor_buffer[sensor_name][0]

        input_data_dict = {}
        input_data_dict["service_id"] = str(service_id)
        input_data_dict["sensor_name"] = str(sensor_name)
        input_data_dict["content"] = str(sensor_data)
        json_data_str = json.dumps(input_data_dict)

        print ("Sending input data to service -->", json_data_str)
        sock_util.send_msg(service_sock, json_data_str.encode())
    def handle_service_requests(self, body):
        req = json.loads(body)
        for gw in req["gateway_addrs"]:
            if gw not in self.gateway_sockets:
                gateway_sock = socket.socket(socket.AF_INET,
                                             socket.SOCK_STREAM)
                self.gateway_sockets[gw] = gateway_sock
                gw_IP, gw_port = gw.split(':')
                gateway_sock.connect((gw_IP, int(gw_port)))
                threading.Thread(target=self.handle_input_stream,
                                 args=(gateway_sock, )).start()

            sock_util.send_msg(self.gateway_sockets[gw], body)
Example #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 service_register_request(self, service_id, sensor_name, input_rate):
        # get a list of gateway IP and ports from registry/... using service_id
        req = {}
        req["opcode"] = "SERVICE_REGISTER"
        req["service_id"] = service_id
        req["sensor_name"] = sensor_name
        req["input_rate"] = input_rate
        req["gateway_addrs"] = ["127.0.0.1:4445"
                                ]  # temp list of gateway IP, port
        json_req = json.dumps(req)

        self.service_to_gatway_socket = socket.socket(socket.AF_INET,
                                                      socket.SOCK_STREAM)
        self.service_to_gatway_socket.connect(
            ("127.0.0.1", 5001))  # Need to pass actual IP
        sock_util.send_msg(self.service_to_gatway_socket, json_req)
    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 handle_service_output(self, body):
        output = json.loads(body)
        #print (f"{self.description} receiving output --> {output}")

        # TODO: read config of output["service_id"] to find out where the output should go

        # OUTPUT of FLOWER_ANALYSIS_SENSOR
        # Destination: UI
        print(output)
        if output["service_id"] == "flower_svc_1":
            print("sendind output to UI", output)
            json_output_str = json.dumps(output)
            print(f"[POS] receiving output --> {json_output_str}")
            try:
                send_to_UI_socket = socket.socket(socket.AF_INET,
                                                  socket.SOCK_STREAM)
                send_to_UI_socket.connect(
                    ("192.168.43.173", 5004))  # Need to pass actual IP
                print("send message")
                sock_util.send_msg(send_to_UI_socket, json_output_str)
            except:
                print("UI Closed")
Example #9
0
    def handle_service_output(self, ch, method, properties, body):
        output = json.loads(body)
        #print (f"{self.description} receiving output --> {output}")

        # TODO: read config of output["service_id"] to find out where the output should go

        service_id = output["service_id"]

        db = DATABASE_CLIENT()
        db_response = db.service_name_get(service_id)
        if not isinstance(db_response, str):
            db_response = db_response.decode()

        service_name = None
        if service_id != "flower_svc_1" and service_id != "sonar_svc_1" and service_id != "earthquake_svc_1":
            db_response = json.loads(db_response)
            print("[POS] Receiving ", db_response)
            db_response = db_response["response"]
            service_name = db_response["service_id"]

        # OUTPUT of DISTANCE_ALARM_SERVICE
        # Destination: DISTANCE_SENSOR
        #if service_id == "dist_svc_1":
        if service_name == "Distance_Alarm_Service":
            output["sensor_name"] = "DISTANCE_SENSOR"
            # it has to send the result back to the sensor
            # get list of gateways where this sensor is running
            gateway_addrs = [
                "127.0.0.1:4446"
            ]  # temp list of gateways, currently only 1 gateway
            for gw in gateway_addrs:
                if gw not in self.gateway_sockets:
                    gateway_sock = socket.socket(socket.AF_INET,
                                                 socket.SOCK_STREAM)
                    self.gateway_sockets[gw] = gateway_sock
                    gw_IP, gw_port = gw.split(':')
                    gateway_sock.connect((gw_IP, int(gw_port)))

                json_output_str = json.dumps(output)
                #print (f"{self.description} sending output --> {json_output_str}")
                sock_util.send_msg(self.gateway_sockets[gw], json_output_str)

        # OUTPUT of NAVAL_MINE_DETECTION_SERVICE
        # Destination: UI
        # Action: Trigger CounterService if Mine detected
        elif service_id == "sonar_svc_1":
            #elif service_name == "Sonar":
            json_output_str = json.dumps(output)
            #print (f"[POS] receiving output --> {json_output_str}")
            self.RBMQ.send("", self.description + "_" + service_id,
                           json_output_str)

            if output["content"] == "Mine":
                # Fetch service_id of COUNTER_SERVICE
                self.RBMQ.send("", self.description + "_counter_svc_1", "Mine")

        # OUTPUT of FLOWER_ANALYSIS_SERVICE
        # Destination: UI
        elif service_id == "flower_svc_1":
            #elif service_name == "Iris":
            json_output_str = json.dumps(output)
            #print (f"[POS] receiving output --> {json_output_str}")
            self.RBMQ.send("", self.description + "_" + service_id,
                           json_output_str)

        elif service_id == "earthquake_svc_1":
            #elif service_id == "Earthquake_Service":
            json_output_str = json.dumps(output)
            print(f"[POS] receiving output --> {json_output_str}")
            self.RBMQ.send("", self.description + "_" + service_id,
                           json_output_str)
        else:
            print("[POS] It shouldn't reach here, service_id: ", service_id)