Smart toys WebSocket communication

Smart Toy Project


First, the needs analysis
a nice shape
2 features: real-time voice communication 1 of
2 a positive content
- audio playback (play parents selected songs, stories, Encyclopedia, English)
- Speech recognition (voice recognition content by the name of the toy can take the initiative on demand content)
3 chat with the kids
4 to answer questions (Why do thousands)
5 parents View circle by toy

 

Second, the specific embodiment of
the first day of:
acquiring audio assets
1 preschool capture content from the Himalayas, stored in the file
are acquired cover / music file
to write data to the database

app acquire the content / CONTENT_LIST
- information is returned as a list of

interface function implemented
in accordance with the front end demand returns the corresponding data
App / get_cover acquiring content image info
App / get_music obtain audio information


2 user registration, login, automatic logon
App / reg registered users
user information App / login user login returned
automatically after App / auto_login user opens the APP returns the login user information

3 creating apparatus DeviceKey (FIG linked network created two-dimensional code)
to ensure the uniqueness of
-hashlib.md5 ((f "{uuid4 ( )} {time.time ()} {uuid4 ()}". encode ( "utf8")). hexdigest ()

The next day:
App bind a two-dimensional code scanning device, the device must have a two-dimensional code in line with our database of
equipment and once bound to create toys from the App App

1 scan code
via {device_key: ""} to database find out if there is the two-dimensional code information

1. the two-dimensional code scanning device is not successful and bound
2-dimensional code scanning failures, bar code scanning equipment is not present in the library

3. the two-dimensional code scanning success, but the equipment has been binding
(not implemented)

Toys 2 binding
construct basic information database according to the requirements of the toy

to create an empty table Chats
-chat_id = MDB.Chats.insert_one ({ "user_list": [], "chat_list": []})


. 1 Creating Toy (binding database structure and API documentation for information)
toy bind the user
give the first toy to add a buddy app

note friend_chat wording
- "friend_chat": str (chat_id.inserted_id )


added
-toy_info [ "FRIEND_LIST"] the append (toy_add_user).
toy_id MDB.Toys.insert_one = (toy_info)

2.User table data supplement
app while also increasing the toy as a friend

user_add_toy = {
"friend_id": toy_id.inserted_id, # toy_id str
"friend_nick": toy_info.get ( "baby_name"), baby_name #
"friend_remark": toy_info.get ( "toy_name"), # toy_name
"friend_avatar": "Toy.jpg", #
"friend_chat": str(chat_id.inserted_id), # chat_id
"friend_type": "toy" # friend type Toy
}

added to the list structure
USER_INFO [ "bind_toys"] the append (STR (toy_id.inserted_id)).
USER_INFO [ "FRIEND_LIST"] the append (user_add_toy).

entirety modified data
# Review Users all data
MDB.Users.update_one ({ "_ id": ObjectId (user_id)}, { "$ set": user_info})

# Chats user_list data will change and the toy_id user_id added
MDB.Chats.update_one ({ "_ id": chat_id.inserted_id}, { "$ set": { "user_list": [user_id, str (toy_id.inserted_id)]} })


data structure

3 toy_list shows
lookup table corresponding data from toys, returned (refer to data table)

 

Day:
1 smart toys WebSocket communication
Websocket connection OpenToy boot
in the format open wevbsocket

solve the problem of cross-domain
App newsletter send music to the Toy
ws: //10.0.0.1: 9528 / App / <user_id>
Toys newsletter to receive App Push Music
ws: //10.0.0.1:9528/toy/ <toy_id>

1 / get_qr interface to obtain QRCODE picture
- take a picture / music / pictures principles of two-dimensional code is the same
2 / friend_list query buddy list

3 / chat_list inquiry chat

4 / app_uploader Upload app voice message

5 / toy_uploader toy to upload the message


Note:
the front end of the transmission request data carried by the rear end of the response data and

app / toy upload note file format conversion voice messages
to solve cross-domain problems (installation package)

to obtain data - save the file - conversion format - chats - Update chats - return RET

 

/ Get_qr Interface

# Returns the two-dimensional code information Pictures 
@ content_bp.route ( " / get_qr / <filename> " , Methods = [ " GET " ]) 
DEF get_qr (filename): 
    qr_path = os.path.join (QRCODE_PATH, filename)
     return the send_file ( qr_path)

 / Friend_list Interface / chat_list Interface

from bson import ObjectId
from flask import Blueprint, request, jsonify

from settings import MDB, RET

friend_bp = Blueprint("friend_bp", __name__)


@friend_bp.route("/friend_list", methods=["POST"])
def friend_list():
    user_id = request.form.get("_id")
    user_info = MDB.Users.find_one({"_id": ObjectId(user_id)})

    RET["CODE"] = 0
    RET["MSG"] = "好友查询"
    RET["DATA"] = user_info.get("friend_list")

    print(user_info, "+++++++++")
    return jsonify(RET)


@friend_bp.route("/chat_list", methods=["POST"])
def chat_list():
    chat = request.form.to_dict()
    chat_window = MDB.Chats.find_one({"_id": ObjectId(chat.get("chat_id"))})

    RET["CODE"] = 0
    RET["MSG"] = "查询聊天记录"
    RET["DATA"] = chat_window.get("chat_list")

    return jsonify(RET)

Implement / app_uploader Interface App upload voice messages
realization / toy_uploader interfaces Toy upload voice messages

 

import os
import time

from bson import ObjectId
from flask import Blueprint, request, jsonify

from settings import MDB, RET, CHAT_PATH

uploader_bp = Blueprint("uploader_bp", __name__)


@uploader_bp.route("/app_uploader", methods=["POST"])
def app_uploader():
    to_user = request.form.get("to_user")
    from_user = request.form.get("user_id") 
    Reco_file = request.FILES. GET ( " reco_file " ) 

    reco_file_path = os.path.join (CHAT_PATH, reco_file.filename) 
    # save file format for the AMR 
    reco_file.save (reco_file_path) 
    # conversion format 
    os.system (f " ffmpeg reco_file_path reco_file_path {} {-i} .mp3 " ) 

    user_list = [to_user, from_user] 
    # chat information recording 
    chat_info = {
         " from_user " : from_user,
         " to_user " : to_user,
         "chat": f"{reco_file.filename}.mp3",
        "createTime": time.time()
    }
    # 更新聊天数据
    MDB.Chats.update_one({"user_list": {"$all": user_list}}, {"$push": {"chat_list": chat_info}})

    RET["CODE"] = 0
    RET["MSG"] = "上传成功"
    RET["DATA"] = {
        "filename": f"{reco_file.filename}.mp3",
        "friend_type": "app"
    }
    return jsonify(RET)


from uuid import uuid4


@uploader_bp.route("/toy_uploader", methods=["POST"])
def toy_uploader():
    to_user = request.form.get("to_user")
    from_user = request.form.get( " User_id " ) 
    reco_file = request.FILES. GET ( " Reco " ) 
     # app_uploader with a different file name is the file type blob reco_file.filename 
    # Print (reco_file.filename, " ++++++ " ) 

    filename = f " uuid4 {()}. WAV " 
    reco_file_path = the os.path.join (CHAT_PATH, filename) 

    reco_file.save (reco_file_path) 

    user_list = [to_user, from_user] 
    # chat information recording 
    chat_info = {
         " from_user": from_user,
        "to_user": to_user,
        "chat": filename,
        "createTime": time.time()
    }
    # 更新聊天数据
    MDB.Chats.update_one({"user_list": {"$all": user_list}}, {"$push": {"chat_list": chat_info}})

    RET["CODE"] = 0
    RET["MSG"Upload successful"] ="
    RET["DATA"] = {
        "filename": filename,
        "friend_type": "toy"
    }
    return jsonify(RET)

 

Guess you like

Origin www.cnblogs.com/XLHIT/p/11234397.html