Exemplo n.º 1
0
def test_server_different_port():
    # server, will be tested:
    server_3000 = OSCThreadServer(encoding='utf8')
    sock_3000 = server_3000.listen(address='0.0.0.0', port=3000, default=True)
    server_3000.bind(b'/callback_3000', callback_3000)
    # clients sending to different ports, used to test the server:
    client_3000 = OSCClient(address='localhost', port=3000, encoding='utf8')

    # server sends message to himself, should work:
    server_3000.send_message(b'/callback_3000', ["a"],
                             ip_address='localhost',
                             port=3000)
    sleep(0.05)
    # client sends message to server, will be received properly:
    client_3000.send_message(b'/callback_3000', ["b"])
    sleep(0.05)
    # sever sends message on different port, might crash the server on windows:
    server_3000.send_message(b'/callback_3000',
                             ["nobody is going to receive this"],
                             ip_address='localhost',
                             port=3001)
    sleep(0.05)
    # client sends message to server again. if server is dead, message will not be received:
    client_3000.send_message(b'/callback_3000', ["c"])
    sleep(0.1)  # give time to finish transmissions

    # if 'c' is missing in the received checklist, the server thread crashed and could not recieve the last message from the client:
    assert checklist == ['a', 'b', 'c']

    server_3000.stop()  # clean up
Exemplo n.º 2
0
def receive_values_from_max(ip, port):
    while (1):
        osc = OSCThreadServer()
        sock = osc.listen(address=ip, port=port, default=True)
        #osc.bind(b'/crowd-accel',acceleration_data)
        osc.bind(b'/crowd-pos', positional_data)
        sleep(1)
        osc.stop()
        return True
Exemplo n.º 3
0
def receive_values_from_max(ip, port):
    while (1):
        osc = OSCThreadServer()
        sock = osc.listen(address=ip, port=port, default=True)
        osc.bind(b'/density', printer)
        osc.bind(b'/cluster', printer)
        osc.bind(b"/mob_speed", printer)
        sleep(1000)
        osc.stop()
        return True
Exemplo n.º 4
0
def test_getaddress():
    osc = OSCThreadServer()
    sock = osc.listen()
    assert osc.getaddress(sock)[0] == '127.0.0.1'
    with pytest.raises(RuntimeError):
        osc.getaddress()

    sock2 = osc.listen(default=True)
    assert osc.getaddress(sock2)[0] == '127.0.0.1'
    osc.stop(sock)
Exemplo n.º 5
0
def test_stop_default():
    osc = OSCThreadServer()
    osc.listen(default=True)
    assert len(osc.sockets) == 1
    osc.stop()
    assert len(osc.sockets) == 0
Exemplo n.º 6
0
def test_stop_unknown():
    osc = OSCThreadServer()
    with pytest.raises(RuntimeError):
        osc.stop(socket.socket())
Exemplo n.º 7
0
def test_listen():
    osc = OSCThreadServer()
    sock = osc.listen()
    osc.stop(sock)
Exemplo n.º 8
0
class OBSRemote:
    def __init__(self,
                 osc_port,
                 osc_client_host,
                 osc_client_port,
                 host,
                 port,
                 password=''):
        self.client = obsws(host, port, password)
        self.register_callbacks()

        self.osc = OSCThreadServer(
            encoding='utf8',
            advanced_matching=True,
        )

        self.socket = None
        self.osc_client_host = osc_client_host
        self.osc_client_port = osc_client_port
        self.osc_port = osc_port
        self.volume_changed = False
        self.levels = [0, 0]

        self.scenes = []

    def start(self):
        self.client.connect()
        self.socket = self.osc.listen(address='0.0.0.0',
                                      port=self.osc_port,
                                      default=True)

        self.update_audio_sources()
        self.update_audio_levels()
        self.update_scenes()
        self.update_mute_status()
        self.register_osc_addresses()

        self.client.call(requests.SetHeartbeat(True))

    def stop(self):
        self.client.disconnect()
        self.osc.stop()
        self.socket = None

    def register_callbacks(self):
        self.client.register(self.scene_changed, event=events.SwitchScenes)
        self.client.register(self.mute_changed,
                             event=events.SourceMuteStateChanged)
        self.client.register(self.update_scenes, event=events.ScenesChanged)
        self.client.register(self.update_scenes,
                             event=events.SceneCollectionChanged)
        self.client.register(self.update_audio_sources,
                             event=events.SourceRenamed)
        self.client.register(self.update_audio_levels,
                             event=events.SourceVolumeChanged)
        self.client.register(self.status_update, event=events.Heartbeat)

    def register_osc_addresses(self):
        @self.osc.address('/scene/?/?', get_address=True)
        def scene_cb(address, values):
            if values < 1.0:
                return

            scene_id = int(address.split(b'/')[-1]) - 1
            if scene_id < len(self.scenes):
                self.osc.answer(address, [1.0])
                self.client.call(
                    requests.SetCurrentScene(self.scenes[scene_id]))

        @self.osc.address('/mic')
        def mic_cb(values):
            self.osc.answer('/mic', [values])
            self.client.call(
                requests.SetMute(self.audio_sources['mic'], values < 1.0))

        @self.osc.address('/audio')
        def audio_cb(values):
            self.osc.answer('/audio', [values])
            self.client.call(
                requests.SetMute(self.audio_sources['desktop'], values < 1.0))

        @self.osc.address('/cam')
        def cam_cb(values):
            self.osc.answer('/cam', [values])
            self.client.call(
                requests.SetSceneItemProperties('Webcam',
                                                visible=values > 0.0))
            self.client.call(
                requests.SetSceneItemProperties('Overlay: Webcam',
                                                visible=values > 0.0))

        @self.osc.address('/audio_level/?', get_address=True)
        def audio_level_cb(address, values):
            slider_id = int(address.split(b'/')[-1]) - 1
            self.levels[slider_id] = values
            self.volume_changed = True

        @self.osc.address('/rec')
        def rec_cb(values):
            if values > 0.0:
                self.client.call(requests.StartRecording())
            else:
                self.client.call(requests.StopRecording())
            self.osc.answer('/rec', [values])

        @self.osc.address('/stream')
        def stream_cb(values):
            if values > 0.0:
                self.client.call(requests.StartStreaming())
            else:
                self.client.call(requests.StopStreaming())
            self.osc.answer('/stream', [values])

    def mute_changed(self, event):
        name = event.getSourceName()
        muted = event.getMuted()
        if name == self.audio_sources['mic']:
            self._send_osc('/mic', 1.0 if muted is False else 0.0)
        if name == self.audio_sources['desktop']:
            self._send_osc('/audio', 1.0 if muted is False else 0.0)

    def scene_changed(self, event):
        name = event.getSceneName()
        for idx, scene_name in enumerate(self.scenes):
            if name == scene_name:
                self._send_osc('/scene/1/{}'.format(idx + 1), 1.0)
        self.update_mute_status(sources=event.getSources())

    def status_update(self, event: events.Heartbeat):
        streaming = event.getStreaming()
        recording = event.getRecording()
        try:
            stream_time = timedelta(seconds=event.getTotalStreamTime())
        except KeyError:
            stream_time = 0
        try:
            rec_time = timedelta(seconds=event.getTotalRecordTime())
        except KeyError:
            rec_time = 0
        self._send_osc('/stream', 1.0 if streaming else 0.0)
        self._send_osc('/rec', 1.0 if recording else 0.0)
        self._send_osc('/stream_time', str(stream_time))
        self._send_osc('/rec_time', str(rec_time))

    def update_mute_status(self, sources=None):
        if sources is None:
            sources = self.client.call(requests.GetCurrentScene()).getSources()
        webcam_found = False
        for source in sources:
            if source['name'] in ['Webcam', 'Overlay: Webcam']:
                webcam_found = True
                self._send_osc('/cam', 1.0 if source['render'] else 0.0)
        if not webcam_found:
            self._send_osc('/cam', 0.0)

    def update_scenes(self, *args):
        if len(args) > 0:
            self.scenes = [
                s['name'] for s in args[0].getScenes()
                if not s['name'].lower().startswith('overlay:')
            ]
        else:
            self.scenes = [
                s['name']
                for s in self.client.call(requests.GetSceneList()).getScenes()
                if not s['name'].lower().startswith('overlay:')
            ]

        for idx in range(0, 8):
            try:
                name = self.scenes[idx]
            except IndexError:
                name = ''

            self._send_osc('/scene_label_{}'.format(idx), name)
        self.update_mute_status()

    def update_audio_sources(self, *args):
        sources = self.client.call(requests.GetSpecialSources())
        self.audio_sources = {
            "mic": sources.getMic1(),
            "desktop": sources.getDesktop1()
        }

    def update_audio_levels(self, *args):
        if len(args) > 0:
            event = args[0]
            name = event.getSourceName()
            volume = None
            if isinstance(event, events.SourceVolumeChanged):
                volume = event.getVolume()
            muted = None
            if isinstance(event, events.SourceMuteStateChanged):
                muted = event.getMuted()

            if name == self.audio_sources['mic']:
                if volume is not None:
                    self._send_osc('/audio_level/1', volume)
                    self.levels[0] = volume
                if muted is not None:
                    self._send_osc('/mic', 1.0 if not muted else 0.0)
            elif name == self.audio_sources['desktop']:
                if volume is not None:
                    self._send_osc('/audio_level/2', volume)
                if muted is not None:
                    self._send_osc('/audio', 1.0 if not muted else 0.0)
        else:
            desktop = self.client.call(
                requests.GetVolume(self.audio_sources['desktop']))
            mic = self.client.call(
                requests.GetVolume(self.audio_sources['mic']))

            self._send_osc('/audio_level/2', desktop.getVolume())
            self.levels[1] = desktop.getVolume()
            self._send_osc('/audio', 1.0 if not desktop.getMuted() else 0.0)
            self._send_osc('/audio_level/1', mic.getVolume())
            self.levels[0] = mic.getVolume()
            self._send_osc('/mic', 1.0 if not mic.getMuted() else 0.0)

    def _send_osc(self, address, value):
        self.osc.send_message(address, [value], self.osc_client_host,
                              self.osc_client_port)

    def tick(self):
        if self.volume_changed:
            self.volume_changed = False
            self.client.call(
                requests.SetVolume(
                    list(self.audio_sources.values())[0], self.levels[0]))
            self.client.call(
                requests.SetVolume(
                    list(self.audio_sources.values())[1], self.levels[1]))
