def handleCommandBeginLesson(server, json_obj):
    """ 开启课堂(开始上课)

    :param server: socket服务端
    :param json_obj:|- command
                    |- account_type
                    |- timestamp
                    |- course_id
                    |- lesson_id
                    |- uid
                    |- username
    :return:
    """
    course_status = RedisForCourseStatus().getCourseStatus(
        course_id=json_obj['course_id'])
    if course_status == CourseStatus.Waiting:
        # 更新课程状态:waiting -> online
        lesson = LessonService().beginLesson(lesson_id=json_obj['lesson_id'])
        RedisForCourseStatus().onLine(course_id=json_obj['course_id'])
        # 给所有课堂中的人发送信息
        send_data = {
            'command': TransportCmd.BeginLesson,
            'course_id': str(lesson.course.id),
            'lesson_id': str(lesson.id),
            'begin_timestamp': lesson.begin_timestamp
        }
        connection_list = findConnectionByLessonId(
            connection_pool=server.lesson_connection_pool,
            lesson_id=str(lesson.id))
        send(connection=connection_list, data=send_data)
def raiseHand(server, json_obj):
    lesson_id = json_obj["lesson_id"]
    student_id = json_obj["uid"]

    speech_status = getSpeechStatus(
        connection_pool=server.lesson_connection_pool, uid=student_id)
    if speech_status == SpeechStatus.InSpeech:
        json_obj["raise_hand_err"] = RaiseHandError.InSpeechError
    elif speech_status == SpeechStatus.Applying:
        json_obj["raise_hand_err"] = RaiseHandError.ApplyingError
    else:
        json_obj["raise_hand_err"] = RaiseHandError.NoError
        # 修改服务器内存中的状态
        setSpeechStatus(connection_pool=server.lesson_connection_pool,
                        uid=student_id,
                        speech_status=SpeechStatus.Applying)
        # 转发给老师
        connection = findTeacherConnectionInLesson(
            connection_pool=server.lesson_connection_pool, lesson_id=lesson_id)
        send(connection_pool=server.paint_connection_pool,
             connection=connection,
             data=json_obj)

    # 回复请求
    reply(server.request, json_obj)
def removeMemberFromInSpeech(server, json_obj):
    lesson_id = json_obj["lesson_id"]
    student_id = json_obj["student_id"]

    # 修改服务器内存中的状态
    setSpeechStatus(connection_pool=server.lesson_connection_pool,
                    uid=student_id,
                    speech_status=SpeechStatus.SpeechFree)
    # 给所有课堂中的人发送信息
    connection_list = findConnectionByLessonId(
        connection_pool=server.lesson_connection_pool, lesson_id=lesson_id)
    send(connection_pool=server.paint_connection_pool,
         connection=connection_list,
         data=json_obj)
def handleCommandStudentCameraFrameData(server, json_obj):
    """ 把用户的帧数据转发到分析服务器

    :param server: socket服务端
    :param json_obj:|- command
                    |- timestamp
                    |- course_id
                    |- lesson_id
                    |- uid
                    |- username
                    |- frame_mat
                    |- concentration_timestamp
    :return:
    """
    send(connection=server.cv_server_connection, data=json_obj)
def chatBan(server, json_obj):
    lesson_id = json_obj["lesson_id"]

    # 刷新
    for lesson in server.lessons:
        if lesson["lesson_id"] == lesson_id:
            if lesson["chat_status"] == ChatStatus.Baned:
                lesson["chat_status"] = ChatStatus.Free
            else:
                lesson["chat_status"] = ChatStatus.Baned

    # 转发给所有课堂中的人
    connection_list = findConnectionByLessonId(
        connection_pool=server.lesson_connection_pool, lesson_id=lesson_id)
    send(connection_pool=server.paint_connection_pool,
         connection=connection_list,
         data=json_obj)
def handleCommandConcentrationFinalData(server, json_obj):
    """ 把接收到的分析数据返回给用户

    :param server: socket服务端
    :param json_obj:|- command
                    |- is_succeed
                    |- course_id
                    |- lesson_id
                    |- uid
                    |- concentration_value
                    |- emotion
                    |- concentration_timestamp
    :return:
    """

    # 转发给相关的用户
    connection = findConnectionByUid(
        connection_pool=server.lesson_connection_pool, uid=json_obj["uid"])
    send(connection=connection, data=json_obj)
def paintCommand(server, json_obj):
    course_id = json_obj["course_id"]
    lesson_id = json_obj["lesson_id"]
    uid = json_obj["uid"]
    lesson = findLessonByLessonId(server.lessons, lesson_id)

    speech_status = getSpeechStatus(
        connection_pool=server.lesson_connection_pool, uid=uid)
    if speech_status == SpeechStatus.InSpeech:
        # 保存
        lesson["paint_command_pool"].append(json_obj)
        # 转发给相关的用户(除自己外)
        connection_list = findOtherConnectionInLesson(
            connection_pool=server.paint_connection_pool,
            lesson_id=lesson_id,
            my_uid=uid)
        send(connection_pool=server.paint_connection_pool,
             connection=connection_list,
             data=json_obj)
        print(connection_list)
def resultOfRaiseHand(server, json_obj):
    lesson_id = json_obj["lesson_id"]
    student_id = json_obj["student_id"]
    application_status = json_obj["application_status"]

    # 修改服务器内存中的状态
    if application_status == ApplicationStatus.Accepted:
        setSpeechStatus(connection_pool=server.lesson_connection_pool,
                        uid=student_id,
                        speech_status=SpeechStatus.InSpeech)
    elif application_status == ApplicationStatus.Refused:
        setSpeechStatus(connection_pool=server.lesson_connection_pool,
                        uid=student_id,
                        speech_status=SpeechStatus.SpeechFree)

    # 转发给所有课堂中的人
    connection_list = findConnectionByLessonId(
        connection_pool=server.lesson_connection_pool, lesson_id=lesson_id)
    send(connection_pool=server.paint_connection_pool,
         connection=connection_list,
         data=json_obj)
def handleCommandEndLesson(server, json_obj):
    """ 结束课堂

    :param server: socket服务端
    :param json_obj:|- command
                    |- account_type
                    |- timestamp
                    |- course_id
                    |- lesson_id
                    |- uid
                    |- username
    :return:
    """
    # 更新课程状态:online/waiting/cantJoinIn -> offline
    lesson = LessonService().endLesson(lesson_id=json_obj['lesson_id'])
    RedisForCourseStatus().offLine(course_id=json_obj['course_id'])
    RedisForUserStatus().free(uid=json_obj['uid'])
    # 清除redis缓存
    RedisForLessonStatus().endLesson(lesson_id=lesson.id)
    uid_arr = RedisForInLesson().getLessonUsersUID(lesson_id=lesson.id)
    redis_for_in_lesson = RedisForInLesson()
    redis_for_user_status = RedisForUserStatus()
    for uid in uid_arr:
        redis_for_in_lesson.quitLesson(uid=uid)
        redis_for_user_status.free(uid=uid)

    # 给所有课堂中的人发送信息
    send_data = {
        'command': TransportCmd.EndLesson,
        'course_id': str(lesson.course.id),
        'lesson_id': str(lesson.id),
        'end_timestamp': lesson.end_timestamp
    }
    connection_list = findConnectionByLessonId(
        connection_pool=server.lesson_connection_pool,
        lesson_id=str(lesson.id))
    send(connection=connection_list, data=send_data)
    # 还需要给分析服务器发送
    send(connection=server.cv_server_connection, data=send_data)