Beispiel #1
0
    def __init__(self):
        self.socketIO = SocketIO(host, port)
        
        def on_connect():
            print("Server connected")

        def on_disconnect(*args):
            print("Server disconnected")

        def on_reconnect():
            print("Server reconnected")

        def on_time(*args):
            print(args[0])

        self.socketIO.on('connect', on_connect) # This is not working
        self.socketIO.on('disconnect', on_disconnect) # This works
        self.socketIO.on('reconnect', on_reconnect) # This is not working
        self.socketIO.on('time', on_time)

        self.receive_events_thread = Thread(target=self._receive_events_thread)
        self.receive_events_thread.daemon = True
        self.receive_events_thread.start()

        while True:
            some_input = input("Please input: ")
            self.socketIO.emit('custom', some_input)
Beispiel #2
0
def sendToNode(channel, data):
    """
  Send string to Node.js server
  No reply expected

  WARNING: while trying to connect, the main loop is blocked
  """
    if debug_sockets: print("[SCK] To Node (on {}): {}".format(channel, data))
    with SocketIO('localhost', 8081, wait_for_connection=False) as socket:
        socket.connect()
        if debug_sockets: print("[SCK] Connected to Node server on 8081")
        if (type(data) == dict):
            socket.emit(channel, json.dumps(dict2list(data),
                                            ensure_ascii=False))
        else:
            socket.emit(channel, json.dumps(data, ensure_ascii=False))
    if debug_sockets: print("[SCK] To Node: sent")
Beispiel #3
0
def emit(name, data=None):
    return
    if not WS_ENABLED:
        return
    try:
        if data is None:
            data = {}
        from socketIO_client_nexus import SocketIO

        with SocketIO(WS_BASE,
                      WS_PORT,
                      wait_for_connection=False,
                      transports=["websocket"]) as socketIO:
            socketIO.emit(name, data)
            socketIO.disconnect()

    except:
        pass
Beispiel #4
0
    def run(self):
        print("trying to connect to", self.url)
        #with SocketIO('http://localhost:8005') as socketIO:
        with SocketIO(self.url) as socketIO:
            print("got socketIO", socketIO)
            self.socket = socketIO
            socketIO.on('connect', lambda args, s=self: s.on_connect(args))
            #
            print("Send status.join message")
            socketIO.emit('MUSE',
                          {'type': 'status.join', 'clientType': 'Python'})

            socketIO.on('MUSE', lambda msg, s=self: s.onMessage(msg))

            #socketIO.wait_for_callbacks(seconds=1)
            while 1:
                socketIO.wait(seconds=10)
                print("wait some more...")
Beispiel #5
0
class SocketMessenger():

    def __init__(self, hostname, port):
        imageio.plugins.freeimage.download()
        self.hostname = hostname
        self.port = port
        self.socket = SocketIO(self.hostname, self.port)
        self.messenger_namespace = self.socket.define(MessengerNameSpace,
            Config.Socket.NAMESPACE)

    def send_alert(self, **kwargs):
        json_string = json.dumps(kwargs)
        self.messenger_namespace.emit(Config.Socket.ALERT_EVENT, json_string)

    def send_statistic(self, **kwargs):
        json_string = json.dumps(kwargs)
        print(json_string)
        self.messenger_namespace.emit(Config.Socket.STATISTIC, json_string)
Beispiel #6
0
    def _setup_socket(self):
        """Create socket handlers and registers the socket"""
        self.socketIO = SocketIO(self.server_url, self.port)

        def on_socket_open(*args):
            shared_utils.print_and_log(
                logging.DEBUG,
                'Socket open: {}'.format(args)
            )
            self._send_world_alive()
            self.alive = True

        def on_disconnect(*args):
            """Disconnect event is a no-op for us, as the server reconnects
            automatically on a retry"""
            shared_utils.print_and_log(
                logging.INFO,
                'World server disconnected: {}'.format(args)
            )
            self.alive = False

        def on_message(*args):
            """Incoming message handler for ACKs, ALIVEs, HEARTBEATs,
            and MESSAGEs"""
            message_data = args[0]
            shared_utils.print_and_log(
                logging.DEBUG,
                'Message data recieved: {}'.format(message_data)
            )
            for message_packet in message_data['entry']:
                self.message_callback(message_packet['messaging'][0])

        # Register Handlers
        self.socketIO.on('socket_open', on_socket_open)
        self.socketIO.on('disconnect', on_disconnect)
        self.socketIO.on('new_packet', on_message)

        # Start listening thread
        self.listen_thread = threading.Thread(
            target=self.socketIO.wait,
            name='Main-Socket-Thread'
        )
        self.listen_thread.daemon = True
        self.listen_thread.start()
Beispiel #7
0
    def connect(self):

        io = SocketIO(self.server, self.port)

        iron_namespace = io.define(IRONNamespace, '/iron')
        flash_namespace = io.define(FLASHNamespace, '/flash')
        mega_namespace = io.define(MEGANamespace, '/mega')


        io.emit('login', {'app_client_secret_id': os.environ.get('SOCKBOT_APPCLIENT_SECRET'),
                          'name': 'AUTOBOT'}, callback_login)

        #login update socketid
        io.wait_for_callbacks(seconds=1)






        #io.wait()


        return dict(io=io,iron_namespace=iron_namespace, flash_namespace=flash_namespace, mega_namespace=mega_namespace)
Beispiel #8
0
    def run(self):
        url = 'http://localhost:8000'
        print "Connecting to SocketIO", url
        with SocketIO(url) as socketIO:
            print "got socketIO", socketIO
            socketIO.on('connect', lambda args, s=self: s.on_connect(args))
            #
            print "Send status.join message"
            socketIO.emit('MUSE', {
                'type': 'status.join',
                'clientType': 'Python'
            })

            socketIO.on('MUSE', lambda msg, s=self: s.onMessage(msg))

            #socketIO.wait_for_callbacks(seconds=1)
            while 1:
                socketIO.wait(seconds=10)
                print "wait some more..."
Beispiel #9
0
def socketio(
    url,
    callback,
    channel="",
    field="",
    sendinit=None,
    json=False,
    wrap=False,
    interval=1,
):
    """Connect to socketIO server and pipe results through the callback

    Args:
        url (str): url to connect to
        callback (callable): function to call on websocket data
        channel (str): socketio channel to connect through
        field (str): field to index result by
        sendinit (list): data to send on socketio connection open
        json (bool): load websocket data as json
        wrap (bool): wrap result in a list
        interval (int): socketio wai interval
    """
    from socketIO_client_nexus import SocketIO as SIO

    o = urlparse(url)
    socketIO = SIO(o.scheme + "://" + o.netloc, o.port)
    if sendinit:
        socketIO.emit(sendinit)

    while True:
        _data = []
        socketIO.on(channel, lambda data: _data.append(data))
        socketIO.wait(seconds=interval)
        for msg in _data:
            if json:
                msg = json.loads(msg)

            if field:
                msg = msg[field]

            if wrap:
                msg = [msg]

            callback(msg)
Beispiel #10
0
def get_points_sample():
	start = time.time()

	simq = ""
	x = 0
	for question in sample_questions:
		x = x + 1
		options = sample_questions[question]

		with SocketIO('localhost', 3000, LoggingNamespace) as socketIO:
		    socketIO.emit('new question', {'q': question,
				'c1': options[0], 'c2': options[1], 'c3': options[2]}, on_bbb_response)

		    socketIO.wait_for_callbacks(seconds=1)


		start = time.time()
		printResult(question, options)
		end = time.time()
		print(end - start)
Beispiel #11
0
def parse_ripe_ris(connection, prefix, host):
    exchange = Exchange('bgp-update',
                        channel=connection,
                        type='direct',
                        durable=False)
    exchange.declare()

    def on_ris_msg(msg):
        try:
            producer = Producer(connection)
            normalize_ripe_ris(msg)
            if mformat_validator(msg):
                msgs = normalize_msg_path(msg)
                for msg in msgs:
                    key_generator(msg)
                    log.debug(msg)
                    producer.publish(msg,
                                     exchange=exchange,
                                     routing_key='update',
                                     serializer='json')
            else:
                log.warning('Invalid format message: {}'.format(msg))
        except Exception:
            log.exception('exception')

    with SocketIO('http://stream-dev.ris.ripe.net/stream2',
                  wait_for_connection=False) as socket_io:
        socket_io.on('ris_message', on_ris_msg)
        socket_io.emit(
            'ris_subscribe', {
                'host': host,
                'type': 'UPDATE',
                'prefix': prefix,
                'moreSpecific': True,
                'lessSpecific': False,
                'socketOptions': {
                    'includeBody': False,
                    'explodePrefixes': True,
                }
            })
        socket_io.wait()
Beispiel #12
0
        def parse_rrcs(self) -> NoReturn:
            """
            SocketIO connection to RIPE RIS to retrieve all active Route Collectors.
            """
            try:
                socket_io = SocketIO('http://stream-dev.ris.ripe.net/stream',
                                     wait_for_connection=False)

                def on_msg(msg):
                    self.available_ris = set(msg)
                    socket_io.disconnect()

                socket_io.on('ris_rrc_list', on_msg)
                socket_io.wait(seconds=3)
            except Exception:
                log.warning('RIPE RIS server is down. Try again later..')
Beispiel #13
0
def socketio(url, callback, channel='', field='', sendinit=None, json=False, wrap=False, interval=1):
    o = urlparse(url)
    socketIO = SIO(o.scheme + '://' + o.netloc, o.port)
    if sendinit:
        socketIO.emit(sendinit)

    while True:
        _data = []
        socketIO.on(channel, lambda data: _data.append(data))
        socketIO.wait(seconds=interval)
        for msg in _data:
            if json:
                msg = json.loads(msg)

            if field:
                msg = msg[field]

            if wrap:
                msg = [msg]

            callback(msg)