Exemplo n.º 9
0
def main():
    global pauser
    pauser = False
    warnings.filterwarnings("ignore")
    logging.disable(logging.CRITICAL)

    tf_session = tf.Session()
    K.set_session(tf_session)

    #Connection Initialization
    print("Creating Connections")
    #Connecting to max
    #Max client: Receive from Max
    try:
        max_client = udp_client.SimpleUDPClient(opt.maxip, opt.maxport)
        print("UDP Client connection to Max established")
    except:
        print(
            "UDP Client connection to Max failed. Will not be able to send from Max."
        )
        print('Error: {}. {}, line: {}'.format(sys.exc_info()[0],
                                               sys.exc_info()[1],
                                               sys.exc_info()[2].tb_lineno))

    #Max server: Listen to Max
    try:
        osc = OSCThreadServer()
        try:
            max_server = osc.listen(address=opt.chatip,
                                    port=opt.chatport,
                                    default=True)
            print("OSC Server initialized to listen to Max")
            osc.bind(b"/chat", chat_callback)
            osc.bind(b"/command", command_callback)
            osc.bind(b"/kill", kill_switch)
            ##########sddrd to test Vishal#########################
            osc.bind(b"/stop", stop_resume_operation)

        except:
            print("Tag is not in exceptable format")
        ##########sddrd to test Vishal#########################
    except:
        print(
            "OSC Server initialization failed. Will not be able to listen to Max"
        )
        print('Error: {}. {}, line: {}'.format(sys.exc_info()[0],
                                               sys.exc_info()[1],
                                               sys.exc_info()[2].tb_lineno))

    #################  Connecting to Camera 1  #####################

    try:
        cam1_socket = socket.socket()
        cam1_socket.connect((opt.camip1, opt.camport1))
        print("Connection to Camera 1 established")
    except:
        print("Unable to connect to Camera 1")
        print('Error: {}. {}, line: {}'.format(sys.exc_info()[0],
                                               sys.exc_info()[1],
                                               sys.exc_info()[2].tb_lineno))

    ############### Connecting to Camera 2 #######################

    try:
        cam2_socket = socket.socket()
        cam2_socket.connect((opt.camip2, opt.camport2))
        print("Connection to Camera 2 established")
    except:
        print("Unable to connect to Camera 2")
        print('Error: {}. {}, line: {}'.format(sys.exc_info()[0],
                                               sys.exc_info()[1],
                                               sys.exc_info()[2].tb_lineno))

    ########### Chatbot Generator #####################

    try:
        chatbot = Chatbot(tf_session)
        univEncoder = UnivEncoder(tf_session, chatbot.intents)
        print("Initialized chatbot dialogue system")
    except:
        print("Unable to initialize chatbot dialogue system. Exiting.")
        print('Error: {}. {}, line: {}'.format(sys.exc_info()[0],
                                               sys.exc_info()[1],
                                               sys.exc_info()[2].tb_lineno))
        exit()

    #################### Code for Haiku Poem Generation #######################
    # try:
    #     haiku_bot = Haiku_Bot(tf_session)
    #     output_dir = Path('models/poem_generator/trained_models')
    #     Get the parameters used for creating the model
    #     latent_dim, n_tokens, max_line_length, tokenizer = joblib.load(output_dir / 'metadata.pkl')
    #     # Create the new placeholder model
    #     training_model, lstm, lines, inputs, outputs = create_training_model(latent_dim, n_tokens)
    #     # Load the specified weights
    #     training_model.load_weights(output_dir / 'poem_generator_weights.hdf5')
    #     haiku_bot = Generator(lstm, lines, tf_session, tokenizer, n_tokens, max_line_length)
    #     print("Initialized chatbot poem generator.")
    # except:
    #     print("Unable to initialize chatbot poem generator.")
    #     print('Error: {}. {}, line: {}'.format(sys.exc_info()[0],
    #                                      sys.exc_info()[1],
    #                                      sys.exc_info()[2].tb_lineno))

    #Waiting for max command to start chatbot
    command = "stopped"
    # while command != "chat":
    #     while max_response.empty():
    #         stopped = True
    #     max_request = max_response.get()
    #     command = max_request['command']

    print(time.time())
    print("Starting chatbot for the first time")

    while command != "kill":

        users = False
        faces_count = 0
        chat_start = True
        returning_user = False
        ask_user_name = True

        while command != "pause" and command != "new" and command != "kill":

            try:
                if ask_user_name:
                    ask_user_name = False
                    try:
                        os.mkdir("users")
                    except:
                        users = True

                    ## Creating a txt file and storing all the sentence ##

                    global file
                    ## kk code to open and close history file start ##
                    file = open("history.txt", "w+")
                    file.close()
                    ## kk code to open and close history file end ##

                    #TODO: There is no kill switch check or command check. Need to implement it.
                    first_sentence = "Hello world!"
                    max_client.send_message(chat_tag, first_sentence)
                    history(first_sentence)
                    # time.sleep(chat_speed_slow)

                    while max_response.empty():
                        waiting_for_user = True
                    user_response = max_response.get()
                    history(user_response['intent'])

                    ## kk code start##
                    chat = "Hello! My name is Odo. I see {0} players. What are your names?".format(
                        players)
                    max_client.send_message(chat_tag, chat)
                    while max_response.empty():
                        waiting_for_user = True

                    history(chat)
                    try:
                        while max_response.empty():
                            waiting_for_user = True

                        for __ in range(players):
                            user_response = max_response.get()
                            print(user_response)
                            #TODO: We might need to validate the names
                            players_names.append(user_response['intent'])
                            history(user_response['intent'])
                    except excep as e:
                        print(e)

                    ######### Making directory for each user, commented for now start- kkk ##########
                    # dir_name = ''.join(e for e in user_name if e.isalnum())
                    # dir_name = dir_name.capitalize()
                    # try:
                    #     os.mkdir("users/"+dir_name)
                    # except:
                    #     returning_user = True
                    ######### Making directory for each user, commented for now end- kkk ##########

                    ## Greeting all the users ##
                    for name in players_names:
                        print("inside for loop")
                        chat = "Hi " + name + ", nice to meet you"
                        max_client.send_message(chat_tag, chat)
                        history(chat)
                        time.sleep(chat_speed)

                    while max_response.empty():
                        waiting_for_user = True

                    chat = "Where do you come from?"
                    max_client.send_message(chat_tag, chat)
                    history(chat)
                    while max_response.empty():
                        waiting_for_user = True

                    user_response = max_response.get()
                    #TODO: We might need to validate the cities
                    user_location = user_response['intent']
                    history(user_location)

                    #Monologue
                    # chat = "Nice to meet you. I am from very far away. I live here. Here on stage. I can’t leave the stage. This is my body. Do you see me?"

                    # max_client.send_message(chat_tag, chat)
                    # time.sleep(chat_speed)

                    ######### Get user emotion, commenting for now start - kkk ##############
                    try:
                        camera_message = "send_emotion"
                        cam1_socket.send(camera_message.encode('utf-8'))
                        emotion1 = cam1_socket.recv(1024).decode('utf-8')
                        emotion1 = json.loads(emotion1)
                        user_emotion1 = emotion1['emotion']
                        total_faces1 = emotion1['total_faces']
                        time1 = emotion1['time']

                        cam2_socket.send(camera_message.encode('utf-8'))
                        emotion2 = cam2_socket.recv(1024).decode('utf-8')
                        emotion2 = json.loads(emotion2)
                        user_emotion2 = emotion2['emotion']
                        total_faces2 = emotion2['total_faces']
                        time2 = emotion2['time']
                    except:
                        print('Error: {}. {}, line: {}'.format(
                            sys.exc_info()[0],
                            sys.exc_info()[1],
                            sys.exc_info()[2].tb_lineno))

                    # chat = 'It seems you are ' + user_emotion1
                    # max_client.send_message(chat_tag, chat)
                    # time.sleep(chat_speed)

                    ######### Get user emotion, commenting for now end - kkk ##############

                    # chat = 'Nice to meet you ' + user_name
                    # max_client.send_message(chat_tag, chat)
                    # history(chat)
                    # time.sleep(chat_speed)

                    chat = 'I am from very far away.'
                    max_client.send_message(chat_tag, chat)
                    history(chat)
                    time.sleep(chat_speed)

                    chat = 'I live here.'
                    max_client.send_message(chat_tag, chat)
                    history(chat)
                    time.sleep(chat_speed)

                    chat = 'Here on the stage'
                    max_client.send_message(chat_tag, chat)
                    history(chat)
                    time.sleep(chat_speed)

                    chat = 'I cannot leave the stage. This is my body.'
                    max_client.send_message(chat_tag, chat)
                    history(chat)
                    time.sleep(chat_speed)

                    chat = 'Do you see me?'
                    max_client.send_message(chat_tag, chat)
                    history(chat)
                    time.sleep(chat_speed)

                #TODO: Add kill switch check and command check
                try:
                    while max_response.empty():
                        waiting_for_user = True

                    user_response = max_response.get()
                    history(user_response['intent'])
                    user_chat = user_response['intent']
                    intent = univEncoder.match_intent(user_chat,
                                                      chatbot.story_progress)
                    if (intent == 'bye'):
                        chat = 'It was nice talking to you.'
                        max_client.send_message(chat_tag, chat)
                        history(chat)
                        command = 'kill'
                        file.close()
                    elif (intent == 'no_matching_intent'):
                        # chat = 'I am sorry. I did not get you. Try saying something else.'
                        chat = "Sorry I didnt get you, try saying something else"
                        # chat = univEncoder.chat_eliza(user_chat)
                        # print("Response from Eliza \n {0}".format(chat))
                        max_client.send_message(chat_tag, chat)
                        history(chat)

                        ##### Haiku poems, Disabling for now start - kkk #####
                        # poem = haiku_bot.generate_haiku([3, 5, 3], temperature=.3, first_char='cold')
                        # poem = haiku_bot.generate_haiku([3, 5, 3], temperature=.3, first_char='cold')
                        # line1 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[0])
                        # line2 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[1])
                        # line3 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[2])
                        # chat = line1 + line2 + line3
                        # max_client.send_message(chat_tag, chat)

                    # elif(intent == 'GIVE_POETRY'):
                    #     poem = haiku_bot.generate_haiku([3, 5, 3], temperature=.3, first_char='cold')
                    #     poem = haiku_bot.generate_haiku([3, 5, 3], temperature=.3, first_char='cold')
                    #     line1 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[0])
                    #     line2 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[1])
                    #     line3 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[2])
                    #     chat = line1 + line2 + line3
                    #     max_client.send_message(chat_tag, chat)

                    ##### Haiku poems, Disabling for now end - kkk #####

                    elif "transition" in intent:
                        next_story = intent.split("_")[2]
                        chatbot.change_story(next_story)
                        univEncoder.set_intent(chatbot.intents)
                        l = len(chatbot.intents[intent].responses)
                        if l > 0:
                            chatbot.story_progress = chatbot.intents[
                                intent].weight
                            chat = chatbot.intents[intent].responses[randrange(
                                l)]
                            max_client.send_message(chat_tag, chat)
                            history(chat)

                        ##### Haiku poems, Disabling for now start - kkk #####
                        # else:
                        #     poem = haiku_bot.generate_haiku([3, 5, 3], temperature=.3, first_char='cold')
                        #     poem = haiku_bot.generate_haiku([3, 5, 3], temperature=.3, first_char='cold')
                        #     line1 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[0])
                        #     line2 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[1])
                        #     line3 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[2])
                        #     chat = line1 + line2 + line3
                        #     max_client.send_message(chat_tag, chat)
                        ##### Haiku poems, Disabling for now end - kkk #####

                    else:
                        l = len(chatbot.intents[intent].responses)
                        if l > 0:
                            chatbot.story_progress = chatbot.intents[
                                intent].weight
                            chat = chatbot.intents[intent].responses[randrange(
                                l)]
                            max_client.send_message(chat_tag, chat)
                            history(chat)

                        ##### Haiku poems, Disabling for now start - kkk #####
                        # else:
                        #     poem = haiku_bot.generate_haiku([3, 5, 3], temperature=.3, first_char='cold')
                        #     poem = haiku_bot.generate_haiku([3, 5, 3], temperature=.3, first_char='cold')
                        #     line1 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[0])
                        #     line2 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[1])
                        #     line3 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[2])
                        #     chat = line1 + line2 + line3
                        #     max_client.send_message(chat_tag, chat)
                        ##### Haiku poems, Disabling for now end - kkk #####

                except:
                    print('Error: {}. {}, line: {}'.format(
                        sys.exc_info()[0],
                        sys.exc_info()[1],
                        sys.exc_info()[2].tb_lineno))
                    chat = 'I am sorry. I did not get you. Try saying something else.'
                    max_client.send_message(chat_tag, chat)
                    history(chat)

                ##### Haiku poems, Disabling for now start - kkk #####
                # poem = haiku_bot.generate_haiku([3, 5, 3], temperature=.3, first_char='cold')
                # poem = haiku_bot.generate_haiku([3, 5, 3], temperature=.3, first_char='cold')
                # line1 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[0])
                # line2 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[1])
                # line3 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[2])
                # chat = line1 + line2 + line3
                # max_client.send_message(chat_tag, chat)
                ##### Haiku poems, Disabling for now end - kkk #####

            except KeyboardInterrupt:
                print("Closing all active connections")
                command = "kill"

    try:
        camera_message = "stop_camera"
        try:
            cam1_socket.send(camera_message.encode('utf-8'))
            cam1_socket.close()
            print("Connection to Camera 1 closed")
        except:
            print("unable to close camera 1")

        try:
            cam2_socket.send(camera_message.encode('utf-8'))
            cam2_socket.close()
            print("Connection to Camera 2 closed")
        except:
            print("unable to close camera 2")

        try:
            osc.stop()
            print("Connection to Max closed")
        except:
            print("unable to close connectoin to Max")

    except:
        print('unable to close connections')
