Exemple #1
0
def put_notification(site_id, text):
    data = {}
    data['siteId'] = site_id
    data['init'] = {}
    data['init']['type'] = 'notification'
    data['init']['text'] = text
    json_data = json.dumps(data)
    mqtt_client.put('hermes/dialogueManager/startSession', str(json_data))
def start_session(hermes, intent_message):
    session_id = intent_message.session_id
    intent_msg_name = intent_message.intent.intent_name
    #pprint(intent_msg_name)
    #pprint(INTENT_FILTER_START_SESSION)
    if intent_msg_name not in INTENT_FILTER_START_SESSION:
        return

    print("Starting device control session " + session_id)
    intent_slots = get_intent_slots(intent_message)
    locations = get_locations(intent_message)
    session_state = {
        "siteId": get_intent_site_id(intent_message),
        "topic": get_intent_msg(intent_message),
        "slot": intent_slots,
        "location": locations
    }

    # device = intent_message.slots.device.first()
    if len(intent_slots) == 0:
        question = get_intent_question(
            session_state.get("topic").split(':')[-1])
        #pprint(question)
        if question == "":
            hermes.publish_end_session(session_id,
                                       "Przepraszam, nie zrozumiałem")
        save_session_state(SessionsStates, session_id, session_state)
        hermes.publish_continue_session(session_id, question,
                                        INTENT_FILTER_GET_ANSWER)
    else:
        session_state["slot"] = intent_slots
        session_state["location"] = locations
        site_id = str(session_state.get("siteId"))
        topic = str(session_state.get("topic"))
        payloads = session_state.get("slot")
        #pprint(payloads)
        payload_suffix = ""
        if len(locations) >= 1:
            payload_suffix = "/" + str(session_state.get("location")[0])
        for payload in payloads:
            payload = payload + payload_suffix
            mqtt_client.put(site_id + "/" + topic, payload)
            mqtt_client.put(topic + "/" + site_id, payload)
        hermes.publish_end_session(session_id, None)
def user_gives_answer(hermes, intent_message):
    print("User is giving an answer")
    session_id = intent_message.session_id
    #print(session_id)
    session_state = SessionsStates.get(session_id)
    session_state, sentence, continues = check_user_answer(
        session_state, intent_message)

    if session_state is None:
        session_state = {
            "siteId": get_intent_site_id(intent_message),
            "topic": get_intent_msg(intent_message),
            "slot": get_intent_slots(intent_message)
        }

    if not continues:
        site_id = str(session_state.get("siteId"))
        topic = str(session_state.get("topic"))
        payloads = session_state.get("slot")
        if len(payloads) == 0:
            hermes.publish_end_session(session_id,
                                       "Przepraszam, nie zrozumiałem")
        locations = get_locations(intent_message)
        payload_suffix = ""
        if len(locations) >= 1:
            payload_suffix = "/" + str(locations[0])
        for payload in payloads:
            payload = payload + payload_suffix
            mqtt_client.put(site_id + "/" + topic, payload)
            mqtt_client.put(topic + "/" + site_id, payload)
        remove_session_state(SessionsStates, session_id)
        hermes.publish_end_session(session_id, None)
        return

    hermes.publish_continue_session(session_id, sentence,
                                    INTENT_FILTER_GET_ANSWER)
def start_session(hermes, intent_message):
    print("Timer session_start")
    session_id = intent_message.session_id
    site_id = get_intent_site_id(intent_message)
    locations = st.get_locations(intent_message)
    if len(locations) >= 1:
        site_id = locations[0]
    target = ''
    targets = st.get_targets(intent_message)
    #pprint(targets)

    if len(targets) > 0:
        target = targets[0]
    intent_msg_name = get_intent_msg(intent_message)

    print("Starting device control session " + session_id)

    intent_slots = st.get_intent_slots(intent_message)
    time_units = st.get_time_units(intent_message)
    hours = st.get_hours(intent_message)
    if len(hours) > 0:
        alarm_time_str = datetime.datetime.today().strftime(
            '%Y-%m-%d ') + st.fix_time(hours[0])
        alarm_datetime = datetime.datetime.strptime(alarm_time_str,
                                                    "%Y-%m-%d %H:%M")
        if datetime.datetime.timestamp(
                alarm_datetime) < datetime.datetime.timestamp(
                    datetime.datetime.now()):
            next_date = alarm_datetime + datetime.timedelta(days=1)
            hour = next_date.strftime("%Y-%m-%d %H:%M")
            hour_only = next_date.strftime("%H:%M")
        else:
            hour = alarm_datetime.strftime("%Y-%m-%d %H:%M")
            hour_only = alarm_datetime.strftime("%H:%M")
    else:
        hour = ''
        hour_only = ''

    if len(intent_slots) < len(time_units):
        intent_slots.insert(0, 1)
    # Get seconds amount
    total_amount = 0
    for key, value in enumerate(intent_slots):
        try:
            amount = float(st.get_intent_amount(value))
        except ValueError:
            print("Error: That's not an float!")
            hermes.publish_end_session(session_id,
                                       "Przepraszam, nie zrozumiałem")
            return
        total_amount = amount * st.get_unit_multiplier(
            time_units[key]) + total_amount

    if intent_msg_name == 'countdown':
        hermes.publish_end_session(session_id, None)

        end_time = int((time.time() * 1000) + (total_amount * 1000))

        # Add new timer
        st.add_timer(site_id, total_amount, end_time, target)

        # Call timer
        st.call_timer(site_id, total_amount, end_time, target)

        # Say
        amount_say = st.get_amount_say(total_amount)
        say = ['Rozpoczynam odliczanie', 'Czas start!', 'Odliczam', 'Robi się']
        amount_say.append(random.choice(say))
        for text in amount_say:
            sc.put_notification(site_id, text)
            time.sleep(0.2)

    if intent_msg_name == 'countdown_interrupt' or intent_msg_name == 'countdown_left':
        hermes.publish_end_session(session_id, None)
        if len(intent_slots) == 0 or len(time_units) == 0:
            mqtt_client.put('timer/' + intent_msg_name + '/' + site_id, 0)
        else:
            mqtt_client.put('timer/' + intent_msg_name + '/' + site_id,
                            int(total_amount))

    if intent_msg_name == 'alarm' and len(hours) > 0:
        hermes.publish_end_session(session_id, None)
        st.add_alarm(site_id, hour, target)
        st.call_alarm(site_id, hour, target)
        say = [
            'OK, godzina', 'Jasne, godzina', 'Planuję alarm, godzina',
            'Dobrze, godzina'
        ]
        alarm_say = random.choice(say)
        alarm_say = alarm_say + " " + hour_only
        sc.put_notification(site_id, alarm_say)

    if intent_msg_name == 'alarm_interrupt':
        mqtt_client.put('timer/' + intent_msg_name + '/' + site_id, hour)