Beispiel #14
0
    def __init__(self):
        self.socketIO = SocketIO(host, port)
        
        def on_socket_open(*args):
            print("on_socket_open: ", args[0])
            self.socketIO.emit('agent_alive', {'task_group_id': 'test_group', 'agent_id': '[World]'})

        def on_disconnect(*args):
            print("Server disconnected", args[0])

        def on_new_messgae(*args):
            print(args[0])

        self.socketIO.on('socket_open', on_socket_open)
        self.socketIO.on('disconnect', on_disconnect) # This works
        self.socketIO.on('new_message', on_new_messgae)

        self.receive_events_thread = Thread(target=self._receive_events_thread)
        self.receive_events_thread.daemon = True
        self.receive_events_thread.start()

        while True:
            some_input = input("Hit ENTER to send message:")
            self.socketIO.emit('new_message', {'task_group_id': 'test_group', 'receiver_agent_id': 'Worker'})
Beispiel #15
0
def calibrate_PH():
    """
    This function creates a client endpoint in socket connection between nodeJS
    and python process, listens continuosly to socketIO event, calibrates PH-circuit
    with received information.
    If calibration succeed, send back a success message.
    Else, send back an error message.
    """
    devices    = AtlasI2C();
    socket_url = "localhost"

    socketIO = SocketIO(socket_url, 9000, verify=False)
    def start_calibrate(*arg):
        try:
            print(arg)
            a  = arg[0][u'data']
            print(a)
            commandList = { "low": "cal,low,4", "medium": "cal,mid,7","high":"cal,high,10"}
            command = commandList.get(a)
            logger.info(command)
            mutex.acquire()
            tempRecalibration = "T,"+str(measure_temperature(26))
            devices.query(tempRecalibration)
            sleep(0.3)
            devices.query(command)
            sleep(0.3)
            status = devices.query('cal,?')
            status = status[-1]
            socketIO.emit('success',status)
            f = open("/home/pi/Public/main/Server/status.txt","w+")
            f.write(status)
            f.close()
            logger.info("Calibration Command Successful,"+str(a))
            mutex.release()
        except :
            socketIO.emit("error")
            logger.debug("Wrong Command")
    socketIO.on('send_data',start_calibrate) #Listen to calibrating event
    socketIO.wait()
    while True: #keep the thread alive to continue listening to socket event
        pass
Beispiel #16
0
def main():
    attempts = 0

    while attempts < 3:
        try:
            socketIO = SocketIO(FMF_SERVER,
                                8080,
                                Namespace,
                                verify=False,
                                wait_for_connection=False)

            socketIO.emit('authenticate', {
                "app_id": APP_ID,
                "device_id": DEVICE_ID
            })
            socketIO.wait()

        except ConnectionError:
            attempts += 1
            time.sleep(30)
            print "Can't connect to server, trying again..."
Beispiel #17
0
class Client(object):

	def __init__(self):
		self.socketio = SocketIO("http://twitchplaysnintendoswitch.com:8110")

		# self.socketio.on("controllerState1", self.on_controller_state1)
		# self.socketio.on("controllerState2", self.on_controller_state2)
		# self.socketio.on("controllerState3", self.on_controller_state3)
		self.socketio.on("controllerState5", self.on_controller_state1)
		self.socketio.on("turnTimesLeft", self.on_turn_times_left)
		self.socketio.emit("join", "wiiu3dscontroller")

		self.receive_events_thread = Thread(target=self._receive_events_thread)
		self.receive_events_thread.daemon = True
		self.receive_events_thread.start()

		self.start = time.clock()
		self.end = time.clock()
		
		self.botstart = time.clock()
		self.botend = time.clock()

		self.controllerStart = time.clock()
		self.controllerEnd = time.clock()

		self.lockon = False

		self.yeaVotes = 0
		self.nayVotes = 0
		self.voting = False
		self.currentPlayers = []

		self.laglessEnabled = True
		self.currentGame = "none"

		self.oldArgs2 = "800000000000000 128 128 128 128"




	def _receive_events_thread(self):
		self.socketio.wait()		

	def on_event(self, event):
		#print(event)
		pass

	def on_controller_command(*args):
		nextCommands.append(args)

	def on_turn_times_left(*args):
		try:
			client.currentPlayers = args[1]["usernames"]
		except:
			pass

	def on_controller_state(*args):

		if(not client.laglessEnabled):
			return

		state = args[1]
		cNum = args[2]

		print("controller state" + str(cNum) + ":", state)

		client.oldArgs2 = state

		controller = None

		if(cNum == 0):
			controller = controller1
		elif(cNum == 1):
			controller = controller2
		elif(cNum == 2):
			return
			cNum = 1
			controller = controller2
		elif(cNum == 3):
			return
			cNum = 1
			controller = controller2
		elif(cNum == 4):
			return
			cNum = 1
			controller = controller2

		controller.reset()

		inputs = state.split()
		cPlayer = ""
		try:
			cPlayer = client.currentPlayers[cNum]
		except:
			pass

		btns = inputs[0]
		LX = inputs[1]
		LY = inputs[2]
		RX = inputs[3]
		RY = inputs[4]

		controller.dpad = int(btns[0])
		if (btns[1] == "1"):
			controller.lstick = 1;
		if (btns[2] == "1"):
			controller.l = 1;
		if (btns[3] == "1"):
			controller.zl = 1;
		if (btns[4] == "1"):
			controller.minus = 1;
		if (btns[5] == "1"):
			try:
				if (cPlayer.lower() in modlist):
					controller.capture = 1
				else:
					controller.capture = 0
			except:
				controller.capture = 0
		if (btns[6] == "1"):
			controller.a = 1;
		if (btns[7] == "1"):
			controller.b = 1;
		if (btns[8] == "1"):
			controller.x = 1;
		if (btns[9] == "1"):
			controller.y = 1;
		if (btns[10] == "1"):
			controller.rstick = 1;
		if (btns[11] == "1"):
			controller.r = 1;
		if (btns[12] == "1"):
			controller.zr = 1;
		if (btns[13] == "1"):
			try:
				if (cPlayer.lower() in pluslist):
					controller.plus = 1
				else:
					controller.plus = 0
			except:
				controller.plus = 0
		if (btns[14] == "1"):
			try:
				if (cPlayer.lower() in modlist):
					controller.home = 1
				else:
					controller.home = 0
			except:
				controller.home = 0

		try:
			controller.LX = int(LX)
			controller.LY = 255-int(LY)
			controller.RX = int(RX)
			controller.RY = 255-int(RY)
		except:
			pass

		duration = 0.001
		reset = 0
		if(cNum == 0):
			send_and_reset(duration, reset)
		elif(cNum == 1):
			send_and_reset2(duration, reset)
		elif(cNum == 2):
			send_and_reset3(duration, reset)
		elif(cNum == 3):
			send_and_reset4(duration, reset)


	# player 1:
	def on_controller_state1(*args):
		client.on_controller_state(args[1], 0)


	def handleChat(self, username, message):
		print(message)

		# handle chat messages here

	def decreaseQueue(self):

		# handle queue from handlechat
		pass


	def loop(self):

		# control switch here:

		# every 5 minutes:
		self.botend = time.clock()
		diffInMilliSeconds = (self.botend - self.botstart)*1000
		if(diffInMilliSeconds > 1000*60*5):
			self.socketio.emit("join", "wiiu3dscontroller")
			self.botstart = time.clock()
			# msg = "Join the discord server! https://discord.gg/ARTbddH\
			# hate the stream delay? go here! https://twitchplaysnintendoswitch.com"
			# twitchBot.chat(msg)

		# every 6 seconds, probably doesn't need to do this so often:
		self.controllerEnd = time.clock()
		diffInMilliSeconds2 = (self.controllerEnd - self.controllerStart)*1000
		if(diffInMilliSeconds2 > 6000):
			self.socketio.emit("join", "wiiu3dscontroller")
			self.controllerStart = time.clock()


		response = twitchBot.stayConnected()
		if(response != "none"):
			# prevent crash
			try:
				username = re.search(r"\w+", response).group(0) # return the entire match
				username = username.lower()
				message = CHAT_MSG.sub("", response)
				message = message.strip()
				message = message.lower()
				self.handleChat(username, message)
			except:
				pass

		self.decreaseQueue()

	def _receive_events_thread(self):
		self.socketio.wait()
Beispiel #18
0
    def _setup_socket(self):
        """Create socket handlers and registers the socket"""
        self.socketIO = SocketIO(self.server_url, self.port)

        def on_socket_open(*args):
            shared_utils.print_and_log(
                logging.DEBUG,
                'Socket open: {}'.format(args)
            )
            self._send_world_alive()
            self.alive = True

        def on_disconnect(*args):
            """Disconnect event is a no-op for us, as the server reconnects
            automatically on a retry"""
            shared_utils.print_and_log(
                logging.INFO,
                'World server disconnected: {}'.format(args)
            )
            self.alive = False

        def on_message(*args):
            """Incoming message handler for ACKs, ALIVEs, HEARTBEATs,
            and MESSAGEs"""
            packet = Packet.from_dict(args[0])
            packet_id = packet.id
            packet_type = packet.type
            connection_id = packet.get_sender_connection_id()
            if packet_type == Packet.TYPE_ACK:
                if packet_id not in self.packet_map:
                    # Don't do anything when acking a packet we don't have
                    return
                # Acknowledgements should mark a packet as acknowledged
                shared_utils.print_and_log(
                    logging.DEBUG,
                    'On new ack: {}'.format(args)
                )
                self.packet_map[packet_id].status = Packet.STATUS_ACK
                # If the packet sender wanted to do something on acknowledge
                if self.packet_map[packet_id].ack_func:
                    self.packet_map[packet_id].ack_func(packet)
                # clear the stored packet data for memory reasons
                self.packet_map[packet_id].data = None
            elif packet_type == Packet.TYPE_HEARTBEAT:
                # Heartbeats update the last heartbeat time and respond in kind
                self.last_heartbeat[connection_id] = time.time()
                self._send_response_heartbeat(packet)
            else:
                # Remaining packet types need to be acknowledged
                shared_utils.print_and_log(
                    logging.DEBUG,
                    'On new message: {}'.format(args)
                )
                self._send_ack(packet)
                # Call the appropriate callback
                if packet_type == Packet.TYPE_ALIVE:
                    self.last_heartbeat[connection_id] = time.time()
                    self.alive_callback(packet)
                elif packet_type == Packet.TYPE_MESSAGE:
                    self.message_callback(packet)

        # Register Handlers
        self.socketIO.on(data_model.SOCKET_OPEN_STRING, on_socket_open)
        self.socketIO.on(data_model.SOCKET_DISCONNECT_STRING, on_disconnect)
        self.socketIO.on(data_model.SOCKET_NEW_PACKET_STRING, on_message)

        # Start listening thread
        self.listen_thread = threading.Thread(
            target=self.socketIO.wait,
            name='Main-Socket-Thread'
        )
        self.listen_thread.daemon = True
        self.listen_thread.start()