Exemplo n.º 10
0
class Midihost(threading.Thread):
    def __init__(self, command_q):
        super(Midihost, self).__init__()
        self.command_q = command_q
        self.stoprequest = threading.Event()
        self.osc = None

    def send_message(self, received_message):
        print received_message
        self.outputport.send(received_message)

    def stop(self):
        self.outputport.close()
        self.osc.stop()

    def connect_output(self):
        while True:
            try:
                self.outputport = mido.open_output()
                outputs = mido.get_output_names()
                if Config.MIDIOUT:
                    out = [
                        s for s in outputs
                        if Config.MIDIOUT.lower() in s.lower()
                    ]
                    if out:
                        self.outputport = mido.open_output(out[0])
                        print "Midi output device connected: " + out[0]
                        break
                    else:
                        raise EnvironmentError
            except EnvironmentError:
                self.outputport.close()
                print "MIDIOUT not found: " + Config.MIDIOUT
                print "Please connect midi device"
                time.sleep(3)

    def callback(self, path, values=None):
        print "osc: " + path + " : " + str(values)
        message = None
        command = path.split('/')[1]
        if command == "control_change":
            splitted = str(values).split('.')
            value = int(splitted[0])
            message = mido.Message('control_change',
                                   channel=Config.MIDICHANNEL - 1,
                                   control=int(path.split('/')[2]),
                                   value=value)
        if command == "program_change":
            message = mido.Message('program_change',
                                   channel=Config.MIDICHANNEL - 1,
                                   program=int(values))
        if command == "control_change_slider":
            message = mido.Message('control_change',
                                   channel=Config.MIDICHANNEL - 1,
                                   control=int(path.split('/')[2]),
                                   value=int(values))
        if command == "quit":
            self.command_q.put("KILLSIGNAL")
        if command == "reboot":
            self.command_q.put("REBOOTSIGNAL")
        if command == "restart":
            self.stop()
            Config.restart(__file__)
        if command == "scroll_preset_up":
            self.callback("/control_change/71", "1.0")
            self.callback("/control_change/50", values)
        if command == "scroll_preset_down":
            self.callback("/control_change/71", "1.0")
            self.callback("/control_change/49", values)

        if message:
            self.send_message(message)

    def run(self):
        self.osc = OSCThreadServer(default_handler=self.callback)
        self.osc.listen(address='0.0.0.0', port=8000, default=True)
        self.connect_output()
        while not self.stoprequest.isSet():
            try:
                message = self.command_q.get(True, 0.05)
                if str(message) == "KILLSIGNAL":
                    Config.shutdown()
                elif str(message) == "REBOOTSIGNAL":
                    Config.reboot()
                else:
                    self.send_message(message)
            except Queue.Empty:
                continue
        self.osc.stop()

    def join(self, timeout=None):
        super(Midihost, self).join(timeout)
Exemplo n.º 11
0
class riot_osc():
    '''classe principale pour le controle du drone par le capteur'''
    #NOTTODO : fonction atterissage;
    #TODO : si tous les modes (surtout avec multiranger en même temps - si thread parallel -> probleme?)
    #todo : orientation fonctionne (lag visible (- une seconde ~250ms)), altitude: pbs pas trop deterministe
    # vitesses max évitement
        #bien modifiée en paramètres mais evolution dynamique bizarre, tests multi-ranger pas faits.
    #cnotrol manuel fonctionne avec clavier directionnel + u et d pour altitude (plutot reactif (comme orientaiton voir moins))

    def __init__(self, multiranger, motion_commander, cf = None, to_alt = 0.5, graph = False):
        self.osc = OSCThreadServer()
        #les valeurs d'angle renvoyée par la centrale gyroscopique sur les 3 axes
        self.euler_x = 0
        self.euler_y = 0
        self.euler_z = 0
        #les angles renvoyés lors du signal précedent
        self.ante_x = 0
        self.ante_z = 0
        self.ante_y = 0
        #la valeur d'angle de la commande pour faire tourner le drone
        self.angle = 0
        #compte le nombre de fois où la différence entre la valeur d'angle recu et la valeur précédente, 
        #pour éviter de surcharger le drone de commande
        self.count = 0
        #booléen renseignant sur l'état du drone : en vol ou non
        self.keep_flying = True 

        #altitude du drone
        self.z = to_alt

        #vitesse d'évitement initiale du drone
        self.velocity = 0.5


        #initialisation des valeurs d'angle sur x et z au commencement du programme
        self.orig_x = 0
        self.orig_z = 0

        #variables propres au drones
        self.cf = cf
        self.multiranger = multiranger
        self.motion_commander = motion_commander

        #variable indiquant si le programme doit commencer à compter (ou pas) le nombre
        #de valeur d'angle non nul 
        self.flag = False
        self.sock = 0

        #Initialisation du thread_multirange
        #ATTENTION A DES POSSIBLES PROBLEMES, LE DRONE EST SUREMENT UNE RESSOURCE CRITIQUE
        self.thread_multirange = Thread(target = self.multirange_push)
        self.thread_multirange.start()


    def set_orig(self, *args, axes = "all"):
        #fonction lancée au début du programme qui récupère les valeurs initiales du capteur pour définir une origine pour la suite
        if axes == "all" or axes == "x": 
            self.orig_x = self._normalize__(args[18],0)
        if axes == "all" or axes == "z": 
            self.orig_z = self._normalize__(args[20],0)

        self.osc.unbind(b'/0/raw',self.set_orig)
        self.osc.bind(b'/0/raw',self.callback)


    def _normalize__(self, value, orig):
        #fonction qui prend en paramètre une valeur renvoyé par le capteur et l'origine définit au lancement
        #qui normalise cette valeur entre 0 et 360
        value = round(value)
        if value < 0:
            value = 360 + value
        value -= orig
        if value < 0 : value += 360
        return value

    def callback(self, *args):
        #fonction principale de callback liée au capteur
        self.euler_z = self._normalize__(args[20], self.orig_z)
        try:
            self.euler_y = round(args[19])
        except:
            self.euler_y = -90
        #print(self.euler_x, self.euler_y, self.euler_z)
        if abs(self.euler_z) <= 10 and abs(self.euler_y) <= 10:
            #appel de la fonction de contrôle de l'orientation
            self.callback_x(*args)
        elif abs(self.euler_z - 90) <= 10  and abs(self.euler_y) <= 10:
            #appel de la fonction d'atterrissage
            self.landing()
        elif abs(self.euler_z - 270) <= 10 and abs(self.euler_y) <= 10:
            #appel de la fonction de contrôle de la vitesse d'évitement
            self.set_velocity(*args)
        elif abs(self.euler_z - 180) <= 15 and abs(self.euler_y) <= 12:
            #appel de la fonction de vol stationaire
            self.idle()
        elif abs(self.euler_y - 90) <= 10:
            #appel de la fonction de contrôle de l'altitude
            self.callback_z(*args)
        elif abs(self.euler_y + 90) <= 20:
            #appel de la fonctoin pour controler manuellement le drone
            self.manual_control()
        else:
            pass
        

    def landing(self):
        #fonction d'atterrisage, besoin de valider par appui sur espace
        print("appuyer sur espace quelque seconde pour atterrir et ensuite arrêter le drone")
        if keyboard.is_pressed("space"):
            self.keep_flying = False
            print("Atterrissage...")
        

    def idle(self):
        #fonction de vol stationnaire
        print("idle...")
        pass

    def set_velocity(self, *args):
        #fonction pour modifier la vitesse d'évitement du multiranger
        self.euler_x = self._normalize__(args[18],self.orig_x)
        angle = self.euler_x - self.ante_x
        if abs(angle) >= 180 :
            n_angle = angle % 180
            angle = n_angle if angle > 0 else -n_angle
        if angle != 0 :
            delta = angle/360
            if delta <= self.velocity: self.velocity += delta
            print("La vitesse d'évitement est maintenant de = ", self.velocity, " m/s. ", angle)
        self.ante_x = self.euler_x
        
    def callback_z(self, *args):
        #fonction de contrôle de l'altitude
        self.euler_x = self._normalize__(args[18],self.orig_x)
        angle = self.euler_x - self.ante_x
        if abs(angle) >= 180 :
            n_angle = angle % 180
            angle = n_angle if angle > 0 else -n_angle
        if angle != 0:
            self.flag = True
            self.angle += angle
        else:
            if self.flag : self.count += 1
        if self.count == 10:
            if self.cf != None :
                if abs(self.angle) > 0:
                    if self.angle <= 0:
                        self.motion_commander.down(abs(self.angle)/360,0.5)
                        self.z -= abs(self.angle)/360
                        if self.z < 0.15:
                            self.keep_flying = False
                    else:
                        self.motion_commander.up(self.angle/720,0.5)
                        self.z += abs(self.angle)/360
            self.count = 0
            self.angle = 0
            self.flag = False
        self.ante_x = self.euler_x
        
    def callback_x(self, *args):
        #fonction de contrôle de l'orientation (lacet)
        self.euler_x = self._normalize__(args[18],self.orig_x)
        angle = self.euler_x - self.ante_x
        if abs(angle) >= 180 :
            n_angle = angle % 180
            angle = n_angle if angle > 0 else -n_angle
        if angle != 0:
            self.flag = True
            self.angle += angle
        else:
            if self.flag : self.count += 1
        if self.count == 10:
            if self.cf != None :
                if abs(self.angle) > 0:
                    if self.angle >= 0:
                        self.motion_commander.turn_left(abs(self.angle), 180)
                    else:
                        self.motion_commander.turn_right(abs(self.angle), 180)
            self.count = 0
            self.angle = 0
            self.flag = False
        self.ante_x = self.euler_x

    def manual_control(self):
        #fonction de contrôle manuel du drone
        velocity_x = 0
        velocity_y = 0
        velocity_z = 0
        VELOCITY = 0.5
        if keyboard.is_pressed("up"):
            velocity_x += VELOCITY
        if keyboard.is_pressed("down"):
            velocity_x -= VELOCITY
        if keyboard.is_pressed("left"):
            velocity_y -= VELOCITY
        if keyboard.is_pressed("right"):
            velocity_y += VELOCITY
        if keyboard.is_pressed("u"):
            velocity_z += VELOCITY
        if keyboard.is_pressed("d"):
            velocity_z -= VELOCITY   
        self.motion_commander.start_linear_motion(velocity_x, velocity_y, velocity_z)
        print("velocity_x = ", velocity_x, " velocity_y = ", velocity_y, " velocity_z = ", velocity_z)

    def sock_connect(self):
        #fonction qui établit la connection avec le capteur au  lancement
        self.sock = self.osc.listen(address='0.0.0.0', port=8000, default=True)

    def run(self):
        #fonction principale qui établit la position d'origine puis lie la fonction de callback au capteur 
        #et lance une boucle infinie tant que la fonction d'aterrrissage n'est pas lancée
        try:
            self.sock_connect()
            self.osc.bind(b'/0/raw',self.set_orig)
            print(self.keep_flying)
            while self.keep_flying:
                sleep(0.05)
        except KeyboardInterrupt:
            self.osc.stop_all()
            print('stopped all for keyboard interupt')
        self.thread_multirange.join()

    def stop(self):
        #fonction qui libère le capteur à la fin du programme
        self.osc.stop(self.sock)
        print("Stopped OCS")

    def multirange_push(self):
        #fonction appelée par le thread du multiranger qui permet de la faire tourner en concurrence avec les fonctions
        #de contrôle de l'orientation et autre
        def is_close(range):
            MIN_DISTANCE = 0.2  # m
            if range is None:
                return False
            else:
                return range < MIN_DISTANCE


        while self.keep_flying:
            velocity_x = 0
            velocity_y = 0

            if is_close(self.multiranger.front):
                velocity_x -= self.velocity
            if is_close(self.multiranger.back):
                velocity_x += self.velocity

            if is_close(self.multiranger.left):
                velocity_y -= self.velocity
            if is_close(self.multiranger.right):
                velocity_y += self.velocity

            if is_close(self.multiranger.up):
                self.keep_flying = False

            self.motion_commander.start_linear_motion(
                velocity_x, velocity_y, 0)

            sleep(0.1)
Exemplo n.º 12
0
from oscpy.server import OSCThreadServer
from time import sleep

print("Starting")


def callback(*values):
    print("got values: {}".format(values))