Beispiel #19
0
class SocketManager():
    """SocketManager is a wrapper around socketIO to stabilize its packet
    passing. The manager handles resending packet, as well as maintaining
    alive status for all the connections it forms
    """

    # Time to acknowledge different message types
    ACK_TIME = {Packet.TYPE_ALIVE: 2,
                Packet.TYPE_MESSAGE: 2}

    # Default time before socket deemed dead
    DEF_SOCKET_TIMEOUT = 8

    def __init__(self, server_url, port, alive_callback, message_callback,
                 socket_dead_callback, task_group_id,
                 socket_dead_timeout=None):
        """
        server_url:           url at which the server is to be run
        port:                 port for the socket to operate on
        alive_callback:       function to be called on alive Packets, defined
                               alive_callback(self, pkt)
        message_callback:     function to be called on message Packets, defined
                               message_callback(self, pkt)
        socket_dead_callback: function to be called when a socket dies, should
                              return false if the socket_manager should ignore
                              the death and treat the socket as alive defined
                               on_socket_dead(self, worker_id, assignment_id)
        socket_dead_timeout:  time to wait between heartbeats before dying
        """
        self.server_url = server_url
        self.port = port
        self.alive_callback = alive_callback
        self.message_callback = message_callback
        self.socket_dead_callback = socket_dead_callback
        if socket_dead_timeout == None:
            self.socket_dead_timeout = self.DEF_SOCKET_TIMEOUT
        else:
            self.socket_dead_timeout = socket_dead_timeout
        self.task_group_id = task_group_id

        self.socketIO = None

        # initialize the state
        self.listen_thread = None
        self.queues = {}
        self.threads = {}
        self.run = {}
        self.last_heartbeat = {}
        self.packet_map = {}

        # setup the socket
        self._setup_socket()

    def get_my_sender_id(self):
        """Gives the name that this socket manager should use for its world"""
        return '[World_{}]'.format(self.task_group_id)

    def _send_world_alive(self):
        """Registers world with the passthrough server"""
        self.socketIO.emit(
            data_model.SOCKET_AGENT_ALIVE_STRING,
            {'id': 'WORLD_ALIVE', 'sender_id': self.get_my_sender_id()}
        )

    def _send_response_heartbeat(self, packet):
        """Sends a response heartbeat to an incoming heartbeat packet"""
        self.socketIO.emit(
            data_model.SOCKET_ROUTE_PACKET_STRING,
            packet.swap_sender().set_data('').as_dict()
        )

    def _send_ack(self, packet):
        """Sends an ack to a given packet"""
        ack = packet.get_ack().as_dict()
        self.socketIO.emit(data_model.SOCKET_ROUTE_PACKET_STRING, ack, None)

    def _send_packet(self, packet, connection_id, send_time):
        """Sends a packet, blocks if the packet is blocking"""
        # Send the packet
        pkt = packet.as_dict()
        shared_utils.print_and_log(
            logging.DEBUG,
            'Send packet: {}'.format(packet.data)
        )
        def set_status_to_sent(data):
            packet.status = Packet.STATUS_SENT
        self.socketIO.emit(
            data_model.SOCKET_ROUTE_PACKET_STRING,
            pkt,
            set_status_to_sent
        )

        # Handles acks and blocking
        if packet.requires_ack:
            if packet.blocking:
                # blocking till ack is received or timeout
                start_t = time.time()
                while True:
                    if packet.status == Packet.STATUS_ACK:
                        # Clear the data to save memory as we no longer need it
                        packet.data = None
                        break
                    if time.time() - start_t > self.ACK_TIME[packet.type]:
                        # didn't receive ACK, resend packet keep old queue time
                        # to ensure this packet is processed first
                        packet.status = Packet.STATUS_INIT
                        self._safe_put(connection_id, (send_time, packet))
                        break
                    time.sleep(shared_utils.THREAD_SHORT_SLEEP)
            else:
                # non-blocking ack: add ack-check to queue
                t = time.time() + self.ACK_TIME[packet.type]
                self._safe_put(connection_id, (t, packet))

    def _setup_socket(self):
        """Create socket handlers and registers the socket"""
        self.socketIO = SocketIO(self.server_url, self.port)

        def on_socket_open(*args):
            shared_utils.print_and_log(
                logging.DEBUG,
                'Socket open: {}'.format(args)
            )
            self._send_world_alive()
            self.alive = True

        def on_disconnect(*args):
            """Disconnect event is a no-op for us, as the server reconnects
            automatically on a retry"""
            shared_utils.print_and_log(
                logging.INFO,
                'World server disconnected: {}'.format(args)
            )
            self.alive = False

        def on_message(*args):
            """Incoming message handler for ACKs, ALIVEs, HEARTBEATs,
            and MESSAGEs"""
            packet = Packet.from_dict(args[0])
            packet_id = packet.id
            packet_type = packet.type
            connection_id = packet.get_sender_connection_id()
            if packet_type == Packet.TYPE_ACK:
                if packet_id not in self.packet_map:
                    # Don't do anything when acking a packet we don't have
                    return
                # Acknowledgements should mark a packet as acknowledged
                shared_utils.print_and_log(
                    logging.DEBUG,
                    'On new ack: {}'.format(args)
                )
                self.packet_map[packet_id].status = Packet.STATUS_ACK
                # If the packet sender wanted to do something on acknowledge
                if self.packet_map[packet_id].ack_func:
                    self.packet_map[packet_id].ack_func(packet)
                # clear the stored packet data for memory reasons
                self.packet_map[packet_id].data = None
            elif packet_type == Packet.TYPE_HEARTBEAT:
                # Heartbeats update the last heartbeat time and respond in kind
                self.last_heartbeat[connection_id] = time.time()
                self._send_response_heartbeat(packet)
            else:
                # Remaining packet types need to be acknowledged
                shared_utils.print_and_log(
                    logging.DEBUG,
                    'On new message: {}'.format(args)
                )
                self._send_ack(packet)
                # Call the appropriate callback
                if packet_type == Packet.TYPE_ALIVE:
                    self.last_heartbeat[connection_id] = time.time()
                    self.alive_callback(packet)
                elif packet_type == Packet.TYPE_MESSAGE:
                    self.message_callback(packet)

        # Register Handlers
        self.socketIO.on(data_model.SOCKET_OPEN_STRING, on_socket_open)
        self.socketIO.on(data_model.SOCKET_DISCONNECT_STRING, on_disconnect)
        self.socketIO.on(data_model.SOCKET_NEW_PACKET_STRING, on_message)

        # Start listening thread
        self.listen_thread = threading.Thread(
            target=self.socketIO.wait,
            name='Main-Socket-Thread'
        )
        self.listen_thread.daemon = True
        self.listen_thread.start()

    def open_channel(self, worker_id, assignment_id):
        """Opens a channel for a worker on a given assignment, doesn't re-open
        if the channel is already open. Handles creation of the thread that
        monitors that channel"""
        connection_id = '{}_{}'.format(worker_id, assignment_id)
        if connection_id in self.queues and self.run[connection_id]:
            shared_utils.print_and_log(
                logging.DEBUG,
                'Channel ({}) already open'.format(connection_id)
            )
            return
        self.run[connection_id] = True
        self.queues[connection_id] = PriorityQueue()

        def channel_thread():
            """Handler thread for monitoring a single channel"""
            # while the thread is still alive
            while self.run[connection_id]:
                try:
                    # Check if client is still alive
                    if (time.time() - self.last_heartbeat[connection_id]
                            > self.socket_dead_timeout):
                        self.run[connection_id] = False
                        self.socket_dead_callback(worker_id, assignment_id)

                    # Make sure the queue still exists
                    if not connection_id in self.queues:
                        self.run[connection_id] = False
                        break

                    # Get first item in the queue, check if we can send it yet
                    item = self.queues[connection_id].get(block=False)
                    t = item[0]
                    if time.time() < t:
                        # Put the item back into the queue,
                        # it's not time to pop yet
                        self._safe_put(connection_id, item)
                    else:
                        # Try to send the packet
                        packet = item[1]
                        if not packet:
                            # This packet was deleted out from under us
                            continue
                        if packet.status is not Packet.STATUS_ACK:
                            # either need to send initial packet
                            # or resend not-acked packet
                            self._send_packet(packet, connection_id, t)
                except Empty:
                    pass
                finally:
                    time.sleep(shared_utils.THREAD_MEDIUM_SLEEP)

        # Setup and run the channel sending thread
        self.threads[connection_id] = threading.Thread(
            target=channel_thread,
            name='Socket-Queue-{}'.format(connection_id)
        )
        self.threads[connection_id].daemon = True
        self.threads[connection_id].start()

    def close_channel(self, connection_id):
        """Closes a channel by connection_id"""
        shared_utils.print_and_log(
            logging.DEBUG,
            'Closing channel {}'.format(connection_id)
        )
        self.run[connection_id] = False
        if connection_id in self.queues:
            # Clean up packets
            packet_ids = list(self.packet_map.keys())
            for packet_id in packet_ids:
                if connection_id == \
                       self.packet_map[packet_id].get_receiver_connection_id():
                    del self.packet_map[packet_id]
            # Clean up other resources
            del self.queues[connection_id]
            del self.threads[connection_id]

    def delay_heartbeat_until(self, connection_id, delayed_time):
        """Delay a heartbeat prolong a disconnect until delayed_time"""
        self.last_heartbeat[connection_id] = delayed_time

    def close_all_channels(self):
        """Closes a channel by clearing the list of channels"""
        shared_utils.print_and_log(logging.DEBUG, 'Closing all channels')
        connection_ids = list(self.queues.keys())
        for connection_id in connection_ids:
            self.close_channel(connection_id)

    def socket_is_open(self, connection_id):
        return connection_id in self.queues

    def queue_packet(self, packet):
        """Queues sending a packet to its intended owner"""
        connection_id = packet.get_receiver_connection_id()
        if not self.socket_is_open(connection_id):
            # Warn if there is no socket to send through for the expected recip
            shared_utils.print_and_log(
                logging.WARN,
                'Can not send packet to worker_id {}: packet queue not found. '
                'Message: {}'.format(connection_id, packet.data)
            )
            return
        shared_utils.print_and_log(
            logging.DEBUG,
            'Put packet ({}) in queue ({})'.format(packet.id, connection_id)
        )
        # Get the current time to put packet into the priority queue
        self.packet_map[packet.id] = packet
        item = (time.time(), packet)
        self._safe_put(connection_id, item)

    def get_status(self, packet_id):
        """Returns the status of a particular packet by id"""
        return self.packet_map[packet_id].status

    def _safe_put(self, connection_id, item):
        """Ensures that a queue exists before putting an item into it, logs
        if there's a failure
        """
        if connection_id in self.queues:
            self.queues[connection_id].put(item)
        else:
            shared_utils.print_and_log(
                logging.WARN,
                'Queue {} did not exist to put a message in'.format(
                    connection_id
                )
            )