osc = OSCThreadServer()
sock = osc.listen(address='127.0.0.1', port=8000, default=True)
osc.bind(b'/hello/world', callback, sock)
sleep(5000)
osc.stop()
Exemplo n.º 13
0
class station():
    """
    Main object to manage a station
    All configuration parameter are token from DB
    For now is possible to manage one board a time and only one transport for each
    You have to specify the station and the board to use.
    If not specified the passive transport will be selected by hard coded predefined priority:
    * bluetooth (hight)
    * serial (medium)
    * tcpip (low)
    For active transport only MQTT will be taken in account for publish (no AMQP)
    """
    def __init__(self,
                 slug=None,
                 username=None,
                 boardslug=None,
                 picklefile="saveddata-service.pickle",
                 trip=None,
                 gps=plyergps(),
                 transport_name=None,
                 logfunc=jsonrpc.log_stdout):
        '''
        do all startup operations
        '''

        print("INITIALIZE rmap station")
        self.picklefile = picklefile

        self.anavarlist = []
        self.datavarlist = []
        self.bluetooth = None
        self.mqtt_status = _('Connect Status: disconnected')
        self.rpcin_message = b""
        self.log = logfunc
        self.now = None

        self.anavarlist = []
        self.datavarlist = []
        self.trip = False
        self.slug = "BT_fixed"
        self.boardslug = "BT_fixed"

        # Exception: The intent ACTION_ACL_DISCONNECTED doesnt exist
        #self.br=BroadcastReceiver(self.on_broadcast,actions=["acl_disconnected"])
        #self.br.start()

        self.gps = gps

        try:
            if os.path.isfile(self.picklefile):
                with open(self.picklefile, "rb") as file:
                    self.anavarlist = pickle.load(file)
                    self.datavarlist = pickle.load(file)
                    self.trip = pickle.load(file)
                    self.slug = pickle.load(file)
                    self.boardslug = pickle.load(file)
            else:
                print("file ", self.picklefile, " do not exist")
        except:
            print("ERROR loading saved data")
            self.anavarlist = []
            self.datavarlist = []
            self.trip = False
            self.slug = "BT_fixed"
            self.boardslug = "BT_fixed"

        if trip is not None:
            self.trip = trip

        if slug is not None:
            self.slug = slug

        if boardslug is not None:
            self.boardslug = boardslug

        try:
            print("get information for station:", self.slug)
            if username is None:
                mystation = StationMetadata.objects.filter(slug=self.slug)[0]
            else:
                mystation = StationMetadata.objects.get(
                    slug=self.slug, ident__username=username)
        except ObjectDoesNotExist:
            print("not existent station in db: do nothing!")
            #raise SystemExit(0)
            raise Rmapdonotexist("not existent station in db")

        if not mystation.active:
            print("Warning: disactivated station!")

        self.lon = mystation.lon
        self.lat = mystation.lat
        self.mqtt_ident = str(mystation.ident)
        self.prefix = mystation.mqttrootpath
        self.maintprefix = mystation.mqttmaintpath
        self.network = mystation.network
        self.transport_name = None
        self.transport = None
        self.active = mystation.active

        for cdata in mystation.stationconstantdata_set.all():
            print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> constant data: ",
                  cdata.btable)
            if not cdata.active: continue
            print("found a good constant data")
            self.anavarlist.append({
                "coord": {
                    "lat": self.lat,
                    "lon": self.lon
                },
                "anavar": {
                    cdata.btable: {
                        "v": cdata.value
                    }
                }
            })

        self.drivers = []

        print("get info for BOARD:", self.boardslug)
        for board in mystation.board_set.all().filter(slug=self.boardslug):
            print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> configure board: ",
                  board.name)
            if not board.active: continue
            print("found a good base board")

            try:
                if (board.transportmqtt.active):
                    print("MQTT Transport", board.transportmqtt)

                    self.mqtt_sampletime = board.transportmqtt.mqttsampletime
                    self.mqtt_host = board.transportmqtt.mqttserver
                    self.mqtt_user = board.transportmqtt.mqttuser
                    self.mqtt_password = board.transportmqtt.mqttpassword

            except ObjectDoesNotExist:
                print("transport mqtt not present")
                self.mqtt_sampletime = None
                self.mqtt_host = None
                self.mqtt_user = None
                self.mqtt_password = None
                #raise SystemExit(0)

            try:
                if (board.transporttcpip.active):
                    print("TCPIP Transport", board.transporttcpip)

                    self.tcpip_name = board.transporttcpip.name
                    self.transport_name = "tcpip"

            except ObjectDoesNotExist:
                print("transport tcpip not present")
                self.tcpip_name = None
            #    raise SystemExit(0)

            try:
                if (board.transportserial.active):
                    print("Serial Transport", board.transportserial)

                    self.serial_device = board.transportserial.device
                    self.serial_baudrate = board.transportserial.baudrate
                    self.transport_name = "serial"

            except ObjectDoesNotExist:
                print("transport serial not present")
                self.serial_device = None
                self.serial_baudrate = None
            #    raise SystemExit(0)

            try:
                if (board.transportbluetooth.active):
                    print("Bluetooth Transport", board.transportbluetooth)
                    self.bluetooth_name = board.transportbluetooth.name
                    self.transport_name = "bluetooth"

            except ObjectDoesNotExist:
                print("transport bluetooth not present")
                self.bluetooth_name = None
                #raise SystemExit(0)

            if not transport_name is None:
                self.transport_name = transport_name

            for sensor in board.sensor_set.all():
                if not sensor.active: continue
                #name,i2cbus,address,timerange,level
                #TODO sensor.type ??
                self.drivers.append({
                    "driver": sensor.driver,
                    "type": sensor.type.type,
                    "i2cbus": sensor.i2cbus,
                    "address": sensor.address,
                    "node": sensor.node,
                    "timerange": sensor.timerange,
                    "level": sensor.level
                })

    def ismobile(self):
        return self.network == "mobile"

    def display(self):

        print("station: >>>>>>>>")

        print("lon:", self.lon)
        print("lat:", self.lat)
        print("mqtt ident:", self.mqtt_ident)
        print("prefix:", self.prefix)
        print("maintprefix", self.maintprefix)
        print("network:", self.network)

        print("board: >>>>>>>>")
        try:
            print("sampletime:", self.mqtt_sampletime)
        except:
            pass
        try:
            print("host:", self.mqtt_host)
        except:
            pass
        try:
            print("user:"******"password:"******"")
            print("transport:", self.transport_name)
        except:
            pass

        if self.transport_name == "bluetooth":
            print("bluetooth_name:", self.bluetooth_name)

        if self.transport_name == "serial":
            print("serial_device:", self.serial_device)
            print("serial_baudrate:", self.serial_baudrate)

        if self.transport_name == "tcpip":
            print("tcpip_name:", self.tcpip_name)

        print(">>>> sensors:")
        print(self.drivers)

    def rpcin(self, message):
        """
        Get a message from osc channel
        """
        print("station RPC: {}".format(message))
        print(message)
        self.rpcin_message = message

    def rpcout(self, message):
        """
        Send a message to osc channel
        """
        send_message(b'/rpc', [
            message,
        ],
                     ip_address='localhost',
                     port=3001,
                     safer=True)

    def on_stop(self):
        '''
        called on application stop
        Here you can save data if needed
        '''
        print(">>>>>>>>> called on application stop")

        try:
            self.stoptransport()
            print("transport stopped")
        except:
            print("stop transport failed")

        # this seems required by android >= 5
        if self.bluetooth:
            self.bluetooth.close()

        self.stopmqtt()
        print("mqtt stopped")

        self.gps.stop()
        print("gps stopped")

        #self.br.stop()

        print("start save common parameters")
        with open(self.picklefile, "wb") as file:
            pickle.dump(self.anavarlist, file)
            pickle.dump(self.datavarlist, file)
            pickle.dump(self.trip, file)
            pickle.dump(self.slug, file)
            pickle.dump(self.boardslug, file)
        print("end save common parameters")

    def on_pause(self):
        '''
        called on application pause
        Here you can save data if needed
        '''
        print(">>>>>>>>> called on application pause")

        self.on_stop()

        return True

    def on_resume(self):
        '''
        called on appication resume
        Here you can check if any data needs replacing (usually nothing)
        '''
        print(">>>>>>>>> called on appication resume")

        #self.br.start()
        try:
            if os.path.isfile(self.picklefile):
                with open(self.picklefile, "rb") as file:
                    self.anavarlist = pickle.load(file)
                    self.datavarlist = pickle.load(file)
                    self.trip = pickle.load(file)
                    self.slug = pickle.load(file)
                    self.boardslug = pickle.load(file)
            else:
                print("file ", self.picklefile, " do not exist")
        except:
            print("ERROR loading saved data")
            self.anavarlist = []
            self.datavarlist = []
            self.trip = False
            self.slug = "BT_fixed"
            self.boardslug = "BT_fixed"

    def configurestation(self, board_slug=None, username=None):
        """
        configure the board with those steps:
        * reset configuration
        * configure board
        * save configuration
        """

        try:
            self.starttransport()
            if board_slug is None:
                board_slug = self.boardslug
            if username is None:
                username = self.username

            print("configstation:", self.slug, board_slug, board_slug,
                  username)
            rmap_core.configstation(station_slug=self.slug,
                                    board_slug=board_slug,
                                    transport=self.transport,
                                    logfunc=self.log,
                                    username=username)

        except Exception as e:
            print("error in configure:")
            print(e)
            raise

        finally:
            self.stoptransport()

    def sensorssetup(self):
        """
        Setup of all sensors.
        This should be done at startup 
        """

        self.sensors = []
        for driver in self.drivers:
            try:
                print("driver: ", driver)

                if driver["driver"] == "JRPC":
                    print("found JRPC driver; setup for bridged RPC")
                    sd = SensorDriver.factory(driver["driver"],
                                              transport=self.transport)
                    # change transport !
                    sd.setup(driver="I2C",
                             node=driver["node"],
                             type=driver["type"],
                             address=driver["address"])

                elif driver["driver"] == "RF24":
                    print("found RF24 driver; setup for bridged RPC")
                    sd = SensorDriver.factory("JRPC", transport=self.transport)
                    # change transport !
                    sd.setup(driver=driver["driver"],
                             node=driver["node"],
                             type=driver["type"],
                             address=driver["address"])

                else:
                    sd = SensorDriver.factory(driver["driver"],
                                              type=driver["type"])
                    sd.setup(i2cbus=driver["i2cbus"],
                             address=driver["address"])

                self.sensors.append({
                    "driver": sd,
                    "timerange": driver["timerange"],
                    "level": driver["level"]
                })

            except:
                print("error in setup; sensor disabled:",\
                " driver=",driver["driver"],\
                " node=",driver["node"],\
                " type=",driver["type"],\
                " address=",driver["address"])
                raise Exception("sensors setup", 1)

    def getdata(self, trip=None, now=None):
        """
        get data for all sensors with those steps:
      
        * prepare sensors
        * wait for samples are ready
        * get samples

        return False if the transport never works (used to reconnect bluetooth) 
        """

        print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>getdata")

        if trip is None:
            trip = self.trip

        if trip and not self.ismobile():
            print("trip with fix station: do nothing")
            return True, {}

        if not trip and self.ismobile():
            print("not on trip with mobile station: do nothing")
            return True, {}

        if trip and self.gps.gpsfix:
            self.lat = self.gps.lat
            self.lon = self.gps.lon
            #self.anavar["B07030"]={"v": self.gps.height}   # add
            #self.anavar={"B07030":{"v": self.gps.height}}   # overwrite
            self.anavarlist.append({
                "coord": {
                    "lat": self.lat,
                    "lon": self.lon
                },
                "anavar": {
                    "B07030": {
                        "v": self.gps.height
                    }
                }
            })

        elif trip:
            print("we have lost gps during a trip")
            return True, {}

        dt = 0
        connected = False
        if now is None:
            now = datetime.utcnow().replace(microsecond=0)

        for sensor in self.sensors:
            print("prepare: ", sensor)
            try:
                dt = max(sensor["driver"].prepare(), dt)
                connected = True

            except Exception as e:
                print(e)
                print("ERROR executing prepare rpc")
                traceback.print_exc()

        print("sleep ms:", dt)
        time.sleep(dt / 1000.)

        #message=""
        datavars = []
        for sensor in self.sensors:
            try:
                for btable, value in sensor["driver"].get().items():
                    datavar = {btable: {"t": now, "v": value}}
                    datavars.append(datavar)
                    self.datavarlist.append({"coord":{"lat":self.lat,"lon":self.lon},"timerange":sensor["timerange"],\
                                             "level":sensor["level"],"datavar":datavar})
#                stringa =""
#                for btable,data in datavar.iteritems():
#                    stringa += btable+": "+ data["t"].strftime("%d/%m/%y %H:%M:%S")+" -> "+str(data["v"])+"\n"
#                message=stringa

            except Exception as e:
                print(e)
                print("ERROR executing getJson rpc")
                traceback.print_exc()

        return connected, datavars

#    def on_broadcast(self,context,intent):
#
#        print "BLUETOOTH disconnected"
#
#        extras=intent.getExtras()
#        device= extras.get("device")
#
#        print "BLUETOOTH received:",device

    def startmqtt(self):
        '''
        begin mqtt connection
        and publish constat station data
        '''
        print(">>>>>>> startmqtt")

        #config = self.config

        if self.lat is None or self.lon is None:
            print("you have to set LAT and LON")
            self.mqtt_status = _(
                'Connect Status: ERROR, you have to define a location !')
            return

        self.stopmqtt()

        self.mqtt_status = _('Connect Status: connecting')

        try:
            self.rmap = rmapmqtt(ident=self.mqtt_ident,
                                 lon=self.lon,
                                 lat=self.lat,
                                 network=self.network,
                                 host=self.mqtt_host,
                                 username=self.mqtt_user,
                                 password=self.mqtt_password,
                                 prefix=self.prefix,
                                 maintprefix=self.maintprefix,
                                 logfunc=self.log)

            self.rmap.loop_start()

        except:
            pass

    def publishmqtt(self):
        '''
        publish data on mqtt server
        '''

        # here we generate randon data to test only
        #-----------------------------------------------------------
        #        timerange="254,0,0"               # dati istantanei
        #        level="105,2,-,-"                 # livello fittizio 2
        #
        #        value=random.randint(25315,30000) # tempertaure in cent K
        #        datavar={"B12101":
        #                 {
        #                     "t": datetime.utcnow(),
        #                     "v": value,
        #                     "a": {
        #                         "B33194": "90",           # attributi di qualita' del dato
        #                         "B33195": "85"
        #                     }
        #                 }}

        #-----------------------------------------------------------

        #print "queue ana :",self.anavarlist
        #print "queue data:",self.datavarlist

        try:
            if self.rmap.connected:

                newanavarlist = []
                for item in self.anavarlist:
                    print("try to publish", item)
                    try:
                        self.rmap.ana(item["anavar"],
                                      lon=item["coord"]["lon"],
                                      lat=item["coord"]["lat"])

                    except:
                        newdatavarlist.append(item)
                        self.mqtt_status = _(
                            'Connect Status: ERROR on Publish')

                self.anavarlist = newanavarlist

                newdatavarlist = []
                for item in self.datavarlist:
                    print("try to publish", item)
                    try:
                        self.rmap.data(item["timerange"],
                                       item["level"],
                                       item["datavar"],
                                       lon=item["coord"]["lon"],
                                       lat=item["coord"]["lat"],
                                       prefix=item.get("prefix", None))
                        self.mqtt_status = _('Connect Status: Published')
                        print("pubblicato", item["datavar"])
                    except:
                        newdatavarlist.append(item)
                        self.mqtt_status = _(
                            'Connect Status: ERROR on Publish')

                self.datavarlist = newdatavarlist
            else:
                self.mqtt_status = _(
                    'Connect Status: ERROR on Publish, not connected')

        except:
            self.mqtt_status = _('Connect Status: ERROR on Publish')

    def stopmqtt(self):
        '''
        disconnect from mqtt server
        '''

        try:
            #if self.rmap.connected:
            self.rmap.disconnect()
        except:
            pass

        try:
            self.rmap.loop_stop()
        except:
            pass

        #force status and messages because we do not wait ACK
        self.connected = False
        self.mqtt_status = _('Connect Status: disconnected')

    #@mainthread
    def publishmqtt_loop(self, *args):
        '''
        call this to publish data 
        '''

        if self.rmap.connected:
            print("mqtt connected")
        else:
            #print "mqtt reconnect"
            #try:
            #    self.rmap.mqttc.reconnect()
            #except:
            #    print "error on reconnect"

            print("try to restart mqtt")
            self.startmqtt()

        if self.rmap.connected:
            self.mqtt_status = _('Connect Status: OK connected')
        else:
            self.mqtt_status = _('Connect Status: disconnected')

        self.publishmqtt()

        return True

    def getdata_loop(self, trip=None):
        '''
        This function manage jsonrpc.
        With bluetooth transport manage reconnection
        '''

        if self.transport_name == "bluetooth":
            if self.bluetooth.bluetooth is None:
                print("Bluetooth try to reconnect")
                self.transport = self.bluetooth.connect()
                if self.transport is None:
                    print("bluetooth disabled")
                    return True
                else:
                    try:
                        self.sensorssetup()
                    except:
                        print("sensorssetup failed")

        connected, datavars = self.getdata(trip, self.now)

        if not connected and self.transport_name == "bluetooth":
            self.bluetooth.close()
            self.transport = self.bluetooth.connect()
            try:
                self.sensorssetup()
            except:
                print("sensorssetup failed")

        return datavars

    def loop(self, *args):
        '''
        This function manage jsonrpc and mqtt messages.
        '''
        print("call in loop")

        self.getdata_loop()
        self.publishmqtt_loop()

        return True

    def starttransport(self):
        """
        start transport
        """
        print(">>>>>>> start transport ", self.transport_name)

        if self.transport_name == "bluetooth":
            print("start bluetooth")
            self.bluetooth = androbluetooth(name=self.bluetooth_name,
                                            logfunc=self.log)
            self.transport = self.bluetooth.connect()

        if self.transport_name == "tcpip":
            print("start tcpip")
            self.transport = jsonrpc.TransportTcpIp(addr=(self.tcpip_name,
                                                          1000),
                                                    timeout=3,
                                                    logfunc=self.log)

        if self.transport_name == "serial":
            print("start serial")
            self.transport = jsonrpc.TransportSERIAL(
                port=self.serial_device,
                baudrate=self.serial_baudrate,
                logfunc=self.log)

        if self.transport_name == "mqtt":
            print("start mqtt")
            self.transport = jsonrpc.TransportMQTT(user=self.mqtt_user,
                                                   password=self.mqtt_password,
                                                   host=self.mqtt_host,
                                                   logfunc=self.log)

        if self.transport is None:
            raise Exception("start transport", 1)

    def stoptransport(self):
        """
        stop transport
        """

        try:
            if not self.transport is None:
                self.transport.close()
        except:
            print("ERROR closing transport")
            raise Exception("stop transport", 1)

    def boot(self, configurestation=False):

        print("background boot station")

        ####   osc IPC   ####
        #osc.init()
        self.osc = OSCThreadServer()
        sock = self.osc.listen(port=3000, default=True)
        self.osc.bind(b'/rpc', self.rpcin)

        #force trip for mobile station in background
        self.trip = self.ismobile()

        try:
            self.starttransport()
        except:
            print("start transport failed")

        notok = True
        while notok:
            try:
                if configurestation:
                    self.configurestation()
                try:
                    self.sensorssetup()
                except:
                    print("sensorssetup failed")

                if self.trip:
                    self.gps.start()
                self.startmqtt()
                notok = False
            except:
                print("Error booting station")
                time.sleep(5)

                if self.rpcin_message == b"stop":
                    print("received stop message from rpc")
                    self.on_stop()
                    print("send stopped message to rpc")
                    self.rpcout(b"stopped")
                    raise SystemExit(0)
                    #time.sleep(60) # wait for kill from father

                if self.transport_name == "bluetooth":
                    self.bluetooth.close()
                    self.transport = self.bluetooth.connect()
                    try:
                        self.sensorssetup()
                    except:
                        print("sensorssetup failed")

        print("background end boot")

    def loopforever(self):

        # wait until a "even" datetime and set nexttime
        now = datetime.utcnow()
        print("now:", now)
        nexttime = now + timedelta(seconds=self.mqtt_sampletime)
        nextsec = int(
            nexttime.second / self.mqtt_sampletime) * self.mqtt_sampletime
        nexttime = nexttime.replace(second=nextsec, microsecond=0)
        print("nexttime:", nexttime)
        waitsec = (max((nexttime - datetime.utcnow()),
                       timedelta())).total_seconds()
        print("wait for:", waitsec)
        time.sleep(waitsec)
        print("now:", datetime.utcnow())

        self.now = nexttime

        #self.rmap.loop(timeout=waitsec)

        # TODO
        # enable STOP SERVICE DURING SLEEP

        while True:
            try:

                #override "even" datetime with more precise but "odd" datetime
                #self.now=datetime.utcnow()
                #print "now:",self.now

                self.loop()

                print("backgroud loop")
                message = self.mqtt_status

                title = "Rmap last status"
                if self.transport_name == "bluetooth":
                    title = self.bluetooth.bluetooth_status

                print("notification title:")
                print(title)
                print("notification message:")
                print(message)

                do_notify(message, title)

            except Exception as e:
                print(e)
                print("ERROR in main loop!")
                traceback.print_exc()

            print("now:", datetime.utcnow())
            nexttime = nexttime + timedelta(seconds=self.mqtt_sampletime)

            self.sleep_and_check_stop(nexttime)
            self.now = nexttime

    def sleep_and_check_stop(self, nexttime):
        waitsec = (max((nexttime - datetime.utcnow()),
                       timedelta())).total_seconds()
        print("wait for:", waitsec)
        #time.sleep(waitsec)
        stop = False
        while (datetime.utcnow() < nexttime):
            if self.rpcin_message == b"stop":
                stop = True
                break
            time.sleep(.5)

        if self.rpcin_message == b"stop":
            stop = True

        if (not stop):
            print("continue on loop")
            return

        print("start shutdown background process")
        try:
            self.on_stop()
        except:
            print("error on_stop")
        print("retuned from on_stop")

        print("RPC send stoppped")
        self.rpcout(b"stopped")
        print("background exit")

        try:
            self.osc.stop()
            print("osc stopped")
        except:
            pass

        raise SystemExit(0)
        #time.sleep(s30)

    def exit(self, *args):
        try:
            self.on_stop()
        except:
            print("error on_stop")
        raise SystemExit(0)