Beispiel #20
0
class MessageSocket():
    """MessageSocket is a wrapper around socketIO to simplify message sends
    and recieves into parlai from FB messenger.
    """

    def __init__(self, server_url, port, secret_token, message_callback):
        """
        server_url:           url at which the server is to be run
        port:                 port for the socket to operate on
        message_callback:     function to be called on incoming message objects
                              format: message_callback(self, data)
        """
        self.server_url = server_url
        self.port = port
        self.message_callback = message_callback

        self.socketIO = None
        self.auth_args = {'access_token': secret_token}

        # initialize the state
        self.listen_thread = None

        # setup the socket
        self._setup_socket()

    def _send_world_alive(self):
        """Registers world with the passthrough server"""
        self.socketIO.emit(
            'world_alive', {'id': 'WORLD_ALIVE', 'sender_id': 'world'}
        )

    def send_fb_payload(self, receiver_id, payload):
        """Sends a payload to messenger, processes it if we can"""
        api_address = 'https://graph.facebook.com/v2.6/me/messages'
        if payload['type'] == 'list':
            data = create_compact_list_message(payload['data'])
        else:
            data = payload['data']
        message = {
            "messaging_type": 'RESPONSE',
            "recipient": {
                "id": receiver_id
            },
            "message": {
                "attachment": data,
            }
        }
        response = requests.post(
            api_address,
            params=self.auth_args,
            json=message,
        )
        result = response.json()
        shared_utils.print_and_log(
            logging.INFO,
            '"Facebook response from message send: {}"'.format(result)
        )
        return result

    def send_fb_message(self, receiver_id, message, is_response,
                        quick_replies=None):
        """Sends a message directly to messenger"""
        api_address = 'https://graph.facebook.com/v2.6/me/messages'
        if quick_replies is not None:
            quick_replies = [create_reply_option(x, x) for x in quick_replies]
        ms = create_text_message(message, quick_replies)
        results = []
        for m in ms:
            if m['text'] == '':
                continue  # Skip blank messages
            payload = {
                "messaging_type": 'RESPONSE' if is_response else 'UPDATE',
                "recipient": {
                    "id": receiver_id
                },
                "message": m
            }
            response = requests.post(
                api_address,
                params=self.auth_args,
                json=payload
            )
            result = response.json()
            shared_utils.print_and_log(
                logging.INFO,
                '"Facebook response from message send: {}"'.format(result)
            )
            results.append(result)
        return results

    def _setup_socket(self):
        """Create socket handlers and registers the socket"""
        self.socketIO = SocketIO(self.server_url, self.port)

        def on_socket_open(*args):
            shared_utils.print_and_log(
                logging.DEBUG,
                'Socket open: {}'.format(args)
            )
            self._send_world_alive()
            self.alive = True

        def on_disconnect(*args):
            """Disconnect event is a no-op for us, as the server reconnects
            automatically on a retry"""
            shared_utils.print_and_log(
                logging.INFO,
                'World server disconnected: {}'.format(args)
            )
            self.alive = False

        def on_message(*args):
            """Incoming message handler for ACKs, ALIVEs, HEARTBEATs,
            and MESSAGEs"""
            message_data = args[0]
            shared_utils.print_and_log(
                logging.DEBUG,
                'Message data recieved: {}'.format(message_data)
            )
            for message_packet in message_data['entry']:
                self.message_callback(message_packet['messaging'][0])

        # Register Handlers
        self.socketIO.on('socket_open', on_socket_open)
        self.socketIO.on('disconnect', on_disconnect)
        self.socketIO.on('new_packet', on_message)

        # Start listening thread
        self.listen_thread = threading.Thread(
            target=self.socketIO.wait,
            name='Main-Socket-Thread'
        )
        self.listen_thread.daemon = True
        self.listen_thread.start()
Beispiel #21
0
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
pwm = GPIO.PWM(12, 100)
pwm.start(float(85) / 10.0 + 2.5)

from socketIO_client_nexus import SocketIO, LoggingNamespace

Sock = SocketIO('38.88.74.79', 80)


def unlock():
    duty = float(185) / 10.0 + 2.5
    pwm.ChangeDutyCycle(duty)
    #start = time.time()
    print("door unlocked")


def lock():
    duty = float(85) / 10.0 + 2.5
    pwm.ChangeDutyCycle(duty)
    print("door locked")


def status(*args):
    if args[0] == 1: lock()
    elif args[0] == 0: unlock()
    else: print("error")
Beispiel #22
0
from config import token,ip,pathSource,selemiunIP
from Utils.logs import write_log
import os, sys, time, json
import shutil
import traceback
from webwhatsapi import WhatsAPIDriver
from webwhatsapi.objects.message import Message, MediaMessage
import shutil
from uuid import uuid4
from threading import Thread

##### Setting for start ######
profiledir=os.path.join(".","firefox_cache_v2")
if not os.path.exists(profiledir): os.makedirs(profiledir)

socketIO = SocketIO(ip,3000, LoggingNamespace)
wsp = None
driver = None
awaitLogin = None

#####################################
#          Functions Auth           #
#####################################
def on_connect(*args):
    write_log('Socket-Info','Connection whit server')
    socketIO.emit('Auth',token)

def on_welcome(*args):
    try:
        global driver
        write_log('Socket-Info','Connection success')
Beispiel #23
0
# sudo python phat.py
# sudo apt-get install python-fourletterphat
# curl https://get.pimoroni.com/fourletterphat  | bash ... then reboot
#

import os
import time
import logging
import fourletterphat as flp

from socketIO_client_nexus import SocketIO, LoggingNamespace

logging.getLogger('socketIO-client').setLevel(logging.DEBUG)
logging.basicConfig(level=logging.DEBUG)

socketIO = SocketIO('https://coffee-pot-pi.herokuapp.com')


def on_connect():
    print 'Connected to server'
    socketIO.emit('piConnected')


def on_reconnect():
    print('Reconnected to server')
    socketIO.emit('piConnected')


def on_disconnect():
    print('disconnected')
    socketIO.emit('piDisconnected')
    print('reconnect')


def on_Response_B(*args):
    print('I got a reponse from server for event_B', args)


def on_Response_C(*args):
    print('I got a reponse from server for event_C', args)


def FuncA():
    print('FuncA: Now I got a message from server.')


socketIO = SocketIO('localhost', 3000, LoggingNamespace)
socketIO.on('connect', on_connect)
socketIO.on('disconnect', on_disconnect)
socketIO.on('reconnect', on_reconnect)
socketIO.on('event_A', FuncA)
# Listen
socketIO.on('Response_B', on_Response_B)
socketIO.emit('event_B')
socketIO.emit('event_B')
socketIO.wait(seconds=1)

# Stop listening
socketIO.off('Response_B')
socketIO.emit('event_B')
socketIO.wait(seconds=1)
Beispiel #25
0
tr = (int(0), int(width))
bl = (int(height), int(0))
br = (int(height), int(width))