Exemplo n.º 14
0
def main():
    global pauser
    global command
    global haiku_bot
    global max_client
    global cam1_socket
    global cam2_socket
    global chatbot

    pauser = False
    command = "pause"
    warnings.filterwarnings("ignore")
    logging.disable(logging.CRITICAL)

    tf_session = tf.Session()
    K.set_session(tf_session)
    bye_list = ['bye', 'see you', 'tada', 'chao']
    

    #Connection Initialization

    print("Creating Connections")
    #Connecting to max
    try:
        max_client = udp_client.SimpleUDPClient(opt.maxip, opt.maxport)
        print("UDP Client connection to Max established")
    except KeyboardInterrupt:
        print("Keyboard Interrupted at UDP connection")
    except:
        print("UDP Client connection to Max failed. Will not be able to send from Max.")
        print('Error: {}. {}, line: {}'.format(sys.exc_info()[0],
                                         sys.exc_info()[1],
                                         sys.exc_info()[2].tb_lineno))


        #################  Connecting to Camera 1  #####################

    if(opt.test==False):
        try:
            cam1_socket = socket.socket()
            cam1_socket.connect((opt.camip1, opt.camport1))
            print("Connection to Camera 1 established")
        except KeyboardInterrupt:
            print("Keyboard Interrupted at Camera 1 connection")
        except:
            print("Unable to connect to Camera 1")
            print('Error: {}. {}, line: {}'.format(sys.exc_info()[0],
                                             sys.exc_info()[1],
                                             sys.exc_info()[2].tb_lineno))
        
        ############### Connecting to Camera 2 #######################

        try:
            cam2_socket = socket.socket()
            cam2_socket.connect((opt.camip2, opt.camport2))
            print("Connection to Camera 2 established")
        except KeyboardInterrupt:
            print("Keyboard Interrupted at Camera 2 connection")
        except:
            print("Unable to connect to Camera 2")
            print('Error: {}. {}, line: {}'.format(sys.exc_info()[0],
                                             sys.exc_info()[1],
                                             sys.exc_info()[2].tb_lineno))
    

    #Max server: Listen to Max
    '''
    try:
        osc = OSCThreadServer()
        try:
            max_server = osc.listen(address=opt.chatip, port=opt.chatport, default=True)
            print("OSC Server initialized to listen to Max")
            osc.bind(b"/chat", chat_callback)
            osc.bind(b"/command", command_callback)
            osc.bind(b"/kill", kill_switch)
            ##########sddrd to test Vishal#########################
            osc.bind(b"/stop", stop_resume_operation)
            ######Vishal#########################
            osc.bind(b"/emotions", get_cached_emotion)

        except:
            print("Tag is not in exceptable format")
        ##########sddrd to test Vishal#########################
    except:
        print("OSC Server initialization failed. Will not be able to listen to Max")
        print('Error: {}. {}, line: {}'.format(sys.exc_info()[0],
                                         sys.exc_info()[1],
                                         sys.exc_info()[2].tb_lineno))'''
    
    #Max server: Listen to Max

    try:
        osc = OSCThreadServer()
        try:
            max_server = osc.listen(address=opt.chatip, port=opt.chatport, default=True)
            print("OSC Server initialized to listen to Max")
            osc.bind(b"/chat", chat_callback)
            osc.bind(b"/command", command_callback)
            osc.bind(b"/kill", kill_switch)
            ##########sddrd to test Vishal#########################
            osc.bind(b"/stop", stop_resume_operation)

            ## kk code for start
            osc.bind(b"/start", start_chat)
            osc.bind(b"/haiku", haiku_generator)
            osc.bind(b"/names", send_names)
            osc.bind(b"/emotions", get_cached_emotion)
            osc.bind(b"/jump", jump_story)
        except Exception as e:
            print(e)
            print("Tag is not in exceptable format")
        ##########sddrd to test Vishal#########################
    except KeyboardInterrupt:
        print("Keyboard Interrupted at OSC Communication")
    except:
        print("OSC Server initialization failed. Will not be able to listen to Max")
        print('Error: {}. {}, line: {}'.format(sys.exc_info()[0],
                                         sys.exc_info()[1],
                                         sys.exc_info()[2].tb_lineno))

    



    ########### Chatbot Generator #####################

    # haiku_intialiser(tf_session)


    try:
        chatbot = Chatbot(tf_session, story_progress=opt.jump)
        univEncoder = UnivEncoder(tf_session, chatbot.intents)
        print("Initialized chatbot dialogue system")
    except KeyboardInterrupt:
        print("Keyboard Interrupted at Chatbot")
        exit()
    except:
        print("Unable to initialize chatbot dialogue system. Exiting.")
        print('Error: {}. {}, line: {}'.format(sys.exc_info()[0],
                                         sys.exc_info()[1],
                                         sys.exc_info()[2].tb_lineno))
        exit()

    #Waiting for max command to start chatbot
    # command = "stopped"
    # while command != "chat":
    #     while max_response.empty():
    #         stopped = True
    #     max_request = max_response.get()
    #     command = max_request['command']
    # if()

    print(time.time())
    print("Starting chatbot for the first time")    
    max_client.send_message(start_tag, "ready")

    
    while command != "kill":

        users = False
        faces_count = 0
        chat_start = True
        returning_user = False
        ask_user_name = True

        while command != "pause" and command != "new" and command !="kill":

            try:
                if ask_user_name:
                    ask_user_name = False
                    try:
                        os.mkdir("users")
                    except:
                        users = True

                    ## Creating a txt file and storing all the sentence ##

                    global file
                    ## kk code to open and close history file start ## 
                    file = open("history.txt", "w+")
                    file.close()
                    ## kk code to open and close history file end ##

                    #TODO: There is no kill switch check or command check. Need to implement it.
                    first_sentence = "Hello! - - - You can text to me with your phone - - - I see a visitor in red, how are you ?"
                    max_client.send_message(chat_tag, first_sentence)
                    history(first_sentence)
                    # time.sleep(chat_speed_slow)

                    while max_response.empty():
                        waiting_for_user = True
                    user_response = max_response.get()
                    history(user_response['intent'])

                    # players_names = ["dummy1", "dummy2", "dummy3", "dummy4", "dummy5"]
                    
                    
                    ## kk code start##
                    if(opt.jump==0):
                        chat = "Thank you! I see five visitors. What is your name, visitor one in red ?" #.format(players)
                        max_client.send_message(chat_tag, chat)
                        while max_response.empty():
                            waiting_for_user = True

                        history(chat)

                        print("players names ", players_names)
                        # try:
                        greetings = ["Hi ", "Welcome ", "Hello ", "Its my pleasure ", "Hello "]
                        user_colors = [". visitor two in green ?", ". visitor three in blue ?", ". visitor four in orange ?", ". visitor five in yellow ?", ""]
                        i = 0
                        while max_response.empty():
                            waiting_for_user = True

                        for __ in range(players):
                            user_response = max_response.get()
                            name = user_response['intent']
                            history(name)
                            players_names.append(name)

                            chat = greetings[i] + name.strip('""') + ". - - - " + user_colors[i]
                            max_client.send_message(chat_tag, chat)
                            history(chat)
                            i+=1
                        # except:
                        #     print("ran into exception ")
                        
                        print("players names ", players_names)

                        
                        # user_response = max_response.get()
                        #TODO: We might need to validate the cities
                        # user_location = user_response['intent']
                        # history(user_location)

                        ############## Sending name ##################
                        names = str(players_names).strip('""')
                        names = names.strip('[]')
                        names = names.replace('""', " ")
                        names = names.replace("\'", " ")
                        names = names.replace(",", " ")
                        names = names.replace('"', " ")
                        # print(names)
                        max_client.send_message(names_tag, names)



                        #Monologue
                        chat = "Nice to meet you all.  - - - I lihve here. - - - Here on stage. I can’t leave the stage."

                        max_client.send_message(chat_tag, chat)
                        time.sleep(chat_speed)

                        pauser = True
                        print("value of pauser", pauser)
                        max_client.send_message(stop_tag, 'PAUSE')

                    
                    # while pauser:
                    #     waiting_for_user = True

                    # chat = 'Where do you lihve, {0} ? - - - Do you live on stage like me - - - or do you lihve in a house, {0} ?'.format(players_names[3].strip('""'))
                    # max_client.send_message(chat_tag, chat)
                    # history(chat)

                    ## commenting code end

                try:
                    while max_response.empty():
                        waiting_for_user = True
                    
                    user_response = max_response.get()
                    history(user_response['intent'])
                    user_chat = user_response['intent']
                    print("user input", user_chat)
                    if(user_chat.strip('""') in bye_list):
                        chat = 'It was nice talking to you.'
                        max_client.send_message(chat_tag, chat)
                        history(chat)
                        command = 'kill'
                        file.close()
                        break
                    intent = univEncoder.match_intent(user_chat,chatbot.story_progress)
                    print("intent name", intent)
                    # print("weight", chatbot.progress)
                    # print(chatbot.intents[intent].dynamic)
                    

                    if(intent == 'no_matching_intent'):
                        chat = "hmm - - I dont understand {0} , can you please say something different".format(user_chat)
                        # chat = univEncoder.chat_eliza(user_chat)
                        # print("Response from Eliza \n {0}".format(chat))
                        max_client.send_message(chat_tag, chat)
                        history(chat)


                    ##### Haiku poems, Disabling for now end - kkk #####


                    elif "transition" in intent:
                        print("inside change transition logic")
                        store_progress = chatbot.story_progress
                        print("progress value", store_progress)
                        # story_transition()
                        next_story = intent.split("_")[2]
                        print("next story", next_story)
                        chatbot.change_story(next_story)
                        univEncoder.set_intent(chatbot.intents)
                        l = len(chatbot.intents[intent].responses)
                        if l > 0:
                            chatbot.story_progress = chatbot.intents[intent].weight
                            chat = chatbot.intents[intent].responses[randrange(l)]
                            chat = check_response(chat)
                            max_client.send_message(chat_tag, chat)
                            history(chat)


                    elif "main" in intent:
                        print("inside change main logic")
                        chatbot.change_story("see_me", store_progress+6)
                        # print(chatbot.intents)
                        univEncoder.set_intent(chatbot.intents)
                        l = len(chatbot.intents[intent].responses)
                        if l > 0:
                            chatbot.story_progress = chatbot.intents[intent].weight
                            chat = chatbot.intents[intent].responses[randrange(l)]
                            chat = check_response(chat)
                            max_client.send_message(chat_tag, chat)
                            history(chat)


                    elif "happiness" in intent:
                        print("inside last intent logic")
                        # chatbot.change_story("see_me", store_progress+6)
                        # print(chatbot.intents)
                        # univEncoder.set_intent(chatbot.intents)
                        l = len(chatbot.intents[intent].responses)
                        if l > 0:
                            chatbot.story_progress = chatbot.intents[intent].weight
                            chat = chatbot.intents[intent].responses[randrange(l)]
                            chat = check_response(chat)
                            max_client.send_message(chat_tag, chat)
                            max_client.send_message(stop_tag, "END")
                            history(chat)
                            command = "kill"



                    else:
                        l = len(chatbot.intents[intent].responses)
                        if l > 0:
                            if(intent=="learning"):
                                print("inside color intent")
                                user_color = user_response['intent']
                                print(user_color)
                                try:
                                    user_rgb = [round(ele, 3) for ele in Color(user_color.strip('"')).rgb]
                                    # user_rgb = [int(i)*255 for i in user_rgb]
                                    user_rgb = str(user_rgb).strip('[]')
                                    user_rgb = user_rgb.replace(",", "")
                                    # print(user_rgb.rgb)
                                    print(user_rgb)
                                    history(str(user_rgb))
                                    history(chat)

                                    chatbot.story_progress = chatbot.intents[intent].weight
                                    chat = chatbot.intents[intent].responses[randrange(l)]
                                    chat = check_response(chat)
                                    max_client.send_message(chat_tag, chat)
                                    
                                    
                                
                                    max_client.send_message(rgb_tag, str(user_rgb))
                                except:
                                    max_client.send_message(chat_tag, "I dont understand this color, please try something else")
                            else:
                                chatbot.story_progress = chatbot.intents[intent].weight
                                chat = chatbot.intents[intent].responses[randrange(l)]
                                chat = check_response(chat)
                                max_client.send_message(chat_tag, chat)
                                history(chat)


                except KeyboardInterrupt:
                    print("Closing all active connections")
                    command = "kill"

                except:
                    print('Error: {}. {}, line: {}'.format(sys.exc_info()[0],
                                             sys.exc_info()[1],
                                             sys.exc_info()[2].tb_lineno))
                    chat = "hmm, I dont know what {0} is, can you please say something different".format(user_response['intent'])
                    max_client.send_message(chat_tag, chat)
                    history(chat)


                ##### Haiku poems, Disabling for now start - kkk #####
                # poem = haiku_bot.generate_haiku([3, 5, 3], temperature=.3, first_char='cold')
                # poem = haiku_bot.generate_haiku([3, 5, 3], temperature=.3, first_char='cold')
                # line1 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[0])
                # line2 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[1])
                # line3 = re.sub(r"[^a-zA-Z0-9]+", ' ', poem[2])
                # chat = line1 + line2 + line3
                # max_client.send_message(chat_tag, chat)
                ##### Haiku poems, Disabling for now end - kkk #####

            except KeyboardInterrupt:
                print("Closing all active connections")
                command = "kill"

                  
    try:
        try:
            cam1_socket.send(camera_message.encode('utf-8'))
            cam1_socket.close()
            print("Connection to Camera 1 closed")
        except:
            print("unable to close camera 1")

        try:
            cam2_socket.send(camera_message.encode('utf-8'))
            cam2_socket.close()
            print("Connection to Camera 2 closed")
        except:
            print("unable to close camera 2")

        try: 
            osc.stop()
            print("Connection to Max closed")
        except:
            print("unable to close connectoin to Max")

    except:
        print('unable to close connections')