#------------------------------------------------------------------------------
# Main
#------------------------------------------------------------------------------
if __name__ == '__main__':

  if len(sys.argv) > 1 and sys.argv[1] == 'DEBUG':
    print 'DEBUG MODE'
    debug_mode = True
  # with SocketIO('localhost', 3001, LoggingNamespace) as socketIO:
  #   socketIO.emit('aaa')
  #   socketIO.wait(seconds=1)
  with SocketIO('127.0.0.1', 3001, LoggingNamespace) as socketIO:
    socketInit()
    try:
      while True:
        time.sleep(0.03)
        detect()
        if debug_mode:
          cv2.imshow("obs", cv2.resize(img, (width // 2, height // 2)))
          #cv2.imshow("banner", banner)
          cv2.waitKey(1)
        # get coords
        array = [[int(tl[0]), int(tl[1])],[int(tr[0]), int(tr[1])],[int(br[0]), int(br[1])],[int(bl[0]), int(bl[1])]]
        quad = np.float32(array)
        source = np.float32([[0,0],[banner_width,0],[banner_width,banner_height],[0,banner_height]])

        M = cv2.getPerspectiveTransform(source, quad)
Beispiel #26
0
CLOSE_MICROPHONE = embedded_assistant_pb2.DialogStateOut.CLOSE_MICROPHONE
PLAYING = embedded_assistant_pb2.ScreenOutConfig.PLAYING
DEFAULT_GRPC_DEADLINE = 60 * 3 + 5
GPIO.setmode(GPIO.BCM)
query = "hi"
#set GPIO Pins
GPIO_TRIGGER = 23
GPIO_ECHO = 24
GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
GPIO.setup(GPIO_ECHO, GPIO.IN)
resp_text = ""
mute = True
startMouth = True
#~ num=""
beep = False
socket = SocketIO('127.0.0.1', 8000, LoggingNamespace)
faceFound = False
name2 = []
onceface = False
keyboard_on = False


class SampleAssistant(object):
    """Sample Assistant that supports conversations and device actions.

	Args:
	  device_model_id: identifier of the device model.
	  device_id: identifier of the registered device instance.
	  conversation_stream(ConversationStream): audio stream
		for recording query and playing back assistant answer.
	  channel: authorized gRPC channel for connection to the
Beispiel #27
0
 def __init__(self, host, port):
     self.host = host
     self.port = port
     self.socket = SocketIO(host, port, wait_for_connection=False)
Beispiel #28
0
SOCKET_ID = None

GPIO.setup(PIN_TRIGGER, GPIO.OUT)
GPIO.setup(PIN_ECHO, GPIO.IN)

GPIO.output(PIN_TRIGGER, False)
print('Waiting for Sensor to settle')
time.sleep(2)



SERVER_IP = 'http://192.168.0.29'
# SERVER_IP = 'http://192.168.1.128'
SERVER_PORT = 3001

socket = SocketIO(SERVER_IP, SERVER_PORT)


START_MSG = "START"
STOP_MSG = "STOP"

q = Queue()

def continuous_loop():
    while True:
        # get start message (blocking)
        while q.get() != START_MSG:
            pass
        while get_poll(q) != STOP_MSG:
            distance = get_measurement()
            json_blob = {
    def __init__(self,
                 videoPath,
                 onboardingMode,
                 imageProcessingEndpoint="",
                 imageProcessingParams="",
                 showVideo=False,
                 verbose=False,
                 loopVideo=False,
                 convertToGray=False,
                 resizeWidth=0,
                 resizeHeight=0,
                 annotate=False,
                 cognitiveServiceKey="",
                 modelId=""):
        self.videoPath = videoPath
        self.onboardingMode = onboardingMode
        # Avihay's bug fix:
        # TODO: add argument to choose which kind of processing - file or stream
        # Explanation: in the original demo, only a number was passed as path, because in RasPi webcam it is the case.
        # the different is when it's a webcam (True) - we take the latest frame at each point. In case it's a video (False) - we run on ALL frames
        if not self.__IsInt(videoPath):
            # case of a stream
            self.isWebcam = True
        else:
            # case of a video file
            self.isWebcam = False

        # TODO: remove all commands related to imageProcessingEndpoint. It's irelevant
        self.imageProcessingEndpoint = imageProcessingEndpoint
        if imageProcessingParams == "":
            self.imageProcessingParams = ""
        else:
            self.imageProcessingParams = json.loads(imageProcessingParams)
        self.showVideo = showVideo
        self.verbose = verbose
        self.loopVideo = loopVideo
        self.convertToGray = convertToGray
        self.resizeWidth = resizeWidth
        self.resizeHeight = resizeHeight
        self.annotate = (self.imageProcessingEndpoint !=
                         "") and self.showVideo & annotate
        self.nbOfPreprocessingSteps = 0
        self.autoRotate = False
        self.vs = None
        # TODO: wrap in try and add default value
        self.monitor_id = os.getenv("DEVICE_ID")
        self.results_list = []

        if not self.onboardingMode:  # live-stream mode, will use known boundries
            self.__get_boundries()
            # connect to server
            SOCKET_URL = os.getenv("SOCKET_URL")
            try:
                socketIO = SocketIO(SOCKET_URL, 443, BaseNamespace, False)
                time.sleep(3)
                self.ocrSocket = socketIO.define(SocketNamespace, '/ocr')
            except:
                print("Failed to open socket!")
                raise SocketInitVAOCVError(
                    "Can't establish a connection to the socket")
        else:
            self.__get_device_type_for_onboarding()

        if self.convertToGray:
            self.nbOfPreprocessingSteps += 1
        if self.resizeWidth != 0 or self.resizeHeight != 0:
            self.nbOfPreprocessingSteps += 1

        self.cognitiveServiceKey = cognitiveServiceKey
        self.modelId = modelId

        if self.verbose:
            print("Container vesrion: --> v1.3 - Intel OCR TEST")
            print(
                "Initialising the camera capture with the following parameters: "
            )
            print("   - Video path: " + str(self.videoPath))
            print("   - OnBoarding mode: " + str(self.onboardingMode))
            print("   - Device ID: " + str(self.monitor_id))
            print("   - Device type: " + str(self.device_type))
            print("   - Computer vision model: " +
                  str(os.getenv('CV_MODEL', "")))
            # print("   - Image processing endpoint: " + self.imageProcessingEndpoint)
            # print("   - Image processing params: " + json.dumps(self.imageProcessingParams))
            # print("   - Show video: " + str(self.showVideo))
            # print("   - Loop video: " + str(self.loopVideo))
            # print("   - Convert to gray: " + str(self.convertToGray))
            # print("   - Resize width: " + str(self.resizeWidth))
            # print("   - Resize height: " + str(self.resizeHeight))
            # print("   - Annotate: " + str(self.annotate))
            print()

        self.displayFrame = None
        # if self.showVideo:
        #     self.imageServer = ImageServer(5012, self)
        #     self.imageServer.start()
        #     # self.imageServer.run()

        COMPUTER_VISION_ENDPOINT = os.environ["COMPUTER_VISION_ENDPOINT"]
        COMPUTER_VISION_SUBSCRIPTION_KEY = os.environ[
            "COMPUTER_VISION_SUBSCRIPTION_KEY"]
        self.computervision_client = ComputerVisionClient(
            COMPUTER_VISION_ENDPOINT,
            CognitiveServicesCredentials(COMPUTER_VISION_SUBSCRIPTION_KEY))
Beispiel #30
0
def main(_):
    counter = 0
    # Definition of the paths
    weights_path = 'yolov2-tiny-voc.weights'
    input_img_path = 'test_zzh_1.jpg'
    output_image_path = 'output/zzh_out.jpg'

    # If you do not have the checkpoint yet keep it like this! When you will run test.py for the first time it will be created automatically
    ckpt_folder_path = './ckpt/'

    # Definition of the parameters
    input_height = 416
    input_width = 416
    score_threshold = 0.1
    iou_threshold = 0.1

    # Definition of the session
    sess = tf.InteractiveSession()

    tf.global_variables_initializer().run()

    # Check for an existing checkpoint and load the weights (if it exists) or do it from binary file
    print('Looking for a checkpoint...')
    saver = tf.train.Saver()
    _ = weights_loader.load(sess, weights_path, ckpt_folder_path, saver)

    # ---------------------q-learning---------------------

    N_STATES = [1, 2, 3, 4, 5, 6, 7, 8, 9]  # 9种states
    ACTIONS = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]  # 可能的actions
    EPSILON = 0.9  # greedy policy 的概率
    ALPHA = 0.1  # for learning rate 的大小
    GAMMA = 0.9  # for discount factor 的大小
    episode = 0

    q_table = q_learning.build_q_table(N_STATES, ACTIONS)  # Initialize Q(s, a)
    print(q_table)
    step_counter = 0  # init step
    S = 1  # init state
    q_learning.update_env(episode, step_counter)  # update env

    # ---------------------q-learning---------------------

    # ------------------------预定义和加载数据-----------------------
    # -----------------连接目标服务器-------------------------------
    socketIO = SocketIO('10.8.204.12', 3333, LoggingNamespace)
    socketIO.on('connection', on_connect)
    socketIO.on('disconnect', on_disconnect)
    socketIO.on('reconnect', on_reconnect)
    # -------------------------------------------------------------
    time_1 = 0
    time_2 = 0
    Fps_past = 0
    Fps = 0

    host = '127.0.0.1'
    port = 12345
    buffsize = 65535

    ADDR = (host, port)

    soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    soc.bind(ADDR)
    soc.listen(2)

    print('Wait for connection ...')
    soc_client, addr = soc.accept()
    print("Connection from :", addr)

    # 进行长连接不断接受信号, 每次data=的时候都会刷新所有接收到的值
    while True:
        data = ""
        data = soc_client.recv(buffsize).decode()
        while True:
            # data为初始化值是系统等待, 以防while占用太大的运算量
            if data == "":
                time.sleep(0.1)
            else:
                break
        # 服务器端 send 各个命令执行的操作
        if data == "time_start":
            # 用来记录总时间 (未用到)
            start_time = time.time()
            print(start_time)
            soc_client.send("done".encode())

        elif data == "time_stop":
            # 结束记录总时间 (未用到)
            stop_time = time.time()
            total_time = stop_time - start_time
            print(total_time)
            soc_client.send("done".encode())

        elif data == "finish":
            # 用来结束连接(未用到)
            soc_client.close()

        elif data == "time_1":
            # 用来接受time_1信息(初始化时间长度)
            soc_client.send("recive".encode())
            time_1 = soc_client.recv(buffsize).decode()
            time_1 = float(time_1)
            soc_client.send("done".encode())

        elif data == "time_2":
            # 用来接受time_2信息 (处理的时间长度)
            soc_client.send("recive".encode())
            time_2 = soc_client.recv(buffsize).decode()
            time_2 = float(time_2)
            soc_client.send("done".encode())

        elif data == "send_original_image":
            # 用来接受输出的原始图像 (记录下下载图像的时间为 recive_time)
            soc_client.send("recive".encode())
            recive_time = time.time()
            data_batches = ""
            new_data = ""

            # 开始下载
            print("downloading...")
            while True:
                new_data = soc_client.recv(buffsize).decode()

                # 结束时的处理
                if ((new_data[-1] == '%')):
                    data_batches = data_batches + new_data[:-1]
                    break
                # 循环体
                data_batches = data_batches + new_data

            print("processing...")
            # 加载下载的原图像解析为np array(注意imshow输出时要转化成np.uint8格式否则会黑屏)(<-坑)
            data_text = json.loads(data_batches)
            input_image = np.array(data_text)

            # 下载时间记录 (下载 + 解析)
            recive_time = time.time() - recive_time
            print("total time: {}".format(recive_time))
            soc_client.send("done".encode())

        elif data == "send_weight_cutpoint":
            # ---------------------q-learning---------------------
            Fps_past = Fps
            A = q_learning.choose_action(S, q_table, EPSILON)

            # ---------------------q-learning---------------------

            soc_client.send("{}".format(A).encode())
            # 记录时间 p4
            point_4 = time.time()
            data_batches = ""
            new_data = ""
            cut_point = ""

            # 开始下载
            print("downloading...")
            while True:
                new_data = soc_client.recv(buffsize).decode()
                # 结束时的处理 (cut point 作为倒数第二个值被接受在新变量中)
                if ((new_data[-1] == '%')):
                    data_batches = data_batches + new_data[:-2]
                    cut_point = new_data[-2:-1]
                    # print(cut_point)
                    break
                # 循环体
                data_batches = data_batches + new_data

            # 记录时间 p5 (time_3 为下载weight的时间)
            point_5 = time.time()
            time_3 = point_5 - point_4
            print("downloading time: {}".format(time_3))

            print("processing...")
            # 加载和解析数据
            data_text = json.loads(data_batches)
            predictions = np.array(data_text)
            cut_point = int(cut_point)
            predictions = inference(sess, predictions, cut_point)

            # 记录时间 p6 (time_4 为解析数据的时间)
            point_6 = time.time()
            time_4 = point_6 - point_5
            print("processing time: {}".format(time_4))

            print('Postprocessinasg...')
            # out_put images
            output_image = postprocessing(predictions, input_image,
                                          score_threshold, iou_threshold,
                                          input_height, input_width)
            # 记录时间 p7
            # time_5 为后处理的时间
            # 计算出后端的运行时间,中间的下载时间, 前端的预处理时间(不包括初始化时间),算出fps
            point_7 = time.time()
            time_5 = point_7 - point_6
            time_backend = time_4 + time_5
            time_downloading = time_3 + recive_time
            time_frontend = time_2
            time_total = time_backend + time_frontend + time_downloading
            Fps = 1. / time_total
            fps = "fps = {}".format(Fps)

            # 将图像格式转换成uint8 否则黑屏
            # 输出fps, 输出处理后的图像
            output_image = np.uint8(output_image)
            cv2.putText(output_image, str(fps), (5, 15),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0, 0, 255), 1)
            #cv2.imshow("hello", output_image)
            #if cv2.waitKey(1) & 0xFF == ord('q'):
            #break

            # 打印出所有可能用到的时间
            counter += 1
            print("time_load_model = {}".format(time_1))
            print("time_preprocess = {}".format(time_2))
            print("time_send_original_image = {}".format(recive_time))
            print("time_downloading = {}".format(time_downloading))
            print("time_load_jsons = {}".format(time_4))
            print("time_postprocess = {}".format(time_5))
            print("time_backend = {}".format(time_backend))
            print("time_frontend = {}".format(time_frontend))
            print("Fps = {}".format(Fps))

            # ---------------------q-learning---------------------

            S_, R = q_learning.get_env_feedback(A, Fps_past, Fps)
            print(str(S_) + "  " + str(S))
            q_predict = q_table.loc[S, A]
            with pd.option_context('display.max_rows', None,
                                   'display.max_columns', None):
                print(q_table)
            q_target = R + GAMMA * q_table.loc[S_, :].max(skipna=True)
            q_table.loc[S, A] += ALPHA * (q_target - q_predict)  # q_table 更新
            S = S_  # 探索者移动到下一个 state
            q_learning.update_env(episode, step_counter + 1)  # 环境更新

            step_counter += 1

            # ---------------------q-learning---------------------
            #--------------------多线程发送------------
            # Create a thread
            thread_socketIO = myThread(1, socketIO, "Frame-" + str(counter),
                                       output_image, Fps, cut_point,
                                       q_table.values)

            # Start a thread
            thread_socketIO.start()
            thread_socketIO.join()
            # --------------------多线程发送------------
            # 返回确认done
            soc_client.send("done".encode())
            # 下一个循环待命

            # ------------send---------
            pass
        else:
            pass
Beispiel #31
0
import random
from kafka import KafkaProducer
import time
#import RNN
#import svm

_SIO_URL_PREFIX = 'https://ws-api.iextrading.com'
_SIO_PORT = 443
SYMBOLS = [
    "AAPL", "MSFT", "AABA", "ACN", "ADP", "FB", "AMZN", "GOOGL", "IBM", "LMT"
]

producer = KafkaProducer(
    bootstrap_servers='127.0.0.1:9092',
    value_serializer=lambda v: ujson.dumps(v).encode('utf-8'))
socketIO = SocketIO(_SIO_URL_PREFIX, _SIO_PORT)
#socketIO.define(LoggingNamespace, path='/1.0/tops')


def predict(data):
    ''' 
    dummy pyspark function. Use as wrapper and add all predictions here 
    
    data: dict object of the form:
    {
        symbol: ,
        marketPercent:
        bidSize:
        bidPrice:
        askSize:
        askPrice:
Beispiel #32
0
from socketIO_client_nexus import SocketIO, LoggingNamespace
from config import infoClient
import os, sys, time, json
from webwhatsapi import WhatsAPIDriver
from webwhatsapi.objects.message import Message, MediaMessage
import shutil

socketIO = SocketIO('172.18.0.4', 4000, LoggingNamespace)
wsp = {'status': 'off', 'data': None, 'token': infoClient['token']}

if os.path.exists('./firefox_cache_v2'): shutil.rmtree('./firefox_cache_v2')
profiledir = os.path.join(".", "firefox_cache_v2")
if not os.path.exists(profiledir): os.makedirs(profiledir)
driver = WhatsAPIDriver(profile=profiledir,
                        client='remote',
                        command_executor=os.environ["SELENIUM"])


def on_connect():
    print('connect')
    socketIO.emit('status', wsp)


def on_welcome():
    print('Sesion completada')


def on_disconnect():
    print('Session desconectada')

Beispiel #33
0
class SocketManager():
    """SocketManager is a wrapper around socketIO to stabilize its packet
    passing. The manager handles resending packet, as well as maintaining
    alive status for all the connections it forms
    """

    # Time to acknowledge different message types
    ACK_TIME = {Packet.TYPE_ALIVE: 2, Packet.TYPE_MESSAGE: 2}

    # Default time before socket deemed dead
    DEF_SOCKET_TIMEOUT = 8

    def __init__(self,
                 server_url,
                 port,
                 alive_callback,
                 message_callback,
                 socket_dead_callback,
                 task_group_id,
                 socket_dead_timeout=None):
        """
        server_url:           url at which the server is to be run
        port:                 port for the socket to operate on
        alive_callback:       function to be called on alive Packets, defined
                               alive_callback(self, pkt)
        message_callback:     function to be called on message Packets, defined
                               message_callback(self, pkt)
        socket_dead_callback: function to be called when a socket dies, should
                              return false if the socket_manager should ignore
                              the death and treat the socket as alive defined
                               on_socket_dead(self, worker_id, assignment_id)
        socket_dead_timeout:  time to wait between heartbeats before dying
        """
        self.server_url = server_url
        self.port = port
        self.alive_callback = alive_callback
        self.message_callback = message_callback
        self.socket_dead_callback = socket_dead_callback
        if socket_dead_timeout == None:
            self.socket_dead_timeout = self.DEF_SOCKET_TIMEOUT
        else:
            self.socket_dead_timeout = socket_dead_timeout
        self.task_group_id = task_group_id

        self.socketIO = None

        # initialize the state
        self.listen_thread = None
        self.queues = {}
        self.threads = {}
        self.run = {}
        self.last_heartbeat = {}
        self.packet_map = {}

        # setup the socket
        self._setup_socket()

    def get_my_sender_id(self):
        """Gives the name that this socket manager should use for its world"""
        return '[World_{}]'.format(self.task_group_id)

    def _send_world_alive(self):
        """Registers world with the passthrough server"""
        self.socketIO.emit(data_model.SOCKET_AGENT_ALIVE_STRING, {
            'id': 'WORLD_ALIVE',
            'sender_id': self.get_my_sender_id()
        })

    def _send_response_heartbeat(self, packet):
        """Sends a response heartbeat to an incoming heartbeat packet"""
        self.socketIO.emit(data_model.SOCKET_ROUTE_PACKET_STRING,
                           packet.swap_sender().set_data('').as_dict())

    def _send_ack(self, packet):
        """Sends an ack to a given packet"""
        ack = packet.get_ack().as_dict()
        self.socketIO.emit(data_model.SOCKET_ROUTE_PACKET_STRING, ack, None)

    def _send_packet(self, packet, connection_id, send_time):
        """Sends a packet, blocks if the packet is blocking"""
        # Send the packet
        pkt = packet.as_dict()
        print_and_log('Send packet: {}'.format(packet.data))

        def set_status_to_sent(data):
            packet.status = Packet.STATUS_SENT

        self.socketIO.emit(data_model.SOCKET_ROUTE_PACKET_STRING, pkt,
                           set_status_to_sent)

        # Handles acks and blocking
        if packet.requires_ack:
            if packet.blocking:
                # blocking till ack is received or timeout
                start_t = time.time()
                while True:
                    if packet.status == Packet.STATUS_ACK:
                        # Clear the data to save memory as we no longer need it
                        packet.data = None
                        break
                    if time.time() - start_t > self.ACK_TIME[packet.type]:
                        # didn't receive ACK, resend packet keep old queue time
                        # to ensure this packet is processed first
                        packet.status = Packet.STATUS_INIT
                        self._safe_put(connection_id, (send_time, packet))
                        break
                    time.sleep(THREAD_SHORT_SLEEP)
            else:
                # non-blocking ack: add ack-check to queue
                t = time.time() + self.ACK_TIME[packet.type]
                self._safe_put(connection_id, (t, packet))

    def _setup_socket(self):
        """Create socket handlers and registers the socket"""
        self.socketIO = SocketIO(self.server_url, self.port)

        def on_socket_open(*args):
            print_and_log('Socket open: {}'.format(args), False)
            self._send_world_alive()
            self.alive = True

        def on_disconnect(*args):
            """Disconnect event is a no-op for us, as the server reconnects
            automatically on a retry"""
            print_and_log('World server disconnected: {}'.format(args), False)
            self.alive = False

        def on_message(*args):
            """Incoming message handler for ACKs, ALIVEs, HEARTBEATs,
            and MESSAGEs"""
            packet = Packet.from_dict(args[0])
            packet_id = packet.id
            packet_type = packet.type
            connection_id = packet.get_sender_connection_id()
            if packet_type == Packet.TYPE_ACK:
                if packet_id not in self.packet_map:
                    # Don't do anything when acking a packet we don't have
                    return
                # Acknowledgements should mark a packet as acknowledged
                print_and_log('On new ack: {}'.format(args), False)
                self.packet_map[packet_id].status = Packet.STATUS_ACK
                # If the packet sender wanted to do something on acknowledge
                if self.packet_map[packet_id].ack_func:
                    self.packet_map[packet_id].ack_func(packet)
                # clear the stored packet data for memory reasons
                self.packet_map[packet_id].data = None
            elif packet_type == Packet.TYPE_HEARTBEAT:
                # Heartbeats update the last heartbeat time and respond in kind
                self.last_heartbeat[connection_id] = time.time()
                self._send_response_heartbeat(packet)
            else:
                # Remaining packet types need to be acknowledged
                print_and_log('On new message: {}'.format(args), False)
                self._send_ack(packet)
                # Call the appropriate callback
                if packet_type == Packet.TYPE_ALIVE:
                    self.last_heartbeat[connection_id] = time.time()
                    self.alive_callback(packet)
                elif packet_type == Packet.TYPE_MESSAGE:
                    self.message_callback(packet)

        # Register Handlers
        self.socketIO.on(data_model.SOCKET_OPEN_STRING, on_socket_open)
        self.socketIO.on(data_model.SOCKET_DISCONNECT_STRING, on_disconnect)
        self.socketIO.on(data_model.SOCKET_NEW_PACKET_STRING, on_message)

        # Start listening thread
        self.listen_thread = threading.Thread(target=self.socketIO.wait,
                                              name='Main-Socket-Thread')
        self.listen_thread.daemon = True
        self.listen_thread.start()

    def open_channel(self, worker_id, assignment_id):
        """Opens a channel for a worker on a given assignment, doesn't re-open
        if the channel is already open. Handles creation of the thread that
        monitors that channel"""
        connection_id = '{}_{}'.format(worker_id, assignment_id)
        if connection_id in self.queues and self.run[connection_id]:
            print_and_log('Channel ({}) already open'.format(connection_id),
                          False)
            return
        self.run[connection_id] = True
        self.queues[connection_id] = PriorityQueue()

        def channel_thread():
            """Handler thread for monitoring a single channel"""
            # while the thread is still alive
            while self.run[connection_id]:
                try:
                    # Check if client is still alive
                    if (time.time() - self.last_heartbeat[connection_id] >
                            self.socket_dead_timeout):
                        self.run[connection_id] = False
                        self.socket_dead_callback(worker_id, assignment_id)

                    # Make sure the queue still exists
                    if not connection_id in self.queues:
                        self.run[connection_id] = False
                        break

                    # Get first item in the queue, check if we can send it yet
                    item = self.queues[connection_id].get(block=False)
                    t = item[0]
                    if time.time() < t:
                        # Put the item back into the queue,
                        # it's not time to pop yet
                        self._safe_put(connection_id, item)
                    else:
                        # Try to send the packet
                        packet = item[1]
                        if not packet:
                            # This packet was deleted out from under us
                            continue
                        if packet.status is not Packet.STATUS_ACK:
                            # either need to send initial packet
                            # or resend not-acked packet
                            self._send_packet(packet, connection_id, t)
                except Empty:
                    pass
                finally:
                    time.sleep(THREAD_MEDIUM_SLEEP)

        # Setup and run the channel sending thread
        self.threads[connection_id] = threading.Thread(
            target=channel_thread,
            name='Socket-Queue-{}'.format(connection_id))
        self.threads[connection_id].daemon = True
        self.threads[connection_id].start()

    def close_channel(self, connection_id):
        """Closes a channel by connection_id"""
        print_and_log('Closing channel {}'.format(connection_id), False)
        self.run[connection_id] = False
        if connection_id in self.queues:
            # Clean up packets
            packet_ids = list(self.packet_map.keys())
            for packet_id in packet_ids:
                if connection_id == \
                       self.packet_map[packet_id].get_receiver_connection_id():
                    del self.packet_map[packet_id]
            # Clean up other resources
            del self.queues[connection_id]
            del self.threads[connection_id]

    def delay_heartbeat_until(self, connection_id, delayed_time):
        """Delay a heartbeat prolong a disconnect until delayed_time"""
        self.last_heartbeat[connection_id] = delayed_time

    def close_all_channels(self):
        """Closes a channel by clearing the list of channels"""
        print_and_log('Closing all channels')
        connection_ids = list(self.queues.keys())
        for connection_id in connection_ids:
            self.close_channel(connection_id)

    def socket_is_open(self, connection_id):
        return connection_id in self.queues

    def queue_packet(self, packet):
        """Queues sending a packet to its intended owner"""
        connection_id = packet.get_receiver_connection_id()
        if not self.socket_is_open(connection_id):
            # Warn if there is no socket to send through for the expected recip
            print_and_log(
                'Can not send packet to worker_id {}: packet queue not found. '
                'Message: {}'.format(connection_id, packet.data))
            return
        print_and_log(
            'Put packet ({}) in queue ({})'.format(packet.id, connection_id),
            False)
        # Get the current time to put packet into the priority queue
        self.packet_map[packet.id] = packet
        item = (time.time(), packet)
        self._safe_put(connection_id, item)

    def get_status(self, packet_id):
        """Returns the status of a particular packet by id"""
        return self.packet_map[packet_id].status

    def _safe_put(self, connection_id, item):
        """Ensures that a queue exists before putting an item into it, logs
        if there's a failure
        """
        if connection_id in self.queues:
            self.queues[connection_id].put(item)
        else:
            print_and_log('Queue {} did not exist to put a message in'.format(
                connection_id))
Beispiel #34
0
class MainClient:
	def __init__(self, token):
		self.APIServer = '13.209.66.217'
		self.port = 3000
		self.Sensor = Sensor(self)
		self.token = token
		self.gid = -1
		self.gname = 'default gateway name'
		self.SocketIO = SocketIO(
			self.APIServer, self.port,
			headers = {'x-access-token': self.token},
		)
		self.Socket = self.SocketIO.define(BaseNamespace, '/gateway')
		
		self.IRSerial = IRSerial(self)
		self.IRSerialTemp = []

		self._setOnListener()

		# Check Setting backfile
		if os.path.isfile('setting.json'): 
			settings = json.loads(open('setting.json').read())
			self.gid = settings['gid']
			self.gname = settings['gname']
			self._emit_notifyGid()
		else:
			self.macAddr = self._getMAC('wlan0')
			self._emit_gatewayInit()

	def Run(self):
		print('MainClient Start!')
		self.Sensor.start()
		while True:
			self.SocketIO.wait()

	def _setOnListener(self):
		self.Socket.on('reconnect', self._on_reconnect)
		self.Socket.on('res/gateway/create', self._on_setGid)
		self.Socket.on('res/sensor/create', self._on_setSID)
		self.Socket.on('req/sensor/refresh', self._on_refreshSensor)
		self.Socket.on('req/sensor/update/name', self._on_setSensorName)
		self.Socket.on('req/sensor/update/interval', self._on_setSensorInterval)
		self.Socket.on('req/remocon/learn', self._on_getIRSerial)
		self.Socket.on('req/remocon/learn/OK', self._on_updateIRSerial)

	def _on_reconnect(self):
		print('reconnect')
		self._emit_notifyGid()

	def _on_setGid(self, obj):
		if obj['status'] is True:
			self.gid = obj['data']['gatewayID']

			f = open('setting.json', 'w', encoding='utf-8')
			f.write(json.dumps({
				'gid': self.gid,
				'gname': self.gname
			}, ensure_ascii=False, indent="\t"))
			f.close()
			self._emit_notifyGid()
	def _on_setSID(self, obj):
		if obj['status'] is True:
			baseID = obj['data']['_id']
			sid = obj['data']['sensorID']

			self.Sensor.updateServerSensorID(baseID, sid)
	def _on_refreshSensor(self, obj):
		self.Sensor.refreshSensor(obj['sid'])
	def _on_setSensorName(self, obj):
		self.Sensor.updateSensorName(obj['sid'],  obj['name'])
	def _on_setSensorInterval(self, obj):
		self.Sensor.updateSensorInterval(obj['sid'], obj['interval'])
	def _on_getIRSerial(self, obj):
		print('On getIRSerial!')
		# self.IRSerial.mode = 'in'
		# self.IRSerial.start()

	def _on_updateIRSerial(self, obj):
		self.IRSerial.start()

	def _emit_gatewayInit(self):
		self.Socket.emit('req/gateway/create',{
			'token': self.token,
			'name': self.gname,
			'mac_addr': self.macAddr
		})
		self.SocketIO.wait(seconds=1)
	def _emit_notifyGid(self):
		self.Socket.emit('req/connection/init',{
			'gid': self.gid
		})
		self.SocketIO.wait(seconds=1)
	def _emit_createSensor(self, s):
		self.Socket.emit('req/sensor/create', {
			'_id' : s['bid'],
			'gid' : self.gid,
			'type' : s['type'],
			'name' : s['name'],
		})
	def _emit_updateValueSensor(self, sid, value, time):
		self.Socket.emit('req/sensor/update', {
			'sid' : sid,
			'gid' : self.gid,
			'token': self.token,
			'time' : time,
			'value' : value,
		})
	def _emit_res_getIRSerial(self):
		self.Socket.emit('res/remocon/learn')

	def _getMAC(self, interface):
		try:
			str = open('/sys/class/net/%s/address' %interface).read()
		except:
			str = "00:00:00:00:00:00"

		return str[0:17]
Beispiel #35
0
class TelemetryReporter:
    """
    This is used to report boat and environment statistics (e.g. position, heading, wind speed & direction) to a
    telemetry server for remote monitoring.
    """

    def __init__(self):
        self.reporter = None
        self.socketIO = None
        self.rosbag_process = None

        # Register as a ROS node
        rospy.init_node('telemetry_reporter')

        # Listen for ROS boat position messages TODO Reference name from another file
        self.subscribers = {}
        self.publishers = {}

        # Listen for SIGINT (process termination requests)
        signal.signal(signal.SIGINT, self.terminate)

    def connect(self, server_address, port, use_ssl=False):
        print('Connecting to {} on port {} {}using SSL'.format(server_address, port, '' if use_ssl else 'without '))

        self.socketIO = SocketIO(server_address, port, verify=(not use_ssl))
        self.reporter = self.socketIO.define(ReportingNamespace, '/reporting')
        self.reporter.on('publishROSMessage', self._handle_server_publish_msg)
        self.reporter.on('getTopics', self._handle_get_published_topics_request)
        self.reporter.on('startStopRosbag', self.start_stop_rosbag)
        self.socketIO.wait()  # Don't finish execution

    def terminate(self, *args):
        if self.socketIO:
            print('Disconnecting...')
            self.socketIO.disconnect()

    def listen_to_topic(self, topic_name, msg_type, save_to_db=True, max_transmit_rate=0, queue_size=1):
        """
        Sets up a subscriber on the given ROS topic.
        :param {string} topic_name: the name of the ROS topic
        :param {string|genpy.Message} msg_type: the type of ROS message
        :param {boolean} save_to_db: whether or not messages sent to the server should be stored in the database
            or just forwarded on to connected observers
        :param {number} max_transmit_rate: the maximum rate at which to broadcast updates to the server (Hz)
        :param {number} queue_size: the maximum number of ROS messages to hold in the buffer
        :return: True if a subscriber was successfully registered, False otherwise
        """
        # Try to resolve the message type if it is not already a ROS message class
        if not issubclass(msg_type, Message):
            if type(msg_type) == str:
                # Try to look up in our dictionary
                msg_type = ROS_MSG_TYPES.get(msg_type)
                if not msg_type:  # Couldn't find given message type in dictionary
                    return False
            else:  # Not a subclass of genpy.Message and not a string
                return False

        # Define a handler to serialize the ROS message and send it to the server
        def msg_handler(msg):
            self.transmit_message(topic_name, msg, msg_type, save_to_db=save_to_db)

        self.subscribers[topic_name] = rospy.Subscriber(topic_name, msg_type, msg_handler, queue_size=queue_size)
        print('Registered listener for ' + topic_name)
        return True

    def transmit_message(self, topic_name, msg, msg_type, save_to_db=True):
        """
        Transmits a message to the server.
        :param {string} topic_name: the name of the ROS topic
        :param {genpy.Message} msg: the actual ROS message
        :param {type} msg_type: the type of ROS message
        :param {boolean} save_to_db: whether or not the message should be saved to the database
        """
        if not self.reporter:
            print('Not connected to /reporter')
            return

        payload = {
            'topicName': topic_name,
            'type': msg_type.__name__,
            'data': self._serialize_ros_message(msg),
            'timestamp': str(datetime.now()),
            'saveToDb': save_to_db
        }
        self.reporter.emit('message', payload)

    @staticmethod
    def _serialize_ros_message(msg):
        """
        Converts a ROS message into a dictionary that can be serialized and sent to the server.
        :param {genpy.Message} msg: the raw ROS message
        :return: a dictionary with the important values from the message
        """
        msg_type = type(msg)
        if msg_type == Pose2D:
            return {
                'x': msg.x,
                'y': msg.y,
                'theta': msg.theta
            }

        elif msg_type == Float32 \
                or msg_type == Float64 \
                or msg_type == UInt8 \
                or msg_type == UInt16 \
                or msg_type == UInt32 \
                or msg_type == UInt64:
            return msg.data

        elif msg_type == Float32MultiArray \
                or msg_type == Float64MultiArray:
            return list(msg.data)

        elif msg_type == GridMap:
            return {
                'grid': TelemetryReporter._serialize_ros_image_message(msg.grid),
                'minLatitude': msg.minLatitude.data,
                'maxLatitude': msg.maxLatitude.data,
                'minLongitude': msg.minLongitude.data,
                'maxLongitude': msg.maxLongitude.data
            }

        elif msg_type == WaypointList:
            points = []
            for lat, long in zip(msg.latitudes.data, msg.longitudes.data):
                points.append({'lat': lat, 'long': long})
            return points

        return None

    @staticmethod
    def _serialize_ros_image_message(msg):
        """
        Converts a ROS Image message into a dictionary.
        :param msg: the ROS Image message (from sensor_msgs.msg)
        :return: a dictionary with the image width, height, encoding, and pixel data
        """
        return {
            'height': msg.height,
            'width': msg.width,
            'encoding': msg.encoding,
            'data': msg.data
        }

    def publish_message(self, topic_name, msg_type, data):
        """
        Publishes a message to the given ROS topic.
        :param {string} topic_name: the name of the ROS topic to publish to
        :param {type} msg_type: the ROS message type
        :param data: the message payload
        """
        # Turn the data into a ROS message
        msg = self._build_ros_msg(data, msg_type)
        
        if msg is not None:
            # Register a publisher if one is not already registered for this topic
            if topic_name not in self.publishers:
                self.configure_publisher(topic_name, msg_type)
                # Note: Messages sent shortly after configuring publisher will likely be lost.
                # See https://github.com/ros/ros_comm/issues/176 for more info.
    
            self.publishers[topic_name].publish(msg)

    def _convert_unicode(self, data):
        """
        Recursively converts attributes unicode string attributes or elements on an object to UTF-8 strings.
        :param data: a dict, list, or string to convert to UTF-8.
        :return: the converted object
        """
        if isinstance(data, dict):
            return {self._convert_unicode(key): self._convert_unicode(value)
                    for key, value in data.iteritems()}
        elif isinstance(data, list):
            return [self._convert_unicode(element) for element in data]
        elif isinstance(data, unicode):
            return data.encode('utf-8')
        else:
            return data

    def configure_publisher(self, topic_name, msg_type, queue_size=1):
        """
        Sets up a ROS message publisher.
        :param topic_name: the name of the ROS topic to publish to
        :param msg_type: the type of messages to publish
        :param queue_size: the number of messages to queue up for sending before dropping old ones
        """
        if topic_name not in self.publishers:
            self.publishers[topic_name] = rospy.Publisher(topic_name, msg_type, queue_size=queue_size)

    def _build_ros_msg(self, data, msg_type):
        """
        Constructs a ROS message to transmit the given data.
        :param data: the data to hold in the message
        :param {genpy.Message} msg_type: the type of message to construct
        :return: a ROS message containing the data
        """
        try:
            # TODO Ints and arrays of ints

            if msg_type == UInt8 \
                    or msg_type == UInt16:  # Integers
                return msg_type(data=int(data))
            if msg_type == Float32 \
                    or msg_type == Float64:  # Floating-point numbers
                return msg_type(data=float(data))

            if msg_type == Float32MultiArray or msg_type == Float64MultiArray:  # Array of floating-point numbers
                return msg_type(data=[float(x) for x in data])

            if msg_type == String:
                return msg_type(data=data)

            if msg_type == Pose2D and 'x' in data and 'y' in data:
                return Pose2D(x=float(data['x']), y=float(data['y']))

        except ValueError:
            error_msg = 'Problem parsing data: ' + data + '. Supposed to be of type ' + str(msg_type)
            self.publish_message(ERROR_TOPIC_NAME, String, error_msg)

        return None

    def _handle_server_publish_msg(self, msg):
        """
        Handles WebSockets "publish" events from the server by publishing their info to ROS.
        :param msg: the WebSockets message payload
        """
        # Convert Unicode strings to UTF-8
        msg = self._convert_unicode(msg)

        # Make sure message type is specified and try to resolve it to ROS message class
        if 'type' in msg:
            msg_type = ROS_MSG_TYPES.get(msg['type'])
            if not msg_type:
                return self._publish_error_msg('Could not understand message type "' + msg['type'] + '".')
        else:
            return self._publish_error_msg('Attribute "type" must be specified.')
        # Check for the topic name
        if 'topicName' not in msg:
            return self._publish_error_msg('Attribute "topicName" must be specified.')
        # Check for the message payload
        elif 'data' not in msg:
            self._publish_error_msg('Attribute "data" must be specified.')

        self.publish_message(msg['topicName'], msg_type, msg['data'])

    def _publish_error_msg(self, error):
        """
        Logs an error by publishing it to the error log topic.
        :param {string} error: the content of the message
        """
        print(error)
        self.publish_message(ERROR_TOPIC_NAME, String, String(data=error))

    def _handle_get_published_topics_request(self, data):
        """
        Gets a list of the published ROS topics and their associated message types
        :param
        :return: A list of dictionaries of the form {name: topicName, type: topicType}
        :rtype list
        """
        res = []
        # Reformat the list items as dictionaries rather than arrays
        for topic in rospy.get_published_topics():
            res.append({
                'name': topic[0],
                'type': topic[1]
            })
        # Send the topics to the server
        self.reporter.emit('topicList', res)

    def start_stop_rosbag(self, data):
        """
        Handles a request from an observer to start or stop rosbag on the boat.
        This currently only supports one instance of rosbag. Attempts to start
        a second instance without first ending the first will be ignored.
        :param data: the WebSockets message from the observer
        """
        if 'action' not in data:
            return

        action = data['action']
        if self.rosbag_process and action == 'stop':  # Stop rosbag
            print('Stopping rosbag...')
            os.killpg(os.getpgid(self.rosbag_process.pid), signal.SIGINT)
            self.rosbag_process = None

        elif action == 'start':  # Start rosbag
            print('Starting rosbag...')
            cmd = 'rosbag record --all'
            if 'args' in data:
                cmd += ' ' + data['args']
            self.rosbag_process = subprocess.Popen(cmd, cwd=os.environ.get('HOME'), shell=True, preexec_fn=os.setsid)
Beispiel #36
0
 def run(self):
     self.socketIO = SocketIO(_SIO_URL_PREFIX, _SIO_PORT)
     self.namespace = self.socketIO.define(self._Namespace, self.addr)
     if self.sendinit:
         self.namespace.emit(*self.sendinit)
     self.socketIO.wait()
Beispiel #37
0
    print('on_chiotte_response', args)


def on_connect(self):
    print('[Connected]')


def on_reconnect(self):
    print('[Reconnected]')


def on_disconnect(self):
    print('[Disconnected]')


socketIO = SocketIO(WS_HOST, WS_PORT, LoggingNamespace)
socketIO.on('chiotte_response', on_chiotte_response)

distance_in_mm_old = 0
while running:
    distance_in_mm = tof.get_distance()
    if (distance_in_mm != distance_in_mm_old and 100 <= distance_in_mm <= 230):
        distance_in_mm_old = distance_in_mm
        data = json.dumps({
            "id": ID,
            "gender": POOP_LOCATION,
            "pq": distance_in_mm
        })
        print(data)
        socketIO.emit('chiotte', data)
        time.sleep(0.5)