Exemplo n.º 15
0
def main(ip_address, port, path):
    osc = OSCThreadServer(encoding="utf8")
    osc.listen(address=ip_address, port=int(port), default=True)
    osc.bind(path, onReceiveMessageCallback)
    sleep(1000)
    osc.stop()
Exemplo n.º 16
0
class OSC_OT_OSCPyServer(OSC_OT_OSCServer):
    bl_idname = "nodeosc.oscpy_operator"
    bl_label = "OSCMainThread"

    _timer = None
    count = 0

    #####################################
    # CUSTOMIZEABLE FUNCTIONS:

    inputServer = "" #for the receiving socket
    outputServer = "" #for the sending socket
            
    # setup the sending server
    def setupInputServer(self, context, envars):
        self.dispatcher = dispatcher.Dispatcher()   
 
    # setup the receiving server
    def setupOutputServer(self, context, envars):
        #For sending
        self.outputServer = OSCClient(envars.udp_out, envars.port_out)
        self.outputServer.send_message(b'/NodeOSC', [b'Python server started up'])     
        print("OSCPy Server sended test message to " + envars.udp_out + " on port " + str(envars.port_out))

    def sendingOSC(self, context, event):

        oscMessage = {}
        
        # gather all the ouput bound osc messages
        make_osc_messages(bpy.context.scene.NodeOSC_outputs, oscMessage)
         
        # and send them 
        for key, args in oscMessage.items():
            values = []
            if isinstance(args, (tuple, list)):
                for argum in args:
                    if type(argum) == str:
                        argum = bytes(argum, encoding='utf-8')
                    values.append(argum)
            else:
                if type(args) == str:
                    args = bytes(args, encoding='utf-8')
                values.append(args)
            self.outputServer.send_message(bytes(key, encoding='utf-8'), values)
  
    # add method 
    def addMethod(self, address, data):
        pass #already set during creation of inputserver
 
    # add default method 
    def addDefaultMethod(self):
        pass #already set during creation of inputserver
    
    # start receiving 
    def startupInputServer(self, context, envars):
        print("Create OscPy Thread...")
        # creating a blocking UDP Server
        #   Each message will be handled sequentially on the same thread.
        self.inputServer = OSCThreadServer(encoding='utf8', default_handler=OSC_callback_oscpy)
        sock = self.inputServer.listen(address=envars.udp_in, port=envars.port_in, default=True)
        print("... server started on ", envars.port_in)

    # stop receiving
    def shutDownInputServer(self, context, envars):
        print("OSCPy Server is shutting down...")
        self.inputServer.stop()                 # Stop default socket
        print("  stopping all sockets...")
        self.inputServer.stop_all()             # Stop all sockets
        print("  terminating server...")
        self.inputServer.terminate_server()     # Request the handler thread to stop looping
        self.inputServer.join_server()          # Wait for the handler thread to finish pending tasks and exit
        print("... OSCPy Server is shutdown")
Exemplo n.º 17
0
        euler_y = args[19]
        euler_z = args[20]
        euler_bonus = args[21]

        euler_x = args[20]
        euler_x = round(euler_x)
        if euler_x < 0:
            euler_x = 360 + euler_x
        angle = euler_x - list[0]
        angle2 = math.atan2(2*(quat_x*quat_w + quat_y*quat_z), 1-2*(quat_z**2+quat_w**2))
        print(euler_x)
        list[0] = euler_x

    print("testing OSC in python")
    osc = OSCThreadServer()

    try :
        sock = osc.listen(address='0.0.0.0', port=8000, default=True)
        osc.bind(b'/0/raw', OSCcallback)
        sleep(25)
        osc.stop(sock)
        print('stopped OSC')

    except KeyboardInterrupt:
        osc.stop_all()
        print('stopped all for keyboard interupt')