From: snickersun Date: Wed, 20 May 2026 06:51:28 +0000 (+0800) Subject: 把ai_VC.py拆分成独立功能模块。 X-Git-Url: http://git.snicker.com.cn/css/static/script.js?a=commitdiff_plain;h=4b29e633bbc9f68d40e08274160ee487f2eeb0a9;p=mirrors%2FASR1603_G5.git 把ai_VC.py拆分成独立功能模块。 --- diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/Makefile b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/Makefile index fc5a2fa6..295ff723 100644 --- a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/Makefile +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/Makefile @@ -968,6 +968,17 @@ frozen_mpy_list = $(BUILD)/_boot.mpy \ $(BUILD)/versions.mpy \ $(BUILD)/version_util.mpy \ $(BUILD)/motor.mpy \ + $(BUILD)/vc_common.mpy \ + $(BUILD)/vc_utils.mpy \ + $(BUILD)/vc_led.mpy \ + $(BUILD)/vc_hw.mpy \ + $(BUILD)/vc_uart.mpy \ + $(BUILD)/vc_buttons.mpy \ + $(BUILD)/vc_storage.mpy \ + $(BUILD)/vc_auth.mpy \ + $(BUILD)/vc_ota.mpy \ + $(BUILD)/vc_fsm.mpy \ + $(BUILD)/vc_session.mpy \ $(BUILD)/ai_VC.mpy \ $(BUILD)/ai_alarm.mpy \ $(BUILD)/voice_record.mpy \ diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/ai_VC.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/ai_VC.py index 84012050..d1cfe0fe 100644 --- a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/ai_VC.py +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/ai_VC.py @@ -1,9487 +1,50 @@ -from common import Abstract, LogAdapter -try: - from qp_log import qp_print as print -except Exception: - pass -import EventMesh -import uwebsocket -import usys -import gc -import urandom -import uhashlib -import ubinascii -import ujson -import osTimer -import _thread -from queue import Queue -import utime -import audio -import uos -from machine import Pin -from machine import ExtInt -from machine import WDT -import modem -import sim -import net -import base64 -import ucryptolib -import urandom as random -import request -import ql_fs -from misc import Power,USB,PowerKey -import ntptime -import machine -import dataCall -import constants -import versions -import version_util -from machine import UART -try: - import vc_config -except Exception as e: - usys.print_exception(e) -try: - import vc_text_intent -except Exception: - vc_text_intent = None -try: - import VAD -except Exception as e: - usys.print_exception(e) -try: - import usr.ai_alarm as _ai_alarm -except Exception as e: - usys.print_exception(e) - import ai_alarm as _ai_alarm -PRESS_AUDIO_MODE = 0 #是按键音 0 是使用音频文件 -# dev_type = "G10" -#dev_type = "7PIN_BK"#小瑞 -#dev_type = "7PIN_BK_coze"#k扣子小瑞 -#dev_type = "7PIN_BK_EU"#英文小瑞 -#dev_type = "7PIN_coze"#柒牌扣子 -dev_type = "7PIN"#柒牌(默认值,实际优先从 usr/default_config.py 覆盖) -# dev_type = "7PIN_1_5"#柒牌1.5 -#dev_type = 'momo'#食梦貘 -#dev_type = 'sjgq'#四季甘泉 -#dev_type = 'szbst'#深圳百事通 -#dev_type = 'jswm'#金三完美 -#dev_type = 'jswm_BOX'#金三完美柒牌盒子 -#dev_type = 'csh'#慈善会 - -# 统一从配置加载 DEV_TYPE(usr 覆盖优先) -try: - if "vc_config" in globals() and vc_config: - dev_type = vc_config.get_dev_type(dev_type) -except Exception: - pass - -# 1=打印 VC 周期性保活专用行「VC: ping ping」;默认 0(WS send 明细里已跳过 ping,无需再开此项) -VC_HEARTBEAT_LOG = 0 -# 1=打印音频帧级日志:Record_len / send_data len / 下发音频长度;默认 0(只看 uplink START/END) -# VC_AUDIO_FRAME_LOG = 0 -# 1=启用我们新增的协议层“严格门控/缓冲/兜底”行为;0=临时排除这些改动(用于定位协议影响) -# 受控项(与对端 V2.5+ 能力对齐;关=回退旧端行为): -# - 上行:收到 started 前缓冲/丢弃 PCM;PTT 模式下 audio.create 前禁发 PCM -# - 兜底:llm.completed 后若久无 tts.started,端侧可重启会话(不做 force_tts) -# 注意:audio.played 不受此开关控制——金三 V2.5 服务端依赖端侧播完后上报才能进入下一轮 ASR; -# 若误关会导致问候结束后首轮用户说话无 vad/asr。 -VC_PROTO_GUARDS = 0 -# 1=打印 VC WebSocket 每条收发的完整文本(流量大、易刷屏);0 仅一行 event 摘要(llm.delta 省略) -VC_WS_IO_LOG = 0 -# 长对话「无业务下行」idle:下列 JSON event/type 及裸文本 ping/pong 不重置 close_timerout。 -# 帧级 WebSocket PING/PONG 在 uwebsocket.recv() 内已处理,不会进入 AI_client_VC_recv。 -# 金三 V2.5 文档示例:WebSocket 文本帧 "ping" 保活;对端可能回 pong / heartbeat 类事件。 -_VC_KEEPALIVE_RX_EVENTS = frozenset(("pong", "ping", "heartbeat", "keepalive")) - - -def _vc_out_json_event_meta(s): - """解析 WS 出站 JSON:event 可能在 header 或顶层(Start/audio.create vs synthesis)。""" - try: - jd = ujson.loads(s) - h = jd.get("header") - if isinstance(h, dict): - ev = h.get("event") or "" - if ev: - return ev, h.get("trace_id") or "" - ev = jd.get("event") or "" - return ev, "" - except Exception: - return "", "" - - -class AI_led(Abstract): - def __init__(self): - try: - if "vc_config" in globals() and vc_config: - r_gpio, g_gpio, b_gpio = vc_config.led_gpio_tuple() - else: - r_gpio, g_gpio, b_gpio = Pin.GPIO36, Pin.GPIO21, Pin.GPIO19 - except Exception: - r_gpio, g_gpio, b_gpio = Pin.GPIO36, Pin.GPIO21, Pin.GPIO19 - self._led_gpio_r = r_gpio - self._led_gpio_g = g_gpio - self._led_gpio_b = b_gpio - self.led_red = Pin(r_gpio, Pin.OUT, Pin.PULL_PD, 0) - self.led_green = Pin(g_gpio, Pin.OUT, Pin.PULL_PD, 1) - self.led_blue = Pin(b_gpio, Pin.OUT, Pin.PULL_PD, 1) - print("AI_led GPIO dev_type={} red={} green={} blue={}".format( - dev_type, r_gpio, g_gpio, b_gpio)) - - # 统一输入状态(充电/电量/信号/AI对话),按优先级表输出 - self._st_ai = "idle" # idle/listening/thinking/playing - self._st_charging = "none" # none/charging/full - self._st_battery = "normal" # normal/low/empty - self._st_signal = "normal" # normal/weak - self._st_alert = None # None / rb_flash_2(REQ-ERR-01/03) - - self._timer = osTimer() - self._mode = "off" # off/red_on/green_on/green_blink/blue_blink/red_blink/bg_alt/alert_rb - self._alert_steps_left = 0 - self._alert_rb_phase = 0 - self._alert_period_ms = 250 - self._blink_on = 0 - self._bg_phase = 0 - self._period_ms = 1000 - self._gb_on_ms = 1000 - self._gb_off_ms = 1000 - - EventMesh.subscribe("set_led_state", self.set_led_state) - EventMesh.subscribe("get_led_debug_state", self.get_led_debug_state) - # LED 电平极性:有些板子是低电平点亮(active-low),也可能仅某一路反相 - self._inv_r = 0 - self._inv_g = 0 - self._inv_b = 0 - try: - if "vc_config" in globals() and vc_config: - inv = vc_config.get_attr("LED_ACTIVE_LOW_RGB", None) - if isinstance(inv, (tuple, list)) and len(inv) == 3: - self._inv_r = 1 if inv[0] else 0 - self._inv_g = 1 if inv[1] else 0 - self._inv_b = 1 if inv[2] else 0 - else: - all_inv = 1 if vc_config.get_bool("LED_ACTIVE_LOW", False) else 0 - self._inv_r = all_inv - self._inv_g = all_inv - self._inv_b = all_inv - except Exception: - pass - try: - self._apply_by_table() - except Exception: - try: - self._set_off() - except Exception: - pass - - def _timer_stop(self): - try: - self._timer.stop() - except Exception: - pass - - def _set_rgb(self, r, g, b): - try: - rr = 1 if r else 0 - gg = 1 if g else 0 - bb = 1 if b else 0 - if self._inv_r: - rr = 0 if rr else 1 - if self._inv_g: - gg = 0 if gg else 1 - if self._inv_b: - bb = 0 if bb else 1 - self.led_red.write(rr) - self.led_green.write(gg) - self.led_blue.write(bb) - except Exception: - pass - - def _set_off(self): - self._mode = "off" - self._timer_stop() - self._set_rgb(0, 0, 0) - - def _set_red_on(self): - self._mode = "red_on" - self._timer_stop() - self._set_rgb(1, 0, 0) - - def _set_green_on(self): - self._mode = "green_on" - self._timer_stop() - self._set_rgb(0, 1, 0) - - def _set_green_blink(self, on_ms=1000, off_ms=None): - """待机/空闲:绿灯慢闪(替代呼吸灯,数字 GPIO 整颗亮灭)。off_ms 缺省则与 on_ms 对称。""" - on_ms = int(on_ms or 1000) - if off_ms is None: - off_ms = on_ms - else: - off_ms = int(off_ms or on_ms) - try: - if (self._mode == "green_blink" and int(self._gb_on_ms) == int(on_ms) - and int(self._gb_off_ms) == int(off_ms)): - return - except Exception: - pass - self._mode = "green_blink" - self._gb_on_ms = on_ms - self._gb_off_ms = off_ms - self._period_ms = on_ms - self._blink_on = 1 - self._timer_stop() - self._set_rgb(0, 1, 0) - self._timer.start(self._gb_on_ms, 0, self._timer_cb) - - def _set_blue_blink(self, period_ms=1000): - self._mode = "blue_blink" - self._period_ms = period_ms or 1000 - self._blink_on = 1 - self._timer_stop() - self._set_rgb(0, 0, 1) - self._timer.start(self._period_ms, 1, self._timer_cb) - - def _set_red_blink(self, period_ms=1000): - self._mode = "red_blink" - self._period_ms = period_ms or 1000 - self._blink_on = 1 - self._timer_stop() - self._set_rgb(1, 0, 0) - self._timer.start(self._period_ms, 1, self._timer_cb) - - def _set_bg_alt(self, period_ms=1000): - self._mode = "bg_alt" - self._period_ms = period_ms or 1000 - self._bg_phase = 0 - self._timer_stop() - self._set_rgb(0, 0, 1) - self._timer.start(self._period_ms, 1, self._timer_cb) - - def _set_alert_rb_flash(self, cycles=2, period_ms=250): - """REQ-ERR-01/03:红蓝快闪(约 2 次/秒),结束后回到 _apply_by_table。""" - try: - cycles = int(cycles or 2) - except Exception: - cycles = 2 - if cycles < 1: - cycles = 1 - try: - period_ms = int(period_ms or 250) - except Exception: - period_ms = 250 - if period_ms < 100: - period_ms = 100 - self._mode = "alert_rb" - self._st_alert = "rb_flash_2" - self._alert_period_ms = period_ms - self._alert_steps_left = cycles * 2 - self._alert_rb_phase = 0 - self._timer_stop() - self._set_rgb(1, 0, 0) - self._timer.start(self._alert_period_ms, 0, self._timer_cb) - - def _set_red_flash_limited(self, flashes=3, period_ms=500): - """REQ-BAT-03:红快闪若干次后熄灭(约 2 次/秒)。""" - try: - flashes = int(flashes or 3) - except Exception: - flashes = 3 - if flashes < 1: - flashes = 1 - try: - period_ms = int(period_ms or 500) - except Exception: - period_ms = 500 - if period_ms < 100: - period_ms = 100 - self._mode = "red_flash_n" - self._red_flash_steps_left = flashes * 2 - self._alert_period_ms = period_ms - self._blink_on = 1 - self._timer_stop() - self._set_rgb(1, 0, 0) - self._timer.start(self._alert_period_ms, 0, self._timer_cb) - - def _timer_cb(self, *args, **kwargs): - EventMesh.publish("publish_AI_btnInfo", {"func": self._timer_cb_}) - - def _timer_cb_(self): - if self._mode == "green_blink": - self._blink_on = 0 if self._blink_on else 1 - self._set_rgb(0, 1 if self._blink_on else 0, 0) - next_ms = self._gb_on_ms if self._blink_on else self._gb_off_ms - try: - self._timer.start(next_ms, 0, self._timer_cb) - except Exception: - pass - return - if self._mode == "blue_blink": - self._blink_on = 0 if self._blink_on else 1 - self._set_rgb(0, 0, 1 if self._blink_on else 0) - return - if self._mode == "red_blink": - self._blink_on = 0 if self._blink_on else 1 - self._set_rgb(1 if self._blink_on else 0, 0, 0) - return - if self._mode == "red_flash_n": - self._red_flash_steps_left -= 1 - if self._red_flash_steps_left <= 0: - self._set_off() - return - self._blink_on = 0 if self._blink_on else 1 - self._set_rgb(1 if self._blink_on else 0, 0, 0) - try: - self._timer.start(self._alert_period_ms, 0, self._timer_cb) - except Exception: - pass - return - if self._mode == "bg_alt": - if self._bg_phase == 0: - self._set_rgb(0, 1, 0) - self._bg_phase = 1 - else: - self._set_rgb(0, 0, 1) - self._bg_phase = 0 - return - if self._mode == "alert_rb": - self._alert_steps_left -= 1 - if self._alert_steps_left <= 0: - self._st_alert = None - try: - self._apply_by_table() - except Exception: - self._set_off() - return - if self._alert_rb_phase == 0: - self._set_rgb(0, 0, 1) - self._alert_rb_phase = 1 - else: - self._set_rgb(1, 0, 0) - self._alert_rb_phase = 0 - try: - self._timer.start(self._alert_period_ms, 0, self._timer_cb) - except Exception: - pass - return - - def set_led_state(self, event=None, msg=None): - # msg: dict {ai_state, charging, battery, signal, alert} - try: - if isinstance(msg, dict): - if "alert" in msg: - aval = msg.get("alert") - if aval in ("rb_flash_2", "rb_flash", "rb_flash2"): - self._set_alert_rb_flash(2, 250) - return - if aval in ("red_flash_3", "red_flash3"): - try: - n = 3 - ms = 500 - if "vc_config" in globals() and vc_config: - n = int(vc_config.get_int("BATTERY_EMPTY_FLASH_COUNT", 3)) - ms = int(vc_config.get_int("BATTERY_EMPTY_FLASH_PERIOD_MS", 500)) - self._set_red_flash_limited(n, ms) - except Exception: - self._set_red_flash_limited(3, 500) - return - if aval in (None, "", "none", "clear"): - self._st_alert = None - if "ai_state" in msg: - self._st_ai = msg.get("ai_state") or "idle" - if "charging" in msg: - self._st_charging = msg.get("charging") or "none" - if "battery" in msg: - self._st_battery = msg.get("battery") or "normal" - if "signal" in msg: - self._st_signal = msg.get("signal") or "normal" - except Exception: - pass - if self._mode in ("alert_rb", "red_flash_n"): - return - if self._led_state_debug_enabled() and isinstance(msg, dict): - try: - print("[LED] set_led_state in={}".format(msg)) - except Exception: - pass - self._apply_by_table(msg) - - def _led_state_debug_enabled(self): - try: - if "vc_config" in globals() and vc_config: - return vc_config.get_bool("LED_STATE_DEBUG", True) - except Exception: - pass - return True - - def _led_log(self, tag, extra=""): - if not self._led_state_debug_enabled(): - return - try: - print("[LED] {} ai={} chg={} bat={} sig={} -> mode={} period_ms={} {}".format( - tag, self._st_ai, self._st_charging, self._st_battery, self._st_signal, - self._mode, self._period_ms, extra)) - except Exception: - pass - - def _apply_by_table(self, _msg=None): - if self._mode in ("alert_rb", "red_flash_n"): - return - _prev_mode = self._mode - # P0:低电/空电(高于 AI 交互灯;充电中不显示,走充电灯态) - try: - if self._st_charging not in ("charging", "full"): - if self._st_battery == "empty": - try: - ms = 500 - if "vc_config" in globals() and vc_config: - ms = int(vc_config.get_int("BATTERY_EMPTY_FLASH_PERIOD_MS", 500)) - self._set_red_blink(ms) - except Exception: - self._set_red_blink(500) - return - if self._st_battery == "low": - self._set_red_blink(1000) - return - except Exception: - pass - # P1~P3:AI 阶段(播放 > 思考 > 聆听) - try: - if self._st_ai == "playing": - self._set_green_on() - return - if self._st_ai == "thinking": - # 蓝绿「交替」:约 0.5 s 换一色(`period_ms` 为换色间隔) - self._set_bg_alt(500) - return - if self._st_ai == "listening": - # 长对话蓝慢闪(≈1s);单对话 PTT 蓝快闪(≈2次/s,REQ-AI-07) - period_ms = 1000 - try: - if EventMesh.publish("get_storage_config", "VC_mode") == 1: - period_ms = 250 - except Exception: - pass - self._set_blue_blink(period_ms) - return - except Exception: - pass - # AI 交互中(聆听/思考/播放/流播)不让充电灯抢占;交互结束 idle 后恢复充电红/充满绿(REQ 冲突规避) - _skip_charge_led = 0 - try: - if self._st_ai in ("listening", "thinking", "playing"): - _skip_charge_led = 1 - except Exception: - pass - if not _skip_charge_led: - try: - if EventMesh.publish("get_vc_stream_active") == 1: - _skip_charge_led = 1 - except Exception: - pass - # 基础态:充电/电量/信号 - try: - if not _skip_charge_led: - if self._st_charging == "full": - self._set_green_on() - return - if self._st_charging == "charging": - self._set_red_on() - return - if self._st_signal == "weak": - # 弱网:蓝绿「慢交替」,与 thinking 同节拍定义(约 1 s 换一色) - self._set_bg_alt(1000) - return - except Exception: - pass - # 空闲待机:默认灭灯(REQ-BAT-01 / GAP-005 方案甲);LED_IDLE_MODE=green_blink 可恢复旧工程绿灯慢闪 - try: - idle_mode = "off" - if "vc_config" in globals() and vc_config: - _im = vc_config.get_attr("LED_IDLE_MODE", None) - if _im is not None: - idle_mode = str(_im).strip().lower() - if idle_mode in ("green_blink", "blink", "green"): - idle_on = 1000 - if "vc_config" in globals() and vc_config: - idle_on = int(vc_config.get_int("LED_IDLE_GREEN_BLINK_MS", 1000)) - idle_off = idle_on - if "vc_config" in globals() and vc_config: - _ioff = vc_config.get_attr("LED_IDLE_GREEN_BLINK_OFF_MS", None) - if _ioff is not None: - try: - idle_off = int(_ioff) - except Exception: - idle_off = idle_on - if idle_on < 400: - idle_on = 400 - if idle_off < 400: - idle_off = 400 - self._set_green_blink(idle_on, idle_off) - else: - self._set_off() - except Exception: - self._set_off() - if self._mode != _prev_mode: - self._led_log("apply_table", "in={}".format(_msg)) - - def get_led_debug_state(self, event=None, msg=None): - # 供按键/状态机打印当前 LED 聚合状态与最终模式 - try: - return { - "ai": self._st_ai, - "charging": self._st_charging, - "battery": self._st_battery, - "signal": self._st_signal, - "mode": self._mode, - "period_ms": self._period_ms, - } - except Exception: - return {"mode": getattr(self, "_mode", "?")} - - -def _default_battery_ocv_map(): - # 与 mgr.py BATTERY_OCV_TABLE nix_coy_mnzo2 默认一致;可被 default_config.BATTERY_OCV_MAP 覆盖 - return { - 4290: 100, 4237: 95, 4179: 90, 4123: 85, 4070: 80, 4020: 75, 3963: 70, 3919: 65, 3882: 60, 3847: 55, - 3818: 50, 3794: 45, 3772: 40, 3752: 35, 3733: 30, 3716: 25, 3699: 20, 3677: 15, 3652: 10, 3629: 5, 3400: 0, - } - - -def _parse_battery_ocv_map(raw): - """解析 default_config.BATTERY_OCV_MAP:dict 或 [(mv, soc), ...]。""" - if raw is None: - return None - out = {} - try: - if isinstance(raw, dict): - pairs = raw.items() - elif isinstance(raw, (tuple, list)): - pairs = ((p[0], p[1]) for p in raw if isinstance(p, (tuple, list)) and len(p) >= 2) - else: - return None - for k, v in pairs: - out[int(k)] = int(v) - return out if out else None - except Exception: - return None - - -def _soc_from_vbatt_mv(volt_mv, ocv_map, ocv_volts, vbatt_simple=None): - """由电压(mV) 算 SoC(0~100);配置来自 default_config / _BatteryCfg。""" - try: - v = int(volt_mv) - except Exception: - return None - try: - if vbatt_simple: - _full, _v20, _v5 = vbatt_simple - if v >= int(_full): - return 100 - if v <= int(_v5): - return 0 - if v <= int(_v20): - return 10 - return 60 - except Exception: - pass - try: - volts = ocv_volts - if not volts: - return None - pre_volt = 0 - volt_not_under = 0 - soc1 = 0 - soc2 = 0 - for vv in volts: - if v > vv: - volt_not_under = 1 - soc1 = ocv_map.get(vv, 0) - soc2 = ocv_map.get(pre_volt, 0) - break - else: - pre_volt = vv - if pre_volt == 0: - return soc1 - if volt_not_under == 0: - return 0 - return int(soc2 - (soc2 - soc1) * (pre_volt - v) // (pre_volt - vv)) - except Exception: - return None - - -class _BatteryCfg(object): - """从 vc_config(default_config) 加载电池参数,供 AI_hw_status / get_battery / AI_dev 共用。""" - _inst = None - - def __init__(self): - self.ocv_map = _default_battery_ocv_map() - self.ocv_volts = sorted(self.ocv_map.keys(), reverse=True) - self.charge_full_mv = 4290 - self.low_mv = 3699 - self.empty_mv = 3629 - self.low_soc = 20 - self.empty_soc = 5 - self.vbatt_simple = None - try: - if "vc_config" in globals() and vc_config: - self.charge_full_mv = int(vc_config.get_int("VBATT_CHARGE_FULL_MV", 4290)) - self.low_mv = int(vc_config.get_int("VBATT_LOW_MV", 3699)) - self.empty_mv = int(vc_config.get_int("VBATT_EMPTY_MV", 3629)) - self.low_soc = int(vc_config.get_int("BATTERY_LOW_SOC_PERCENT", 30)) - self.empty_soc = int(vc_config.get_int("BATTERY_EMPTY_SOC_PERCENT", 10)) - _ocv_cfg = _parse_battery_ocv_map(vc_config.get_attr("BATTERY_OCV_MAP", None)) - if _ocv_cfg: - self.ocv_map = _ocv_cfg - self.ocv_volts = sorted(self.ocv_map.keys(), reverse=True) - if vc_config.get_bool("USE_SIMPLE_VBATT_THRESH", False): - self.vbatt_simple = (self.charge_full_mv, self.low_mv, self.empty_mv) - except Exception: - pass - - @classmethod - def instance(cls): - if cls._inst is None: - cls._inst = cls() - return cls._inst - - def soc_from_vbatt(self, volt_mv): - return _soc_from_vbatt_mv(volt_mv, self.ocv_map, self.ocv_volts, self.vbatt_simple) - - -class AI_hw_status(Abstract): - # 精简硬件状态采集:仅维护 LED 状态机所需的 charging/battery/signal - def __init__(self): - # 无条件首行:确认是否进入初始化(避免被异常/注册顺序误判) - try: - print("[HW] __init__ enter") - except Exception: - pass - _bcfg = _BatteryCfg.instance() - self._bcfg = _bcfg - self._ocv_map = _bcfg.ocv_map - self._ocv_volts = _bcfg.ocv_volts - self._charge_full_mv = _bcfg.charge_full_mv - self._low_soc = _bcfg.low_soc - self._empty_soc = _bcfg.empty_soc - self._vbatt_simple = _bcfg.vbatt_simple - # cached state (avoid repeat publish) - self._chg = None - self._bat = None - self._sig = None - self._usb = None - self._charge_full_latched = 0 - self._charge_full_streak = 0 - self._charge_full_streak_need = 2 - self._timer = osTimer() - self._period_ms = 10000 - self._weak_csq = 10 - self._dbg = 0 - try: - if "vc_config" in globals() and vc_config: - self._period_ms = vc_config.get_int("HW_STATUS_PERIOD_MS", 10000) or 10000 - self._weak_csq = vc_config.get_int("SIGNAL_WEAK_CSQ", 10) or 10 - self._dbg = 1 if vc_config.get_bool("HW_STATUS_DEBUG", False) else 0 - except Exception: - pass - # 若开启了 LED/按键调试,也一并打开硬件监控打印,便于关联分析 - try: - if "vc_config" in globals() and vc_config: - if vc_config.get_bool("LED_DEBUG", False) or vc_config.get_bool("KEY_LED_DEBUG", False): - self._dbg = 1 - except Exception: - pass - - # 注意:AI_dev 也会注册 USB callback;USB 模块通常只支持一个 callback,后注册会覆盖先注册。 - # 为避免“刚开始有效,后面失效”,这里不直接 setCallback,改为订阅 AI_dev 转发的 USB 状态事件, - # 并在 10 秒轮询中再读一次 GET_USB_STATE 作兜底。 - try: - EventMesh.subscribe("hw_usb_state", self._on_usb_state) - except Exception: - pass - - # 初始化:先立即刷新一次,再开启周期轮询(避免 one-shot/覆盖差异导致只跑一次) - try: - self._update() - except Exception: - pass - try: - self._timer.start(self._period_ms, 1, self._tick_cb) - except Exception: - pass - # 初始化打印(一次) - try: - print("[HW] init ok period_ms={} weak_csq={} usb_init={} algo={}".format( - self._period_ms, - self._weak_csq, - self._usb, - "simple" if getattr(self, "_vbatt_simple", None) else "ocv")) - if self._dbg == 1: - print("[HW] init detail vbatt_simple={}".format(getattr(self, "_vbatt_simple", None))) - except Exception: - pass - - def _on_usb_state(self, event=None, msg=None): - try: - state = msg - prev = self._usb - self._usb = 1 if state == 1 else 0 - if self._usb == 1 and prev != 1: - self._charge_full_latched = 0 - self._charge_full_streak = 0 - if self._usb == 0: - self._charge_full_latched = 0 - self._charge_full_streak = 0 - except Exception: - pass - try: - if self._dbg == 1: - print("[HW] usb_evt state={} usb={}".format(state, self._usb)) - except Exception: - pass - try: - self._update() - except Exception: - pass - - def _tick_cb(self, *args, **kwargs): - # 定时器回调直接刷新(不依赖 AI_dev 的事件线程) - try: - self._tick() - except Exception: - pass - - def _tick(self, *args, **kwargs): - self._update() - - def _update(self): - # 每次刷新都“同步推送一次”当前硬件基础态到 LED(不依赖变化检测) - # 目的:保证 10 秒轮询和 USB callback 都能及时刷新充电/电量/信号的基础态显示 - # charging/battery - try: - v = Power.getVbatt() - except Exception: - v = None - soc = None - csq = None - rssi = None - usb_status = None - chg = "none" - bat = "normal" - sig = "normal" - try: - usb_status = self._usb - if usb_status is None: - try: - usb_status = EventMesh.publish("GET_USB_STATE") - if usb_status is None: - usb_status = 0 - except Exception: - usb_status = 0 - # charging:插入后先红灯;勿用瞬时 vbatt≥满电阈值直接判满(充电器接入电压即偏高) - if usb_status == 1: - if getattr(self, "_charge_full_latched", 0) == 1: - chg = "full" - else: - if v is not None and v >= int(self._charge_full_mv): - self._charge_full_streak = int(getattr(self, "_charge_full_streak", 0)) + 1 - need = int(getattr(self, "_charge_full_streak_need", 2) or 2) - if self._charge_full_streak >= need: - self._charge_full_latched = 1 - chg = "full" - else: - chg = "charging" - else: - self._charge_full_streak = 0 - chg = "charging" - # battery level for LED table - if usb_status != 1 and v is not None: - soc = self._bcfg.soc_from_vbatt(v) - if soc is not None: - if soc < int(self._empty_soc): - bat = "empty" - elif soc < int(self._low_soc): - bat = "low" - self._chg = chg - self._bat = bat - except Exception: - pass - - # signal - try: - csq = net.csqQueryPoll() - # csqQueryPoll 返回 (rssi, ber) 或 int,按保守取 rssi - try: - if isinstance(csq, (tuple, list)) and len(csq) > 0: - rssi = csq[0] - else: - rssi = csq - except Exception: - rssi = csq - sig = "weak" if (rssi is not None and int(rssi) >= 0 and int(rssi) < int(self._weak_csq)) else "normal" - self._sig = sig - except Exception: - pass - - # 统一推送一次当前基础态(一次调用带齐字段,减少来回切换) - try: - EventMesh.publish("set_led_state", {"charging": chg, "battery": bat, "signal": sig}) - except Exception: - pass - - # 轮询调试打印(每次 10s tick 一次):硬件采样一行 + LED 聚合状态一行内打完 - try: - if self._dbg == 1: - led = None - try: - led = EventMesh.publish("get_led_debug_state") - except Exception: - pass - print("[HW] usb={} vbatt_mv={} soc={} chg={} bat={} csq={} rssi={} sig={} period_ms={} led={}".format( - usb_status, v, soc, self._chg, self._bat, csq, rssi, self._sig, self._period_ms, led)) - except Exception: - pass - - def _soc_from_vbatt(self, volt_mv): - return self._bcfg.soc_from_vbatt(volt_mv) - - -def set_ipv4_datacall(): - # 获取第一路的APN,ipType 等信息 - pdpCtx = dataCall.getPDPContext(1) - if pdpCtx != -1: - if pdpCtx[0] != 0: - # 如果不是用户需要的ipv4,使用如下方式配置,使用如下方式配置 - ret = dataCall.setPDPContext(1, 0,pdpCtx[1],pdpCtx[2],pdpCtx[3],pdpCtx[4]) - if ret == 0: - print('配置成功。') - # cfun0/1后按照配置的信息进行拨号 - net.setModemFun(0) - net.setModemFun(1) - else: - print('配置失败。') - else: - print('已经配置为IPv4') - else: - print('获取PDP Context失败。') - -#HEARDER+TOTALLEN+DATELEN+DATA -HEARDER = "FFFE" -TOTALLEN = "00000000" -DATELEN = "0000" -DATA = {} -class AI_UART(): - #串口命令通道 - def __init__(self): - self.send_count=0 - self.AI_uart = UART(UART.UART4, 230400, 8, 0, 1, 0) - self.AI_uart.set_callback(self.uart_call) - - EventMesh.subscribe("bt_ota_run",self.bt_ota_run)# - EventMesh.subscribe("start_ota",self.start_ota) - EventMesh.subscribe("end_ota",self.end_ota) - EventMesh.subscribe("send_bt_switch_toble",self.send_bt_switch_toble) - EventMesh.subscribe("send_ble_switch_toble",self.send_ble_switch_toble) - EventMesh.subscribe("send_ble_scan",self.send_ble_scan) - EventMesh.subscribe("send_connect_bt_mac",self.send_connect_bt_mac) - EventMesh.subscribe("send_disconnect_bt_mac",self.send_disconnect_bt_mac) - EventMesh.subscribe("send_imei_to_ble",self.send_imei_to_ble) - EventMesh.subscribe("send_get_connect_bt_state",self.send_get_connect_bt_state) - EventMesh.subscribe("recv_bt_connect_rest",self.recv_bt_connect_rest) - EventMesh.subscribe("AI_send_bt_ble_switch_toble",self.AI_send_bt_ble_switch_toble) - EventMesh.subscribe("get_bt_versions",self.get_bt_versions) - EventMesh.subscribe("adjust_volume_toBle",self.adjust_volume_toBle) - EventMesh.subscribe("clear_earphone_record",self.clear_earphone_record) - self.bt_switch = 0 - self.ble_switch = 0 - self.uart_timer = osTimer() - self.get_bt_connect_timer = osTimer() - self.get_bt_connect_flag = 0 - self.uart_timer_count = 0 - self.bt_ota_flag = 0 - self.read_ack_flag = 0 - self.ping_json_str = {} - self.ping_json_str_tmp ="" - self.read_uart_data = "" - self.bt_versions="" - try: - uart_ok = False - if "vc_config" in globals() and vc_config: - uart_ok = vc_config.uart_supported() - except Exception: - uart_ok = False - if uart_ok: - self.uart_send_queue = Queue(100) - self.uart_send_queue_thread_id = _thread.start_new_thread(self.uart_send_queue_thread, ()) - - self.uart_recv_queue = Queue(100) - self.uart_recv_queue_thread_id = _thread.start_new_thread(self.uart_recv_queue_thread, ()) - self.send_get_versions()#获取蓝牙版本 - - def uart_send_queue_thread(self): - while True: - data = self.uart_send_queue.get() - - #EventMesh.debug_log("Info_queue:data = {}".format(data)) - if data != None: - try: - if len(data) != 0: - print("send_queu_uart_data2:{} data:{}".format(data,data["data"])) - # json_str =ujson.dumps(data) - if data["event"] == "uart_send_data": - print("send_queu_uart_data sucess data:{} {}".format(data["data"],type(data["data"]))) - self.uart_queue_send_data(data["data"]) - if "resend_flag" in data: - print("send_reset_cb:{} {}".format(data["resend_flag"],data["ACK_time"])) - if data["resend_flag"] == 1: - self.send_count = 5#重试5次 - ack_timer = 0 #单次最大等待时间 - count = 0 - if "ACK_time" in data: - if data["ACK_time"] == None: - ack_timer = 100#最大重发5次,没ack或者返回error重发 - else: - ack_timer = data["ACK_time"] - if ack_timer > 0: - while self.send_count: - - if self.send_count <=0 or self.read_ack_flag == 1: - print("4G串口重发机制退出:{} self.read_ack_flag:{} json_str:{}".format(self.send_count,self.read_ack_flag,data["data"])) - break - if count >= ack_timer: - count = 0 - print("串口异常,重发:") - self.uart_queue_send_data(data["data"]) - self.send_count-=1 - count += 1 - utime.sleep_ms(1) - - except Exception as e: - usys.print_exception(e) - utime.sleep_ms(10) - - def uart_recv_queue_thread(self): - while True: - data = self.uart_recv_queue.get() - - #EventMesh.debug_log("Info_queue:data = {}".format(data)) - if data != None: - try: - if len(data) != 0: - print("recv_queu_uart_data2:{}".format(data)) - # json_str =ujson.dumps(data) - # queue_data={"event":"uart_recv_data"} - if data["event"] == "uart_recv_data": - print("recv_queu_uart_data sucess data:{}".format(type(data["event"]))) - self.pares_uart_data() - except Exception as e: - usys.print_exception(e) - - def send_imei_to_ble(self,event,msg): - EventMesh.publish("publish_AI_btnInfo",{"func":self.send_imei_to_ble_cb}) - - def send_imei_to_ble_cb(self): - self.read_ack_flag=0 - imei = modem.getDevImei() - json_data = \ - { - - "event": "imei", - "data":str(imei) - - } - json_str =ujson.dumps(json_data) - print("send imei:{}".format(json_str)) - self.send_data(json_str) - pass - def send_connect_bt_mac(self,event,msg): - self.read_ack_flag=0 - json_data = \ - { - "event": "connect_bt", - "data":msg - } - json_str =ujson.dumps(json_data) - self.send_data(json_str,1,2000) - # self.send_reset_cb(json_str,0,2000) #每2s发送一次 10ms为单位 - pass - def send_disconnect_bt_mac(self,event,msg): - self.read_ack_flag=0 - json_data = \ - { - "event": "disconnect_bt", - "data":msg - } - json_str =ujson.dumps(json_data) - self.send_data(json_str,1) - # self.send_reset_cb(json_str) - - pass - def uart_timer_cb(self, *args, **kwargs): - self.uart_timer_count+=1 - if self.uart_timer_count >40: - self.read_uart_data = "" - if self.uart_timer_count >200: - self.read_uart_data = "" - self.uart_timer.stop() - def split_combined_json(self,combined_str): - # 将连续的JSON字符串分割为多个独立的JSON字符串列表 - # - # 参数: - # combined_str: 多个JSON拼接的字符串 - # 返回: - # 分割后的 - json_list = [] - start_index = 0 - brace_count = 0 # 用于匹配{}括号 - print("combined_str:{}".format(combined_str)) - for i, char in enumerate(combined_str): - if char == '{': - brace_count += 1 - elif char == '}': - brace_count -= 1 - - # 当括号完全匹配(count=0)时,说明一个完整的JSON对象结束 - if brace_count == 0: - # 提取从start_index到当前位置的子串(包含当前}) - json_str = combined_str[start_index:i+1].strip() - if json_str: # 避免空字符串 - json_list.append(json_str) - start_index = i + 1 # 下一个JSON的起始位置 - print("json_list{}".format(json_list)) - - return json_list - - def uart_call(self,para): - queue_data={"event":"uart_recv_data"} - - self.uart_recv_queue.put(queue_data) - # print("uart_call start") - # EventMesh.publish("publish_AI_btnInfo",{"func":self.pares_uart_data}) - # print("uart_call end") - #print("ai_uart:{}".format(para)) - #[{"event":""},{},{}] - def pares_uart_data(self): - uart_data = self.AI_uart.read() - self.uart_timer.start(10, 1, self.uart_timer_cb) - self.uart_timer_count = 0 - ack_flag = 0#是否要返回ack - print("uart_data:{}",uart_data) - uart_data_str = uart_data.decode('utf-8') - - self.read_uart_data = self.read_uart_data +uart_data_str - try: - ujson.loads(self.read_uart_data) - except Exception as e: - print("json数据不完整:self.read_uart_data{}".format(self.read_uart_data)) - usys.print_exception(e) - return - self.uart_timer.stop() - self.uart_timer_count = 0 - self.playload_data(self.read_uart_data) - - # 分割JSON - # json_strings = self.split_combined_json(uart_data_str) - - # # 逐个解析并处理 - # for i, json_str in enumerate(json_strings, 1): - # print("第{}个JSON{}".format(i,json_str)) - # try: - # data = ujson.loads(json_str) - - # # 在这里添加你的业务逻辑,比如根据event处理不同事件 - # self.uart_timer.stop() - # self.uart_timer_count = 0 - # self.playload_data(self.read_uart_data) - # # if data.get("event") == "bt_connect_single": - - # # elif data.get("event") == "bt_scan_rest": - - # except Exception as e: - # print("JSON解析错误:原始字符串:") - # print("-" * 50) - - - - # print("uart_data_str:{}",uart_data_str) - # if len(uart_data_str) < (HEARDER+1+1): - # return - # head = uart_data_str[0:4] - # pack_flag = uart_data_str[4:5] - # recvflag = uart_data_str[5:6] - - - # if head == HEARDER: - # if pack_flag == "1": #分包数据 - # if recvflag == "1": #未发送完成 - # self.ping_json_str_tmp+=uart_data_str[6:] - # pass - # else: - # self.playload_data(self.ping_json_str_tmp) - # pass - # else: - # self.playload_data(uart_data_str[6:]) - - # else: - # print("uart is not FFFE:") - - def playload_data(self,msg): - ack_flag=0 - try: - print("playload_data:{}".format(msg)) - jsload_data = ujson.loads(msg) - #print("AI_client_VC_recv jsload_data = {}".format(jsload_data)) - - if jsload_data["event"] == "AI_KEY_PRESS" :#AI按键按下 - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_key_press}) - ack_flag = 1 - elif jsload_data["event"] == "BT_AI_KEY_PRESS" :#蓝牙耳机key ai对话模式不支持长按对话模式 只支持短按按键对话模式 重新定义按键 - EventMesh.publish("publish_AI_btnInfo",{"func":self.BT_AI_key_press}) - ack_flag = 1 - elif jsload_data["event"] == "AI_PWR_PRESS" :#电源按键按下 - EventMesh.publish("publish_AI_btnInfo",{"func":self.pwk_key_release}) - ack_flag = 1 - elif jsload_data["event"] == "bt_scan_rest":#扫描列表 #蓝牙停止扫描后主动向4g发送扫描列表需要发送ack - EventMesh.publish("send_bt_scan_list",jsload_data) - ack_flag=1 - elif jsload_data["event"]=="bt_connect_single": #收到蓝牙主动发送过来的单个连接蓝牙耳机的连接状态 需要发送ack - EventMesh.publish("save_bt_connect_list",jsload_data) - ack_flag=1 - - elif jsload_data["event"]=="need_send_bt_connect_list": #发送连接蓝牙连接记录列表 不需要发送ack - self.get_bt_connect_timer.stop() - self.get_bt_connect_flag = 0 - EventMesh.publish("save_bt_connect_list",jsload_data) #先保存连接记录再发送连接耳机列表to IOT - EventMesh.publish("send_bt_connect_list") - - elif jsload_data["event"]=="get_ble_switch": - ble_switch_flag = EventMesh.publish("get_storage_config","switch_ble_state")#ble开关 不需要发送ack - if ble_switch_flag == None: - ble_switch_flag = "1" - EventMesh.publish("send_ble_switch_toble",str(ble_switch_flag)) - - pass - elif jsload_data["event"]=="get_bt_switch": - bt_switch_flag = EventMesh.publish("get_storage_config","switch_bt_state")#bt开关 不需要发送ack - if bt_switch_flag == None: - bt_switch_flag = "1" - EventMesh.publish("send_bt_switch_toble",bt_switch_flag) - - pass - elif jsload_data["event"]=="get_cat1_imei": #蓝牙主动向4G获取imei 4g不需要发送ack 蓝牙收到imei需要回ack给4改 - EventMesh.publish("send_imei_to_ble") - ack_flag=1 - pass - elif jsload_data["event"] == "post_bt_connect_rest": #蓝牙主动蓝牙耳机连接状态 需要发送ack给蓝牙 - EventMesh.publish("recv_bt_connect_rest",jsload_data) - ack_flag=1 - - elif jsload_data["event"] == "clear_earphone_record_ok":#清除蓝牙耳机连接记录 - tts_play_str = "删除成功" - EventMesh.publish("play_tts",[tts_play_str,1]) - self.read_ack_flag = 1 - - elif jsload_data["event"] == "versions": - self.bt_versions = jsload_data["data"] - print("当前蓝牙版本是:{}".format(self.bt_versions)) - self.read_ack_flag = 1 - - # elif jsload_data["event"] == "kws" :# - # EventMesh.publish("kws_even") - elif jsload_data["event"] == "ack" :# - self.read_ack_flag = 1 - if ack_flag == 1: - self.send_ack() - self.read_uart_data = "" - except Exception as e: - usys.print_exception(e) - self.read_uart_data = "" - def recv_bt_connect_rest(self,event,msg): - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - if "data" in msg: - EventMesh.publish("send_bt_connect_state_to_iot",msg) - pass - def get_bt_versions(self,event=None,msg=None): - return self.bt_versions - def clear_earphone_record(self,event,msg): #清楚所有耳机记录 - - json_data = \ - { - "event": "clear_earphone_record", - } - json_str =ujson.dumps(json_data) - print("clear_earphone_record{}".format(json_str)) - self.send_data(json_str,1) - def adjust_volume_toBle(self,event,msg): # - self.read_ack_flag=0 - volume = int(msg) - if volume <= 10: - volume = 10 - json_data = \ - { - "event": "adjust_volume", - "data":str(volume) - } - json_str =ujson.dumps(json_data) - print("adjust_volume_toBle{}".format(json_str)) - self.send_data(json_str,1) - - def AI_send_bt_ble_switch_toble(self,event,msg): - # // { - # // "event":"switch_bt_state", - # // "data":{ - # // "switch_bt_state":"0" - # // } - # // } #格式 - - self.read_ack_flag=0 - if msg == 1: #AI对话开启蓝牙 - json_data = \ - { - - "event": "switch_ble_state", - "data":{ - "switch_ble_state":"1" - } - - } - json_str =ujson.dumps(json_data) - print("switch_ble_state{}".format(json_str)) - self.send_data(json_str,1) - - elif msg == 2: #AI对话开启蓝牙 - json_data = \ - { - - "event": "switch_ble_state", - "data":{ - "switch_ble_state":"0" - } - - } - json_str =ujson.dumps(json_data) - print("switch_ble_state{}".format(json_str)) - self.send_data(json_str,1) - elif msg == 3: #AI对话开启蓝牙 - json_data = \ - { - - "event": "switch_bt_state", - "data":{ - "switch_bt_state":"1" - } - } - json_str =ujson.dumps(json_data) - print("switch_bt_state{}".format(json_str)) - self.send_data(json_str,1) - - elif msg == 4: #AI对话开启蓝牙 - json_data = \ - { - - "event": "switch_bt_state", - "data":{ - "switch_bt_state":"0" - } - - } - json_str =ujson.dumps(json_data) - print("switch_bt_state{}".format(json_str)) - self.send_data(json_str,1) - - def send_bt_switch_toble(self,event,msg): - EventMesh.publish("publish_AI_btnInfo",{"func":self.send_bt_switch_toble_cb,"data":msg}) - def send_bt_switch_toble_cb(self,data): - # // { - # // "event":"switch_bt_state", - # // "data":{ - # // "switch_bt_state":"0" - # // } - # // } #格式 - self.read_ack_flag=0 - json_data = \ - { - - "event": "switch_bt_state", - "data":{ - "switch_bt_state":str(data) - } - - } - json_str =ujson.dumps(json_data) - print("send_bt_switch_toble{}".format(json_str)) - self.send_data(json_str,1) - - def send_ble_switch_toble(self,event,msg): - EventMesh.publish("publish_AI_btnInfo",{"func":self.send_ble_switch_toble_cb,"data":msg}) - def send_ble_switch_toble_cb(self,data): - # // { - # // "event":"switch_ble_state", - # // "data":{ - # // "switch_ble_state":"0" - # // } - # // } #格式 - - self.read_ack_flag=0 - json_data = \ - { - "event": "switch_ble_state", - "data":{ - "switch_ble_state":str(data) - } - } - json_str =ujson.dumps(json_data) - print("send_ble_switch_toble{}".format(json_str)) - self.send_data(json_str,1) - - # 重发机制 - def send_reset_cb(self,json_str,need_resend = None,timeOut=None): - print("send_reset_cb:{} {} {}".format(json_str,need_resend,timeOut)) - if timeOut == None: - self.send_count = 100#最大重发5次,没ack或者返回error重发 - else: - self.send_count = timeOut - - while self.send_count: - self.send_count-=1 - utime.sleep_ms(10) - if self.send_count <=0 or self.read_ack_flag == 1: - print("4G串口重发机制退出:{} self.read_ack_flag:{} json_str:{}".format(self.send_count,self.read_ack_flag,json_str)) - break - if self.send_count%(self.send_count//10) == 0: - print("串口异常,重发:") - self.send_data(json_str) - - pass - def send_get_connect_bt_state(self,event,msg): - json_data = \ - { - - "event": "get_connect_bt_state" - } - json_str =ujson.dumps(json_data) - print("send_get_connect_bt_state{}".format(json_str)) - self.send_data(json_str) - self.get_bt_connect_flag = 1 - self.get_bt_connect_timer.start(2000,0,self.get_bt_connect_timer_cb) - def get_bt_connect_timer_cb(self,*agrc): - if self.get_bt_connect_flag == 1: - EventMesh.publish("send_bt_connect_list") - pass - self.get_bt_connect_flag = 0 - pass - def send_ble_scan(self,event,msg): - self.read_ack_flag=0 - # // { - # // "event":"send_ble_scan", - # // "data":{ - # // "send_ble_scan":"0" - # // } - # // } #格式 - json_data = \ - { - - "event": "bt_scan", - "data":msg - - } - json_str =ujson.dumps(json_data) - print("send_ble_scan{}".format(json_str)) - self.send_data(json_str,1) - # self.send_reset_cb(json_str) - - print("bt_send_end") - def send_data(self,str_data,need_resend = None,ACK_time =None): - try: - if not ("vc_config" in globals() and vc_config and vc_config.uart_supported()): - return - except Exception: - return - if self.bt_ota_flag == 1: - print("ota 中不需要上传") - return - print("send_data{} {} {}".format(str_data,need_resend,ACK_time)) - queue_data={"event":"uart_send_data","data":str_data} - if need_resend != None: - queue_data={"event":"uart_send_data","data":str_data,"resend_flag":1,"ACK_time":ACK_time} - - self.uart_send_queue.put(queue_data) - def uart_queue_send_data(self,str_data): - if self.bt_ota_flag == 1: - return - #EventMesh.publish("wakeup_BT") - print("bt_seuart_queue_send_data start {}".format(str_data)) - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - self.AI_uart.write(str_data) - print("bt_seuart_queue_send_data end {}".format(str_data)) - def led_r_open(self,event=None,msg=None): - json_data = \ - { - - "event": "led_r_open", - - - } - json_str =ujson.dumps(json_data) - print("led_r_open{}".format(json_str)) - self.send_data(json_str) - def led_g_open(self,event=None,msg=None): - json_data = \ - { - - "event": "led_g_open", - - } - json_str =ujson.dumps(json_data) - print("led_g_open{}".format(json_str)) - self.send_data(json_str) - def led_b_open(self,event=None,msg=None): - json_data = \ - { - - "event": "led_b_open", - - } - json_str =ujson.dumps(json_data) - print("led_b_open{}".format(json_str)) - self.send_data(json_str) - def led_rgb_open(self,event=None,msg=None): - json_data = \ - { - - "event": "led_rgb_open", - - } - json_str =ujson.dumps(json_data) - print("led_rgb_open{}".format(json_str)) - self.send_data(json_str) - def led_close(self,event=None,msg=None): - json_data = \ - { - - "event": "led_close", - - } - json_str =ujson.dumps(json_data) - print("led_close{}".format(json_str)) - self.send_data(json_str) - - def send_ack(self): - json_data = \ - { - - "event": "ack", - - } - json_str =ujson.dumps(json_data) - print("send_ack{}".format(json_str)) - self.send_data(json_str) - def AI_key_press(self): - self.check_rest_timer_count = 0 - print("AI_key_press") - EventMesh.publish("AI_key_press") - def BT_AI_key_press(self): - self.check_rest_timer_count = 0 - print("BT_AI_key_press") - EventMesh.publish("BT_AI_key_press") - - def pwk_key_release(self): - self.check_rest_timer_count = 0 - #print("pwk_key_release") - EventMesh.publish("pwk_key_release") - def send_get_versions(self,event=None,msg=None): - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - #self.read_ack_flag = 0 - json_data = \ - { - - "event": "get_versions", - } - json_str =ujson.dumps(json_data) - print("send_get_versions {}".format(json_str)) - self.send_data(json_str,1) - #self.AI_uart.write(json_str) - count =1000 - while True: - count -=1 - if count <=0 or self.read_ack_flag == 1: - break - utime.sleep_ms(1) - - pass - def start_ota(self,event=None,msg=None):#通过串口给蓝牙升级 - #EventMesh.publish("wakeup_BT") - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - print("start_ota!!!") - print("start_ota!!!") - json_data = \ - { - "event": "ota_start", - } - json_str =ujson.dumps(json_data) - self.AI_uart.write(json_str) - print(json_str) - count =100 - while True: - count -=1 - if count <=0 or self.read_ack_flag == 1: - break - utime.sleep_ms(1) - print("start_ota count:{}!!!".format(count)) - - def end_ota(self,event=None,msg=None):#通过串口给蓝牙升级 - #EventMesh.publish("wakeup_BT") - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - print("end_ota!!!") - print("end_ota!!!") - json_data = \ - { - "event": "ota_end", - } - json_str =ujson.dumps(json_data) - self.AI_uart.write(json_str) - print(json_str) - count =100 - while True: - count -=1 - if count <=0 or self.read_ack_flag == 1: - break - utime.sleep_ms(1) - print("end_ota count:{}!!!".format(count)) - - def bt_ota_run(self,event=None,msg=None):#通过串口给蓝牙升级 - # EventMesh.publish("wakeup_BT") - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - print("bt_ota_run!!!:{}".format(msg)) - print("bt_ota_run!!!") - print("bt_ota_run!!!") - self.bt_ota_flag = 1 - self.read_ack_flag = 0 - - json_data = \ - { - "event": "ota_start", - } - json_str =ujson.dumps(json_data) - self.AI_uart.write(json_str) - print(json_str) - count =2000#等20秒,擦除flash - while True: - count -=1 - if count <=0 or self.read_ack_flag == 1: - break - utime.sleep_ms(10) - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - print("ota_start:count:{}".format(count)) - url = msg[0] - bt_size = msg[1] - headers_T = {"Content-Type":"application/octet-stream"} - response = request.get(url = url,headers=headers_T,timeout = 120,decode = False) - offset = 0 - checksum = 0 - for i in response.content: - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - - - data = base64.b64encode(i) - send_count = 5#最大重发5次,没ack或者返回error重发 - while 1: - send_count-=1 - self.read_ack_flag = 0 - checksum = 0 - # 对每个字节进行异或运算 - for byte in i: - checksum ^= byte # 等价于 checksum = checksum ^ byte - json_data = \ - { - "event": "ota_data", - "update":data, - "offset":offset, - "checksum":checksum - } - print("offset:{},checksum:{}".format(offset,checksum)) - json_str =ujson.dumps(json_data) - self.AI_uart.write(json_str) - #utime.sleep_ms(100) - count =300 - while True: - count -=1 - if count <=0 or self.read_ack_flag != 0 : - break - utime.sleep_ms(10) - print("ota_data:{} count:{}".format(len(json_str),count)) - if self.read_ack_flag == 1: - break - if send_count <=0 : - return -1 - offset+=len(i) - if offset < bt_size: - return -1 - self.read_ack_flag = 0 - json_data = \ - { - "event": "ota_end", - } - json_str =ujson.dumps(json_data) - self.AI_uart.write(json_str) - - count =100 - while True: - count -=1 - if count <=0 or self.read_ack_flag == 1: - break - utime.sleep_ms(1) - self.bt_ota_flag = 0 - return 0 -audio_buf = ['chaobingfatis', 'chenggong', 'didianguanji', 'IMEIhao', 'jianquancuowu', 'key_prompt', 'liuliangtishi', 'Low_power_warning', 'R_CHARGE_FULL', 'R_CHARGE_IN', 'R_CHARGE_OUT', 'R_READ_TIME_0', 'R_READ_TIME_1', 'R_READ_TIME_2', 'R_READ_TIME_3', 'R_READ_TIME_4', 'R_READ_TIME_5', 'R_READ_TIME_6', 'R_READ_TIME_7', 'R_READ_TIME_8', 'R_READ_TIME_9', 'R_READ_TIME_A', 'R_READ_TIME_B', 'R_READ_TIME_C', 'R_READ_TIME_D', 'R_READ_TIME_E', 'R_READ_TIME_F', 'R_READ_TIME_G', 'R_READ_TIME_H', 'R_READ_TIME_I', 'R_READ_TIME_J', 'R_READ_TIME_K', 'R_READ_TIME_L', 'R_READ_TIME_M', 'R_READ_TIME_N', 'R_READ_TIME_O', 'R_READ_TIME_P', 'R_READ_TIME_Q', 'R_READ_TIME_R', 'R_READ_TIME_S', 'R_READ_TIME_T', 'R_READ_TIME_U', 'R_READ_TIME_V', 'R_READ_TIME_W', 'R_READ_TIME_X', 'R_READ_TIME_Y', 'R_READ_TIME_Z', 'send_tone', 'shouhou', 'xinhaoruo1', 'zhengzaiguanji'] -class AI_dev(Abstract): - # AI - def __init__(self): - # if(dev_type != "7PIN"): - # set_ipv4_datacall() - self.is_wdt = 0 #是否开启看门狗 - #AT+QDAI=1,0,0,1,0,0,1,1PCM音频输出设置为主模式、短同步、16 kHz采样率和256 kHz时钟频率,采样位深为16bit,使用的时间间隙数量为1,并且使用第一个时间间隙。 - try: - bt_pm_en = False - pin_bt_on = None - pin_mot = None - qdai_cfg = None - if "vc_config" in globals() and vc_config: - bt_pm_en = bool(vc_config.get_attr("BT_POWER_MOTOR_ENABLED", False)) - pin_bt_on = vc_config.get_attr("PIN_BT_ON_GPIO", None) - pin_mot = vc_config.get_attr("PIN_MOT_GPIO", None) - qdai_cfg = vc_config.get_attr("AUDIO_QDAI_CFG", None) - except Exception: - bt_pm_en = False - pin_bt_on = None - pin_mot = None - qdai_cfg = None - - if bt_pm_en: - try: - if pin_bt_on: - self.PIN_BT_ON = Pin(getattr(Pin, pin_bt_on), Pin.OUT, Pin.PULL_DISABLE, 0) - if pin_mot: - self.PIN_MOT = Pin(getattr(Pin, pin_mot), Pin.OUT, Pin.PULL_DISABLE, 1) - except Exception: - pass - try: - utime.sleep_ms(200) - if hasattr(self, "PIN_MOT"): - self.PIN_MOT.write(0) - if hasattr(self, "PIN_BT_ON"): - self.PIN_BT_ON.write(1) - except Exception: - pass - try: - if isinstance(qdai_cfg, (tuple, list)) and len(qdai_cfg) == 8: - audio.set_qdai_cfg(*qdai_cfg) # 用iis做音频输入输出 - except Exception: - pass - self.KeyAI_status = 0 - self.KeyAI_timer_count = 0 - self.KeyAI_long_press = 0 - self.KeySos_long_press = 0 - self.KeyAI_press = 0 - self.last_ai_key_time = 0#让ai按键不要频繁调用 - if self.is_wdt == 1: - self.wdt = WDT(30) #开启看门狗 - # self.wdt_timer = osTimer() - # self.wdt_timer.start(5000, 1, self.wdt_timer_cb) - - count = 0 - while True: - count += 1 - if count > 20 : - break - data = dataCall.getInfo(1, 0) - print("data call INFO {}".format(data)) - if data[2][0]: - self.network_state = 1 - break - print("等待网络 {}...".format(count)) - utime.sleep_ms(500) - self.KeyAI_timer = osTimer() - self.KeySOS_timer = osTimer() - self.play_imei_timer = osTimer() - self.play_imei_timer_count=0 - self.KeySOS_timer_count = 0 - self.KeySOS_timer_flag = 0 - self._single_power_ai_merged = False - self._pwk_shutdown_ticks = 30 # 100ms 节拍,默认 3s - try: - ai_gpio = "GPIO22" - pwk_gpio = "GPIO26" - bt_wakeup_en = False - wakeup_cta1_gpio = None - wakeup_bt_gpio = None - if "vc_config" in globals() and vc_config: - raw_ai = vc_config.get_attr("EXTINT_AI_GPIO", "GPIO22") - if raw_ai is None or raw_ai == "": - ai_gpio = None - else: - ai_gpio = raw_ai - pwk_gpio = vc_config.get_attr("PWK_KEY_GPIO", "GPIO26") or "GPIO26" - bt_wakeup_en = bool(vc_config.get_attr("BT_WAKEUP_4G_ENABLED", False)) - wakeup_cta1_gpio = vc_config.get_attr("EXTINT_WAKEUP_CTA1_GPIO", None) - wakeup_bt_gpio = vc_config.get_attr("PIN_WAKEUP_BT_GPIO", None) - try: - self._single_power_ai_merged = bool( - vc_config.get_bool("SINGLE_POWER_KEY_AI_MERGED", False) - ) or (ai_gpio is None) - except Exception: - self._single_power_ai_merged = ai_gpio is None - try: - _pwk_ms = int(vc_config.get_int("PWK_LONG_PRESS_SHUTDOWN_MS", 3000)) - except Exception: - _pwk_ms = 3000 - self._pwk_shutdown_ticks = max(1, (_pwk_ms + 99) // 100) - except Exception: - ai_gpio = "GPIO22" - pwk_gpio = "GPIO26" - bt_wakeup_en = False - wakeup_cta1_gpio = None - wakeup_bt_gpio = None - - if ai_gpio: - self.extint_AI = ExtInt( - getattr(ExtInt, ai_gpio), - ExtInt.IRQ_RISING_FALLING, - ExtInt.PULL_PU, - self.keyAI_extint_cb, - ) - self.extint_AI.enable() - else: - self.extint_AI = None - - self.pwk_key = ExtInt(getattr(ExtInt, pwk_gpio), ExtInt.IRQ_RISING_FALLING, ExtInt.PULL_PU, self.pwk_extint_cb) - - if bt_wakeup_en: - try: - if wakeup_cta1_gpio: - self.extint_wakeup_cta1 = ExtInt(getattr(ExtInt, wakeup_cta1_gpio), ExtInt.IRQ_RISING, ExtInt.PULL_DISABLE, self.wakeup_extint_cb) - if wakeup_bt_gpio: - self.PIN_wakeup_bt = Pin(getattr(Pin, wakeup_bt_gpio), Pin.OUT, Pin.PULL_DISABLE, 0) - self.extint_wakeup_cta1_timer = osTimer() - if hasattr(self, "extint_wakeup_cta1"): - self.extint_wakeup_cta1.enable() - except Exception: - pass - - self.pwk_key_status = 0 - self.pwk_key_timer_count = 0 - self.pwk_key_long_press = 0 - self.pwk_key_press = 0 - self.pwk_key_timer = osTimer() - self.pwk_is_double_timer = osTimer() - self.double_click_threshold = 1000#双击间隔时间 - self.pwk_press_count = 0#按下次数 - self.last_key_time = 0 # 上一次有效触发时间 - self.key_debounce_interval = 20 # 防抖窗口20ms,标准值 - #self.pwk_key = ExtInt(ExtInt.GPIO26, ExtInt.IRQ_RISING_FALLING, ExtInt.PULL_DISABLE, self.pwk_extint_cb) - - self.pwk_key.enable() - print("AI_dev:1") - self.btnInfo_queue = Queue(100) - self.btnInfo_queue_count = 0 - _thread.stack_size(16*1024) - self.btnInfo_queue_thread_id = _thread.start_new_thread(self.btnInfo_queue_thread, ()) - _thread.stack_size(8*1024) - EventMesh.subscribe("publish_AI_btnInfo",self.publish_AI_btnInfo) - EventMesh.subscribe("get_battery",self.get_battery) - EventMesh.subscribe("audio_montage_play",self.audio_montage_play) - # EventMesh.subscribe("paly_iccid_imei",self.paly_iccid_imei) - EventMesh.subscribe("paly_device_res_error",self.paly_device_res_error) - EventMesh.subscribe("Power_off",self.pwk_key_Long_Press) - EventMesh.subscribe("wakeup_BT",self.wakeup_BT) - EventMesh.subscribe("sleep_BT",self.sleep_BT) - EventMesh.subscribe("sys_sleep_cb",self.sys_sleep_cb)#系统休眠前回调 - EventMesh.subscribe("sys_up_cb",self.sys_up_cb)#系统唤醒后回调 - - EventMesh.subscribe("GET_USB_STATE",self.get_usb_state) - EventMesh.subscribe("SET_SPEECH_RATE",self.set_speech_rate) - EventMesh.subscribe("rest_TB",self.rest_TB) - - print("AI_dev:2") - self.pk = PowerKey() - self.pk.powerKeyEventRegister(self.powerKey_callback) - self.usb = USB() - self.usb.setCallback(self.usb_event_cb) - self.usb_status = self.usb.getStatus() - self.usb_charge_flag = 0 - self._usb_evt_last_ms = 0 - self._usb_evt_last_state = -1 - self.Low_power_warning_timer = 0 - self.AI_dev_timer = osTimer() - self.AI_dev_timer.start(10000, 1, self.AI_dev_timer_cb) - print("AI_dev:AI_dev_timer") - self.aud = audio.Audio(0) - self.Low_power_warning_flag = 0 - self._low_warn_last_ms = 0 - self._empty_shutdown_done = 0 - self.VC_TTS_volume = EventMesh.publish("get_storage_config","VC_TTS_volume")#音量配置 - if self.VC_TTS_volume == None: - self.VC_TTS_volume = 100 - self.aud.setVolume(self.VC_TTS_volume //10) - EventMesh.publish("set_storage_config",["VC_TTS_volume",self.VC_TTS_volume]) - #self.aud.play(1,0,'U:/static/xianyikaiji.mp3')#开机音频 - #EventMesh.publish("audio_montage_play",["xianyikaiji.mp3"]) - #EventMesh.publish("publish_AI_btnInfo",{"func":self.ntptime_cb}) - self.check_rest_timer_count = 0 - try: - if "vc_config" in globals() and vc_config and vc_config.uart_supported(): - self.ai_uart = AI_UART() - except Exception: - pass - - self.Keycall1_press_count = 0 - self.ble_switch_flag_time = osTimer() - self.ble_switch_flag_time.start(20000, 0, self.ble_switch_flag_time_init) - # AI_hw_status 先于本模块初始化,开机已插 USB 时需补同步一次 - try: - if int(self.usb_status or 0) == 1: - EventMesh.publish("hw_usb_state", 1) - EventMesh.publish("publish_AI_btnInfo", {"func": self.usb_event, "data": 1}) - except Exception: - pass - def ble_switch_flag_time_init(self,*args): - EventMesh.publish("publish_AI_btnInfo",{"func":self.ble_switch_flag_time_cb}) - def ble_switch_flag_time_cb(self,*args): - bt_switch_flag = EventMesh.publish("get_storage_config","switch_bt_state")#bt开关 - if bt_switch_flag == None: - bt_switch_flag = "1" - EventMesh.publish("send_bt_switch_toble",str(bt_switch_flag)) - # EventMesh.publish("send_imei_to_ble") - ble_switch_flag = EventMesh.publish("get_storage_config","switch_ble_state")#ble开关 - if ble_switch_flag == None: - ble_switch_flag = "1" - EventMesh.publish("send_ble_switch_toble",str(ble_switch_flag)) - vc_tts_volume = EventMesh.publish("get_storage_config","VC_TTS_volume") - if vc_tts_volume == None: - vc_tts_volume=100 - # EventMesh.publish("adjust_volume_toBle",str(vc_tts_volume)) - - pass - print("ble_switch_flag_time_cb!!!!!!!") - def ntptime_cb(self): - #ntptime.settime(8) - print("timer:{}!!!!!!!!\r\n".format(utime.localtime())) - def usb_event_cb(self, state): - # USB 插拔抖动过滤(日志中 powerKey 与 usb_evt 0/1 连跳会导致误播 OUT/IN) - try: - now = utime.ticks_ms() - last_ms = getattr(self, "_usb_evt_last_ms", 0) - last_st = getattr(self, "_usb_evt_last_state", -1) - if last_ms and utime.ticks_diff(now, last_ms) < 800 and last_st != state: - return - self._usb_evt_last_ms = now - self._usb_evt_last_state = state - except Exception: - pass - # 转发给精简硬件监控(AI_hw_status),避免 USB callback 被覆盖导致“后面失效” - try: - EventMesh.publish("hw_usb_state", state) - except Exception: - pass - EventMesh.publish("publish_AI_btnInfo",{"func":self.usb_event,"data":state}) - - def usb_event(self, state): - status = state - self.usb_charge_flag = 0 - if status == 1:#插入USB - print("charge R_CHARGE_in") - self.usb_status = 1 - VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag")#获取vc是否在运行 - if VC_satrt_flag == None: - VC_satrt_flag = 0 - - voice_record_satrt_flag = EventMesh.publish("get_voice_record_flag")#获取静默或者语音留言是否在运行 - if voice_record_satrt_flag == None: - voice_record_satrt_flag = 0 - if VC_satrt_flag == 0 and voice_record_satrt_flag == 0: - self.aud.stop() - #EventMesh.publish("flicker_led_red")#充电闪灯 - #self.aud.play(1,1,'U:/static/R_CHARGE_IN.mp3') - try: - if not ("vc_config" in globals() and vc_config and vc_config.get_bool("USE_MIN_HW_STATUS", True)): - EventMesh.publish("set_led_state", {"charging": "charging"}) - except Exception: - pass - EventMesh.publish("audio_montage_play",["R_CHARGE_IN.mp3"]) - self.Low_power_warning_flag = 0 - try: - self._empty_shutdown_done = 0 - except Exception: - pass - pass - elif status == 0:#拨出usbs - self.usb_status = 0 - VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag")#获取vc是否在运行 - if VC_satrt_flag == None: - VC_satrt_flag = 0 - voice_record_satrt_flag = EventMesh.publish("get_voice_record_flag")#获取静默或者语音留言是否在运行 - if voice_record_satrt_flag == None: - voice_record_satrt_flag = 0 - if VC_satrt_flag == 0 and voice_record_satrt_flag == 0: - self.aud.stop() - try: - if not ("vc_config" in globals() and vc_config and vc_config.get_bool("USE_MIN_HW_STATUS", True)): - EventMesh.publish("set_led_state", {"charging": "none"}) - except Exception: - pass - EventMesh.publish("audio_montage_play",["R_CHARGE_OUT.mp3"]) - #self.aud.play(1,1,'U:/static/R_CHARGE_OUT.mp3') - def get_usb_state(self,event,msg): - return self.usb_status - - def set_speech_rate(self,event,msg): - #msg的取值是0.5~2.0 - #而语速初始值是0,范围是-50~50 - rate = 0 - if msg == 0.5: - rate = -50 - elif msg < 1: - rate = (msg - 1)* 100 - elif msg >= 1: - rate = (msg - 1)* 50 - rate = round(rate) - print("速率已经设置为:{}".format(rate)) - EventMesh.publish("rate_change",rate) - return rate - def btnInfo_queue_thread(self): - while True: - data = self.btnInfo_queue.get() - self.btnInfo_queue_count-=1 - #EventMesh.debug_log("Info_queue:data = {}".format(data)) - if data != None: - try: - if "data" in data : - data["func"](data["data"]) - else : - data["func"]() - except Exception as e: - #EventMesh.debug_log("btnInfo_queue:ERROR:{}".format(e)) - if "data" in data : - print(data["data"]) - usys.print_exception(e) - def publish_AI_btnInfo(self,event,msg): - #print("publish_AI_btnInfo 1") - self.btnInfo_queue.put(msg) - self.btnInfo_queue_count+=1 - if self.btnInfo_queue_count > 80: - self.btnInfo_queue_count =0 - print("事件处理线程队列出问题,重启!!!!!!!!!!!!!!") - print("线程:{} 队列:{}".format(_thread.threadIsRunning(self.btnInfo_queue_thread_id),self.btnInfo_queue.size())) - # if _thread.threadIsRunning(self.btnInfo_queue_thread_id): - # _thread.stop_thread(self.btnInfo_queue_thread_id) - # _thread.stack_size(10*1024) - # self.btnInfo_queue_thread_id = _thread.start_new_thread(self.btnInfo_queue_thread, ()) - # _thread.stack_size(8*1024) - # self.btnInfo_queue.clear() - # self.btnInfo_queue = Queue(100) - EventMesh.publish("VC_error") #播放报错 - utime.sleep_ms(1000) - self.aud.stopPlayStream() - self.aud.stopAll() - Power.powerRestart() - EventMesh.publish("set_storage_config",["powerRestart",1]) - #print("publish_AI_btnInfo 2") - - def sys_sleep_cb(self,event=None,msg=None):#休眠前回调 - try: - if not ("vc_config" in globals() and vc_config and vc_config.get_bool("BT_WAKEUP_4G_ENABLED", False)): - return - except Exception: - return - print("sys_sleep_cb!!!!!!!!!!!!!!!") - EventMesh.publish("sleep_BT")#休眠蓝牙 - self.PIN_wakeup_bt.write(0) - def sys_up_cb(self,event=None,msg=None):#唤醒前回调 - try: - if not ("vc_config" in globals() and vc_config and vc_config.get_bool("BT_WAKEUP_4G_ENABLED", False)): - return - except Exception: - return - print("sys_up_cb!!!!!!!!!!!!!!!") - self.wakeup_BT() - self.PIN_wakeup_bt.write(1) - def rest_TB(self,event=None,msg=None): - print("rest_TB!!!!!!!!!!!!!!!") - self.PIN_BT_ON.write(0) - utime.sleep_ms(100) - self.PIN_BT_ON.write(1) - def wakeup_BT(self,event=None,msg=None):#4G唤醒蓝牙 - try: - if not ("vc_config" in globals() and vc_config and vc_config.get_bool("BT_WAKEUP_4G_ENABLED", False)): - return - except Exception: - return - print("wakeup_BT!!!!!!!!!!!!!!!") - wakeup_cta1_io = self.extint_wakeup_cta1.read_level() - if wakeup_cta1_io == 1: - return 1 - self.PIN_wakeup_bt.write(0) - count = 5 - while(True): - count-=1 - if count<=0: - return -1 - wakeup_cta1_io = self.extint_wakeup_cta1.read_level() - print("bt io状态:{}".format(wakeup_cta1_io)) - if wakeup_cta1_io == 1: - self.PIN_wakeup_bt.write(1) - return 1 - utime.sleep_ms(100) - if count%2 == 0: - self.PIN_wakeup_bt.write(1) - else: - self.PIN_wakeup_bt.write(0) - - - def sleep_BT(self,event=None,msg=None):#使蓝牙休眠 - try: - if not ("vc_config" in globals() and vc_config and vc_config.get_bool("BT_WAKEUP_4G_ENABLED", False)): - return - except Exception: - return - print("sleep_BT!!!!!") - print("sleep_BT!!!!!") - print("sleep_BT!!!!!") - print("sleep_BT!!!!!") - self.PIN_wakeup_bt.write(0) - count = 300 - while(True): - count-=1 - if count<=0: - return -1 - if self.extint_wakeup_cta1.read_level() == 0: - return 1 - utime.sleep_ms(1) - - - def wakeup_extint_cb(self,args):#蓝牙唤醒4g - try: - if not ("vc_config" in globals() and vc_config and vc_config.get_bool("BT_WAKEUP_4G_ENABLED", False)): - return - except Exception: - return - print("wakeup_extint_cb:!!!!!!!!!") - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - self.PIN_wakeup_bt.write(1) - EventMesh.publish("publish_AI_btnInfo",{"func":self.extint_wakeup_cta1_cb}) - def extint_wakeup_cta1_cb(self): - #self.extint_wakeup_cta1_timer.start(100, 1, self.extint_wakeup_cta1_timer_cb) - pass - def extint_wakeup_cta1_timer_cb(self, *args, **kwargs): - if self.extint_wakeup_cta1.read_level() == 0: - self.extint_wakeup_cta1_timer.stop() - #self.PIN_wakeup_bt.write(0) - - def powerKey_callback(self, status): - print("powerKey_callback:{}".format(status)) - - - def pwk_extint_cb(self,args):#pwk - current_time = 0 - interval = 0 - - try: - #按键电平判断 - press_level = 0 - current_level = self.pwk_key.read_level() - if current_level != press_level: - return # 非按下状态,直接返回 - - #消抖处理 - current_time = utime.ticks_ms() - if utime.ticks_diff(current_time, self.last_key_time) < self.key_debounce_interval: - return - self.last_key_time = current_time # 更新最后按键时间 - print("pwk 按键按下") - - #仅处理"按下瞬间"事件(避免重复触发) - if len(args) < 2: - print("args参数不足,跳过") - return - self.pwk_key_status = int(args[1]) - if self.pwk_key_status != 1: - return - - #双击计数逻辑 - self.pwk_press_count += 1 - current_time = utime.ticks_ms() - - #第一次按下(启动长按定时器+双击等待) - if self.pwk_press_count == 1: - self.pwk_first_press_time = current_time # 记录首次按下时间 - self.pwk_key_long_press = 0 # 重置长按标记 - self.pwk_key_timer_count = 0 # 重置定时器计数 - self.pwk_key_press = 1 # 标记按键按下 - - # 停止旧定时器,启动长按/单击判定定时器(100ms单次触发,循环靠回调自启) - self.pwk_key_timer.stop() - self.pwk_key_timer.start(100, 1, self.pwk_key_timer_cb) - print("第一次按下,等待第二次按下") - if getattr(self, "_single_power_ai_merged", False): - self._merged_pwk_ai_press_start() - - #第二次按下(双击判断) - elif self.pwk_press_count == 2: - # 计算两次按下间隔 - interval = utime.ticks_diff(current_time, self.pwk_first_press_time) - - #符合双击阈值:执行双击逻辑 - if interval <= self.double_click_threshold: - self.pwk_key_timer.stop() # 停止长按定时器(关键:避免触发长按) - self.pwk_key_double_press() # 执行双击业务 - print("双击生效") - # 重置所有状态 - self.pwk_press_count = 0 - self.pwk_key_press = 0 - self.pwk_key_timer_count = 0 - - #间隔过长:视为新的第一次按下 - else: - self.pwk_first_press_time = current_time # 更新首次按下时间 - self.pwk_press_count = 1 # 重置为第一次按下 - print("间隔过长,视为新的第一次按下") - - #按下次数超过2次(防计数溢出) - else: - self.pwk_press_count = 0 - self.pwk_key_timer.stop() - print("按键次数过多,重置计数") - - except Exception as e: - # 异常时重置所有状态,避免卡死 - self.pwk_press_count = 0 - self.pwk_key_press = 0 - self.pwk_key_timer_count = 0 - self.pwk_key_timer.stop() - usys.print_exception(e) - - - def pwk_is_double_judge(self, *args): - # if self.press_count == 1: # 仅一次按下,判定为单击 - - # 重置计数(无论何种情况,超时后清零) - self.pwk_press_count = 0 - - - def pwk_key_timer_cb(self, *args, **kwargs): - EventMesh.publish("set_wait_first_flag",0) - EventMesh.publish("set_history_board_flag",0) - EventMesh.publish("set_play_new_voice_state",0) - EventMesh.publish("set_history_voice_flag",0) - EventMesh.publish("set_wait_voice_flag",0) - EventMesh.publish("set_stop_play",0) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.pwk_key_timer_cb_}) - # def pwk_key_timer_cb_(self) : - try: - self.pwk_key_status = self.pwk_key.read_level() - self.pwk_key_timer_count +=1 - if self.pwk_key_timer_count >= self._pwk_shutdown_ticks: - #print("pwk_key_long_press {}".format(self.pwk_key_long_press)) - if self.pwk_key_long_press == 0: - self.pwk_key_long_press = 1 - #EventMesh.publish("AI_key_long_press") - # EventMesh.publish("audio_montage_play",["zhengzaiguanji.mp3"]) - self.aud.play(1,0,'U:/static/zhengzaiguanji.mp3')#关机音频 - EventMesh.publish("set_led_state", {"battery": "empty"}) - utime.sleep_ms(2000) - self.aud.stopPlayStream() - self.aud.stopAll() - Power.powerDown() - #print("AI_key_release") - #EventMesh.publish("pwk_key_long_press") - #EventMesh.publish("publish_AI_btnInfo",{"func":self.pwk_key_Long_Press}) - if self.pwk_key_status == 1:#松开 - #print("pwk_key_timer_cb:{}".format(self.pwk_key_timer_count)) - try: - key_count = vc_config.get_int("KEY_RELEASE_DEBOUNCE_COUNT", 5) - except Exception: - key_count = 5 - if self.pwk_key_timer_count >= key_count: #防抖200ms - #EventMesh.publish("AI_key_release") - EventMesh.publish("publish_AI_btnInfo",{"func":self.pwk_key_release}) - self.pwk_key.enable() - self.pwk_key_timer_count = 0 - self.pwk_key_press = 0 - self.pwk_press_count = 0#按下次数 - self.pwk_key_timer.stop() - - except Exception as e: - usys.print_exception(e) - - def _ai_int_disable_for_hold(self): - if self.extint_AI is not None: - self.extint_AI.disable() - - def _ai_int_enable_after_hold(self): - if self.extint_AI is not None: - self.extint_AI.enable() - - def _key_ai_read_level(self): - if self.extint_AI is not None: - return self.extint_AI.read_level() - return self.pwk_key.read_level() - - def _merged_pwk_ai_press_start(self): - """单电源键:按下瞬间走原 AI 键按下链路(松手走 AI_key_release,不按电源短按 Close)。""" - try: - current_time = utime.ticks_ms() - if utime.ticks_diff(current_time, self.last_ai_key_time) < 1: - return - self.last_ai_key_time = current_time - if EventMesh.publish("get_wait_voice_flag") == 1: - print("播放未读") - EventMesh.publish("set_wait_voicerupt_flag", 1) - return - if EventMesh.publish("get_recv_voice") == 1: - print("收到留言") - EventMesh.publish("set_voicerupt_flag", 1) - return - if self.KeyAI_press == 0: - EventMesh.publish(constants.EVENT.SYSTEM_UP) - if EventMesh.publish("get_play_new_voice_state") == 1: - EventMesh.publish("set_play_new_voice_state", 0) - EventMesh.publish("set_VC_stop_play_audio_flag", 1) - self._ai_int_disable_for_hold() - self.KeyAI_press = 1 - self.KeyAI_long_press = 0 - self.KeyAI_timer.start(100, 1, self.KeyAI_timer_cb) - self.KeyAI_timer_count = 0 - EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_key_press}) - except Exception as e: - usys.print_exception(e) - - def keyAI_extint_cb(self,args):#call 2 - try: - if self.extint_AI is None: - return - #print("AI_btn:keyAI_extint_cb{}".format(args[1])) - #定义最短触发间隔 - MIN_TRIGGER_INTERVAL = 1 # 可根据需求调整,单位ms - #记录上次按键触发时间(初始化放类的__init__里:self.last_ai_key_time = 0) - current_time = utime.ticks_ms() # 获取当前毫秒时间 - #重复按下,直接返回 - if utime.ticks_diff(current_time, self.last_ai_key_time) < MIN_TRIGGER_INTERVAL: - print("AI按键触发过频,忽略本次操作") - return - #更新上次触发时间 - self.last_ai_key_time = current_time - - self.KeyAI_status = int(args[1]) - if self.KeyAI_status == 0: - self.KeyAI_status = 1 - else: - self.KeyAI_status = 0 - - if self.KeyAI_status == 0:#按下 - if EventMesh.publish("get_wait_voice_flag") == 1: - print("播放未读") - EventMesh.publish("set_wait_voicerupt_flag",1) - return - if EventMesh.publish("get_recv_voice") == 1:#当处于收到留言的时候 - print("收到留言") - EventMesh.publish("set_voicerupt_flag",1) - return - if self.KeyAI_press == 0 : - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - if EventMesh.publish("get_play_new_voice_state") == 1: - EventMesh.publish("set_play_new_voice_state",0) - - EventMesh.publish("set_VC_stop_play_audio_flag",1)#按键按下不允许播放之前下发的音频数据 - self._ai_int_disable_for_hold() - self.KeyAI_press = 1 - self.KeyAI_long_press = 0 - self.KeyAI_timer.start(100, 1, self.KeyAI_timer_cb) - self.KeyAI_timer_count = 0 - #EventMesh.publish("AI_key_press") - # if dev_type == "7PIN": - # self.aud.aud_tone_play(10,100)#播放按键音 - - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_key_press}) - except Exception as e: - usys.print_exception(e) - def KeyAI_timer_cb(self, *args, **kwargs): - # EventMesh.publish("publish_AI_btnInfo",{"func":self.KeyAI_timer_cb_}) - # def KeyAI_timer_cb_(self): - try: - self.KeyAI_status = self._key_ai_read_level() - self.KeyAI_timer_count +=1 - if self.KeyAI_timer_count >= 10*1 : #长按1s - - if self.KeyAI_long_press == 0: - self.KeyAI_long_press = 1 - #EventMesh.publish("AI_key_long_press") - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_key_long_press}) - try: - _sos_on = vc_config.get_bool("SOS_ENABLED", True) - except Exception: - _sos_on = True - if _sos_on and self.KeyAI_timer_count >= 10*3 : #长按3s 上一次是短按则拨打sos紧急联系人 - print("long sos phone:{}".format(self.KeySOS_timer_flag)) - - if self.KeySOS_timer_flag == 1: - self.KeySOS_timer_flag=0 - self.KeySos_long_press = 1 - self.KeySOS_timer.stop() - get_sos_flag = EventMesh.publish("get_sos_flag") - if get_sos_flag == 0: - EventMesh.publish("set_sos_info_call") - pass - - elif self.KeyAI_timer_count >= 10*8:#关机 - try: - allow_power_off = vc_config.get_bool("AI_LONG_PRESS_POWER_OFF_ENABLED", False) - except Exception: - allow_power_off = False - if allow_power_off: - EventMesh.publish("audio_montage_play",["zhengzaiguanji.mp3"]) - #self.aud.play(1,0,'U:/static/zhengzaiguanji.mp3')#关机音频 - EventMesh.publish("set_led_state", {"battery": "empty"}) - utime.sleep_ms(2000) - self.aud.stopPlayStream() - print("stopAll:8") - self.aud.stopAll() - Power.powerDown() - if self.KeyAI_status == 1:#松开 - #print("KeyAI_timer_cb:{}".format(self.KeyAI_timer_count)) - try: - key_count = vc_config.get_int("KEY_RELEASE_DEBOUNCE_COUNT", 5) - except Exception: - key_count = 5 - if self.KeyAI_timer_count >= key_count: #防抖200ms - if self.KeySOS_timer_flag == 1: - self.KeySOS_timer_flag = 0 - self.KeySos_long_press = 0 - self.KeySOS_timer.stop() - print("KeySOS_timer_flag!!!!!!!!!!!!!!!") #短按两次需要清除标识 - - if self.KeyAI_long_press == 0: - try: - _sos_on = vc_config.get_bool("SOS_ENABLED", True) - except Exception: - _sos_on = True - if _sos_on and self.KeySOS_timer_flag == 0: - self.KeySOS_timer_flag = 1 - self.KeySos_long_press = 0 - self.KeySOS_timer.start(5000, 0, self.KeySos_timer_cb) #短按开启5s定时器 5s之内没有长按3s则清除标识 长按3s拨打sos紧急联系人 - else: - self.KeySOS_timer_flag = 0 - self.KeySos_long_press = 0 - self.KeySOS_timer.stop() - - #EventMesh.publish("AI_key_release") - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_key_release}) - self._ai_int_enable_after_hold() - self.KeyAI_timer.stop() - self.KeyAI_timer_count = 0 - self.KeyAI_press = 0 - self.KeyAI_long_press=0 - - except Exception as e: - usys.print_exception(e) - - def AI_key_press(self): - # EventMesh.publish("wakeup_BT") - - # if dev_type == "7PIN" or dev_type == "7PIN_coze" : - # # self.aud.aud_tone_play(10,60)#播放按键音 - # EventMesh.publish("audio_montage_play",["chenggong.mp3"]) - self.check_rest_timer_count = 0 - print("AI_key_press") - EventMesh.publish("AI_key_press") - def KeySos_timer_cb(self,*argc): - print("KeySos_timer_cb!!!!!!!!!!{} {}".format(self.KeySos_long_press,self.KeySOS_timer_flag)) - - if self.KeySos_long_press == 1 and self.KeySOS_timer_flag == 1: #清空sos标识 - self.KeySos_long_press=0 - self.KeySOS_timer_flag=0 - - self.KeySos_long_press=0 - self.KeySOS_timer_flag = 0 - return - self.KeySOS_timer_flag = 0 - if self.KeySOS_timer_count >= 3: - self.KeySOS_timer_count = 0 - get_sos_flag = EventMesh.publish("get_sos_flag") - if get_sos_flag == 0: - EventMesh.publish("set_sos_info_call") - self.KeySOS_timer_count = 0 - pass - - def AI_key_long_press(self): - self.check_rest_timer_count = 0 - print("AI_key_long_press") - - EventMesh.publish("AI_key_long_press") - - def AI_key_release(self): - print("AI_key_release") - # self.KeySOS_timer_count +=1 - # print("self.KeySOS_timer_count:{} {}".format(self.KeySOS_timer_count,self.KeySOS_timer_flag)) - # if self.KeySOS_timer_flag == 0: - # self.KeySOS_timer_flag = 1 - # self.KeySOS_timer.start(5000, 0, self.KeySos_timer_cb) - EventMesh.publish("AI_key_release") - - def pwk_key_double_press(self): - self.check_rest_timer_count = 0 - try: - if getattr(self, "_single_power_ai_merged", False): - try: - self.KeyAI_timer.stop() - except Exception: - pass - self.KeyAI_timer_count = 0 - self.KeyAI_press = 0 - self.KeyAI_long_press = 0 - self._ai_int_enable_after_hold() - except Exception: - pass - print("pwk_key_double_press") - EventMesh.publish("pwk_key_double_press") - - def pwk_key_Long_Press(self,event=None,msg=None): - self.check_rest_timer_count = 0 - print("pwk_key_long_press!!!!!!!") - print("pwk_key_long_press!!!!!!!") - - # EventMesh.publish("paly_iccid_imei") - # return - EventMesh.publish("audio_montage_play",["zhengzaiguanji.mp3"]) - #self.aud.play(1,0,'U:/static/zhengzaiguanji.mp3')#关机音频 - EventMesh.publish("set_led_state", {"battery": "empty"}) - utime.sleep_ms(3000) - self.aud.stopPlayStream() - self.aud.stopAll() - Power.powerDown() - #print("AI_key_release") - #EventMesh.publish("pwk_key_long_press") - - def pwk_key_release(self): - self.check_rest_timer_count = 0 - #print("pwk_key_release") - EventMesh.publish("pwk_key_release") - - - def wdt_timer_cb(self, *args, **kwargs): - if self.is_wdt == 1: - self.wdt.feed() - - def AI_dev_timer_cb(self, *args, **kwargs): - #print("AI_dev_timer_cb") - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_dev_timer_event}) - def _battery_empty_shutdown_seq(self): - """REQ-BAT-03:红快闪 3 次 → 灭灯 → 关机提示音 → 关机(btn 线程执行)。""" - try: - EventMesh.publish("battery_priority_mute_vc") - except Exception: - pass - try: - n, ms = 3, 500 - if "vc_config" in globals() and vc_config: - n = int(vc_config.get_int("BATTERY_EMPTY_FLASH_COUNT", 3)) - ms = int(vc_config.get_int("BATTERY_EMPTY_FLASH_PERIOD_MS", 500)) - EventMesh.publish("set_led_state", {"alert": "red_flash_3"}) - utime.sleep_ms(n * ms * 2 + 200) - EventMesh.publish("set_led_state", {"ai_state": "idle", "battery": "normal", "charging": "none"}) - except Exception: - pass - try: - EventMesh.publish("audio_montage_play", ["didianguanji.mp3"]) - utime.sleep_ms(4500) - self.aud.stopPlayStream() - self.aud.stopAll() - except Exception: - pass - try: - Power.powerDown() - except Exception: - pass - - def AI_dev_timer_event(self): - #EventMesh.publish("audio_montage_play",["liuliangtishi.mp3"]) - EventMesh.publish("send_power_init_data") - if self.is_wdt == 1: - self.wdt.feed() - VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag")#获取vc是否在运行 - if VC_satrt_flag == None: - VC_satrt_flag = 0 - voice_record_satrt_flag = EventMesh.publish("get_voice_record_flag")#获取静默或者语音留言是否在运行 - if voice_record_satrt_flag == None: - voice_record_satrt_flag = 0 - Vbatt = Power.getVbatt() - if self.usb_status == 1 and VC_satrt_flag == 0 and voice_record_satrt_flag == 0: - try: - if not ("vc_config" in globals() and vc_config and vc_config.get_bool("USE_MIN_HW_STATUS", True)): - EventMesh.publish("set_led_state", {"charging": "charging"})#充电红灯常亮 - except Exception: - pass - _bcfg = _BatteryCfg.instance() - if self.usb_status == 1 and Vbatt >= _bcfg.charge_full_mv and VC_satrt_flag == 0 and voice_record_satrt_flag == 0: - try: - if not ("vc_config" in globals() and vc_config and vc_config.get_bool("USE_MIN_HW_STATUS", True)): - EventMesh.publish("set_led_state", {"charging": "full"}) - except Exception: - pass - if self.usb_charge_flag == 0 : - EventMesh.publish("audio_montage_play",["R_CHARGE_FULL.mp3"]) - #self.aud.play(1,0,'U:/static/R_CHARGE_FULL.mp3')#充电已满 - self.usb_charge_flag = 1 - try: - low_soc = int(_bcfg.low_soc) - empty_soc = int(_bcfg.empty_soc) - except Exception: - low_soc, empty_soc = 30, 10 - warn_interval_ms = 300000 - try: - if "vc_config" in globals() and vc_config: - warn_interval_ms = int(vc_config.get_int("BATTERY_LOW_WARN_INTERVAL_MS", 300000)) - except Exception: - pass - soc = None - try: - soc = self.get_battery() - except Exception: - soc = None - if self.usb_status == 0 and soc is not None: - empty_by_soc = soc < empty_soc - empty_by_mv = Vbatt is not None and Vbatt < int(_bcfg.empty_mv) - if empty_by_soc or empty_by_mv: - if getattr(self, "_empty_shutdown_done", 0) == 0: - self._empty_shutdown_done = 1 - try: - print("[BAT] empty shutdown soc={} vbatt={}".format(soc, Vbatt)) - except Exception: - pass - EventMesh.publish("publish_AI_btnInfo", {"func": self._battery_empty_shutdown_seq}) - elif soc < low_soc: - if voice_record_satrt_flag == 0: - try: - now = utime.ticks_ms() - last = getattr(self, "_low_warn_last_ms", 0) - if (not last) or utime.ticks_diff(now, last) >= warn_interval_ms: - self._low_warn_last_ms = now - try: - print("[BAT] low warn soc={} interval_ms={}".format(soc, warn_interval_ms)) - except Exception: - pass - EventMesh.publish("audio_montage_play", ["Low_power_warning.mp3"]) - except Exception: - pass - else: - try: - self._low_warn_last_ms = 0 - self._empty_shutdown_done = 0 - except Exception: - pass - if VC_satrt_flag == 0 and Vbatt is not None and Vbatt >= int(_bcfg.low_mv): - try: - if not ("vc_config" in globals() and vc_config and vc_config.get_bool("USE_MIN_HW_STATUS", True)): - EventMesh.publish("set_led_state", {"charging": "none", "battery": "normal"}) - except Exception: - pass - - self.check_rest_timer_count += 1 - if self.check_rest_timer_count >= 8640:#24小时没按键,则关机 - self.pwk_key_Long_Press() - - def get_battery(self, event=None, msg=None): - # 返回 int 型 SoC(0~100):便于 TTS/上报/比较,且 OCV 插值本身比裸电压稳;不做小数避免频繁跳变 - try: - Vbatt = Power.getVbatt() - except Exception: - return 0 - soc = _BatteryCfg.instance().soc_from_vbatt(Vbatt) - if soc is None: - return 0 - if soc < 0: - return 0 - if soc > 100: - return 100 - return int(soc) - def _audio_is_battery_priority(self, msg): - try: - for i in (msg or []): - n = str(i) - if ("Low_power" in n) or ("didianguanji" in n): - return True - except Exception: - pass - return False - - def audio_montage_play(self,event,msg): - self.wakeup_BT() - bat_pri = self._audio_is_battery_priority(msg) - if bat_pri: - try: - EventMesh.publish("battery_priority_mute_vc") - except Exception: - pass - else: - # 仅服务端 TTS PCM 在播时跳过;低电/关机提示优先级更高(见上) - try: - if EventMesh.publish("get_vc_tts_playback_active") == 1: - print("[audio-arb] skip local montage_play during tts_pcm") - return - except Exception: - pass - self.aud.stopPlayStream() - self.aud.stopAll() - str_data='mp3files=' - for i in msg: - try: - str_tmp = i.replace(".mp3","") - str_data+="{}.mp3+".format(audio_buf.index(str_tmp)) - except Exception as e: - usys.print_exception(e) - str_data = str_data[:-1] - print("audio_montage_play:{}".format(str_data)) - self.aud.montage_play('U:/static/audio.bin',1,1,str_data) - self.play_imei_timer_count=1 - self.play_imei_timer.stop() - self.play_imei_timer.start(1000,1,self.play_imei_timer_cb) - - def paly_iccid_imei(self,event,msg): - - self.wakeup_BT() - self.aud.stopPlayStream() - self.aud.stopAll() - str_data = 'mp3files={}.mp3+'.format(audio_buf.index("liuliangtishi"))#流量提示 - imei = modem.getDevImei() - iccid = sim.getIccid() - print("paly_iccid_imei:ICCID:{}".format(iccid)) - if iccid != -1 and len(iccid) > 5: - iccid = iccid.upper() - if len(iccid) <20: - for i in range(20-len(iccid)): - iccid = "0"+iccid - str_data += '{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+'.format(audio_buf.index("R_READ_TIME_{}".format(iccid[0]))\ - ,audio_buf.index("R_READ_TIME_{}".format(iccid[1])),audio_buf.index("R_READ_TIME_{}".format(iccid[2])),audio_buf.index("R_READ_TIME_{}".format(iccid[3])),\ - audio_buf.index("R_READ_TIME_{}".format(iccid[4])),audio_buf.index("R_READ_TIME_{}".format(iccid[5])),audio_buf.index("R_READ_TIME_{}".format(iccid[6])),audio_buf.index("R_READ_TIME_{}".format(iccid[7])),\ - audio_buf.index("R_READ_TIME_{}".format(iccid[8])),audio_buf.index("R_READ_TIME_{}".format(iccid[9])),audio_buf.index("R_READ_TIME_{}".format(iccid[10])),audio_buf.index("R_READ_TIME_{}".format(iccid[11])),\ - audio_buf.index("R_READ_TIME_{}".format(iccid[12])),audio_buf.index("R_READ_TIME_{}".format(iccid[13])),audio_buf.index("R_READ_TIME_{}".format(iccid[14])),audio_buf.index("R_READ_TIME_{}".format(iccid[15])),\ - audio_buf.index("R_READ_TIME_{}".format(iccid[16])),audio_buf.index("R_READ_TIME_{}".format(iccid[17])),audio_buf.index("R_READ_TIME_{}".format(iccid[18])),audio_buf.index("R_READ_TIME_{}".format(iccid[19]))) - str_data += '{}.mp3+'.format(audio_buf.index("shouhou"))#可联系售后解决,联系方式见售后客服电话是400-886-8777或到微信公众号柒牌SEVENBRAND自行充值: - self.aud.montage_play('U:/static/audio.bin',1,1,str_data) - #开定时器不让系统休眠 - self.play_imei_timer_count=1 - self.play_imei_timer.stop() - self.play_imei_timer.start(1000,1,self.play_imei_timer_cb) - - def play_imei_timer_cb(self,event=None,msg=None): - if self.play_imei_timer_count == 1: - if EventMesh.publish("get_audio_play_state") == 0: - print("音频文件播放完成") - self.play_imei_timer_count=0 - self.play_imei_timer.stop() - return - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - - #鉴权出错,播放imei号 - def paly_device_res_error(self,event,msg): - self.wakeup_BT() - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - str_data = 'mp3files={}.mp3+'.format(audio_buf.index("jianquancuowu"))#AI功能异常,请联系客户查询,设备号为: - imei = modem.getDevImei() - imei = imei.upper() - str_data += '{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3'.format(audio_buf.index("R_READ_TIME_{}".format(imei[0]))\ - ,audio_buf.index("R_READ_TIME_{}".format(imei[1])),audio_buf.index("R_READ_TIME_{}".format(imei[2])),audio_buf.index("R_READ_TIME_{}".format(imei[3])),\ - audio_buf.index("R_READ_TIME_{}".format(imei[4])),audio_buf.index("R_READ_TIME_{}".format(imei[5])),audio_buf.index("R_READ_TIME_{}".format(imei[6])),audio_buf.index("R_READ_TIME_{}".format(imei[7])),\ - audio_buf.index("R_READ_TIME_{}".format(imei[8])),audio_buf.index("R_READ_TIME_{}".format(imei[9])),audio_buf.index("R_READ_TIME_{}".format(imei[10])),audio_buf.index("R_READ_TIME_{}".format(imei[11])),\ - audio_buf.index("R_READ_TIME_{}".format(imei[12])),audio_buf.index("R_READ_TIME_{}".format(imei[13])),audio_buf.index("R_READ_TIME_{}".format(imei[14]))) - #print("str_data:{}".format(str_data)) - self.aud.montage_play('U:/static/audio.bin',1,1,str_data) - self.play_imei_timer_count=1 - self.play_imei_timer.stop() - self.play_imei_timer.start(1000,1,self.play_imei_timer_cb) - -#存储信息 -class getStorageDataManager(Abstract):#获取内存数据 - def __init__(self): - self.config_data = {} - check = ql_fs.path_exists("/usr/static/storage_file/config.json") - if check == True: - with open("/usr/static/storage_file/config.json","r") as fd: - self.config_data = ujson.load(fd) - else : - self.config_data = {'old_time': 0, 'flip_sw': 0} - pass - - def post_processor_after_instantiation(self): - EventMesh.subscribe("set_storage_data", self.set_storage_data) - EventMesh.subscribe("set_storage_config", self.set_storage_config) - EventMesh.subscribe("get_storage_data", self.get_storage_data) - EventMesh.subscribe("get_storage_config", self.get_storage_config) - - def set_storage_data(self, event, msg): - # EventMesh.debug_log("set_storage_data:{}".format(msg)) - if msg != None: - self.config_data = msg - with open("/usr/static/storage_file/config.json",'w+',encoding="utf-8") as fd: - ujson.dump(self.config_data,fd) - - - def set_storage_config(self,event,msg): - # EventMesh.debug_log("set_storage_key_data1:{}".format(msg)) - if len(msg) >= 2: - self.config_data[msg[0]]=msg[1] - # EventMesh.debug_log("set_storage_key_data2:{}".format(self.config_data[msg[0]])) - EventMesh.publish("publish_message_event_ui",{"func":self.set_storage_save,"data":0}) - def set_storage_save(self, data): - with open("/usr/static/storage_file/config.json",'w+',encoding="utf-8") as fd: - ujson.dump(self.config_data,fd) - def get_storage_data(self, event, msg): - # EventMesh.debug_log("get_storage_data:{}".format(self.config_data)) - return self.config_data - def get_storage_config(self,event,msg): - # EventMesh.debug_log("get_storage_key_data1:{}".format(msg)) - b = msg in self.config_data.keys() - if b == True: - # EventMesh.debug_log("get_storage_key_data2:{}".format(self.config_data[msg])) - return self.config_data[msg] - else : - return None - # try: - # return self.config_data[msg] - # except: - # EventMesh.debug_log("get_storage_key_data:none key:{}",msg) - # return None -#设备资源授权平台 -class device_res_author(Abstract): - - def __init__(self) : - print("device_res_author __init__ start !!!!!!!!!!!!!!!!!") - self.deviceId = modem.getDevImei()#"876226002022574" - self.SHA1_BLOCK_SIZE = 64 - self.PAD_BLOCK_SIZE = 16 - self.SHA256 = uhashlib.sha256 - self.pad = lambda s: s + chr(self.PAD_BLOCK_SIZE - len(s) % self.PAD_BLOCK_SIZE) * (self.PAD_BLOCK_SIZE - len(s) % self.PAD_BLOCK_SIZE) - # 除去补16字节的多余字符 - self.unpad = lambda s: s[:-s[-1]] - # 设备鉴权:优先 default_config / usr/default_config 的 PRODUCT_PID、PRODUCT_KEY - self.pid = "cqxJZi5CNgzjS3mX21Zw" - self.key = b"uKLrChwrxA1LklgBc3VwawYjUkbT3BgX" - try: - if "vc_config" in globals() and vc_config: - _pid = vc_config.get_attr("PRODUCT_PID", None) - if _pid: - self.pid = str(_pid) - _key = vc_config.get_attr("PRODUCT_KEY", None) - if _key is not None and _key != "": - self.key = _key if isinstance(_key, bytes) else str(_key).encode() - except Exception: - pass - self.dev_res_timer = osTimer() - self.dev_res_timer.start(20000, 1, self.dev_timer_cb) - self.dev_res_timerCount = 0 - self.ud_token_timer = osTimer() - self.ud_token_timer.start(10000, 1, self.ud_token_timer_cb) - self.ud_token_timer_Count = 0 - self.dev_token = None - self.res_error_count = 0 - self.ntptime_flag = 0 - EventMesh.subscribe("get_dev_token", self.get_dev_token) - EventMesh.subscribe("update_dev_token", self.update_dev_token) - EventMesh.subscribe("dev_active", self.dev_active) - EventMesh.subscribe("dev_sign_in", self.dev_sign_in) - EventMesh.subscribe("dev_NTP", self.dev_NTP) - EventMesh.subscribe("post_iccid_user", self.post_iccid_user) - # self.post_iccidd_token_timer = osTimer() - # self.post_iccidd_token/_timer.start(10000, 0, self.post_iccidd_token_timer_cb) - self.connect_web_flag = 0 - #EventMesh.publish_async("dev_NTP",thread_size = 8192) - - # count = 0 - # while True: - # if self.ntptime_flag == 1: - # break - # utime.sleep_ms(1) - # count+=1 - # if count>600: - # break - # EventMesh.publish_async("dev_active",thread_size = 8192) - # EventMesh.publish_async("dev_sign_in",thread_size = 8192) - - self.dev_NTP()#获取时间 - self.dev_active() #设备激活 - self.dev_sign_in() #设备登入 - - - print("device_res_author __init__ end !!!!!!!!!!!!!!!!!") - def post_iccidd_token_timer_cb(self,*argc): - EventMesh.publish("post_iccid_user") - - def get_dev_token(self, event, msg): - return self.dev_token - #网络校时 - def dev_NTP(self, event=None, msg=None): - response = None - try: - print("dev_NTP1") - url = constants.DMP_BASE + "/dev/time" - print("dev_NTP:{}".format(url)) - response = request.get(url,timeout=5) - data = response.json() - print("dev_NTP:{}".format(data)) - if data: - try: - timeSecond = data["data"]["timestamp"]//1000 - - next_alarm_time = utime.localtime(timeSecond) - print("timeSecond {} next_alarm_time {}".format(timeSecond,next_alarm_time)) - machine.RTC().datetime((next_alarm_time[0],next_alarm_time[1],next_alarm_time[2],next_alarm_time[6],next_alarm_time[3],next_alarm_time[4],next_alarm_time[5],0)) - self.ntptime_flag = 1 - except Exception as e: - usys.print_exception(e) - except Exception as e: - usys.print_exception(e) - if response != None: - response.close() - print("dev_NTP end") - def dev_timer_cb(self, *args): #设备资源管理鉴权平台定时器 - EventMesh.publish("publish_AI_btnInfo",{"func":self.dev_timer_cb_}) - - def dev_timer_cb_(self): - #if EventMesh.DEV_RES_MANAGE_AUTHEN_PLATFORM == 1: - try: - if self.dev_token == None: - if self.ntptime_flag == 0: - self.ntptime_flag = 1 - EventMesh.publish_async("dev_NTP",thread_size = 8192) - #EventMesh.publish("publish_AI_btnInfo",{"func":self.dev_NTP}) - print("timer:{}!!!!!!!!\r\n".format(utime.localtime())) - EventMesh.publish_async("dev_active",thread_size = 8192) - EventMesh.publish_async("dev_sign_in",thread_size = 8192) - # self.dev_active() #设备激活 - # self.dev_sign_in() #设备登入 - self.dev_res_timerCount+=1 - if self.dev_res_timerCount > 3: #开机一分钟内没有获取到密码和token 等下一次更新令牌在走登入 激活流程 - self.dev_res_timerCount=0 - self.dev_res_timer.stop() - except Exception as e: - usys.print_exception(e) - def ud_token_timer_cb(self, *args): - EventMesh.publish("publish_AI_btnInfo",{"func":self.ud_token_timer_cb_}) - - def ud_token_timer_cb_(self): - try: - #print("ud_token_timer_!!!!") - self.ud_token_timer_Count+=1 - if self.ud_token_timer_Count >= 720: #定时10*3000s时更新token - self.ud_token_timer_Count = 0 - EventMesh.publish_async("dev_sign_in",thread_size = 8192) - except Exception as e: - usys.print_exception(e) - def get_sim_Status(self): - return sim.getStatus() - def HMAC(self,k, m): - - KEY_BLOCK = k + (b'\0' * (self.SHA1_BLOCK_SIZE - len(k))) - KEY_INNER = bytes((x ^ 0x36) for x in KEY_BLOCK) - KEY_OUTER = bytes((x ^ 0x5C) for x in KEY_BLOCK) - inner_message = KEY_INNER + m - outer_message = KEY_OUTER + self.SHA256(inner_message).digest() - return self.SHA256(outer_message) - def get_hmac_sha256(self,k, m): - data = self.HMAC(k, m).digest() - # print("HMAC(k, m).digest():", data) - # print("HMAC(k, m).digest():", ubinascii.hexlify(data)) - hex_data = ubinascii.hexlify(data) - hex_data = hex_data.decode('ascii') - return hex_data - # 加密函数 - def aes_CBC_Encrypt(self,data, key, iv): # CBC模式的加密函数,data为明文,key为16字节密钥,iv为16字节的偏移量 - key = key.encode('utf-8') - iv = iv.encode('utf-8') # CBC 模式下的偏移量 - data = self.pad(data) # 补位 - data = data.encode('utf-8') - aes = ucryptolib.aes(key,ucryptolib.MODE_CBC,iv) - result = base64.b64encode(aes.encrypt(data)) - return str(result, 'utf-8') # 以字符串的形式返回 - - # 解密函数 - def aes_CBC_Decrypt(self,data, key, iv): # CBC模式的解密函数,data为密文,key为16字节密钥 - key = key.encode('utf-8') - iv = iv.encode('utf-8') - - aes = ucryptolib.aes(key,ucryptolib.MODE_CBC,iv) #CBC模式 - - # decrypt AES解密 B64decode为base64 转码 - result = aes.decrypt(base64.b64decode(data)) - result = self.unpad(result) # 除去补16字节的多余字符 - return str(result, 'utf-8') # 以字符串的形式返回 - #激活接口 没有密码走激活 - def dev_active(self, event=None, msg=None): - signal = net.csqQueryPoll() - if signal == -1 or self.get_sim_Status() != 1:#网络不正常 - print("dev_active 无 SIM卡!!!!!!!!\r\n") - return - print("dev_active 1\r\n") - # 签名内容: = deviceId+pid+timeStamp - dev_pass = "" - dev_pass = EventMesh.publish("get_storage_config","dev_pass") - print("dev_pass:{}\r\n".format(dev_pass)) - if dev_pass == None: - dev_pass = "" - if dev_pass == "" or dev_pass == None: - try: - date_tmp = utime.localtime() #获取当前时间 - print("date_tmp:{}\r\n".format(date_tmp)) #转换时间戳s - timeStamp = (utime.mktime(date_tmp))*1000#获取时间戳ms - deviceId = self.deviceId #"876226002022574" #以imei号 - pid = self.pid - key = self.key - sign_data = "{}{}{}".format(deviceId,pid,timeStamp) #签名内容 - print("sign_data:{}\r\n".format(sign_data)) - sha256_hex = self.get_hmac_sha256(key,sign_data) - print("sha256_hex:{}\r\n".format(sha256_hex)) - - data = { - "deviceId":"{}".format(deviceId), #设备名 - "pid":pid, #产品PID - "sign":"{}".format(sha256_hex), #HMACSHA256签名后的数据 - "timeStamp":timeStamp, #要以ms的形式 - "signMethod":"HMACSHA256" #签名方式 HMACSHA256 - } - print(ujson.dumps(data)) - headers_T ={"Content-Type":"application/json"} - url = constants.DMP_BASE + "/dev/auth/active" - print("dev_active:{}".format(url)) - data =ujson.dumps(data) - response = None - response_result = 0 - try: - response = request.put(url,data=data,headers = headers_T,timeout=5) - response_result = 1 - except Exception as e: - usys.print_exception(e) - print("put error!!!!!\r\n") - response_result = 0 - - IV = "" #偏移量 - PASSWORD= "" #服务器返回后的加密内容 密码 - if response_result == 1: #访问成功ip - if response.status_code == 200: - print("headers:{}".format(response.headers)) - content = response.content - c = next(content) - # "api.xcloudtech.com" - # d_server_port = 9000 - data = ujson.loads(c) - # data = response.json() - print("data-{}".format(data)) - if data != None: - if "success" in data: - if data['success'] == True: - if "data" in data: - data_tmp = data["data"] - if "nonce" in data_tmp: #偏移量 - IV = data_tmp["nonce"] - if "password" in data_tmp: - PASSWORD = data_tmp["password"] - # EventMesh.publish("set_storage_config",["dev_pass",PASS]) - if data["code"] == 20001:#鉴权成功 - pass - elif data["code"] > 50000:# - self.res_error_count+=1 - if self.res_error_count>=3: - self.res_error_count = 0 - - EventMesh.publish("paly_device_res_error") - if data["code"] == 50003:#时间错误 - self.ntptime_flag = 0 - if PASSWORD != None and IV != None: - if len(PASSWORD) != 0 and len(IV) != 0: - password = PASSWORD - iv = IV - #解密 之后保存本地下一次登入做使用 - decrypt_data = self.aes_CBC_Decrypt(password, self.key, iv) #对服务器下发的密码进行解密 - print("decrypt_data {}".format(decrypt_data)) - EventMesh.publish("set_storage_config",["dev_pass",decrypt_data]) - print("IV:{}\r\n PASS:{}".format(IV,PASSWORD)) - if response != None: - response.close() - except Exception as e: - usys.print_exception(e) - print("dev_active end\r\n") - return [response_result,PASSWORD,IV] - #随机生成16个字节的字符串作为加密的IV 偏移量 - def generate_random_string(self,length=16): - random_str = "" - chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' - for i in range(length): - random_str+=chars[random.randint(0,length)] - return random_str - - #设备登入 没有Token 走登入获取token - def dev_sign_in(self, event=None, msg=None): - - - signal = net.csqQueryPoll() - if signal == -1 or self.get_sim_Status() != 1:#网络不正常 - print("dev_sign_in 无 SIM卡!!!!!!!!\r\n") - return - print("dev_sign_in 1") - token = "" - try: - token = self.dev_token - dev_pass = EventMesh.publish("get_storage_config","dev_pass") - #print("token:{}\r\n".format(token)) - if token == None: - token = "" - if dev_pass == None: - dev_pass = "" - if len(dev_pass) == 0:#没有密码 - return - #if token == None or token == "": - if 1: #返回结果成功 - #获取本地保存的密码 - decrypt_data=EventMesh.publish("get_storage_config","dev_pass") - # decrypt_data = self.aes_CBC_Decrypt(password, self.key, iv) #对服务器下发的密码进行解密 - print("decrypt_data {}".format(decrypt_data)) - #随机生成16个字符串 设备生成做加密使用 - random_string = self.generate_random_string(16) #随机的16个字符串 - enc_iv = random_string #加密的IV偏移量 - #设备端进行密码加密 - encrypt_data = self.aes_CBC_Encrypt(decrypt_data, self.key, enc_iv) - print("encrypt_data {}".format(encrypt_data)) - - #登入数据 - deviceId = self.deviceId - sign_in_data = { - "deviceId":"{}".format(deviceId), #设备ID - "password":"{}".format(encrypt_data), #加密的数据 - "nonce":"{}".format(random_string) #随机的16个字符串 - } - print(ujson.dumps(sign_in_data)) - sign_url =None - sign_url = constants.DMP_BASE + "/dev/auth/sign_in" - #获取验证码 - #url = "http://dmp.qipai.com.cn:30461/dev/device/bind-code/860132066060377" - # token = EventMesh.publish("get_dev_token") - # headers_T = {"Content-Type":"application/x-www-form-urlencoded","Accept":"*/*","bz-dt":"{}".format(token)} - # response = request.get(url,headers = headers_T,timeout=5) - # data = response.json() - print("dev_sign_in:{}".format(sign_url)) - headers_T ={"Content-Type":"application/json"} - response_result = 0 - response = 0 - try: - response = request.post(sign_url,data=ujson.dumps(sign_in_data),headers = headers_T,timeout=5) - response_result = 1 - except Exception as e: - usys.print_exception(e) - print("post error!!!!!\r\n") - response_result = 0 - # {'msg': '', 'code': 20001, 'success': True, - # 'data': {'deviceId': '876226002022574', - # 'token': '95436a9e09fd0ff89feb8825257dca982f'} - # } - - #self.dev_token = "" - if response_result == 1: - if response.status_code == 200: - content = response.content - c = next(content) - data = ujson.loads(c) - print("data-{}".format(data)) - if data != None: - if "success" in data: - if data['success'] == True: #登入成功 - if "data" in data: - data_tmp = data["data"] - if "token" in data_tmp: #token - self.dev_token = data_tmp["token"] - else:#报错清空密钥 - print("登录失败,清空密钥") - EventMesh.publish("set_storage_config",["dev_pass",""]) - if data["code"] == 50003:#时间错误 - self.ntptime_flag = 0 - - print("token:{}\r\n".format(self.dev_token)) - if response != None: - response.close() - pass - except Exception as e: - print("post error!!!!!\r\n") - response_result = 0 - if EventMesh.publish("get_dev_token") != None: - if len(EventMesh.publish("get_dev_token")) != 0: - if self.connect_web_flag == 0: - EventMesh.publish("post_iccid_user") - self.connect_web_flag = 1 - print("dev_sign_in end") - #ICCID上传 - def post_iccid_user(self,event=None,msg=None): - response = None - try: - iccid = sim.getIccid() - if iccid != -1 and len(iccid) > 5: - print("pots_ICCID:{}".format(iccid)) - #Headers = {'Transfer-Encodong':'chunked'} - token = EventMesh.publish("get_dev_token") - headers_T = None - if token: - headers_T = {"qp-dt":"{}".format(token)} - #http://dmp.raisound.com/bz-api/dev/iccid?deviceId=866610001099679&iccid=89860002192307889057 - url =None - url = (constants.DMP_BASE + "/dev/iccid?deviceId={}&iccid={}").format(modem.getDevImei(),iccid) - print("{} H:{}".format(url,headers_T)) - response = request.post(url,headers = headers_T) - if response.status_code == 200: - data = response.json() - print("pots_ICCID:{}".format(data)) - else: - print("pots_ICCID error!!!!") - for i in response.text: - print(i) - response.close() - except Exception as e: - usys.print_exception(e) - if response != None: - response.close() - #获取新的令牌 更新token - def update_dev_token(self, event=None, msg=None): - print("update_dev_token") - try: - dev_pass = "" - dev_pass = EventMesh.publish("get_storage_config","dev_pass") - - if dev_pass == None: - dev_pass = "" - if len(dev_pass) == 0: #没有密码走激活 - print("no dev_pass:{}\r\n".format(dev_pass)) - EventMesh.publish("dev_active") - token = self.dev_token - if token == None: - token = "" - if len(token) == 0: #没有token走登入 - print("no dev_token:{}\r\n".format(token)) - #self.dev_sign_in() - EventMesh.publish("dev_sign_in") - dev_tokenip =None - dev_tokenip = constants.DMP_BASE + "/dev/auth/token" - - token = self.dev_token - if token == None: - token = "" - if len(token) != 0: - response_result=0 - response = None - headers_T = {"bz-dt":"{}".format(token)} - try: - response = request.get(dev_tokenip,headers = headers_T,timeout=5) - response_result = 1 - except Exception as e: - print("get error!!!!!\r\n") - response_result = 0 - if response_result == 1: - if response.status_code == 200: - try: - headers_data = response.headers - # print("headers_data!!!!! headers_data:{}\r\n".format(headers_data)) - if "bz-dt" in headers_data: #bz-dt字段是否在头部数据里面 如果有就需要更新 - bz_dt = headers_data["bz-dt"] - self.dev_token = bz_dt.replace(" ", "") #去掉所有的空格符 - print("update_dev_token:{}".format(self.dev_token)) - except Exception as e: - print("headers_data!!!!! error\r\n") - if response != None: - response.close() - except Exception as e: - usys.print_exception(e) - print("update_dev_token end") - - - - -#OTA升级 -class OTA(): - def __init__(self): - #EventMesh.subscribe("min_sys_ota",self.min_sys_ota) - self.downloadFileVoURL1= "" - self.downloadFileVoURL2= "" - self.downloadFileVoURLBT="" - self.BT_ota_size =0 - self.ota_task_id = None - #蓝牙升级升级,返回0,没有升级;返回,1有升级,正在执行升级;返回2,电压不够 - def bt_ota(self,event=None,msg=None): - print("bt_ota_ota") - signal = net.csqQueryPoll() - if signal == -1 or sim.getStatus() != 1:#网络不正常 - print("bt_ota 无 SIM卡!!!!!!!!\r\n") - return 0 - # return 0 - response = None - ret_str =0 - - self.downloadFileVoURLBT="" - try: - #sourceVersion = versions.Build_Time - # sourceVersion = "7PIN-"+sourceVersion[:10]+"-"+sourceVersion[11:-3] - # print("sourceVersion:{}".format(sourceVersion)) - # sourceVersion = sourceVersion.replace(" ", "0") - # print("sourceVersion2:{}".format(sourceVersion)) - sourceVersion = version_util.get_unified_version() - print("sourceVersion:{}".format(sourceVersion)) - bt_versions = EventMesh.publish("get_bt_versions") - if bt_versions == None: - bt_versions="" - print("bt_versions:{}".format(bt_versions)) - #url = "http://iot.api.thisiot.com/ota/task?deviceId=860132060348836&sourceVersion=2024-12-20-15:35-1" - url =None - url = "https://dmp-api.qipai.com/qp-dmp-api/ota/task?deviceId={}&sourceVersion={}".format(modem.getDevImei(),sourceVersion) - print(url) - response = request.get(url,timeout=5) - if response.status_code == 200: - data = response.json() - print("OTA升级请求:{}".format(data)) - - if data: - try: - if data["code"] == 20007:#有新版本 - if data["data"]["targetVersion"] != bt_versions and "fileList"in data["data"]: - #if len(data["data"]["fileList"]) == 2: - for fileList in data["data"]["fileList"]: - - if fileList["fileName"] == "update.ufw": - self.BT_ota_size = fileList["size"] - self.downloadFileVoURLBT = fileList["url"] - if data["code"] == 20006:#已是最新版本 - ret_str = 0 - - except Exception as e: - usys.print_exception(e) - else: - print("bt_ota get error") - response.close() - except Exception as e: - usys.print_exception(e) - if response != None: - response.close() - - print("downloadFileVoURLBT{}".format(self.downloadFileVoURLBT)) - print("bt_ota end") - if self.downloadFileVoURLBT !="": - battery = EventMesh.publish("get_battery") - if battery>=40: - self.ota_timer = osTimer() - #self.ota_timer.start(15000, 0, self.ota_timer_cb) - if self.ota_task_id ==None: - self.ota_task_id = _thread.start_new_thread(self.ota_task,()) - ret_str = 1 - else: - ret_str = 2 - return ret_str - #小系统升级,返回0,没有升级;返回,1有升级,正在执行升级;返回2,电压不够 - def min_sys_ota(self,event=None,msg=None): - print("min_sys_ota") - signal = net.csqQueryPoll() - if signal == -1 or sim.getStatus() != 1:#网络不正常 - print("min_sys_ota 无 SIM卡!!!!!!!!\r\n") - return 0 - # return 0 - response = None - ret_str =0 - self.downloadFileVoURL1= "" - self.downloadFileVoURL2= "" - #self.downloadFileVoURLBT="" - try: - sourceVersion = version_util.get_unified_version() - print("sourceVersion:{}".format(sourceVersion)) - url =None - url = "https://dmp-api.qipai.com/qp-dmp-api/ota/task?deviceId={}&sourceVersion={}".format(modem.getDevImei(),sourceVersion) - print(url) - response = request.get(url,timeout=5) - if response.status_code == 200: - data = response.json() - print("OTA升级请求:{}".format(data)) - - if data: - try: - if data["code"] == 20007:#有新版本 - if "fileList"in data["data"]: - #if len(data["data"]["fileList"]) == 2: - for fileList in data["data"]["fileList"]: - if fileList["fileName"] == "dfota_2.bin": - self.downloadFileVoURL2 = fileList["url"] - elif fileList["fileName"] == "dfota_1.bin": - self.downloadFileVoURL1 = fileList["url"] - elif fileList["fileName"] == "update.ufw": - self.BT_ota_size = fileList["size"] - self.downloadFileVoURLBT = fileList["url"] - if data["code"] == 20006:#已是最新版本 - ret_str = 0 - - except Exception as e: - usys.print_exception(e) - else: - print("min_sys_ota get error") - response.close() - except Exception as e: - usys.print_exception(e) - if response != None: - response.close() - - print("min_sys_ota:downloadFileVoURL1:{},\ndownloadFileVoURL2:{},\ndownloadFileVoURLBT{}".format(self.downloadFileVoURL1,self.downloadFileVoURL2,self.downloadFileVoURLBT)) - print("min_sys_ota end") - if self.downloadFileVoURL2!="" and self.downloadFileVoURL1!="" : - battery = EventMesh.publish("get_battery") - if battery>=40: - self.ota_timer = osTimer() - #self.ota_timer.start(15000, 0, self.ota_timer_cb) - if self.ota_task_id ==None: - self.ota_task_id = _thread.start_new_thread(self.ota_task,()) - ret_str = 1 - else: - ret_str = 2 - return ret_str - - # def ota_timer_cb(self, *args, **kwargs): - # EventMesh.publish("publish_AI_btnInfo",{"func":self.usb_event}) - - def ota_task(self): - import fota - utime.sleep_ms(15000) - count = 30#最多等待30秒 - while 1: - count-=1 - VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag")#获取vc是否在运行 - if VC_satrt_flag == None: - VC_satrt_flag = 0 - voice_record_satrt_flag = EventMesh.publish("get_voice_record_flag")#获取静默或者语音留言是否在运行 - if voice_record_satrt_flag == None: - voice_record_satrt_flag = 0 - if VC_satrt_flag == 0 or count<=0 or voice_record_satrt_flag == 0: - break - utime.sleep_ms(100) - if self.downloadFileVoURLBT != "" and self.BT_ota_size != 0: - count = 5 #重试5次 - while 1: - count -=1 - ret = EventMesh.publish("bt_ota_run",[self.downloadFileVoURLBT,self.BT_ota_size ]) #升级蓝牙 - - if ret == 0 or count <=0: - break - utime.sleep_ms(1000) - utime.sleep_ms(1000) - #EventMesh.publish("rest_TB") #复位蓝牙 - utime.sleep_ms(50000) - if self.downloadFileVoURL1 == "" and self.downloadFileVoURL2 == "": #cat1没有升级的时候,要重启设备 - Power.powerRestart() - if self.downloadFileVoURL1 != "" and self.downloadFileVoURL2 != "": - fota_obj = fota() - fota_obj.httpDownload(url1=self.downloadFileVoURL1,url2=self.downloadFileVoURL2) - #ota升级结果返回,开机时调用一次即可 - def ota_result(self,event=None,msg=None): - response = None - try: - currentVersion = version_util.get_unified_version() - print("currentVersion:{}".format(currentVersion)) - url = (constants.DMP_BASE + "/ota/result?deviceId={}¤tVersion={}").format(modem.getDevImei(),currentVersion) - print(url) - response = request.post(url,timeout=5) - if response.status_code == 200: - data = response.json() - print("OTA升级结果上报:{}".format(data)) - else: - print("ota_result error!!!!") - for i in response.text: - print(i) - response.close() - except Exception as e: - usys.print_exception(e) - if response != None: - response.close() - #ICCID上传 - def pots_ICCID(self,event=None,msg=None): - response = None - try: - iccid = sim.getIccid() - if iccid != -1 and len(iccid) > 5: - print("pots_ICCID:{}".format(iccid)) - #Headers = {'Transfer-Encodong':'chunked'} - token = EventMesh.publish("get_dev_token") - headers_T = None - if token: - headers_T = {"bz-dt":"{}".format(token)} - #http://dmp.raisound.com/bz-api/dev/iccid?deviceId=866610001099679&iccid=89860002192307889057 - url =None - url = (constants.DMP_BASE + "/dev/iccid?deviceId={}&iccid={}").format(modem.getDevImei(),iccid) - print("{} H:{}".format(url,headers_T)) - response = request.post(url,headers = headers_T,timeout=5) - if response.status_code == 200: - data = response.json() - print("pots_ICCID:{}".format(data)) - else: - print("pots_ICCID error!!!!") - for i in response.text: - print(i) - response.close() - except Exception as e: - usys.print_exception(e) - if response != None: - response.close() -# 原 VC_State_* 整型已移除;会话/轮次见 VCTurnState / VCStateMachine(含 tts_rx_phase、speech_uplink_open)。 - -VC_record_TYPE_PCM = 0 -VC_record_TYPE_AMR = 1 -VC_play_TYPE_PCM = 0 -VC_play_TYPE_MP3 = 1 - -#websocket发送数据编号 -# AI_client_asr_num = 0 -# AI_client_chat_num = 1 -# AI_client_tts_num = 2 -AI_client_VC_num = 3 -AI_client_asr_num = 4 - - -class VCSessionState: - Disconnected = 0 - Connecting = 1 - Starting = 2 - Started = 3 - - -class VCTurnState: - Idle = 0 - Listening = 1 - SpeechStreaming = 2 - SpeechEnded = 3 - Thinking = 4 - TtsReceiving = 5 - Playing = 6 - AwaitPlayedAck = 7 - - -class VC_TtsRxPhase: - Idle = 0 - Started = 1 - ServerCompleted = 2 - - -class VCMode: - Realtime = 0 # VAD on(长对话,协议:无需 audio.create/completed) - PushToTalk = 1 # VAD off(单对话,协议:必须 audio.create/completed 包裹音频帧) - - -class VCStateMachine: - # 轻量状态机骨架(先落地骨架与动作接口,后续逐步把旧 if/flag 逻辑收敛进来)。 - # - # 约束来自《金三服务-语音对话V2.5》: - # - 收到 started 后才允许发音频帧(否则 session not initialized) - # - VAD 开启:不需要 audio.create/completed - # - VAD 关闭:必须 audio.create → frames → audio.completed - # - 播放结束 audio.played 是进入下一轮的门控信号之一(尤其关键词打断场景) - - def __init__(self, vc): - self.vc = vc - self.session = VCSessionState.Disconnected - self.turn = VCTurnState.Idle - self.last_llm_ms = 0 - self.last_tts_pending_ms = 0 - self.await_played = False - self.last_tts_completed_ms = 0 - self.tts_rx_phase = VC_TtsRxPhase.Idle - self.speech_uplink_open = 0 - - def reset_rx_phases(self): - self.tts_rx_phase = VC_TtsRxPhase.Idle - self.speech_uplink_open = 0 - - def mark_tts_rx_idle(self): - self.tts_rx_phase = VC_TtsRxPhase.Idle - - def on_server_asr_started(self): - if self.session == VCSessionState.Started: - self.speech_uplink_open = 1 - self.log("server.asr.started") - - def _turn_name(self, t): - try: - return { - VCTurnState.Idle: "Idle", - VCTurnState.Listening: "Listening", - VCTurnState.SpeechStreaming: "SpeechStreaming", - VCTurnState.SpeechEnded: "SpeechEnded", - VCTurnState.Thinking: "Thinking", - VCTurnState.TtsReceiving: "TtsReceiving", - VCTurnState.Playing: "Playing", - VCTurnState.AwaitPlayedAck: "AwaitPlayedAck", - }.get(t, str(t)) - except Exception: - return str(t) - - def _session_name(self, s): - try: - return { - VCSessionState.Disconnected: "Disconnected", - VCSessionState.Connecting: "Connecting", - VCSessionState.Starting: "Starting", - VCSessionState.Started: "Started", - }.get(s, str(s)) - except Exception: - return str(s) - - def log(self, reason=""): - # 只用于关键节点的单行状态日志,便于串口快速定位状态漂移 - try: - print("[FSM] {} session={} turn={} await_played={} tts_rx={} speech_up={}".format( - reason, - self._session_name(self.session), - self._turn_name(self.turn), - 1 if self.await_played else 0, - int(self.tts_rx_phase), - 1 if self.speech_uplink_open else 0, - )) - except Exception: - pass - - def mode(self): - try: - return VCMode.Realtime if self.vc.VC_mode == 0 else VCMode.PushToTalk - except Exception: - return VCMode.Realtime - - def on_session_start_request(self): - self.session = VCSessionState.Starting - self.turn = VCTurnState.Idle - # 新会话开始时必须清空上一轮“待播完”状态,否则会出现: - # - 切换模式/重连后 await_played 仍为 True → play_end 逻辑抢跑/不触发 → 无法自动开麦 - self.await_played = False - self.last_tts_completed_ms = 0 - self.last_llm_ms = 0 - self.reset_rx_phases() - - def on_server_started(self): - self.reset_rx_phases() - self.session = VCSessionState.Started - # 新会话 started:确保进入 Listening,且清理上轮播放门控 - self.await_played = False - self.last_tts_completed_ms = 0 - if self.turn in (VCTurnState.Idle, VCTurnState.AwaitPlayedAck): - self.turn = VCTurnState.Listening - self.log("server.started") - - def on_server_vad_silence(self): - if self.session == VCSessionState.Started: - self.turn = VCTurnState.SpeechEnded - self.log("server.vad.silence") - - def on_server_llm_created(self): - if self.session == VCSessionState.Started: - self.turn = VCTurnState.Thinking - try: - self.last_llm_ms = utime.ticks_ms() - except Exception: - self.last_llm_ms = 1 - self.log("server.llm.created") - - def on_server_tts_started(self): - if self.session == VCSessionState.Started: - self.turn = VCTurnState.TtsReceiving - self.tts_rx_phase = VC_TtsRxPhase.Started - self.log("server.tts.started") - - def on_server_tts_completed(self): - if self.session == VCSessionState.Started: - # tts.completed 只表示服务端音频下发结束,本地可能仍在播放;保持 Playing,由 await_played 门控播完上报 - self.await_played = True - self.tts_rx_phase = VC_TtsRxPhase.ServerCompleted - try: - self.last_tts_completed_ms = utime.ticks_ms() - except Exception: - self.last_tts_completed_ms = 1 - self.log("server.tts.completed") - - def on_server_canceled(self): - if self.session == VCSessionState.Started: - self.turn = VCTurnState.Listening - self.reset_rx_phases() - self.log("server.canceled") - - def on_server_completed(self): - if self.session == VCSessionState.Started: - # 本轮结束:默认回到 Listening;是否自动开麦由 action 门控决定 - # 若仍在等待本地播完(await_played),completed 不应把 turn 拉回 Listening, - # 否则会触发 completed→uplink_start 抢跑,打断播放链路。 - if not self.await_played and self.turn != VCTurnState.AwaitPlayedAck: - self.turn = VCTurnState.Listening - self.log("server.completed") - - def on_server_event(self, event, jsload_data=None): - # 事件→动作(action list)适配层。 - # 现阶段只产出“可幂等的控制动作”(uplink start/stop),避免和旧逻辑重复触发播放等副作用。 - # 后续会逐步把更多行为(play_start/audio.played/ptt create/completed)迁移到这里。 - actions = [] - try: - # LLM 阶段必须驱动 FSM.turn,否则 turn 会停在 SpeechEnded,导致: - # - _fsm_timeout_table_tick 里「Thinking 超时 / synthesis 超时」永不命中 - # - 与注释和 force_tts 前置条件不一致 - if event == "llm.created": - self.on_server_llm_created() - return actions - if event == "llm.completed": - # 若对端偶发跳过 llm.created,用 completed 把 turn 补到 Thinking,避免兜底全失效 - try: - if self.session == VCSessionState.Started: - if self.turn in (VCTurnState.SpeechEnded, VCTurnState.Listening): - self.turn = VCTurnState.Thinking - except Exception: - pass - return actions - if event == "started": - self.on_server_started() - return actions - if event == "vad.silence": - self.on_server_vad_silence() - actions.append(("uplink_stop", "vad.silence")) - return actions - if event == "tts.started": - self.on_server_tts_started() - actions.append(("uplink_stop", "tts.started")) - actions.append(("play_start", "tts.started")) - return actions - if event == "tts.completed": - self.on_server_tts_completed() - # 播放可能已在 started 时开启,这里不再强制 play_start,避免反复 open PCM - return actions - if event == "canceled": - self.on_server_canceled() - # canceled:仅长对话允许“立刻回到聆听并尝试开麦”(由 _uplink_request_start 门控保证安全幂等) - try: - if self.mode() == VCMode.Realtime and getattr(self.vc, "Close_flag", 0) == 0: - actions.append(("uplink_start", "canceled")) - except Exception: - pass - return actions - if event == "completed": - self.on_server_completed() - # completed:若不在等待 audio.played(说明本轮无 TTS),仅长对话可直接进入下一轮聆听 - try: - if self.mode() == VCMode.Realtime and getattr(self.vc, "Close_flag", 0) == 0: - if (not getattr(self, "await_played", False)) and self.turn != VCTurnState.AwaitPlayedAck: - actions.append(("uplink_start", "completed")) - except Exception: - pass - return actions - except Exception: - pass - return actions - - def on_local_audio_played(self): - # 本地播报完成后:长对话回到 Listening,等待下一轮输入 - if self.session == VCSessionState.Started: - self.turn = VCTurnState.Listening - self.await_played = False - self.last_tts_completed_ms = 0 - self.mark_tts_rx_idle() - self.log("local.audio_played") - - def on_local_play_start(self): - if self.session == VCSessionState.Started: - self.turn = VCTurnState.Playing - self.log("local.play_start") - - def on_local_synthesis_sent(self): - # synthesis 发出后到 tts.started 之间:视为 TTS 接收阶段,禁止开麦 - if self.session == VCSessionState.Started: - self.turn = VCTurnState.TtsReceiving - self.log("local.synthesis_sent") - - def on_local_play_end(self): - actions = [] - try: - if self.session != VCSessionState.Started: - return actions - if self.await_played: - actions.append(("send_audio_played", "local.play_end")) - # 长对话模式:对话过程中的 TTS 播完后,自动开麦继续当前对话。 - # 例外: - # - 刚切到长对话模式的提示音(mode_switch_long)不应自动开麦 - # - 其它提示/提醒(prompt)不应自动开麦 - # 实际开麦是否允许由 _uplink_request_start 的门控(FSM.turn / record_state 等)保证安全幂等。 - try: - if self.mode() == VCMode.Realtime and getattr(self.vc, "Close_flag", 0) == 0: - k = getattr(self.vc, "_last_tts_kind", "") or "" - if k == "dialog": - # 用户短按打断(RequestToSpeak):等 canceled 再开麦,勿 play_end 抢跑 - if getattr(self.vc, "_vc_pending_interrupt_uplink", 0) != 1: - actions.append(("uplink_start", "local.play_end")) - except Exception: - pass - self.on_local_audio_played() - except Exception: - pass - self.log("local.play_end") - return actions - - -class AI_VC(Abstract): - def get_vc_tts_playback_active(self, *args, **kwargs): - try: - return 1 if self._vc_tts_playback_active() else 0 - except Exception: - return 0 - - def get_vc_stream_active(self, *args, **kwargs): - # 供本地提示音侧查询:TTS 流播放期间本地不可抢占。 - try: - if self._vc_tts_playback_active(): - return 1 - except Exception: - pass - try: - if getattr(self.fsm, "await_played", False): - return 1 - except Exception: - pass - try: - if getattr(self.fsm, "tts_rx_phase", VC_TtsRxPhase.Idle) != VC_TtsRxPhase.Idle: - return 1 - except Exception: - pass - try: - t = getattr(self.fsm, "turn", VCTurnState.Idle) - if t in (VCTurnState.TtsReceiving, VCTurnState.Playing, VCTurnState.AwaitPlayedAck): - return 1 - except Exception: - pass - try: - if getattr(self, "_tts_pending", 0) == 1: - return 1 - except Exception: - pass - return 0 - - def _vc_fsm_log_snip(self): - try: - f = self.fsm - return "sess={} turn={} tts_rx={} await={} sp_up={}".format( - int(getattr(f, "session", -1)), - int(getattr(f, "turn", -1)), - int(getattr(f, "tts_rx_phase", -1)), - 1 if getattr(f, "await_played", False) else 0, - 1 if getattr(f, "speech_uplink_open", 0) else 0, - ) - except Exception: - return "fsm=?" - - def _vc_tts_pipeline_busy(self): - try: - if getattr(self.fsm, "await_played", False): - return True - if getattr(self.fsm, "tts_rx_phase", VC_TtsRxPhase.Idle) != VC_TtsRxPhase.Idle: - return True - t = getattr(self.fsm, "turn", VCTurnState.Idle) - if t in (VCTurnState.TtsReceiving, VCTurnState.Playing, VCTurnState.AwaitPlayedAck): - return True - if getattr(self, "_tts_pending", 0) == 1: - return True - if getattr(self, "VC_Play_flag", 0) == 1: - return True - if self._vc_local_play_busy(): - return True - except Exception: - pass - return False - - def _vc_local_play_busy(self): - try: - if getattr(self, "_vc_local_play_active", 0) != 1: - return False - until = getattr(self, "_vc_local_play_until_ms", 0) - if until and utime.ticks_diff(until, utime.ticks_ms()) > 0: - return True - self._vc_local_play_active = 0 - self._vc_local_play_until_ms = 0 - except Exception: - pass - return False - - def _vc_capture_allowed(self, reason=""): - """采集/上行开麦前:下行播放或本地短音未结束则禁止。""" - try: - if self._vc_tts_pipeline_busy(): - return False - if self._vc_local_play_busy(): - return False - except Exception: - return False - return True - - def _vc_stop_downlink_for_interrupt(self, reason="interrupt"): - """详设 DES-SCN-003:打断/取消时先本地停播清队列,再允许开麦。""" - try: - if getattr(self, "AI_audio_queue", None) is not None: - if self.AI_audio_queue.empty() != True: - self.AI_audio_queue.clear() - except Exception: - pass - try: - if self._vc_tts_pipeline_busy() or getattr(self, "is_ruppt_tts", 0) == 1: - self._drop_tts_audio = 1 - except Exception: - pass - try: - self._ptt_hard_mute_local_playback() - except Exception: - pass - try: - if getattr(self, "VC_Play_flag", 0) == 1 or getattr(self, "play_pcm", None) is not None: - self.VC_Play_End() - except Exception: - pass - try: - self._vc_local_play_active = 0 - self._vc_local_play_until_ms = 0 - except Exception: - pass - try: - self.fsm.await_played = False - self._need_audio_played_ack = 0 - self._tts_pending = 0 - self._tts_pending_ms = 0 - except Exception: - pass - try: - pending_intr = getattr(self, "_vc_pending_interrupt_uplink", 0) == 1 - if ( - not pending_intr - and getattr(self, "VC_record_state_flag", 0) != 0 - and getattr(self, "record_data_save_flag", 0) == 0 - ): - self._uplink_request_stop("stop_downlink/" + str(reason)) - except Exception: - pass - - def _vc_local_play(self, path, play_mode=1, loop=1, hold_ms=650): - """本地 MP3 短音:播前停录/停流式 TTS,避免与麦克风采集叠用。""" - try: - if getattr(self, "record_data_save_flag", 0) == 0 and getattr(self, "VC_record_state_flag", 0) != 0: - self._uplink_request_stop("local_play") - utime.sleep_ms(30) - except Exception: - pass - try: - if self._vc_tts_playback_active(): - self._ptt_hard_mute_local_playback() - try: - self.VC_Play_End() - except Exception: - pass - except Exception: - pass - try: - hold = int(hold_ms) - if hold < 200: - hold = 200 - self._vc_local_play_active = 1 - self._vc_local_play_until_ms = utime.ticks_add(utime.ticks_ms(), hold) - except Exception: - pass - try: - self.aud.play(play_mode, loop, path) - except Exception as e: - try: - self._vc_local_play_active = 0 - self._vc_local_play_until_ms = 0 - except Exception: - pass - usys.print_exception(e) - - def _vc_tts_playback_active(self): - """仅服务端 TTS/PCM 流在播(VC_Play_Start);aud.play 本地短音不算。""" - try: - if getattr(self, "VC_Play_flag", 0) == 1: - return True - except Exception: - pass - try: - if getattr(self, "play_pcm", None) is not None: - return True - except Exception: - pass - return False - - def _vc_vad_uplink_stop_eligible(self): - # 对应旧 VC_state >= SpeechStart - try: - if getattr(self.fsm, "speech_uplink_open", 0): - return True - t = getattr(self.fsm, "turn", VCTurnState.Idle) - return t not in (VCTurnState.Idle, VCTurnState.Listening) - except Exception: - return False - - def _vc_long_idle_active_speech_or_later(self): - # 对应旧 VC_state >= SpeechIng - try: - t = getattr(self.fsm, "turn", VCTurnState.Idle) - return t not in (VCTurnState.Idle, VCTurnState.Listening) - except Exception: - return False - - def _vc_is_business_idle(self): - # Level2:LED ai_state 为 idle,且无录音/流播/音乐闹钟等(与 WS ping 无关) - try: - if getattr(self, "music_play_flag", 0) == 1: - return False - if getattr(self, "alarm_play_flag", 0) == 1: - return False - except Exception: - pass - try: - if getattr(self, "VC_record_state_flag", 0) != 0: - return False - if getattr(self, "VC_Play_flag", 0) != 0: - return False - except Exception: - pass - try: - if EventMesh.publish("get_vc_stream_active") == 1: - return False - except Exception: - pass - try: - st = EventMesh.publish("get_led_debug_state") - if st and isinstance(st, dict): - if (st.get("ai") or "") != "idle": - return False - except Exception: - return False - return True - - def _vc_idle_deep_sleep_impl(self): - try: - _need_close = (self.VC_satrt_flag == 1) or (getattr(self, "AI_client_VC_isconnect", 0) == 1) - print("[VC][idle_L2] 触发休眠流程 need_Close={} VC_mode={}".format( - 1 if _need_close else 0, getattr(self, "VC_mode", -1))) - except Exception: - pass - try: - if self.VC_satrt_flag == 1 or getattr(self, "AI_client_VC_isconnect", 0) == 1: - try: - print("[VC][idle_L2] 调用 Close 断开会话/链路") - except Exception: - pass - self.Close() - except Exception: - pass - try: - utime.sleep_ms(100) - except Exception: - pass - try: - print("[VC][idle_L2] 发布 Hsensor_set_sleep") - EventMesh.publish("Hsensor_set_sleep") - except Exception: - pass - try: - print("[VC][idle_L2] 发布 SYSTEM_SLEEP ({})".format(constants.EVENT.SYSTEM_SLEEP)) - EventMesh.publish(constants.EVENT.SYSTEM_SLEEP) - except Exception: - pass - - def _vc_rx_resets_long_idle_timer(self, recv_data): - """是否应因本帧下行清零 close_timerout(排除保活/心跳,含 V2.5 文本 ping 对端应答)。""" - try: - if recv_data is None: - return False - if isinstance(recv_data, (bytes, bytearray)): - return True - if not isinstance(recv_data, str): - return False - t = recv_data.strip() - if not t: - return False - tl = t.lower() - if tl in ("ping", "pong"): - return False - try: - jd = ujson.loads(t) - except Exception: - return True - ev = jd.get("event") - if ev is None or ev == "": - ev = jd.get("type") - if ev is None: - return True - if str(ev).strip().lower() in _VC_KEEPALIVE_RX_EVENTS: - return False - return True - except Exception: - return True - - # ------------------------------------------------------------------------- - # 电源键 / AI 键:按 VC_mode 拆函数,便于阅读与修改(长对话 vs 单对话 PTT) - # ------------------------------------------------------------------------- - def _handle_pwk_short_release(self, event=None, msg=None, merged_ai_tail=False): - """电源键短按:读留言标志、退会议/翻译;否则 Close,或单键模式下走 AI_key_release。""" - print("pwk_key_release") - if EventMesh.publish("get_recv_voice") == 1: - EventMesh.publish("update_play_new_voice_info") - self.recv_voice = 0 - EventMesh.publish("set_history_voice_flag", 0) - self.AI_new_voice_url = "" - if self.trans_flag == 1 or self.meet_flag == 1: - EventMesh.publish("exit_meet_mode") - EventMesh.publish("exit_trans_mode") - return - if merged_ai_tail: - self.AI_key_release(event, msg) - else: - EventMesh.publish("publish_AI_btnInfo", {"func": self.Close}) - EventMesh.publish("stop_sos_call") - - def _ai_key_press_ptt_mode(self): - """单对话 PTT:短按仅停 TTS(不换 sid);非播放态短按吞掉;长按由 AI_key_long_press→ptt_begin。""" - if self.VC_mode != 1: - return False - try: - playing = False - try: - t = getattr(self.fsm, "turn", VCTurnState.Idle) - playing = t in (VCTurnState.TtsReceiving, VCTurnState.Playing) - except Exception: - playing = False - if playing or self._vc_tts_pipeline_busy(): - self._ptt_short_press_stop = 1 - try: - self._ptt_hard_mute_local_playback() - except Exception: - pass - try: - self.VC_Play_End() - except Exception: - pass - try: - self.fsm.await_played = False - except Exception: - pass - try: - self._ptt_led_lock_listening = 0 - self._ptt_interrupt_active = 0 - self.is_ruppt_tts = 0 - except Exception: - pass - try: - self._vc_publish_led("idle", "ptt_short_press/stop_tts") - except Exception: - pass - return True - except Exception: - pass - return True - - def _ai_key_press_long_dialogue_interrupt(self): - """长对话:TTS/播放忙或 turn==Thinking 时短按 → RequestToSpeak(先 cancel,canceled 后再开麦)。""" - if self.VC_mode != 0: - return False - try: - if getattr(self, "AI_client_VC_isconnect", 0) != 1 or getattr(self, "_vc_session_started", 0) != 1: - return False - except Exception: - return False - need_cancel = False - if self._vc_tts_pipeline_busy(): - need_cancel = True - else: - try: - if getattr(self.fsm, "turn", VCTurnState.Idle) == VCTurnState.Thinking: - need_cancel = True - except Exception: - pass - if not need_cancel: - return False - print("[KEY] long interrupt -> RequestToSpeak (cancel then uplink)") - self.is_ruppt_tts = 1 - try: - self._vc_pending_interrupt_uplink = 1 - except Exception: - pass - EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_RequestToSpeak}) - return True - - def _ai_key_press_long_dialogue_wake(self): - """长对话未连接或未 started:直接 AI_VC_start,勿 Close+sleep。""" - if self.VC_mode != 0: - return False - if getattr(self, "voice_flag", 0) == 1 or getattr(self, "meet_flag", 0) == 1 or getattr(self, "trans_flag", 0) == 1: - return False - try: - if getattr(self, "AI_client_VC_isconnect", 0) == 1 and getattr(self, "_vc_session_started", 0) == 1: - return False - except Exception: - pass - print("[KEY] long wake AI_VC_start (no Close)") - try: - self.Close_flag = 0 - except Exception: - pass - self.get_config_timer_count = 0 - self.key_state = 1 - self.AI_TTS_str = "" - self.AI_ASR_str = "" - self.AI_ASR_str_tmp = "" - self.AI_VC_start() - self.AI_key_long_press_flag = 0 - return True - - def _vc_led_alert_rb_flash(self): - try: - EventMesh.publish("set_led_state", {"alert": "rb_flash_2"}) - except Exception: - pass - - def _vc_clear_stale_record_state(self, reason=""): - """record_state_flag 非 0 但可能无真实录音时先停录复位,避免 uplink 门控拒开麦。""" - try: - if getattr(self, "VC_record_state_flag", 0) != 0: - self.VC_Speech_End() - except Exception: - try: - self.VC_record_state_flag = 0 - except Exception: - pass - - def _ai_key_press_long_dialogue_when_session_ready(self): - """长对话已连接且 session å·² started:非播放态直接开麦续聊(勿 idle 时发 cancel)。""" - if self.VC_mode != 0: - return False - try: - if getattr(self, "AI_client_VC_isconnect", 0) == 1 and getattr(self, "_vc_session_started", 0) == 1: - # 模式切换提示音会置 Close_flag=1;用户再短按须恢复可开麦 - try: - self.Close_flag = 0 - except Exception: - pass - self._vc_clear_stale_record_state("key.press/continue") - self.request_uplink_start("key.press/continue") - return True - except Exception: - pass - return False - - def _ai_key_release_handle_ptt(self): - """单对话 PTT 松手:仅在上行录音中时 end_speech;禁止误触发 AI_VC_start(否则新 sid / cancel 竞态)。""" - if self.AI_key_long_press_flag == 1: - if getattr(self, "VC_record_state_flag", 0) != 0: - try: - gap = 200 - if "vc_config" in globals() and vc_config: - gap = int(vc_config.get_int("PTT_RELEASE_END_SPEECH_DELAY_MS", 200)) - if gap > 0: - utime.sleep_ms(gap) - except Exception: - pass - print("[PTT] release end_speech asr_str={!r} rec={}".format( - self.AI_ASR_str, getattr(self, "VC_record_state_flag", -1))) - self.AI_client_VC_end_speech() - else: - self.AI_key_long_press_flag = 0 - try: - if getattr(self, "_ptt_short_press_stop", 0) == 1: - self._ptt_short_press_stop = 0 - except Exception: - pass - print("[PTT] release_no_uplink_skip") - return - try: - if getattr(self, "_ptt_short_press_stop", 0) == 1: - self._ptt_short_press_stop = 0 - except Exception: - pass - print("[PTT] release_short_no_long") - - def _ai_key_long_press_route(self, event=None, msg=None): - """长按:翻译先 Close 再开翻译录音;单对话→ptt_begin;长对话→与短按一致走 AI_key_press 链路。""" - if self.trans_flag == 1: - self.Close() - EventMesh.publish("start_voice_record", 4) - self.AI_key_long_press_flag = 1 - if self.VC_mode == 1: - EventMesh.publish("publish_AI_btnInfo", {"func": self.ptt_begin}) - else: - EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_key_press}) - - # ===== Push-To-Talk(单对话模式)===== - # 语义: - # - 长按:无论当前处于 TTS/Thinking/任何中间态,立即打断并进入新一轮录音上行 - # - 松开:停止录音并等待服务端返回(由 AI_client_VC_end_speech 触发) - def _ptt_end_led_suppress(self): - # 保持 _vc_play_end_suppress_idle_led / _ptt_led_lock_listening 直至真开录(VC_Speech_Start) - pass - - def _ptt_should_drop_inbound_tts(self): - """单对话打断窗口:丢弃迟到 TTS 帧 / 禁止起播。""" - try: - if getattr(self, "VC_mode", -1) != 1: - return False - if getattr(self, "_drop_tts_audio", 0) == 1: - return True - if getattr(self, "is_ruppt_tts", 0) == 1: - return True - if getattr(self, "_ptt_interrupt_active", 0) == 1: - return True - if getattr(self, "_vc_serial_canceling", 0) == 1: - return True - if getattr(self, "_ptt_pending_arm", 0) == 1: - return True - except Exception: - pass - return False - - def _ptt_should_skip_remote_playback(self): - return self._ptt_should_drop_inbound_tts() - - def _ptt_enter_interrupt_arm(self): - try: - self._ptt_arm_epoch = int(getattr(self, "_ptt_arm_epoch", 0)) + 1 - self._ptt_interrupt_active = 1 - self._ptt_led_lock_listening = 1 - self._vc_play_end_suppress_idle_led = 1 - self._drop_tts_audio = 1 - self.is_ruppt_tts = 1 - except Exception: - pass - - def _ptt_abort_interrupt_arm(self): - try: - self._ptt_interrupt_active = 0 - self._ptt_led_lock_listening = 0 - self._vc_play_end_suppress_idle_led = 0 - self._drop_tts_audio = 0 - self.is_ruppt_tts = 0 - except Exception: - pass - - def _ptt_on_record_hardware_started(self): - """真开录后:允许新 TTS、放开灯锁;旧 TTS 已在打断期丢弃。""" - try: - self._ptt_interrupt_active = 0 - self._ptt_led_lock_listening = 0 - self._vc_play_end_suppress_idle_led = 0 - self._drop_tts_audio = 0 - self.is_ruppt_tts = 0 - except Exception: - pass - - def _ptt_hard_mute_local_playback(self): - try: - self.VC_Play_flag = 0 - if getattr(self, "VC_play_TYPE", None) == VC_play_TYPE_PCM: - if getattr(self, "play_pcm", None) is not None: - try: - self.play_pcm.stream_stop() - self.play_pcm.close() - except Exception: - pass - self.play_pcm = None - try: - self.aud.stopPlayStream() - self.aud.stopAll() - except Exception: - pass - if getattr(self, "AI_audio_queue", None) is not None: - try: - if self.AI_audio_queue.empty() != True: - self.AI_audio_queue.clear() - except Exception: - pass - except Exception: - pass - try: - gap = 0 - if "vc_config" in globals() and vc_config: - gap = int(vc_config.get_int("PTT_POST_MUTE_MS", 30)) - if gap > 0: - utime.sleep_ms(gap) - except Exception: - pass - - def battery_priority_mute_vc(self, event=None, msg=None): - """低电/关机提示音:短暂停 VC TTS,避免被流式播放挡住。""" - try: - self._ptt_hard_mute_local_playback() - except Exception: - pass - try: - if getattr(self, "VC_Play_flag", 0) == 1 or getattr(self, "play_pcm", None) is not None: - self.VC_Play_End() - except Exception: - pass - - def _vc_enqueue_btn(self, func, data=None): - """统一经 publish_AI_btnInfo 入队,在 btnInfo_queue_thread 串行执行。""" - try: - if data is not None: - EventMesh.publish("publish_AI_btnInfo", {"func": func, "data": data}) - else: - EventMesh.publish("publish_AI_btnInfo", {"func": func}) - except Exception: - pass - - def _vc_ptt_cancel_wait_ms(self): - try: - if "vc_config" in globals() and vc_config: - return int(vc_config.get_int("PTT_CANCEL_WAIT_MS", 2500)) - except Exception: - pass - return 2500 - - def _vc_ptt_need_interrupt_wait(self): - """单对话:服务端仍在思考/收 TTS/播报时,开录前须先 cancel 并等 canceled。""" - try: - if getattr(self, "VC_mode", -1) != 1: - return False - turn = getattr(self.fsm, "turn", VCTurnState.Idle) - if self._vc_tts_pipeline_busy(): - return True - if turn in ( - VCTurnState.Thinking, VCTurnState.TtsReceiving, - VCTurnState.Playing, VCTurnState.AwaitPlayedAck, - ): - return True - except Exception: - pass - return False - - def _vc_begin_ptt_cancel_wait(self, reason="ptt_interrupt"): - try: - self._ptt_pending_arm = 1 - self._vc_serial_canceling = 1 - try: - self._vc_cancel_deadline_ms = utime.ticks_add( - utime.ticks_ms(), self._vc_ptt_cancel_wait_ms()) - except Exception: - self._vc_cancel_deadline_ms = 0 - json_data = {"event": "cancel", "payload": {}} - self.AI_client_VC_send(ujson.dumps(json_data)) - try: - self.fsm.reset_rx_phases() - except Exception: - pass - self._drop_tts_audio = 1 - print("[VC][serial] cancel_wait reason={} turn={}".format( - reason, getattr(getattr(self, "fsm", None), "turn", "?"))) - except Exception: - pass - - def _vc_on_ptt_cancel_settled(self, via="canceled"): - """btn 线程:cancel 已收尾,执行挂起的 PTT 开录。""" - try: - if not getattr(self, "_vc_serial_canceling", 0) and not getattr(self, "_ptt_pending_arm", 0): - return - self._vc_serial_canceling = 0 - self._vc_cancel_deadline_ms = 0 - if getattr(self, "_ptt_pending_arm", 0): - self._ptt_pending_arm = 0 - print("[VC][serial] ptt_arm after {}".format(via)) - self._ptt_arm_uplink() - except Exception: - pass - - def _vc_tick_serial_cancel_timeout(self): - if not getattr(self, "_vc_serial_canceling", 0): - return - try: - dl = getattr(self, "_vc_cancel_deadline_ms", 0) - if dl and utime.ticks_diff(utime.ticks_ms(), dl) >= 0: - print("[VC][serial] cancel_wait timeout -> arm") - self._vc_on_ptt_cancel_settled(via="timeout") - except Exception: - pass - - def _vc_fsm_dispatch_cb(self, data): - """btn 线程:处理服务端 FSM 事件(单对话串行)。""" - try: - ev, jsload = data - except Exception: - return - try: - actions = self.fsm.on_server_event(ev, jsload) - self._fsm_apply_actions(actions) - except Exception as e: - usys.print_exception(e) - if ev == "canceled": - self._vc_on_ptt_cancel_settled(via="canceled") - - def _vc_dispatch_server_event(self, ev, jsload_data=None): - try: - if ev == "canceled": - self._vc_stop_downlink_for_interrupt("canceled") - except Exception: - pass - if getattr(self, "VC_mode", -1) == 1: - self._vc_enqueue_btn(self._vc_fsm_dispatch_cb, (ev, jsload_data)) - else: - try: - self._fsm_apply_actions(self.fsm.on_server_event(ev, jsload_data)) - except Exception: - pass - - def _ptt_arm_uplink(self, t0=0): - """btn 线程:cancel 已结束或无需 cancel 时,发 audio.create 并开麦。""" - try: - self.is_ruppt_tts = 0 - except Exception: - pass - try: - self._ptt_audio_created = 0 - except Exception: - pass - try: - self.AI_ASR_flag = 0 - except Exception: - pass - try: - self.AI_client_VC_send_speech() - except Exception: - pass - try: - self.VC_Speech_Start_now() - except Exception: - pass - try: - self._vc_publish_led("listening", "ptt_begin/recording") - except Exception: - pass - self._ptt_end_led_suppress() - try: - if t0: - print("[PTT] record started (ms={})".format(utime.ticks_ms() - t0)) - except Exception: - pass - - def ptt_begin(self, reason="key.long_press"): - try: - if self.VC_mode != 1: - return - except Exception: - return - - # PTT 新一轮开始:清理模式切换/提示音退出标志 - try: - self.Close_flag = 0 - self._ptt_await_server = 0 - except Exception: - pass - - t0 = 0 - try: - t0 = utime.ticks_ms() - except Exception: - t0 = 0 - - self._ptt_enter_interrupt_arm() - try: - self._vc_publish_led("listening", "ptt_begin/pre") - except Exception: - pass - - # 本地先硬静音再 VC_init,避免 DMA/队列尾包漏播 - try: - self._ptt_hard_mute_local_playback() - except Exception: - pass - try: - self.VC_init() - except Exception: - pass - try: - if getattr(self, "VC_Play_flag", 0) == 1 or getattr(self, "play_pcm", None) is not None: - self.VC_Play_End() - except Exception: - pass - try: - self._ptt_hard_mute_local_playback() - except Exception: - pass - - # 若上一轮录音尚未结束,先强制停掉,避免 record_thread 复用出错 - try: - if getattr(self, "VC_record_state_flag", 0) != 0: - self.VC_Speech_End() - except Exception: - pass - - # 已在等 canceled:只刷新「待开录」,勿重复 audio.create - try: - if getattr(self, "_vc_serial_canceling", 0): - self._ptt_pending_arm = 1 - print("[PTT] cancel in progress, refresh pending arm") - self._ptt_end_led_suppress() - return - except Exception: - pass - - # 会话未连接:先建链并发 Start,等待 started 后再自动开麦 - try: - if getattr(self, "AI_client_VC_isconnect", 0) != 1: - self._ptt_force_record_on_started = 1 - self.AI_VC_start() - return - except Exception: - self._ptt_force_record_on_started = 1 - try: - self.AI_VC_start() - except Exception: - pass - return - - # 会话还没 initialized:先发 Start,请求服务端进入 started - if getattr(self, "_vc_session_started", 0) != 1: - self._ptt_force_record_on_started = 1 - try: - try: - if t0: - print("[PTT] wait server.started (ms={})".format(utime.ticks_ms() - t0)) - except Exception: - pass - self.AI_client_VC_start_requset() - except Exception: - pass - return - - # 服务端仍在思考/TTS:先 cancel,在 btn 队列收到 canceled 后再开录(方案甲串行) - if self._vc_ptt_need_interrupt_wait(): - self._vc_begin_ptt_cancel_wait("ptt_begin") - return - - self._ptt_arm_uplink(t0) - - def _vc_effective_asr_text(self): - try: - t = (getattr(self, "AI_ASR_str", "") or "").strip() - if t: - return t - return (getattr(self, "_last_asr_result_text", "") or "").strip() - except Exception: - return "" - - def _vc_long_dialogue_auto_uplink_ok(self, reason): - """长对话 canceled/completed 自动开麦:须有有效 ASR,且防抖/勿在 LLM 无 TTS 后抢开。""" - try: - if reason not in ("canceled", "completed", "local.play_end", "audio.played"): - return True - if getattr(self, "Close_flag", 0) == 1: - return False - if reason == "canceled" and getattr(self, "_vc_pending_interrupt_uplink", 0) == 1: - pass - elif not self._vc_effective_asr_text(): - try: - print("[VC][uplink_ctrl] Deny START by {} cause=empty_asr".format(reason)) - except Exception: - pass - return False - if reason == "completed": - if getattr(self, "_turn_has_tts_started", 0) == 0: - try: - if getattr(self, "_llm_completed_ms", 0) or getattr(self.fsm, "turn", None) == VCTurnState.Thinking: - print("[VC][uplink_ctrl] Deny START by completed cause=llm_without_tts") - return False - except Exception: - pass - try: - now = utime.ticks_ms() - last = getattr(self, "_vc_last_auto_uplink_ms", 0) - if last and utime.ticks_diff(now, last) < 1200: - print("[VC][uplink_ctrl] Deny START by {} cause=auto_uplink_debounce".format(reason)) - return False - self._vc_last_auto_uplink_ms = now - except Exception: - pass - except Exception: - pass - return True - - def _vc_led_state_debug_enabled(self): - try: - if "vc_config" in globals() and vc_config: - return vc_config.get_bool("LED_STATE_DEBUG", True) - except Exception: - pass - return True - - def _vc_led_should_standby_idle(self): - """未录音且非 TTS 播中:待机/提示音/模式切换/单对话轮间应灭灯(DES-L3 idle),勿被 FSM.turn=Listening 误导。""" - try: - if getattr(self, "VC_record_state_flag", 0) != 0: - return False - if self._vc_tts_playback_active(): - return False - if getattr(self, "_ptt_await_server", 0) == 1: - return False - t = getattr(self.fsm, "turn", VCTurnState.Idle) - if t in ( - VCTurnState.Thinking, - VCTurnState.TtsReceiving, - VCTurnState.Playing, - VCTurnState.AwaitPlayedAck, - ): - return False - if getattr(self, "Close_flag", 0) == 1: - return True - if getattr(self, "VC_mode", -1) == 1: - return True - k = getattr(self, "_last_tts_kind", "") or "" - if k in ("prompt", "boot_prompt", "mode_switch_long", "mode_switch_ptt"): - return True - except Exception: - pass - return False - - def _vc_publish_led(self, ai_state, reason=""): - try: - if getattr(self, "_ptt_led_lock_listening", 0) == 1: - if ai_state != "listening": - if self._vc_led_state_debug_enabled(): - print("[LED] publish blocked ai={} reason={} (ptt_arm_lock)".format( - ai_state, reason)) - return - except Exception: - pass - try: - if self._vc_led_state_debug_enabled(): - print("[LED] publish ai={} reason={} VC_mode={} rec={} turn={}".format( - ai_state, reason, getattr(self, "VC_mode", -1), - getattr(self, "VC_record_state_flag", -1), - getattr(getattr(self, "fsm", None), "turn", "?"))) - except Exception: - pass - try: - EventMesh.publish("set_led_state", {"ai_state": ai_state}) - except Exception: - pass - - def _vc_led_sync_from_fsm(self): - """按 FSM/真实 TTS 播放恢复 AI 灯;本地 aud.play 短音不参与。""" - try: - if getattr(self, "_ptt_led_lock_listening", 0) == 1: - if getattr(self, "VC_record_state_flag", 0) == 0: - return - except Exception: - pass - try: - if self._vc_led_should_standby_idle(): - self._vc_publish_led("idle", "fsm_sync/standby_idle") - return - except Exception: - pass - try: - if getattr(self, "VC_mode", -1) == 1 and getattr(self, "_ptt_await_server", 0) == 1: - self._vc_publish_led("thinking", "fsm_sync/ptt_await") - return - except Exception: - pass - try: - if self._vc_tts_playback_active(): - self._vc_publish_led("playing", "fsm_sync/tts") - return - except Exception: - pass - try: - t = getattr(self.fsm, "turn", VCTurnState.Idle) - if t in (VCTurnState.Thinking, VCTurnState.TtsReceiving): - self._vc_publish_led("thinking", "fsm_sync/thinking") - elif t in (VCTurnState.Playing, VCTurnState.AwaitPlayedAck): - # 服务端已标记播放态但本地 PCM 尚未起播:仍显示思考,勿误绿灯 - self._vc_publish_led("thinking", "fsm_sync/await_tts") - elif t in (VCTurnState.Listening, VCTurnState.SpeechEnded, VCTurnState.SpeechStreaming): - self._vc_publish_led("listening", "fsm_sync/listening") - else: - self._vc_publish_led("idle", "fsm_sync/idle") - except Exception: - try: - self._vc_publish_led("idle", "fsm_sync/fallback") - except Exception: - pass - - def _uplink_request_start(self, reason="", *args, **kwargs): - # 统一入口:避免多处同时 start 造成冲突(尤其是 TTS/播放阶段误开麦导致回声触发与状态紊乱) - try: - if self.Close_flag == 1: - try: - if reason in ("canceled", "local.play_end", "audio.played", "key.press/continue"): - print("[VC][uplink_ctrl] Deny START by {} cause=Close_flag".format(reason)) - except Exception: - pass - return - except Exception: - return - - # 连续对话才允许“自动开新一轮上行” - try: - if self.VC_mode != 0: - try: - if reason in ("canceled", "local.play_end", "audio.played"): - print("[VC][uplink_ctrl] Deny START by {} cause=VC_mode({})".format(reason, self.VC_mode)) - except Exception: - pass - return - except Exception: - return - - try: - if self.VC_mode == 0 and not self._vc_long_dialogue_auto_uplink_ok(reason): - return - except Exception: - pass - - try: - if not self._vc_capture_allowed(reason): - try: - print("[VC][uplink_ctrl] Deny START by {} cause=playback_busy".format(reason)) - except Exception: - pass - return - except Exception: - return - - # ===== 业务门控(收敛到 FSM turn/session)===== - try: - if getattr(self.fsm, "session", VCSessionState.Disconnected) != VCSessionState.Started: - try: - if reason in ("canceled", "local.play_end", "audio.played"): - print("[VC][uplink_ctrl] Deny START by {} cause=fsm.session".format(reason)) - except Exception: - pass - return - turn = getattr(self.fsm, "turn", VCTurnState.Idle) - # 仅 Listening/SpeechEnded 允许开麦进入下一轮;其它阶段一律拒绝(避免抢跑打乱链路) - if turn not in (VCTurnState.Listening, VCTurnState.SpeechEnded): - try: - if reason in ("canceled", "local.play_end", "audio.played"): - print("[VC][uplink_ctrl] Deny START by {} cause=fsm.turn({})".format(reason, turn)) - except Exception: - pass - return - except Exception: - return - - # 若当前不在录音上行,才允许开新一轮 - if getattr(self, "VC_record_state_flag", 0) != 0: - # canceled 打断后:可能是僵死 flag=2(started 未真开麦),先停录复位再开麦 - if reason == "canceled": - try: - self._vc_clear_stale_record_state("uplink_start/canceled") - except Exception: - pass - if getattr(self, "VC_record_state_flag", 0) != 0: - try: - if reason in ("canceled", "local.play_end", "audio.played"): - print("[VC][uplink_ctrl] Deny START by {} cause=VC_record_state_flag({})".format( - reason, getattr(self, "VC_record_state_flag", 0))) - except Exception: - pass - return - - # 新一轮开始录音:允许上行(上一轮可能已置 silence 闸门) - if getattr(self, "record_data_save_flag", 0) == 0: - self._server_vad_silence = 0 - - print("[VC][uplink_ctrl] Request START by {}".format(reason)) - try: - if reason == "canceled" and getattr(self, "_vc_pending_interrupt_uplink", 0) == 1: - self._vc_pending_interrupt_uplink = 0 - print("[VC][uplink_ctrl] interrupt_uplink armed -> start (canceled)") - except Exception: - pass - self.VC_Speech_Start_now() - - def _uplink_request_stop(self, reason="", *args, **kwargs): - # 统一入口:避免多处同时 stop 造成冲突 - if getattr(self, "record_data_save_flag", 0) == 0: - self._server_vad_silence = 1 - - if getattr(self, "VC_record_state_flag", 0) == 0: - try: - if getattr(self, "VC_mode", 0) != 1: - print("[VC][long_idle] _uplink_request_stop skip (record_state==0) reason={}".format(reason)) - except Exception: - pass - return - - try: - if getattr(self, "VC_mode", 0) != 1: - print("[VC][long_idle] step=uplink_stop_exec reason={} VC_record_state_flag={}".format( - reason, getattr(self, "VC_record_state_flag", -1))) - except Exception: - pass - print("[VC][uplink_ctrl] Request STOP by {}".format(reason)) - self.VC_Speech_End() - - def _uplink_mark_start(self, first_len=0): - try: - if self._uplink_active == 1: - return - self._uplink_active = 1 - self._uplink_bytes = 0 - self._uplink_start_ms = utime.ticks_ms() - print("[VC][uplink] START len={} {} VC_record_state_flag={}".format( - first_len, self._vc_fsm_log_snip(), self.VC_record_state_flag)) - except Exception: - pass - - def _uplink_mark_end(self, reason=""): - try: - if self._uplink_active != 1: - return - dur = utime.ticks_diff(utime.ticks_ms(), self._uplink_start_ms) if self._uplink_start_ms else -1 - print("[VC][uplink] END reason={} bytes={} dur_ms={} {} VC_record_state_flag={}".format( - reason, self._uplink_bytes, dur, self._vc_fsm_log_snip(), self.VC_record_state_flag)) - except Exception: - pass - self._uplink_active = 0 - self._uplink_bytes = 0 - self._uplink_start_ms = 0 - - # 最近一次 TTS 的语义来源(dialog/prompt/mode_switch_long/mode_switch_ptt/...) - self._last_tts_kind = "" - # 下一个“无 dialog_id 的提示音 TTS”在 tts.started 时采用该 kind - self._pending_tts_kind = "" - - # ===== 状态机动作接口(骨架)===== - # 说明:统一把“副作用动作”收敛到这些方法,便于后续把 recv/timer 的 if/flag 逻辑迁移到 VCStateMachine。 - def request_send_start(self): - EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_start_requset}) - - def request_send_audio_create(self): - EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_send_speech}) - - def request_send_audio_completed(self): - EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_end_speech}) - - def request_send_audio_played(self): - EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_audio_played}) - - def request_send_synthesis(self, text): - try: - self.AI_TTS_str = text - except Exception: - pass - EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_synthesis}) - - def request_uplink_start(self, reason=""): - EventMesh.publish("publish_AI_btnInfo", {"func": self._uplink_request_start, "data": reason}) - - def request_uplink_stop(self, reason=""): - EventMesh.publish("publish_AI_btnInfo", {"func": self._uplink_request_stop, "data": reason}) - - def _fsm_apply_actions(self, actions): - try: - for a in actions: - if not a: - continue - if a[0] == "uplink_stop": - self.request_uplink_stop(a[1] if len(a) > 1 else "") - elif a[0] == "uplink_start": - self.request_uplink_start(a[1] if len(a) > 1 else "") - elif a[0] == "play_start": - if getattr(self, "VC_mode", -1) == 1: - if self._ptt_should_skip_remote_playback(): - if self._vc_led_state_debug_enabled(): - print("[VC][serial] skip play_start (ptt_interrupt)") - continue - try: - self.fsm.on_local_play_start() - except Exception: - pass - EventMesh.publish("publish_AI_btnInfo", {"func": self.VC_Play_Start}) - elif a[0] == "send_audio_played": - self.request_send_audio_played() - except Exception: - pass - - # AI - - def _print_hw_diag(self): - # 仅诊断使用:打印当前分支能看到的麦克/音频相关配置和运行时状态。 - # JSWM2 老分支没有 device_profile/_PROFILE,这里宽容处理 - try: - profile = _PROFILE if isinstance(_PROFILE, dict) else {} - except Exception: - profile = {} - dev_type_profile = profile.get("dev_type", None) - flags = profile.get("flags", {}) - hardware = profile.get("hardware", {}) - product_cfg = profile.get("product_cfg", {}) - vc_cfg = {} - try: - vc_cfg = product_cfg.get("vc", {}) - except Exception: - vc_cfg = {} - - print("[hw-diag] ===== AI_VC hardware diag =====") - print("[hw-diag] profile.dev_type =", dev_type_profile) - print("[hw-diag] profile.flags =", flags) - print("[hw-diag] profile.hardware =", hardware) - print("[hw-diag] product_cfg.vc =", vc_cfg) - - try: - print("[hw-diag] VC_record_samplerate =", self.VC_record_samplerate) - print("[hw-diag] VC_play_samplerate =", self.VC_play_samplerate) - except Exception: - pass - try: - print("[hw-diag] vad_silence_runtime =", self.vad_silence) - except Exception: - pass - try: - print("[hw-diag] record_data_save_flag =", self.record_data_save_flag) - print("[hw-diag] record_max_bytes =", self.record_max_bytes) - except Exception: - pass - - try: - g = self.record.gain_get() - except Exception as e: - g = "gain_get_error: {}".format(e) - print("[hw-diag] record.gain_get =", g) - try: - print("[hw-diag] type(record) =", type(self.record)) - print("[hw-diag] type(aud) =", type(self.aud)) - except Exception: - pass - print("[hw-diag] ===== end of hw-diag =====") - - def __init__(self): - # import voiceCall - # voiceCall.callStart("10086") - # return - self.http_get_config_flag = 0 - if self.http_get_config_flag == 1: - try: - self.http_get_config()#获取/更新配置参数 - except Exception as e: - usys.print_exception(e) - self.VC_30S_timer = osTimer() - self.ai_key_press_audio = osTimer() - self.VC_30S_timer.start(30000, 1, self.VC_30S_timer_cb) - self.online_kws = 0#是否开启线上关键词 - self.get_config_timer_count = 0 - self.record_data_save_flag = 0 #是否保存录音文件 - self.play_data_save_flag = 0#是否保存tts数据到文件 - self.record_fd = None - self.play_fd = None - self.is_VC_ACK = 0 #是否开启ack应答模式 - self.is_long_record = 0 #是否开启长录音模式(语音打断模式) - self.ai_OTA = OTA() - self.is_auth = 1 #是否开启dmp鉴权 - #self.VC_mode = 1 #0:短按连续问答模式 1:长按录音模式 - self.VC_mode = EventMesh.publish("get_storage_config","VC_mode")# - if self.VC_mode == None : - self.VC_mode = 0 - self.is_up_error= 0 #是否上传错误id - self.Close_flag =0 #=1时,连续对话播放完音频就退出 - # 服务端 VAD 结束标志:收到 vad.silence 后置 1,用于硬闸门禁止继续上行音频 - # (避免仍持续上行导致服务端推迟/抑制后续 TTS,从而一直停留在思考态蓝绿闪) - self._server_vad_silence = 0 - # 单轮状态标志:用于判定 completed 是否应自动进入下一轮 - self._turn_has_asr_completed = 0 - self._turn_has_vad_silence = 0 - self._turn_has_tts_started = 0 - # VC 会话是否已进入 started(服务端 session initialized) - # 重要:在收到 started 前发送音频帧会触发服务端 "session not initialized" → failed → 端侧重连死循环 - self._vc_session_started = 0 - # 开机/提示音:本轮 started 不自动开麦(避免问候 TTS 前误进 listening) - self._skip_started_uplink_once = 0 - self._last_tts_kind = "" - self._pending_tts_kind = "" - # Push-To-Talk(VAD关闭/单对话)是否已发送 audio.create - # 重要:协议要求 disable_vad=true 时必须 audio.create→音频帧→audio.completed - self._ptt_audio_created = 0 - # PTT:松手等服务器期间保持 thinking 勿回蓝闪聆听 - self._ptt_await_server = 0 - # 方案甲 btnInfo 串行:打断时先 cancel,收到 canceled(或超时)后再 audio.create - self._vc_serial_canceling = 0 - self._ptt_pending_arm = 0 - self._vc_cancel_deadline_ms = 0 - # PTT 打断:轮次号 + 开录前丢弃旧 TTS / 灯只显示聆听 - self._ptt_arm_epoch = 0 - self._ptt_interrupt_active = 0 - self._ptt_led_lock_listening = 0 - # 端侧主动发 synthesis 后到收到 tts.started 之间的窗口标志,用于阻止误开下一轮录音上行 - self._tts_pending = 0 - self._tts_pending_ms = 0 - # 各类兜底的节流时间戳(避免“兜底过激/连续触发”) - self._fb_last_thinking_restart_ms = 0 - self._fb_last_tts_pending_timeout_ms = 0 - # thinking 兜底:部分情况下服务端只返回 llm.completed 而不触发 tts.started/completed, - # 会导致一直停留在 thinking。此时超时自动回到聆听。 - self._llm_completed_ms = 0 - # 语音上行统计(真实发送到 VC 发送队列的开始/结束) - self._uplink_active = 0 - self._uplink_bytes = 0 - self._uplink_start_ms = 0 - - # 状态机骨架(后续逐步把旧逻辑迁移进来) - self.fsm = VCStateMachine(self) - - self.play_new_voice_flag =0 #播放文字留言flag - - self.wait_first_flag = 0#是否进入未读判定(只播报一次) - self.wait_first_timer = osTimer()#如果播放未读留言的时候未读则不要播放了 - self.wait_first_cnt = 0 - - self.wait_sort = 0#未读留言正在播第几条 - EventMesh.subscribe("play_tts",self.play_tts) - EventMesh.subscribe("play_new_voice",self.play_new_voice) - EventMesh.subscribe("play_his_voice",self.play_his_voice) - EventMesh.subscribe("play_wait_voice",self.play_wait_voice) - EventMesh.subscribe("play_single_voice",self.play_single_voice)#不带tts的播放(闹钟) - EventMesh.subscribe("play_music",self.play_music)#播放音乐 - EventMesh.subscribe("play_trans_tts",self.play_trans_tts)#接收翻译tts - EventMesh.subscribe("set_vad_silence",self.set_vad_silence) - EventMesh.subscribe("set_wait_first_flag",self.set_wait_first_flag) - EventMesh.subscribe("kws_even",self.kws_even) - EventMesh.subscribe("change_volume",self.change_volume) - EventMesh.subscribe("find_cmd",self.find_cmd) #查询命令 - EventMesh.subscribe("NLP_set_vad_silence",self.NLP_set_vad_silence) #设置聆听间隔 - EventMesh.subscribe("change_voice",self.change_voice) #改变音色 - EventMesh.subscribe("Close_VC",self.Close_VC) #外部结束对话 - EventMesh.subscribe("get_recv_voice",self.get_recv_voice) #获取留言播放状态 - EventMesh.subscribe("set_recv_voice",self.set_recv_voice) #设置留言播放状态 - self.voice_timer = osTimer()#语音留言10S按下按键确定是否播放的判断定时器 - self.voice_cnt = 0 #留言定时器计数 - self.voicerupt_flag = 0#留言选择按键接听标志位 - self.wait_voicerupt_flag = 0#未读留言选择按键接听标志位 - self.recv_voice = 0 #是否收到留言 - self.voice_notice = 0 #收到留言并播放则置为1,接下来要播放提示词 - self.voice_choose_flag = 0#当tts和语音留言冲突时,为了防止语音地址被误清,做标志位在close做判断 - - self.revoice_timer = osTimer()#语音留言10S按下按键确定是否回复的判断定时器 - self.revoice_cnt = 0 #回复留言定时器计数 - self.revoicerupt_flag = 0#留言选择按键接听标志位 - - self.music_timer = osTimer()#音乐播放定时器,等待tts播完才放音乐 - self.music_cnt = 0 #留言定时器计数 - - self.his_timer = osTimer()#历史留言播放定时器,等待tts播完再播留言 - self.his_cnt = 0#历史留言定时器技术 - - self.wait_timer = osTimer()#未读留言播放定时器,等待tts播完再播留言 - self.wait_cnt = 0#未读留言定时器技术 - - self.error_data = [] - self.asr_taskid = "" - self.VC_taskid = "" - self.VC_server = 2 #0柒牌正式服务器,1:测试服务器1,2:测试服务器2 - self.voice_flag = 0 - - #会议记录标志 - self.meet_flag = 0 - #AI翻译标志 - self.trans_flag = 0 - - #历史留言查看判断标志 - self.history_voice_flag = 0#公共变量判断 - #60S等待留言判断标志 - self.wait_voice_flag = 0#公共变量判断 - EventMesh.publish("AI_alarm_open")# - self.server_ip = EventMesh.publish("get_storage_config","server_ip")# - if self.server_ip == None: - if self.VC_server == 1:# - self.server_ip="118.145.158.207:22083" - elif self.VC_server == 2:# - self.server_ip="118.145.158.207:22082" - elif self.VC_server == 4:# - self.server_ip="118.145.158.207:22084" - elif self.VC_server == 5:# - self.server_ip="amp-wss.raisound.com" - elif self.VC_server == 6:# - self.server_ip="118.145.158.207:22080" - elif self.VC_server == 7:# - self.server_ip="118.145.158.207:22080" - elif self.VC_server == 8:# - self.server_ip="115.190.94.208:22080" - elif self.VC_server == 86:# - self.server_ip="118.145.158.207:22086" - else: - #self.server_ip="sevenpin.qipai.com.cn" - self.server_ip="118.145.158.207:22082" - # 默认走 constants.AMP_SERVER(由 default_srv.py 统一管理,可在 usr 覆盖) - try: - if constants.AMP_SERVER: - self.server_ip = constants.AMP_SERVER - except Exception: - pass - - #开机提示音 - self.power_on_tone = EventMesh.publish("get_storage_config","power_on_tone")# - try: - if "vc_config" in globals() and vc_config: - if not vc_config.get_bool("POWER_ON_TONE_ENABLED", True): - self.power_on_tone = None - except Exception: - pass - if self.power_on_tone == None: - try: - if "vc_config" in globals() and vc_config: - self.power_on_tone = vc_config.get_attr("POWER_ON_TONE_DEFAULT", None) - except Exception: - pass - - self.VC_record_TYPE = VC_record_TYPE_PCM #0:PCM 1:AMR - self.VC_play_TYPE = VC_play_TYPE_PCM #0:PCM 1:mp3 - self.VC_record_samplerate = 16000 - self.VC_play_samplerate = 16000 - self.VAD_data_len = 640 #10ms数据 - self.is_VAD = 0#=1打开vad打断 - # ARG_sample_rate, // 采样率 - # ARG_channel, // 通道数 - # ARG_frame_size, // 每帧字节数 - # ARG_energy_threshold, // 能量阈值 - # ARG_voice_time, // 语音持续时间阈值(ms) - # ARG_silence_time, // 静音持续时间阈值(ms) - # ARG_silence_start_threshold, // 静音开始阈值(ms) - self.vad = None - if self.is_VAD == 1: - self.vad = VAD.VAD(self.VC_record_samplerate ,1,self.VAD_data_len,600,100,300,100) - self.speech_rate = EventMesh.publish("get_storage_config","speech_rate")#语速配置 - if self.speech_rate == None: - self.speech_rate = 0 - - self.vad_silence = EventMesh.publish("get_storage_config","vad_silence")#语速配置 - if self.vad_silence == None: - try: - if "vc_config" in globals() and vc_config: - self.vad_silence = vc_config.get_int("VAD_SILENCE_DEFAULT", 300) - else: - self.vad_silence = 300 - except Exception: - self.vad_silence = 300 - - self.VC_TTS_voice_type = EventMesh.publish("get_storage_config","VC_TTS_voice_type")#音色配置 - if self.VC_TTS_voice_type == None : - try: - if "vc_config" in globals() and vc_config: - cfg = vc_config.get_attr("VC_TTS_VOICE_TYPE_DEFAULT", None) - if isinstance(cfg, dict): - self.VC_TTS_voice_type = cfg - except Exception: - pass - # 若配置未提供,沿用原逻辑内置兜底(保持兼容) - if self.VC_TTS_voice_type == None: - self.VC_TTS_voice_type = { - "女":{"bot_name":"柒妹妹","voice":"zh_female_wanwanxiaohe_moon_bigtts","lang":"zh-cn"}, - "男":{"bot_name":"柒先生","voice":"zh_male_wennuanahu_moon_bigtts","lang":"zh-cn"}, - "ç«¥":{"bot_name":"柒乐乐","voice":"ICL_zh_female_huoponvhai_tob","lang":"zh-cn"}, - } - - # self.VC_TTS_voice_type = {0:"zh_female_xinlingjitang_moon_bigtts",1:"zh_male_wennuanahu_moon_bigtts",2:"ICL_zh_female_huoponvhai_tob", - # 3:"aliyun::longjiaxin",4:"zh_female_daimengchuanmei_moon_bigtts"} - #self.VC_TTS_voice_type = ["aliyun::longxiaoxia","aliyun::longnan","aliyun::longling"] - - #EventMesh.publish("set_storage_config",["VC_TTS_voice_type",self.VC_TTS_voice_type]) - - try: - self.VC_TTS_voice_key = EventMesh.publish("get_storage_config","VC_TTS_voice_key")#音色配置 - if self.VC_TTS_voice_key == None : - try: - if "vc_config" in globals() and vc_config: - self.VC_TTS_voice_key = vc_config.get_attr("VC_TTS_VOICE_KEY_DEFAULT", None) - except Exception: - pass - if not self.VC_TTS_voice_key: - self.VC_TTS_voice_key = "男" - if self.VC_TTS_voice_key in self.VC_TTS_voice_type: - pass - else: - self.VC_TTS_voice_key = list(self.VC_TTS_voice_type.keys())[0] - print("音色配置:{}:{}\r\n\r\n".format(self.VC_TTS_voice_key,self.VC_TTS_voice_type[self.VC_TTS_voice_key])) - self.VC_TTS_volume = EventMesh.publish("get_storage_config","VC_TTS_volume")#音量配置 - except Exception as e: - usys.print_exception(e) - #配置词 - self.commands_mode = 2 #=1用大模型判断命令,=0用服务器本地插件判断命令 =2用设备本地判断方式 - self.commands = [] - if self.VC_TTS_volume == None: - self.VC_TTS_volume = 100 - #可ota配置的参数变量 - self.emotion = EventMesh.publish("get_storage_config","emotion")#情绪 - if self.emotion == None: - self.emotion = "" - self.ota_update_notice = EventMesh.publish("get_storage_config","ota_update_notice")#OTA升级提示语 - if self.ota_update_notice == None: - self.ota_update_notice = "" - self.AI_audio_queue_thread_id = 0 - self.AI_audio_queue_thread_lock = 0 - self.AI_VC_send_data_queue_thread_id = 0 - self.AI_VC_send_data_queue_thread_lock = 0 - self.AI_ASR_send_data_queue_thread_id = 0 - self.AI_ASR_send_data_queue_thread_lock = 0 - self.AI_record_tmp_queue = Queue(100) - self.AI_ASR_send_data_queue = Queue(200) - self.AI_VC_send_data_queue = Queue(100) - self.AI_audio_queue = Queue(100) - #self.AI_WS_connect_Queue = Queue(100) #释放用 - self.AI_TTS_str = "" - self.AI_ASR_str = "" - self.AI_ASR_flag = 0 - self.AI_ASR_str_tmp = "" - self.AI_ASR_send_buf_len = 0 - self.AI_ASR_text_tmp = "" - self.AI_ASR_text_repet_count = 0 - # #开始线程 - # if self.AI_audio_queue_thread_id ==0: - # self.AI_audio_queue_thread_id = _thread.start_new_thread(self.AI_audio_queue_thread, ()) - # else: - # print("AI_VC_start :AI_audio_queue_thread_id != 0 ") - # if self.AI_VC_send_data_queue_thread_id == 0: - # self.AI_VC_send_data_queue_thread_id = _thread.start_new_thread(self.AI_VC_send_data_queue_thread, ()) - - self.VC_dialog_id = ""# - self.VC_message_id=None - self.VC_task_id =None - self.AI_client_VC = None - self.AI_client_VC_recv_id = 0 - self.AI_client_VC_recv_lock = 0 - self.AI_client_VC_isconnect =0 - self.AI_client_VC_debug =0 - - self.asr_state = 0 # 预留:ASR 侧状态(当前未参与分支) - self.key_state = 0 - - self.AI_client_asr = None - self.AI_client_asr_recv_id = 0 - self.AI_client_asr_recv_lock = 0 - self.AI_client_asr_isconnect =0 - self.AI_ASR_timer = osTimer() - self.AI_ASR_timer_count = 0 - self.AI_client_asr_debug =0 - self.AI_ASR_check_timeout = 0 - self.AI_ASR_error_timeout = 0 - self.VC_Thinking_timerout = 0 - - - self.record_pcm = None - self.play_pcm = None - self.VC_record_thread_id = 0 - self.VC_record_thread_lock = 0 - self.VC_pingping_timerout = 0 - self.asr_pingping_timerout = 0 - self.factory_timer = osTimer() - self._1S_timer = osTimer() - self.Audio_buf_size = 20*1024 - self.record = audio.Record() - #self.record.end_callback(self.record_callback) - self.record.gain_set(4,10) - #self.record.gain_set(4,12) - self.aud = audio.Audio(0) - self.aud.setCallback(self.audio_cb) - # 确保 record/aud 已创建后再打印硬件/音频诊断信息(需要 gain_get/type(record)) - try: - self._print_hw_diag() - except Exception as e: - usys.print_exception(e) - #self.aud.setVolume(self.VC_TTS_volume //10) - self.audio_play_state = 1 #音频播放完状态 - self.close_timerout = 0 - self._idle_deep_sec = 0 - self._vc_idle_deep_armed_suppress = 0 - self.LocalRespondingEnded_timerout = 0 - - self.VC_Play_flag = 0 - self.play_flag = 0 - self.VC_record_state_flag = 0 - self.AI_key_long_press_flag = 0 - self._asr_completed_tone_done = 0 # asr.completed 本地 think 提示每轮仅一次 - self._last_asr_result_text = "" # 保留最近 asr.result,防 VC_Speech_End 早于 asr.completed 清空 - self._vc_last_auto_uplink_ms = 0 # 长对话自动开麦防抖 - self._vc_local_play_active = 0 # aud.play 本地短音占用(与采集互斥) - self._vc_local_play_until_ms = 0 - self._vc_pending_interrupt_uplink = 0 # 长对话短按打断:等 canceled 再开麦 - self.websocket_error_count = 0 - self.AI_new_voice_url = "" #未读语音地址/音乐播放地址/闹钟播放标志 - self.music_play_flag = 0 #音乐播放标志 - self.alarm_play_flag = 0 #闹钟播放标志 - self.tts_in_data_len = 0 - self.tts_out_data_len = 0 - self._need_audio_played_ack = 0#tts.completed 后需在本地播报结束后上报 audio.played;勿用 tts_in/out:tts.completed 会再调 VC_Play_Start 把二者清零 - self.VC_satrt_flag = 0 - self.VC_stop_play_audio_flag = 0 - self.VC_satrt_kws_flag = 0 - self.session_id = "" - self.stop_voice_timer=osTimer() - self.pwk_cnt_voice = 0 #不听语音后,积攒的次数,需要一起播放 - self.pwk_notice_flag = 0#是否处于积攒留言状态(用来实现单次提醒) - self.is_noread_voice = EventMesh.publish("get_storage_config","is_noread_voice") - if self.is_noread_voice == None: - self.is_noread_voice = 0#是否还有未读留言(开机判断) - self.is_ruppt_tts = 0#是否已经打断tts播放 - EventMesh.subscribe("AI_key_press", self.AI_key_press) - EventMesh.subscribe("BT_AI_key_press", self.BT_AI_key_press) - EventMesh.subscribe("stop_AI_record", self.stop_AI_record) - EventMesh.subscribe("AI_key_release", self.AI_key_release) - EventMesh.subscribe("pwk_key_double_press", self.pwk_key_double_press) - EventMesh.subscribe("AI_key_long_press", self.AI_key_long_press) - EventMesh.subscribe("pwk_key_release", self.pwk_key_release) - EventMesh.subscribe("get_AI_audio_queue", self.get_AI_audio_queue) - EventMesh.subscribe("get_VC_satrt_flag", self.get_VC_satrt_flag) - EventMesh.subscribe("get_vc_stream_active", self.get_vc_stream_active) - EventMesh.subscribe("get_vc_tts_playback_active", self.get_vc_tts_playback_active) - EventMesh.subscribe("battery_priority_mute_vc", self.battery_priority_mute_vc) - EventMesh.subscribe("VC_error", self.VC_error) - EventMesh.subscribe("audio_test", self.audio_test) - EventMesh.subscribe("audio_test_close", self.audio_test_close) - EventMesh.subscribe("set_record_data_save_flag", self.set_record_data_save_flag) - EventMesh.subscribe("set_VC_TTS_voice_type", self.set_VC_TTS_voice_type) - EventMesh.subscribe("SET_TTS_VOLUME", self.set_tts_volume) - EventMesh.subscribe("set_VC_stop_play_audio_flag", self.set_VC_stop_play_audio_flag) - EventMesh.subscribe("set_yuyinaudio_flag",self.set_yuyinaudio_flag) - EventMesh.subscribe("get_yuyinaudio_flag",self.get_yuyinaudio_flag) - EventMesh.subscribe("set_pwk_cnt_voice",self.set_pwk_cnt_voice) - EventMesh.subscribe("get_pwk_cnt_voice",self.get_pwk_cnt_voice) - EventMesh.subscribe("exit_voice_message_mode",self.exit_voice_message_mode) #超时未退出语音留言模式 - EventMesh.subscribe("come_meet_mode",self.come_meet_mode)#进入会议模式 - EventMesh.subscribe("exit_meet_mode",self.exit_meet_mode)#退出会议模式 - EventMesh.subscribe("come_trans_mode",self.come_trans_mode)#进入翻译模式 - EventMesh.subscribe("exit_trans_mode",self.exit_trans_mode)#退出翻译模式 - EventMesh.subscribe("set_trans_flag",self.set_trans_flag)#设置翻译状态 - EventMesh.subscribe("get_audio_play_state",self.get_audio_play_state) - EventMesh.subscribe("set_voicerupt_flag",self.set_voicerupt_flag)#按键中断触发留言播放 - EventMesh.subscribe("set_wait_voicerupt_flag",self.set_wait_voicerupt_flag)#按键中断触发未读留言播放 - EventMesh.subscribe("get_recv_voice",self.get_recv_voice)#获取是否收到留言 - EventMesh.subscribe("rate_change",self.rate_change)#直接修改语速 - EventMesh.subscribe("get_history_voice_flag",self.get_history_voice_flag)#获取查找历史留言状态 - EventMesh.subscribe("set_history_voice_flag",self.set_history_voice_flag)#修改查找历史留言状态 - EventMesh.subscribe("clean_unread_vocie",self.clean_unread_vocie)#修改查找历史留言状态 - EventMesh.subscribe("get_wait_voice_flag",self.get_wait_voice_flag)#获取60S等待留言播放定时器状态 - EventMesh.subscribe("set_wait_voice_flag",self.set_wait_voice_flag)#修改60S等待留言播放定时器状态 - EventMesh.subscribe("rate_move",self.rate_move)#NLP修改语速 - EventMesh.subscribe("find_rate",self.find_rate)#NLP查询语速 - EventMesh.subscribe("set_pwk_notice_flag",self.set_pwk_notice_flag)#设置未读状态 - # EventMesh.subscribe("get_voicerupt_flag",self.get_voicerupt_flag)#获取留言播放状态 - EventMesh.subscribe("chenggong_tone",self.chenggong_tone) - - pass - EventMesh.publish("set_led_state", {"ai_state": "idle"}) - self.dialog_attributes_provider = EventMesh.publish("get_storage_config","dialog_attributes_provider")#智能体类型配置 - if self.dialog_attributes_provider == None: - self.dialog_attributes_provider = "coze" - self.outbound_attributess_provider = EventMesh.publish("get_storage_config","outbound_attributess_provider")#tts服务选择 - if self.outbound_attributess_provider == None: - self.outbound_attributess_provider = "volcengine" - self.inbound_attributess_provider = EventMesh.publish("get_storage_config","inbound_attributess_provider")#tts服务选择 - if self.inbound_attributess_provider == None: - self.inbound_attributess_provider = "volcengine" - - self.bot_name = "柒先生" - try: - if "bot_name" in self.VC_TTS_voice_type[self.VC_TTS_voice_key]: - self.bot_name = self.VC_TTS_voice_type[self.VC_TTS_voice_key]["bot_name"] - else: - print("not bot_name!!!!!!!!!!!!!!!!!!!!!!") - except Exception as e: - usys.print_exception(e) - try: - bt_ota_flag=0 - try: - if "vc_config" in globals() and vc_config and vc_config.get_bool("BT_OTA_ENABLED", False): - bt_ota_flag = self.ai_OTA.bt_ota()#先检查蓝牙是否要升级 - except Exception: - pass - - cat1_ota_flag = self.ai_OTA.min_sys_ota() - if bt_ota_flag==1 or cat1_ota_flag == 1: - #if 0: - if self.ota_update_notice != "": - self.AI_TTS_str = self.ota_update_notice - else: - self.AI_TTS_str = "检测到有新版本需要升级啦!升级时可千万别断电或乱按哦,升级完成后设备会自动重启,重启就代表升级成功" - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - self.Close_flag = 1 - # if self.ai_OTA.min_sys_ota()==2: - # self.AI_TTS_str = "检测到有新版本需要升级啦!电量低于40%,无法保证升级正常执行,请充满电后重启设备再次升级!" - # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - else: - try: - self.ai_OTA.ota_result() - self.ai_OTA.pots_ICCID() - except Exception as e: - usys.print_exception(e) - #开机提示语,如果联网失败,直接提示信号弱 - - - #EventMesh.publish("get_storage_config",["powerRestart",1]) - powerRestart = EventMesh.publish("get_storage_config","powerRestart")#=1非正常重启 - if powerRestart == None: - powerRestart = 0 - if powerRestart == 1: - EventMesh.publish("set_storage_config",["powerRestart",0]) - - else: - pass - self.AI_TTS_str = self.power_on_tone.replace("bot_name", self.bot_name) - print("开机提示:{}".format(self.AI_TTS_str)) - try: - self._pending_tts_kind = "boot_prompt" - self._last_tts_kind = "boot_prompt" - self._skip_started_uplink_once = 1 - except Exception: - pass - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - - except Exception as e: - usys.print_exception(e) - def set_VC_stop_play_audio_flag(self,event = None, msg = None): - self.VC_stop_play_audio_flag = msg - print("self.VC_stop_play_audio_flag:{}".format(self.VC_stop_play_audio_flag)) - - def rate_change(self,event,msg):#直接设置语速(数字) - self.speech_rate = msg - EventMesh.publish("set_storage_config",["speech_rate",self.speech_rate]) - print("set_speech_rate:{}".format(self.speech_rate)) - def rate_move(self,event,msg):#设置语速(模糊(å¿«/慢一点)) - if msg == 1 and self.speech_rate<50:#增大 - self.speech_rate += 10 - else:#减小 - if self.speech_rate >-50: - self.speech_rate -= 10 - EventMesh.publish("set_storage_config",["speech_rate",self.speech_rate]) - print("set_speech_rate:{}".format(self.speech_rate)) - def find_rate(self,event,msg):#查询APP语速 - print("查询语速处理:原始数据为:{}".format(self.speech_rate)) - y = 1 - if self.speech_rate >= -50 and self.speech_rate < 0:#负数 - y = 1 - (-0.01 * self.speech_rate) - elif self.speech_rate <= 50 and self.speech_rate >= 0:#正数 - y = 0.02 * self.speech_rate + 1.0 - print("处理后数据为{}".format(y)) - rate = str(y) + "倍" - return rate - def set_pwk_notice_flag(self,event,msg):#设置未读状态 - self.pwk_notice_flag = msg - #获取60S等待留言播放定时器状态 - def get_wait_voice_flag(self,event,msg): - return self.wait_voice_flag - def set_wait_voice_flag(self,event,msg): - # print("未读留言标记修改为:{}".format(msg)) - self.wait_voice_flag = msg - - #查看历史留言状态修改 - def get_history_voice_flag(self,event,msg): - return self.history_voice_flag - def set_history_voice_flag(self,event,msg): - # print("历史留言标记修改为:{}".format(msg)) - self.history_voice_flag = msg - def clean_unread_vocie(self,event,msg): - self.pwk_cnt_voice = 0 - self.pwk_notice_flag = 0 - EventMesh.publish("set_app_new_voice_flag",0) - def audio_test(self,event = None, msg = None): - self._uplink_request_start("audio_test") - self.VC_Play_Start() - def audio_test_close(self,event = None, msg = None): - self._uplink_request_stop("audio_test_close") - self.VC_Play_End() - if self.record_data_save_flag == 1: - if self.record_fd : - self.record_fd.close() - self.record_fd = None - def set_record_data_save_flag(self,event = None, msg = None): - self.record_data_save_flag = msg - def post_processor_after_instantiation(self): - pass - def get_AI_audio_queue(self, event, msg): - return self.AI_audio_queue - def get_VC_satrt_flag(self, event, msg): - return self.VC_satrt_flag - #检查有没有意外打开ai部分功能 - def check_vc_start(self): - print("check_vc_start:{} {} {} {}".format(self.VC_satrt_flag,self.VC_record_state_flag,self.AI_client_VC_isconnect,self.VC_Play_flag)) - if self.VC_satrt_flag == 0: - if self.VC_record_state_flag != 0 or self.AI_client_VC_isconnect == 1 or self.VC_Play_flag == 1: - self.Close() - def VC_30S_timer_cb(self, *args): # - EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_30S_timer_cb_}) - - def VC_30S_timer_cb_(self): - self.get_config_timer_count += 1 - if self.get_config_timer_count >= 2*60*2:#2小时更新配置参数 - self.get_config_timer_count = 0 - if self.http_get_config_flag == 1: - try: - self.http_get_config()#获取/更新配置参数 - except Exception as e: - usys.print_exception(e) - self.check_vc_start()#30秒检查一下有没有关闭 - #提交错误id - def ud_error(self,error_data): - response = None - if self.is_up_error == 1: - try: - # currentVersion = versions.Build_Time - # currentVersion = "7PIN-"+currentVersion[:10]+"-"+currentVersion[11:-3] - url = "http://{}/health?task_id={}".format(self.server_ip,error_data[0]+error_data[1]) - print(url) - response = request.get(url,timeout=5) - if response.status_code == 200: - - print("ud_error:ok") - else: - print("ud_error error!!!!") - for i in response.text: - print(i) - response.close() - except Exception as e: - usys.print_exception(e) - if response != None: - response.close() - #获取配置参数 - def http_get_config(self, event=None, msg=None): - print("http_get_config") - - response = None - #http://180.184.206.142/bz-api/dev/config?deviceId=xxx - try: - url = (constants.DMP_BASE + "/dev/config/{}").format(modem.getDevImei()) - - - print(url) - response = request.get(url,timeout=5) - if response.status_code == 200: - data = response.json() - print("http_get_config:{}".format(data)) - - if data: - try: - if data["code"] == 20000:#有配置 - if data["data"] : - if "version" in data["data"]: - config_version = EventMesh.publish("get_storage_config","config_version")#音量配置 - if config_version != data["data"]["version"]: - config_version = data["data"]["version"] - EventMesh.publish("set_storage_config",["config_version",config_version]) - if "VC_TTS_voice_type" in data["data"]: - self.VC_TTS_voice_type = data["data"]["VC_TTS_voice_type"] - EventMesh.publish("set_storage_config",["VC_TTS_voice_type",self.VC_TTS_voice_type]) - print("配置VC_TTS_voice_type成功") - if "server_ip" in data["data"]: - self.server_ip = data["data"]["server_ip"] - EventMesh.publish("set_storage_config",["server_ip",self.server_ip]) - print("配置server_ip成功") - if "power_on_tone" in data["data"]: - self.power_on_tone = data["data"]["power_on_tone"] - EventMesh.publish("set_storage_config",["power_on_tone",self.power_on_tone]) - print("配置power_on_tone成功") - if "dialog_attributes_provider" in data["data"]: - self.dialog_attributes_provider = data["data"]["dialog_attributes_provider"] - EventMesh.publish("set_storage_config",["dialog_attributes_provider",self.dialog_attributes_provider]) - if "outbound_attributess_provider" in data["data"]: - self.outbound_attributess_provider = data["data"]["outbound_attributess_provider"] - EventMesh.publish("set_storage_config",["outbound_attributess_provider",self.outbound_attributess_provider]) - print("配置outbound_attributess_provider成功") - if "inbound_attributess_provider" in data["data"]: - self.inbound_attributess_provider = data["data"]["inbound_attributess_provider"] - EventMesh.publish("set_storage_config",["inbound_attributess_provider",self.inbound_attributess_provider]) - print("配置inbound_attributess_provider成功") - if "speech_rate" in data["data"]: - self.speech_rate = EventMesh.publish("SET_SPEECH_RATE",float(data["data"]["speech_rate"])) - EventMesh.publish("set_storage_config",["speech_rate",self.speech_rate]) - print("配置speech_rate成功") - if "emotion" in data["data"]: - self.emotion = data["data"]["emotion"] - EventMesh.publish("set_storage_config",["emotion",self.emotion]) - print("配置emotion成功") - if "ota_update_notice" in data["data"]: - self.ota_update_notice =data["data"]["ota_update_notice"] - EventMesh.publish("set_storage_config",["ota_update_notice",self.ota_update_notice]) - print("配置ota_update_notice成功") - if "VC_TTS_volume" in data["data"]: - self.VC_TTS_volume =int(data["data"]["VC_TTS_volume"]) - EventMesh.publish("set_storage_config",["VC_TTS_volume",self.VC_TTS_volume]) - print("配置VC_TTS_volume成功") - - except Exception as e: - usys.print_exception(e) - else: - print("status_code:{}".format(response.status_code)) - print("http_get_config:error!!!!!!!!!!") - response.close() - except Exception as e: - usys.print_exception(e) - if response != None: - response.close() - - def audio_cb(self,event): - if event == 0: - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - self.audio_play_state =1 - #EventMesh.publish("set_audio_state", 1) - print('audio-play start.') - # 本地 aud.play(开录/思考短音等)不改动 AI 灯,仅 TTS 流由 VC_Play_Start 控绿灯 - if self._vc_led_state_debug_enabled(): - print("[LED] audio_cb start skip (local_play)") - return - elif event == 7: - self.audio_play_state = 0 - #EventMesh.publish("set_audio_state", 0) - print('audio-play finish.!!!!!!') - try: - if getattr(self, "VC_mode", -1) == 1 and getattr(self, "_ptt_await_server", 0) == 1: - self._vc_publish_led("thinking", "audio_cb/finish/ptt_await") - return - except Exception: - pass - try: - if getattr(self, "VC_record_state_flag", 0) != 0: - self._vc_publish_led("listening", "audio_cb/finish/recording") - return - except Exception: - pass - try: - if self._vc_tts_playback_active(): - if self._vc_led_state_debug_enabled(): - print("[LED] audio_cb finish skip (tts_playback)") - return - except Exception: - pass - try: - if getattr(self, "_ptt_led_lock_listening", 0) == 1: - return - except Exception: - pass - self._vc_led_sync_from_fsm() - def set_tts_volume(self,event,msg): - EventMesh.publish("set_storage_config",["VC_TTS_volume",msg])#音量配置 - self.VC_TTS_volume = msg - print("set_tts_volume:{} {}".format(self.VC_TTS_volume,msg)) - if int(msg) <= 10: #最小一定要设置1要不然没有声音 - volume = 10 //10 - self.aud.setVolume(volume) - else: - self.aud.setVolume(self.VC_TTS_volume //10) - pass - def get_yuyinaudio_flag(self, event=None, msg = None): - return self.voice_flag - - def set_yuyinaudio_flag(self, event=None, msg = None): - self.voice_flag=0 - - #留言主动拒听 - def get_pwk_cnt_voice(self, event=None, msg = None): - return self.pwk_cnt_voice - - def set_pwk_cnt_voice(self, event=None, msg = None): - self.pwk_cnt_voice=msg - print("设置未读次数为:{}".format(self.pwk_cnt_voice)) - - #会议 - def get_meetaudio_flag(self, event=None, msg = None): - return self.meet_flag - def set_meetaudio_flag(self, event=None, msg = None): - self.meet_flag = 0 - - def get_audio_play_state(self, event=None, msg = None): - return self.audio_play_state - - - - def come_meet_mode(self, event=None, msg = None): - if self.meet_flag == 0: #还是为0就提示进入会议纪要模式 - EventMesh.publish("send_meet_open_flag_to_iot")#创建会议 - self.meet_flag=1 - exit_tts ="开启会议纪要,正在录音" - EventMesh.publish("play_tts",[exit_tts,1]) - utime.sleep_ms(4000) - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_key_press}) - pass - def exit_meet_mode(self, event=None, msg = None): - if self.meet_flag == 1: #还是为1就提示已退出会议纪要模式 - EventMesh.publish("send_meet_end_flag_to_iot")#结束会议 - self.meet_flag=0 - EventMesh.publish("stop_voice_record") #关闭语音录音 - exit_tts ="录音结束,已退出会议纪要模式" - EventMesh.publish("play_tts",[exit_tts,1]) - pass - def come_trans_mode(self, event=None, msg = None): - if self.trans_flag == 0: #还是为0就提示进入AI翻译模式 - EventMesh.publish("send_trans_come_flag_to_iot")#进入翻译 - pass - def exit_trans_mode(self, event=None, msg = None): - if self.trans_flag == 1: #还是为1就提示已退出AI翻译模式 - EventMesh.publish("stop_voice_record") #关闭语音录音 - exit_tts ="已退出AI翻译模式" - EventMesh.publish("play_tts",[exit_tts,1]) - self.trans_flag=0 - EventMesh.publish("send_trans_end_flag_to_iot")#退出翻译 - pass - def set_trans_flag(self,event,msg): - self.trans_flag = msg - def exit_voice_message_mode(self, event=None, msg = None): - if self.voice_flag == 1: #还是为1就提示已退出会议纪要模式 - self.voice_flag=0 - exit_tts ="已退出语音留言模式" - EventMesh.publish("play_tts",[exit_tts,1]) - pass - def ai_key_press_audio_cb1(self, event=None, msg = None): - print("播放按键音!!!!!!!!!!") - # EventMesh.publish("audio_montage_play",["chenggong.mp3"]) - # self.chenggong_tone() - self.aud.aud_tone_play(10,400)#播放按键音 - def ai_key_press_audio_cb2(self, event=None, msg = None): - EventMesh.publish("audio_montage_play",["chenggong.mp3"]) - # self.chenggong_tone() #播放按键音 - - def AI_key_press(self, event=None, msg = None):#按键按下 - # 按键时打印 LED 聚合状态/最终模式(用于定位“绿灯不灭/闪一下”) - try: - if "vc_config" in globals() and vc_config and vc_config.get_bool("KEY_LED_DEBUG", False): - try: - led = EventMesh.publish("get_led_debug_state") - except Exception: - led = None - try: - t = getattr(self.fsm, "turn", None) - except Exception: - t = None - print("[KEY] AI_key_press VC_mode={} {} turn={} led={}".format( - getattr(self, "VC_mode", -1), self._vc_fsm_log_snip(), t, led)) - except Exception: - pass - # 单对话 PTT / 长对话分支(按键短按提示音已关,避免与开录等提示音叠在一起) - try: - if self._ai_key_press_ptt_mode(): - return - except Exception: - pass - if self._ai_key_press_long_dialogue_interrupt(): - return - try: - if self._ai_key_press_long_dialogue_when_session_ready(): - return - except Exception: - pass - try: - if self._ai_key_press_long_dialogue_wake(): - EventMesh.publish(constants.EVENT.SYSTEM_UP) - return - except Exception: - pass - - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - if EventMesh.publish("get_sos_flag")== 1: - return - if self.voice_flag == 1: #语音留言模式 - self.stop_voice_timer.stop() #关闭语音留言模式定时器 - # EventMesh.publish("stop_AI_record") - self.Close() - self.revoice_cnt = 0 - self.revoice_timer.stop()#回复定时器关闭,计数器清零 - EventMesh.publish("start_voice_record",2)#开始语音录音 - return - elif self.meet_flag == 1:#会议纪要模式 - self.Close() - EventMesh.publish("start_voice_record",3)#开始会议录音 - return - elif self.trans_flag == 1:#AI翻译模式 - return - - EventMesh.publish("stop_voice_record") #关闭语音录音 - print("AI_key_press: unhandled mode branch") - self.AI_key_long_press_flag = 0 - def BT_AI_key_press(self, event=None, msg = None):#按键按下 - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - if EventMesh.publish("get_sos_flag")== 1: - return - if self.voice_flag == 1: #语音留言模式 - tts_str = "语音留言模式,长按按钮开始录音,松开按钮结束" - EventMesh.publish("play_tts",[tts_str,1]) - return - - EventMesh.publish("stop_voice_record") #开启静默需要关闭 - print("AI_key_press") - self.get_config_timer_count = 0 - self.key_state = 1 - # EventMesh.publish("audio_montage_play",["chenggong.mp3"]) - # self.chenggong_tone() #播放按键音 - # 长对话:蓝牙按键同单按 - # - 对话进行中:打断/续聊(复用 dialog_id) - # - 已停止/未 started:走新会话流程 - try: - if self.VC_mode == 0: - if getattr(self, "AI_client_VC_isconnect", 0) == 1 and getattr(self, "_vc_session_started", 0) == 1: - EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_RequestToSpeak}) - return - except Exception: - pass - - self.Close() - - if self.VC_mode == 1: #长按对话模式 接收到蓝牙耳机按键事件 - - tts_str = "请您按住圆形的按钮开始讲话,讲完松开即可 " - EventMesh.publish("play_tts",[tts_str,1]) - return - # if dev_type == "7PIN": - # self.chenggong_tone() - #self.aud.aud_tone_play(10,200)#播放按键音 - self.AI_TTS_str = ""#"你好,有什么吩咐" - self.AI_ASR_str = "" - self.AI_ASR_str_tmp = "" - # if self.VC_state != VC_State_None: - # EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) - # EventMesh.publish("publish_AI_btnInfo",{"func":utime.sleep_ms,'data':(10)}) - self._uplink_request_stop("BT_AI_key_press/compat2") - self._uplink_request_start("BT_AI_key_press/compat2") - - self.AI_VC_start() - - - # self.Close() - # self.AI_VC_start() - self.AI_key_long_press_flag = 0 - def stop_AI_record(self, event=None, msg=None):#停止AI对话 - if self.VC_satrt_flag == 1: - self.get_config_timer_count = 0 - self.AI_TTS_str = ""#"你好,有什么吩咐" - self.AI_ASR_str = "" - self.AI_ASR_str_tmp = "" - self.AI_key_long_press_flag = 0 - EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) - self.request_uplink_stop("stop_AI_record") - count = 0 - while True: - if count >= 20 or self.VC_satrt_flag == 0: - break - count+=1 - utime.sleep_ms(500) - - def exit_voice_message_timer(self,*argc): - EventMesh.publish("exit_voice_message_mode") - - def AI_key_release(self, event=None, msg=None):#按键弹起 - try: - if "vc_config" in globals() and vc_config and vc_config.get_bool("KEY_LED_DEBUG", False): - try: - led = EventMesh.publish("get_led_debug_state") - except Exception: - led = None - try: - t = getattr(self.fsm, "turn", None) - except Exception: - t = None - print("[KEY] AI_key_release VC_mode={} {} turn={} led={}".format( - getattr(self, "VC_mode", -1), self._vc_fsm_log_snip(), t, led)) - except Exception: - pass - print("AI_key_release123") - self.key_state = 0 - if self.trans_flag == 1:#AI翻译模式 - EventMesh.publish("stop_voice_record") #关闭语音录音 - return - if self.voice_flag == 1: - self.stop_voice_timer.stop() - self.voice_flag=0 - if EventMesh.publish("GET_USB_STATE") == 0: - EventMesh.publish("set_led_state", {"ai_state": "idle"}) - EventMesh.publish("stop_voice_record")#停止静默录音 - tts_play_str = "留言已发送" - self.voice_flag=0 - EventMesh.publish("play_tts",[tts_play_str,1]) - return - if self.VC_mode == 1: - self._ai_key_release_handle_ptt() - return - def pwk_key_double_press(self,event,msg):#pwk双击 - print("come 双击") - # 切换模式:必须打断当前对话/录音/播放,回到 idle,等待按键触发新一轮 - try: - self.Close_flag = 1 - except Exception: - pass - try: - EventMesh.publish("publish_AI_btnInfo", {"func": self.Close}) - except Exception: - pass - if self.VC_mode == 0: - self.VC_mode = 1 - tts_str = "已切换到单对话模式" - # 下一次“无 dialog_id 的提示音 TTS”归类为模式切换提示(单对话) - try: - self._pending_tts_kind = "mode_switch_ptt" - self._last_tts_kind = "mode_switch_ptt" - except Exception: - pass - else: - self.VC_mode = 0 - tts_str = "已切换到长对话模式" - # 下一次“无 dialog_id 的提示音 TTS”归类为模式切换提示(长对话) - try: - self._pending_tts_kind = "mode_switch_long" - self._last_tts_kind = "mode_switch_long" - except Exception: - pass - # 提示音播完即退出(Close_flag=1),保持 idle,等待按键 - EventMesh.publish("play_tts",[tts_str,1]) - EventMesh.publish("set_storage_config",["VC_mode",self.VC_mode]) - - def AI_key_long_press(self, event, msg):#按键长按 - try: - if "vc_config" in globals() and vc_config and vc_config.get_bool("KEY_LED_DEBUG", False): - try: - led = EventMesh.publish("get_led_debug_state") - except Exception: - led = None - try: - t = getattr(self.fsm, "turn", None) - except Exception: - t = None - print("[KEY] AI_key_long_press VC_mode={} {} turn={} led={}".format( - getattr(self, "VC_mode", -1), self._vc_fsm_log_snip(), t, led)) - except Exception: - pass - print("AI_key_long_press") - self._ai_key_long_press_route(event, msg) - - def pwk_key_release(self, event, msg):#电源键短按,关闭 - try: - if "vc_config" in globals() and vc_config: - _m = bool(vc_config.get_bool("SINGLE_POWER_KEY_AI_MERGED", False)) - if not _m: - _ai = vc_config.get_attr("EXTINT_AI_GPIO", "GPIO22") - _m = _ai is None or _ai == "" - else: - _m = False - except Exception: - _m = False - if _m: - self._handle_pwk_short_release(event, msg, merged_ai_tail=True) - return - self._handle_pwk_short_release(event, msg, merged_ai_tail=False) - - - def key_prompt(self): - EventMesh.publish("audio_montage_play",["key_prompt.mp3"]) - #self.aud.play(1,1,'U:/static/key_prompt.mp3')#提示请您按住中间的按钮开始讲话,讲完松开即可 - def VC_error(self, event, msg):#错误播报 - self.websocket_error() - #EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error}) - def set_VC_record_state_flag(self,data): - - self.VC_record_state_flag = data - print("[VC] record_state_flag={}".format(self.VC_record_state_flag)) - #开始录音,不缓存,直接发 - def VC_Speech_Start_now(self): - # 新一轮开始录音:允许上行(上一轮可能已置 silence 闸门) - self._server_vad_silence = 0 - self.VC_Speech_Start() - if self.VC_record_state_flag == 1: - self.VC_record_state_flag =2 - print("[VC] Speech_Start_now record_state={}".format(self.VC_record_state_flag)) - #开始录音,先缓存到buf,等服务器响应再发 - def VC_Speech_Start(self): - try: - - #self.VC_state = VC_State_SpeechStart - print("[VC] Speech_Start record_state={} AI_ASR_flag={}".format(self.VC_record_state_flag,self.AI_ASR_flag)) - if self.AI_ASR_flag != 0: - return - - if self.VC_record_state_flag == 0: - # 新一轮开始录音:允许上行(上一轮可能已置 silence 闸门) - self._server_vad_silence = 0 - self.AI_ASR_str = "" - self.AI_ASR_str_tmp = "" - self._last_asr_result_text = "" - try: - self._asr_completed_tone_done = 0 - except Exception: - pass - - self.VC_record_thread_close()#关闭录音线程 - _thread.stack_size(self.Audio_buf_size+8*1024) - self.VC_record_thread_id = _thread.start_new_thread(self.VC_record_thread,()) - _thread.stack_size(8*1024) - print("self.VC_record_thread_id:{}".format(self.VC_record_thread_id)) - try: - - if self.VC_record_TYPE == VC_record_TYPE_AMR : - try : - self.record.stream_start(audio.Record.AMRNB, self.VC_record_samplerate, 0) - except Exception as e: - usys.print_exception(e) - elif self.VC_record_TYPE == VC_record_TYPE_PCM: - print("record_pcm.stream_read_start()!!!!!!!!!!!!") - self.record.stream_start(audio.Record.WAV, self.VC_record_samplerate, 0) - # if self.record_pcm != None: - # try: - # self.record_pcm.stream_stop() - # self.record_pcm.close() - # self.record_pcm = None - # except Exception as e: - # usys.print_exception(e) - # if self.record_pcm == None: - # ret = self.record_pcm = audio.Audio.PCM(0,1,16000,0,1,2) - # print("audio.Audio.PCM:ret:{}".format(ret)) - # ret = self.record_pcm.stream_read_start() - # print("self.record_pcm.stream_read_start:ret:{}".format(ret)) - except Exception as e: - usys.print_exception(e) - - self.VC_record_state_flag = 1 - # 录音硬件已起:协议侧应对应 audio.create(PTT)或长对话直传 PCM - try: - print("[VC][proto] record_start sr={} pcm_type={} VC_mode={} session_started={} " - "ptt_audio_created={} thread_id={}".format( - self.VC_record_samplerate, self.VC_record_TYPE, self.VC_mode, - getattr(self, "_vc_session_started", -1), - getattr(self, "_ptt_audio_created", -1), - self.VC_record_thread_id)) - except Exception: - pass - # 录音开始:聆听灯(PTT 蓝快闪 / 长对话蓝慢闪) - try: - self._vc_publish_led("listening", "VC_Speech_Start") - except Exception: - pass - try: - if getattr(self, "VC_mode", -1) == 1: - self._ptt_on_record_hardware_started() - except Exception: - pass - - except Exception as e: - self.websocket_error() - self.VC_record_state_flag = 1 - self.VC_Speech_End()#关闭录音 - usys.print_exception(e) - #停止录音 - def VC_Speech_End(self): - try: - self._uplink_mark_end("VC_Speech_End") - self.AI_ASR_flag = 0 - self.AI_ASR_str_tmp = "" - # 勿在此清空 AI_ASR_str:vad.silence 停麦常早于 asr.completed,清空会导致 check_text:len:0 连播 think 音 - #EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_asr}) - - if self.VC_record_state_flag == 0: - return - #self.AI_client_asr_StopTranscription_requset()#请求停止 - if self.VC_record_TYPE == VC_record_TYPE_AMR: - self.record.stream_stop() - self.record.stop()#关闭录音 - Record_read_buf = bytearray(self.Audio_buf_size) - for i in range(10): - Record_len = self.record.stream_read(Record_read_buf, self.Audio_buf_size) - # if VC_AUDIO_FRAME_LOG: - # print("Record_len:!!!!!!{}".format(Record_len)) - if Record_len <= 0: - break - elif self.VC_record_TYPE == VC_record_TYPE_PCM: - self.record.stream_stop() - self.record.stop()#关闭录音 - Record_read_buf = bytearray(self.Audio_buf_size) - for i in range(10): - Record_len = self.record.stream_read(Record_read_buf, self.Audio_buf_size) - # if VC_AUDIO_FRAME_LOG: - # print("Record_len:!!!!!!{}".format(Record_len)) - if Record_len <= 0: - break - # if self.record_pcm != None: - # try: - # self.record_pcm.stream_stop() - # self.record_pcm.close() - # self.record_pcm = None - # except Exception as e: - # usys.print_exception(e) - except Exception as e: - usys.print_exception(e) - self.VC_record_thread_close()#关闭录音线程 - if self.AI_record_tmp_queue.empty() != True: - self.AI_record_tmp_queue.clear() - - self.VC_record_state_flag = 0 - # PTT 一轮录音结束:清长按语义,避免后续短按松手误走 end_speech 而不播提示音 - try: - if getattr(self, "VC_mode", -1) == 1: - self.AI_key_long_press_flag = 0 - except Exception: - pass - # 录音停止:单对话进入思考态(等服务器),勿继续蓝闪聆听 - try: - if getattr(self, "VC_mode", -1) == 1: - self._ptt_await_server = 1 - self._vc_publish_led("thinking", "VC_Speech_End/ptt") - else: - self._ptt_await_server = 0 - self._vc_publish_led("idle", "VC_Speech_End") - except Exception: - pass - print("[VC] Speech_End record_state={}".format(self.VC_record_state_flag)) - #关闭录音线程 - def VC_record_thread_close(self): - if self.VC_record_thread_id != 0: - count =50 - while True: - count -= 1 - if self.VC_record_thread_lock == 1: - self.VC_record_thread_lock = 2 - if self.VC_record_thread_lock == 0 or count <=0: - break - else: - print("VC_record_thread_thread going:{}...".format(count)) - utime.sleep_ms(1) - self.VC_record_thread_lock = 0 - print("VC_record_thread_thread_close") - if self.VC_record_thread_id != 0: - try: - _thread.stop_thread(self.VC_record_thread_id) - except Exception as e: - usys.print_exception(e) - self.VC_record_thread_id = 0 - #获取录音数据 - - def VC_record_thread(self): - ##return - print("[VC] record_thread start") - count = 0 - RequestToSpeak_flag = 0 - # try: - # uos.remove('/usr/test.pcm') - # except Exception as e: - # usys.print_exception(e) - #record_data_queue = Queue(100) - #fd = open('/usr/test.pcm','w') - # if self.VC_record_TYPE == 1: - # h_data= [0x23,0x21 ,0x41 ,0x4D ,0x52 ,0x0A ] - # h_data_b = bytes(h_data) - # self.AI_client_VC_send(h_data_b) - AI_record_tmp_queue_len = 0 - vad_buf = bytearray() - VAD_data_count = 0 - vad_state = 0 - self.VAD_count = 0 - while True: - try: - if self.VC_record_thread_lock == 2: - break - self.VC_record_thread_lock = 1 - # 死循环接收数据 - #if self.VC_state == VC_State_SpeechStart: - Record_read_buf = bytearray(self.Audio_buf_size) - - Record_len = 0 - if self.VC_record_TYPE == VC_record_TYPE_PCM:#pcm - Record_len = self.record.stream_read(Record_read_buf, self.Audio_buf_size) - #utime.sleep_ms(60) - - else : - - Record_len = self.record.stream_read(Record_read_buf, self.Audio_buf_size) - utime.sleep_ms(600) - - #Record_len = 1024*2 - #print("VC_record_thread:{}".format(Record_len)) - if Record_len > 0: - read_buf = bytes(Record_read_buf[:Record_len]) - # if VC_AUDIO_FRAME_LOG: - # print("Record_len:{}".format(Record_len)) - if self.record_data_save_flag == 1: - if self.record_fd == None: - try: - uos.remove('/usr/record_data.pcm') - except Exception as e: - pass - # 二进制写入,避免文本模式导致数据异常 - self.record_fd = open('/usr/record_data.pcm', 'wb') - self.record_fd_len = 0 - if self.record_fd != None: - try: - self.record_fd.write(read_buf) - print("write record:{}".format(len(read_buf))) - self.record_fd_len = self.record_fd_len+len(read_buf) - if self.record_fd_len >=500*1024: - EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) - except Exception as e: - usys.print_exception(e) - EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) - # count+=Record_len - # if count >150*1024: - # self.record_fd.close() - #self.record_fd = None - #VAD检测 - if self.is_VAD == 1: - vad_state = 0 - tmp_c = 1000 - while True: - tmp_c -=1 - if tmp_c <=0: - break - #vad_buf_len = len(vad_buf) - if VAD_data_count == 0:#没数据的时候 - vad_buf = Record_read_buf[:self.VAD_data_len] - VAD_data_count = self.VAD_data_len - self.vad.custom_vad_reset()#重置vad - else:#有数据的时候 - if Record_len - VAD_data_count < self.VAD_data_len :#剩余长度不够一次填充,先保留,但不填算法 - vad_buf = Record_read_buf[VAD_data_count:Record_len] - VAD_data_count = 0 - break - else:#剩余长度够填一次数据 - vad_buf = Record_read_buf[VAD_data_count:VAD_data_count+self.VAD_data_len] - VAD_data_count += len(vad_buf) - #print("vad_buf_len:{},{}".format(len(vad_buf),VAD_data_count)) - - if len(vad_buf) >= self.VAD_data_len: - vad_state = self.vad.custom_vad_process(vad_buf,len(vad_buf))#给vad数据 - #print("vad_state:{}".format(vad_state)) - - if VAD_data_count >= Record_len: - VAD_data_count = 0 - break - if vad_state == 1: - print("检测到VAD=1!!!!!!!!!!!!!") - - # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_RequestToSpeak})#打断 - # RequestToSpeak_flag = 1 - - self.VAD_count+=1 - if self.VAD_count >= 20: - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_RequestToSpeak})#打断 - - else: - if self.VAD_count != 0: - EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Play_Pause,"data":0})#恢复音频播放 - self.VAD_count = 0 - if self._vc_tts_pipeline_busy(): - pass - if vad_state == 1: - self.AI_record_tmp_queue.put(read_buf)#入缓存队列 - AI_record_tmp_queue_len += len(read_buf) - - - else: - VAD_data_count = 0 - if self.VC_record_state_flag <=1 :#没发送前先存起来 - self.AI_record_tmp_queue.put(read_buf) - AI_record_tmp_queue_len += len(read_buf) - else: - AI_record_tmp_queue_len = 0 - while True: - if self.AI_record_tmp_queue.empty() == True: - break - else: - pass - # 协议门控:会话未 started 前禁止上行音频(否则 session not initialized) - if VC_PROTO_GUARDS and self._vc_session_started != 1 and self.record_data_save_flag == 0: - _ = self.AI_record_tmp_queue.get() - AI_record_tmp_queue_len = max(0, AI_record_tmp_queue_len - len(_)) - continue - if not (self._server_vad_silence == 1 and self.record_data_save_flag == 0): - self.AI_client_VC_send(self.AI_record_tmp_queue.get()) #发送之前存的数据 - else: - self._uplink_mark_end("silence_gate(drop_queue)") - _ = self.AI_record_tmp_queue.get() - # 当前帧发送前协议门控(可用 VC_PROTO_GUARDS 临时关闭): - # - session started 前禁止发帧 - # - Push-To-Talk 在 audio.create 前禁止发帧 - if VC_PROTO_GUARDS and ( - (self._vc_session_started != 1 and self.record_data_save_flag == 0) or - (self.VC_mode == 1 and self._ptt_audio_created != 1 and self.record_data_save_flag == 0) - ): - # 未 ready:转入缓存,等待 started 再发(并做容量控制) - try: - self.AI_record_tmp_queue.put(read_buf) - AI_record_tmp_queue_len += len(read_buf) - while AI_record_tmp_queue_len > 128*1024 and self.AI_record_tmp_queue.empty() != True: - d = self.AI_record_tmp_queue.get() - AI_record_tmp_queue_len = max(0, AI_record_tmp_queue_len - len(d)) - except Exception: - pass - else: - if not (self._server_vad_silence == 1 and self.record_data_save_flag == 0): - self.AI_client_VC_send(read_buf) - else: - self._uplink_mark_end("silence_gate(drop_buf)") - - - - - - - #print("VC_record_thread:{}".format(len(read_buf))) - #count+=len(read_buf) - # if count>=500*1024:#结束 - # self.AI_client_VC_end_speech() - # break - # count+=1 - # if count %10 == 0: - # print("VC_record_thread on line {} asr recv :{} send:{}".format(self.VC_record_state_flag,_thread.threadIsRunning(self.AI_client_asr_recv_id),_thread.threadIsRunning(self.AI_VC_send_data_queue_thread_id))) - - # if self.VC_state >= VC_State_SpeechEnd: - # break - - except Exception as e: - usys.print_exception(e) - #break - self.VC_record_thread_lock = 0 - if self.record_data_save_flag == 1: - utime.sleep_ms(1) - else: - utime.sleep_ms(100) - #fd.close() - print("VC_record_thread:exit") - self.VC_record_thread_id = 0#线程退出 - self.VC_record_thread_lock = 0 -###################################################### VC ################################################################################### - def websocket_connect_VC(self): - print("websocket_connect_VC:{} e:{}!!!!!!!!!!!!!!!!!!!!!".format(self.AI_client_VC_isconnect,self.AI_client_VC_debug)) - if self.AI_client_VC_isconnect == 1: - return - # 创建VC客户端 - try: - signal = net.csqQueryPoll() - if signal == -1 or sim.getStatus() != 1:#无蜂窝/无卡等:弱网提示 - self.websocket_error(play_weak_signal_tone=True) - return - - req_str = "" - ack = "" - if self.is_VC_ACK == 1: - ack = "ack=1" - lang = "lang=zh-cn"#普通话测试环境 - try: - if "lang" in self.VC_TTS_voice_type[self.VC_TTS_voice_key]: - lang = self.VC_TTS_voice_type[self.VC_TTS_voice_key]["lang"] - else: - print("not lang!!!!!!!!!!!!!!!!!!!!!!") - except Exception as e: - usys.print_exception(e) - - #女,男,童,粤,川 - self.bot_name = "柒先生" - try: - if "bot_name" in self.VC_TTS_voice_type[self.VC_TTS_voice_key]: - self.bot_name = self.VC_TTS_voice_type[self.VC_TTS_voice_key]["bot_name"] - else: - print("not bot_name!!!!!!!!!!!!!!!!!!!!!!") - except Exception as e: - usys.print_exception(e) - - # Headers = {'X-NLS-Token':'0c8b8f05a46f423096be64a9c4c46310'} - # self.AI_client_VC = uwebsocket.Client.connect('wss://nls-gateway-cn-beijing.aliyuncs.com/ws/v1',headers=Headers, debug=False) - token = EventMesh.publish("get_dev_token") - print("token:{}".format(token)) - - req_str = "vc/chat?is_auth={}".format(self.is_auth) - try: - if "vc_config" in globals() and vc_config: - base = vc_config.get_attr("VC_WS_PATH", "vc/chat") or "vc/chat" - req_str = "{}?is_auth={}".format(base, self.is_auth) - except Exception: - pass - if(token): - url = 'ws://{}/{}&dt={}'.format(self.server_ip,req_str,token) - else: - url = 'ws://{}/{}'.format(self.server_ip,req_str) - #url = 'ws://115.190.94.208:22080/vc/coze?dt={}'.format(token) - - if VC_WS_IO_LOG: - print("[VC] ws url {}".format(url)) - self.AI_client_VC = uwebsocket.Client.connect(url, debug=False) - #self.AI_WS_connect_Queue.put(self.AI_client_VC) - - - print("[VC] websocket_connect ok") - except Exception as e: #网络连接异常(已通过 CSQ/SIM,多为 URL/握手/服务端问题,勿播「信号弱」) - usys.print_exception(e) - print("websocket_connect_VC error!!!\r\n") - try: - print("[VC] ws connect exc: {}".format(e)) - except Exception: - pass - str_error = str(e) - if str_error.find("[Errno 22] EINVAL")!= -1: - print("[VC] websocket EINVAL: {}".format(str_error)) - self.websocket_error(play_weak_signal_tone=False) - EventMesh.publish("set_storage_config",["powerRestart",1])#调试临时关闭 - utime.sleep_ms(1000) - self.aud.stopPlayStream() - self.aud.stopAll() - Power.powerRestart() - - self.websocket_error(play_weak_signal_tone=False) - return - #print("AI_client_VC:{}".format(self.AI_client_VC)) - if self.AI_client_VC == None: - EventMesh.publish("update_dev_token")#获取token - self.websocket_error(play_weak_signal_tone=False) - return - self.AI_client_VC_debug+=1 - utime.sleep_ms(10) - self.AI_client_VC.settimeout(5) - #self.AI_VC_send_data_queue_thread_close()#关闭发送线程 - if self.AI_VC_send_data_queue_thread_id == 0: - _thread.stack_size(self.Audio_buf_size+8*1024) - self.AI_VC_send_data_queue_thread_id = _thread.start_new_thread(self.AI_VC_send_data_queue_thread, ()) - _thread.stack_size(8*1024) - print("AI_VC_start :AI_VC_send_data_queue_thread_id :{} ".format(self.AI_VC_send_data_queue_thread_id)) - self.AI_client_VC_recv_thread_close()#关闭读线程 - # 线程接收数据 - _thread.stack_size(self.Audio_buf_size+8*1024) - self.AI_client_VC_recv_id = _thread.start_new_thread(self.AI_client_VC_recv, (self.AI_client_VC,)) - _thread.stack_size(8*1024) - self.AI_client_VC_isconnect = 1 - print("websocket_connect_VC end") - - def websocket_error(self, TTS_str=None, play_weak_signal_tone=False): - """play_weak_signal_tone=True 时播 xinhaoruo1(弱网/无卡类);连接/协议失败请传 False,避免误报「信号弱」。""" - try: - if getattr(self, "VC_mode", 0) != 1: - print("[VC][long_idle] websocket_error enter VC_mode={} TTS_str={} weak_hint={} -> Close then uplink_stop(async)".format( - getattr(self, "VC_mode", -1), TTS_str, play_weak_signal_tone)) - except Exception: - pass - print("websocket_error1!!!!") - utime.sleep_ms(800) - self.Close() - print("websocket_error1.1!!!!") - print("websocket_error!!!!") - print("websocket_error!!!!") - self.VC_Thinking_timerout = 0 - self.VC_satrt_flag = 0 - self.AI_TTS_str = "" - self.AI_ASR_str = "" - self.AI_ASR_str_tmp = "" - self.AI_ASR_flag = 0 - # 可能由收包线程/其它线程触发:统一切回主线程执行 stop,避免“异常→重试→再异常”的循环 - try: - if getattr(self, "VC_mode", 0) != 1: - print("[VC][long_idle] websocket_error schedule uplink_stop reason=websocket_error + set_led_idle") - except Exception: - pass - EventMesh.publish("publish_AI_btnInfo",{"func":self._uplink_request_stop, "data":"websocket_error"}) - EventMesh.publish("set_led_state", {"ai_state": "idle"}) - if TTS_str == None: - self.websocket_error_count+=1 - # if self.websocket_error_count <=3: - # EventMesh.publish("audio_montage_play",["xinhaoruo1.mp3"]) - if play_weak_signal_tone and self.websocket_error_count < 10: - EventMesh.publish("audio_montage_play", ["xinhaoruo1.mp3"]) - elif self.websocket_error_count >=10: - # EventMesh.publish("paly_iccid_imei") - EventMesh.publish("audio_montage_play", ["xinhaoruo1.mp3"]) - # else: - # EventMesh.publish("audio_montage_play",["R_SIM_FAILE.mp3"]) - #self.aud.play(1,1,'U:/static/R_SIM_FAILE.mp3') - if self.websocket_error_count <3 : - utime.sleep_ms(3000) - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - else: - print("websocket_error:tts:{}".format(TTS_str)) - self.AI_TTS_str = TTS_str - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - self.Close_flag = 1 - print("websocket_error2{}".format(self.websocket_error_count)) - - - print("websocket_error4") - - - def websocket_deconnect_VC(self): - #print("websocket_deconnect_VC {}!!!!!!!".format(self.AI_client_VC_isconnect)) - print("websocket_deconnect_VC!!!!!!!") - self._vc_session_started = 0 - # 断链/模式切换时必须重置“上行录音状态”,否则会被 _uplink_request_start 的幂等门控挡住, - # 表现为:切回长对话后无法自动开麦,按键 cancel/canceled 也无法恢复。 - try: - self.VC_record_state_flag = 0 - except Exception: - pass - try: - self._server_vad_silence = 0 - except Exception: - pass - try: - self._ptt_audio_created = 0 - except Exception: - pass - try: - # 若录音线程/录音设备仍在占用,先停掉 - self.VC_Speech_End() - except Exception: - try: - self.VC_record_thread_close() - except Exception: - pass - # 断链必须同步重置 FSM,否则 await_played/turn 残留会导致动态切换后“无法自动开麦/按键无效” - try: - self.fsm.session = VCSessionState.Disconnected - self.fsm.turn = VCTurnState.Idle - self.fsm.await_played = False - self.fsm.last_tts_completed_ms = 0 - self.fsm.last_llm_ms = 0 - self.fsm.reset_rx_phases() - except Exception: - pass - self.AI_client_VC_recv_thread_close()#关闭读线程 - self.AI_client_VC_recv_id = 0 - if self.AI_client_VC_isconnect == 1 or self.AI_client_VC!=None: - try: - self.AI_client_VC_isconnect = 0 - count = 500 - while True:#等待发完数据 - count-=1 - if count <=0 or self.AI_VC_send_data_queue_thread_lock == 0: - break - utime.sleep_ms(1) - - try: - self.AI_client_VC.close() - self.AI_client_VC_debug -=1 - #self.AI_client_VC=None - except Exception as e: - usys.print_exception(e) - try : - self.AI_client_VC.close() - self.AI_client_VC_debug -=1 - #self.AI_client_VC=None - except Exception as e: - usys.print_exception(e) - try : - self.AI_client_VC._close() - except Exception as e: - usys.print_exception(e) - self.AI_client_VC = None - except Exception as e: - usys.print_exception(e) - print("websocket_deconnect_VC {}!!!!!!!".format(self.AI_client_VC_isconnect)) - #websocket发送ping - def AI_client_VC_ping(self): - pass - if self.AI_client_VC_isconnect == 1: - #self.AI_client_VC.write_frame(0x09,b'ping ping') - self.AI_client_VC_send("ping") - if VC_HEARTBEAT_LOG: - print("VC: ping ping") - - #websocket发送数据 - def AI_client_VC_send(self,data_str): - # if isinstance(data_str, str):#是字符串 - # print("AI_client_VC_send:{}".format(data_str)) - # pass - # else: - # print("AI_client_VC_send:len{}".format(len(data_str))) - # 统计“语音上行”真实开始/结束:这里只统计二进制音频(非 ping/字符串) - try: - if not isinstance(data_str, str): - l = len(data_str) - if l > 0: - self._uplink_mark_start(l) - self._uplink_bytes += l - except Exception: - pass - self.AI_VC_send_data_queue.put([AI_client_VC_num,data_str]) - #关闭数据发送线程 - def AI_VC_send_data_queue_thread_close(self): - if self.AI_VC_send_data_queue_thread_id != 0: - # 先清空待发队列,避免关闭过程持续堆积/刷屏 - if self.AI_VC_send_data_queue.empty() != True: - self.AI_VC_send_data_queue.clear() - # 通知发送线程退出;若发送线程正阻塞在底层 send,则只能等超时或强制 stop_thread - self.AI_VC_send_data_queue_thread_lock = 2 - count = 500 - while True: - count -= 1 - if self.AI_VC_send_data_queue_thread_lock == 0 or count <=0: - break - else: - # 该打印只是“等待线程停下来”的调试心跳,默认关闭以免影响系统实时性 - if VC_HEARTBEAT_LOG and (count % 50 == 0 or count > 490): - print("AI_VC_send_data_queue_thread going:{}...".format(count)) - utime.sleep_ms(5) - self.AI_VC_send_data_queue_thread_lock = 0 - if VC_HEARTBEAT_LOG: - print("AI_VC_send_data_queue_thread_close") - if self.AI_VC_send_data_queue_thread_id != 0: - try: - # 线程可能已自然退出/被复用,stop_thread 会抛 ValueError - try: - running = _thread.threadIsRunning(self.AI_VC_send_data_queue_thread_id) - except Exception: - running = True - if running: - _thread.stop_thread(self.AI_VC_send_data_queue_thread_id) - except Exception as e: - usys.print_exception(e) - self.AI_VC_send_data_queue_thread_id = 0 - #发送数据队列 - def AI_VC_send_data_queue_thread(self): - print("AI_VC_send_data_queue_thread:start") - count_t = 0 - while True: - if self.AI_VC_send_data_queue_thread_lock == 2: - print("AI_VC_send_data_queue_thread,该退出了") - break - if self.AI_VC_send_data_queue.empty() :#不能去掉,去掉会卡死 - self.AI_VC_send_data_queue_thread_lock = 0 - count_t+=1 - # if count_t%200 == 0: - # print("self.AI_VC_send_data_queue.empty") - utime.sleep_ms(1) - else: - #data[0]是websocket连接对象编号,data[1]是数据 - #print("AI_VC_send_data_queue_thread:get") - - self.AI_VC_send_data_queue_thread_lock = 1 - data = self.AI_VC_send_data_queue.get() - - - #print("AI_VC_send_data_queue_thread:num{} len{}".format(data[0],len(data[1]))) - if data != None: - try: - #data["func"](data["data"]) - # 发送数据 - #bytes_data = bytes(data[1]) - #data[0].send(data[1]) - #print("AI_VC_send_data_queue_thread:len:{}".format(len(data[1]))) - #print("AI_VC_send_data_queue_thread on line asr recv :{} vc recv:{}".format(_thread.threadIsRunning(self.AI_client_asr_recv_id),_thread.threadIsRunning(self.AI_client_VC_recv_id))) - if data[0] == AI_client_VC_num: - - if isinstance(data[1], str):#是字符串 - self.VC_pingping_timerout = 0 - s = data[1] - if s is None: - s = "" - # 保活 ping 不落 WS IO 日志(避免周期性刷屏) - if VC_WS_IO_LOG and s != "ping": - try: - if len(s) > 240: - preview = s[:160] + " ... " + s[-60:] - else: - preview = s - print("[VC] WS send_txt len={} txt={}".format(len(s), preview)) - except Exception: - try: - print("[VC] WS send_txt {}".format(data[1])) - except Exception: - pass - elif not VC_WS_IO_LOG: - if s == "ping": - pass - else: - try: - ev, tid = _vc_out_json_event_meta(s) - if ev: - print("[VC][proto] tx event={} trace_id={} len={} ws_conn={}".format( - ev, tid or "-", len(s), self.AI_client_VC_isconnect)) - else: - print("[VC][proto] tx unknown_json len={} ws_conn={}".format( - len(s), self.AI_client_VC_isconnect)) - except Exception: - print("[VC][proto] tx parse_fail len={}".format(len(s))) - else: - pass - # if VC_AUDIO_FRAME_LOG: - # print("VC: send_data len:{}".format(len(data[1]))) - # print("self.AI_client_VC = {} , self.AI_client_VC_isconnect = {}".format(self.AI_client_VC,self.AI_client_VC_isconnect)) - if self.AI_client_VC != None and self.AI_client_VC_isconnect == 1: - self.AI_client_VC.send(data[1]) - else: - try: - if isinstance(data[1], str) and data[1] != "ping": - _ev, _tid = _vc_out_json_event_meta(data[1]) - print("VC VC: send_data:ERROR len={} event={} trace_id={}".format( - len(data[1]), _ev or "?", _tid or "-")) - else: - print("VC VC: send_data:ERROR:{}".format(len(data[1]))) - except Exception: - print("VC VC: send_data:ERROR:{}".format(len(data[1]))) - #print("VC: send_data end") - - #print("VC: send_data end") - except Exception as e: - print("DeviceInfo_queue_thread:ERROR:{}".format(e)) - usys.print_exception(e) - if data[0] == AI_client_VC_num: - #self.websocket_error() - pass - - - try: - if self.AI_client_VC_isconnect != 0: - print("VC发送数据错误,退出") - EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error,"data":"VC发送出错了,请重试"}) - self.ud_error(["VC",self.VC_taskid]) - except Exception as e: #网络连接异常 - usys.print_exception(e) - - utime.sleep_ms(1) - self.AI_VC_send_data_queue_thread_lock = 0 - #VC关闭读线程 - def AI_client_VC_recv_thread_close(self): - if self.AI_client_VC_recv_id != 0: - count =500 - while True: - count -= 1 - if self.AI_client_VC_recv_lock == 1: - self.AI_client_VC_recv_lock = 2 - if self.AI_client_VC_recv_lock == 0 or count <=0: - break - else: - print("AI_client_VC_recv_thread going:{}...".format(count)) - utime.sleep_ms(1) - self.AI_client_VC_recv_lock = 0 - print("AI_client_VC_recv_thread_close") - if self.AI_client_VC_recv_id!= 0: - try: - _thread.stop_thread(self.AI_client_VC_recv_id) - except Exception as e: - usys.print_exception(e) - self.AI_client_VC_recv_id = 0 - #Ai VC websocket返回数据 - def AI_client_VC_recv(self,cli): - count = 0 - asr_led_flag = 0 - tts_led_flag = 0 - while True: - try: - # 死循环接收数据 - self.AI_client_VC_recv_lock = 0 - - count+=1 - if self._vc_tts_pipeline_busy() and (self.tts_in_data_len - self.tts_out_data_len > 64*1024 or count>5): - count = 0 - utime.sleep_ms(1) - if not self._vc_tts_pipeline_busy(): - utime.sleep_ms(1) - recv_data = cli.recv() - if self.AI_client_VC_recv_lock == 2: - print("AI_client_VC_recv 该退出了") - break - self.AI_client_VC_recv_lock = 1 - if self._vc_rx_resets_long_idle_timer(recv_data): - self.close_timerout = 0 - if recv_data == "websocket close": - # 服务器关闭连接 - print("VC 服务器关闭连接!!!!!!!!!!!!!!!!!!!!!!!!!!!") - #self.AI_client_VC_isconnect = 0 - #self.websocket_deconnect_VC() - #EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_asr}) - # #EventMesh.publish("publish_AI_btnInfo",{"func":utime.sleep_ms,'data':(1500)}) - #EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_connect_asr}) - EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error,"data":"VC连接断开了,请重试"}) - self.ud_error(["VC",self.VC_taskid]) - break - elif recv_data == "":#客户端关闭连接 - print("VC VC :客户端关闭连接") - EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error,"data":"VC连接断开了,请重试"}) - self.ud_error(["VC",self.VC_taskid]) - break - try: - self.websocket_error_count = 0 - if isinstance(recv_data, str):#返回的是字符串 - jsload_data = ujson.loads(recv_data) - if VC_WS_IO_LOG: - print("[VC] recv raw:{}".format(recv_data)) - else: - try: - _rev = jsload_data.get("event", "") - if _rev != "llm.delta": - print("[VC] rx event={} sid={}".format( - _rev, jsload_data.get("sid", "") or "-")) - except Exception: - pass - #print("AI_client_VC_recv jsload_data = {}".format(jsload_data)) - try: - self._cur_sid = jsload_data.get("sid", "") or "" - except Exception: - self._cur_sid = "" - try: - ev = jsload_data.get("event", "") - if ev: - self._vc_dispatch_server_event(ev, jsload_data) - except Exception: - pass - - if jsload_data["event"] == "started" :#会话开始 - #payload = jsload_data["payload"] - # self.VC_dialog_id = payload["dialog_id"] - # self.VC_task_id = jsload_data["task_id"] - # started 仅表示链路就绪:不在这里提前切灯态; - # 聆听灯只在“录音真正开始/停止”处更新(VC_Speech_Start/End)。 - # 会话开始:允许上行;连续对话模式下直接进入“可发送音频”状态 - self._vc_session_started = 1 - # started 时不要强制清 Close_flag: - # - Close_flag==1:提示音/模式切换播完退出 - # - Close_flag==0:正常对话流程 - self._ptt_audio_created = 0 - self._server_vad_silence = 0 - self._turn_has_asr_completed = 0 - self._turn_has_vad_silence = 0 - self._turn_has_tts_started = 0 - self._llm_completed_ms = 0 - if self.VC_mode == 0 and getattr(self, "Close_flag", 0) == 0: - _skip_uplink = 0 - try: - if getattr(self, "_skip_started_uplink_once", 0) == 1: - self._skip_started_uplink_once = 0 - _skip_uplink = 1 - print("[VC][uplink_ctrl] Deny START by server.started cause=boot_prompt") - except Exception: - pass - if not _skip_uplink: - # 会话已 started:若此时上行未启动,则在这里补一次开麦,避免“Start 前开麦被 Close 停掉”后 - # 出现首轮无音频上行导致“第一次对话没有”的情况。 - if self.record_data_save_flag == 0: - if self.VC_record_state_flag == 0: - self.request_uplink_start("server.started") - else: - self._vc_clear_stale_record_state("server.started") - self.request_uplink_start("server.started/retry") - - print("VC:Started!!!!!!!!\r\n") - print("TTS音频为:{},语音下发地址为:{}".format(self.AI_TTS_str,self.AI_new_voice_url)) - if self.AI_new_voice_url != "" and self.alarm_play_flag == 1 :#有闹钟,先响闹钟 - self.AI_VC_start() - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放铃声 - self.alarm_play_flag = 0 - self.voice_choose_flag = 1 - elif self.AI_TTS_str == "" and self.AI_new_voice_url != "" and self.music_play_flag == 0: - # if EventMesh.publish("get_history_voice_flag") == 1: - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 - # self.recv_voice = 0#当留言列表为空,留言播放标志清空 - # else: - # self.voice_cnt = 0 - # print("定时器开启222222222") - # self.voice_timer.start(1000,1,self.voice_judge) - elif self.AI_TTS_str != "" and self.AI_new_voice_url == "": - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_synthesis}) - - elif self.AI_TTS_str != "" and self.AI_new_voice_url != "" and self.music_play_flag == 0:#遇到tts和语音留言冲突时(并且没有音乐播放) - if EventMesh.publish("get_history_voice_flag") == 1:#播放历史留言 - if EventMesh.publish("get_wait_voice_flag") == 0: - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_synthesis}) - self.his_cnt = 0 - self.his_timer.start(1000,1,self.history_play) - self.voice_choose_flag = 1 - else:#等待留言 - if self.pwk_notice_flag == 0: - self.wait_cnt = 0 - print("未听播放1") - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_synthesis}) - self.wait_timer.start(1000,1,self.wait_play) - self.voice_choose_flag = 1 - - else:#已确定播放 - print("未听播放2") - self.wait_cnt = 0 - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_synthesis}) - self.wait_timer.start(1000,1,self.wait_single_play) - self.voice_choose_flag = 1 - else:#播放正常留言 - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_synthesis}) - self.voice_cnt = 0 - print("定时器开启111111111") - self.voice_timer.start(1000,1,self.voice_judge) - self.voice_choose_flag = 1 - # self.AI_TTS_str = "" - elif self.AI_TTS_str != "" and self.AI_new_voice_url != "" and self.music_play_flag == 1 :#遇到tts和语音留言冲突时(并且有音乐播放) - self.music_play_flag = 0 - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_synthesis}) - self.music_timer.start(1000,1,self.music_play) - self.voice_choose_flag = 1 - else: - #开始上传语音 - try: - if "vc_config" in globals() and vc_config and vc_config.get_bool("UPLOAD_START_TONE_ENABLED", False): - self.chenggong_tone() - except Exception: - pass - if self.VC_mode == 0: - self.request_uplink_start("server.started/idle") - else: - # Push-To-Talk:必须先同步发出 audio.create,再开麦。 - # request_send_audio_create() èµ° EventMesh 异步队列,会导致录音线程先于 - # audio.create 上行,深度睡眠唤醒后首轮 ASR 空结果、第二轮才正常。 - try: - self.AI_client_VC_send_speech() - except Exception: - self.request_send_audio_create() - # 若长按发生在“未 started”窗口,收到 started 后立即开麦进入新一轮 PTT - try: - if getattr(self, "_ptt_force_record_on_started", 0) == 1: - self._ptt_force_record_on_started = 0 - self.VC_Speech_Start_now() - except Exception: - pass - - - # if self.AI_new_voice_url != "": - # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice}) - - elif jsload_data["event"] == "config.update" :#配置更新中,需要停止录音 - try: - self.fsm.reset_rx_phases() - except Exception: - pass - self.request_uplink_stop("config.update") - - elif jsload_data["event"] == "asr.started" :#asr开始 - try: - self.fsm.on_server_asr_started() - except Exception: - pass - self._server_vad_silence = 0 - self._turn_has_asr_completed = 0 - self._turn_has_vad_silence = 0 - self._turn_has_tts_started = 0 - self._llm_completed_ms = 0 - try: - self._asr_completed_tone_done = 0 - except Exception: - pass - # 单对话:_drop_tts_audio 在 VC_Speech_Start 真开录后再清 0,避免 canceled 后仍播旧 TTS - try: - if getattr(self, "VC_mode", 0) != 1: - self._drop_tts_audio = 0 - except Exception: - pass - # 单对话:勿在 asr.started ç½® flag=2(未真开麦会僵死);由 VC_Speech_Start 维护 - self.LocalRespondingEnded_timerout = 0 - elif jsload_data["event"] == "asr.result" :#asr结果 - try: - self.AI_ASR_str = jsload_data["payload"]["content"] - except Exception: - try: - self.AI_ASR_str = jsload_data.get("payload", {}).get("text", "") or "" - except Exception: - self.AI_ASR_str = "" - print("[VC][ASR] {}".format(self.AI_ASR_str)) - try: - if (self.AI_ASR_str or "").strip(): - self._last_asr_result_text = self.AI_ASR_str - except Exception: - pass - if self.AI_ASR_str == "":#没识别到 - self.AI_TTS_str = "我没听清,请再说一遍" - self._vc_led_alert_rb_flash() - elif jsload_data["event"] == "asr.completed" :#asr 终止的标识 - self._turn_has_asr_completed = 1 - try: - if not (self.AI_ASR_str or "").strip(): - self.AI_ASR_str = getattr(self, "_last_asr_result_text", "") or "" - except Exception: - pass - # 本地 think 提示:仅在有有效 ASR 文本时播,避免空轮次连播「滴」 - try: - if getattr(self, "_asr_completed_tone_done", 0) == 1: - pass - elif not self._vc_effective_asr_text(): - try: - print("[VC][ASR] completed empty, skip think tone") - except Exception: - pass - else: - stream_on = False - try: - stream_on = EventMesh.publish("get_vc_tts_playback_active") == 1 - except Exception: - stream_on = False - pcm_busy = getattr(self, "play_pcm", None) is not None - playing = getattr(self, "VC_Play_flag", 0) == 1 - try: - thinking = getattr(self.fsm, "turn", VCTurnState.Idle) == VCTurnState.Thinking - except Exception: - thinking = False - if (not stream_on) and (not pcm_busy) and (not playing) and (not thinking): - self._vc_local_play('U:/static/think.mp3', 1, 1, 650) - self._asr_completed_tone_done = 1 - except Exception: - pass - print("self.commands_mode:{}".format(self.commands_mode)) - if self.commands_mode ==2: - ret= self.check_text(self._vc_effective_asr_text()) - if ret == 0:#配置指令 - - if self.AI_VC_send_data_queue.empty()!=True: - self.AI_VC_send_data_queue.clear() - if self.AI_ASR_send_data_queue.empty()!=True: - self.AI_ASR_send_data_queue.clear() - self.AI_ASR_send_buf_len = 0 - EventMesh.publish("set_led_state", {"ai_state": "thinking"}) - try: - - self.request_uplink_stop("asr.completed/config_cmd") - EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - except Exception as e: - usys.print_exception(e) - continue - elif ret == 1:#不处理 - pass - #self.VC_Speech_End() - #EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) - - # if self.record_data_save_flag == 1: - # if self.record_fd : - # self.record_fd.close() - # self.record_fd = None - - # if dev_type != "7PIN" : - #EventMesh.publish("publish_AI_btnInfo",{"func":self.send_tone}) - elif jsload_data["event"] == "vad.active" :#VAD 检测到说话→聆听 - try: - if "vc_config" in globals() and vc_config and vc_config.get_bool("VAD_ACTIVE_BLUE_LED", True): - self._server_vad_silence = 0 - except Exception: - pass - elif jsload_data["event"] == "vad.silence" :#VAD 终止的标识 - # 说明服务端已判定“本轮说话结束”;此时继续上行只会干扰后续(例如推迟 TTS)。 - # 仅在“正在录音上行”的语音阶段才停止,避免误触发。 - self._server_vad_silence = 1 - self._turn_has_vad_silence = 1 - self._uplink_mark_end("vad.silence") - if self.record_data_save_flag == 0: - if self.VC_record_state_flag != 0 and self._vc_vad_uplink_stop_eligible(): - self.request_uplink_stop("vad.silence") - elif jsload_data["event"] == "llm.created" :#思考(LLM) - try: - self._ptt_await_server = 0 - except Exception: - pass - try: - EventMesh.publish("set_led_state", {"ai_state": "thinking"}) - except Exception: - pass - self._llm_completed_ms = 0 - # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Play_Start}) - pass - elif jsload_data["event"] == "llm.completed" :#llm完成(可能无 tts) - try: - self._llm_completed_ms = utime.ticks_ms() - except Exception: - # 勿用 1:ticks_diff(now,1)≈开机至今,会瞬间满足 thinking 超时误以为该重启 - self._llm_completed_ms = 0 - try: - self._cur_dialog_id = jsload_data.get("payload", {}).get("dialog_id", "") or "" - except Exception: - self._cur_dialog_id = "" - try: - self.fsm.log("server.llm.completed") - except Exception: - pass - pass - elif jsload_data["event"] == "llm.delta" :#llm返回数据 - if self.is_VAD == 0 and self.record_data_save_flag == 0: - if self.VC_record_state_flag == 1:#llm返回数据还没停止,就先停止 - self.request_uplink_stop("llm.delta") - elif jsload_data["event"] == "tts.started" :#tts开始 - try: - self._ptt_await_server = 0 - except Exception: - pass - self._server_vad_silence = 1 - self._turn_has_tts_started = 1 - self._tts_pending = 0 - self._tts_pending_ms = 0 - self._uplink_mark_end("tts.started") - # stop uplink + play_start 由 FSM action list 统一触发,避免多处重复调用 - # 标记本轮 TTS 来源: - # - 有 payload.dialog_id:认为是“对话回复 TTS” - # - 无 dialog_id:认为是“提示/提醒 TTS”(模式切换可通过 _pending_tts_kind 标记) - try: - did = jsload_data.get("payload", {}).get("dialog_id", "") or "" - except Exception: - did = "" - # 注意:无 dialog_id 的 tts.started 很可能是提示音/提醒音; - # 但“模式切换提示音”的 kind 可能已在 synthesis 时写入 _last_tts_kind, - # 这里不应无条件覆盖为 prompt,否则播完无法触发下一轮开麦。 - try: - if did: - self._last_tts_kind = "dialog" - else: - k = getattr(self, "_pending_tts_kind", "") or "" - if k: - self._last_tts_kind = k - else: - cur = getattr(self, "_last_tts_kind", "") or "" - if cur in ( - "mode_switch_long", - "mode_switch_ptt", - "boot_prompt", - ): - pass - elif not cur: - self._last_tts_kind = "prompt" - except Exception: - pass - try: - self._pending_tts_kind = "" - except Exception: - pass - # 播放灯由阶段事件控制 - elif jsload_data["event"] == "tts.completed" :#tts结束 - # tts.completed 仅表示下发结束,本地可能仍在播放;绿灯关闭在本地播放结束时处理 - try: - # server event 已在统一入口 on_server_event 触发 FSM,这里避免重复驱动 - pass - except Exception: - pass - try: - try: - sid = getattr(self, "_cur_sid", "") - did = getattr(self, "_cur_dialog_id", "") - except Exception: - sid, did = "", "" - print("[TTS] completed in_len={} out_len={} q_empty={} play_pcm={} sid={} did={}".format( - getattr(self, "tts_in_data_len", 0), - getattr(self, "tts_out_data_len", 0), - 1 if self.AI_audio_queue.empty() else 0, - 1 if self.play_pcm != None else 0, - sid, - did, - )) - except Exception: - pass - # 播完上报完全由 FSM await_played + play_end 驱动,这里不再使用 LocalRespondingEnded_timerout/_need_audio_played_ack - pass - - elif jsload_data["event"] == "canceled" :#打断 - # 停播/清队列已在 _vc_dispatch_server_event(canceled) 完成;此处仅收尾标志 - self.is_ruppt_tts = 0#设备上发中断tts,收到服务器返回的cancel后置为0 - self._need_audio_played_ack = 0 - self._tts_pending = 0 - self._tts_pending_ms = 0 - # 开麦由 FSM action list 统一触发(uplink_start:canceled),避免重复调用 - if self.AI_audio_queue_thread_id ==0: - _thread.stack_size(24*1024) - self.AI_audio_queue_thread_id = _thread.start_new_thread(self.AI_audio_queue_thread, ()) - _thread.stack_size(8*1024) - print("AI_VC_start :AI_audio_queue_thread_id :{} ".format(self.AI_audio_queue_thread_id)) - else: - print("AI_VC_start :AI_audio_queue_thread_id != 0 ") - elif jsload_data["event"] == "completed" :#会话结束(可能无 TTS,如 ASR 空) - # 重要:服务端常在 tts.completed 后紧跟 completed。 - # 若本轮存在 TTS/待发 audio.played,则不能在此清零 LocalRespondingEnded_timerout/_need_audio_played_ack, - # 否则会掐断「播完→audio.played→下一轮录音」链路,表现为最多 1~2 轮就停。 - if self._need_audio_played_ack == 1 or self._vc_tts_pipeline_busy() or self.VC_Play_flag == 1: - pass - else: - # 无 TTS 的 completed:清状态并按需进入下一轮(见下方条件) - self.LocalRespondingEnded_timerout = 0 - self._need_audio_played_ack = 0 - try: - self.fsm.reset_rx_phases() - except Exception: - pass - # 连续对话:若本轮没有 TTS(或服务端直接 completed),需要立刻回到聆听状态 - # 否则会出现“有时 1 è½®/2 轮后就停住”的现象(服务端 completed 但端侧未重新开录音上行)。 - if self.VC_mode == 0 and self.Close_flag == 0: - # 仅当“本轮已结束一次语句(vad.silence 或 asr.completed)且从未进入 tts.started” - # 才自动进入下一轮;避免把真正的“会话结束/异常结束”误当成下一轮入口。 - if (self._turn_has_tts_started == 0) and (self._turn_has_vad_silence == 1 or self._turn_has_asr_completed == 1): - self._server_vad_silence = 0 - # 开麦由 FSM action list 统一触发(uplink_start:completed),这里不再重复调用 - pass - - elif jsload_data["event"] == "command" :#命令匹配 - ret = -1 - try: - if self.commands_mode ==1: - if len(jsload_data["payload"]["functions"])>0: - - cmd = jsload_data["payload"]["functions"][0]["function"]["arguments"] - print("llm:cmd:{}".format(cmd)) - cmd = ujson.loads(cmd) - ret = self.check_text_llm(cmd) - elif self.commands_mode ==0: - - cmd = jsload_data["payload"] - print("server:cmd:{}".format(cmd)) - ret = self.check_text_server(cmd) - else: - cmd = jsload_data["payload"]["functions"][0]["function"]["arguments"] - print("llm:cmd:{}".format(cmd)) - cmd = ujson.loads(cmd) - ret = self.check_text_llm(cmd) - except Exception as e: - usys.print_exception(e) - if ret == -1: - if self.AI_TTS_str =="": - self.AI_TTS_str = "抱歉,没找到对应操作,可以换个说法试试" - #EventMesh.publish("publish_AI_btnInfo",{"func":utime.sleep_ms,'data':(10)}) - self.request_uplink_stop("command") - EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - elif jsload_data["event"] == "failed":#接口返回错误 - if jsload_data["payload"]["error"] == "concurrency_limit_exceeded": - EventMesh.publish("publish_AI_btnInfo",{"func":self.concurrency_limit_exceeded_tone}) - - #utime.sleep_ms(4000) - - else: - EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error,"data":jsload_data["payload"]["message"]}) - pass - - elif jsload_data["event"] == "audio.convert.completed":#音频转成完成没有出错 接口 - # self.VC_state = VC_State_End - print("未读留言初始数值:{}".format(self.pwk_cnt_voice)) - if self.pwk_notice_flag == 1:#只有在播放的时候才会去计数 - if self.pwk_cnt_voice > 0: - self.pwk_cnt_voice -= 1#减1 - print("未读留言又-1,当前留言剩余数量为:{}".format(self.pwk_cnt_voice)) - else: - print("未读列表为空") - if self.pwk_cnt_voice == 0:#积累的留言已经全部播完 - self.pwk_notice_flag = 0 - self.wait_sort = 0 - self.wait_first_flag = 0#让下次未读播放提示恢复 - self.voicerupt_flag = 0 #标志位置0 - self.wait_voicerupt_flag = 0 - self.voice_notice = 1#开启回复 - if self.history_voice_flag == 1 and self.wait_voice_flag == 0: - self.voice_notice = 0 - EventMesh.publish("set_storage_config",["is_noread_voice",0]) - - if self.voice_choose_flag == 1:#播放完语音后,有冲突的时候关闭 - self.voice_choose_flag = 0 - self.Close_flag = 1 - self.LocalRespondingEnded_timerout = 8 - # EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) - if self.voice_notice == 1:#已收到留言,进行该留言回复逻辑处理 - tts_play_str = "如需回复,长按确认键开始录制,松开发送。十秒内无操作自动退出" - EventMesh.publish("play_tts",[tts_play_str,1]) - self.voice_notice = 0#标志位清空 - self.voice_timer.stop() - self.revoice_cnt = 0 - self.voice_flag = 1 - print("语音留言回复停止") - self.revoice_timer.start(1000,1,self.revoice_judge) - print("是否未读:{},是否播完:{}".format(EventMesh.publish("get_wait_voice_flag"),len(EventMesh.publish("get_play_new_voice_list")))) - # if EventMesh.publish("get_wait_voice_flag") == 1 and len(EventMesh.publish("get_play_new_voice_list")) ==0:#还在未读并且列表为空 - # self.wait_first_flag = 0 - # print("未读留言播完,清空标志") - # EventMesh.publish("set_wait_voice_flag",0) - # EventMesh.publish("set_history_voice_flag",0) - EventMesh.publish("update_play_new_voice_info")#更新已读状态 - self.recv_voice = 0 - print("不开启定时器标志位置0") - self.LocalRespondingEnded_timerout = 8 - try: - self.fsm.reset_rx_phases() - except Exception: - pass - - pass - elif jsload_data["event"] == "audio.convert.failed": #音频转换失败了 - pass - - - else:#下发音频数据 - #self.AI_ASR_str = ""#下发音频后清掉asr结果 - - - # PTT 打断后丢弃残留 TTS 音频帧,避免“断断续续继续播” - try: - if self._ptt_should_drop_inbound_tts(): - continue - except Exception: - pass - if self.VC_stop_play_audio_flag == 0: - print("新的AI对话不允许以前的播放数据:") - break - - # if VC_AUDIO_FRAME_LOG: - # print("下发音频数据1:{}".format(len(recv_data))) - - if self.play_data_save_flag == 1: - if self.play_fd ==None: - try: - uos.remove('/usr/play_data.pcm') - except Exception as e: - usys.print_exception(e) - self.play_fd = open('/usr/play_data.pcm','w') - if self.play_fd !=None: - self.play_fd.write(recv_data) - # print("下发音频数据2:{}".format(len(recv_data))) - #if self.VC_state >= VC_State_TTSStart: - #self.put_record_data([recv_data,""]) - if self.is_ruppt_tts == 0:#被设备主动打断打断则不播放,知道收到服务器返回的cancel才修改标志位 - if self.VC_play_TYPE == VC_play_TYPE_PCM: - if self.play_pcm != None: - try: - #print("play_pcm.stream_write !!!") - buf_len = self.play_pcm.stream_get_buf_len() - count_tmp = 1000 - while buf_len < len(recv_data): - utime.sleep_ms(1) - count_tmp-=1 - if count_tmp<=0 or self.play_pcm == None: - break - buf_len = self.play_pcm.stream_get_buf_len() - #print("buf已满,等待释放:buf_len:{},count_tmp:{}".format(buf_len,count_tmp)) - except Exception as e: - usys.print_exception(e) - EventMesh.publish("publish_AI_btnInfo",{"func":self.put_record_data,"data":[recv_data,""]}) - else: - print("被打断不播放") - utime.sleep_ms(1) - except Exception as e: - usys.print_exception(e) - - except Exception as e: - error_str = str(e) - #print("VC VC:e:{}".format(error_str)) - if error_str.find("[Errno 110] ETIMEDOUT")!= -1 or error_str.find("[Errno 115] ")!= -1: - if VC_HEARTBEAT_LOG: - print("VC VC:recv 正常报错:{}".format(error_str)) - # recv 超时仅表示本周期无帧,链路仍可能在等 LLM/TTS;清零避免误触发「接口无响应」 - self.close_timerout = 0 - pass - else: - usys.print_exception(e) - #self.websocket_error() - print("VC VC :读数据报错,退出") - EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error,"data":"VC出错了,请重试"}) - self.ud_error(["VC",self.VC_taskid]) - break - self.AI_client_VC_isconnect = 0 - self.AI_client_VC_recv_id = 0 - self.AI_client_VC_recv_lock = 0 - print("[VC] recv_thread exit") - - def voice_play_10S(self,event,msg):#按键开启留言播放等待10S定时器判断 - self.voice_timer.start(1000,1,self.voice_judge) - #语音留言定时器 - def voice_judge(self, *args, **kwargs): - EventMesh.publish("publish_AI_btnInfo",{"func":self.voice_judger_cb}) - def voice_judger_cb(self): - self.voice_cnt += 1 - # print("语音留言定时器cnt = {}".format(self.voice_cnt)) - if self.voicerupt_flag == 1:#按键被按下 - self.pwk_notice_flag = 1#表示进入收听未听留言 - self.AI_VC_start() - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 - self.voice_cnt = 0 - # self.recv_voice = 0#进入留言定时器,留言清空 - print("进入留言定时器1,留言清空!!!!!") - EventMesh.publish("update_play_new_voice_info")#更新已读状态 - self.voice_timer.stop() - if self.pwk_cnt_voice > 0: - EventMesh.publish("get_new_voice_msg_S") - if self.voice_cnt > 15:#10S后退出播放,5S是为了给语音提示留时间 - self.voice_cnt = 0 - self.recv_voice = 0#进入留言定时器,留言清空 - if self.voice_choose_flag == 1:#播放完语音后,有冲突的时候关闭 - self.voice_choose_flag = 0 - print("进入留言定时器2,留言清空!!!!!") - self.voice_timer.stop() - #语音留言回复定时器 - def revoice_judge(self, *args, **kwargs): - EventMesh.publish("publish_AI_btnInfo",{"func":self.revoice_judger_cb}) - def revoice_judger_cb(self): - self.revoice_cnt += 1 - # print("语音留言回复定时器cnt = {}".format(self.revoice_cnt)) - if self.revoice_cnt > 20:#10S后退出播放,10S是为了给语音提示留时间 - self.revoice_cnt = 0 - self.voice_flag=0 - EventMesh.publish("set_1M_nowplay_flag",0) - print("语音留言回复停止") - self.revoice_timer.stop() - #音乐播放定时器 - def music_play(self, *args, **kwargs): - EventMesh.publish("publish_AI_btnInfo",{"func":self.music_play_cb}) - def music_play_cb(self): - self.music_cnt += 1 - if self.music_cnt > 4:#4S后播放音乐,4S是为了给语音提示留时间 - self.AI_VC_start() - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 - self.music_cnt = 0 - self.music_timer.stop() - #历史留言播放定时器 - def history_play(self, *args, **kwargs): - EventMesh.publish("publish_AI_btnInfo",{"func":self.history_play_cb}) - def history_play_cb(self): - self.his_cnt += 1 - if self.his_cnt > 4:#4S后播放,4S是为了给语音提示留时间 - self.AI_VC_start() - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 - self.his_cnt = 0 - self.his_timer.stop() - #未读留言是否播放定时器 - def wait_play(self, *args, **kwargs): - EventMesh.publish("publish_AI_btnInfo",{"func":self.wait_play_cb}) - def wait_play_cb(self): - self.wait_cnt += 1 - if self.wait_voicerupt_flag == 1:#按键被按下 - EventMesh.publish("set_is_listen_wait",1)#已听 - self.pwk_notice_flag = 1#表示进入收听未听留言 - self.AI_VC_start() - self.wait_sort = 1 - self.AI_TTS_str = "第"+str(self.wait_sort)+"条" - # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 - # self.wait_cnt = 0 - # print("进入留言定时器1,留言清空!!!!!") - # EventMesh.publish("update_play_new_voice_info")#更新已读状态 - self.wait_timer.stop() - if self.wait_cnt > 10:#10S后退出播放,5S是为了给语音提示留时间 - self.wait_cnt = 0 - self.recv_voice = 0#进入留言定时器,留言清空 - EventMesh.publish("set_history_voice_flag",0) - EventMesh.publish("set_wait_voice_flag",0) - EventMesh.publish("set_open_device",0)#开机不听 - self.wait_first_flag = 0 - if self.voice_choose_flag == 1:#播放完语音后,有冲突的时候关闭 - self.voice_choose_flag = 0 - print("进入留言定时器2,留言清空!!!!!") - self.wait_timer.stop() - #未读留言一条条播放定时器 - def wait_single_play(self, *args, **kwargs): - EventMesh.publish("publish_AI_btnInfo",{"func":self.wait_single_play_cb}) - def wait_single_play_cb(self): - self.wait_cnt += 1 - if self.wait_cnt > 2:#2S后播放,2S是为了给语音提示留时间 - self.AI_VC_start() - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 - self.wait_cnt = 0 - self.wait_timer.stop() - def set_voicerupt_flag(self,event,msg):#按键控制当前留言播放 - self.voicerupt_flag = msg - def set_wait_voicerupt_flag(self,event,msg):#按键控制未读留言播放 - self.wait_voicerupt_flag = msg - def get_voicerupt_flag(self,event,msg):#获取按键控制留言播放状态 - return self.voicerupt_flag - def get_recv_voice(self,event,msg): - return self.recv_voice - def set_recv_voice(self,event,msg): - return self.recv_voice - def put_record_data(self,data): - try: - if self._ptt_should_drop_inbound_tts(): - return - except Exception: - pass - #print("下发音频数据1:{}".format(len(data[0]))) - ret=self.AI_audio_queue.put(data) - try: - if self.tts_in_data_len == 0 and data and len(data[0]) > 0: - try: - sid = getattr(self, "_cur_sid", "") - did = getattr(self, "_cur_dialog_id", "") - except Exception: - sid, did = "", "" - print("[TTS] first_chunk len={} play_pcm={} VC_Play_flag={} sid={} did={}".format( - len(data[0]), - 1 if self.play_pcm != None else 0, - getattr(self, "VC_Play_flag", 0), - sid, - did, - )) - except Exception: - pass - self.tts_in_data_len += len(data[0]) - #print("下发音频数据2:{},{}".format(len(data[0]),self.tts_in_data_len)) - #msg[0]是tts播放字符串,msg[1]是是否关闭当前tts - def play_tts(self,event = None,msg= None): - EventMesh.publish("publish_AI_btnInfo",{"func":self.play_tts_cb,"data":msg}) - - def play_tts_cb(self,data): - try: - tts_str = data[0] - is_close_VC = data[1] - if tts_str != "": - self.AI_TTS_str = tts_str - if is_close_VC != None: - self.Close_flag = is_close_VC - if self.AI_VC_send_data_queue.empty()!=True: - self.AI_VC_send_data_queue.clear() - if self.AI_ASR_send_data_queue.empty()!=True: - self.AI_ASR_send_data_queue.clear() - try: - if is_close_VC: - EventMesh.publish("set_led_state", {"ai_state": "idle"}) - else: - EventMesh.publish("set_led_state", {"ai_state": "thinking"}) - except Exception: - pass - self.AI_ASR_send_buf_len = 0 - self._uplink_request_stop("play_tts_cb") - self.websocket_deconnect_VC() - self.AI_VC_start() - - # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - except Exception as e: - usys.print_exception(e) - pass - def check_text_server(self,cmd): - return -1 - - def _apply_vc_text_intent(self, text, cmd, llm_style_tts): - """表驱动文本意图(vc_text_intent)。未命中返回 None;命中返回 0/1/-1。""" - if vc_text_intent is None: - return None - try: - try: - _btv = bool("vc_config" in globals() and vc_config and vc_config.get_bool("BT_VOICE_FEATURES_ENABLED", False)) - except Exception: - _btv = False - r = vc_text_intent.match_vc_command(text, {"bt_voice": _btv}) - except Exception as e: - usys.print_exception(e) - return None - if not r: - return None - return self._apply_vc_text_intent_matched(r, cmd, llm_style_tts) - - def _apply_vc_text_intent_matched(self, r, cmd, llm_style_tts): - if r.get("needs_payload") and cmd is None: - return None - it = r["intent"] - if r.get("tts"): - self.AI_TTS_str = r["tts"] - if r.get("close_flag"): - self.Close_flag = 1 - if it == "goodbye": - return 0 - if it == "shutdown": - if llm_style_tts: - EventMesh.publish("publish_AI_btnInfo", {"func": self.Close}) - EventMesh.publish("Power_off") - return 1 - try: - close_only = ("vc_config" in globals() and vc_config and vc_config.get_bool("POWEROFF_CMD_CLOSE_ONLY", True)) - except Exception: - close_only = True - if close_only: - EventMesh.publish("publish_AI_btnInfo", {"func": self.Close}) - return 1 - EventMesh.publish("publish_AI_btnInfo", {"func": self.Close}) - EventMesh.publish("Power_off") - return 1 - if it == "toggle_dialog_mode": - if self.VC_mode == 0: - self.VC_mode = 1 - if llm_style_tts: - self.AI_TTS_str = "了解,已切换到单对话模式" - else: - try: - self.AI_TTS_str = vc_config.get_attr("CMD_TOGGLE_MODE_TO_PTT_TTS", "已切换到单对话模式") - except Exception: - self.AI_TTS_str = "已切换到单对话模式" - else: - self.VC_mode = 0 - if llm_style_tts: - self.AI_TTS_str = "了解,已切换到长对话模式" - else: - try: - self.AI_TTS_str = vc_config.get_attr("CMD_TOGGLE_MODE_TO_LONG_TTS", "已切换到长对话模式") - except Exception: - self.AI_TTS_str = "已切换到长对话模式" - EventMesh.publish("set_storage_config", ["VC_mode", self.VC_mode]) - return 0 - if it == "volume_set": - try: - pj = ujson.loads(cmd["command"]["params_json"]) - volume = pj["n"] - except Exception as e: - usys.print_exception(e) - try: - self.AI_TTS_str = cmd["command"]["fail_prompt"] - except Exception: - self.AI_TTS_str = "" - return -1 - if volume > 100 or volume < 0: - self.AI_TTS_str = "音量调整超出范围了,只能0-100哦" - else: - self.VC_TTS_volume = volume - self.aud.setVolume(self.VC_TTS_volume // 10) - EventMesh.publish("set_storage_config", ["VC_TTS_volume", self.VC_TTS_volume]) - self.AI_TTS_str = "了解,音量已经调整,当前音量是{}".format(self.VC_TTS_volume) - EventMesh.publish("post_device_state") - EventMesh.publish("adjust_volume_toBle", self.VC_TTS_volume) - return 0 - if it == "volume_up": - if llm_style_tts: - try: - print("self.VC_TTS_volume add:{}".format(self.VC_TTS_volume)) - except Exception: - pass - self.VC_TTS_volume += 10 - if self.VC_TTS_volume > 100: - self.AI_TTS_str = "音量已经是最大啦" if llm_style_tts else "音量已最大" - self.VC_TTS_volume = 100 - else: - self.aud.setVolume(self.VC_TTS_volume // 10) - EventMesh.publish("set_storage_config", ["VC_TTS_volume", self.VC_TTS_volume]) - EventMesh.publish("post_device_state") - EventMesh.publish("adjust_volume_toBle", self.VC_TTS_volume) - self.AI_TTS_str = ( - "了解,音量已经调大了,当前音量是{}".format(self.VC_TTS_volume) - if llm_style_tts - else "配置成功,当前音量 {}".format(self.VC_TTS_volume) - ) - return 0 - if it == "volume_down": - if llm_style_tts: - try: - print("self.VC_TTS_volume end:{}".format(self.VC_TTS_volume)) - except Exception: - pass - self.VC_TTS_volume -= 10 - if self.VC_TTS_volume < 0: - self.VC_TTS_volume = 0 - self.AI_TTS_str = "音量已经是最小啦" if llm_style_tts else "音量已最小" - else: - self.aud.setVolume(self.VC_TTS_volume // 10) - EventMesh.publish("set_storage_config", ["VC_TTS_volume", self.VC_TTS_volume]) - EventMesh.publish("post_device_state") - EventMesh.publish("adjust_volume_toBle", self.VC_TTS_volume) - self.AI_TTS_str = ( - "好嘞,音量已经调小了,当前音量是{}".format(self.VC_TTS_volume) - if llm_style_tts - else "配置成功,当前音量 {}".format(self.VC_TTS_volume) - ) - return 0 - if it in ("speech_faster", "speech_rate_up"): - self.speech_rate += 10 - if self.speech_rate > 50: - self.AI_TTS_str = "语速已经是最快了" - self.speech_rate = 50 - else: - EventMesh.publish("set_storage_config", ["speech_rate", self.speech_rate]) - self.AI_TTS_str = "明白,语速已经调快了,当前语速是{}".format(self.speech_rate) - return 0 - if it in ("speech_slower", "speech_rate_down"): - self.speech_rate -= 10 - if self.speech_rate < -50: - self.speech_rate = -50 - self.AI_TTS_str = "语速已经是最慢了" - else: - EventMesh.publish("set_storage_config", ["speech_rate", self.speech_rate]) - self.AI_TTS_str = "明白,语速已经调慢了,当前语速是{}".format(self.speech_rate) - return 0 - if it == "vad_faster": - self.vad_silence -= 200 - if self.vad_silence < 100: - self.AI_TTS_str = "已经是最快了" - self.vad_silence = 100 - else: - self.AI_TTS_str = "配置成功,当前语音断句速度是{}毫秒".format(self.vad_silence) - EventMesh.publish("set_storage_config", ["vad_silence", self.vad_silence]) - return 0 - if it == "vad_slower": - self.vad_silence += 200 - if self.vad_silence > 2000: - self.vad_silence = 2000 - self.AI_TTS_str = "已经是最慢了" - else: - self.AI_TTS_str = "配置成功,当前语音断句速度是{}毫秒".format(self.vad_silence) - EventMesh.publish("set_storage_config", ["vad_silence", self.vad_silence]) - return 0 - if it == "query_signal": - signal = net.csqQueryPoll() - self.AI_TTS_str = "当前信号{}".format(signal) - return 0 - if it == "query_battery": - battery = EventMesh.publish("get_battery") - try: - print("电量查询:{}".format(battery)) - except Exception: - pass - self.AI_TTS_str = "电量剩余百分之{}".format(battery) - return 0 - if it == "query_voltage": - Vbatt = Power.getVbatt() - self.AI_TTS_str = "当前电池电压{}".format(Vbatt) - return 0 - if it == "query_iccid": - iccid = sim.getIccid() - if iccid != -1 and len(iccid) > 5: - try: - print("iccid{}".format(iccid)) - except Exception: - pass - self.AI_TTS_str = "当前卡号为:{}".format(iccid) - else: - self.AI_TTS_str = "卡号查询失败" - return 0 - if it == "query_imei": - imei = modem.getDevImei() - if imei != -1 and len(imei) > 5: - try: - print("IMEI:{}".format(imei)) - except Exception: - pass - self.AI_TTS_str = "当前设备号为:{}".format(imei) - else: - self.AI_TTS_str = "设备号查询失败" - return 0 - if it == "query_version": - bt_versions = EventMesh.publish("get_bt_versions") - if bt_versions is None or bt_versions == "": - self.AI_TTS_str = "当前版本号:" + version_util.get_tts_version() - else: - self.AI_TTS_str = "当前版本号:" + version_util.get_tts_version(bt_versions) - return 0 - if it == "query_server": - self.AI_TTS_str = "服务器地址" + self.server_ip - return 0 - if it == "ota_check": - if self.ai_OTA.min_sys_ota() == 1: - self.AI_TTS_str = "有新版本,准备开始升级,升级过程会多次重启设备,请保证流量充足及电量充足" - else: - self.AI_TTS_str = "当前已是最新版本" - return 0 - if it == "factory_mode": - self.factory_mode() - return 1 - if it == "alarm_set": - try: - alarm = ujson.loads(cmd["command"]["params_json"]) - try: - print("params_json:{}".format(cmd["command"]["params_json"])) - except Exception: - pass - add_alarm_tmp = EventMesh.publish("AI_add_alarm", alarm["alarm"]) - if add_alarm_tmp[0] > 0: - self.AI_TTS_str = add_alarm_tmp[1] - return 1 - except Exception as e: - usys.print_exception(e) - try: - self.AI_TTS_str = cmd["command"]["fail_prompt"] - except Exception: - self.AI_TTS_str = "" - return -1 - if it == "alarm_clear": - try: - self.AI_TTS_str = EventMesh.publish("AI_delete_alarm") - except Exception as e: - usys.print_exception(e) - return 0 - if it == "alarm_list": - EventMesh.publish("read_alarm_list") - return 0 - if it == "voice_message_mode": - self.voice_flag = 1 - self.AI_TTS_str = "已进入语音留言模式,长按按钮开始录音,松开按钮结束" - self.Close_flag = 1 - self.stop_voice_timer.stop() - self.stop_voice_timer.start(30 * 1000, 0, self.exit_voice_message_timer) - try: - print("语音留言!!!!!!!!!!") - except Exception: - pass - return 0 - if it == "bt_ble_adv_on": - self.AI_TTS_str = "正在开蓝牙广播" - EventMesh.publish("AI_send_bt_ble_switch_toble", 1) - try: - print("正在开蓝牙广播") - except Exception: - pass - return 0 - if it == "bt_ble_adv_off": - self.AI_TTS_str = "正在关蓝牙广播" - EventMesh.publish("AI_send_bt_ble_switch_toble", 2) - try: - print("正在关蓝牙广播") - except Exception: - pass - return 0 - if it == "bt_classic_on": - self.AI_TTS_str = "正在开经典蓝牙" - EventMesh.publish("AI_send_bt_ble_switch_toble", 3) - try: - print("正在开经典蓝牙") - except Exception: - pass - return 0 - if it == "bt_classic_off": - self.AI_TTS_str = "正在关经典蓝牙" - EventMesh.publish("AI_send_bt_ble_switch_toble", 4) - try: - print("正在关经典蓝牙") - except Exception: - pass - return 0 - if it == "unbind_confirm": - self.AI_TTS_str = "设备解绑中...." - self.Close_flag = 1 - EventMesh.publish("send_unbind_dev_to_iot") - try: - print("解绑设备!!!!!!!!!!!!!!") - except Exception: - pass - return 0 - if it == "clear_earphone": - self.AI_TTS_str = "正在删除" - EventMesh.publish("clear_earphone_record") - try: - print("正在删除") - except Exception: - pass - return 0 - return None - - def check_text_llm(self,cmd): - try: - desc = cmd["command"]["chinese_description"] - if desc.find("切换音色") != -1: - text = cmd["command"]["params_json"] - try: - for key in self.VC_TTS_voice_type: - if text.find(key) != -1: - self.VC_TTS_voice_key = key - EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) - self.AI_TTS_str = "了解,声音已切换" - return 0 - except Exception as e: - usys.print_exception(e) - if text.find("男") != -1: - if "男" in self.VC_TTS_voice_type: - self.VC_TTS_voice_key = "男" - EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) - self.AI_TTS_str = "了解,声音已切换" - return 0 - if text.find("女") != -1: - if "女" in self.VC_TTS_voice_type: - self.VC_TTS_voice_key = "女" - EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) - self.AI_TTS_str = "了解,声音已切换" - return 0 - if text.find("同声") != -1 or text.find("ç«¥") != -1: - if "ç«¥" in self.VC_TTS_voice_type: - self.VC_TTS_voice_key = "ç«¥" - EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) - self.AI_TTS_str = "了解,声音已切换" - return 0 - if text.find("粤") != -1 or text.find("越野") != -1 or text.find("广东话") != -1: - if "粤" in self.VC_TTS_voice_type: - self.VC_TTS_voice_key = "粤" - EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) - self.AI_TTS_str = "了解,声音已切换" - return 0 - if text.find("四川") != -1: - if "四川" in self.VC_TTS_voice_type: - self.VC_TTS_voice_key = "四川" - EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) - self.AI_TTS_str = "了解,声音已切换" - return 0 - if text.find("个性") != -1: - if "个性" in self.VC_TTS_voice_type: - self.VC_TTS_voice_key = "个性" - EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) - self.AI_TTS_str = "了解,声音已切换" - return 0 - if text.find("英语") != -1: - if "英语" in self.VC_TTS_voice_type: - self.VC_TTS_voice_key = "英语" - EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) - self.AI_TTS_str = "了解,声音已切换" - return 0 - self.AI_TTS_str = "很抱歉,没找到对应声音" - return 0 - ret = self._apply_vc_text_intent(desc, cmd, True) - if ret is not None: - return ret - except Exception as e: - usys.print_exception(e) - try: - self.AI_TTS_str = cmd["command"]["fail_prompt"] - except Exception: - self.AI_TTS_str = "" - return -1 - - def check_text(self, text): - print("check_text:len:{}".format(len(text))) - if len(text) > 60: - return -1 - try: - if not isinstance(text, str): - return -1 - ret = self._apply_vc_text_intent(text, None, False) - if ret is not None: - return ret - except Exception as e: - usys.print_exception(e) - return -1 - - #获取trace_id - def get_trace_id(self): - t = utime.mktime(utime.localtime()) - t = t*1000+urandom.randint(0, 1000) - md5_data = "{}".format(t)#md5签名 - sign_data = "" - hash_obj = uhashlib.md5(md5_data) - res = hash_obj.digest() - - # 转成十六进制表示 - sign_data = ubinascii.hexlify(res) - sign_data = sign_data.decode('ascii') - return sign_data - - def get_session_id(self,data = "session_id"): - t = utime.mktime(utime.localtime()) - t = t*1000+urandom.randint(0, 1000) - md5_data = "{}{}".format(data,t)#md5签名 - sign_data = "" - hash_obj = uhashlib.md5(md5_data) - res = hash_obj.digest() - - # 转成十六进制表示 - sign_data = ubinascii.hexlify(res) - sign_data = sign_data.decode('ascii') - return sign_data - - def get_new_message_id(self): - message_id = self.get_trace_id()+"aaa"+self.get_trace_id() - self.VC_message_id=message_id[:32] - return self.VC_message_id - def get_new_task_id(self): - task_id = self.get_trace_id()+"aaa"+self.get_trace_id() - self.VC_task_id=task_id[:32] - return self.VC_task_id - #开始播放声音 - def VC_Play_Start(self): - try: - if self._ptt_should_skip_remote_playback(): - if self._vc_led_state_debug_enabled(): - print("[VC] VC_Play_Start skip (ptt_interrupt)") - return - except Exception: - pass - self.tts_in_data_len = 0 - self.tts_out_data_len = 0 - if self.VC_Play_flag == 0: - try: - _ptt_fast_open = False - try: - if getattr(self, "VC_mode", -1) == 1: - _ptt_fast_open = True - except Exception: - pass - # 本地提示音/文件播放可能占用同一音频通道,导致 TTS 流播放“写入成功但无声”; - # 在开启 TTS PCM 前先停掉本地播放,避免资源冲突。 - try: - self.aud.stopPlayStream() - self.aud.stopAll() - except Exception: - pass - # 资源互斥:播放前确保录音上行已停止(无 AEC 场景) - try: - if self.record_data_save_flag == 0 and self.VC_record_state_flag != 0: - self._uplink_request_stop("VC_Play_Start") - utime.sleep_ms(30) - except Exception: - pass - if self.VC_play_TYPE == VC_play_TYPE_PCM: - if self.play_pcm == None: - # 刚停采集时延后打开播音 PCM,且失败时重试,降低「pcm open write only fail」 - last_err = None - _open_retries = 1 if _ptt_fast_open else 3 - for i in range(_open_retries): - try: - if not _ptt_fast_open: - utime.sleep_ms(80 + i * 60) - self.play_pcm = audio.Audio.PCM(0, 1, self.VC_play_samplerate, 1, 1, 2) - ret = self.play_pcm.stream_write_start() - print("self.record_pcm.stream_write_start:ret:{}".format(ret)) - last_err = None - break - except Exception as e: - last_err = e - try: - if self.play_pcm != None: - self.play_pcm.close() - except Exception: - pass - self.play_pcm = None - if last_err: - raise last_err - elif self.VC_play_TYPE == VC_play_TYPE_MP3: - pass - self.VC_Play_flag = 1 - try: - if not self._ptt_should_skip_remote_playback(): - self._vc_publish_led("playing", "VC_Play_Start/tts") - except Exception: - pass - except Exception as e: - self.VC_Play_End() - usys.print_exception(e) - #暂停/继续声音播放 Pause_flag = 1暂停 - def VC_Play_Pause(self,Pause_flag): - try: - if self.VC_play_TYPE == VC_play_TYPE_PCM: - if self.play_pcm != None: - self.play_pcm.stream_pause(Pause_flag) - except Exception as e: - self.VC_Play_End() - usys.print_exception(e) - #停止播放声音 - def VC_Play_End(self): - self.VC_Play_flag = 0 - if self.VC_play_TYPE == VC_play_TYPE_PCM: - if self.VC_play_samplerate == 8000: - self.aud.stopPlayStream() - self.aud.stopAll() - elif self.VC_play_samplerate == 16000: - if self.play_pcm != None: - print("Close:play_pcm.close()!!!!!!!!!!!!!!!!!!") - try: - self.play_pcm.stream_stop() - self.play_pcm.close() - except Exception as e: - usys.print_exception(e) - self.play_pcm=None - - self.aud.stopPlayStream() - self.aud.stopAll() - elif self.VC_play_TYPE == VC_play_TYPE_MP3: - self.aud.stopPlayStream() - self.aud.stopAll() - if self.AI_audio_queue.empty()!=True: - self.AI_audio_queue.clear() - - # TTS 播完:按真实 FSM/录音态恢复灯;本地短音不走此路径 - try: - if getattr(self, "_vc_play_end_suppress_idle_led", 0): - if self._vc_led_state_debug_enabled(): - print("[LED] VC_Play_End skip led (suppress)") - elif getattr(self, "VC_record_state_flag", 0) != 0: - self._vc_publish_led("listening", "VC_Play_End/recording") - else: - self._vc_led_sync_from_fsm() - except Exception: - pass - # 关键日志:定位“tts 结束绿灯仍不灭”究竟是 base 态(如 charging/full)还是 playing 未退出 - try: - if "vc_config" in globals() and vc_config and ( - vc_config.get_bool("LED_DEBUG", False) or vc_config.get_bool("KEY_LED_DEBUG", False) or vc_config.get_bool("HW_STATUS_DEBUG", False) - ): - try: - led = EventMesh.publish("get_led_debug_state") - except Exception: - led = None - print("[VC] VC_Play_End led={}".format(led)) - except Exception: - pass - - - def _1S_timer_cb(self,*argc): - EventMesh.publish("publish_AI_btnInfo",{"func":self._1S_timer_cb_}) - - def _fsm_timeout_table_tick(self, now_ms): - # 兜底统一入口:按“状态机状态 + 超时阈值 + 节流”驱动,避免兜底抢跑/多入口打架。 - # 只在对应 turn/session 状态触发动作。 - try: - turn = getattr(self.fsm, "turn", VCTurnState.Idle) - except Exception: - turn = VCTurnState.Idle - - # 1) synthesis→tts.started 超时:只在 Thinking 期允许清 pending 并重启会话 - # 根因:服务端未返回 tts.started 时,继续“回到聆听/开麦”会把本轮状态机拉扯得更乱; - # 这里直接重启会话,让协议重新进入 started→下一轮流程。 - try: - if turn == VCTurnState.Thinking and self._tts_pending == 1 and self._tts_pending_ms: - try: - if self._fb_last_tts_pending_timeout_ms: - tts_gap_ok = utime.ticks_diff(now_ms, self._fb_last_tts_pending_timeout_ms) > 15000 - else: - tts_gap_ok = True - except Exception: - tts_gap_ok = True - if utime.ticks_diff(now_ms, self._tts_pending_ms) > 8000 and tts_gap_ok: - self._fb_last_tts_pending_timeout_ms = now_ms - print("[VC][tts_pending] timeout, clear pending and restart session") - self._tts_pending = 0 - self._tts_pending_ms = 0 - if self.VC_mode == 0 and self.Close_flag == 0: - try: - self.fsm.reset_rx_phases() - except Exception: - pass - EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_VC_start}) - except Exception: - pass - - # 2) thinking 超时:只在「llm.completed 已收到但 tts.started 长时间未到」时才重启会话 - # 注意:不能以 llm.created 的时间戳作为超时基准,否则会在 LLM 正常推理期间误判超时(notts 场景)。 - try: - if self.VC_mode == 0 and self.Close_flag == 0: - llm_completed_ms = getattr(self, "_llm_completed_ms", 0) - # 只有在 llm.completed 之后才允许超时重启 - if turn == VCTurnState.Thinking and llm_completed_ms and self._turn_has_tts_started == 0: - age_ms = utime.ticks_diff(now_ms, llm_completed_ms) - restart_threshold_ms = 20000 - # _fb_last_thinking_restart_ms==0 时 ticks_diff(now,0) 非预期(间隔被误判极大), - # 会在 llm.completed 后首批 tick 就触发 AI_VC_start → notts(无 tts.started);首次仅信任 age 门限 - try: - if self._fb_last_thinking_restart_ms: - gap_ok = utime.ticks_diff(now_ms, self._fb_last_thinking_restart_ms) > 15000 - else: - gap_ok = True - except Exception: - gap_ok = True - if (age_ms > restart_threshold_ms and gap_ok and - self._tts_pending == 0 and self._need_audio_played_ack == 0 and self.VC_Play_flag == 0): - self._fb_last_thinking_restart_ms = now_ms - self._llm_completed_ms = 0 - self._server_vad_silence = 0 - try: - self.fsm.reset_rx_phases() - except Exception: - pass - try: - print("[VC][thinking_timeout] AI_VC_start age_ms={} thr={}".format( - age_ms, restart_threshold_ms)) - except Exception: - pass - self._vc_led_alert_rb_flash() - # thinking_timeout 策略:重启会话(而不是抢开麦),避免干扰服务端 LLM/TTS 流程 - EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_VC_start}) - except Exception: - pass - - def _1S_timer_cb_(self): - try: - self._vc_tick_serial_cancel_timeout() - except Exception: - pass - - EventMesh.publish("system_sleep_rest")#防止黑屏 - # --- Level 1:仅长对话(VC_mode==0),无业务下行 idle_sec 后结束会话(需再次按键进入) - # close_timerout:保活清除策略见 _vc_rx_resets_long_idle_timer(排除文本 ping/pong、heartbeat 等) - # 与浅休眠/灭屏解耦;单对话不走本计时 - idle_sec = 20 - try: - if "vc_config" in globals() and vc_config: - idle_sec = int(vc_config.get_int("VC_IDLE_AUTO_CLOSE_SEC", 20)) - except Exception: - idle_sec = 20 - if idle_sec < 5: - idle_sec = 5 - if self.VC_mode == 0 and self.VC_satrt_flag == 1 and getattr(self, "AI_client_VC_isconnect", 0) == 1: - self.close_timerout += 1 - else: - self.close_timerout = 0 - if self.VC_mode == 0 and idle_sec > 0 and self.close_timerout > idle_sec: - try: - print("[VC][idle_L1] 长对话无业务下行超时: elapsed={}s thr={}s VC_mode={} VC_satrt={} ws_conn={} {}".format( - self.close_timerout, idle_sec, self.VC_mode, - getattr(self, "VC_satrt_flag", -1), getattr(self, "AI_client_VC_isconnect", -1), - self._vc_fsm_log_snip())) - except Exception: - pass - if self._vc_long_idle_active_speech_or_later(): - try: - print("[VC][idle_L1] 处理=websocket_error(对话中无服务端响应)") - except Exception: - pass - print("[VC][long_idle] branch=speech_no_rsp -> websocket_error + ud_error") - print("接口无响应,超时退出") - EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error,"data":"接口超时了。请重试"}) - self.ud_error(["VC",self.VC_taskid]) - else: - try: - print("[VC][idle_L1] 处理=Close(结束对话,需再次按键进入)") - except Exception: - pass - print("[VC][long_idle] branch=idle_timeout -> Close") - EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) - #EventMesh.publish("sleep_BT")#休眠蓝牙 - - # --- Level 2:长对话+单对话,业务 idle 累计 VC_IDLE_DEEP_SLEEP_SEC → 断开(若仍连)+ 深睡事件 - deep_sec = 60 - deep_en = 1 - try: - if "vc_config" in globals() and vc_config: - deep_sec = int(vc_config.get_int("VC_IDLE_DEEP_SLEEP_SEC", 60)) - deep_en = int(vc_config.get_int("VC_IDLE_DEEP_SLEEP_ENABLE", 1)) - except Exception: - deep_sec, deep_en = 60, 1 - active_deep = bool(deep_en) and deep_sec > 0 - if active_deep and deep_sec < 10: - deep_sec = 10 - if not active_deep: - self._idle_deep_sec = 0 - elif not self._vc_is_business_idle(): - self._idle_deep_sec = 0 - try: - self._vc_idle_deep_armed_suppress = 0 - except Exception: - pass - elif getattr(self, "_vc_idle_deep_armed_suppress", 0): - pass - else: - self._idle_deep_sec += 1 - if active_deep and self._idle_deep_sec > deep_sec: - try: - print("[VC][idle_L2] 业务idle累计超时: elapsed={}s thr={}s VC_mode={} VC_satrt={} ws_conn={}".format( - self._idle_deep_sec, deep_sec, self.VC_mode, - getattr(self, "VC_satrt_flag", -1), getattr(self, "AI_client_VC_isconnect", -1))) - except Exception: - pass - try: - self._vc_idle_deep_armed_suppress = 1 - except Exception: - pass - self._idle_deep_sec = 0 - EventMesh.publish("publish_AI_btnInfo", {"func": self._vc_idle_deep_sleep_impl}) - - # 统一兜底表(按状态机状态触发) - try: - self._fsm_timeout_table_tick(utime.ticks_ms()) - except Exception: - pass - - # (不做额外兜底)动态切换依赖 FSM 状态正确清理后,由 server.tts.completed→local.play_end 驱动自动开麦 - - # print("get_audio_state !!!!!{} {} {}\r\n".format(self.play_end,EventMesh.publish("get_audio_state"),self.play_flag)) - # ===== 播放结束检测:用 FSM 驱动 play_end 动作链 ===== - try: - if getattr(self.fsm, "await_played", False): - # PTT(VC_mode==1) 也必须走本段:旧逻辑整段包在 VC_mode!=1 内会导致 tts.completed 后仍 await_played, - # 无人触发 VC_Play_End,绿灯/playing 卡死至按键。 - # 不能只用 stream_is_empty:队列仍可能有待播数据时提前关播会“没声”;in/out 字节在分帧下可能未完全对齐, - # 在已 tts.completed 且队列已空时,以服务端结束下发 + PCM 空为准。 - is_pcm_empty = (self.play_pcm == None) or (self.play_pcm != None and self.play_pcm.stream_is_empty() == 1) - try: - q_empty = self.AI_audio_queue.empty() - except Exception: - q_empty = True - try: - in_len = getattr(self, "tts_in_data_len", 0) - out_len = getattr(self, "tts_out_data_len", 0) - except Exception: - in_len, out_len = 0, 0 - try: - tts_done_ms = getattr(self.fsm, "last_tts_completed_ms", 0) - except Exception: - tts_done_ms = 0 - drained_strict = (in_len > 0 and out_len >= in_len and q_empty) - drained_after_server = (bool(tts_done_ms) and q_empty) - drained = drained_strict or drained_after_server - # 8s 超时:兜底认为播放结束(避免播放状态卡死) - too_long = False - try: - t0 = getattr(self.fsm, "last_tts_completed_ms", 0) - if t0: - too_long = utime.ticks_diff(utime.ticks_ms(), t0) > 8000 - except Exception: - pass - if (is_pcm_empty and drained) or too_long: - EventMesh.publish("publish_AI_btnInfo", {"func": self.VC_Play_End}) - self._fsm_apply_actions(self.fsm.on_local_play_end()) - try: - self.fsm.reset_rx_phases() - except Exception: - pass - except Exception: - pass - if self.online_kws == 1: - if self.VC_satrt_kws_flag == 1 and self.VC_satrt_flag == 0:#要打开录音上传数据实现kws - pass - - # thinking/llm→tts 等兜底已在 _fsm_timeout_table_tick 内统一处理 - # VC WebSocket 文本 ping:间隔由 default_config 的 VC_WS_PING_INTERVAL_SEC 控制(秒),<=0 表示关闭 ping - try: - _ping_iv = int(vc_config.get_int("VC_WS_PING_INTERVAL_SEC", 4)) - except Exception: - _ping_iv = 4 - if _ping_iv > 0: - self.VC_pingping_timerout += 1 - if self.VC_pingping_timerout >= _ping_iv: - self.VC_pingping_timerout = 0 - self.AI_client_VC_ping() - # self.asr_pingping_timerout += 1 - # if self.asr_pingping_timerout>3: - # self.asr_pingping_timerout=0 - # self.AI_client_asr_ping() - # 历史遗留:VC_Thinking_timerout 的重启通道会在 llm.created 后抢跑触发 AI_VC_start, - # 直接打断本轮 LLM→TTS(notts 日志:llm.created 后立刻 AI_VC_start,导致无 tts.started)。 - # 现在统一由 _fsm_timeout_table_tick(Thinking 超时) 负责重启会话,因此禁用该旧通道。 - if self.VC_Thinking_timerout > 0 and self.VC_Thinking_timerout < 100: - self.VC_Thinking_timerout = 100 - # def AI_kws_start(self): - # print("AI_kws_start") - # EventMesh.publish("wakeup_BT") - # EventMesh.publish("system_sleep_rest")#防止黑屏 - # url="ws://118.145.158.207:22080/wakeup/ws" - # self.AI_client_kws = uwebsocket.Client.connect(url, debug=True) - # self.AI_client_VC_recv_id = _thread.start_new_thread(self.AI_client_VC_recv, (self.AI_client_VC,)) - # self.VC_Speech_Start()#开启线程获取录音数据 - #VC开始 - def AI_VC_start(self): - print("AI_VC_start1") - - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - self.VC_satrt_flag = 1 - self.VC_stop_play_audio_flag = 1 - self.VC_satrt_kws_flag = 1 - if self.VC_Thinking_timerout < 100: - self.VC_Thinking_timerout = 0 - # 不在这里提前切灯态:播放/聆听灯只在“播放/录音真正开始/停止”处更新 - EventMesh.publish("wakeup_BT") - #self.VC_Play_End()#关闭音频播放 - print("AI_VC_start2") - #self.aud.aud_tone_play(16,200)#播放按键音 - self.LocalRespondingEnded_timerout = 0 - self.close_timerout = 0 - self._idle_deep_sec = 0 - self._vc_idle_deep_armed_suppress = 0 - - # - if self.AI_client_VC_isconnect == 0:#没连接的时候请求开始 - self.websocket_connect_VC()#连接服务器 - - #utime.sleep_ms(50) - else: - print("AI_VC_start2 (already connected) {}".format(self._vc_fsm_log_snip())) - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - if self.AI_client_VC_isconnect == 0: - #self.Close() - return - # 单对话 PTT:会话已 started 时勿重复发 Start(否则第二轮起上行/ASR 易乱) - _ptt_skip_start = False - try: - if self.VC_mode == 1 and getattr(self, "_vc_session_started", 0) == 1: - _ptt_skip_start = True - print("[PTT] AI_VC_start skip duplicate Start (session active)") - except Exception: - pass - if not _ptt_skip_start: - self.AI_client_VC_start_requset() - print("AI_VC_start3") - - if (not _ptt_skip_start) and self.AI_TTS_str == "" and self.AI_new_voice_url == "": - self._uplink_request_start("AI_VC_start") - - - # pass - print("AI_VC_start4") - - #self.VC_Play_Start() - #EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Play_Start}) - #开始线程 - - # if self.AI_VC_send_data_queue_thread_id == 0: - # _thread.stack_size(self.Audio_buf_size+8*1024) - # self.AI_VC_send_data_queue_thread_id = _thread.start_new_thread(self.AI_VC_send_data_queue_thread, ()) - # _thread.stack_size(8*1024) - # print("AI_VC_start :AI_VC_send_data_queue_thread_id :{} ".format(self.AI_VC_send_data_queue_thread_id)) - - if self.AI_audio_queue_thread_id ==0: - _thread.stack_size(24*1024) - self.AI_audio_queue_thread_id = _thread.start_new_thread(self.AI_audio_queue_thread, ()) - _thread.stack_size(8*1024) - print("AI_VC_start :AI_audio_queue_thread_id :{} ".format(self.AI_audio_queue_thread_id)) - else: - print("AI_VC_start :AI_audio_queue_thread_id != 0 ") - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - self._1S_timer.start(1000,1,self._1S_timer_cb) - # self.VC_pingping_timerout = 0 - # self.asr_pingping_timerout = 0 - print("AI_VC_start5") - #开始会话 - def AI_client_VC_start_requset(self): - voice="zh_male_wennuanahu_moon_bigtts" - try: - voice = self.VC_TTS_voice_type[self.VC_TTS_voice_key]["voice"] - except Exception as e: - usys.print_exception(e) - #voice="zh_female_dongmanxiaoyang_moon_bigtts" - token = EventMesh.publish("get_dev_token") - if token ==None: - token = "" - # bot_option:优先从 default_config(usr 可覆盖)读取 VC_BOT_ID / VC_WORKFLOW_ID,允许空字符串 - _def_bot_id = "7462617881076351002" - _def_workflow_id = "7462621757327196187" - try: - if "vc_config" in globals() and vc_config: - _vc_bot_id = vc_config.get_attr("VC_BOT_ID", _def_bot_id) - _vc_workflow_id = vc_config.get_attr("VC_WORKFLOW_ID", _def_workflow_id) - if _vc_bot_id is None: - _vc_bot_id = "" - if _vc_workflow_id is None: - _vc_workflow_id = "" - else: - _vc_bot_id, _vc_workflow_id = _def_bot_id, _def_workflow_id - except Exception: - _vc_bot_id, _vc_workflow_id = _def_bot_id, _def_workflow_id - json_data = \ - { - "header": { - "event": "Start", - "trace_id": self.get_trace_id(), - }, - "payload": { - "simple_response": True,#不返回llm过程文本 - "device_id": modem.getDevImei(), - - "user_agent": "AudioClient/1.0", - "dialog_attributes": { - #"provider": "doubao", - # "provider": "coze", - - "cfg": { - "disable_empty_prompt" : True,#拦截空字符请求大模型 - "custom_variables": { - "name": self.bot_name, - "devToken":token, - }, - "greeting":{ - "enabled":False, - "content":"" - }, - "openai_option":{ - }, - - "bot_option": { - "use_workflow": True, - #"input_format": "text", - "bot_id": _vc_bot_id, - "workflow_id": _vc_workflow_id, - - } - } - }, - "outbound_attributes": { - "provider": "volcengine", - "cfg": { - "format": "pcm", - "sample_rate": self.VC_play_samplerate, - "bit_rate": 16, - "channel": 1, - "volume": self.VC_TTS_volume, - "emotion": self.emotion, - #"voice": "7426720361732980745", - "voice": voice,#音色 - "speech_rate": self.speech_rate,#语速 - "chunk_interval_ms": 10, # 配置每段音频入队间隔时间 100ms - } - }, - "inbound_attributes": { - # "provider": "xun-fei",# - "provider": "volcengine", - "disable_vad":True, - "vad_silence":self.vad_silence, - "interrupt_options":{ - "concatenate_results":True, - "thinking_time_only": True, - #"keyword_interrupt": false, // 启用打断词功能 (打断词只有在思考以及之后才能打断) - #"interrupt_keywords": ["停止", "打断","打断一下", "等一下", "别说了", "打住"] // 配置打断词列表 - }, - "cfg": { - "format": "pcm", - "sample_rate": self.VC_record_samplerate, - "bit_rate": 16, - "channel": 1, - "enable_punctuation": True, - "enable_itn":True, - "keywords":[] - } - }, - "commands":self.commands, - } - } - # "bot_id":"7481164663863459850",#鸭子 - # "workflow_id":"7480485093845991443", - # "bot_id":"7462617881076351002",#语言助手 - # "workflow_id":"7462621757327196187", - # "bot_id":"7469964258592931852",#深圳百事通 - # "workflow_id":"7469968083063603241", - # "bot_id":"7411071327182438412",#柒牌 - # "workflow_id":"7469049680291610636", - # if self.online_kws == 1:#打开 - # json_data["payload"]["inbound_attributes"]["interrupt_options"]["keyword_interrupt"] = True - # json_data["payload"]["inbound_attributes"]["interrupt_options"]["interrupt_keywords"] = ["你好,柒先生","你好"] - #如果 - json_data["payload"]["outbound_attributes"]["provider"]=self.outbound_attributess_provider#TTS音色服务选择,火山 - json_data["payload"]["inbound_attributes"]["provider"]=self.inbound_attributess_provider#ASR服务选择,火山 - #优先选择self.VC_TTS_voice_type[self.VC_TTS_voice_key]里得参数 - # if "inbound_attributess_provider" in self.VC_TTS_voice_type[self.VC_TTS_voice_key]: - # if len(self.VC_TTS_voice_type[self.VC_TTS_voice_key]["inbound_attributess_provider"])>0: - # json_data["payload"]["inbound_attributes"]["provider"]=self.VC_TTS_voice_type[self.VC_TTS_voice_key]["inbound_attributess_provider"] - # print("inbound_attributess_provider是VC_TTS_voice_type里的:{}".format(self.VC_TTS_voice_type[self.VC_TTS_voice_key]["inbound_attributess_provider"])) - if "outbound_attributess_provider" in self.VC_TTS_voice_type[self.VC_TTS_voice_key]: - if len(self.VC_TTS_voice_type[self.VC_TTS_voice_key]["outbound_attributess_provider"])>0: - json_data["payload"]["outbound_attributes"]["provider"]=self.VC_TTS_voice_type[self.VC_TTS_voice_key]["outbound_attributess_provider"] - print("outbound_attributess_provider是VC_TTS_voice_type里的:{}".format(self.VC_TTS_voice_type[self.VC_TTS_voice_key]["outbound_attributess_provider"])) - - # 根据配置设置默认 bot/workflow,并允许 VC_TTS_voice_type 覆盖 - try: - if "vc_config" in globals() and vc_config: - opt = vc_config.get_attr("DIALOG_BOT_OPTION", None) - if isinstance(opt, dict): - if "bot_id" in opt: - json_data["payload"]["dialog_attributes"]["cfg"]["bot_option"]["bot_id"] = opt.get("bot_id") or "" - if "workflow_id" in opt: - json_data["payload"]["dialog_attributes"]["cfg"]["bot_option"]["workflow_id"] = opt.get("workflow_id") or "" - if "use_workflow" in opt: - json_data["payload"]["dialog_attributes"]["cfg"]["bot_option"]["use_workflow"] = bool(opt.get("use_workflow")) - except Exception: - pass - - try: - # voice 配置可覆盖 bot_id/workflow_id/openai_option - cur = self.VC_TTS_voice_type[self.VC_TTS_voice_key] - if isinstance(cur, dict): - if cur.get("bot_id"): - json_data["payload"]["dialog_attributes"]["cfg"]["bot_option"]["bot_id"] = cur.get("bot_id") - if "workflow_id" in cur: - json_data["payload"]["dialog_attributes"]["cfg"]["bot_option"]["workflow_id"] = cur.get("workflow_id") or "" - if cur.get("openai_option"): - json_data["payload"]["dialog_attributes"]["cfg"]["openai_option"] = cur.get("openai_option") - except Exception: - pass - - try: - _bo = json_data["payload"]["dialog_attributes"]["cfg"]["bot_option"] - if not _bo.get("bot_id", "") or not _bo.get("workflow_id", ""): - _bo["use_workflow"] = False - except Exception: - pass - try: - if "vc_config" in globals() and vc_config: - p = vc_config.get_attr("DIALOG_PROVIDER_DEFAULT", None) - if p: - json_data["payload"]["dialog_attributes"]["provider"] = p - except Exception: - pass - - try: - if "vc_config" in globals() and vc_config and vc_config.get_bool("KEYWORDS_FROM_VOICE_TYPE", False): - keywords = [] - for key in self.VC_TTS_voice_type: - keywords.append(key) - json_data["payload"]["inbound_attributes"]["cfg"]["keywords"] = keywords - except Exception: - pass - if self.dialog_attributes_provider != None: - json_data["payload"]["dialog_attributes"]["provider"] = self.dialog_attributes_provider - if self.commands_mode !=0 : - json_data["payload"]["commands"] = [] - if self.VC_mode == 0 :#and dev_type != "7PIN_BK_coze":#长对话模式打开vad - json_data["payload"]["inbound_attributes"]["disable_vad"] = False - # 新会话开始:在收到服务端 started 前,禁止发送音频帧(避免 session not initialized) - self._vc_session_started = 0 - try: - self.fsm.on_session_start_request() - except Exception: - pass - self.AI_client_VC_send(ujson.dumps(json_data)) - try: - _tid = json_data.get("header", {}).get("trace_id", "") - print("[VC][proto] Start enqueued trace_id={} VC_mode={} disable_vad={} ws_conn={}".format( - _tid or "-", - self.VC_mode, - json_data.get("payload", {}).get("inbound_attributes", {}).get("disable_vad", "?"), - self.AI_client_VC_isconnect)) - except Exception: - pass - - #开始上传语音 - def AI_client_VC_send_speech(self): - json_data = \ - { - "header": { - "event": "audio.create", - - }, - } - - if self.VC_mode == 1:#单对话模式 - # 协议门控(可用 VC_PROTO_GUARDS 临时关闭) - if (not VC_PROTO_GUARDS) or (self._vc_session_started == 1): - self._ptt_audio_created = 1 - try: - print("[VC][proto] audio.create send session_started={} proto_guards={}".format( - self._vc_session_started, VC_PROTO_GUARDS)) - except Exception: - pass - self.AI_client_VC_send(ujson.dumps(json_data)) - else: - try: - print("[VC][proto] audio.create SKIP session_started={} VC_PROTO_GUARDS={} " - "(等对端 started 后再开麦)".format(self._vc_session_started, VC_PROTO_GUARDS)) - except Exception: - pass - - - - - - - try: - self.fsm.on_server_asr_started() - except Exception: - pass - #结束上传语音 - def AI_client_VC_end_speech(self): - json_data = \ - { - "header": { - "event": "audio.completed", - - }, - } - self.AI_client_VC_send(ujson.dumps(json_data)) - self._ptt_audio_created = 0 - EventMesh.publish("publish_AI_btnInfo",{"func":self._uplink_request_stop, "data":"audio.completed"}) - - - #客户端打断对话 - def AI_client_VC_RequestToSpeak(self): - json_data = \ - { - "event": "cancel", - "payload": {} - } - print("AI_client_VC_RequestToSpeak!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") - # 模式切换等场景 play_tts(..., is_close_VC=1) 会置 Close_flag=1;用户再按 AI 键走 cancel→canceled→ - # uplink_start 时,FSM 与 _uplink_request_start 均会因 Close_flag 拒绝开麦,表现为“切回长对话后按键无上行”。 - try: - if self.VC_mode == 0: - self.Close_flag = 0 - self._vc_pending_interrupt_uplink = 1 - self._vc_clear_stale_record_state("RequestToSpeak") - except Exception: - pass - # 只做“打断+清播放队列”,不要在这里关音频队列线程(会导致后续 TTS/提示音不稳定,且触发竞态) - EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_init}) - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_send,"data":ujson.dumps(json_data)}) - utime.sleep_ms(1) - try: - self.fsm.reset_rx_phases() - except Exception: - pass - # #self.Close() - # self.AI_VC_start() - #TTS播放 - def AI_client_VC_synthesis(self): - # 幂等保护:在上一条 synthesis 还未等到 tts.started 前,不重复发(防止多兜底/多入口重复触发) - try: - if self._tts_pending == 1: - return - except Exception: - pass - json_data = \ - { - "event": "synthesis", - "payload": { - "text": self.AI_TTS_str - } - } - # 语义标记只用于“是否自动开麦”的策略。 - # 当前策略:长对话不自动开麦(需按键开始),因此这里不对提示音做自动开麦标记。 - self.AI_TTS_str = "" - # 标记为“等待 tts.started”,避免在 synthesis→tts.started 窗口误开下一轮录音上行 - self._tts_pending = 1 - try: - self._tts_pending_ms = utime.ticks_ms() - except Exception: - self._tts_pending_ms = 0 - try: - self.fsm.on_local_synthesis_sent() - except Exception: - pass - print("AI_client_VC_synthesis!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") - # if self.VC_state == VC_State_RequestToRespond: - # print("AI_client_VC_RequestToRespond 重复 tts_str:{}\nasr_str:{}".format(self.AI_TTS_str,self.AI_ASR_str)) - self.AI_client_VC_send(ujson.dumps(json_data)) - # 金三 V2.5:AudioPlayed — 端侧 TTS 播报结束后上报,服务端进入下一轮对话(轻度重同步,不断开 WebSocket) - def AI_client_VC_audio_played(self): - # V2.5:播完上报;与 VC_PROTO_GUARDS 无关(见文件头说明) - try: - if self.AI_client_VC_isconnect != 1 or self.AI_client_VC == None: - return - json_data = {"event": "audio.played", "payload": {}} - print("AI_client_VC_audio_played") - self.AI_client_VC_send(ujson.dumps(json_data)) - except Exception as e: - usys.print_exception(e) - def kws_even(self,event = None,msg= None): - EventMesh.publish("publish_AI_btnInfo",{"func":self.kws_even_cb}) - def kws_even_cb(self): - if EventMesh.publish("get_sos_flag") == 1: - return - print("kws_even_cb") - if self.VC_mode == 1: - try: - tts_str = vc_config.get_attr("KWS_PTT_HINT", "请您按住圆形的按钮开始讲话,讲完松开即可 ") - except Exception: - tts_str = "请您按住圆形的按钮开始讲话,讲完松开即可 " - EventMesh.publish("play_tts",[tts_str,1]) - return - if self.VC_mode == 0: - if EventMesh.publish("get_voice_record_flag") == 1:#停止静默录音 - if EventMesh.publish("get_voice_record_mode") == 2:#语音留言不能被打断 - return - elif EventMesh.publish("get_voice_record_mode") == 1: #静默录音需要打断 - EventMesh.publish("stop_voice_record")#停止静默录音 - self.aud.aud_tone_play(10,400) - # EventMesh.publish("audio_montage_play",["chenggong.mp3"]) - self.Close() - # if dev_type == "7PIN" : - # self.aud.aud_tone_play(10,400)#播放按键音 - #self.aud.aud_tone_play(10,200)#播放按键音 - self.AI_TTS_str = ""#"你好,有什么吩咐" - self.AI_ASR_str = "" - self.AI_ASR_str_tmp = "" - self._uplink_request_stop("kws_even_cb") - self._uplink_request_start("kws_even_cb") - self.AI_VC_start() - - def set_vad_silence(self,event = None,msg= None): - self.vad_silence = msg - EventMesh.publish("set_storage_config",["vad_silence",self.vad_silence]) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.play_new_voice_cb,"data":msg}) - #设置tts参数 - def set_wait_first_flag(self,event = None,msg= None):#设置未读语音播报初次记录flag - self.wait_first_flag = msg - def set_VC_TTS_voice_type(self,event = None,msg= None): - self.VC_TTS_voice_type["APP_voice"] = msg - #self.VC_TTS_voice_type[self.VC_TTS_voice_key] - self.VC_TTS_voice_key = "APP_voice" - EventMesh.publish("set_storage_config",["VC_TTS_voice_type",self.VC_TTS_voice_type]) - EventMesh.publish("set_storage_config",["VC_TTS_voice_key",self.VC_TTS_voice_key]) - print("[VC] TTS_voice key={} map={}".format(self.VC_TTS_voice_key, self.VC_TTS_voice_type)) - #修改音量大小 msg为1则为加大,msg为0则为减小,返回1成功,返回0失败 - def change_volume(self,event =None, msg =None): - if msg == 0 : - self.VC_TTS_volume -= 10 - if self.VC_TTS_volume < 0: - self.VC_TTS_volume = 0 - return 0 - else: - self.aud.setVolume(self.VC_TTS_volume //10) - EventMesh.publish("set_storage_config",["VC_TTS_volume",self.VC_TTS_volume]) - EventMesh.publish("post_device_state") - EventMesh.publish("adjust_volume_toBle",self.VC_TTS_volume) - return 1 - elif msg == 1: - self.VC_TTS_volume += 10 - if self.VC_TTS_volume > 100: - self.VC_TTS_volume = 100 - return 0 - else: - self.aud.setVolume(self.VC_TTS_volume //10) - EventMesh.publish("set_storage_config",["VC_TTS_volume",self.VC_TTS_volume]) - EventMesh.publish("post_device_state") - EventMesh.publish("adjust_volume_toBle",self.VC_TTS_volume) - return 1 - elif msg >= 100:#修改为具体音量 - if msg > 200: - self.VC_TTS_volume = 100 - return 0 - else: - self.VC_TTS_volume = msg - 100#还原数据 - self.aud.setVolume(self.VC_TTS_volume //10) - EventMesh.publish("set_storage_config",["VC_TTS_volume",self.VC_TTS_volume]) - EventMesh.publish("post_device_state") - EventMesh.publish("adjust_volume_toBle",self.VC_TTS_volume) - return 1 - #调节聆听快慢(以秒为单位) - def NLP_set_vad_silence(self,event,msg): - if msg == 1:#聆听变快 - self.vad_silence -= 1000 - if self.vad_silence < 1000: - AI_TTS_str = "已经是最快了" - self.vad_silence = 1000 - else: - AI_TTS_str = "配置成功,当前语音断句速度是{}秒".format(self.vad_silence/1000) - elif msg == 0:#聆听变慢 - self.vad_silence += 1000 - if self.vad_silence > 4000: - self.vad_silence = 4000 - #EventMesh.publish("audio_montage_play",["yinliangyizuixiao.mp3"]) - #self.aud.play(1,0,'U:/static/yinliangyizuixiao.mp3')#音量已最小 - AI_TTS_str = "已经是最慢了" - else: - AI_TTS_str = "配置成功,当前语音断句速度是{}秒".format(self.vad_silence/1000) - EventMesh.publish("set_storage_config",["vad_silence",self.vad_silence]) - return AI_TTS_str - #查询指令判断 - def find_cmd(self,event ,msg): - if "电量" in msg: - battery = EventMesh.publish("get_battery") - print("电量查询:{}".format(battery)) - tts_play_str = "电量剩余百分之{}".format(battery) - EventMesh.publish("play_tts",[tts_play_str,0]) - # print("电量!!!!!!!!!!!") - elif "信号" in msg: - signal = net.csqQueryPoll() - tts_play_str = "当前信号{}".format(signal) - EventMesh.publish("play_tts",[tts_play_str,0]) - # print("信号!!!!!!!!!!!") - elif "卡号" in msg: - # print("卡号!!!!!!!!!!!") - iccid = sim.getIccid() - iccid = num_str_to_chinese(iccid)#转为中文 - if iccid != -1 and len(iccid) > 5: - print("iccid{}".format(iccid)) - tts_play_str = "当前卡号为:{}".format(iccid) - else: - tts_play_str = "卡号查询失败" - EventMesh.publish("play_tts",[tts_play_str,0]) - elif "设备号" in msg: - imei = modem.getDevImei() - if imei != -1 and len(imei) > 5: - print("IMEI:{}".format(imei)) - tts_play_str = "当前设备号为:{}".format(imei) - else: - tts_play_str = "设备号查询失败" - EventMesh.publish("play_tts",[tts_play_str,0]) - elif "音量" in msg : - tts_play_str = "当前音量是{}".format(self.VC_TTS_volume) - EventMesh.publish("play_tts",[tts_play_str,0]) - elif "语速" in msg : - tts_play_str = "当前语速是{}".format(EventMesh.publish("find_rate")) - EventMesh.publish("play_tts",[tts_play_str,0]) - - #改变音色判断 - def change_voice(self,event ,msg): - voice = msg - # if "男声" in msg or "普通话" in msg: - # self.VC_TTS_voice_key = "男" - # else: - # self.VC_TTS_voice_key = voice - self.outbound_attributess_provider = "xun-fei" - if "男声" in msg or "普通话" in msg: - self.VC_TTS_voice_key = "男" - self.outbound_attributess_provider = "volcengine" - elif "女声" in msg: - self.VC_TTS_voice_key = "女" - self.outbound_attributess_provider = "volcengine" - elif "童声" in msg: - self.VC_TTS_voice_key = "ç«¥" - self.outbound_attributess_provider = "volcengine" - elif "香港粤语" in msg: - self.VC_TTS_voice_key = "香港粤语" - elif "四川话" in msg: - self.VC_TTS_voice_key = "四川" - elif "上海话" in msg: - self.VC_TTS_voice_key = "上海" - elif "安徽合肥话" in msg: - self.VC_TTS_voice_key = "安徽合肥" - elif "山东话" in msg: - self.VC_TTS_voice_key = "山东话" - elif "陕西话" in msg: - self.VC_TTS_voice_key = "陕西" - elif "广东话" in msg: - self.VC_TTS_voice_key = "广东" - elif "成都话" in msg: - self.VC_TTS_voice_key = "成都" - elif "湖南话" in msg: - self.VC_TTS_voice_key = "湖南" - elif "苏州话" in msg: - self.VC_TTS_voice_key = "苏州" - elif "东北话" in msg: - self.VC_TTS_voice_key = "东北" - elif "河南话" in msg: - self.VC_TTS_voice_key = "河南" - elif "湖北话" in msg: - self.VC_TTS_voice_key = "湖北" - elif "闽南话" in msg: - self.VC_TTS_voice_key = "闽南" - elif "内蒙古话" in msg: - self.VC_TTS_voice_key = "内蒙古话" - EventMesh.publish("set_storage_config",["VC_TTS_voice_key",self.VC_TTS_voice_key]) - tts_play_str = "声音已改为{}".format(voice) - EventMesh.publish("play_tts",[tts_play_str,0]) - - #闹钟的url播放接口 - def play_single_voice(self,event = None,msg= None): - EventMesh.publish("publish_AI_btnInfo",{"func":self.play_single_voice_cb,"data":msg}) - def play_single_voice_cb(self,data): - try: - new_voice_url = data[0] #音频地址 - is_close_VC = data[1] - if new_voice_url != "": - self.AI_new_voice_url = new_voice_url - self.alarm_play_flag = 1 - if is_close_VC != None: - self.Close_flag = is_close_VC - if self.AI_VC_send_data_queue.empty()!=True: - self.AI_VC_send_data_queue.clear() - if self.AI_ASR_send_data_queue.empty()!=True: - self.AI_ASR_send_data_queue.clear() - EventMesh.publish("set_led_state", {"ai_state": "thinking"}) - self.AI_ASR_send_buf_len = 0 - self._uplink_request_stop("play_single_voice_cb") - self.websocket_deconnect_VC() - self.AI_VC_start() - self.play_new_voice_flag = 1 - # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - except Exception as e: - usys.print_exception(e) - pass - #语音留言 - def play_new_voice(self,event = None,msg= None): - EventMesh.publish("publish_AI_btnInfo",{"func":self.play_new_voice_cb,"data":msg}) - def play_new_voice_cb(self,data): - try: - new_voice_url = data[0] #音频地址 - is_close_VC = data[1] - if new_voice_url != "": - self.recv_voice = 1#收到留言 - print("已收到最新留言!!!!!") - # EventMesh.publish("set_motor_time",[1,1,5,8,4])#设置马达参数 - self.AI_new_voice_url = new_voice_url - # if self.pwk_cnt_voice == 0 and self.pwk_notice_flag == 0:#并且不在多条留言的状态 - # EventMesh.publish("play_tts",["您有一条语音留言,10秒内按下中间键可以收听",1]) - if self.pwk_notice_flag == 0 and EventMesh.publish("get_app_new_voice_flag"):#不处于播放状态并且是小程序下发的语音而不是请求的 - self.pwk_cnt_voice += 1#加上当前这条 - EventMesh.publish("set_storage_config",["is_noread_voice",1]) - EventMesh.publish("set_app_new_voice_flag",0) - print("voice_add_to = {}".format(self.pwk_cnt_voice)) - print("self.pwk_notice_flag = {}".format(self.pwk_notice_flag)) - if self.pwk_cnt_voice >1: - if self.pwk_notice_flag == 0: - tts_str = "您有" + str(self.pwk_cnt_voice) + "条未读语音留言,10秒内按下中间键可以收听" - EventMesh.publish("play_tts",[tts_str,1]) - elif self.pwk_cnt_voice == 1: - if self.pwk_notice_flag == 0: - tts_str = "您有1条语音留言,10秒内按下中间键可以收听" - EventMesh.publish("play_tts",[tts_str,1]) - if is_close_VC != None: - self.Close_flag = is_close_VC - if self.AI_VC_send_data_queue.empty()!=True: - self.AI_VC_send_data_queue.clear() - if self.AI_ASR_send_data_queue.empty()!=True: - self.AI_ASR_send_data_queue.clear() - EventMesh.publish("set_led_state", {"ai_state": "thinking"}) - self.AI_ASR_send_buf_len = 0 - self._uplink_request_stop("play_new_voice_cb") - self.websocket_deconnect_VC() - self.AI_VC_start() - self.play_new_voice_flag = 1 - # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - except Exception as e: - usys.print_exception(e) - pass - #历史留言查看 - def play_his_voice(self,event = None,msg= None): - EventMesh.publish("publish_AI_btnInfo",{"func":self.play_his_voice_cb,"data":msg}) - def play_his_voice_cb(self,data): - try: - new_voice_url = data[0] #音频地址 - is_close_VC = data[1] - time = data[2] - tts_play_str = time + "收到" - EventMesh.publish("play_tts",[tts_play_str,1]) - if new_voice_url != "": - self.recv_voice = 1#收到留言 - print("历史留言查看") - # tts_play_str = "收到" - # EventMesh.publish("play_tts",[tts_play_str,1]) - # EventMesh.publish("set_motor_time",[1,1,5,8,4])#设置马达参数 - self.AI_new_voice_url = new_voice_url - if is_close_VC != None: - self.Close_flag = is_close_VC - if self.AI_VC_send_data_queue.empty()!=True: - self.AI_VC_send_data_queue.clear() - if self.AI_ASR_send_data_queue.empty()!=True: - self.AI_ASR_send_data_queue.clear() - EventMesh.publish("set_led_state", {"ai_state": "thinking"}) - self.AI_ASR_send_buf_len = 0 - self._uplink_request_stop("play_his_voice_cb") - self.websocket_deconnect_VC() - self.AI_VC_start() - self.play_new_voice_flag = 1 - # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - except Exception as e: - usys.print_exception(e) - pass - #等待未读留言查看 - def play_wait_voice(self,event = None,msg= None): - EventMesh.publish("publish_AI_btnInfo",{"func":self.play_wait_voice_cb,"data":msg}) - def play_wait_voice_cb(self,data): - try: - new_voice_url = data[0] #音频地址 - is_close_VC = data[1] - print("是否为第一次:{}".format(self.wait_first_flag)) - if self.wait_first_flag == 0: - tts_play_str = "您有" + str(len(EventMesh.publish("get_play_new_voice_list"))) + "条未听留言,10秒内按下中间键可以收听" - EventMesh.publish("play_tts",[tts_play_str,1]) - # self.wait_first_timer.start(1000,1,self.noread_wait_vocie) - self.wait_first_flag = 1 - elif self.wait_first_flag == 1: - self.wait_sort += 1 - self.AI_TTS_str = "第"+str(self.wait_sort)+"条" - if new_voice_url != "": - self.recv_voice = 1#收到留言 - print("未读留言查看") - self.AI_new_voice_url = new_voice_url - if is_close_VC != None: - self.Close_flag = is_close_VC - if self.AI_VC_send_data_queue.empty()!=True: - self.AI_VC_send_data_queue.clear() - if self.AI_ASR_send_data_queue.empty()!=True: - self.AI_ASR_send_data_queue.clear() - EventMesh.publish("set_led_state", {"ai_state": "thinking"}) - self.AI_ASR_send_buf_len = 0 - self._uplink_request_stop("play_wait_voice_cb") - self.websocket_deconnect_VC() - self.AI_VC_start() - self.play_new_voice_flag = 1 - # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - except Exception as e: - usys.print_exception(e) - pass - def noread_wait_vocie(self,*args):#如果一直没读 - self.wait_first_cnt += 1 - print("未读留言等待收听中:{}".format(self.wait_first_cnt)) - if EventMesh.publish("get_recv_voice") == 1:#如果检测到留言播放 - self.wait_first_cnt = 0 - self.wait_first_timer.stop() - if self.wait_first_cnt == 15: - EventMesh.publish("set_wait_first_flag",0) - EventMesh.publish("set_history_voice_flag",0) - EventMesh.publish("set_wait_voice_flag",0) - self.wait_first_cnt = 0 - self.wait_first_timer.stop() - #音乐播放 - def play_music(self,event = None,msg= None): - EventMesh.publish("publish_AI_btnInfo",{"func":self.play_music_cb,"data":msg}) - def play_music_cb(self,data): - try: - music_name = data[0] - new_voice_url = data[1] #音频地址 - is_close_VC = data[2] - if new_voice_url != "": - self.AI_new_voice_url = new_voice_url - self.music_play_flag = 1#表示音乐播放开启 - EventMesh.publish("play_tts",["下面为您播放歌曲{}".format(music_name),1]) - if is_close_VC != None: - self.Close_flag = is_close_VC - if self.AI_VC_send_data_queue.empty()!=True: - self.AI_VC_send_data_queue.clear() - if self.AI_ASR_send_data_queue.empty()!=True: - self.AI_ASR_send_data_queue.clear() - EventMesh.publish("set_led_state", {"ai_state": "thinking"}) - self.AI_ASR_send_buf_len = 0 - self._uplink_request_stop("play_music_cb") - self.websocket_deconnect_VC() - self.AI_VC_start() - self.play_new_voice_flag = 1 - # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - except Exception as e: - usys.print_exception(e) - - #AI翻译接收TTS - def play_trans_tts(self,event = None,msg= None): - EventMesh.publish("publish_AI_btnInfo",{"func":self.play_trans_tts_cb,"data":msg}) - def play_trans_tts_cb(self,data): - try: - new_voice_url = data #音频地址 - if new_voice_url != "": - self.AI_new_voice_url = new_voice_url - if self.AI_VC_send_data_queue.empty()!=True: - self.AI_VC_send_data_queue.clear() - if self.AI_ASR_send_data_queue.empty()!=True: - self.AI_ASR_send_data_queue.clear() - EventMesh.publish("set_led_state", {"ai_state": "thinking"}) - self.AI_ASR_send_buf_len = 0 - self._uplink_request_stop("play_trans_tts_cb") - self.websocket_deconnect_VC() - self.AI_VC_start() - EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 - self.play_new_voice_flag = 1 - # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) - except Exception as e: - usys.print_exception(e) - def AI_client_new_voice(self): #上传未读的语音地址/包括音频地址也可以 - json_data = \ - { - "event": "audio.convert", - "payload": { - "audio_convert_request": { - "audio_url": self.AI_new_voice_url, - "bit_depth": 16, - "channels": 1, - "sample_rate": 16000, - "target_format": "pcm" - } - } - } - self.AI_new_voice_url = "" - print("AI_client_new_voice!!!!!!!!!!!!!!!!!!!!!!!!!!!!!{}".format(json_data)) - self.AI_client_VC_send(ujson.dumps(json_data)) - - - def chenggong_tone(self): - EventMesh.publish("audio_montage_play",["chenggong.mp3"]) - # self.aud.aud_tone_play(10,400) - def send_tone(self): - EventMesh.publish("audio_montage_play",["send_tone.mp3"]) - def concurrency_limit_exceeded_tone(self): - self.Close() - EventMesh.publish("audio_montage_play",["chaobingfatis.mp3"]) #播放超并发提醒 - #给服务器回应ack - - - ############################################ - #关闭音频播放线程 - def AI_audio_queue_thread_close(self): - print("AI_audio_queue_thread_close1!!!!!!!!!!!!!!!!!!!!!!") - if self.AI_audio_queue_thread_id != 0: - count =500 - while True: - count -= 1 - if self.AI_audio_queue_thread_lock == 0 or count <=0: - break - else: - self.AI_audio_queue_thread_lock = 2 - print("AI_audio_queue_thread going:{}...".format(count)) - utime.sleep_ms(1) - self.AI_audio_queue_thread_lock = 0 - print("AI_audio_queue_thread_close2!!!!!!!!!!!!!!!!!") - if self.AI_audio_queue_thread_id != 0: - try: - _thread.stop_thread(self.AI_audio_queue_thread_id) - except Exception as e: - usys.print_exception(e) - self.AI_audio_queue_thread_id = 0 - # 音频队列 - def AI_audio_queue_thread(self): - print("AI_audio_queue_thread start") - count = 0 - if self.AI_audio_queue.empty()!=True: - self.AI_audio_queue.clear() - print("音频数据还有剩余!!!!!!!!!!!!") - while True: - try: - if self.AI_audio_queue_thread_lock == 2 or self.VC_stop_play_audio_flag == 0: - print("AI_audio_queue_thread 该退出了") - break - # - count+=1 - if count%500 == 1 and VC_HEARTBEAT_LOG: - print("AI_audio_queue_thread on") - utime.sleep_ms(1) - #print("AI_audio_queue_thread") - if self.AI_audio_queue.empty() : - self.AI_audio_queue_thread_lock = 0 - - - else: - self.AI_audio_queue_thread_lock = 1 - data = self.AI_audio_queue.get() - self.close_timerout = 0 - if self.play_flag == 0: - self.play_flag =1 - #self.AI_client_tts_data_len=len(data) - #print("audio_queue:len:{}".format(len(data[0]))) - - if len(data[0]) > 10: - try: - if self._ptt_should_drop_inbound_tts(): - continue - except Exception: - pass - for i in range(5): - if self.VC_Play_flag == 0: - self.VC_Play_Start() - utime.sleep_ms(2) - else: - break - # 打断后 PCM 偶发未打开:加重试,避免「队列有数据、play_pcm=None、无声」 - if self.VC_play_TYPE == VC_play_TYPE_PCM and self.play_pcm is None and self.VC_stop_play_audio_flag == 1: - for j in range(4): - if self.VC_Play_flag == 0: - self.VC_Play_Start() - utime.sleep_ms(30 + j * 35) - if self.play_pcm is not None: - break - try: - if self.VC_play_TYPE == VC_play_TYPE_PCM: - # if self.VC_play_samplerate == 8000: - # tmp= self.aud.playStream(1,data[0] ) - # else: - write_buf = bytearray(data[0]) - #print("play_pcm.stream_write1 len:{}".format(len(write_buf))) - if self.play_pcm != None: - #print("play_pcm.stream_write !!!") - ret = self.play_pcm.stream_write(write_buf,len(write_buf)) - #ret= self.aud.playStream(1, data[0])#播放音频流 - if self.tts_out_data_len == 0: - try: - sid = getattr(self, "_cur_sid", "") - did = getattr(self, "_cur_dialog_id", "") - except Exception: - sid, did = "", "" - print("[TTS] first_write ret={} len={} sid={} did={}".format( - ret, len(write_buf), sid, did - )) - if ret <= 0: - print("[TTS] stream_write ret={} len={} (no audio?)".format(ret, len(write_buf))) - self.tts_out_data_len += ret - #print("play_pcm.stream_write2:{}".format(self.tts_out_data_len)) - elif self.VC_play_TYPE == VC_play_TYPE_MP3: - tmp= self.aud.playStream(3, data[0])#播放音频流 - - if tmp != 0: - print("2playStream:error tmp:{} data len:{}!!!!!!!!!!!!".format(tmp,len(data[0]))) - # # #播放失败的时候填充WAV头 - # data_len = len(data[0]) - # data_len_b = data_len.to_bytes(4,'little')#数据长度转16进制数组 - # h_data = [ 0x52 ,0x49 ,0x46 ,0x46 #,0x24 ,0x00 ,0x00 ,0x00 #文件标识、文件大小 - # ,0x57 ,0x41 ,0x56 ,0x45 ,0x66 ,0x6D ,0x74 ,0x20 #文件格式、格式块标识符 - # ,0x10 ,0x00 ,0x00 ,0x00 ,0x01 ,0x00 ,0x01 ,0x00 ,0x80 ,0x3E ,0x00 ,0x00 #格式块大小4、音频格式2、声道数2、采样率4 - # ,0x80 ,0x3E ,0x00 ,0x00 ,0x02 ,0x00 ,0x10 ,0x00 ,0x64 ,0x61 ,0x74 ,0x61]#每秒字节数4、数据块对齐2、位深度2、data4 - # h_data_b = bytes(h_data) - # file_len = data_len+len(h_data_b)+4+4 - # file_len_b = file_len.to_bytes(4,'little')#文件长度转16进制数组 - # h_data_b = h_data_b[:4]+file_len_b+h_data_b[4:]+data_len_b - # #self.aud.playStream(2, h_data_b)#播放音频流 - # tmp= self.aud.playStream(2, h_data_b+data[0])#播放音频流 - # print("3playStream:tmp:{} !!!!!!!!!!!!".format(tmp)) - - - mem = gc.mem_free() - #print('1剩余内存RAM:{} KB'.format(mem / 1024)) - except Exception as e: - print("AI_audio_queue_thread:ERROR:") - usys.print_exception(e) - - # if self.VC_state == VC_State_TTSEnd and self.AI_audio_queue.empty() :#self.AI_audio_queue为空且tts下发结束 - # #通知服务器播放结束 - # self.AI_client_VC_LocalRespondingEnded() - except Exception as e: - print("AI_audio_queue_thread:ERROR:") - usys.print_exception(e) - self.AI_audio_queue_thread_lock = 0 - self.AI_audio_queue_thread_id = 0 - print("AI_audio_queue_thread:exit") - def factory_mode(self): - print("factory_mode") - EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) - EventMesh.publish("publish_AI_btnInfo",{"func":self.factory_start}) - def factory_start(self): - self.factory_timer.start(3000, 1, self.factory_timer_cb) - def factory_timer_cb(self, *args, **kwargs): - print("factory_timer_cb play......") - #self.aud.play(1,0,'U:/static/peizhichenggong.mp3')#切换成功 - EventMesh.publish("audio_montage_play",["xinhaoruo1.mp3"]) - def Close_VC(self,event,msg): - EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) - def Close(self): - try: - if getattr(self, "VC_mode", 0) != 1: - print("[VC][long_idle] Close() enter VC_mode={} {} VC_satrt_flag={} VC_record_state_flag={}".format( - getattr(self, "VC_mode", -1), self._vc_fsm_log_snip(), - getattr(self, "VC_satrt_flag", -1), getattr(self, "VC_record_state_flag", -1))) - except Exception: - pass - self.VC_stop_play_audio_flag = 0 - try: - self.AI_key_long_press_flag = 0 - except Exception: - pass - self.VC_Thinking_timerout = 0 - self.AI_ASR_timer.stop() - self._1S_timer.stop() - self.close_timerout = 0 - self._idle_deep_sec = 0 - self.factory_timer.stop() - self.AI_TTS_str = "" - self.AI_ASR_str = "" - self.AI_ASR_str_tmp = "" - self.AI_ASR_flag = 0 - self.AI_ASR_check_timeout = 0 - self.AI_ASR_error_timeout = 0 - self.LocalRespondingEnded_timerout = 0 - self._need_audio_played_ack = 0 - self._tts_pending = 0 - self._tts_pending_ms = 0 - self._vc_session_started = 0 - self._ptt_audio_created = 0 - self._vc_pending_interrupt_uplink = 0 - try: - self._skip_started_uplink_once = 0 - except Exception: - pass - try: - self._ptt_abort_interrupt_arm() - except Exception: - pass - # Power 短按停止对话后,再次按 AI 需要新建 dialog_id:清理本轮 dialog/session 标识 - try: - self._cur_dialog_id = "" - except Exception: - pass - try: - self._cur_sid = "" - except Exception: - pass - self.is_ruppt_tts = 0#打断标志位清零 - print("ai_VC Close1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") - try: - if getattr(self, "VC_mode", 0) != 1: - print("[VC][long_idle] step=set_led_state_idle (first)") - except Exception: - pass - EventMesh.publish("set_led_state", {"ai_state": "idle"}) - self.tts_in_data_len = 0 - self.tts_out_data_len = 0 - #必须先销毁线程,不然线程会容易报错 - - try: - self.AI_audio_queue_thread_close()#销毁音频线程 - except Exception as e: - usys.print_exception(e) - try: - self.AI_VC_send_data_queue_thread_close()#销毁数据发送线程 - except Exception as e: - usys.print_exception(e) - - # Close 可能被不同线程触发:统一切回主线程执行 stop - try: - if getattr(self, "VC_mode", 0) != 1: - print("[VC][long_idle] step=schedule_uplink_stop reason=Close (async publish_AI_btnInfo)") - except Exception: - pass - EventMesh.publish("publish_AI_btnInfo",{"func":self._uplink_request_stop, "data":"Close"}) - - print("ai_VC Close1.1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") - - print("ai_VC Close1.2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") - - try: - if getattr(self, "VC_mode", 0) != 1: - print("[VC][long_idle] step=websocket_deconnect_VC") - except Exception: - pass - try: - self.websocket_deconnect_VC() - except Exception as e: - usys.print_exception(e) - - - - - - print("ai_VC Close1.3!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") - - print("ai_VC Close2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") - - - - - if self.record_data_save_flag == 1: - if self.record_fd : - self.record_fd.close() - self.record_fd = None - if self.play_data_save_flag == 1: - if self.play_fd !=None: - self.play_fd.close() - self.play_fd = None - print("ai_VC Close3!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n") - - if self.AI_VC_send_data_queue.empty()!=True: - self.AI_VC_send_data_queue.clear() - self.play_new_voice_flag = 0 #语音消息播放状态 - if self.voice_choose_flag == 0: # 如果没有未读语音 - self.AI_new_voice_url = "" # 语音播放地址 - print("关闭并重置语音标志位") - else: - print("关闭但是没有重置语音标志位") - self.AI_ASR_send_buf_len = 0 - #self.AI_WS_connect_Queue.put(self.AI_client_VC) - try: - if getattr(self, "VC_mode", 0) != 1: - print("[VC][long_idle] step=set_led_state_idle (second) -> VC_init VC_satrt_flag=0") - except Exception: - pass - EventMesh.publish("set_led_state", {"ai_state": "idle"}) - self.VC_init() - self.VC_satrt_flag = 0 - # Level2:会话结束后仍可继续 1s 节拍累计 idle 直至深睡(依赖 VC_IDLE_DEEP_SLEEP_SEC) - try: - _dsec = 60 - _den = 1 - if "vc_config" in globals() and vc_config: - _dsec = int(vc_config.get_int("VC_IDLE_DEEP_SLEEP_SEC", 60)) - _den = int(vc_config.get_int("VC_IDLE_DEEP_SLEEP_ENABLE", 1)) - if _den and _dsec > 0: - self._1S_timer.start(1000, 1, self._1S_timer_cb) - except Exception: - pass - # try: - # while True: - # if self.AI_WS_connect_Queue.empty()!=True: - # AI_WS_connect = self.AI_WS_connect_Queue.get() - # if AI_WS_connect : - # try: - # AI_WS_connect._close() - # AI_WS_connect = None - # except Exception as e: - # usys.print_exception(e) - # else: - # print("websocket链接已为空") - # else: - # break - # except Exception as e: - # usys.print_exception(e) - - def VC_init2(self): - self.VC_init() - - print("ai_VC VC_init2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") - try: - self.websocket_deconnect_VC() - except Exception as e: - usys.print_exception(e) - print("ai_VC Close3!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n") - self._1S_timer.stop() - if self.AI_VC_send_data_queue.empty()!=True: - self.AI_VC_send_data_queue.clear() - - self.AI_ASR_send_buf_len = 0 - if self.AI_audio_queue.empty()!=True: - self.AI_audio_queue.clear() - def VC_init(self): - _q0 = self.AI_audio_queue.size() - self.VC_Play_End() - if self.AI_audio_queue.empty()!=True: - self.AI_audio_queue.clear() - print("[VC] init audio_queue {} -> {}".format(_q0, self.AI_audio_queue.size())) - # num = EventMesh.publish("get_volum_size") - # if num == None: - # num = 8 - # self.aud.setVolume(num)#恢复原始音量 - -def num_str_to_chinese(num_str): - # 数字与中文映射(含零) - num_map = { - '0': '零', '1': '一', '2': '二', '3': '三', '4': '四', - '5': '五', '6': '六', '7': '七', '8': '八', '9': '九' - } - chinese = '' - # 逐位转换,拼接单位 - for char in num_str: - if char in num_map: - digit = num_map[char] - chinese += digit - else: - chinese += char - - return chinese - +# -*- coding: utf-8 -*- +""" +语音对话入口(兼容 frozen 模块名 ai_VC)。 +实现已拆分至 vc_* 子模块,本文件仅 re-export。 +""" +from vc_led import AI_led +from vc_hw import AI_hw_status, set_ipv4_datacall +from vc_uart import AI_UART +from vc_buttons import AI_dev, audio_buf +from vc_storage import getStorageDataManager +from vc_auth import device_res_author +from vc_ota import OTA +from vc_fsm import ( + VCSessionState, + VCTurnState, + VC_TtsRxPhase, + VCMode, + VCStateMachine, +) +from vc_session import AI_VC +from vc_utils import num_str_to_chinese +from vc_common import ( + dev_type, + VC_HEARTBEAT_LOG, + VC_PROTO_GUARDS, + VC_WS_IO_LOG, + _VC_KEEPALIVE_RX_EVENTS, + _vc_out_json_event_meta, + PRESS_AUDIO_MODE, +) + +__all__ = [ + "AI_led", + "AI_hw_status", + "set_ipv4_datacall", + "AI_UART", + "AI_dev", + "audio_buf", + "getStorageDataManager", + "device_res_author", + "OTA", + "VCSessionState", + "VCTurnState", + "VC_TtsRxPhase", + "VCMode", + "VCStateMachine", + "AI_VC", + "num_str_to_chinese", + "dev_type", +] diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/main_init.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/main_init.py index ccb2a211..e1439b1e 100644 --- a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/main_init.py +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/main_init.py @@ -107,62 +107,7 @@ def main_init(): app = watchApp() app.set_ui(ui) - # if EventMesh.model == "D15": - # from motor import motorEventManager - # app.append_manager(motorEventManager()) - - # app.append_manager(mgr.getStorageDataManager()) - # app.append_manager(mgr.AlarmVolumeLevelManager()) \ - # .append_manager(mgr.MsgVolumeLevelManager()) \ - # .append_manager(mgr.PhoneVolumeLevelManager()) \ - # .append_manager(mgr.DeviceInfoManager()) \ - # .append_manager(mgr.BatteryManager()) \ - # .append_manager(intervManager())\ - # .append_manager(ledEventManager())\ - # .append_manager(playImeiManager())\ - # .append_manager(playCurStepManager())\ - # .append_manager(usbStateManager())\ - # .append_manager(hrandBloodOxgenManager())\ - # .append_manager(simManager())\ - # .append_manager(PoweronManager())\ - # .append_manager(sosManager())\ - # .append_manager(playCurTimeManager()) - # .append_manager(mgr.StopwatchManager()) \ - # .append_manager(mgr.CountdownManager()) \ - - # app.append_manager(mgr.ConfigStoreManager()) - # app.append_manager(protocol.NetManager()) - # app.append_manager(mgr.LocationManager()) - # app.append_manager(mgr.AddrBookManager()) - # app.append_manager(mgr.MsgManager()) - # app.append_manager(mgr.MsgdetailManager()) - # app.append_manager(mgr.CallLogManager()) - # app.append_manager(mgr.getWeatherDataManager()) - # app.append_manager(mgr.PhoneCallingManager()) - # app.append_manager(mgr.ClockManager()) - # app.append_manager(mgr.sendDevinfoManager()) - - # app.append_manager(mgr.gethistoryDataManager()) - # app.append_manager(mgr.getHrTimeManager()) - # app.append_manager(mgr.getBrightnessManager()) - - # app.append_manager(mgr.getUpLoadDataManager()) - # app.append_manager(mgr.getVolumSizeManager()) - # app.append_manager(mgr.getTempTimeManager()) - # app.append_manager(mgr.getplayAudiodStateManager()) - # app.append_manager(mgr.getLowBatManager()) - # app.append_manager(mgr.getBootOffManager()) - # app.append_manager(mgr.getOldTimeManager()) - # app.append_manager(mgr.getSosFlagManager()) - # app.append_manager(mgr.CountdownManager()) - # app.append_manager(mgr.sosInfoCallManager()) - # app.append_manager(mgr.testSmsSetIPManager()) - # app.append_manager(mgr.deleteSmsManager()) - # app.append_manager(mgr.schoolclassManager()) - # app.append_manager(mgr.firwDownloadManager()) - # if EventMesh.model == "D15": - # app.append_manager(mgr.vedioInforManager()) try: try: import usr.ai_VC as _ai @@ -318,68 +263,7 @@ def main_init(): EventMesh.debug_log("init21:{}".format(utime.localtime())) EventMesh.debug_log("init11:{}".format(utime.localtime())) - - #EventMesh.publish("open_power_on", 1)# - # pcr = pro.ProtoColResolver() - # pcr.add_support(protocol.LK_ProtoCol()) - # pcr.add_support(protocol.PING_ProtoCol()) - # pcr.add_support(protocol.KA_ProtoCol()) - # pcr.add_support(protocol.UD_ProtoCol()) - # pcr.add_support(protocol.CR_ProtoCol()) - # pcr.add_support(protocol.AL_ProtoCol()) - # pcr.add_support(protocol.FIND_ProtoCol()) - # pcr.add_support(protocol.POWEROFF_ProtoCol()) - # pcr.add_support(protocol.PHL_ProtoCol()) - # pcr.add_support(protocol.TK_ProtoCol()) - # pcr.add_support(protocol.TKS_ProtoCol()) - # pcr.add_support(protocol.MESSAGE_ProtoCol()) - # pcr.add_support(protocol.WT_ProtoCol()) - # pcr.add_support(protocol.REMIND_ProtoCol()) - # pcr.add_support(protocol.HRSTART_ProtoCol()) - # pcr.add_support(protocol.HEART_ProtoCol()) - # pcr.add_support(protocol.OXYGEN_ProtoCol()) - # pcr.add_support(protocol.OXYGEN_One_ProtoCol()) - # pcr.add_support(protocol.TEMP_ProtoCol()) - # pcr.add_support(protocol.DLT_ProtoCol()) - # pcr.add_support(protocol.UPLOAD_ProtoCol()) - # pcr.add_support(protocol.MONITOR_ProtoCol()) - # # pcr.add_support(protocol.CENTER_ProtoCol()) - # pcr.add_support(protocol.CENTER_ProtoCol()) - # pcr.add_support(protocol.bootoff_ProtoCol()) - # pcr.add_support(protocol.WDSTART_ProtoCol()) - # pcr.add_support(protocol.LOWBAT_ProtoCol()) - # pcr.add_support(protocol.sosHome_dial_ProtoCol()) - # pcr.add_support(protocol.Time_ProtoCol()) - # pcr.add_support(protocol.SILENCETIME2_ProtoCol()) - # pcr.add_support(protocol.FALLDOWN_ProtoCol()) - # pcr.add_support(protocol.HELP_ProtoCol()) - # pcr.add_support(protocol.REMOVE_ProtoCol()) - # pcr.add_support(protocol.UD_ProtoCol()) - # pcr.add_support(protocol.bldstart_ProtoCol()) - # pcr.add_support(protocol.blood_ProtoCol()) - # pcr.add_support(protocol.bgstart_ProtoCol()) - # pcr.add_support(protocol.bloodglucose_ProtoCol()) - # pcr.add_support(protocol.REMIND_CHDRINK_ProtoCol()) - # pcr.add_support(protocol.REMIND_CHMEDICINE2_ProtoCol()) - # pcr.add_support(protocol.WLKEY_ProtoCol()) - # pcr.add_support(protocol.REMIND_CH_WAKEUP_ProtoCol()) - # pcr.add_support(protocol.REMIND_CH_SLEEP_ProtoCol()) - # pcr.add_support(protocol.REMIND_CH_HOME_ProtoCol()) - # pcr.add_support(protocol.REMIND_CH_REST_ProtoCol()) - # pcr.add_support(protocol.SLEEP_DATA_ProtoCol()) - # pcr.add_support(protocol.SLEEPTIME_ProtoCol()) - # pcr.add_support(protocol.FACTORY_ProtoCol()) - # pcr.add_support(protocol.RESET_ProtoCol()) - # pcr.add_support(protocol.REMIND_CH_SIT_ProtoCol()) - # pcr.add_support(protocol.SCREEN_SHOWProtoCol()) - - - - - # sm = protocol.SockManager() - # sm.add_support_resolver(pcr) - # app.append_manager(sm) EventMesh.debug_log("init11.0:{}".format(utime.localtime())) app.start() diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/protocol.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/protocol.py index f6b2ea7e..499b3411 100644 --- a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/protocol.py +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/protocol.py @@ -1,7072 +1,4200 @@ -try: - from qp_log import qp_print as print -except Exception: - pass -import ujson -import utime -import EventMesh -import constants -from common import Abstract -import uhashlib -import ubinascii -from misc import Power -import audio -from machine import Pin -import ujson -import ql_fs -import dataCall -import checkNet -import net -import usocket -import osTimer - -import machine -import urandom -import _thread -import usys -import request -import umqtt -import modem -SHA256 = uhashlib.sha256 -import SecureData -import versions -import version_util -try: - import vc_config as _vc_cfg - _NLP_EXTENDED_CMDS = _vc_cfg.get_bool("NLP_EXTENDED_CMDS", False) -except Exception: - _NLP_EXTENDED_CMDS = False - -def server_init(server_type=None): - # 服务器选择1,防走丢,2大锤,3三基同创,4童信,5云米,6荣为 8:ds未知 dey.hinwatch. com:9968 9:183.62.239.121:31869 10:183.62.239.121:33461 - if server_type is None: - server_type = 2 - # EventMesh.debug_log("server_init:server_type:{}".format(server_type)) - - d_server_addr = "api.xcloudtech.com" - d_server_port = 9000 - d_md5_key = "bZld0ALsZTt15tKEZhOlLPgbsO75BLTP" - d_PID = "SZKW" - if server_type == 1: - pass - - elif server_type == 2: - # test.iot.socket.thisiot.com:9999 - # iot.socket .thisiot.com:9999 - # d_server_addr = "test.iot.socket.thisiot.com" - # d_server_port = 9999 - # d_md5_key = "3JyCjsw4ryXKzjzudayY" - # d_PID = "qAELPTKmFanR4RHe" - - d_server_addr = "iot.socket.thisiot.com" - d_server_port = 9999 - d_md5_key = "SZYhyGhroQ3xFKFWZq1g" - d_PID = "oxLwDqcwJCtiPSRu" - - elif server_type == 3: - d_server_addr = "tcp.zhihuixuexiao.com" - d_server_port = 2346 - d_md5_key = "" - d_PID = "3G" - elif server_type == 4: - d_server_addr = "tcp.tx.kidxin.com" - d_server_port = 20500 - elif server_type == 5: - d_server_addr = "device.miwitracker.com" - d_server_port = 8505 - d_md5_key = "" - d_PID = "3G" - elif server_type == 6: - d_server_addr = "120.26.104.21" - d_server_port = 9968 - d_md5_key = "bZld0ALsZTt15tKEZhOlLPgbsO75BLTP" - d_PID = "SZKW" - elif server_type == 7: - d_server_addr = "8.134.68.15" - d_server_port = 30346 - d_PID = "KnHnj1Cd5VxcO3Ft" - d_md5_key = "evxThVhMBmRNiQ1Twigw" - elif server_type == 99: - d_server_addr = "lab.xcloudtech.com" - d_server_port = 9000 - elif server_type == 8: - d_server_addr = "dev.hxinwatch.com" - d_server_port = 9968 - elif server_type == 9: - d_server_addr = "183.62.239.121" - d_server_port = 31869 - elif server_type == 10: - d_server_addr = "183.62.239.121" - d_server_port = 33461 - - return [d_server_addr, d_server_port, d_md5_key, d_PID] -from queue import Queue -def unicode_to_utf8(src): - ref = 0xe08080 - result = ref - - #取最后六位 - a = src & 0x3f - - #将最后六位放在ref最后六位的空位置 - result = result | a - src = src >> 6 - - #取接下来的六位 - a = src & 0x3f - - #放在ref对应的六个位置 - result = result | (a << 8) - - #保留最后四位 - src = src >> 6 - - #放在ref对应的四个位置 - result = result | (src << 16) - return result - -def UnicodeHexStr_To_Utf8Str(str): - odata = 0 - - result = bytes() - su =str.upper() - - i = 0 - for c in su: - tmp=ord(c) - if tmp <= ord('9') : - odata = odata << 4 - odata += tmp - ord('0') - - elif ord('A') <= tmp <= ord('F'): - odata = odata << 4 - odata += tmp - ord('A') + 10 - if(i%4==3): #每2字节获取一个int,拿到转换后的unicode int - #print(odata.to_bytes(2, 'big')) - #result.append(odata) - if odata > 255: - tmp = unicode_to_utf8(odata)#拿到转换后的utf8 int - tmp2 = tmp.to_bytes(3, 'big') #转bytes - else : - tmp2 = odata.to_bytes(1, 'big') - #print("tmp2:{}".format(tmp2)) - result = result + tmp2 - odata = 0 - i+=1 - return result.decode('utf-8') #返回utf8的str - -def gewu_init(): - productKey = "cu1shld45y4hjr1L" - return productKey -mqtt_state = 0 -def mqttsub_cb(topic, msg): - global mqtt_state - print("Subscribe Recv: Topic={},Msg={}".format(topic.decode(), msg.decode())) - jsload_data = ujson.loads(msg.decode()) - print("jsload_data-{} type:{}".format(jsload_data,type(msg.decode()))) - # jsload_data = {'data':'111','protocolType':'', 'host':'device.miwitracker.com', 'secretKey': 'bZld0ALsZTt15tKEZhOlLPgbsO75BLTP', 'port':8505, 'bid': '3G', 'extJsonStr': ''} - # ujson.loads(c) - # $sys/cu1shld45y4hjr1L/866610001011658/ext/regist - #data = {"messageId":"25","params":{"deviceSecret":"C1D75A63C0338978438E9197372FC698""}} - if "params" in jsload_data: - params_str = jsload_data["params"] - if "deviceSecret" in params_str: - str_tmp = params_str["deviceSecret"] - print(str_tmp) - EventMesh.publish("set_deviceSecret",str_tmp) - mqtt_state = 1 -def mqtterr_cb(error): - ''' - 接收umqtt线程内异常的回调函数 - ''' - print("error!!!!!!!!!!!".format(error)) - -def HMAC(k, m): - SHA1_BLOCK_SIZE = 64 - KEY_BLOCK = k + (b'\0' * (SHA1_BLOCK_SIZE - len(k))) - KEY_INNER = bytes((x ^ 0x36) for x in KEY_BLOCK) - KEY_OUTER = bytes((x ^ 0x5C) for x in KEY_BLOCK) - inner_message = KEY_INNER + m - outer_message = KEY_OUTER + SHA256(inner_message).digest() - return SHA256(outer_message) - - -def get_hmac_sha256(k, m): - data = HMAC(k, m).digest() - # print("HMAC(k, m).digest():", data) - # print("HMAC(k, m).digest():", ubinascii.hexlify(data)) - hex_data = ubinascii.hexlify(data) - hex_data = hex_data.decode('ascii') - return hex_data - -######################IOT###################### -import sms -import voiceCall -import quecgnss -import wifiScan -from misc import Power,USB -def wifiScan_cb(args): - print("wifi_data:{}".format(args)) - EventMesh.publish(constants.EVENT.WIFIINFO, args) - wifiScan.control(0) -#定位thered -class IOTWebLocationManager(Abstract): - - def __init__(self): - self.gps = None - #self.match = GPSMatch() - self.timer_read_GPS_info = osTimer() - self.timer_sos_report_info = osTimer() - self.wifi_data = [] - self.cell_data = [] - self.location_timer_count = 0 - self.location_check_data_timer_count = 0 - self.GGA_count = 0 - self.RMC_A_count = 0 - self.location_type = 0 #定位类型,0:正常定位,有gps数据或60秒关闭;1:测试模式,只有发CLOSE_LOCATION消息才关闭 - self.location_flag = 0 - self.start_location_flag = 0 - self.location_thread_id = 0 - self.location_queue = Queue(100) - self.lpm_fd = None - self.start_agps = 0 - self.sos_report_flag=0 - quecgnss.gnssEnable(0) - - def post_processor_after_instantiation(self): - EventMesh.subscribe(constants.EVENT.START_LOCATION, self.start_location) - EventMesh.subscribe(constants.EVENT.WIFIINFO, self.WIFI_INFO) - EventMesh.subscribe(constants.EVENT.WIFISCANOPEN,self.WIFI_scan_open) - EventMesh.subscribe(constants.EVENT.CLOSE_LOCATION,self.close_location) - EventMesh.subscribe("get_location_flag",self.get_location_flag) - #EventMesh.subscribe(constants.EVENT.GPSCLOSE, self.gps_close) - def get_location_flag(self, event, msg): - return self.location_flag - - def sos_report_info(self): - EventMesh.debug_log("sos_report_info:") - if self.sos_report_flag == 1: - return - self.soswifi_data = [] - self.soscell_data = [] - self.sos_report_flag = 1 - self.timer_sos_report_info.start(3000, 1, self.sos_timer) - - def close_sos(self): - self.soswifi_data = [] - self.soscell_data = [] - self.sos_report_flag=0 - self.timer_sos_report_info.stop() - - - def sos_timer(self, *args, **kwargs): - if self.sos_report_flag == 1: - try : - #检查wifi数据 - if self.soswifi_data == [] or (self.soswifi_data != [] and self.soswifi_data[0] == 0):#and self.wifi_data[0] <= 0: - EventMesh.publish(constants.EVENT.WIFISCANOPEN) - - #检查基站数据 - if self.soscell_data== []: - data_tmp = net.getCellInfo() - if(len(data_tmp)>=2): - for cell in data_tmp: - if cell and cell[0][0] == 0 and len(cell[0]) > 1: - self.soscell_data = cell - break - if self.soscell_data != [] and self.soswifi_data != []: - self.close_sos() - self.Location_send([WEB_CMD_ENUM.SOS_REPORT_S_CMD,self.soswifi_data,self.soscell_data]) - - except Exception as e: - usys.print_exception(e) - - def start_location(self, event, msg): - self.location_queue.put(msg)#测试类型入队 - self.wifi_data = [] - self.cell_data = [] - print("start_location:{} self.location_flag:{}".format(msg,self.location_flag)) - if( self.location_flag == 1): - return - self.location_flag = 1 - _thread.stack_size((9+5)*1024) - self.location_thread_id = _thread.start_new_thread(self.location_thread, ()) - _thread.stack_size(8*1024) - # self.timer_read_GPS_info.start(1000, 1, self.location_timer) - # if msg == WEB_CMD_ENUM.SOS_REPORT_S_CMD: - # self.sos_report_info() - # pass - # elif msg == WEB_CMD_ENUM.GPS_REPORT_S_CMD: - # self.gps_report_init() - # pass - def gps_report_init(self): - - EventMesh.debug_log("start_location:") - if self.location_flag == 1: - return - self.wifi_data = [] - self.cell_data = [] - self.location_timer_count = 0 - self.location_flag = 1 - self.timer_read_GPS_info.start(3000, 1, self.location_timer) - - - def close_location(self,event,msg): - self.location_flag = 0 - if wifiScan.getState() == True: - wifiScan.control(0) - # self.timer_read_GPS_info.stop() - if self.location_queue.empty() == False: - self.location_queue.clear() - if self.location_thread_id != 0: - try: - _thread.stop_thread(self.location_thread_id) - except Exception as e: - pass - self.location_thread_id=0 - print("close_location:!!!!!!!!!") - - - - def WIFI_INFO(self, event, msg): - self.wifi_data = msg - - def WIFI_scan_open(self, event, msg): - - if wifiScan.getState() == False : - self.wifi_data = [] - wifiScan.control(1) - wifiScan.setCfgParam(6, 2, 5, 0) #设置WiFi参数,10秒上层,两轮,最多6个,6秒底层,PS优先 - wifiScan.setCallback(wifiScan_cb) #注册回调函数 - wifiScan.asyncStart() - #EventMesh.debug_log("wifiScan.asyncStart") - - def location_timer(self, *args, **kwargs): - if self.location_flag == 1: - try : - #检查wifi数据 - if self.wifi_data == [] or (self.wifi_data != [] and self.wifi_data[0] == 0):#and self.wifi_data[0] <= 0: - EventMesh.publish(constants.EVENT.WIFISCANOPEN) - - #检查基站数据 - if self.cell_data == []: - data_tmp = net.getCellInfo() - if(len(data_tmp)>=2): - for cell in data_tmp: - if cell and cell[0][0] == 0 and len(cell[0]) > 1: - self.cell_data = cell - break - if self.cell_data != [] and self.wifi_data != []: - self.close_location(0,0) - self.Location_send([WEB_CMD_ENUM.POST_LOCATION_S_CMD,self.wifi_data,self.cell_data]) - - except Exception as e: - usys.print_exception(e) - - def Location_send(self, msg) : - EventMesh.debug_log("Location_send1") - if self.location_queue.empty() == True: - return - EventMesh.debug_log("Location_send2") - print("Location_send1") - while True: - if self.location_queue.empty() == False: - TYPE = self.location_queue.get() - # (5, [('70:2A:D7:63:BD:B4', -38), ('C0:94:AD:80:21:5F', -61), ('D8:38:0D:05:1F:E3', -65), ('08:9E:84:E7:A6:38', -73), ('B0:44:14:BC:13:B0', -73)]) - wifi_tmp = [] - wifi_num = msg[0][0] - print("wifi_num:{}".format(wifi_num)) - if wifi_num == 0: - wifi_tmp = [] - else: #([], [], [(0, 69483137, 1120, 1, 385, 30504, 100, -75, -8), (3, 111950593, 1120, 1, 58, 30504, 1650, -98, -19), (3, 219097617, 1120, 17, 76, 30504, 2452, -106, -23), (3, 102345712, 1120, 1, 58, 30504, 3740, -96, -18)]) - - wifi_tmp_data = msg[0][1] - print(wifi_tmp_data) - for i in range(wifi_num): - list_data = {} - list_data["mac"] = str(wifi_tmp_data[i][0]) - list_data["rsi"] = str(wifi_tmp_data[i][1]) - # list_data = "{},{}".format(wifi_tmp_data[i][0],wifi_tmp_data[i][1]) - - wifi_tmp.append(list_data) - pass - # ([(flag, cid, mcc, mnc, lac, arfcn, bsic, rssi)], [(flag, cid, licd, mcc, mnc, lac, uarfcn, psc, rssi)], [(flag, cid, mcc, mnc, pci, tac, earfcn, rssi, rsrq),...]) - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - celList = [] - if msg[1] != []: - for CELL in msg[1] : - print("CELL:{}".format(CELL)) - CELL_data = {} - CELL_data["mcc"] = str(CELL[2]) - CELL_data["mnc"] = str(CELL[3]) - CELL_data["lac"] = str(CELL[4]) - CELL_data["cid"] = str(CELL[1]) - CELL_data["rsi"] = str(CELL[7]) - - - # CELL_data += str(CELL[2]) +"," #MCC - # CELL_data += str(CELL[3]) +"," #MNC - # CELL_data += str(CELL[4]) +"," #LAC - # CELL_data += str(CELL[7]) +"," #RSSI - celList.append(CELL_data) - s_msg="" - s_cmd = "" - if TYPE == WEB_CMD_ENUM.POST_LOCATION_S_CMD: - s_msg = "上报经纬度" - s_cmd = "post_location" - elif TYPE == WEB_CMD_ENUM.POST_SOS_S_CMD: - s_msg = "上报SOS求救" - s_cmd = "post_sos" - send_data = { - "cmd":s_cmd, - "ts":int(time_tmp), - "msg":s_msg, - "data":{ - "gps": { - "longitude": "0", - "latitude": "0", - "located": "0", - "speed": "0" - }, - "wifiList":wifi_tmp, - "celList":celList - - } - } - print("TYPE{}".format(TYPE)) - EventMesh.publish(TYPE,send_data) - else: - self.location_thread_id=0 - EventMesh.publish(constants.EVENT.CLOSE_LOCATION) - break - - - def location_thread(self): - - self.wifi_data = [] - print("location_thread self.location_flag {}".format(self.location_flag)) - while self.location_flag == 1: - try : - - #检查wifi数据 - if self.wifi_data == [] or (self.wifi_data != [] and self.wifi_data[0] == 0):#and self.wifi_data[0] <= 0: - EventMesh.publish(constants.EVENT.WIFISCANOPEN) - - #检查基站数据 - if self.cell_data == []: - print("location_thread") - data_tmp = net.getCellInfo() - if(len(data_tmp)>=2): - for cell in data_tmp: - if cell and cell[0][0] == 0 and len(cell[0]) > 1: - self.cell_data = cell - break - - if self.cell_data != [] and self.wifi_data != []: - self.Location_send([self.wifi_data,self.cell_data]) - - break - - except Exception as e: - usys.print_exception(e) - EventMesh.publish(constants.EVENT.CLOSE_LOCATION)#关闭定位 - utime.sleep_ms(1000) - wifiScan.control(0) - - - -import uwebsocket -import sim -# 设备主动上报后台返回cmd的字符串 -DEVICE_REPORT_RETURN_STRING = "post_rest" -# 服务器主动下发给设备的cmd的字符串 -IOT_AUTO_REPORT_RETURN_STRING = "dispatch_rest" -MAGIC_STR = len("FFFE") -VERSION_STR =len("03") -TYPE_STR = len("03") -PLAYLOAD_STR = len("0000") -PROTOCLO_LEN = MAGIC_STR+VERSION_STR+TYPE_STR+PLAYLOAD_STR+len('{')+len('}')+len("cmd:")+len("data:")+len("ts:") - -class WEB_CMD_ENUM: - HR_R_CMD = "post_heart_beat_R" - HR_S_CMD = "post_heart_beat_S" - - POST_LOCATION_R_CMD = "post_location_R" - POST_LOCATION_S_CMD = "post_location_S" - - POST_SOS_R_CMD = "post_sos_R" - POST_SOS_S_CMD = "post_sos_S" - - POST_DEVICE_STATE_R_CMD = "post_device_state_R" - POST_DEVICE_STATE_S_CMD = "post_device_state_S" - - POST_ALARM_R_CMD = "post_alarm_R" - POST_ALARM_S_CMD = "post_alarm_S" - - POST_SCHEDULE_R_CMD = "post_schedule_R" - POST_SCHEDULE_S_CMD = "post_schedule_S" - - FIND_DEVCE_R_CMD = "find_device_R" - FIND_DEVCE_S_CMD = "find_device_S" - - LOCATION_NOW_R_CMD = "locate_now_R" - LOCATION_NOW_S_CMD = "locate_now_S" - - LOCATION_DURATION_R_CMD = "locate_duration_R" - LOCATION_DURATION_S_CMD = "locate_duration_S" - - ALARM_CONFIG_R_CMD = "alarm_config_R" - ALARM_CONFIG_S_CMD = "alarm_config_S" - - ALARM_LIST_R_CMD = "alarm_list_R" - ALARM_LIST_S_CMD = "alarm_list_S" - - SCHEDULE_LIST_R_CMD = "schedule_list_R" - SCHEDULE_LIST_S_CMD = "schedule_list_S" - - SOS_LIST_R_CMD ="sos_list_R" - SOS_LIST_S_CMD ="sos_list_S" - - SOS_CONFIG_R_CMD = "sos_config_R" - SOS_CONFIG_S_CMD = "sos_config_S" - - DEVICE_SHAKE_STATE_R_CMD = "device_shake_state_R" - DEVICE_SHAKE_STATE_S_CMD = "device_shake_state_S" - - RING_TONE_URL_R_CMD = "ring_tone_url_R" - RING_TONE_URL_S_CMD = "ring_tone_url_S" - - VOLUME_R_CMD = "volume_R" - VOLUME_S_CMD = "volume_S" - - TEXT_MSG_R_CMD = "text_msg_R" - TEXT_MSG_S_CMD = "text_msg_S" - - VOICE_RECORD_R_CMD = "voice_record_R" - VOICE_RECORD_S_CMD = "voice_record_S" - - GET_RINGTONE_R_CMD = "get_ringtone_R" - GET_RINGTONE_S_CMD = "get_ringtone_S" - - RINGTONE_LIST_R_CMD = "ringtone_list_R" - RINGTONE_LIST_S_CMD = "ringtone_list_S" - - SCHEDULE_R_CMD = "schedule_R" - SCHEDULE_S_CMD = "schedule_S" - - SWITCH_BT_STATE_R_CMD="switch_bt_state_R" - SWITCH_BT_STATE_S_CMD="switch_bt_state_S" - - - BT_SCAN_R_CMD="bt_scan_R" - BT_SCAN_S_CMD="bt_scan_S" - - BT_STATE_R_CMD="bt_state_R" - BT_STATE_S_CMD="bt_state_S" - - BT_SCAN_REST_R_CMD = "bt_scan_rest_R" - BT_SCAN_REST_S_CMD = "bt_scan_rest_S" - - CONNECT_BT_R="connect_bt_R" - CONNECT_BT_S="connect_bt_S" - - DISCONNECT_BT_R="disconnect_bt_R" - DISCONNECT_BT_S="disconnect_bt_S" - - NEW_VOICE_MSG_R = "new_voice_msg_R" - NEW_VOICE_MSG_S = "new_voice_msg_S" - - GET_NEW_VOICE_MSG_R="get_new_voice_msg_R" - GET_NEW_VOICE_MSG_S="get_new_voice_msg_S" - - NEW_VOICE_MSG_LIST_R="new_voice_msg_list_R" - NEW_VOICE_MSG_LIST_S="new_voice_msg_list_S" - - PUT_VOICE_MSG_STATE_R="put_voice_msg_state_R" - PUT_VOICE_MSG_STATE_S="put_voice_msg_state_S" - - GET_BT_STATE_R="get_bt_state_R" - GET_BT_STATE_S="get_bt_state_S" - - GET_BT_CONNECT_RECORD_LIST_R = "get_bt_connect_record_list_R" - GET_BT_CONNECT_RECORD_LIST_S = "get_bt_connect_record_list_S" - - BT_CONNECT_RECORD_LIST_R="bt_connect_record_list_R" - BT_CONNECT_RECORD_LIST_S="bt_connect_record_list_S" - - FENCE_SOS_R="fence_sos_R" - FENCE_SOS_S="fence_sos_S" - - PLAY_CONTROL_R="player_control_R" - PLAY_CONTROL_S="player_control_S" - - GET_CHAT_CONFIG_R="get_chat_config_R" - GET_CHAT_CONFIG_S="get_chat_config_S" - - CHAT_CONFIG_R = "chat_config_R" - CHAT_CONFIG_S = "chat_config_S" - - POST_BT_CONNECT_REST_R = "post_bt_connect_rest_R" - POST_BT_CONNECT_REST_S = "post_bt_connect_rest_S" - - UNBIND_DEV_R="unbind_dev_R" - UNBIND_DEV_S="unbind_dev_S" - - USER_UNBIND_DEV_R="user_unbind_dev_R" - USER_UNBIND_DEV_S="user_unbind_dev_S" - - MUSIC_PLAY_R="music_play_R" - MUSIC_PLAY_S="music_play_S" - - NLP_PLAY_R = "ai_cmd_R" - NLP_PLAY_S = "ai_cmd_S" - - #金三 - GET_DEVICE_STATE_R = "get_device_state_R" - GET_DEVICE_STATE_S = "get_device_state_S" -class IOTSendCmdManager(Abstract): - # - def __init__(self): - self.send_data = "" - self.time_tmp = 0 - def post_processor_after_instantiation(self): - EventMesh.subscribe("GET_IOT_WEB_SEND_CMD", self.get_iot_send_cmd) - print("IOTSendCmdManager") - - def get_iot_send_cmd(self, event, msg): - date_tmp = utime.localtime() - self.time_tmp = utime.mktime(date_tmp) - if msg == WEB_CMD_ENUM.HR_S_CMD: - self.send_data = self.hr_send_data(self.time_tmp) - return self.send_data - elif msg == WEB_CMD_ENUM.POST_LOCATION_S_CMD: - self.send_data = self.gps_send_data() - return self.send_data - elif msg == WEB_CMD_ENUM.POST_SOS_S_CMD: - self.send_data = self.sos_wraing_send_data() - return self.send_data - elif msg == WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD: - self.send_data = self.device_report_state_send_data() - return self.send_data - elif msg == WEB_CMD_ENUM.POST_ALARM_S_CMD: - self.send_data = self.alarm_config_send_data() - elif msg == WEB_CMD_ENUM.POST_SCHEDULE_S_CMD: - self.send_data = self.schedule_config_send_data() - return self.send_data - - # 心率上报数据格式 - def hr_send_data(self,time): - print("hr_send_data") - battery = (EventMesh.publish("screen_get_battery"))[1] - if battery >= 100 or battery == None: - battery = 100 - hr_send_data = { - "cmd":"post_heart_beat", - "ts":time, - "msg":"心跳", - "data":{ - "battery": int(battery) - } - } - return hr_send_data - - # gps上报数据格式 - def gps_send_data(self): - gps_data = { - "cmd":"post_location", - "ts":self.time_tmp, - "data":{ - "gps":{ - "longitude":"W,67", - "latitude":"S,465", - "direction":"FR", - "speed":"45", - "altude":"56", - "count":"3" - }, - "wifiList":[ - "89:78:78:67:89:98,-98" - ], - "celList":[ - "MCC,MNC,LAC,CID,RSSI" - ], - "btList":[ - "D4:78:65:67:89:78" - ] - } - } - return gps_data - - # sos警告上报数据格式 - def sos_wraing_send_data(self): - sos_wraing_data ={ - "cmd":"post_sos", - "ts":self.time_tmp, - "data":{ - "gps":{ - "longitude":"W,67", - "latitude":"S,465", - "direction":"FR", - "speed":"45", - "altude":"56", - "count":"3" - }, - "wifiList":[ - "89:78:78:67:89:98,-98" - ], - "celList":[ - "MCC,MNC,LAC,CID,RSSI" - ], - "btList":[ - "D4:78:65:67:89:78" - ] - } - } - return sos_wraing_data - - #设备上报状态上报数据格式 - def device_report_state_send_data(self): - get_volume = EventMesh.publish("get_storage_config","VC_TTS_volume") - device_report_data ={ - "cmd":"post_device_state", - "ts":self.time_tmp, - "data":{ - "mediaTitle":"haha", - "mediaState":"stop", - "cover":"899", - "volume":get_volume - } - } - # HR_DATA=EventMesh.publish("GET_IOT_WEB_SEND_CMD",WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD) - # print("HR_DATA{}".format(HR_DATA)) - # EventMesh.publish(WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD,HR_DATA) - return device_report_data - - #闹钟配置上报数据格式 - def alarm_config_send_data(self): - alarm_data = { - "cmd":"post_alarm", - "ts":self.time_tmp, - "msg":"上报闹钟", - "data":[ - { - "name":"喝水", - "loop":"0101001", - "state":"0", - "time":"14:34", - "ringTimes":1, - "alarmMode":"ring", - "shakeType":"short" - }, - { - "name":"起床", - "loop":"0101001", - "state":"0", - "time":"07:40", - "ringTimes":1, - "alarmMode":"ring", - "shakeType":"short" - } - ] - } - - return alarm_data - #日程配置上报数据格式 - def schedule_config_send_data(self): - schedule_data = { - "cmd":"post_schedule", - "ts":self.time_tmp, - "msg":"上报日程", - "data":[ - { - "name":"喝水", - "loop":"0101001", - "state":"0", - "time":"14:34", - "ringTimes":1, - "alarmMode":"ring", - "shakeType":"short" - }, - { - "name":"起床", - "loop":"0101001", - "state":"0", - "time":"07:40", - "ringTimes":1, - "alarmMode":"ring", - "shakeType":"short" - } - ] - } - - return schedule_data -#解析收到的数据协议 -def playpolad_data(data): - if(isinstance(data, bytes) == 0 or len(data) <= PROTOCLO_LEN): - return - - if data[0] != 0xff and data[1] !=0xfe and data[2] != 0x03 and data[3] !=0x03: - return - - # print(type(data)) - json_bytes = data[8:] - # print(data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]) - # 2. 将二进制JSON转换为字符串(使用UTF-8解码) - json_str = json_bytes.decode('utf-8') - print("解析出的JSON字符串:") - print(json_str) - print("\n" + "-"*50 + "\n") - - # 3. 将JSON字符串转换为Python字典 - try: - data_dict = ujson.loads(json_str) - print("解析后的字典数据:") - print(data_dict) - - # 可以直接访问字典中的字段 - - cmd = data_dict['cmd'] - if cmd != None: - if cmd == DEVICE_REPORT_RETURN_STRING: - data_cmd = data_dict['data'] - if "cmd" in data_cmd: - cmd = data_dict['data']["cmd"] - # {"cmd ":"post_rest","ts":1753870158426,"data":{"cmd":"post_heart_beat","rest":"success","msg":"."}} - print("设备主动上报后台返回的数据 设备不需要回复") - EventMesh.publish(cmd+"_R",data_dict) - else: - print("返回的data里面没有cmd 返回的数据出错了") - else: - # {"cmd":"locate_now","msg":"","data":"876226211472149","ts":1753870224117} - print("后台主动下发给设备 需要设备回复给后台的数据命令") #后台主动下发的数据不为dict类型 - EventMesh.publish(cmd+"_R",data_dict) - else: - print("cmd is None") #下发的指令无不解析 - except Exception as e: - print("解析JSON出错:", str(e)) -date_tmp = utime.localtime() -time_tmp = utime.mktime(date_tmp) -MAGIC = (0xfffe).to_bytes(2,"big") -VERSION = (0x01).to_bytes(1,"big")#"03".encode('utf-8') -TYPE = (0x03).to_bytes(1,"big") -DATA_LEN = (0x00000000).to_bytes(4,"big") -#magic+version+type+playload_len+playload -#普通协议数据格式根据协议指定 -def protocol_send_total_data(data): - MAGIC = (0xfffe).to_bytes(2,"big") - VERSION = (0x01).to_bytes(1,"big")#"03".encode('utf-8') - TYPE = (0x03).to_bytes(1,"big") - HR_JSON = ujson.dumps(data) - GPS_JSON_b = HR_JSON.encode('utf-8') - DATA_LEN = (len(HR_JSON)).to_bytes(4,"big") - heards_infor = b'' - heards_infor = MAGIC+VERSION+TYPE+DATA_LEN+GPS_JSON_b - return heards_infor -#音频流数据格式 -#magic+version+type+msgid+playload_len+playload -def protocol_audio_ji_playload_data(data,msgid,endflag): - MAGIC = (0xfffe).to_bytes(2,"big") - VERSION = (0x01).to_bytes(1,"big")#"03".encode('utf-8') - TYPE = (0x05).to_bytes(1,"big") - MSGID = msgid.to_bytes(4,"big") - ENDFLAG = endflag.to_bytes(1,"big") - DATA_LEN = (len(data)).to_bytes(4,"big") - heards_infor = b'' - heards_infor = MAGIC+VERSION+TYPE+MSGID+ENDFLAG+DATA_LEN+data - return heards_infor - -#语音留言格式 -def protocol_yuyin_voice_playload_data(data,msgid,endflag): - MAGIC = (0xfffe).to_bytes(2,"big") - VERSION = (0x01).to_bytes(1,"big")#"03".encode('utf-8') - TYPE = (0x08).to_bytes(1,"big") - MSGID = msgid.to_bytes(4,"big") - ENDFLAG = endflag.to_bytes(1,"big") - DATA_LEN = (len(data)).to_bytes(4,"big") - heards_infor = b'' - heards_infor = MAGIC+VERSION+TYPE+MSGID+ENDFLAG+DATA_LEN+data - return heards_infor - -#会议纪要格式 -def protocol_meet_playload_data(data,msgid,endflag): - MAGIC = (0xfffe).to_bytes(2,"big") - VERSION = (0x01).to_bytes(1,"big")#"03".encode('utf-8') - TYPE = (0x09).to_bytes(1,"big") - MSGID = msgid.to_bytes(4,"big") - ENDFLAG = endflag.to_bytes(1,"big") - DATA_LEN = (len(data)).to_bytes(4,"big") - heards_infor = b'' - heards_infor = MAGIC+VERSION+TYPE+MSGID+ENDFLAG+DATA_LEN+data - return heards_infor - -#AI翻译格式 -def protocol_trans_playload_data(data,msgid,endflag): - MAGIC = (0xfffe).to_bytes(2,"big") - VERSION = (0x01).to_bytes(1,"big")#"03".encode('utf-8') - TYPE = (0x0A).to_bytes(1,"big") - MSGID = msgid.to_bytes(4,"big") - ENDFLAG = endflag.to_bytes(1,"big") - DATA_LEN = (len(data)).to_bytes(4,"big") - heards_infor = b'' - heards_infor = MAGIC+VERSION+TYPE+MSGID+ENDFLAG+DATA_LEN+data - return heards_infor -#sos紧急电话class -class sosInfoCallManager(Abstract): - def __init__(self): - self.num = 0 - self.num=0 - self.num_len = 0 - self.sos_list = [] - self.sos_flag = 0 - self.sos_timer1 = osTimer() - self.sos_timer2 = osTimer() - self.save_list = [] - self.call_state = 0 - self.sos_config = {} - self.phone110112_flag = 0 - self.phone110112 = "" - if EventMesh.publish("get_storage_config","sos_list") != None: - self.sos_list = EventMesh.publish("get_storage_config","sos_list") - if EventMesh.publish("get_storage_config","sos_config") != None: - self.sos_config= EventMesh.publish("get_storage_config","sos_config") - - # self.sos_list=[{"index":0,"phone":"15521469561","name":"dengxueying0"}, {"name":"dengxueying1","index":0,"phone":"15692025051"},{"name":"dengxueying2","index":0,"phone":"15521469561"}] - EventMesh.subscribe("call_out_cb", self.call_out_cb) - EventMesh.subscribe("get_sos_flag", self.get_sos_flag) - - def post_processor_after_instantiation(self): - self.num = 0 - self.sos_flag = 0 - voiceCall.setCallback(self.voice_callback) - EventMesh.subscribe("set_sos_info_call", self.set_sos_info_call) - EventMesh.subscribe("set_sos_list_info",self.set_sos_list_info) - EventMesh.subscribe("stop_sos_call",self.stop_sos_call) - EventMesh.subscribe("set_sos_config",self.set_sos_config) - def set_sos_config(self,event,msg): - # sos_config:{'enable110': '1', 'pressDuration': 3, 'enableSos': '0'} - print("set_sos_config:{}".format(msg)) - self.sos_config = msg - # self.start_phone110112() - pass - def get_sos_flag(self,event,msg): - if self.call_state != 0: - return 1 - else: - return 0 - # import EventMesh - # EventMesh.publish("set_sos_info_call") - def close_call_timer(self): - self.sos_timer2.stop() - pass - def stop_sos_call(self,event,msg): #停止sos拨打 - EventMesh.publish("publish_AI_btnInfo",{"func":self.stop_sos_call_cb}) - def stop_sos_call_cb(self): - if self.call_state == 0: - return - self.phone110112_flag=0 - self.sos_flag = 0 - self.call_state = 0 - self.num=0 #不管有没有拨打sos都清除 - self.num_len = 0 - self.sos_timer2.stop() - self.sos_timer1.stop() - voiceCall.callEnd() #挂断 - - def start_phone110112(self): - print("start_phone110112:{} self.sos_config:{}".format(self.call_state,self.sos_config)) - if self.sos_config != {}: - if "enableSos" in self.sos_config: - enableSos = self.sos_config["enableSos"] - if enableSos == "1": - if "enable110" in self.sos_config: - p110 = self.sos_config["enable110"] - if p110 == "0": - self.phone110112 = "110" - tts_str = "正在呼叫{}".format(self.phone110112) - EventMesh.publish("stop_voice_record") #电话优先级最高 - EventMesh.publish("stop_AI_record") - EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 - self.sos_timer1.stop() - self.sos_timer1.start(2000, 0, self.phone110112_cb) - elif p110 == "1": - self.phone110112 = "112" - tts_str = "正在呼叫{}".format(self.phone110112) - EventMesh.publish("stop_voice_record") #电话优先级最高 - EventMesh.publish("stop_AI_record") - EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 - self.sos_timer1.stop() - self.sos_timer1.start(2000, 0, self.phone110112_cb) - - else: - return 1 - - def sos_call_out_phone(self, msg):#电话拨出去 - if self.call_state == 0: - ret = voiceCall.callStart(self.sos_phone_num) - if ret == -1: - - tts_str = "请确认卡支持语音拨号及余额充足" - EventMesh.publish("stop_voice_record") #电话优先级最高 - EventMesh.publish("stop_AI_record") - EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 - self.call_state = 0 - self.num=0 #不管有没有拨打sos都清除 - self.num_len = 0 - self.sos_flag = 0 - self.phone110112_flag=0 - return -1 - else: - EventMesh.publish(constants.EVENT.START_LOCATION,WEB_CMD_ENUM.POST_SOS_S_CMD) #开始AL定位 - self.sos_timer2.stop() - self.sos_timer2.start(30000, 0, self.sos_time_out_phone) - return 1 - - def phone110112_cb(self,*argc): - if self.call_state == 0: - ret = voiceCall.callStart(self.phone110112) - if ret != -1: - self.phone110112_flag=1 - else: - self.phone110112_flag=0 - # print("ret:{} {}".format(ret,self.phone110112)) - self.phone110112 = "" - - def set_sos_list_info(self,event,msg): - self.sos_list = msg - - - def call_out_cb(self,event,phonenumber): - - if self.call_state == 0: - self.sos_phone_num = phonenumber - EventMesh.publish("stop_voice_record") #电话优先级最高 - EventMesh.publish("stop_AI_record") - - tts_str = "正在呼叫号码{}".format(self.sos_phone_num) - EventMesh.publish("stop_voice_record") #电话优先级最高 - EventMesh.publish("stop_AI_record") - EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 - self.sos_timer1.stop() - self.sos_timer1.start(2000, 0, self.sos_call_out_phone) - - - - def sos_time_out_phone(self, msg):#30s超时未打通或者信号不好则重新拨打 - - if self.sos_flag == 1: - if self.call_state == 0: - voiceCall.callEnd() #挂断 - EventMesh.publish("set_sos_info_call") #电话优先级最高 - pass #再次拨打 - - def set_sos_info_call(self,event,msg): - if EventMesh.publish("get_yuyinaudio_flag") == 1 or EventMesh.publish("get_voice_record_mode") != 0: #语言留言模式不允许拨打电话 - self.call_state = 0 - self.num=0 #不管有没有拨打sos都清除 - self.num_len = 0 - self.sos_flag = 0 - self.phone110112_flag=0 - return - if sim.getStatus() != 1:#网络正常 - tts_str = "无SIM卡" - EventMesh.publish("stop_voice_record") #电话优先级最高 - EventMesh.publish("stop_AI_record") - EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 - self.call_state = 0 - self.num=0 #不管有没有拨打sos都清除 - self.num_len = 0 - self.sos_flag = 0 - self.phone110112_flag=0 - return - if self.call_state != 0: - return - if len(self.sos_list) > 0: - self.call_state = 0 - self.call_sos_cb(1) - else: - tts_str = "未设置号码" - EventMesh.publish("stop_voice_record") #电话优先级最高 - EventMesh.publish("stop_AI_record") - EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 - self.call_state = 0 - self.num=0 #不管有没有拨打sos都清除 - self.num_len = 0 - self.sos_flag = 0 - self.phone110112_flag=0 - - def call_sos_cb(self,*argc): - self.phone110112_flag=0 - if self.sos_flag == 0: - self.sos_flag = 1 - sos_flag = self.sos_flag - EventMesh.debug_log('voicecall sos_flag',sos_flag) - if sos_flag == 0 : - self.num = 0 - else: - if len(self.sos_list) > 0: - # for list_t in self.sos_list: - # tmp = list_t["phone"] - # self.save_list.append(tmp) - - self.num +=1 - if len(self.sos_list) == 1: - self.num_len = len(self.sos_list)*3#三次 1 - elif len(self.sos_list) == 2: - self.num_len = len(self.sos_list)*3 #1+2+1+2 - elif len(self.sos_list) == 3: - self.num_len = len(self.sos_list)*3 #1+3+5 - # EventMesh.debug_log("number:{} number_len :{} num:{} num_len:{}".format(number,len(number),num,num_len)) - if self.num > self.num_len : - - self.start_phone110112() - EventMesh.debug_log("call phone!!!!!!!") - self.sos_flag = 0 - self.num = 0 - self.num_len = 0 - else: - self.phone110112_flag=0 - EventMesh.debug_log("self.num:{} self.num_len :{}".format(self.num,self.num_len)) - - if len(self.sos_list) == 1: - if len(self.sos_list[0]["phone"]) != 0: - EventMesh.publish("call_out_cb", self.sos_list[0]["phone"]) - # EventMesh.publish("set_callnumer_cb",self.sos_list[0]["pnone"]) - EventMesh.debug_log("phone 1 {}".format(self.sos_list[0]["phone"])) - elif len(self.sos_list) == 2: - # 1 2 - # 3 4 - # 5 6 - if self.num == 1 or self.num == 3 or self.num == 5: #1 2 3 4 5 - if len(self.sos_list[0]) != 0: - EventMesh.debug_log("phone 2 {}".format(self.sos_list[0]["phone"])) - EventMesh.publish("call_out_cb", self.sos_list[0]["phone"]) - # EventMesh.publish("set_callnumer_cb",self.sos_list[1]["pnone"]) - elif self.num == 2 or self.num == 4 or self.num == 6: - if len(self.sos_list[1]) != 0: - EventMesh.debug_log("phone 3 {}".format(self.sos_list[1]["phone"])) - EventMesh.publish("call_out_cb", self.sos_list[1]["phone"]) - # EventMesh.publish("set_callnumer_cb",self.sos_list[0]["pnone"]) - elif len(self.sos_list) == 3: - # 1 2 3 - # 4 5 6 - # 7 8 9 - if self.num == 1 or self.num == 4 or self.num == 7: - if len(self.sos_list[0]) != 0: - EventMesh.publish("call_out_cb", self.sos_list[0]["phone"]) - # EventMesh.publish("set_callnumer_cb",self.sos_list[0]["phone"]) - EventMesh.debug_log("phone 4 {}".format(self.sos_list[0]["phone"])) - elif self.num == 2 or self.num == 5 or self.num == 8: - if len(self.sos_list[1]) != 0: - EventMesh.debug_log("phone 5 {}".format(self.sos_list[1]["phone"])) - EventMesh.publish("call_out_cb", self.sos_list[1]["phone"]) - # EventMesh.publish("set_callnumer_cb",self.sos_list[2]["phone"]) - elif self.num == 3 or self.num == 6 or self.num == 9: - if len(self.sos_list[2]) != 0: - EventMesh.debug_log("phone 6 {}".format(self.sos_list[2]["phone"])) - EventMesh.publish("call_out_cb", self.sos_list[2]["phone"]) - # EventMesh.publish("set_callnumer_cb",self.sos_list[0]["phone"]) - else: #清除表示 - tts_str = "未设置号码" - EventMesh.publish("stop_voice_record") #电话优先级最高 - EventMesh.publish("stop_AI_record") - EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 - self.sos_flag = 0 - self.num = 0 - self.call_state = 0 - self.num=0 #不管有没有拨打sos都清除 - self.num_len = 0 - self.phone110112_flag=0 - #未设置号码 - - - def voice_callback(self,args): - EventMesh.debug_log('111test voicecall iinfo:{}'.format(args)) - if args[0] == 10: #电话拨进来 - self.sos_flag = 0 - self.call_state = 1 - self.phone110112_flag=0 - elif args[0] == 11: #已接听 - self.call_state = 2 - self.num=0 #不管有没有拨打sos都清除 - self.num_len = 0 - self.sos_flag = 0 - self.phone110112_flag=0 - self.close_call_timer() - - EventMesh.debug_log('voicecall connected, PhoneNO.: ', args[6]) - - elif args[0] == 12: #电话挂断 - self.phone110112_flag=0 - if self.call_state == 2 or self.call_state == 1: #电话拨出去没有接听 就挂断 - pass - else: - - EventMesh.debug_log('voicecall disconnect end !!!') - self.close_call_timer() - self.call_state = 0 - if self.sos_flag == 1: - EventMesh.publish("set_sos_info_call") - pass - self.call_state = 0 - #再次拨打下一个电话号码 - EventMesh.debug_log('voicecall disconnect end') - - - elif args[0] == 13: # - self.call_state = 7 - EventMesh.debug_log('voicecall is waiting, PhoneNO.: ', args[6]) - elif args[0] == 14: #电话拨出去 - EventMesh.debug_log('voicecall dialing, PhoneNO.: ', args[6]) - if self.sos_flag == 1: - self.call_state = 3 #当前是拨打sos号码标识 - else: - self.call_state = 4 - - elif args[0] == 15: - self.call_state = 5 - EventMesh.debug_log('voicecall alerting, PhoneNO.: ', args[6]) - - - elif args[0] == 16: - self.call_state = 6 - EventMesh.debug_log('voicecall holding, PhoneNO.: ', args[6]) - - - - -try: - import usr.ai_alarm as _schedule -except Exception as e: - usys.print_exception(e) - import ai_alarm as _schedule -DEFIEN_HR_REPORT_TIME = 5*60 -DEFINE_TIME_COUNT = 10 -DEFIEN_LOCATION_REPORT_TIME = DEFINE_TIME_COUNT -WEB_CONNECT_MAX_COUNT = 10 #默认重连次数 -class SockMangager(Abstract): - def __init__(self): - self.thread_buf_size = 16*1024 - self.iot_web_send_queue = Queue(100) - self.iot_web_protoclo_recv_id=0 - self.iot_protocol_client = None - self.connect_web_timer = osTimer() - self.connect_web_timer_count = 0 - self.hr_report_timer = osTimer() - self.resolver = None - self.timerHr = 0 - self.timerLocation = 0 - self.send_device_time = 0 - self.vbat_buf = [] - self.network_state = 0 - self.curr_battery_level = 0 - self.iot_web_send_queue_id = 0 - self.bat_count = 0 - self.minute_now_old = 0 - self.old_minute = 0 - self.is_alarm = 0 - self.reconnect_count = 0 #连续重连次数 默认十次 不过就不重连了 等下一个心跳重连 - self.schedule = _schedule.schedule() - self._1S_timer = osTimer() - self._5S_timer = osTimer() - self.timer_id = 0 - self.start_voice_flag = 0 - self.init_send_default=0 - self.play_new_voice_state = 0 - self.stop_play = 0 - self.get_voice_new_msginit = 0 - self.voice_id = "0" - self.bt_connect_list = [] - self.new_voice_list = [] - self.location_info= {} - self.locate_duration = 0 - self.locate_duration_state = 0 - self.location_info = EventMesh.publish("get_storage_config","locate_duration_time") #获取定位频率信息 - - self.temp = 0 - if self.location_info == None: - self.location_info = {} - else: - # location_time["duration"]=locate_duration_time - # location_time["state"]=locate_duration_state - if "duration" in self.location_info: - self.locate_duration = self.location_info["duration"] #定位频率 - if "state" in self.location_info: - self.locate_duration_state = self.location_info["state"] #是否开启频率定位 - pass - EventMesh.subscribe("REPORT_ALARM_TO_IOT",self.report_alarm_to_iot) - EventMesh.subscribe("REPORT_SCHEDULE_TO_IOT",self.report_schedule_to_iot) - - def report_alarm_to_iot(self,event,msg): - EventMesh.publish(WEB_CMD_ENUM.POST_ALARM_S_CMD,msg) - pass - def report_schedule_to_iot(self,event,msg): - EventMesh.publish(WEB_CMD_ENUM.POST_SCHEDULE_S_CMD,msg) - pass - def usb_event_cb(self, state): - status = state - def post_processor_after_instantiation(self): - self.minute_now_old=0 - EventMesh.subscribe("set_curr_battery_level",self.set_curr_battery_level) - EventMesh.subscribe("get_curr_battery_level",self.get_curr_battery_level) - EventMesh.subscribe("screen_get_battery",self.get_battery) - EventMesh.subscribe("CHANNEL_IOT_WEB_SEND",self.user_send) - EventMesh.subscribe("set_temp",self.set_temp) - EventMesh.subscribe("GET_IOT_PROTOCOL_CILENT",self.get_iot_protoclo_client) - EventMesh.subscribe("SEND_JINMOAUDIO_TO_IOT",self.send_jm_audio_to_iot) - EventMesh.subscribe("SEND_YUYINAUDIO_TO_IOT",self.send_yuyin_audio_to_iot) - EventMesh.subscribe("SEND_MEETAUDIO_TO_IOT",self.send_meet_audio_to_iot) - EventMesh.subscribe("SEND_TRANSAUDIO_TO_IOT",self.send_trans_audio_to_iot) - EventMesh.subscribe("open_websocket_timer",self.open_websocket_timer) - EventMesh.subscribe("close_websocket_timer",self.close_websocket_timer) - EventMesh.subscribe("connect_iot_websocket_cb",self.connect_iot_websocket_cb) - EventMesh.subscribe("set_play_new_voice_list",self.set_play_new_voice_list) - EventMesh.subscribe("get_play_new_voice_list",self.get_play_new_voice_list) - - EventMesh.subscribe("send_meet_open_flag_to_iot",self.send_meet_open_flag_to_iot)#会议创建 - EventMesh.subscribe("send_meet_end_flag_to_iot",self.send_meet_end_flag_to_iot)#会议结束 - - EventMesh.subscribe("send_trans_come_flag_to_iot",self.send_trans_come_flag_to_iot)#进入翻译 - EventMesh.subscribe("send_trans_ok_flag_to_iot",self.send_trans_ok_flag_to_iot)#向APP发送进入翻译成功 - EventMesh.subscribe("send_trans_end_flag_to_iot",self.send_trans_end_flag_to_iot)#退出翻译 - # import EventMesh - # EventMesh.publish("play_new_voice_list") - EventMesh.subscribe("play_new_voice_list",self.play_new_voice_list) #播放语音未读信息 - EventMesh.subscribe("update_play_new_voice_info",self.update_play_new_voice_info) #更新语音未读信息状态上报IOT - EventMesh.subscribe("send_bt_scan_list",self.send_bt_scan_list) #上报扫描列表 - EventMesh.subscribe("save_bt_connect_list",self.save_bt_connect_list) #保存耳机连接状态列表 - EventMesh.subscribe("send_bt_connect_list",self.send_bt_connect_list) #发送耳机连接状态列表 - EventMesh.subscribe("set_play_new_voice_state",self.set_play_new_voice_state) #设置播放语音未读状态1正在播放语音未读消息 - EventMesh.subscribe("get_play_new_voice_state",self.get_play_new_voice_state) #获取播放语音未读状态1正在播放语音未读消息 - EventMesh.subscribe("set_stop_play",self.set_stop_play) #外部控制url音频播放列表暂停 - EventMesh.subscribe("get_stop_play",self.get_stop_play) #获取外部控制url音频播放列表暂停情况 - EventMesh.subscribe("send_bt_connect_state_to_iot",self.send_bt_connect_state_to_iot) #发耳机;链接状态->IOT - EventMesh.subscribe("send_unbind_dev_to_iot",self.send_unbind_dev_to_iot) #发送解绑指令到IOT平台 - EventMesh.subscribe("set_location_info",self.set_location_info) #设置定位信息 - EventMesh.subscribe("post_device_state",self.post_device_state) #上报设备状态信息-》IOT - EventMesh.subscribe("send_power_init_data",self.send_power_init_data) #上电初始化上报数据集合 - # count = 0 - # while True: - # count += 1 - # if count > 20 : - # break - # data = dataCall.getInfo(1, 0) - # print("data call INFO {}".format(data)) - # if data[2][0]: - # self.network_state = 1 - # break - # print("web等待网络 {}...".format(count)) - - # utime.sleep_ms(500) - self.timerHr = 0 - self.reconnect_count = 0 - self.voice_id = "0" - self.deviceReadState = "" - self.get_voice_new_msginit = 0 - self.bt_connect_list = [] - self.new_voice_list = [] - self.connect_web_timer.start(2000,0,self.iot_web_connect_timerCb) #开机2s之后连接iot - def connect_iot_websocket(self): - if EventMesh.publish("get_voice_record_flag") == 1: - EventMesh.publish("stop_voice_record") - print("connect_iot_websocket !!!!") - self.iot_close_web_therad() - - if sim.getStatus() != 1 or self.start_voice_flag == 1:#网络正常 - return - - if self.reconnect_count >= WEB_CONNECT_MAX_COUNT: #重连次数大于10次不在进重连只有下发心跳才重新连接 - return - token = EventMesh.publish("get_dev_token") - - print("token:{}".format(token)) - # token = "957119d6b67b89dc1bc533d2e0c058d559" - # token = "951b74498eabe241e8a8c644a4f07531ef" - - try: - if token != None: - self.connect_web_timer_count = 0 - url_t = "{}?bz-dt={}".format(constants.IOT_WS_URL, token) - try: - self.iot_protocol_client=uwebsocket.Client.connect(url_t, debug=True) - self.reconnect_count=0 - except Exception as e: - print("再次连接,iot连接报错是") - print(e) - self.reconnect_count+=1 - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) #self.connect_iot_websocket() - return - - if self.iot_protocol_client != None: - self.reconnect_count = 0 - _thread.stack_size(self.thread_buf_size+8*1024) #_thread.stack_size(16*1024) - self.iot_web_send_queue_id = _thread.start_new_thread(self.websocket_send_queue_thread, ()) - _thread.stack_size(8*1024) - - _thread.stack_size(self.thread_buf_size+8*1024) - self.iot_web_protoclo_recv_id = _thread.start_new_thread(self.websocket_recv_queue_thread, ()) - _thread.stack_size(8*1024) - - else: - print("self.iot_protocol_client init!!!!!!!!!") - self.reconnect_count+=1 - # EventMesh.publish("connect_iot_websocket_cb") - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) #self.connect_iot_websocket() - return - - if self.iot_protocol_client != None: - self.start_voice_flag = 1 - else: - self.start_voice_flag = 0 - - print("self.iot_protocol_client %d self.start_voice_flag:%d",self.iot_protocol_client,self.start_voice_flag) - if self.iot_protocol_client != None: - if self.timer_id == 0: - # self.hr_report_timer.start(DEFINE_TIME_COUNT*1000, 1, self.public_report_event) - self.timer_id=1 - else: - print("没有token!!!!!!!!!!") - self.connect_web_timer_count+=1 - if self.connect_web_timer_count <= 10: - self.connect_web_timer.start(5000,0,self.iot_web_connect_timerCb) #没有token 5s之后再重连 - pass - except Exception as e: - self.iot_protocol_client = None - print("connect_iot_websocket error") - - def post_device_state(self,event,msg): #上报设备状态 - device_state=EventMesh.publish("GET_IOT_WEB_SEND_CMD",WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD) - print("device_state{}".format(device_state)) - EventMesh.publish(WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD,device_state) #上报设备状态 - def set_location_info(self,event,msg): - if "duration" in msg: - self.locate_duration=msg["duration"] - if "state" in msg: - self.locate_duration_state=msg["state"] - - - def save_bt_connect_list(self,event,msg): - print("msg:{}".format(msg)) - # msg = {"event":"bt_connect_single","data":{"name":"yeiwyei","mac":"89:89:78:78:98:89","state":"connected"}} - try: - data = msg["data"] - if "None" in data: #开机没有连接过蓝牙耳机就为kong - return - if isinstance(data,dict) == True: - mac = data["mac"] - state = data["state"] - name = data["name"] - need_save = 0 - for i,_alarm in enumerate(self.bt_connect_list): - mac_tmp = _alarm["mac"] - if mac_tmp == mac: - need_save = 1 - self.bt_connect_list[i]["state"] = state - self.bt_connect_list[i]["name"] = name - break - self.bt_connect_list[i]["state"] ="disconnected" - - if need_save == 0: #列表里面没有才需要保存 - self.bt_connect_list.append(data) - print("self.bt_connect_list:{}".format(self.bt_connect_list)) - except Exception as e: - print("json数据不完整:self.recv_bt_connect_record_list") - usys.print_exception(e) - pass - def send_bt_connect_list(self,event,msg): - - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - bt_connect_record_list = { - "cmd":"bt_connect_record_list", - "ts":time_tmp, - "msg":"上报蓝牙连接列表", - "data":self.bt_connect_list - - } - print("bt_connect_record_list:{}".format(bt_connect_record_list)) - EventMesh.publish(WEB_CMD_ENUM.BT_CONNECT_RECORD_LIST_S,bt_connect_record_list) - pass - - def set_play_new_voice_list(self,event,msg): - self.new_voice_list = msg - pass - - def get_play_new_voice_list(self,event,msg): - return self.new_voice_list - - def set_play_new_voice_state(self,event,msg): - self.play_new_voice_state = msg - pass - - def get_play_new_voice_state(self,event,msg): - return self.play_new_voice_state - - def get_stop_play(self,event,msg): - return self.stop_play - - def set_stop_play(self,event,msg): - self.stop_play = msg - pass - - def _1S_timer_cb(self,*argc): - EventMesh.publish("publish_AI_btnInfo",{"func":self._1S_timer_cb_}) - - def _1S_timer_cb_(self): - #self.iot_web_send_queue.put("ping") - get_VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag") - if get_VC_satrt_flag == None: - get_VC_satrt_flag = 0 - if get_VC_satrt_flag == 0: - self.play_new_voice_list_timer_cb() - self._1S_timer.stop() - - def play_new_voice_list(self,event,msg): - if len(self.new_voice_list) != 0: - self._1S_timer.start(1000, 1, self._1S_timer_cb) - - def play_new_voice_list_timer_cb(self): - print("play_new_voice_list:{}".format(len(self.new_voice_list))) - voice_list = self.new_voice_list - if len(self.new_voice_list) != 0: - - for i,data in enumerate(self.new_voice_list): - if EventMesh.publish("get_history_voice_flag") == 1: - data = voice_list[len(self.new_voice_list)-1-i] - print("data:{}".format(data)) - self.voice_index = i - self.voice_id = str(data["id"]) - url = str(data["url"]) - # url = "http://qipaiai-oss1.tos-cn-beijing.volces.com/qi_pai_ringtone/2025_08_11/742659759219544064test.mp3" - self.play_new_voice_state = 1 - if self.stop_play == 1: - print("结束播放") - self.stop_play = 0 - break - if EventMesh.publish("get_storage_config","is_noread_voice") == 1:#还有未读的(开机) - if EventMesh.publish("get_open_device") == 1:#刚开机 - EventMesh.publish("play_wait_voice",[url,1]) - EventMesh.publish("set_open_device",0) - if EventMesh.publish("get_history_voice_flag") == 1:#播放历史留言 or 播放当前留言 - if EventMesh.publish("get_wait_voice_flag") == 1:#等待留言 - EventMesh.publish("play_wait_voice",[url,1]) - else: - time = str(data["createTimeStr"]) - EventMesh.publish("play_his_voice",[url,1,time]) - if len(EventMesh.publish("get_play_new_voice_list")) == 0:#列表为空才清零 - EventMesh.publish("set_history_voice_flag",0)#修改标志位 - else: - EventMesh.publish("play_new_voice",[url,1]) - break - pass - def update_play_new_voice_info(self,event,msg): #更新播放语音留言信息 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - put_voice_msg_state = { - "cmd":"put_voice_msg_state", - "ts":time_tmp, - "msg":"修改语音消息阅读状态", - "data":self.voice_id - } - if len(self.new_voice_list) != 0: - self.new_voice_list.pop(self.voice_index) - EventMesh.publish(WEB_CMD_ENUM.PUT_VOICE_MSG_STATE_S,put_voice_msg_state) - pass - - def send_bt_scan_list(self,event,msg): - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - try: - # print("type:{}".format(msg)) - data = msg["data"] - bt_scan_rest ={ - "cmd":"bt_scan_rest", - "ts":time_tmp, - "msg":"上报扫描列表", - "data":data - } - EventMesh.publish(WEB_CMD_ENUM.BT_SCAN_REST_S_CMD,bt_scan_rest) - except Exception as e: - usys.print_exception(e) - pass - def send_unbind_dev_to_iot(self,event,msg): - self._5S_timer.start(5000,0,self._5S_timer_cb) - def _5S_timer_cb(self,*argc): - EventMesh.publish("publish_AI_btnInfo",{"func":self.send_unbind_dev_to_iot_cb}) - pass - def send_unbind_dev_to_iot_cb(self): - EventMesh.publish(WEB_CMD_ENUM.UNBIND_DEV_S) - pass - def send_bt_connect_state_to_iot(self,event,msg): - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - - try: - # { - # "cmd":"post_bt_connect_rest", - # "ts":1738283829, - # "msg":"上报蓝牙连接结果", - # "data":{ - # "mac":"mac地址", - # "state":"connected/disconnected/connecting/failed" - # } - # } - event = msg["event"] - mac = msg["data"]["mac"] - state = msg["data"]["state"] - json_data = \ - { - - "cmd": event, - "ts":time_tmp, - "msg":"上报蓝牙连接结果", - "data":{ - "mac":mac, - "state":state - } - - } - EventMesh.publish(WEB_CMD_ENUM.POST_BT_CONNECT_REST_S,json_data) - except Exception as e: - usys.print_exception(e) - pass - - def send_meet_open_flag_to_iot(self,event,msg): #创建会议纪要上报到IOT平台 - time_tmp = utime.mktime(date_tmp) - meet_open = { - "msg": "开始会议", - "data": None, - "cmd": "meet_begin", - "ts": time_tmp - } - send_data = protocol_send_total_data(meet_open) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - pass - - def send_meet_end_flag_to_iot(self,event,msg): #结束会议纪要上报到IOT平台 - time_tmp = utime.mktime(date_tmp) - meet_end = { - "msg": "结束会议", - "data": None, - "cmd": "meet_end", - "ts": time_tmp - } - send_data = protocol_send_total_data(meet_end) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - pass - - def send_trans_come_flag_to_iot(self,event,msg): #进入翻译上报到IOT平台 - time_tmp = utime.mktime(date_tmp) - trans_open = { - "msg": "进入翻译模式", - "data": None, - "cmd": "translate_device_enter", - "ts": time_tmp - } - send_data = protocol_send_total_data(trans_open) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - pass - - def send_trans_ok_flag_to_iot(self,event,msg): #成功进入翻译发给APP - time_tmp = utime.mktime(date_tmp) - trans_ok = { - "msg": "进入成功", - "data": None, - "cmd": "translate_device_complete", - "ts": time_tmp - } - send_data = protocol_send_total_data(trans_ok) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - pass - - def send_trans_end_flag_to_iot(self,event,msg): #退出翻译上报到IOT平台 - time_tmp = utime.mktime(date_tmp) - trans_open = { - "msg": "退出翻译", - "data": None, - "cmd": "translate_device_exit", - "ts": time_tmp - } - send_data = protocol_send_total_data(trans_open) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - pass - - def send_jm_audio_to_iot(self,event,msg): #静默录音音频数据上报到IOT平台 - send_data = b'' - send_data = protocol_audio_ji_playload_data(msg[0],msg[1],msg[2]) - # print("jm:{}".format(msg[2])) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - pass - - def send_yuyin_audio_to_iot(self,event,msg): #上报语音留言音频数据到IOT平台 - send_data = b'' - send_data = protocol_yuyin_voice_playload_data(msg[0],msg[1],msg[2]) - # print("yuyin:{}".format(msg[2])) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - pass - - def send_meet_audio_to_iot(self,event,msg): #上报会议纪要音频数据到IOT平台 - send_data = b'' - send_data = protocol_meet_playload_data(msg[0],msg[1],msg[2]) - # print("yuyin:{}".format(msg[2])) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - pass - - def send_trans_audio_to_iot(self,event,msg): #上报AI翻译音频数据到IOT平台 - send_data = b'' - send_data = protocol_trans_playload_data(msg[0],msg[1],msg[2]) - # print("yuyin:{}".format(msg[2])) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - pass - - - #获取电池电量 - def get_battery(self, event=None, msg=None): - """ - 获取电池事件,暂时没支持 - :param event: - :param msg: - :return: - """ - - charge_state = '0' - usb_state = EventMesh.publish("GET_USB_STATE") - if usb_state == None: - usb_state = 0 - - if usb_state == 1: - charge_state = '1' - else: - charge_state = '0' - - #temp = Power.getVbatt() - temp = EventMesh.publish("get_curr_battery_level") - vbat = EventMesh.publish("get_battery",temp) - if vbat == None: - vbat = 100 - # EventMesh.debug_log("vbat",vbat) - # if temp != None : - # if temp <= 3505: - # vbat = 0 - # elif temp <= 3699: - # vbat = 20 #"E:/vbat_20.jpg" - # elif temp <= 3772: - # vbat = 40 #"E:/vbat_40.jpg" - # elif temp <= 3882: - # vbat = 60 #"E:/vbat_60.jpg" - # elif temp <= 4070: - # vbat = 80 #"E:/vbat_80.jpg" - # elif temp <= 4200: - # vbat = 90 # - # else: - # vbat = 100 #"E:/vbat_100.jpg" - # else: - # vbat = 100 - - result = [charge_state, vbat] - - return result - def set_curr_battery_level(self, event=None, msg=None): - self.curr_battery_level = msg - - def get_curr_battery_level(self, event=None, msg=None): - return self.curr_battery_level - - def initialization(self, *args): - pass - #EventMesh.debug_log("ProtoCol:{}".format(self.TYPE + "_SEND")) - def connect_iot_websocket_cb(self,event,msg): - self.connect_iot_websocket() - def add_support_resolver(self, resolver): - self.resolver = resolver - def public_report_event(self, *args, **kwargs): - EventMesh.publish("publish_AI_btnInfo",{"func":self.public_report_event_cb}) #self.connect_iot_websocket() - def send_power_init_data(self,event=None,msg=None): - self.public_report_event_cb() - #上报心跳 上报设备状态 检查闹钟 日程 - def public_report_event_cb(self): - """电池平均值""" - temp = Power.getVbatt() - if self.bat_count < 10: - self.vbat_buf.append(temp) - self.bat_count += 1 - else: - for i in range(9): - self.vbat_buf[i] = self.vbat_buf[i+1] - self.vbat_buf[9] = temp - curr_count = len(self.vbat_buf) - bat_total_value = 0 - for i in range(curr_count): - bat_total_value += self.vbat_buf[i] - bat_total_value = bat_total_value//curr_count#一分钟更新一次电量 10个平均值 - # EventMesh.publish("AI_schedule_open")#读取闹钟数组 - # print("AI_schedule_open!!!") - if EventMesh.publish("get_sos_flag") != None: - if EventMesh.publish("get_sos_flag") == 1: - print("get_sos_flag!!!!!!") - EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 - print("get_sos_flag1!!!!!!") - - EventMesh.publish("AI_alarm_start") #检查是否有闹钟需要提醒 - EventMesh.publish("AI_schedule_open")#检查日程是否需要提醒 - # self.alarm_test() - EventMesh.publish("set_curr_battery_level",bat_total_value) - # EventMesh.publish("publish_AI_btnInfo",{"func":self.hr_report_publish_event,"data":0}) - self.timerHr+=DEFINE_TIME_COUNT - if self.timerHr >= DEFIEN_HR_REPORT_TIME: - self.timerHr= 0 - self.reconnect_count = 0 - self.connect_web_timer_count=0 - EventMesh.publish("publish_AI_btnInfo",{"func":self.hr_report_publish_event,"data":0}) - # print("self.locate_duration_state:{} self.locate_duration:{}".format(self.locate_duration_state,self.locate_duration)) - if self.locate_duration_state == 1: - if self.locate_duration == None: - self.timerLocation = 0 - else: - if self.locate_duration != 0: - self.timerLocation+=DEFINE_TIME_COUNT - if self.timerLocation >= self.locate_duration: - self.timerLocation= 0 - EventMesh.publish(constants.EVENT.START_LOCATION,WEB_CMD_ENUM.POST_LOCATION_S_CMD) #定位上报 - else: - self.timerLocation= 0 - else: - self.timerLocation= 0 - - - if self.send_device_time < DEFIEN_LOCATION_REPORT_TIME : #上电初始化上报一次设备状态信息 - self.send_device_time+=DEFINE_TIME_COUNT - else: - if self.get_voice_new_msginit == 0: - self.get_voice_new_msginit=1 - EventMesh.publish("publish_AI_btnInfo",{"func":self.hr_report_publish_event,"data":0}) - device_state=EventMesh.publish("GET_IOT_WEB_SEND_CMD",WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD) - print("device_state{}".format(device_state)) - EventMesh.publish(WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD,device_state) #上报设备状态 - # if dev_type == "7PIN_1_5":#二代开机获取留言 - # EventMesh.publish("set_open_device",1) - # EventMesh.publish("set_history_voice_flag",1) - # EventMesh.publish("set_wait_voice_flag",1) - # EventMesh.publish(WEB_CMD_ENUM.GET_NEW_VOICE_MSG_S)#向IOT获取语音未读消息 - - - # http://220.160.104.34:30800/ws-bus/test/voice-record/876226211472149?state=1, - # http://220.160.104.34:30800/ws-bus/test/860132066107046?testType=alarmList - def hr_report_publish_event(self,data): - - HR_DATA = EventMesh.publish("GET_IOT_WEB_SEND_CMD",WEB_CMD_ENUM.HR_S_CMD) - print("HR_DATA{}".format(HR_DATA)) - EventMesh.publish(WEB_CMD_ENUM.HR_S_CMD,HR_DATA) - pass - - def iot_web_connect_timerCb(self,*argc): - self.connect_iot_websocket() - - - def open_websocket_timer(self,event,msg): - self.VC_pingping_timerout = 0 - # self._1S_timer.start(500,1,self._1S_timer_cb) - - def close_websocket_timer(self,event,msg): - self._1S_timer.stop() - - - - def websocket_send_queue_thread(self): - while True: - data = self.iot_web_send_queue.get() - - if data != None: - # EventMesh.debug_log("websocket:send data1 len:{}".format(len(data))) - try: - # EventMesh.debug_log("websocket:send data1 len:{} {}".format(len(data),self.iot_protocol_client)) - if self.iot_protocol_client != None: - self.iot_protocol_client.send(data) - self.reconnect_count = 0 - else: - # EventMesh.publish("connect_iot_websocket_cb") - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) - EventMesh.debug_log("websocket:send data2 len:{}".format(len(data),self.iot_protocol_client)) - except Exception as e: - EventMesh.debug_log("websocket:send error") - # EventMesh.publish("connect_iot_websocket_cb") - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) - break - - - def websocket_recv_queue_thread(self): - while True: - # 死循环接收数据 - try: - recv_data = self.iot_protocol_client.recv() - print("recv_data = {}".format(len(recv_data))) - if recv_data == "websocket close": - # 服务器关闭连接或客户端关闭连接 - print("对端 服务器强制关闭 cli close") - # EventMesh.publish("connect_iot_websocket_cb") - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) - break - elif recv_data == "": - print("下发的空数据 不做处理") - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) - break - else: - if len(recv_data) != 0: - self.reconnect_count = 0 - self.resolver.handler(recv_data) - - except Exception as e: - print("cli break") - # EventMesh.publish("connect_iot_websocket_cb") - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) - break - self.iot_web_protoclo_recv_id =0 - - def iot_web_send_queue_close(self): - if self.iot_web_send_queue_id != 0: - if self.iot_web_send_queue.empty()!=True: - self.iot_web_send_queue.clear() - - try: - _thread.stop_thread(self.iot_web_send_queue_id) - except Exception as e: - print("iot_web_send_queue_close !!!!!!!!") - print("iot_web_send_queue_close 1111!!!!!!!!") - self.iot_web_send_queue_id = 0 - - def iot_web_protoclo_recv_close_therad(self): - if self.iot_web_protoclo_recv_id != 0: - try: - _thread.stop_thread(self.iot_web_protoclo_recv_id) - except Exception as e: - print("self.iot_web_protoclo_recv_id !!!!!!!!") - self.iot_web_protoclo_recv_id = 0 - if self.iot_protocol_client != None: - try: - print("关闭websocke") - self.iot_protocol_client.close() - except Exception as e: - print("关闭websocke失败") - self.iot_protocol_client = None - - - - def iot_close_web_therad(self): - try: - self.iot_web_send_queue_close() - except Exception as e: - print("iot_web_send_queue_close error!!!!!!!!") - - try: - self.iot_web_protoclo_recv_close_therad() - except Exception as e: - print("iot_web_protoclo_recv_close_therad error!!!!!!!!") - self.start_voice_flag = 0 - print("iot_close_web_therad") - - def get_iot_protoclo_client(self,event,msg): - return self.iot_protocol_client - def set_temp(self,event,data): - if self.temp == 0: - self.temp = 1 - else: - self.temp = 0 - def user_send(self,event,data): - # EventMesh.debug_log("TCP:send data:{}".format(msg)) - # EventMesh.debug_log("TCP:send data:{}".format(msg)) - # sim_state = sim.getStatus() - if sim.getStatus() != 1:#网络不正常 - print("无 sim卡") - return - # if self.temp == 1: - # print("没信号") - else : - try: - if self.start_voice_flag == 0: #如果关闭了就需要重新再 - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) - return - if data != None: - if len(data) != 0: - self.iot_web_send_queue.put(data) - # EventMesh.debug_log("self.socket_send_queue.put:success:{}".format(len(data))) - except Exception as e: - EventMesh.debug_log("self.socket_send_queue.put:ERROR:") - usys.print_exception(e) - - return - sim_state = sim.getStatus() - if sim_state != 1 or self.network_state == 0: #没sim,则不发送 ,没联网 - return - else : - try: - if self.iot_protocol_client != None: - EventMesh.publish("publish_AI_btnInfo",{"func":self.iot_protocol_send_cb,"data":data}) - else: - print("reconnect1 {}".format(data)) - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) - - except Exception as e: - print("senc data err reconnect{}".format(data)) - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) - - def iot_protocol_send_cb(self,data): - try: - if data != None: - print("websocket connect success:{} self.iot_protocol_client:{}".format(data,self.iot_protocol_client)) - if self.iot_protocol_client != None: - self.iot_protocol_client.send(data) - self.reconnect_count = 0 - print("websocket send success") - else: - print("reconnect2 {}".format(data)) - # EventMesh.publish("connect_iot_websocket_cb") - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) - except Exception as e: - print("发送失败 重新连接") - self.reconnect_count+=1 - # EventMesh.publish("connect_iot_websocket_cb") - EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) - - - - -class NetManager(Abstract): - THRESHOLD = 10 - - def __init__(self): - self.__data_call = dataCall - self.__net = net - self.check_net = checkNet.CheckNetwork("QuecPython_Helios_Framework", "this latest version") - EventMesh.subscribe("get_network_state", self.get_network_state) - self.network_state = 0 - - def post_processor_after_instantiation(self, *args, **kwargs): - count = 0 - while True: - count += 1 - if count > 5 : - return - data = dataCall.getInfo(1, 0) - EventMesh.debug_log("data call INFO {}".format(data)) - if data[2][0]: - self.network_state = 1 - break - utime.sleep(1) - self.__data_call.start(2, 0, "", "", "", 0) - self.__data_call.setCallback(self.__datacall_cb) - - def __datacall_cb(self, args): - pdp = args[0] - nw_sta = args[1] - if nw_sta == 1: - self.network_state = 1 - EventMesh.debug_log("*** network %d connected! ***" % pdp) - - else: - self.network_state = 0 - EventMesh.debug_log("*** network %d not connected! ***" % pdp) - - def get_network_state(self, topic, msg): - return self.network_state - -#http://220.160.104.34:30800/ws-bus/test/test/voice-record/876226211472149?state=0/1,这个是录音开关的。 -#http://220.160.104.34:30800/ws-bus/test/876226211472149?testType=sosList -class ProtoCol(Abstract): - TYPE = "" - HEADER = "#@{}@#;" - SEPARATOR = ";" - END = ";" + chr(0x01) - #PID = "SZKW" - #PID = "qAELPTKmFanR4RHe" - md5_key = '' - PID = '' - btstate = 0 - blestate = 0 - - def __init__(self): - pass - - def initialization(self,*args): - EventMesh.subscribe(self.TYPE+"_S", self.handler) - EventMesh.subscribe(self.TYPE+"_R", self.handler) - pass - - def handler(self, event, msg): - print("handler {}".format(event)) - if event.endswith("_S"): - self.send(event, msg) - else: - self.recv(event, msg) - - def send(self, event, msg): - - pass - - def recv(self, event, msg): - pass -#上报蓝牙连接状态 -class IOTWeb_post_bt_connect_restProtoCol(ProtoCol): - """post_bt_connect_rest""" - TYPE = "post_bt_connect_rest" - - def send(self, event, msg): - # print("send post_bt_connect_rest{}".format(msg)) - send_data = b'' - send_data = protocol_send_total_data(msg) - print("send post_bt_connect_rest{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event: 上报蓝牙耳机连接状态 - @param msg: 接收到的下行数据 - """ - print("recv post_bt_connect_rest{}".format(msg)) -#电子围栏触发SOS警告 -class IOTWeb_fence_sosProtoCol(ProtoCol): - """fence_sos""" - TYPE = "fence_sos" - - def send(self, event, msg): - # print("send fence_sos{}".format(msg)) - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - # 通过websocket发送到服务器端 - fence_sos = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"fence_sos", - "rest":"success", - "msg":"收到紧急呼叫指令" - } - } - send_data = b'' - send_data = protocol_send_total_data(fence_sos) - print("send fence_sos{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event: 电子围栏警告 - @param msg: 接收到的下行数据 - """ - EventMesh.publish(WEB_CMD_ENUM.FENCE_SOS_S) - sos_list = EventMesh.publish("get_storage_config","sos_list") - if sos_list != None: - if len(sos_list) > 0: - get_sos_flag = EventMesh.publish("get_sos_flag") - if get_sos_flag == 0: - EventMesh.publish("set_sos_info_call") - pass - - print("recv fence_sos{}".format(msg)) -#上报心跳 -class IOTWeb_HRProtoCol(ProtoCol): - """post_heart_beat""" - TYPE = "post_heart_beat" - - def send(self, event, msg): - print("send hr{}".format(msg)) - # 通过websocket发送到服务器端 - send_data = b'' - send_data = protocol_send_total_data(msg) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event: 心跳成功 - @param msg: 接收到的下行数据 - """ - # { - # "cmd":"post_rest", - # "ts":1738283829, - # "msg":"消息收到响应", - # "data":{ - # "cmd":"post_heart_beat", - # "rest":"bind/unbind", - # "msg":"上报成功/格式错误" - # } - # } - print("recv hr{}".format(msg)) - if "data" in msg: - data = msg["data"] - rest = data["rest"] - if rest == "unbind": #解绑需要开启蓝牙广播 - self.btstate = 0 - if self.blestate == 0: - EventMesh.publish("set_storage_config",["switch_ble_state","1"]) - EventMesh.publish("send_ble_switch_toble","1") - - EventMesh.publish("set_storage_config",["switch_bt_state","1"]) - EventMesh.publish("send_bt_switch_toble","1") - self.blestate=1 - - elif rest == "bind": #绑定需要关闭蓝牙广播 - self.blestate =0 - if self.btstate == 0: - EventMesh.publish("set_storage_config",["switch_ble_state","0"]) - EventMesh.publish("send_ble_switch_toble","0") - self.btstate=1 - -#定位周期 -class IOTWeb_locatedurationProtoCol(ProtoCol): - """locate_duration""" - TYPE = "locate_duration" - - def send(self, event, msg): - print("send locate_duration{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - locatio = { - "cmd":"dispatch_rest", - "ts":int(time_tmp), - "msg":"消息收到响应", - "data":{ - "cmd":"locate_duration", - "rest":"success", - "msg":"收到定位周期指令" - } - } - send_data = b'' - send_data = protocol_send_total_data(locatio) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - - def recv(self, event, msg): - """ - - @param event:定位周期 - @param msg: 接收到的下行数据 - """ - # locate_now = { - # "cmd":"locate_duration", - # "ts":1738283829, - # "msg":"下发定位周期", - # "data":300,0表示关 - # } - # {"cmd":"locate_duration","msg":"下发定位周期","data":{"duration":300,"state":"1"},"ts":1756089226052,"targetDeviceId":"860132064633266"} - #location_time - location_time = {} - if "data" in msg: - data = msg["data"] - if "duration" in data: - locate_duration_time = int(data["duration"]) - locate_duration_state = int(data["state"]) - location_time["duration"]=locate_duration_time - location_time["state"]=locate_duration_state - - EventMesh.publish("set_storage_config",["locate_duration_time",location_time]) - EventMesh.publish("set_location_info",location_time) - locate_duration_c = EventMesh.publish("get_storage_config","locate_duration_time") - print("locate_duration_c:{}".format(locate_duration_c)) - -#上报SOS告警信息 -class IOTWeb_post_sosProtoCol(ProtoCol): - """post_sos""" - TYPE = "post_sos" - - def send(self, event, msg): - print("send post_sos{}".format(msg)) - # 通过websocket发送到服务器端 - - send_data = b'' - send_data = protocol_send_total_data(msg) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - - def recv(self, event, msg): - """ - @param event:msg - @param msg: 接收到的下行数据 - """ - # locate_now = { - # "cmd":"locate_duration", - # "ts":1738283829, - # "msg":"下发定位周期", - # "data":300,0表示关 - # } - print("recv post_sos{}".format(msg)) - -#上报设备状态 -class IOTWeb_post_device_stateProtoCol(ProtoCol): - """post_device_state""" - TYPE = "post_device_state" - - def send(self, event, msg): - print("send post_device_state{}".format(msg)) - # 通过websocket发送到服务器端 - - send_data = b'' - send_data = protocol_send_total_data(msg) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - - def recv(self, event, msg): - """ - - @param event:mpost_device_state - @param msg: 接收到的下行数据 - """ - # locate_now = { - # "cmd":"locate_duration", - # "ts":1738283829, - # "msg":"下发定位周期", - # "data":300,0表示关 - # } - print("send post_device_state{}".format(msg)) - -#上报闹钟列表 -class IOTWeb_post_alarmProtoCol(ProtoCol): - """post_alarm""" - TYPE = "post_alarm" - - def send(self, event, msg): - print("send post_alarm{}".format(msg)) - # 通过websocket发送到服务器端 - - send_data = b'' - send_data = protocol_send_total_data(msg) - print("send send_data{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - - def recv(self, event, msg): - """ - - @param event:post_alarm - @param msg: 接收到的下行数据 - """ - print("recv post_alarm{}".format(msg)) - -#查找设备 -class IOTWeb_FindDeviceProtoCol(ProtoCol): - """find_device""" - TYPE = "find_device" - - def send(self, event, msg): - # print("send locafind_devicetion{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - find_device = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"find_device", - "rest":"success", - "msg":"收到查找设备指令" - } - } - send_data = b'' - send_data = protocol_send_total_data(find_device) - print("send_data find_device{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:定位信息下发 - @param msg: 接收到的下行数据 - """ - alarm_TTS_str="我在这呢" - base_sentence = "{}".format(alarm_TTS_str) - repeat_sentence = (base_sentence + ",") * 3 # 重复5次并加逗号分隔 - final_sentence = repeat_sentence.rstrip(",") - - EventMesh.publish("play_tts",[final_sentence,1]) #播放tts闹钟文本内容 - print("recv find_device{}".format(msg)) - EventMesh.publish(WEB_CMD_ENUM.FIND_DEVCE_S_CMD) - -#立即定位 -class IOTWeb_LocateNowProtoCol(ProtoCol): - """locate_now""" - TYPE = "locate_now" - def send(self, event, msg): - print("send locate_now{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - locate_now = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"locate_now", - "rest":"success", - "msg":"收到立即定位指令" - } - } - send_data = b'' - send_data = protocol_send_total_data(locate_now) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - - def recv(self, event, msg): - """ - - @param event:立即定位 - @param msg: 接收到的下行数据 - """ - - EventMesh.publish(WEB_CMD_ENUM.LOCATION_NOW_S_CMD) - print("recv locate_now{}".format(msg)) - EventMesh.publish(constants.EVENT.START_LOCATION,WEB_CMD_ENUM.POST_LOCATION_S_CMD) #开始定位 定位完之后上报定位数据 - - -#上报定位信息 -class IOTWeb_post_locationProtoCol(ProtoCol): - """post_location""" - TYPE = "post_location" - - def send(self, event, msg): - # 通过websocket发送到服务器端 - send_data = b'' - send_data = protocol_send_total_data(msg) - print("send_data post_location{}".format(msg)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:定位信息下发 - @param msg: 接收到的下行数据 - """ - print("recv post_location{}".format(msg)) -#闹钟列表 -class IOTWeb_alarm_listProtoCol(ProtoCol): - """alarm_list""" - TYPE = "alarm_list" - - def send(self, event, msg): - print("send alarm_list {}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - rest = "error" - if msg == 1: - rest = "success" - alarm_list ={ - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"alarm_list", - "rest":rest, - "msg":"闹钟配置成功." - } - } - send_data = b'' - send_data = protocol_send_total_data(alarm_list) - print("send_data alarm_list {}".format(msg)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发闹钟配置 - @param msg: 接收到的下行数据 - """ - # 定义要添加的新元素 - # new_item = {'time': '11:30','index': 1} - # data = [{"index":1,"name":"hihihi1"},{"index":2,"name":"hihihi2"},{"index":0,"name":"hihihi0"}] - # len_t = len(data) - # j = 0 - # for i in range(len_t): - # if data[j]['index'] == 1: - # removed_item = data.pop(j) - # print(data) - # j=0 - # continue - # j+=1 - # len_t = len(data) - # print(len_t) - # for i in range(len(data)): - # data[i]["index"] = i - - # url = http://220.160.104.34:30800/ws-bus/test/voice-record/876226211472149?state=1 - # url = http://220.160.104.34:30800/ws-bus/test/876226211472149?testType = alarmList - - alarm_list=[] - ret = 1 - if "data" in msg: - data = msg["data"] - if isinstance(data,list) == True: - for i in range(len(data)): - data_list = data[i] - data_list["index"] = i - print("data_list:{}".format(data_list)) - alarm_list.append(data_list) - alarm_list.sort(key=lambda x: x['index']) - EventMesh.publish("set_storage_config",["alarm_list",alarm_list]) - EventMesh.publish("set_alarm_info",alarm_list) - alarm_list = EventMesh.publish("get_storage_config","alarm_list") - print("alarm_list:{}".format(alarm_list)) - else: - ret = 0 - - EventMesh.publish(WEB_CMD_ENUM.ALARM_LIST_S_CMD,ret) - - -#闹钟配置下发 - -class IOTWeb_alarm_configProtoCol(ProtoCol): - """alarm_config""" - TYPE = "alarm_config" - - def send(self, event, msg): - # print("send alarm_config{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - alarm_config ={ - "cmd":"dispatch_rest", - "ts":int(time_tmp), - "msg":"消息收到响应", - "data":{ - "cmd":"alarm_config", - "rest":"success", - "msg":"闹钟配置成功." - } - } - send_data = b'' - send_data = protocol_send_total_data(alarm_config) - print("send_data alarm_config{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发闹钟配置 - @param msg: 接收到的下行数据 - """ - print("recv alarm_config{}".format(msg)) - EventMesh.publish(WEB_CMD_ENUM.ALARM_CONFIG_S_CMD) - -#SOS联系人下发 -class IOTWeb_sos_listProtoCol(ProtoCol): - """sos_list""" - TYPE = "sos_list" - - def send(self, event, msg): - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - sos_list ={ - "cmd":"dispatch_rest", - "ts":int(time_tmp), - "msg":"消息收到响应", - "data":{ - "cmd":"sos_list", - "rest":"success", - "msg":"收到SOS联系人列表." - } - } - send_data = b'' - send_data = protocol_send_total_data(sos_list) - print("send_data sos_list{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发sos列表 - @param msg: 接收到的下行数据 - """ - #{'msg': 'SOS', 'data': [{'name': '0', 'createTime': 1754031896067, 'updateTime': 1754031896067, 'phone': '12345678900', 'id': '1754031896067', 'deviceId': '876226211472149'}, - # {'name': '1', 'createTime': 1754031896067, 'updateTime': 1754031896067, 'phone': '12345678901', 'id': '1754031896067', 'deviceId': '876226211472149'}, - # {'name': '2', 'createTime': 1754031896067, 'updateTime': 1754031896067, 'phone': '12345678902', 'id': '1754031896067', 'deviceId': '876226211472149'}, - # {'name': '3', 'createTime': 1754031896067, 'updateTime': 1754031896067, 'phone': '12345678903', 'id': '1754031896067', 'deviceId': '876226211472149'}, - # {'name': '4', 'createTime': 1754031896067, 'updateTime': 1754031896067, 'phone': '12345678904', 'id': '1754031896067', 'deviceId': '876226211472149'}], - # 'ts': 1754031896067, 'cmd': 'sos_list'} - print("recv sos_list{}".format(msg)) - sos_list = [] - if "data" in msg: - data = msg["data"] - if isinstance(data,list) == True: - sos_list_len = len(data) - if sos_list_len >= 3: - sos_list_len = 3 - for i in range(sos_list_len): - save_data = {} - # EventMesh.publish("set_storage_config",["SOS_NUMBER{}".format(i+1),data[i+1]]) - # sos_list.append(str(data[i+1])) - save_data["index"] = i - save_data["name"] = data[i]["name"] - save_data["phone"] = data[i]["phone"] - sos_list.append(save_data) - # EventMesh.publish("set_storage_config",["sos_list",sos_list]) - sos_list.sort(key=lambda x: x['index']) - EventMesh.publish("set_storage_config",["sos_list",sos_list]) - EventMesh.publish("set_sos_list_info",sos_list) - read_soslist = EventMesh.publish("get_storage_config","sos_list") - print(read_soslist) - EventMesh.publish(WEB_CMD_ENUM.SOS_LIST_S_CMD) -#NLP指令处理 -def ai_cmd_handle(cmd): - # print("") - if "查询" in cmd:#当指令中有查询时,截取查询操作 - EventMesh.publish("find_cmd",cmd) - elif "讲话" in cmd:#当指令中有讲话时,截取语速操作 - voice_start_index = cmd.index("讲话") + len("讲话")#动态判断截取部分 - find = cmd[voice_start_index:]#查询截取 - if "å¿«" in find: - EventMesh.publish("rate_move",1) - volume = "变快" - elif "慢" in find: - EventMesh.publish("rate_move",0) - volume = "变慢" - tts_play_str = "语速已经" + volume - EventMesh.publish("play_tts",[tts_play_str,0]) - elif "声音改为" in cmd:#当指令中有声音改为时,进入音色修改 - voice_start_index = cmd.index("声音改为") + len("声音改为")#动态判断截取部分 - find = cmd[voice_start_index:]#查询截取 - EventMesh.publish("change_voice",find) - elif "结束对话" in cmd:#当指令中有结束对话时,结束当前对话 - EventMesh.publish("Close_VC") - elif "音量" in cmd:#当指令中有音量改为时,音量改为具体数值 - if "调节" in cmd : - voice_start_index = cmd.index("音量调节:") + len("音量调节:")#动态判断截取部分 - find = cmd[voice_start_index:]#查询截取 - print("find = {}".format(find)) - volume = int(find) - volume += 100#设置音量以100-200为参数 - result = EventMesh.publish("change_volume",volume) - if result == 1: - tts_play_str = "音量已经改为" + find - EventMesh.publish("play_tts",[tts_play_str,0]) - else: - tts_play_str = "设置失败,请重新设置0~100的音量大小" - EventMesh.publish("play_tts",[tts_play_str,0]) - if "加大" in cmd: - result = EventMesh.publish("change_volume",1) - if result == 1: - tts_play_str = "音量已经加大" - EventMesh.publish("play_tts",[tts_play_str,0]) - else: - tts_play_str = "当前音量已经是最大了" - EventMesh.publish("play_tts",[tts_play_str,0]) - elif "减小" in cmd: - result = EventMesh.publish("change_volume",0) - if result == 1: - tts_play_str = "音量已经减小" - EventMesh.publish("play_tts",[tts_play_str,0]) - else: - tts_play_str = "当前音量已经是最小了" - EventMesh.publish("play_tts",[tts_play_str,0]) - - elif "语速倍速设置为:" in cmd:#修改对话模式 - voice_start_index = cmd.index("语速倍速设置为:") + len("语速倍速设置为:")#动态判断截取部分 - find = cmd[voice_start_index:]#查询截取 - print("find = {}".format(find)) - if float(find) > 2.0 or float(find) < 0.5: - tts_play_str = "抱歉,语速仅支持0.5-2倍,请重新设置" - EventMesh.publish("play_tts",[tts_play_str,0]) - return - EventMesh.publish("SET_SPEECH_RATE",float(find)) - tts_play_str = "语速已经改为" + find - EventMesh.publish("play_tts",[tts_play_str,0]) - elif "聆听" in cmd:#聆听快/慢一些 - if "å¿«" in cmd: - tts_play_str = EventMesh.publish("NLP_set_vad_silence",1) - elif "慢" in cmd: - tts_play_str = EventMesh.publish("NLP_set_vad_silence",0) - EventMesh.publish("play_tts",[tts_play_str,0]) - elif "这个不支持哦,请换一个" in cmd: - tts_play_str = "抱歉呀,暂不支持该方言,请换一个" - EventMesh.publish("play_tts",[tts_play_str,0]) - elif "不支持的操作" in cmd: - tts_play_str = "抱歉呀,暂不支持该命令" - EventMesh.publish("play_tts",[tts_play_str,0]) - - if not _NLP_EXTENDED_CMDS: - return - if "会议纪要" in cmd: - print("cmd:已进入会议") - EventMesh.publish("come_meet_mode") - elif "翻译" in cmd: - print("cmd:已进入翻译") - EventMesh.publish("come_trans_mode") - elif "查看历史留言" in cmd: - EventMesh.publish(WEB_CMD_ENUM.GET_NEW_VOICE_MSG_S) - EventMesh.publish("set_history_voice_flag", 1) - EventMesh.publish("clean_unread_vocie") - EventMesh.publish("set_storage_config", ["is_noread_voice", 0]) - -#NLP指令 -class IOTWeb_Nlp_ConfigProtoCol(ProtoCol): - """ai_cmd""" - TYPE = "ai_cmd" - - def send(self, event, msg): - # print("ai_cmd{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - ai_cmd = { - "cmd":"ai_cmd", - "ts":time_tmp, - "msg":"AI指令", - "data":None - } - send_data = b'' - send_data = protocol_send_total_data(ai_cmd) - print("send_data ai_cmd{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - @param event:上行下发push_music - @param msg: 接收到的下行数据 - """ - #提取指令名称 - cmdName = msg["data"] - print("提取到的AI指令为{}".format(cmdName)) - ai_cmd_handle(cmdName) -#设备进入翻译,小程序返回在线情况 -class IOTWeb_translate_device_enterProtoCol(ProtoCol): - """translate_device_enter""" - TYPE = "translate_device_enter" - - def send(self, event, msg): - # print("translate_device_enter{}".format(msg)) - # 通过websocket发送到服务器端 - print("设备进入翻译,小程序返回在线情况") - - def recv(self, event, msg): - """ - @param event:上行下发translate_device_enter - @param msg: 接收到的下行数据 - """ - print("设备进入翻译获取状态:{}".format(msg["data"]["msg"])) - status = msg["data"]["rest"] - - if status == "success": - EventMesh.publish("set_trans_flag",1)#能进入AI翻译 - exit_tts = "已进入实时翻译模式" - EventMesh.publish("play_tts",[exit_tts,1]) - elif status == "error": - EventMesh.publish("set_trans_flag",0)#不能进入AI翻译 - exit_tts = msg["data"]["msg"] - EventMesh.publish("play_tts",[exit_tts,0]) - print("recv translate_device_enter") - -#接收小程序控制进入翻译 -class IOTWeb_come_transProtoCol(ProtoCol): - """translate_app_enter""" - TYPE = "translate_app_enter" - - def send(self, event, msg): - # print("translate_app_enter{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - come_trans = { - "cmd":"translate_device_complete", - "ts":time_tmp, - "msg":"进入成功", - "data":None - } - send_data = b'' - send_data = protocol_send_total_data(come_trans) - print("send_data translate_app_enter{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - @param event:上行下发translate_app_enter - @param msg: 接收到的下行数据 - """ - EventMesh.publish("come_trans_mode")#进入AI翻译 - print("recv translate_app_enter{}".format(msg)) - -#接收小程序控制退出翻译 -class IOTWeb_exit_transProtoCol(ProtoCol): - """translate_app_exit""" - TYPE = "translate_app_exit" - - def send(self, event, msg): - # print("translate_app_enter{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - come_trans = { - "msg": "退出翻译", - "data": None, - "cmd": "translate_device_exit", - "ts": time_tmp - } - send_data = b'' - send_data = protocol_send_total_data(come_trans) - print("send_data translate_app_enter{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - @param event:上行下发translate_app_enter - @param msg: 接收到的下行数据 - """ - EventMesh.publish("exit_trans_mode")#退出AI翻译 - print("recv translate_app_exit{}".format(msg)) - -#接收小程序控制进入会议 -class IOTWeb_appcontrol_meetProtoCol(ProtoCol): - """meet_control""" - TYPE = "meet_control" - def send(self, event, msg): - # print("translate_app_enter{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - come_trans = { - "msg": "开始会议", - "data": None, - "cmd": "meet_begin", - "ts": 1761121597 - } - send_data = b'' - send_data = protocol_send_total_data(come_trans) - print("send_data translate_app_enter{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - @param event:上行下发meeting_app_enter - @param msg: 接收到的下行数据 - """ - data = msg["data"]#判断是开启还是关闭 - if data == "begin": - EventMesh.publish("come_meet_mode")#进入会议纪要 - elif data == "end": - EventMesh.publish("exit_meet_mode")#退出会议纪要 - print("APP Meeting Control") - -#翻译播放TTS -class IOTWeb_play_trans_TTSProtoCol(ProtoCol): - """tts_audio_url""" - TYPE = "tts_audio_url" - - def send(self, event, msg): - # print("tts_audio_url{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - tts_audio_url = { - "cmd":"tts_audio_url_complete", - "ts":time_tmp, - "msg":"返回状态", - "data":None - } - send_data = b'' - send_data = protocol_send_total_data(tts_audio_url) - print("send_data tts_audio_url{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - @param event:上行下发push_music - @param msg: 接收到的下行数据 - """ - #提取录音播放名称 - voiceUrl = msg["data"] - print("提取到的翻译路径为{}".format(voiceUrl)) - EventMesh.publish("play_trans_tts",voiceUrl) - print("recv tts_audio_url{}".format(msg)) - -#音乐播放 -class IOTWeb_play_MusicProtoCol(ProtoCol): - """push_music""" - TYPE = "push_music" - - def send(self, event, msg): - # print("push_music{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - push_music = { - "cmd":"push_music", - "ts":time_tmp, - "msg":"音乐播放", - "data":None - } - send_data = b'' - send_data = protocol_send_total_data(push_music) - print("send_data push_music{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - @param event:上行下发push_music - @param msg: 接收到的下行数据 - """ - #提取音乐播放名称,地址 - print("recv push_music{}".format(msg)) - musicName = msg["data"]["musicName"] - musicUrl = msg["data"]["musicUrl"] - print("提取到的音乐名称为{},路径为{}".format(musicName,musicUrl)) - EventMesh.publish("play_music",[musicName,musicUrl,1]) - - -#音量下发 -class IOTWeb_volumeProtoCol(ProtoCol): - """volume""" - TYPE = "volume" - - def send(self, event, msg): - print("send volume{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - volume = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"volume", - "rest":"success", - "msg":"收到音量设置." - } - } - send_data = b'' - send_data = protocol_send_total_data(volume) - print("send_data volume{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发volume - @param msg: 接收到的下行数据 - """ - if "data" in msg: - r_volume = msg["data"] - print("recv volume{} type:{}".format(r_volume,type(msg))) - - EventMesh.publish("SET_TTS_VOLUME",int(r_volume)) - EventMesh.publish("adjust_volume_toBle",r_volume) - EventMesh.publish(WEB_CMD_ENUM.VOLUME_S_CMD) -#下发SOS配置 -class IOTWeb_sos_configProtoCol(ProtoCol): - """sos_config""" - TYPE = "sos_config" - - def send(self, event, msg): - print("send sos_config{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - sos_config = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"sos_config", - "rest":"success", - "msg":"SOS配置成功." - } - } - send_data = b'' - send_data = protocol_send_total_data(sos_config) - print("send_data sos_config{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发sos_config - @param msg: 接收到的下行数据 - """ - print("recv sos_config{}".format(msg)) - if "data" in msg: - sos_config = msg["data"] - print("recv sos_config{} type:{}".format(sos_config,type(msg))) - EventMesh.publish("set_storage_config",["sos_config",sos_config]) - EventMesh.publish("set_sos_config",sos_config) - sos_config = EventMesh.publish("get_storage_config","sos_config") - # print("sos_config:{} type:{}".format(sos_config,sos_config["enable110"])) - EventMesh.publish(WEB_CMD_ENUM.SOS_CONFIG_S_CMD) -#闹钟铃声获取 - -class IOTWeb_get_ringtoneProtoCol(ProtoCol): - """get_ringtone""" - TYPE = "get_ringtone" - - def send(self, event, msg): - print("send get_ringtone{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - get_ringtone ={ - "cmd":"get_ringtone", - "ts":time_tmp, - "msg":"请求下发铃声列表", - "data":None - } - send_data = b'' - send_data = protocol_send_total_data(get_ringtone) - print("send_data get_ringtone{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发get_ringtone - @param msg: 接收到的下行数据 - """ - print("recv get_ringtone{}".format(msg)) - -#闹钟铃声列表 -class IOTWeb_ringtone_listProtoCol(ProtoCol): - """ringtone_list""" - TYPE = "ringtone_list" - - def send(self, event, msg): - print("send ringtone_list{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - ringtone_list ={ - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"ringtone_list", - "rest":"success", - "msg":"收到闹钟铃声列表." - } - } - send_data = b'' - send_data = protocol_send_total_data(ringtone_list) - print("send_data ringtone_list{}".format(msg)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发ringtone_list - @param msg: 接收到的下行数据 - """ - print("recv ringtone_list{}".format(msg)) - - -#下发静默录音 -class IOTWeb_voice_recordProtoCol(ProtoCol): - """voice_record""" - TYPE = "voice_record" - start_ai = 0 - def send(self, event, msg): - print("send voice_record{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - s_rest = "0" - s_msg = "停止录音." - if msg == "1": - print("设备允许录音") - s_rest = "1" - s_msg = "开始录音." - - voice_record = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"voice_record", - "rest":s_rest, - "msg":s_msg - } - } - send_data = b'' - send_data = protocol_send_total_data(voice_record) - print("send_data voice_record{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发voice_record - @param msg: 接收到的下行数据 - """ - print("recv voice_record{}".format(msg)) - if "data" in msg: - data = msg["data"] - s_rest = "0" - if data == "0": - s_rest = "0" - - record_mode = EventMesh.publish("get_voice_record_mode") - if record_mode == None: - record_mode = 0 - if record_mode != 2:#语音留言不允许打断 - EventMesh.publish("stop_voice_record") - print("停止录音") - EventMesh.publish(WEB_CMD_ENUM.VOICE_RECORD_S_CMD,s_rest) - - elif data == "1": - - get_sos_flag = EventMesh.publish("get_sos_flag") - if get_sos_flag != 0: - s_rest = "0" - print("正在电话") - EventMesh.publish(WEB_CMD_ENUM.VOICE_RECORD_S_CMD,s_rest) - return - record_mode = EventMesh.publish("get_voice_record_mode") - if record_mode == None: - record_mode = 0 - get_VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag") - if get_VC_satrt_flag == None: - get_VC_satrt_flag = 0 - - if record_mode == 2 or get_VC_satrt_flag == 1:# 如果在AI对话 或者语音留言不允许静默录音 - s_rest = "0" - EventMesh.publish(WEB_CMD_ENUM.VOICE_RECORD_S_CMD,s_rest) - EventMesh.publish("stop_voice_record") - else: - s_rest = "1" - EventMesh.publish(WEB_CMD_ENUM.VOICE_RECORD_S_CMD,s_rest) - EventMesh.publish("start_voice_record",1) - print("开始录音") - -#设备解绑 -class IOTWeb_unbind_devProtoCol(ProtoCol): - """unbind_dev""" - TYPE = "unbind_dev" - unbind_devtimer = osTimer() - init_flag = 0 - def send(self, event, msg): - print("send unbind_dev{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - unbind_dev={ - "cmd":"unbind_dev", - "ts":time_tmp, - "msg":"主动解绑设备", - "data":None - } - send_data = b'' - send_data = protocol_send_total_data(unbind_dev) - print("unbind_dev{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - self.init_flag=1 - self.unbind_devtimer.start(2000,0,self.unbind_devtimer_cb) - def unbind_devtimer_cb(self,*argc): - if self.init_flag == 1: - play_str = "设备解绑失败" - EventMesh.publish("play_tts",[play_str,1]) - - def recv(self, event, msg): - """ - - @param event:上行下发unbind_dev - @param msg: 接收到的下行数据 - """ - # { - # "cmd":"post_rest", - # "ts":1738283829, - # "msg":"消息收到响应", - # "data":{ - # "cmd":"unbind_dev", - # "rest":"success|error", - # "msg":"收到主动解绑设备指令" - # } - # } - self.btstate = 0 - self.blestate = 0 - self.unbind_devtimer.stop() - self.init_flag=0 - if "data" in msg: - data = msg["data"] - rest = data["rest"] - if rest == "success": - play_str = "设备解绑成功" - EventMesh.publish("set_storage_config",["switch_ble_state","1"]) - EventMesh.publish("send_ble_switch_toble","1") - - - EventMesh.publish("set_storage_config",["switch_bt_state","1"]) - EventMesh.publish("send_bt_switch_toble","1") - EventMesh.publish("play_tts",[play_str,1]) - # ret = 0 - # ret = ql_fs.path_exists("/usr/static/storage_file")#配置文件 - # if ret == True: - # ql_fs.rmdirs("/usr/static/storage_file") - # ql_fs.mkdirs("/usr/static/storage_file") - # utime.sleep_ms(2000) - # Power.powerRestart() - pass - else: - play_str = "设备解绑失败" - EventMesh.publish("play_tts",[play_str,1]) - pass - print("recv unbind_dev{}".format(msg)) -#用户解绑 -class IOTWeb_user_unbind_devProtoCol(ProtoCol): - """user_unbind_dev""" - TYPE = "user_unbind_dev" - def send(self, event, msg): - print("send user_unbind_dev{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - user_unbind_dev={ - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"user_unbind_dev", - "rest":"success", - "msg":"收到用户解绑设备指令" - } - } - send_data = b'' - send_data = protocol_send_total_data(user_unbind_dev) - print("user_unbind_dev{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发user_unbind_dev - @param msg: 接收到的下行数据 - """ - # { - # "cmd":"post_rest", - # "ts":1738283829, - # "msg":"消息收到响应", - # "data":{ - # "cmd":"user_unbind_dev", - # "rest":"success|error", - # "msg":"收到主动解绑设备指令" - # } - # } - self.btstate = 0 - self.blestate = 0 - EventMesh.publish("set_storage_config",["switch_ble_state","1"]) #用户解绑设备需要开启广播 开启经典蓝牙 - EventMesh.publish("send_ble_switch_toble","1") - - EventMesh.publish("set_storage_config",["switch_bt_state","1"]) - EventMesh.publish("send_bt_switch_toble","1") - EventMesh.publish(WEB_CMD_ENUM.USER_UNBIND_DEV_S) - tts_play_str = "设备已解绑" - EventMesh.publish("play_tts",[tts_play_str,1]) - # ret = 0 - # ret = ql_fs.path_exists("/usr/static/storage_file")#配置文件 - # if ret == True: - # ql_fs.rmdirs("/usr/static/storage_file") - # ql_fs.mkdirs("/usr/static/storage_file") - # utime.sleep_ms(100) - # Power.powerRestart() - print("recv user_unbind_dev{}".format(msg)) -#下发位子留言 -class IOTWeb_text_msgProtoCol(ProtoCol): - """text_msg""" - TYPE = "text_msg" - - def send(self, event, msg): - print("send text_msg{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - - rest = "error" - if msg == 1: - rest = "success" - text_msg = { - "cmd":"post_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"text_msg", - "rest":rest, - "msg":"收到文本消息." - } - } - send_data = b'' - send_data = protocol_send_total_data(text_msg) - print("send_data text_msg{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发voice_record - @param msg: 接收到的下行数据 - """ - print("recv time_tmp{}".format(msg)) - recv_state = 0 - if "data" in msg: - play_tts_text = msg["data"] - try: - EventMesh.publish("play_tts",[play_tts_text,1]) - recv_state = 1 - except Exception as e: - print("下发文字留言出错了") - EventMesh.publish(WEB_CMD_ENUM.TEXT_MSG_S_CMD,recv_state) - -#AI对话音色配置 -class IOTWeb_chat_configProtoCol(ProtoCol): - """chat_config""" - TYPE = "chat_config" - - def send(self, event, msg): - # print("send chat_config{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - chat_config = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"chat_config", - "rest":"success", - "msg":"收到AI对话配置" - } - } - send_data = b'' - send_data = protocol_send_total_data(chat_config) - print("send_data chat_config{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发chat_config - @param msg: 接收到的下行数据 - """ - print("recv chat_config{}".format(msg)) - # { - # "cmd":"chat_config", - # "ts":1738283829, - # "msg":"下发AI对话配置", - # "data":{ - # "speed":1.5, - # "vadDuration":600, - # "voiceId":"音色ID,火山音色或者科大讯飞音色ID" - # } - # } - # {'msg': '测试聊天配置.', 'data': {'botName': '柒乐乐', 'vadDuration': 600, 'speed': '1.0', 'provider': 'iflytek', 'voiceId': 'x3_linlin'}, - # 'ts': 1755600807553, 'cmd': 'chat_config'} - - if "data" in msg: - data = msg["data"] - speed = None - try: - speed = float(data["speed"]) - except Exception as e: - usys.print_exception(e) - vadDuration = data["vadDuration"] - voiceId = data["voiceId"] - provider = data["provider"] - botName = data["botName"] - asr = data["asr"] - if speed != None: - EventMesh.publish("SET_SPEECH_RATE",speed) - if vadDuration != None: - #if len(vadDuration) != 0: - EventMesh.publish("set_vad_silence",vadDuration) - tts_type = {} - if voiceId != None and len(voiceId) != 0: - tts_type["voice"] = voiceId - if provider != None and len(provider) != 0: - tts_type["outbound_attributess_provider"] = provider - if botName != None and len(botName) != 0: - tts_type["bot_name"] = botName - if voiceId != None and len(voiceId) != 0: - tts_type["voice"] = voiceId - if asr != None and len(asr) != 0: - tts_type["inbound_attributess_provider"] = asr - if len(tts_type)>0: - print("tts_type:{}".format(tts_type)) - EventMesh.publish("set_VC_TTS_voice_type",tts_type) - EventMesh.publish(WEB_CMD_ENUM.CHAT_CONFIG_S) -#获取AI对话音色配置 -class IOTWeb_get_chat_configProtoCol(ProtoCol): - """get_chat_config""" - TYPE = "get_chat_config" - - def send(self, event, msg): - # print("send chat_config{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - get_chat_config = { - "cmd":"get_chat_config", - "ts":time_tmp, - "msg":"获取AI对话配置", - "data":None - } - send_data = b'' - send_data = protocol_send_total_data(get_chat_config) - print("send_data chat_config{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发get_chat_config - @param msg: 接收到的下行数据 - """ - print("recv get_chat_config{}".format(msg)) - -#上报日程列表 -class IOTWeb_post_scheduleProtoCol(ProtoCol): - """post_schedule""" - TYPE = "post_schedule" - - def send(self, event, msg): - print("send post_schedule{}".format(msg)) - # 通过websocket发送到服务器端 - - send_data = b'' - send_data = protocol_send_total_data(msg) - print("send send_data{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - - def recv(self, event, msg): - """ - - @param event:post_schedule - @param msg: 接收到的下行数据 - """ - print("recv post_schedule{}".format(msg)) - - -#下发日程 -class IOTWeb_scheduleProtoCol(ProtoCol): - """schedule""" - TYPE = "schedule" - - def send(self, event, msg): - print("send schedule{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - schedule = { - "cmd":"post_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"schedule", - "rest":"success", - "msg":"收到日程列表." - } - } - send_data = b'' - send_data = protocol_send_total_data(schedule) - print("send_data schedule{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发schedule - @param msg: 接收到的下行数据 - """ - # { - # "cmd":"text_msg", - # "ts":1738283829, - # "msg":"下发文本消息", - # "data":"文本内容" - # } - print("recv time_tmp{}".format(msg)) - # { - # "cmd":"schedule", - # "ts":1738283829, - # "msg":"下发日程列表", - # "data":[ - # "title":"日程名称", - # "description":"提醒内容", - # "repeat":1~4播报重复次数, - # "date":"日期,yyyy-MM-dd", - # "时间":"HH:mm" - # ] - # } - schedule = [] - if "data" in msg: - data = msg["data"] - for i,sh in enumerate(data): - sh["index"] = i - schedule.append(sh) - schedule.sort(key=lambda x: x['index']) - EventMesh.publish("set_storage_config",["schedule",schedule]) - - schedule = EventMesh.publish("get_storage_config","schedule") - EventMesh.publish("set_schedule_info",schedule)# - print(schedule) - EventMesh.publish(WEB_CMD_ENUM.SCHEDULE_S_CMD) -#下发新的语音未读留言 -class IOTWeb_new_voice_msgProtoCol(ProtoCol): - """new_voice_msg""" - TYPE = "new_voice_msg" - - def send(self, event, msg): - print("send new_voice_msg{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - new_voice_msg = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"new_voice_msg", - "rest":"success", - "msg":"收到新语音消息指令." - } - } - send_data = b'' - send_data = protocol_send_total_data(new_voice_msg) - print("send send_data{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - EventMesh.publish(WEB_CMD_ENUM.GET_NEW_VOICE_MSG_S) - def recv(self, event, msg): - """ - - @param event:new_voice_msg - @param msg: 接收到的下行数据 - """ - EventMesh.publish(WEB_CMD_ENUM.NEW_VOICE_MSG_S) - - print("recv new_voice_msg{}".format(msg)) - -#设备主动查询未读的语音消息列表 -class IOTWeb_find_new_voice_msgProtoCol(ProtoCol): - """find_voice_msg_list""" - TYPE = "find_voice_msg_list" - def __init__(self): - super().__init__() - self.history_board_flag = 0#第一次进去要广播 - self.open_device = 0#开机播报未读留言 - self.has_processed_history_voice = 0 #播完列表不许再拨“历史未读留言为空” - EventMesh.subscribe("set_history_board_flag",self.set_history_board_flag) - EventMesh.subscribe("set_open_device",self.set_open_device) - EventMesh.subscribe("get_open_device",self.get_open_device) - def send(self, event, msg): - print("send find_voice_msg_list") - - def recv(self, event, msg): - """ - - @param event:find_voice_msg_list - @param msg: 接收到的下行数据 - """ - print("recv find_voice_msg_list{}".format(msg)) - - new_voice_list = []#字典列表 - voice_list = msg["data"] - print("msg[\"data\"] = {}".format(voice_list)) - if EventMesh.publish("get_history_voice_flag") == 1: - if len(voice_list) == 0:#为空 - if EventMesh.publish("get_wait_voice_flag") == 0:#未听历史留言 - tts_play_str = "已无更多历史留言" - EventMesh.publish("play_tts",[tts_play_str,0]) - self.history_board_flag = 0 - self.has_processed_wait_voice = 0 - self.wait_first_flag = 0 - # if self.has_processed_history_voice == 0: - # tts_play_str = "已无更多历史留言" - # EventMesh.publish("play_tts",[tts_play_str,0]) - # else: - # self.has_processed_history_voice = 0#读完了置为0 - # self.history_board_flag = 0#广播置为0 - # self.history_board_flag = 0 - # self.has_processed_wait_voice = 0 - # self.wait_first_flag = 0 - else:#等待历史留言 - self.has_processed_wait_voice = 0 - self.wait_first_flag = 0 - EventMesh.publish("set_recv_voice",0) #设置留言播放状态 - EventMesh.publish("set_history_voice_flag",0) - EventMesh.publish("set_wait_voice_flag",0) - else:#有历史留言 - if EventMesh.publish("get_wait_voice_flag") == 0:#历史留言 - print("历史广播状态!!!!! = {}".format(self.history_board_flag)) - if self.history_board_flag == 0: - tts_play_str = "您共有" + str(len(voice_list)) + "条历史留言,正在为您播放" - EventMesh.publish("play_tts",[tts_play_str,1]) - EventMesh.publish("set_pwk_notice_flag",1) - EventMesh.publish("set_pwk_cnt_voice",len(voice_list))#设置多,防止进入留言播完判断 - self.history_board_flag = 1 - self.has_processed_history_voice = 1 - for da in voice_list: - new_voice_list.append(da) - if len(new_voice_list) > 0: - pass - else:#未读留言 - if self.open_device == 1 or EventMesh.publish("get_60S_voice_flag") == 1: - EventMesh.publish("set_pwk_cnt_voice",len(voice_list))#设置未读次数,由于是开机一起查看的 - EventMesh.publish("set_60S_voice_flag",0) - for da in voice_list: - new_voice_list.append(da) - if len(new_voice_list) > 0: - pass - EventMesh.publish("set_play_new_voice_list",new_voice_list) - EventMesh.publish("play_new_voice_list") - else:#正常留言 - if EventMesh.publish("get_1M_flag") == 0: - if EventMesh.publish("get_pwk_cnt_voice") >= 0: - for da in voice_list: - new_voice_list.append(da) - if len(new_voice_list) > 0: - pass - EventMesh.publish("set_play_new_voice_list",new_voice_list) - EventMesh.publish("play_new_voice_list") - def set_history_board_flag(self,event,msg): - self.history_board_flag = msg - def set_open_device(self,event,msg): - self.open_device = msg - def get_open_device(self,event,msg): - return self.open_device -#请求未读的语音消息列表 -class IOTWeb_get_new_voice_msgProtoCol(ProtoCol): - """get_new_voice_msg""" - TYPE = "get_new_voice_msg" - - def send(self, event, msg): - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - # { - # "cmd":"get_new_voice_msg", - # "ts":1738283829, - # "msg":"请求未读的语音消息列表", - # "data":null - # } - get_new_voice_msg = { - "cmd":"get_new_voice_msg", - "ts":time_tmp, - "msg":"请求未读的语音消息列表", - "data":None - } - - send_data = b'' - send_data = protocol_send_total_data(get_new_voice_msg) - print("send get_new_voice_msg{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - - def recv(self, event, msg): - """ - - @param event:get_new_voice_msg - @param msg: 接收到的下行数据 - """ - # get_new_voice_msg = { - # "cmd":"post_rest", - # "ts":time_tmp, - # "msg":"消息收到响应", - # "data":{ - # "cmd":"get_new_voice_msg", - # "rest":"success", - # "msg":"收到请求新语音列表." - # } - # } - print("recv get_new_voice_msg{}".format(msg)) -# 1播放 0暂停 2上一首 3下一首 -class IOTWeb_player_controlProtoCol(ProtoCol): - """player_control""" - TYPE = "player_control" - - def send(self, event, msg): - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - paly_str = "停止" - paly_data = "stop" - if msg == 1: - paly_str = "播放" - paly_data = "play" - pass - elif msg == 0: - paly_str = "停止" - paly_data = "stop" - pass - elif msg == 2: - paly_str = "上一首" - paly_data = "pre" - pass - elif msg == 3: - paly_str = "下一首" - paly_data = "next" - pass - player_control = { - "cmd":"player_control", - "ts":time_tmp, - "msg":paly_str, - "data":paly_data - } - - send_data = b'' - send_data = protocol_send_total_data(player_control) - print("send player_control{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",msg) - - def recv(self, event, msg): - """ - - @param event:get_new_voice_msg - @param msg: 接收到的下行数据 - """ - # { - # "cmd":"post_rest", - # "ts":1738283829, - # "msg":"消息收到响应", - # "data":{ - # "cmd":"player_control", - # "rest":"success|error", - # "msg":"收到播放/停止/暂停指令/下一首/上一首" - # } - # } - paly_state = 0 - if "data" in msg: - data = msg["data"] - msg_str = data["msg"] - if msg_str.find("播放") != -1: - paly_state=1 - pass - elif msg_str.find("停止") != -1: - paly_state=0 - pass - elif msg_str.find("下一首") != -1: - paly_state = 3 - pass - elif msg_str.find("上一首") != -1: - paly_state = 2 - pass - - EventMesh.publish(WEB_CMD_ENUM.PLAY_CONTROL_S,paly_state) - print("recv send_data{}".format(msg)) -class IOTWeb_new_voice_msg_listProtoCol(ProtoCol): - """new_voice_msg_list""" - TYPE = "new_voice_msg_list" - def __init__(self): - super().__init__() - self._1M_flag = 0#判断一分钟定时器是否开启中 - self._1M_nowplay_flag = 0#判断当前留言播放是否结束去影响新留言播放的标志位 - self.app_new_voice_flag = 0#app是否下发留言 - self._1M_timer = osTimer()#多发留言间隙时间60S - self._60S_voice_flag = 0 - self.is_listen_wait = 0#是否收听未听留言 - self.close_timer_cnt = 0 #若两次未听定时器关闭 - EventMesh.subscribe("get_app_new_voice_flag",self.get_app_new_voice_flag) - EventMesh.subscribe("set_app_new_voice_flag",self.set_app_new_voice_flag) - EventMesh.subscribe("get_1M_flag",self.get_1M_flag) - EventMesh.subscribe("set_1M_nowplay_flag",self.set_1M_nowplay_flag) - EventMesh.subscribe("set_is_listen_wait",self.set_is_listen_wait) - EventMesh.subscribe("get_60S_voice_flag",self.get_60S_voice_flag) - EventMesh.subscribe("set_60S_voice_flag",self.set_60S_voice_flag) - - def send(self, event, msg): - print("send new_voice_msg_list{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - new_voice_msg_list = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"new_voice_msg_list", - "rest":"success", - "msg":"收到新语音列表." - } - } - send_data = b'' - send_data = protocol_send_total_data(new_voice_msg_list) - print("send send_data{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - - def recv(self, event, msg): - """ - - @param event:new_voice_msg_list - @param msg: 接收到的下行数据 - """ - self.app_new_voice_flag = 1#标志位为1 - new_voice_list = [] - if "data" in msg: - data = msg["data"] - for da in data: - new_voice_list.append(da) - if len(new_voice_list) > 0: - pass - EventMesh.publish("set_play_new_voice_list",new_voice_list) - print("是否收到留言:{},self._1M_flag = {}".format(EventMesh.publish("get_recv_voice"),self._1M_flag)) - if EventMesh.publish("get_recv_voice") == 0 and self._1M_flag == 0 and self._1M_nowplay_flag == 0:#当没有音频在播放时,再播放新的,否则保存下来不播放,等60S后一起播放 - EventMesh.publish("set_recv_voice",1) #设置留言播放状态 - EventMesh.publish("play_new_voice_list") - self._1M_nowplay_flag == 1 - else:#当有音频在播放时,存下来开始倒计时 - pass - if self._1M_flag == 0 : - print("等待留言60S倒计时开启") - self._1M_timer.start(60000, 0, self._1M_timer_cb) - self._1M_flag = 1 - EventMesh.publish(WEB_CMD_ENUM.NEW_VOICE_MSG_LIST_S) - print("recv new_voice_msg_list{}".format(msg)) - def _1M_timer_cb(self,*argc): - EventMesh.publish("publish_AI_btnInfo",{"func":self._1M_timer_cb_}) - - def _1M_timer_cb_(self): - #self.iot_web_send_queue.put("ping") - self._1M_flag = 0 - self._60S_voice_flag = 1 - get_VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag") - if get_VC_satrt_flag == None: - get_VC_satrt_flag = 0 - if get_VC_satrt_flag == 0: - print("收到多条信息,统一播放") - EventMesh.publish("set_history_voice_flag",1)#修改历史留言标志位 - EventMesh.publish("set_wait_voice_flag",1)#修改60S标志位 - EventMesh.publish(WEB_CMD_ENUM.GET_NEW_VOICE_MSG_S) - - def get_app_new_voice_flag(self,event,msg): - return self.app_new_voice_flag - def set_app_new_voice_flag(self,event,msg): - self.app_new_voice_flag = msg - def get_1M_flag(self,event,msg): - return self._1M_flag - def get_60S_voice_flag(self,event,msg): - return self._60S_voice_flag - def set_60S_voice_flag(self,event,msg): - self._60S_voice_flag = msg - def set_1M_nowplay_flag(self,event,msg): - self._1M_nowplay_flag = msg - def set_is_listen_wait(self,event,msg): - self.is_listen_wait = msg -class IOTWeb_put_voice_msg_stateProtoCol(ProtoCol): - """put_voice_msg_state""" - TYPE = "put_voice_msg_state" - - def send(self, event, msg): - print("send put_voice_msg_state{}".format(msg)) - # 通过websocket发送到服务器端 - - send_data = b'' - send_data = protocol_send_total_data(msg) - print("send send_data{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - - def recv(self, event, msg): - """ - - @param event:put_voice_msg_state - @param msg: 接收到的下行数据 - """ - print("recv put_voice_msg_state{}".format(msg)) -#收到蓝牙连接记录获取请求 -class IOTWeb_get_bt_connect_record_listProtoCol(ProtoCol): - """get_bt_connect_record_list""" - TYPE = "get_bt_connect_record_list" - - def send(self, event, msg): - print("send get_bt_connect_record_list{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - get_bt_connect_record_list = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"get_bt_connect_record_list", - "rest":"success", - "msg":"收到蓝牙连接记录获取请求" - } - } - send_data = b'' - send_data = protocol_send_total_data(get_bt_connect_record_list) - print("send send_data{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) - - def recv(self, event, msg): - """ - - @param event:get_bt_connect_record_list - @param msg: 接收到的下行数据 - """ - # { - # "cmd":"get_bt_connect_record_list", - # "ts":1738283829, - # "msg":"上报蓝牙连接列表", - # "data": None - # } - EventMesh.publish("send_get_connect_bt_state") - EventMesh.publish(WEB_CMD_ENUM.GET_BT_CONNECT_RECORD_LIST_S) - print("recv get_bt_connect_record_list{}".format(msg)) - -#蓝牙开关 -class IOTWeb_switch_bt_stateProtoCol(ProtoCol): - """switch_bt_state""" - TYPE = "switch_bt_state" - - def send(self, event, msg): - # print("send switch_bt_state{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - # { - # "cmd":"dispatch_rest", - # "ts":1738283829, - # "msg":"消息收到响应", - # "data":{ - # "cmd":"switch_bt_state", - # "rest":"success|error", - # "msg":"收到蓝牙已打开/蓝牙已关闭指令" - # } - # } - s_msg = "蓝牙已关闭指令" - if msg == "1": - s_msg = "收到蓝牙已打开" - elif msg == "0": - s_msg = "蓝牙已关闭指令" - switch_bt_state = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"switch_bt_state", - "rest":"success", - "msg":s_msg - } - } - send_data = b'' - send_data = protocol_send_total_data(switch_bt_state) - print("send_data switch_bt_state{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发switch_bt_state - @param msg: 接收到的下行数据 - """ - s_state = "1" - if "data" in msg: - data = msg["data"] - if data == "0": - s_state = "0" - EventMesh.publish("set_storage_config",["switch_bt_state","0"]) - EventMesh.publish("send_bt_switch_toble","0") - pass - elif data == "1": - s_state = "1" - EventMesh.publish("set_storage_config",["switch_bt_state","1"]) - EventMesh.publish("send_bt_switch_toble","1") - pass - print("recv switch_bt_state{}".format(msg)) - - EventMesh.publish(WEB_CMD_ENUM.SWITCH_BT_STATE_S_CMD,s_state) - -#蓝牙扫描 -class IOTWeb_bt_scanProtoCol(ProtoCol): - """bt_scan""" - TYPE = "bt_scan" - - def send(self, event, msg): - print("send schedule{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - s_msg = "" - if msg == "1": - s_msg = "开始扫描附近设备" - elif msg == "0": - s_msg = "停止扫描附近设备" - bt_scan = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"bt_scan", - "rest":"success", - "msg":s_msg - } - } - send_data = b'' - send_data = protocol_send_total_data(bt_scan) - print("send_data bt_scan{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发bt_scan - @param msg: 接收到的下行数据 - """ - # { - # "cmd":"bt_scan", - # "ts":1738283829, - # "msg":"开始扫描附近设备/停止扫描附近设备", - # "data":"0/1" - # } - s_ret = "1" - if "data" in msg: - data = msg["data"] - if data == "1": - s_ret = "1" - EventMesh.publish("send_ble_scan","1") - elif data == "0": - s_ret = "0" - EventMesh.publish("send_ble_scan","0") - - pass - - print("recv bt_scan{}".format(msg)) - EventMesh.publish(WEB_CMD_ENUM.BT_SCAN_S_CMD,s_ret) - -#蓝牙当前状态 -class IOTWeb_get_bt_stateProtoCol(ProtoCol): - """get_bt_state""" - TYPE = "get_bt_state" - - def send(self, event, msg): - print("send schedule{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - bt_state ={ - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"get_bt_state", - "rest":msg, - "msg":"上报蓝牙开关状态" - } - } - send_data = b'' - send_data = protocol_send_total_data(bt_state) - print("send_data bt_state{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发bt_state - @param msg: 接收到的下行数据 - """ - print("recv bt_state{}".format(msg)) - switch_bt_state = EventMesh.publish("get_storage_config","switch_bt_state") - if switch_bt_state == None: - switch_bt_state = "1" - - EventMesh.publish(WEB_CMD_ENUM.GET_BT_STATE_S,str(switch_bt_state)) - -#蓝牙扫描列表 -class IOTWeb_bt_scan_restProtoCol(ProtoCol): - """bt_scan_rest""" - TYPE = "bt_scan_rest" - - def send(self, event, msg): - print("send bt_scan_rest{}".format(msg)) - # 通过websocket发送到服务器端 - - send_data = b'' - send_data = protocol_send_total_data(msg) - print(" bt_scan_rest{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发bt_scan_rest - @param msg: 接收到的下行数据 - """ - print("recv bt_scan_rest{}".format(msg)) - - -#连接目标蓝牙设备 -class IOTWeb_connect_btProtoCol(ProtoCol): - """connect_bt""" - TYPE = "connect_bt" - - def send(self, event, msg): - print("send connect_bt{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - s_ret = "success" - if msg == 0: - s_ret = "error" - connect_bt = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"connect_bt", - "rest":s_ret, - "msg":"收到蓝牙连接请求" - } - } - send_data = b'' - send_data = protocol_send_total_data(connect_bt) - print("send_data bt_state{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发connect_bt - @param msg: 接收到的下行数据 - """ - # { - # "cmd":"connect_bt", - # "ts":1738283829, - # "msg":"连接蓝牙设备", - # "data":"目标设备mac地址" - # } - ok = 1 - if "data" in msg: - mac = msg["data"] - # mac = "36:72:cc:e6:dd:e8"#deep - # mac = "61:E8:6D:CD:7F:17" #Q36 - # mac = "75:E8:8C:62:9A:DF" Fineblue Clip - if len(mac) == 17: - ok = 1 - EventMesh.publish("send_connect_bt_mac",mac) - else: - ok = 0 - EventMesh.publish(WEB_CMD_ENUM.CONNECT_BT_S,ok) - print("recv connect_bt{}".format(msg)) -#上报连接记录 -class IOTWeb_bt_connect_record_listProtoCol(ProtoCol): - """bt_connect_record_list""" - TYPE = "bt_connect_record_list" - - def send(self, event, msg): - print("send bt_connect_record_list{}".format(msg)) - # 通过websocket发送到服务器端 - send_data = b'' - send_data = protocol_send_total_data(msg) - print("send_data bt_state{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发bt_connect_record_list - @param msg: 接收到的下行数据 - """ - # { - # "cmd":"post_rest", - # "ts":1738283829, - # "msg":"消息收到响应", - # "data":{ - # "cmd":"bt_connect_record_list", - # "rest":"success|error", - # "msg":"收到蓝牙连接记录列表" - # } - # } - print("recv bt_connect_record_list{}".format(msg)) -#断开设备蓝牙连接 -class IOTWeb_disconnect_btProtoCol(ProtoCol): - """disconnect_bt""" - TYPE = "disconnect_bt" - - def send(self, event, msg): - print("send disconnect_bt{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - disconnect_bt = { - "cmd":"dispatch_rest", - "ts":time_tmp, - "msg":"消息收到响应", - "data":{ - "cmd":"disconnect_bt", - "rest":"success", - "msg":"断开蓝牙设备连接请求" - } - } - send_data = b'' - send_data = protocol_send_total_data(disconnect_bt) - print("send_data bt_state{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发disconnect_bt - @param msg: 接收到的下行数据 - """ - #{ - # 'msg': '断开设备蓝牙', - # 'data': 'AB:12:34:56:78', - # 'ts': 1754555005787, - # 'cmd': 'disconnect_bt'} - if "data" in msg: - mac = msg["data"] - EventMesh.publish("send_disconnect_bt_mac",mac) - print("recv disconnect_bt{}".format(msg)) - EventMesh.publish(WEB_CMD_ENUM.DISCONNECT_BT_S) -class IOTWeb_get_chat_configProtoCol(ProtoCol): - """get_chat_config""" - TYPE = "get_chat_config" - - def send(self, event, msg): - # print("send get_chat_config{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - get_chat_config = { - "cmd":"get_chat_config", - "ts":time_tmp, - "msg":"获取AI对话配置", - "data":None - } - send_data = b'' - send_data = protocol_send_total_data(get_chat_config) - print("send_data get_chat_config{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - - @param event:上行下发get_chat_config - @param msg: 接收到的下行数据 - """ - # { - # "cmd":"post_rest", - # "ts":1738283829, - # "msg":"消息收到响应", - # "data":{ - # "cmd":"get_chat_config", - # "rest":"success|error", - # "msg":"收到获取AI对话配置请求" - # } - # } - print("RECV get_chat_config{}".format(msg)) -#IOT金三 -#接收小程序控制查找设备 -class IOTWeb_find_deviceProtoCol(ProtoCol): - """find_device""" - TYPE = "find_device" - - def send(self, event, msg): - # print("c{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - come_trans = { - "msg": "查找设备", - "data": None, - "cmd": "find_device", - "ts": time_tmp - } - send_data = b'' - send_data = protocol_send_total_data(come_trans) - print("send_data find_device{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - @param event:上行下发translate_app_enter - @param msg: 接收到的下行数据 - """ - tts_str = "我在这呀,我在这呀,我在这呀 " - EventMesh.publish("play_tts",[tts_str,1]) - -#接收小程序控制上传设备状态 -class IOTWeb_get_device_stateProtoCol(ProtoCol): - """get_device_state""" - TYPE = "get_device_state" - - def send(self, event, msg): - battery_state = (EventMesh.publish("screen_get_battery"))[0] - battery = (EventMesh.publish("screen_get_battery"))[1] - if battery >= 100 or battery == None: - battery = 100 - VC_TTS_volume = EventMesh.publish("get_storage_config","VC_TTS_volume")#音量配置 - if VC_TTS_volume == None: - VC_TTS_volume =100 - Timeversion = version_util.get_unified_version() - ts = utime.mktime(utime.localtime()) - come_trans = { - "data":{ - "version":Timeversion,#版本号 - "charge":battery_state,#0表示未充电,1表示充电中 - "currentRoleName":"",#当前音色的name - "volume":str(VC_TTS_volume),#百分比数值 - "wifiRssi":str(net.csqQueryPoll()//6),#wifi强弱:0,1,2,3,4,5。转成这几个梯度 - "battery":str(battery),#电量 - "bargein": "0",#不能手动设置打断模式 - "deviceId":modem.getDevImei(),#设备号 - }, - "cmd":"device_info", - "msg": "上报设备信息", - "ts": ts - } - send_data = b'' - send_data = protocol_send_total_data(come_trans) - print("send_data get_device_state{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - @param event:get_device_state - @param msg: 接收到的下行数据 - """ - EventMesh.publish(WEB_CMD_ENUM.GET_DEVICE_STATE_S) - -#接收小程序控制设备音量 -class IOTWeb_switch_volumeProtoCol(ProtoCol): - """switch_volume""" - TYPE = "switch_volume" - - def send(self, event, msg): - # print("c{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - come_trans = { - "msg": "设置音量", - "data": None, - "cmd": "switch_volume", - "ts": time_tmp - } - send_data = b'' - send_data = protocol_send_total_data(come_trans) - print("send_data switch_volume{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - @param event:switch_volume - @param msg: 接收到的下行数据 - """ - volume = int(msg["data"]["target"]) + 100 - print("音量修改为:{}".format(volume-100)) - EventMesh.publish("change_volume",volume) - -#接收小程序控制设置音量 -class IOTWeb_switch_volumeProtoCol(ProtoCol): - """switch_volume""" - TYPE = "switch_volume" - - def send(self, event, msg): - # print("c{}".format(msg)) - # 通过websocket发送到服务器端 - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp) - come_trans = { - "msg": "设置音量", - "data": None, - "cmd": "switch_volume", - "ts": time_tmp - } - send_data = b'' - send_data = protocol_send_total_data(come_trans) - print("send_data switch_volume{}".format(send_data)) - EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) - - def recv(self, event, msg): - """ - @param event:switch_volume - @param msg: 接收到的下行数据 - """ - volume = int(msg["data"]["target"]) + 100 - print("音量修改为:{}".format(volume-100)) - EventMesh.publish("change_volume",volume) - -class LK_ProtoCol(ProtoCol): - """链路保持""" - TYPE = "LK" - - def send(self, event, msg): - #[SZKW*334588000000156*0002*LK] - #EventMesh.debug_log('self.imei:%s'%(self.imei)) - send_data = EventMesh.publish("get_new_pid")+"*%s*0002*LK"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - # EventMesh.debug_log("LK_ProtoCol:send:%s"%(data)) - - - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - """ - - @param event: LK成功 - @param msg: 接收到的下行数据 - """ - EventMesh.debug_log("LK_ProtoCol:recv:%s"%(msg)) - EventMesh.publish(constants.PROTOCOL.PING_SEND) - EventMesh.publish(constants.PROTOCOL.KA_SEND) - EventMesh.publish(constants.PROTOCOL.TIME_SEND) - - EventMesh.publish(constants.EVENT.START_LOCATION,constants.PROTOCOL.UD_SEND) #开始UD定位 - EventMesh.publish(constants.EVENT.START_LOCATION,constants.PROTOCOL.WT_SEND) #开始WT定位 - if EventMesh.model == 'D15': - EventMesh.publish(constants.PROTOCOL.MEMBERS_SEND) - -class PING_ProtoCol(ProtoCol): - """获取绑定状态""" - TYPE = "PING" - - def send(self, event, msg): - #[SZKWT*YYYYYYYYYYYYYYY*LEN*PING] - send_data = EventMesh.publish("get_new_pid")+"*%s*0004*PING"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #EventMesh.debug_log("PING_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - """ - - @param event: PING - @param msg: 接收到的下行数据 - """ - #EventMesh.debug_log("PING_ProtoCol:recv:%s"%(msg)) - data_tmp = msg.decode('ascii') - data_tmp = data_tmp.split(']')[0] #去掉] - EventMesh.publish(constants.EVENT.SET_PING, data_tmp.split(',')[1]) - if EventMesh.model == 'D15': - EventMesh.publish(constants.PROTOCOL.JPREG_SEND) - EventMesh.publish(constants.PROTOCOL.MEMBERS_SEND) - -class KA_ProtoCol(ProtoCol): - """心跳""" - TYPE = "KA" - count = 0 - roll_count = 0 - def send(self, event, msg): - #"{:04d}-{:02d}-{:02d}".format(*utime.localtime()[0:3]) - #[XT*YYYYYYYYYYYYYYY*LEN*KA,日期,步数,翻滚次数,电量百分数,车载里程数(km)] 实例:[XT*334588000000156*0014*KA,120414,50,100,100,100.12] - date = "{:04d}{:02d}{:02d}".format(*utime.localtime()[0:3]) - date = date[2:] - #EventMesh.debug_log("date:",date) - #步数 - old_step = 0 - step = 0 - step = EventMesh.publish("get_old_step_value") - if step != None: - if step != 0: - read_data = EventMesh.publish("get_storage_config","old_step_new") - if read_data != None: - old_step = read_data - - if old_step != step: - # EventMesh.debug_log("old_step:send:%d %d"%(old_step,step)) - EventMesh.publish("set_storage_config",["old_step_new",step+10]) - EventMesh.publish("set_storage_config",["old_step_new",step]) - - old_step = step - # step = EventMesh.publish(constants.EVENT.GET_STEP)[0] - # 电池电量百分比 - - bat_tmp = EventMesh.publish("screen_get_battery") - sleep_time = EventMesh.publish("get_storage_config","sleep_time") - if sleep_time != None: - if len(sleep_time) != 0: - hour = utime.localtime()[3] - min = utime.localtime()[4] - cur_time = hour*60+min - start_hour = int(sleep_time["start_hour"]) - start_min = int(sleep_time["start_min"]) - start_total = start_hour*60+start_min - - end_hour = int(sleep_time["end_hour"]) - end_min = int(sleep_time["end_min"]) - end_total = end_hour*60+end_min - # EventMesh.debug_log("ka roll_:",hour,min,cur_time,start_total,end_total) - if cur_time >= start_total and cur_time <= end_total: - self.roll_count = int(hour) - if self.roll_count == 0: - self.roll_count+=1 - else: - self.roll_count = 0 - else: - self.roll_count = 0 - else: - self.roll_count = 0 - hour = utime.localtime()[3] - min = utime.localtime()[4] - EventMesh.txt_log("ka step:{} hour:{} min:{}".format(step,hour,min)) - - battery_caption = bat_tmp[1] - send_data = "KA,%s,%d,%d,%d"%(date,step,self.roll_count,battery_caption) - send_len = len(send_data) - - send_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,send_len,send_data) - MD5_head_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,send_len,"KA") - - MD5_SIGN = self.pack_MD5_head(MD5_head_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #EventMesh.debug_log("KA_ProtoCol:send:%s"%(data)) - - - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - """ - - @param event: PING - @param msg: 接收到的下行数据 - """ - #EventMesh.debug_log("KA_ProtoCol:recv:%s"%(msg)) - -class UD_ProtoCol(ProtoCol): - """位置信息""" - TYPE = "UD" - UD_count = 0#每6次定位获取一次天气数据 - def send(self, event, msg): - #EventMesh.debug_log("UD:send:%s"%(msg)) - #"{:04d}-{:02d}-{:02d}".format(*utime.localtime()[0:3]) - #[6f62cfc40fc50173f9a7b21e87be3424IS*861217001020120*00f6*UD,220726,211210,A,22.649529,N,114.119165,E,0.28,137.4,100.0,8,24,63,119,0,00000000,2,1,460,11,30472,125667122,24,30472,218944048,19,4, - # wifi0,34:93:44:01:28:b9,-74,wifi1,34:20:03:96:35:ad,-78,wifi2,ea:b5:7b:e8:56:54,-80,wifi3,c4:49:bb:d4:d9:78,-88] - - - self.UD_count+=1 - #每6次定位获取一次天气数据 - if self.UD_count>=6: - self.UD_count = 0 - EventMesh.publish(constants.PROTOCOL.WT_SEND,msg) - - send_data ="UD,"+msg - send_len = len(send_data) - #wt_data = "%s"%(send_data) - send_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,send_len,send_data) - MD5_head_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,send_len,"UD") - - MD5_SIGN = self.pack_MD5_head(MD5_head_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - # EventMesh.debug_log("{}".format(data)) - # wt_data = EventMesh.publish("get_weather_data") - # # #如果没有天气数据,则获取天气数据 - # if wt_data != None or wt_data[0] == None : - # - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - if EventMesh.platform == "gewu": - if(msg != ""): - client = EventMesh.publish("get_mqtt_dev_connect_state") - if client != 0: - mqtt_connect = -1 - try: - mqtt_connect = client.connect() - except Exception as e: - print(e) - mqtt_connect = -1 - print("mqtt_connect:{}".format(mqtt_connect)) - if mqtt_connect == 0: - # 订阅主题 - get_device_id = EventMesh.publish(constants.EVENT.GET_IMEI) - get_deviceKey = EventMesh.publish(constants.EVENT.GET_IMEI) - get_productKey = gewu_init() - #$sys/{productKey}/{deviceKey}/ext/regist - event_msg = "$sys/{}/{}/event/pub_reply".format(get_productKey,get_deviceKey) - # client_msg = "$sys/cu1shld45y4hjr1L/866610001011658/ext/regist" - subscribe_msg = event_msg.encode('ascii') - - client.subscribe(subscribe_msg,0)#订阅事件消息消息主题 - print("Connected to dmp-mqtt.cuiot.cn, subscribed to /public/TEST/quecpython topic") - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - longitude = float(msg_tmp[3]) - latitude = float(msg_tmp[5]) - date_tmp = utime.localtime() - time_tmp = utime.mktime(date_tmp)#需要转换成毫秒级别 - updataTime = "{}".format(time_tmp*1000) - data_info = { - "messageId":"0", - "params": { - "key":"location", - "info": [ - { - "key":"longitude", - "value":longitude - }, - { - "key":"latitude", - "value":latitude - }, - { - "key":"UpdateTime", - "value":updataTime - } - ] - } - } - data_json = ujson.dumps(data_info) - event_pub_msg = "$sys/{}/{}/event/pub".format(get_productKey,get_deviceKey) - pub_msg = event_pub_msg.encode('ascii') - client.publish(pub_msg,data_json,0)#publis主题内容 - - print("Publish topic: /public/TEST/quecpython758, msg: my name is Quecpython") - - - - def recv(self, event, msg): - pass - # EventMesh.debug_log("UD_ProtoCol:recv:%s"%(msg)) - -class CR_ProtoCol(ProtoCol): - """立即定位""" - TYPE = "CR" - - def send(self, event, msg): - #[SZKWT*YYYYYYYYYYYYYYY*LEN*PING] - send_data = EventMesh.publish("get_new_pid")+"*%s*0002*CR"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #EventMesh.debug_log("CR_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - """ - - @param event: PING - @param msg: 接收到的下行数据 - """ - EventMesh.publish(constants.PROTOCOL.CR_SEND) - EventMesh.publish(constants.EVENT.START_LOCATION,constants.PROTOCOL.UD_SEND) #开始ud定位 - #EventMesh.debug_log("CR_ProtoCol:recv:%s"%(msg)) - -class AL_ProtoCol(ProtoCol): - """报警数据上报""" - TYPE = "AL" - timer_repetition_send = None - repetition_queue = None - - def send(self, event, msg): - if EventMesh.platform != "gewu": - if self.timer_repetition_send == None: - self.timer_repetition_send = osTimer() - if self.repetition_queue == None: - self.repetition_queue = Queue(100) - - - send_data ="AL,"+msg - send_len = len(send_data) - #wt_data = "%s"%(send_data) - send_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,send_len,send_data) - MD5_head_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,send_len,"AL") - - MD5_SIGN = self.pack_MD5_head(MD5_head_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - # EventMesh.debug_log("AL_ProtoCol:send:{}".format(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - if EventMesh.platform != "gewu": - self.repetition_queue.put(data)#发送入队 - self.timer_repetition_send.start(10000, 1, self.repetition_timer_cb) - EventMesh.publish("set_dev_state",0) - def recv(self, event, msg): - """ - - @param event: PING - @param msg: 接收到的下行数据 - """ - #EventMesh.publish(constants.PROTOCOL.CR_SEND) - #EventMesh.publish(constants.EVENT.START_LOCATION,constants.PROTOCOL.UD_SEND) #开始ud定位 - # print("报警数据上报:recv:%s"%(msg)) - EventMesh.debug_log("报警数据上报:recv:{}".format(msg)) - # - if EventMesh.platform != "gewu": - if self.repetition_queue.empty() == False: - self.repetition_queue.get()#出队 - else: - self.timer_repetition_send.stop() - - def repetition_timer_cb(self, *args, **kwargs):#每隔10秒发送一次 - if self.repetition_queue.empty() == False: - data = self.repetition_queue.get()#出队 - self.repetition_queue.put(data)#发送入队 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - else: - self.timer_repetition_send.stop() - -class FIND_ProtoCol(ProtoCol): - """查找设备""" - TYPE = "FIND" - - def send(self, event, msg): - #[SZKWT*YYYYYYYYYYYYYYY*LEN*PING] - send_data = EventMesh.publish("get_new_pid")+"*%s*0004*FIND"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #EventMesh.debug_log("FIND_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - """ - - @param event: FIND,播放铃声 - @param msg: 接收到的下行数据 - """ - EventMesh.publish(constants.PROTOCOL.FIND_SEND) - # a = audio.Audio(0) - # a.set_pa(Pin.GPIO29) - # a.setVolume(10) - # a.play(1, 1, 'E:/audio_find_haixiu.mp3') #文件名前面要加上路径 - - # EventMesh.publish("load_screen", {"screen": "FIND"}) - EventMesh.publish("OPEN_FIND_WATCH")# - #EventMesh.debug_log("FIND_ProtoCol:recv:%s"%(msg)) - -class POWEROFF_ProtoCol(ProtoCol): - """关机""" - TYPE = "POWEROFF" - - def send(self, event, msg): - # [SZKW*867387060319491*0008*POWEROFF] - send_data = EventMesh.publish("get_new_pid")+"*%s*0008*POWEROFF"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #EventMesh.debug_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - """ - - @param event: POWEROFF关机 - @param msg: 接收到的下行数据 - """ - EventMesh.publish(constants.PROTOCOL.POWEROFF_SEND) - #EventMesh.debug_log("POWEROFF_ProtoCol:recv:%s"%(msg)) - battery = EventMesh.publish("screen_get_battery") - if battery != None: - if battery[0] != "1": - EventMesh.publish("open_power_on", 2)# #远程关机 - -class PHL_ProtoCol(ProtoCol): - """电话本同步""" - TYPE = "PHL" - - - def send(self, event, msg): - # [SZKW*867387060319491*0008*PHL] - send_data = EventMesh.publish("get_new_pid")+"*%s*0003*PHL"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #EventMesh.debug_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - """ - - @param event: 电话本同步 - @param msg: 接收到的下行数据 - """ - msg = msg[1:-1] - # EventMesh.debug_log("phonebook:msg:%s\r\n"%msg) - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - # EventMesh.debug_log("phonebook:len:{}\r\n data:{}".format(len(msg_tmp),msg_tmp)) - - phonebook = {} - msg_tmp = msg_tmp[1:] - - j = 0 - i = 0 - phonebook_oldtmp = {} - # 0 1 2 3 - for phonebook_tmp in msg_tmp: - j+=1 - # EventMesh.debug_log("phonebook_tmp:{} len:{}".format(phonebook_tmp,len(phonebook_tmp))) - - if (j%2) == 0: - i+=1 - if len(phonebook_tmp) != 0: - phonebook_name = UnicodeHexStr_To_Utf8Str(phonebook_tmp) - phonebook_name = "{}:{}".format("%03d"%i,phonebook_name) - phonebook[phonebook_name] = phonebook_oldtmp - - phonebook_oldtmp = phonebook_tmp - # EventMesh.debug_log("phonebook_oldtmp!!!:{}".format(str(phonebook_oldtmp))) - # EventMesh.debug_log("phonebook!!!:{}".format(str(phonebook))) - with open("/usr/static/storage_file/phonebook.json","w",encoding="utf-8") as fd: - ujson.dump(phonebook,fd) - EventMesh.debug_log("创建电话簿") - - EventMesh.publish(constants.PROTOCOL.PHL_SEND) - EventMesh.publish("set_phonebook_list",[1,phonebook]) - EventMesh.publish("contact_updata_list_show") - - -class WT_ProtoCol(ProtoCol): - """天气数据""" - TYPE = "WT" - WT_T = 0 - - def send(self, event, msg): - # [SZKW*867387060319491*0008*WT] - # if self.WT_T != 0: - # tmp_WT = - if(msg != ""): - msg_tmp = "WT," + msg - data_len = len(msg_tmp) - MD5_head_data = EventMesh.publish("get_new_pid")+"*%s*%04X*WT"%(self.imei,data_len) - MD5_SIGN = self.pack_MD5_head(MD5_head_data) - send_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,data_len,msg_tmp) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - EventMesh.debug_log("WT_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - """ - - @param event: T天气温度数据下发 - @param msg: 接收到的下行数据 - """ - # b'SZKW*860132060004322*0035*WT,23-04-06,14:41:06,b6e0d4c6,1,27,-1,40,6DF157335E02' - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split('WT,') - # EventMesh.debug_log("phonebook:len:{}\r\n data:{}".format(len(msg_tmp),msg_tmp)) - msg_tmp = str(msg_tmp[1]) - - # EventMesh.debug_log("msg_tmp :%s"%(msg_tmp)) - - msg_date = msg_tmp.split(',')[0] - - msg_time = msg_tmp.split(',')[1] - msg_weather_content = msg_tmp.split(',')[2] - msg_type = msg_tmp.split(',')[3] - msg_temp = msg_tmp.split(',')[4] - msg_min_temp = msg_tmp.split(',')[5] - msg_max_temp = msg_tmp.split(',')[6] - msg_city = msg_tmp.split(',')[7] - - msg_city = UnicodeHexStr_To_Utf8Str(msg_city) - msg_weather_content = UnicodeHexStr_To_Utf8Str(msg_weather_content) - # EventMesh.debug_log("msg_date:{} msg_time:{} msg_weather_content:{} msg_type:{} msg_temp:{} msg_min_temp:{} msg_max_temp:{} msg_city:{}".format(msg_date,msg_time,\ - # msg_weather_content,msg_type,msg_temp,msg_min_temp,msg_max_temp,msg_city)) - EventMesh.debug_log("WT_ProtoCol RECV msg:%s"%(msg)) - - EventMesh.publish("set_weather_data",[msg_date,msg_time,msg_weather_content,msg_type,msg_temp,msg_min_temp,msg_max_temp,msg_city,1]) - EventMesh.publish("updata_weather_show") -class REMIND_ProtoCol(ProtoCol):#普通闹钟 - """ - - 平台发送: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND,,时间1-开关,时间2-开关,周几] - 实例:[XT*334588000000156*0008*REMIND,,时间1-开关,时间2-开关,周几] - 终端回复: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND] - """ - TYPE = "REMIND" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*0006*REMIND"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #Wechat_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - # SZKW*860132060346475*0013*REMIND_CH_MEDICINE2,4-1 - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp_new = str(msg_tmp.split('REMIND,')[1]) - msg_tmp_data = str(msg_tmp.split('REMIND')[1]) - count = 0 - for i in str(msg_tmp_data): - # print("i",i) - if i == ",": - count+=1 - - normal_alarm_save = [] - # # ("handler -> b'[SZKW*860132060346475*001C*REMIND,08:00-1-1,,]'",) - # b'[SZKW*860132060346475*001B*REMIND,08:00-1-1,,]'",) - # medicine_alarmday = str(msg_data[4]) - # alarmday=[] - alarm_day = [] - alarm_switch = 0 - - print("count",count,msg_tmp_new) - if count >= 3: - count = 3 - for i in range(0,count):#默认是三个时间多了也不保存 - alarm_day = [] - alarm_data = str(msg_tmp_new.split(',')[i]) - # print("i",i,sleep_data) - if len(alarm_data) != 0: - b = alarm_data.find('-') - if b != -1: - alarm_time = alarm_data.split('-')[0]#-作为分隔符 - alarm_switch = int(alarm_data.split('-')[1])#开关 - alarm_repeat = int(alarm_data.split('-')[2])#是单次还是每天 还是自定义 - - alarm_hour = int(alarm_time.split(":")[0])#时间 - alarm_min = int(alarm_time.split(":")[1])#冒号分隔时间 时 分 - - - if alarm_repeat == 2:#每一天 - alarm_day = [0,1,2,3,4,5,6] - elif alarm_repeat == 3:#自定义 - alarm_day_tmp = alarm_data.split('-')[3]#-作为分隔符 - if len(str(alarm_day_tmp)) != 0: - for i in range(0,7): - if alarm_day_tmp[i] != '0': - alarm_day.append(i) - else:#单次 1 - alarm_day=[] - if alarm_switch != 0: - normal_alarm_save.append({"hour":alarm_hour,"min":alarm_min,"day":alarm_day,"enable":alarm_switch}) - - EventMesh.publish("set_storage_config",["normal_alarm",normal_alarm_save]) - # normal_alarm = EventMesh.publish("get_storage_config","normal_alarm") - - # EventMesh.debug_log("recv REMIND_SEND:{} normal_alarm ;{}".format(msg,normal_alarm)) - - EventMesh.publish(constants.PROTOCOL.REMIND_SEND) - EventMesh.publish("clear_normal_alarm") - - - - - - - -class HRSTART_ProtoCol(ProtoCol): - """心率频率数据""" - TYPE = "hrtstart" - - - def send(self, event, msg): - # [SZKW*867387060319491*0008*hrtstart] - send_data = EventMesh.publish("get_new_pid")+"*%s*0008*hrtstart"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - # EventMesh.debug_log("HRSTART_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: 心率数据下发 - @param msg: 接收到的下行数据 - """ - # b'SZKW*860132060004322*0035*REMIND,23-04-06,14:41:06,b6e0d4c6,1,27,-1,40,6DF157335E02' - # EventMesh.debug_log("REMIND_ProtoCol RECV msg:%s"%(msg)) - # HRSTART_ProtoCol RECV msg:b'SZKW*860132060004322*000D*hrtstart,7200' msg_:7200 - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - msg_tmp = msg_tmp[1] - EventMesh.debug_log("HRSTART_ProtoCol RECV msg:{} msg_:{}".format(msg,msg_tmp)) - hrTotal = int(msg_tmp) - if hrTotal == 1:#单次测量 - EventMesh.debug_log("HRSTART_ 2".format(hrTotal)) - get_hr_state = EventMesh.publish("get_hr_state") - get_spo2_state = EventMesh.publish("get_spo2_state") - get_bp_state = EventMesh.publish("get_bp_state") - get_bg_state = EventMesh.publish("get_bg_state") - # if (get_hr_state == 3 ) or (get_spo2_state == 0 and get_hr_state == 0):#只要两个不在测量 单次优先级最低 - if (get_spo2_state == 0 and get_hr_state == 0 and get_bg_state == 0 and get_bp_state == 0): - battery = EventMesh.publish("screen_get_battery") - if battery != None: - if battery[0] != "1": - EventMesh.publish("set_hr_state",3)#3代表单次测量 - pass - else: - if hrTotal > 0 and hrTotal <= 300: - hrTotal = 300 - elif hrTotal >= 65536: - hrTotal = 65536 - EventMesh.debug_log("HRSTART_ 3".format(hrTotal)) - on = 0 - if hrTotal != 0: - on = 1 - EventMesh.publish("set_time_hr",[on,hrTotal]) - - - EventMesh.publish(constants.PROTOCOL.HRTSTART_SEND) - -class HEART_ProtoCol(ProtoCol): - """心率数据""" - TYPE = "heart" - - - def send(self, event, msg): - # [SZKW*334588000000156*len*heart,x] - # EventMesh.debug_log("HEART:send:{}".format(msg)) - len_size = len(str(msg))+len("heart,") - send_data = EventMesh.publish("get_new_pid")+"*%s*%04X*heart"%(self.imei,len_size) - MD5_head_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,len_size,"heart") - MD5_SIGN = self.pack_MD5_head(MD5_head_data) - data = "[%s%s,%d]"%(str(MD5_SIGN),send_data,msg) - # EventMesh.debug_log("HEART_ProtoCol:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: 心率数据下发 - @param msg: 接收到的下行数据 - """ - # EventMesh.debug_log("HRSTART_ProtoCol RECV msg:") - #EventMesh.publish(constants.PROTOCOL.HEART_SEND) -class OXYGEN_ProtoCol(ProtoCol): - """上传血氧测试数据""" - TYPE = "oxygen" - def send(self, event, msg): - # [SZKW*334588000000156*len*heart,x] - # EventMesh.debug_log("oxstart:send:{}".format(msg)) - len_size = len(str(msg))+len("oxygen,") - send_data = EventMesh.publish("get_new_pid")+"*%s*%04X*oxygen"%(self.imei,len_size) - MD5_head_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,len_size,"oxygen") - MD5_SIGN = self.pack_MD5_head(MD5_head_data) - data = "[%s%s,%d]"%(str(MD5_SIGN),send_data,msg) - # EventMesh.debug_log("HEART_ProtoCol:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: 心率数据下发 - @param msg: 接收到的下行数据 - """ - # EventMesh.debug_log("HRSTART_ProtoCol RECV msg:") - #EventMesh.publish(constants.PROTOCOL.OXYGEN_SEND) - -class OXYGEN_One_ProtoCol(ProtoCol):#单次测量 - """上传单次测量测试数据""" - TYPE = "oxstart" - -# 终端发送[XT*YYYYYYYYYYYYYYY*LEN*oxygen,x] -# x 表示血氧饱和度 -# 示例[XT*334588000000156*0009*oxygen,15] -# 平台回复:handler -> b'[SZKW*860132060004322*0009*oxstart,1]' -# [XT*334588000000156*0006*oxygen] - def send(self, event, msg): - # [SZKW*334588000000156*len*heart,x] - EventMesh.debug_log("oxstart:") - send_data = EventMesh.publish("get_new_pid")+"*%s*0007*oxstart"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - # EventMesh.debug_log("HEART_ProtoCol:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: 血氧单次测量下发 - @param msg: 接收到的下行数据 - """ - EventMesh.debug_log("oxstart:{}".format(msg)) - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - msg_tmp = msg_tmp[1] - - oxstartTotal = int(msg_tmp) - # oxstartTotal = 300 - print("oxstartTotal:{}".format(oxstartTotal)) - if oxstartTotal == 1:#单次测量 - get_bp_state = EventMesh.publish("get_bp_state") - get_hr_state = EventMesh.publish("get_hr_state") - get_spo2_state = EventMesh.publish("get_spo2_state") - get_bg_state = EventMesh.publish("get_bp_state") - print("get_bp_state:{}".format(get_bp_state)) - if get_bp_state == 0 and get_hr_state == 0 and get_spo2_state == 0 and get_bg_state == 0:#只要两个不在测量 单次优先级最低 - battery = EventMesh.publish("screen_get_battery") - if battery != None: - if battery[0] != "1": - EventMesh.publish("set_spo2_state",3)#3代表单次测量 - else: - if oxstartTotal > 0 and oxstartTotal <= 300: - oxstartTotal = 300 - elif oxstartTotal >= 65536: - oxstartTotal = 65536 - - # if oxstartTotal == 0:#关闭自动测量 - # EventMesh.debug_log("bpSTART_ 1") - # EventMesh.publish("set_spo2_time",[0,0]) - # else: - on = 0 - if oxstartTotal != 0: - on = 1 - EventMesh.publish("set_spo2_time",[on,oxstartTotal]) - - EventMesh.publish(constants.PROTOCOL.OXSTART_SEND) - -class TEMP_ProtoCol(ProtoCol): - """上传体温测试数据""" - TYPE = "temp" - def send(self, event, msg): - # [SZKW*334588000000156*len*heart,x] - # EventMesh.debug_log("temp:send:{}".format(msg)) - len_size = len(str(msg[0]))+len("temp,")+1+len(str(msg[1])) - EventMesh.debug_log("temp:send:{} len:{}".format(msg,len_size)) - # 实例:[XT*334588000000156*0009*temp,36.2] - # 代表终端的温度为:36.2 - # 平台回复: - # [XT*334588000000156*0004*temp] - send_data = EventMesh.publish("get_new_pid")+"*%s*%04X*temp"%(self.imei,len_size) - MD5_head_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,len_size,"temp") - MD5_SIGN = self.pack_MD5_head(MD5_head_data) - data = "[%s%s,%d.%d]"%(str(MD5_SIGN),send_data,msg[0],msg[1]) - # EventMesh.debug_log("temp:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # EventMesh.debug_log("HRSTART_ProtoCol RECV msg:") - #EventMesh.publish(constants.PROTOCOL.TEMP_SEND) - -class WDSTART_ProtoCol(ProtoCol): - """上传体温频率""" - TYPE = "wdstart" - def send(self, event, msg): - #[XT*334588000000156*len*wdstart,300] - send_data = EventMesh.publish("get_new_pid")+"*%s*0007*wdstart"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - # EventMesh.debug_log("temp:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: 体温下发频率数据 - @param msg: 接收到的下行数据 - """ - # EventMesh.debug_log("WDSTART_ProtoCol RECV msg:") - EventMesh.debug_log("wdstart:{}".format(msg)) - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - msg_tmp = msg_tmp[1] - - tempTotal = int(msg_tmp) - EventMesh.debug_log("tempTotal:{}".format(tempTotal)) - if tempTotal == 1:#单次测量 - get_temp_state = EventMesh.publish("get_temp_state") - EventMesh.debug_log("get_temp_state:{}".format(get_temp_state)) - if get_temp_state == 0:#只要两个不在测量 单次优先级最低 - battery = EventMesh.publish("screen_get_battery") - if battery != None: - if battery[0] != "1": - EventMesh.publish("set_temp_state",3)#3代表单次测量 - else: - if tempTotal > 0 and tempTotal <= 300: - tempTotal = 300 - elif tempTotal >= 65536: - tempTotal = 65536 - - EventMesh.publish("set_temp_time",tempTotal) - - - - EventMesh.publish(constants.PROTOCOL.WDSTART_SEND) -#解绑 -# 设置血压上传频率 -# 平台发送[XT*YYYYYYYYYYYYYYY*LEN*bldstart,x] -# x为上传间隔时间,单位秒,连续上传时最小时间不小于300秒,最大不超过65535. -# 示例[XT*334588000000156*0008*bldstart,300] -# 终端回复: -# [XT*334588000000156*0005*bldstart] -# x为1则代表终端单次上传。 -# x为0则代表终端上传关闭。 -class bldstart_ProtoCol(ProtoCol): - """app下发血压测试频率""" - TYPE = "bldstart" - def send(self, event, msg): - #[XT*334588000000156*len*wdstart,300] - send_data = EventMesh.publish("get_new_pid")+"*%s*0008*bldstart"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - # print("temp:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: 体温下发频率数据 - @param msg: 接收到的下行数据 - """ - # print("WDSTART_ProtoCol RECV msg:") - print("bldstart:{}".format(msg)) - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - msg_tmp = msg_tmp[1] - - bldstartTotal = int(msg_tmp) - # bldstartTotal = 300 - print("bldstartTotal:{}".format(bldstartTotal)) - if bldstartTotal == 1:#单次测量 - get_bp_state = EventMesh.publish("get_bp_state") - get_hr_state = EventMesh.publish("get_hr_state") - get_spo2_state = EventMesh.publish("get_spo2_state") - get_bg_state = EventMesh.publish("get_bg_state") - print("get_bp_state:{}".format(get_bp_state)) - if get_bp_state == 0 and get_hr_state == 0 and get_spo2_state == 0 and get_bg_state == 0:#只要两个不在测量 单次优先级最低 - battery = EventMesh.publish("screen_get_battery") - if battery != None: - if battery[0] != "1": - EventMesh.publish("set_bp_state",3)#3代表单次测量 - else: - if bldstartTotal > 0 and bldstartTotal <= 300: - bldstartTotal = 300 - elif bldstartTotal >= 65536: - bldstartTotal = 65536 - - on = 0 - if bldstartTotal != 0:#关闭自动测量 - on = 1 - print("on:{} bldstartTotal:{} ".format(on,bldstartTotal)) - EventMesh.publish("set_bp_time",[on,bldstartTotal]) - - EventMesh.publish(constants.PROTOCOL.BLDSTART_SEND) - - -# 注:回复完后开始测量血压并上传(一般测一次90秒,app端会提醒用户两分钟后刷新页面查看) -# 上传血压数据 -# 终端发送[XT*YYYYYYYYYYYYYYY*LEN*blood,x,y] -# x 是收缩压(高压) -# y 是舒张压(低压) -# 示例[XT*334588000000156*000C*blood,150,70] -# 平台回复: -# [XT*334588000000156*0005*blood] -class blood_ProtoCol(ProtoCol): - """上传血压测试数据""" - TYPE = "blood" - def send(self, event, msg): - len_size = len(str(msg[0]))+len("blood,")+1+len(str(msg[1])) - print("blood:send:{} len:{}".format(msg,len_size)) - # 实例:[XT*334588000000156*0009*blood,36,2] - # 代表终端的 - # 平台回复: - # [XT*334588000000156*0004*blood] - send_data = EventMesh.publish("get_new_pid")+"*%s*%04X*blood"%(self.imei,len_size) - MD5_head_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,len_size,"blood") - MD5_SIGN = self.pack_MD5_head(MD5_head_data) - data = "[%s%s,%d,%d]"%(str(MD5_SIGN),send_data,msg[0],msg[1]) - print("blood:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # print("HRSTART_ProtoCol RECV msg:") - #EventMesh.publish(constants.PROTOCOL.TEMP_SEND) - - - - -# 设置血糖上传频率 -# 平台发送: -# [XT*334588000000156*len*bgstart,x] -# x为上传间隔时间,单位秒,连续上传时最小时间不小于300秒,最大不超过65535. -# 实例:[XT*334588000000156*len*bgstart,300] -# 终端会每隔300秒检测一次 - -# x为1则代表终端单次上传。 -# x为0则代表终端上传关闭。 -class bgstart_ProtoCol(ProtoCol): - """app下发血糖测试频率""" - TYPE = "bgstart" - def send(self, event, msg): - #[XT*334588000000156*len*wdstart,300] - send_data = EventMesh.publish("get_new_pid")+"*%s*0007*bgstart"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - # print("temp:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: 体温下发频率数据 - @param msg: 接收到的下行数据 - """ - # print("WDSTART_ProtoCol RECV msg:") - print("bgstart:{}".format(msg)) - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - msg_tmp = msg_tmp[1] - - bgstartTotal = int(msg_tmp) - print("bgstartTotal:{}".format(bgstartTotal)) - if bgstartTotal == 1:#单次测量 - get_bp_state = EventMesh.publish("get_bp_state") - get_hr_state = EventMesh.publish("get_hr_state") - get_spo2_state = EventMesh.publish("get_spo2_state") - get_bg_state = EventMesh.publish("get_bg_state") - print("get_bg_state:{}".format(get_bg_state)) - if get_bp_state == 0 and get_hr_state == 0 and get_spo2_state == 0 and get_bg_state == 0:#只要两个不在测量 单次优先级最低 - battery = EventMesh.publish("screen_get_battery") - if battery != None: - if battery[0] != "1": - EventMesh.publish("set_bg_state",3)#3代表单次测量 - else: - if bgstartTotal > 0 and bgstartTotal <= 300: - bgstartTotal = 300 - elif bgstartTotal >= 65536: - bgstartTotal = 65536 - - # if bgstartTotal == 0:#关闭自动测量 - # EventMesh.debug_log("bgSTART_ 1") - # EventMesh.publish("set_bgauto_time",[0,0]) - # else: - on = 0 - if bgstartTotal != 0: - on = 1 - EventMesh.publish("set_bgauto_time",[on,bgstartTotal]) - - EventMesh.publish(constants.PROTOCOL.BGSTART_SEND) - -# 上传血糖数据 -# 终端发送[XT*YYYYYYYYYYYYYYY*LEN*bloodglucose,x] -# x 表示血糖,单位mmol/L -# 示例[XT*334588000000156*0010*bloodglucose,6.1] -# 平台回复: -# [XT*334588000000156*000B*bloodglucose] -class bloodglucose_ProtoCol(ProtoCol): - """上传血糖测试数据""" - TYPE = "bloodglucose" - def send(self, event, msg): - len_size = len("bloodglucose,")+1+len(str(msg)) - print("bloodglucose:send:{} len:{}".format(msg,len_size)) - # 实例:[XT*334588000000156*0009*blood,36,2] - # 代表终端的 - # 平台回复: - # [XT*334588000000156*0004*blood] - send_data = EventMesh.publish("get_new_pid")+"*%s*%04X*bloodglucose"%(self.imei,len_size) - MD5_head_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,len_size,"bloodglucose") - MD5_SIGN = self.pack_MD5_head(MD5_head_data) - data = "[%s%s,%s]"%(str(MD5_SIGN),send_data,msg) - print("bloodglucose:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # print("HRSTART_ProtoCol RECV msg:") - #EventMesh.publish(constants.PROTOCOL.TEMP_SEND) - -# [XT*334588000000156*0003*DLT] -def reset_factory(msg): - if msg == 1: - Power.powerRestart() -class DLT_ProtoCol(ProtoCol): - """解绑数据""" - TYPE = "DLT" - def send(self, event, msg): - # [XT*334588000000156*0004*temp] - send_data = EventMesh.publish("get_new_pid")+"*%s*0003*DLT"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - EventMesh.debug_log("temp:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # EventMesh.debug_log("DLT_ProtoCol RECV msg:") - - -#定位频率 -class UPLOAD_ProtoCol(ProtoCol): - """定位频率数据""" - TYPE = "UPLOAD" - def send(self, event, msg): - # [XT*334588000000156*0004*temp] - send_data = EventMesh.publish("get_new_pid")+"*%s*0006*UPLOAD"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # b'[SZKW*867387060319166*000A*UPLOAD,600]' - # EventMesh.debug_log("DLT_ProtoCol RECV msg:".format(msg)) - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - msg_tmp = msg_tmp[1] - upload_time = int(msg_tmp) - EventMesh.publish("set_storage_config",["upload_time",upload_time]) - - EventMesh.publish("set_upload_data", upload_time) - EventMesh.publish(constants.PROTOCOL.UPLOAD_SEND) - -#设置快捷号码 -class CENTER_ProtoCol(ProtoCol): - """设置快捷号码""" - TYPE = "CENTER" - def send(self, event, msg): - # [XT*334588000000156*0004*temp] - send_data = EventMesh.publish("get_new_pid")+"*%s*0006*CENTER"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - EventMesh.debug_log("UPLOAD:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # b'[SZKW*867387060319166*000A*UPLOAD,600]' - # EventMesh.debug_log("DLT_ProtoCol RECV msg:".format(msg)) - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - msg_tmp = msg_tmp[1] - EventMesh.debug_log("CENTER_ProtoCol RECV msg:{} msg_:{}".format(msg,msg_tmp)) - center_dial = str(msg_tmp) - EventMesh.publish("set_storage_config",["center_dial",center_dial]) - EventMesh.publish(constants.PROTOCOL.CENTER_SEND) - -#呼叫快捷电话号码 - -class MONITOR_ProtoCol(ProtoCol): - """呼叫快捷号码""" - TYPE = "MONITOR" - def send(self, event, msg): - # [XT*334588000000156*0004*MONITOR] - send_data = EventMesh.publish("get_new_pid")+"*%s*0007*MONITOR"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - EventMesh.debug_log("MONITOR:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # b'[SZKW*867387060319166*000A*MONITOR]' - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split('MONITOR') - msg_tmp = msg_tmp[1] - EventMesh.debug_log("MONITOR_ProtoCol RECV msg:{} msg_:{}".format(msg,len(msg_tmp))) - if len(msg_tmp) != 0: - EventMesh.publish("save_phonename", contact) - EventMesh.publish("save_phonenum", contact) - EventMesh.publish("dial_out_call_center_cb",contact)#电话拨出去 - else: - read_data = EventMesh.publish("get_storage_config","center_dial") - EventMesh.debug_log("MONITOR:data:%s"%(read_data)) - if read_data != None: - contact = read_data - if len(contact) > 0: - EventMesh.debug_log("MONITOR RECV msg:{} {}".format(contact,type(contact))) - EventMesh.publish("save_phonename", contact) - EventMesh.publish("save_phonenum", contact) - EventMesh.publish("dial_out_call_center_cb",contact)#电话拨出去 - EventMesh.publish(constants.PROTOCOL.MONITOR_SEND) - -#定时开关机 -class bootoff_ProtoCol(ProtoCol): - """定时开关机""" - TYPE = "BOOTOFF" - def send(self, event, msg): - # [XT*334588000000156*0004*MONITOR] - send_data = EventMesh.publish("get_new_pid")+"*%s*0007*BOOTOFF"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - EventMesh.debug_log("BOOTOFF:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # b'[SZKW*867387060319166*000A*SOS_SEND]' - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - on_bt = int(msg_tmp[1]) - start_hour = int(str(msg_tmp[2]).split(":")[0]) - start_min = int(str(msg_tmp[2]).split(":")[1]) - - end_hour = int(str(msg_tmp[3]).split(":")[0]) - end_min = int(str(msg_tmp[3]).split(":")[1]) - - EventMesh.debug_log("BOOTOFF:send data:",on_bt,start_hour,start_min,end_hour,end_min) - poweroff = [] - poweroff.append({"poweroff_on":on_bt,"start_hour":start_hour,"start_min":start_min,"end_hour":end_hour,"end_min":end_min}) - #b'[SZKW*860132060006970*0015*BOOTOFF,1,06:00,23:59]' - EventMesh.publish("set_boot_off_time",poweroff)#设置关机时间 - - EventMesh.publish(constants.PROTOCOL.BOOTOFF_SEND) - - -#sos家庭号码 -class sosHome_dial_ProtoCol(ProtoCol): - """sos家庭号码""" - TYPE = "SOS" - def send(self, event, msg): - # [XT*334588000000156*0004*MONITOR] - send_data = EventMesh.publish("get_new_pid")+"*%s*0003*SOS"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - print("sosHome_dial_ProtoCol:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - - # b'[SZKW*867387060319166*000A*SOS_SEND]' - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - - # print("sosHome_dial_ProtoCol:send data :{} {}".format(len(msg_tmp),msg_tmp)) - #b'[SZKW*860132060006970*0015*sosHome_dial_ProtoCol,15521469561,10086,15521469561]' - i = 0 - num = 0 - count = 0 - print("sos msg_tmp",msg_tmp) - if len(msg_tmp) > 4: - num = 3 - else: - num = len(msg_tmp)-1 - data = [] - for i in range(num): - # print("sos:send data :{}".format(msg_tmp[i+1])) - if len(msg_tmp[i+1]) != 0: - EventMesh.publish("set_storage_config",["SOS_NUMBER{}".format(i+1),msg_tmp[i+1]]) - data.append(str(msg_tmp[i+1])) - count+=1 - print("data:{}".format(data)) - EventMesh.publish("set_dial_sos_number",data) - - EventMesh.publish(constants.PROTOCOL.SOS_SEND) -# 低电提醒 -#[XT*334588000000156*0008*LOWBAT,1] -class LOWBAT_ProtoCol(ProtoCol): - """低电提醒""" - TYPE = "LOWBAT" - def send(self, event, msg): - # [XT*334588000000156*0006*LOWBAT] - send_data = EventMesh.publish("get_new_pid")+"*%s*0006*LOWBAT"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - EventMesh.debug_log("LOWBAT:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # b'[SZKW*867387060319166*000A*LOWBAT]' - - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - on_bt = msg_tmp[1] - EventMesh.debug_log("LOWBAT:send data:{}".format(on_bt)) - #b'[SZKW*860132060006970*0015*LOWBAT,1,06:00,23:59]' - EventMesh.publish("set_low_bat_btn",on_bt) - EventMesh.publish(constants.PROTOCOL.LOWBAT_SEND) - -# 设置系统时间 -# -class Time_ProtoCol(ProtoCol): - """设置系统时间""" - TYPE = "Time" - def send(self, event, msg): - # - send_data = EventMesh.publish("get_new_pid")+"*%s*0004*Time"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - EventMesh.debug_log("Time:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # rtc = RTC() - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_data = msg_tmp.split(',') - - timeSecond = int(msg_data[1]) - #EventMesh.debug_log("type {} timeSecond {}".format((msg_data[1]),timeSecond)) - # timeSecond = 1703001360 - next_alarm_time = utime.localtime(timeSecond) - #EventMesh.debug_log("next_alarm_time",next_alarm_time) - #EventMesh.debug_log("local time",msg) - # machine.RTC().datetime((tm[0], tm[1], tm[2], tm[6], tm[3], tm[4], tm[5], 0)) - - machine.RTC().datetime((next_alarm_time[0],next_alarm_time[1],next_alarm_time[2],next_alarm_time[6],next_alarm_time[3],next_alarm_time[4],next_alarm_time[5],0)) - #next_alarm_time = utime.localtime(old_Timesec)#年月日时分秒星期365中的第几天 - EventMesh.publish("set_rtc_alarm") - #EventMesh.publish(constants.PROTOCOL.TIME_SEND) - - - - -#免打扰 -class SILENCETIME2_ProtoCol(ProtoCol): - """免打扰""" - TYPE = "SILENCETIME2" - def send(self, event, msg): - # [XT*334588000000156*0006*SILENCETIME2] - send_data = EventMesh.publish("get_new_pid")+"*%s*000C*SILENCETIME2"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - # EventMesh.debug_log("KEYBRD:data:%s"%(data)) - # EventMesh.debug_log("SILENCETIME2!!!!!!!!!!!!!",msg) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # b'[SZKW*867387060319166*000A*KEYBRD]' - # EventMesh.debug_log("SILENCETIME2!!!!!!!!!!!!!",msg) - EventMesh.publish(constants.PROTOCOL.SILENCETIME2_SEND) - -#键盘开关 -class KEYBRD_ProtoCol(ProtoCol): - """免打扰""" - TYPE = "KEYBRD" - def send(self, event, msg): - # [XT*334588000000156*0006*SILENCETIME2] - send_data = EventMesh.publish("get_new_pid")+"*%s*0006*KEYBRD"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - # EventMesh.debug_log("KEYBRD:data:%s"%(data)) - # EventMesh.debug_log("KEYBRD!!!!!!!!!!!!!",msg) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # b'[SZKW*867387060319166*000A*KEYBRD]' - # EventMesh.debug_log("KEYBRD!!!!!!!!!!!!!",msg) - EventMesh.publish(constants.PROTOCOL.KEYBRD_SEND) - -class TK_ProtoCol(ProtoCol): - """微聊""" - TYPE = "TK" - - - def send(self, event, msg): - EventMesh.debug_log("TK send:") - if len(str(msg)) == 1: - - len_size = len(str(msg))+len("TK,") - # 实例:[XT*334588000000156*0009*TK,1] - send_data = EventMesh.publish("get_new_pid")+"*%s*%04X*TK"%(self.imei,len_size) - MD5_head_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,len_size,"TK") - MD5_SIGN = self.pack_MD5_head(MD5_head_data) - data = "[%s%s,%d]"%(str(MD5_SIGN),send_data,msg) - EventMesh.debug_log("TK:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - else: - # [SZKW*YYYYYYYYYYYYYYY*LEN*TK,AMR 格式音频数据] - #[XT*YYYYYYYYYYYYYYY*LEN*TKS,秒数(十进制),AMR 格式音频数据] - arm_data = msg - # arm_sec = msg[0] - # EventMesh.debug_log("TK_ProtoCol:msgtype:{} arm_sec:{}".format(len(msg[1]),arm_sec)) - - arm = bytearray() - for i in range(len(arm_data)): - if arm_data[i] == 0x7d: - arm.extend(b'\x7d\x01') - elif arm_data[i] == 0x5b : - arm.extend(b'\x7d\x02') - elif arm_data[i] == 0x5d : - arm.extend(b'\x7d\x03') - elif arm_data[i] == 0x2c : - arm.extend(b'\x7d\x04') - elif arm_data[i] == 0x2a : - arm.extend(b'\x7d\x05') - else : - arm.append(arm_data[i]) - - datalen = len(arm) + len("TK,")+1 - #Wechat_log("len(str(arm_sec)):{}".format(len(str(arm_sec))+1)) - send_data = bytearray(EventMesh.publish("get_new_pid")+"*%s*%04X*TK,"%(self.imei,datalen)) - send_data.extend(arm) - MD5_head_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,datalen,"TK") - MD5_SIGN = self.pack_MD5_head(MD5_head_data) - data = bytearray('[') - data.extend(MD5_SIGN) - data.extend(send_data) - data.extend(']') - # EventMesh.debug_log("TK:send data:%s MD5_head_data:%s"%(data,MD5_head_data)) - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - - def recv(self, event, msg): - """ - - ("TK_ProtoCol:datalen:{} {} {}".format(wechat_num,MXA_MSG,wechat_recode_list)) - @param event: 微聊 - @param msg: 接收到的下行数据 - [SZKW*860132060004322*20BD*TK,#!AMR...] - """ - #Wechat_log("K_ProtoCol:recv:data:{}".format(msg)) - - msg_tmp = msg.decode('ascii') - str = msg_tmp.split('TK,') - # Wechat_log("TK_ProtoCol:recv:len:{} {} {}".format(len(msg),len(str),msg_tmp.find("#!AMR"))) - - read_AMR_flag = msg_tmp.find("#!AMR") - #EventMesh.debug_log("TK recv:read_AMR_flag:{} ,{}".format(read_AMR_flag,EventMesh.publish("get_wechat_record_flag"))) - # if EventMesh.publish("get_wechat_record_flag") != 1 and read_AMR_flag != -1: #是否是服务器下发音频数据 - if read_AMR_flag != -1: #是否是服务器下发音频数据 - # Wechat_log("TK_ProtoCol:recv:datalen{}".format(len(msg))) - arm_data = bytearray(str[1])#获取amr输出 - get_wechat_data = EventMesh.publish("get_wechat_data") - MXA_MSG = get_wechat_data[0] - wechat_recode_list = get_wechat_data[1] - wechat_num = int(wechat_recode_list["num"]) - # Wechat_log("TK_ProtoCol:datalen:{} {} {}".format(wechat_num,MXA_MSG,wechat_recode_list)) - - if wechat_num >= MXA_MSG: - wechat_num = 0 - if wechat_num == 0: - wechat_num += 1 - elif wechat_num > 0: - wechat_num += 1 - - - EventMesh.debug_log("num_total:datalen:{} {}".format(MXA_MSG,wechat_recode_list)) - wechat_recode_list["%02d"%(wechat_num)] = "read:1" - wechat_recode_list['num'] = wechat_num - EventMesh.publish("set_wechat_data",[MXA_MSG,wechat_recode_list]) - #写语音文件数据 - - # path = "/usr/static/storage_file/wechat.amr" - path = "/usr/static/storage_file/{}.amr".format(wechat_num) - with open(path,"w") as fd: - arm = bytearray() - j = 0 - for i in range(len(arm_data)): - if arm_data[j] == 0x7d and arm_data[j+1] == 0x01: - arm.extend(b'\x7d') - j+=1 - elif arm_data[j] == 0x7d and arm_data[j+1] == 0x02: - arm.extend(b'\x5b') - j+=1 - elif arm_data[j] == 0x7d and arm_data[j+1] == 0x03: - arm.extend(b'\x5d') - j+=1 - elif arm_data[j] == 0x7d and arm_data[j+1] == 0x04: - arm.extend(b'\x2c') - j+=1 - elif arm_data[j] == 0x7d and arm_data[j+1] == 0x05: - arm.extend(b'\x2a') - j+=1 - else : - arm.append(arm_data[j]) - j+=1 - if j >= len(arm_data): - break - fd.write(arm) - EventMesh.publish("recv_wechat_audio")# - EventMesh.publish(constants.PROTOCOL.TK_SEND,1) - EventMesh.debug_log("constants.PROTOCOL.TK_SEND1!!!") - else : - if read_AMR_flag != -1: - EventMesh.publish(constants.PROTOCOL.TK_SEND,0) - -class TKS_ProtoCol(ProtoCol): - """微聊""" - TYPE = "TKS" - - - def send(self, event, msg): - EventMesh.debug_log("TKS send:") - if len(str(msg)) == 1: - len_size = len(str(msg))+len("TKS,") - # 实例:[XT*334588000000156*0009*TK,1] - send_data = EventMesh.publish("get_new_pid")+"*%s*%04X*TKS"%(self.imei,len_size) - MD5_head_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,len_size,"TKS") - MD5_SIGN = self.pack_MD5_head(MD5_head_data) - data = "[%s%s,%d]"%(str(MD5_SIGN),send_data,msg) - EventMesh.debug_log("TKS:data:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - else: - # [SZKW*YYYYYYYYYYYYYYY*LEN*TK,AMR 格式音频数据] - #[XT*YYYYYYYYYYYYYYY*LEN*TKS,秒数(十进制),AMR 格式音频数据] - arm_data = msg[1] - arm_sec = msg[0] - EventMesh.debug_log("TK_ProtoCol:msgtype:{} arm_sec:{}".format(len(msg[1]),arm_sec)) - - arm = bytearray() - for i in range(len(arm_data)): - if arm_data[i] == 0x7d: - arm.extend(b'\x7d\x01') - elif arm_data[i] == 0x5b : - arm.extend(b'\x7d\x02') - elif arm_data[i] == 0x5d : - arm.extend(b'\x7d\x03') - elif arm_data[i] == 0x2c : - arm.extend(b'\x7d\x04') - elif arm_data[i] == 0x2a : - arm.extend(b'\x7d\x05') - else : - arm.append(arm_data[i]) - - datalen = len(arm) + len("TKS,")+len(str(arm_sec))+1 - #Wechat_log("len(str(arm_sec)):{}".format(len(str(arm_sec))+1)) - send_data = bytearray(EventMesh.publish("get_new_pid")+"*%s*%04X*TKS,%d,"%(self.imei,datalen,arm_sec)) - send_data.extend(arm) - MD5_head_data = EventMesh.publish("get_new_pid")+"*%s*%04X*%s"%(self.imei,datalen,"TKS") - MD5_SIGN = self.pack_MD5_head(MD5_head_data) - data = bytearray('[') - data.extend(MD5_SIGN) - data.extend(send_data) - data.extend(']') - # EventMesh.debug_log("TK:send data:%s MD5_head_data:%s"%(data,MD5_head_data)) - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - - def recv(self, event, msg): - """ - - ("TK_ProtoCol:datalen:{} {} {}".format(wechat_num,MXA_MSG,wechat_recode_list)) - @param event: 微聊 - @param msg: 接收到的下行数据 - [SZKW*860132060004322*20BD*TK,#!AMR...] - """ - #Wechat_log("K_ProtoCol:recv:data:{}".format(msg)) - # EventMesh.debug_log("TK recv:{}".format(msg)) - msg_tmp = msg.decode('ascii') - str = msg_tmp.split('TKS,') - # Wechat_log("TK_ProtoCol:recv:len:{} {} {}".format(len(msg),len(str),msg_tmp.find("#!AMR"))) - read_AMR_flag = msg_tmp.find("#!AMR") - # if EventMesh.publish("get_wechat_record_flag") != 1 and read_AMR_flag != -1: #是否是服务器下发音频数据 - if read_AMR_flag != -1: #是否是服务器下发音频数据 - # Wechat_log("TK_ProtoCol:recv:datalen{}".format(len(msg))) - arm_data = bytearray(str[1])#获取amr输出 - get_wechat_data = EventMesh.publish("get_wechat_data") - MXA_MSG = get_wechat_data[0] - wechat_recode_list = get_wechat_data[1] - wechat_num = int(wechat_recode_list["num"]) - # Wechat_log("TK_ProtoCol:datalen:{} {} {}".format(wechat_num,MXA_MSG,wechat_recode_list)) - - if wechat_num >= MXA_MSG: - wechat_num = 0 - if wechat_num == 0: - wechat_num += 1 - elif wechat_num > 0: - wechat_num += 1 - - - EventMesh.debug_log("num_total:datalen:{} {}".format(MXA_MSG,wechat_recode_list)) - wechat_recode_list["%02d"%(wechat_num)] = "read:1" - wechat_recode_list['num'] = wechat_num - EventMesh.publish("set_wechat_data",[MXA_MSG,wechat_recode_list]) - #写语音文件数据 - - # path = "/usr/static/storage_file/wechat.amr" - path = "/usr/static/storage_file/{}.amr".format(wechat_num) - with open(path,"w") as fd: - arm = bytearray() - j = 0 - for i in range(len(arm_data)): - if arm_data[j] == 0x7d and arm_data[j+1] == 0x01: - arm.extend(b'\x7d') - j+=1 - elif arm_data[j] == 0x7d and arm_data[j+1] == 0x02: - arm.extend(b'\x5b') - j+=1 - elif arm_data[j] == 0x7d and arm_data[j+1] == 0x03: - arm.extend(b'\x5d') - j+=1 - elif arm_data[j] == 0x7d and arm_data[j+1] == 0x04: - arm.extend(b'\x2c') - j+=1 - elif arm_data[j] == 0x7d and arm_data[j+1] == 0x05: - arm.extend(b'\x2a') - j+=1 - else : - arm.append(arm_data[j]) - j+=1 - if j >= len(arm_data): - break - fd.write(arm) - EventMesh.publish("recv_wechat_audio")# - EventMesh.publish(constants.PROTOCOL.TKS_SEND,1)#返回接收成功 - else : - if read_AMR_flag != -1 : - EventMesh.publish(constants.PROTOCOL.TKS_SEND,0)#返回接收失败 - - - - - - - -class MESSAGE_ProtoCol(ProtoCol): - """微聊""" - TYPE = "MESSAGE" - - - def send(self, event, msg): - #[XT*334588000000156*0007*MESSAGE] - send_data = EventMesh.publish("get_new_pid")+"*%s*0007*MESSAGE"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #Wechat_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - """ - - @param event: 微聊文字 - @param msg: 接收到的下行数据 - """ - # msg = msg[1:-1] - - # msg_s = msg.decode('ascii') - # msg_s = msg_s.split(',') - # msg_s = msg_s[1] - # # Wechat_log("msg_tmp:len:{}\r\n data:{}".format(len(msg_s),msg_s)) - - - # get_wechat_data = EventMesh.publish("get_wechat_data") - - # MXA_MSG = get_wechat_data[0] - # wechat_recode_list = get_wechat_data[1] - # wechat_num = wechat_recode_list["num"] - # # Wechat_log("TK_ProtoCol:datalen:{} {} {}".format(wechat_num,MXA_MSG,wechat_recode_list)) - - # if wechat_num >= MXA_MSG: - # wechat_num = 0 - # if wechat_num == 0: - # wechat_num += 1 - # elif wechat_num > 0: - # wechat_num += 1 - # msg_txt = {} - # # 0 1 2 3 - # Wechat_log("wechat_num:init {}".format(wechat_num)) - # msg_txt = UnicodeHexStr_To_Utf8Str(msg_s) - # # Wechat_log("msg_txt:{}".format(msg_txt)) - # wechat_recode_list["%02d"%(wechat_num)] = "T:{}".format(msg_txt) - # wechat_recode_list['num'] = wechat_num - # EventMesh.publish("set_wechat_data",[MXA_MSG,wechat_recode_list]) - # # Wechat_log("wechat_recode_list:len data:{}".format(wechat_recode_list)) - - # screen_NAME = EventMesh.publish("get_screen_NAME") - # get_lcd_state = EventMesh.publish("get_lcd_state") - # if screen_NAME != "wechat" or get_lcd_state == 0: - # EventMesh.publish("load_screen", {"screen": "wechat_popup"}) - # else: - # EventMesh.publish("wechat_updata_list_show") - EventMesh.publish(constants.PROTOCOL.MESSAGE_SEND) - # EventMesh.publish("recv_wechat_audio")# - -class FALLDOWN_ProtoCol(ProtoCol): - """ - 跌倒提醒开关 - 服务器下发跌倒报警设置命令 - [XT*334588000000156*LEN*FALLDOWN,ONOFF] - ONOFF: 1:开启,0:关闭。 - 终端回复 - [XT*334588000000156*LEN*FALLDOWN] - """ - TYPE = "FALLDOWN" - - def send(self, event, msg): - - #[XT*334588000000156*LEN*FALLDOWN] - send_data = EventMesh.publish("get_new_pid")+"*%s*0008*FALLDOWN"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #Wechat_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - """ - [SZKW*860132060341484*000A*FALLDOWN,1] - """ - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_data = msg_tmp.split(',') - - Falldowm = msg_data[1] - EventMesh.debug_log("FALLDOWN:{}".format(Falldowm)) - EventMesh.publish("set_storage_config",["fall_sw",Falldowm]) - - EventMesh.publish(constants.PROTOCOL.FALLDOWN_SEND) - -class REMOVE_ProtoCol(ProtoCol): - """ - 取下手环报警开关 - 平台发送: - [XT*YYYYYYYYYYYYYYY*LEN*REMOVE,0或者1] - 实例:[XT*334588000000156*0008*REMOVE,1] - 终端回复: - [XT*YYYYYYYYYYYYYYY*LEN*REMOVE] - """ - TYPE = "REMOVE" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*0006*REMOVE"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #Wechat_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_data = msg_tmp.split(',') - Falldowm = msg_data[1] - EventMesh.debug_log("REMOVE:{}".format(Falldowm)) - EventMesh.publish("set_storage_config",["remove_sw",Falldowm]) - EventMesh.publish(constants.PROTOCOL.REMOVE_SEND) - -class HELP_ProtoCol(ProtoCol): - """ -一键报警 - [XT*YYYYYYYYYYYYYYY*LEN*HELP,状态] - 状态值为整数:60或不带默认是普通报警,61光电式感烟火灾报警,62检测环境温度(高温报警,温度变化报警), - 63人体活动检测报警,64电池欠压提醒功能,65设备脱落提醒功能,66离线报警,67测试报警,123气感报警, - 124烟感报警,125水浸报警 - 实例: - [XT*334588000000156*0004*HELP,60] - """ - TYPE = "HELP" - - def send(self, event, msg): - #[XT*334588000000156*LEN*FALLDOWN] - send_data = EventMesh.publish("get_new_pid")+"*%s*0008*HELP,%d"%(self.imei,msg) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #Wechat_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - """ - [XT*334588000000156*0004*HELP,60] - """ - - pass - - -#喝水闹钟提示 -# 喝水闹钟设置指令 -# 平台发送: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_DRINK,时间间隔(小时),开关(0为关,1为开)] -# 实例: -# [XT*334588000000156*0026*REMIND_CH_DRINK,4,0] -# 终端回复: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_DRINK] -# 实例:[XT*334588000000156*000E*REMIND_CH_DRINK] -class REMIND_CHDRINK_ProtoCol(ProtoCol): - """ - - 平台发送: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_DRINK,时间间隔(小时),开关(0为关,1为开)] - 实例:[XT*334588000000156*0008*REMIND_CH_DRINK,时间间隔(小时),开关(0为关,1为开)] - 终端回复: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_DRINK] - """ - TYPE = "REMIND_CH_DRINK" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*000F*REMIND_CH_DRINK"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #Wechat_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - # SZKW*860132060346475*0013*REMIND_CH_DRINK,4-1 - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_data = msg_tmp.split(',') - data = str(msg_data[1]) - hour = int(data.split('-')[0]) - btn_on = int(data.split('-')[1]) - - EventMesh.debug_log("recv REMIND_CH_DRINK:{}-{}-{}".format(hour,btn_on,msg)) - EventMesh.publish("set_storage_config",["drink_on",btn_on]) - EventMesh.publish("set_storage_config",["drink_time",hour]) - EventMesh.publish(constants.PROTOCOL.REMIND_CH_DRINK_SEND) - -# 自定义服药闹钟设置指令 -# 平台发送: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_MEDICINE2,时间1-开关,时间2-开关,周几] -# 实例: -# [XT*334588000000156*0026*REMIND_CH_MEDICINE2,08:10-0,09:10-0,1000001] -# 终端回复: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_MEDICINE2] -# 实例:[XT*334588000000156*000E*REMIND_CH_MEDICINE2] -# 说明:闹铃格式为:时间-开关(0/1),周几(周六到周日) - -#REMIND_CH_MEDICINE2,09:00-1,14:00-0,18:00-0,1111111]'",) -class REMIND_CHMEDICINE2_ProtoCol(ProtoCol):#吃药提醒 - """ - - 平台发送: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_MEDICINE2,,时间1-开关,时间2-开关,周几] - 实例:[XT*334588000000156*0008*REMIND_CH_MEDICINE2,,时间1-开关,时间2-开关,周几] - 终端回复: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_MEDICINE2] - """ - TYPE = "REMIND_CH_MEDICINE2" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*0013*REMIND_CH_MEDICINE2"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #Wechat_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - # SZKW*860132060346475*0013*REMIND_CH_MEDICINE2,4-1 - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - - msg_data = str(msg_tmp.split('REMIND_CH_MEDICINE2,')[1]) - msg_data_tmp = str(msg_tmp.split('REMIND_CH_MEDICINE2')[1]) - - medicin_data = [] - alarmday=[] - count = 0 - for i in str(msg_data_tmp): - # print("i",i) - if i == ",": - count+=1 - - if count >= 3: - count = 3 - medicine_alarmday = [] - for i in range(0,count):#默认是三个时间多了也不保存 - medicine_alarmday = str(msg_data.split(",")[count]) - # print("i",i) - medicine_alarm = str(msg_data.split(",")[i]) - if str(medicine_alarm).find("-") != -1: - on = int(medicine_alarm.split('-')[1]) - if on != 0: - alarmday = [] - data = str(medicine_alarm.split('-')[0]) - hour = int(data.split(":")[0]) - min = int(data.split(":")[1]) - for i in range(0,7): - # print("medicine_alarmday",medicine_alarmday,medicine_alarmday[i]) - if medicine_alarmday[i] != '0': - alarmday.append(i) - enable = 1 - medicin_data.append({"hour":hour,"min":min,"day":alarmday,"enable":enable}) - - EventMesh.publish("set_storage_config",["medicine",medicin_data]) - EventMesh.debug_log("recv REMIND_CH_MEDICINE2:{} medicin_data ;{}".format(msg,medicin_data)) - - EventMesh.publish(constants.PROTOCOL.REMIND_CH_MEDICINE2_SEND) - EventMesh.publish("clear_medicine_alarm") - -# 回家闹钟设置指令 -# 可设置闹钟的提醒文字 -# 平台发送: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_HOME,闹钟1,闹钟2,闹钟3] -# 实例: -# [XT*334588000000156*0018*REMIND_CH_HOME,08:10,12:10] -# 终端回复: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_HOME] -# 实例:[XT*334588000000156*0006*REMIND_CH_HOME] -# 说明:闹钟为时间点; -class REMIND_CH_HOME_ProtoCol(ProtoCol):#回家提醒 - """ - - 平台发送: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_HOME,,时间1-开关,时间2-开关,周几] - 实例:[XT*334588000000156*0008*REMIND_CH_HOME,,时间1-开关,时间2-开关,周几] - 终端回复: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_HOME] - """ - TYPE = "REMIND_CH_HOME" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*000E*REMIND_CH_HOME"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #Wechat_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - # SZKW*860132060346475*0013*REMIND_CH_MEDICINE2,4-1 - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_data = msg_tmp.split(',') - - # [SZKW*860132060346475*0014*REMIND_CH_HOME,00:00',) - home_data = str(msg_data[1]) - - EventMesh.debug_log("recv REMIND_CH_HOME:{} medicin_data ;{}".format(msg,home_data)) - if len(home_data) != 0: - EventMesh.publish("set_storage_config",["home_alarm",home_data]) - EventMesh.publish("clear_home_alarm") - EventMesh.publish(constants.PROTOCOL.REMIND_CH_HOME_SEND) -# 起床闹钟设置指令 -# 平台发送: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_WAKEUP,闹钟1,闹钟2] -# 实例: -# [XT*334588000000156*0026*REMIND_CH_WAKEUP,08:10-0-3-1000001,09:10-0-3-1000001] -# 闹钟格式为:时间-开关-频率(1:一次;2:每天;3:自定义) -# 08:10-1-1:闹钟时间8:10,打开,响铃一次 -# 08:10-1-2:闹钟时间8:10,打开,每天响铃 -# 08:10-1-3-0111110:闹钟时间8:10,打开,自定义周一至周五打开 -# 最左边是周日,最右边是周六,即0111110的顺序是:周日,周一,周二,周三,周四,周五,周六(1为打开,0为关闭),其中周一至周五打开闹钟 -# 终端回复: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_WAKEUP] -# 实例:[XT*334588000000156*000E*REMIND_CH_WAKEUP] -# 说明:闹铃格式为:时间-开关-周几(周六到周日) -class REMIND_CH_WAKEUP_ProtoCol(ProtoCol):#该起床了 - """ - - 平台发送: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_WAKEUP,,时间1-开关,时间2-开关,周几] - 实例:[XT*334588000000156*0008*REMIND_CH_WAKEUP,,时间1-开关,时间2-开关,周几] - 终端回复: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_WAKEUP] - """ - TYPE = "REMIND_CH_WAKEUP" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*0011*REMIND_CH_WAKEUP"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #Wechat_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - # SZKW*860132060346475*0013*REMIND_CH_MEDICINE2,4-1 - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp_new = str(msg_tmp.split('REMIND_CH_WAKEUP,')[1]) - msg_tmp_data = str(msg_tmp.split('REMIND_CH_WAKEUP')[1]) - count = 0 - for i in str(msg_tmp_data): - # print("i",i) - if i == ",": - count+=1 - wakeup_data_save = [] - # # ("handler -> b'[SZKW*860132060346475*001C*REMIND_CH_WAKEUP,08:00-1-1,,]'",) - # medicine_alarmday = str(msg_data[4]) - # alarmday=[] - wakeup_day = [] - wakeup_switch = 0 - - print("count",count) - if count >= 3: - count = 3 - for i in range(0,count):#默认是三个时间多了也不保存 - wakeup_day = [] - wakeup_data = str(msg_tmp_new.split(',')[i]) - # print("i",i,wakeup_data) - if len(wakeup_data) != 0: - b = wakeup_data.find('-') - if b != -1: - wakeup_time = wakeup_data.split('-')[0]#-作为分隔符 - wakeup_switch = int(wakeup_data.split('-')[1])#开关 - wakeup_repeat = int(wakeup_data.split('-')[2])#是单次还是每天 还是自定义 - - wakeup_hour = int(wakeup_time.split(":")[0])#时间 - wakeup_min = int(wakeup_time.split(":")[1])#冒号分隔时间 时 分 - - - if wakeup_repeat == 2:#每一天 - wakeup_day = [0,1,2,3,4,5,6] - elif wakeup_repeat == 3:#自定义 - wakeup_day_tmp = wakeup_data.split('-')[3]#-作为分隔符 - for i in range(0,7): - if wakeup_day_tmp[i] != '0': - wakeup_day.append(i) - else:#单次 1 - wakeup_day=[] - if wakeup_switch != 0: - wakeup_data_save.append({"hour":wakeup_hour,"min":wakeup_min,"day":wakeup_day,"enable":wakeup_switch}) - - EventMesh.publish("set_storage_config",["wakeup_alarm",wakeup_data_save]) - wakeup = EventMesh.publish("get_storage_config","wakeup_alarm") - - EventMesh.debug_log("recv REMIND_CH_WAKEUP:{} wakeup ;{}".format(msg,wakeup)) - - EventMesh.publish(constants.PROTOCOL.REMIND_CH_WAKEUP_SEND) - - EventMesh.publish("clear_wakeup_alarm") - - - -# 睡觉闹钟设置指令 -# 平台发送: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_SLEEP,闹钟1,闹钟2] -# 实例: -# [XT*334588000000156*0026*REMIND_CH_SLEEP,08:10-0-3-1000001,09:10-0-3-1000001] -# 闹钟格式为:时间-开关-频率(1:一次;2:每天;3:自定义) -# 08:10-1-1:闹钟时间8:10,打开,响铃一次 -# 08:10-1-2:闹钟时间8:10,打开,每天响铃 -# 08:10-1-3-0111110:闹钟时间8:10,打开,自定义周一至周五打开 -# 最左边是周日,最右边是周六,即0111110的顺序是:周日,周一,周二,周三,周四,周五,周六(1为打开,0为关闭),其中周一至周五打开闹钟 -# 终端回复: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_SLEEP] -# 实例:[XT*334588000000156*000E*REMIND_CH_SLEEP] -# 说明:闹铃格式为:时间-开关-周几(周六到周日) -class REMIND_CH_SLEEP_ProtoCol(ProtoCol):#该睡觉了 - """ - - 平台发送: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_SLEEP,,时间1-开关,时间2-开关,周几] - 实例:[XT*334588000000156*0008*REMIND_CH_SLEEP,,时间1-开关,时间2-开关,周几] - 终端回复: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_SLEEP] - """ - TYPE = "REMIND_CH_SLEEP" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*0011*REMIND_CH_SLEEP"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #Wechat_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - # SZKW*860132060346475*0013*REMIND_CH_MEDICINE2,4-1 - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp_new = str(msg_tmp.split('REMIND_CH_SLEEP,')[1]) - msg_tmp_data = str(msg_tmp.split('REMIND_CH_SLEEP')[1]) - count = 0 - for i in str(msg_tmp_data): - # print("i",i) - if i == ",": - count+=1 - - sleep_data_save = [] - # # ("handler -> b'[SZKW*860132060346475*001C*REMIND_CH_SLEEP,08:00-1-1,,]'",) - # b'[SZKW*860132060346475*001B*REMIND_CH_SLEEP,08:00-1-1,,]'",) - # medicine_alarmday = str(msg_data[4]) - # alarmday=[] - sleep_day = [] - sleep_switch = 0 - - print("count",count) - if count >= 3: - count = 3 - for i in range(0,count):#默认是三个时间多了也不保存 - sleep_day = [] - sleep_data = str(msg_tmp_new.split(',')[i]) - # print("i",i,sleep_data) - if len(sleep_data) != 0: - b = sleep_data.find('-') - if b != -1: - sleep_time = sleep_data.split('-')[0]#-作为分隔符 - sleep_switch = int(sleep_data.split('-')[1])#开关 - sleep_repeat = int(sleep_data.split('-')[2])#是单次还是每天 还是自定义 - - sleep_hour = int(sleep_time.split(":")[0])#时间 - sleep_min = int(sleep_time.split(":")[1])#冒号分隔时间 时 分 - - - if sleep_repeat == 2:#每一天 - sleep_day = [0,1,2,3,4,5,6] - elif sleep_repeat == 3:#自定义 - sleep_day_tmp = sleep_data.split('-')[3]#-作为分隔符 - for i in range(0,7): - if sleep_day_tmp[i] != '0': - sleep_day.append(i) - else:#单次 1 - sleep_day=[] - if sleep_switch != 0: - sleep_data_save.append({"hour":sleep_hour,"min":sleep_min,"day":sleep_day,"enable":sleep_switch}) - - EventMesh.publish("set_storage_config",["sleep_alarm",sleep_data_save]) - # sleep = EventMesh.publish("get_storage_config","sleep_alarm") - - # EventMesh.debug_log("recv REMIND_CH_SLEEP_SEND:{} sleep ;{}".format(msg,sleep)) - - EventMesh.publish(constants.PROTOCOL.REMIND_CH_SLEEP_SEND) - EventMesh.publish("clear_sleep_alarm") - - -# 可设置闹钟的提醒文字 -# 平台发送: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_REST,时间间隔1,时间间隔2,时间间隔3] -# 实例: -# [XT*334588000000156*0026*REMIND_CH_REST,08:10-10:00,12:10-15:00] -# 终端回复: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_REST] -# 实例:[XT*334588000000156*000E*REMIND_CH_REST] -# 说明:时间间隔 【起始-结束】,起始为开始休息,结束为休息结束 - -class REMIND_CH_REST_ProtoCol(ProtoCol):#该休息了 - """ - - 平台发送: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_REST,,时间1-开关,时间2-开关,周几] - 实例:[XT*334588000000156*0008*REMIND_CH_REST,,时间1-开关,时间2-开关,周几] - 终端回复: - [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_REST] - """ - TYPE = "REMIND_CH_REST" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*000E*REMIND_CH_REST"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #Wechat_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - # SZKW*860132060346475*0013*REMIND_CH_REST,4-1 - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_data = str(msg_tmp.split('REMIND_CH_REST,')[1]) - msg_data_new = str(msg_tmp.split('REMIND_CH_REST')[1]) - print("REMIND_CH_REST",len(msg_data),msg_data) - - reset_data_save = [] - count = 0 - for i in str(msg_data_new): - # print("i",i) - if i == ",": - count+=1 - - if count >= 2: - count = 2 - - for i in range(0,count):#默认是三个时间多了也不保存 - reset_data = str(msg_data.split(',')[i]) - print("i",i,reset_data) - if len(reset_data) != 0: - b = reset_data.find('-') - if b != -1: - start_time = reset_data.split('-')[0]#-作为分隔符 - end_time = reset_data.split('-')[1]#-作为分隔符 - - start_hour = int(start_time.split(":")[0]) - start_min = int(start_time.split(":")[1]) - - end_hour = int(end_time.split(":")[0]) - end_min = int(end_time.split(":")[1]) - - reset_data_save.append({"start_hour":start_hour,"start_min":start_min,"end_hour":end_hour,"end_min":end_min}) - - EventMesh.publish("set_storage_config",["reset_alarm",reset_data_save]) - # reset = EventMesh.publish("get_storage_config","reset_alarm") - - # EventMesh.debug_log("recv REMIND_CH_REST:{} reset ;{}".format(msg,reset)) - - EventMesh.publish(constants.PROTOCOL.REMIND_CH_REST_SEND) - EventMesh.publish("clear_reset_alarm") - - - -class WLKEY_ProtoCol(ProtoCol):#白名单开关 - """ - 白名单开关 - [XT*YYYYYYYYYYYYYYY*LEN*WLKEY,ONOFF] ONOFF: 1:开启,0:关闭,2:30秒后自动接听。 - 实例:[XT*334588000000156*0008*WLKEY,1] - 终端回复 - [XT*YYYYYYYYYYYYYYY*LEN*WLKEY] - 实例:[XT*334588000000156*0006*WLKEY] - """ - TYPE = "WLKEY" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*00006*WLKEY"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - #Wechat_log("POWEROFF_ProtoCol:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_data = msg_tmp.split(',') - EventMesh.debug_log("WLKEY_ProtoCol".format(msg_data[1])) - EventMesh.publish("set_storage_config",["WLKEY",msg_data[1]]) - - - EventMesh.publish(constants.PROTOCOL.WLKEY_SEND) -# 终端发送: -# [XT*YYYYYYYYYYYYYYY*LEN*SLEEP_DATA,清醒时间(分钟),浅睡时间(分钟),深睡时间(分钟),日期(年月日)] -# 实例: [XT*334588000000156*0004*SLEEP_DATA,120,240,360,20221008] -# 实例表示:上传睡眠数据 -#[XT*334588000000156*0010*SleepReport,2022-02-09,80,2,532,164,164,25,25,25,25,21:45-0-164-155-3,22:00-1-162-155-0,22:15-2-162-155-0] -class SLEEP_DATA_ProtoCol(ProtoCol):#睡眠时间 - """ - 白名单开关 - [XT*YYYYYYYYYYYYYYY*LEN*WLKEY,ONOFF] ONOFF: 1:开启,0:关闭,2:30秒后自动接听。 - 实例:[XT*334588000000156*0008*WLKEY,1] - 终端回复 - [XT*YYYYYYYYYYYYYYY*LEN*WLKEY] - 实例:[XT*334588000000156*0006*WLKEY] - """ - TYPE = "SleepReport" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*0000b*SleepReport"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s,%s]"%(str(MD5_SIGN),send_data,msg) - EventMesh.debug_log("SLEEP_DATA:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - - EventMesh.debug_log("SLEEP_DATA!!!!!!!!!") - # msg = msg[1:-1] - # msg_tmp = msg.decode('ascii') - # msg_data = msg_tmp.split(',') - # EventMesh.debug_log("WLKEY_ProtoCol".format(msg_data[1])) - # EventMesh.publish("set_storage_config",["WLKEY",msg_data[1]]) - - - # EventMesh.publish(constants.PROTOCOL.WLKEY_SEND) - -class SLEEPTIME_ProtoCol(ProtoCol):#睡眠时间 - """ - """ - TYPE = "SLEEPTIME" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*00009*SLEEPTIME"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - EventMesh.debug_log("SLEEPTIME:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - # 实例:[XT*5678901234*0014*SLEEPTIME,23:30-7:30] - EventMesh.debug_log("SLEEPTIME!!!!!!!!!") - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_data = str(msg_tmp.split(',')[1]) - data = {} - if len(msg_data) == 0: - EventMesh.publish("set_storage_config",["sleep_time",data]) - pass - else: - sleep_start = msg_data.split("-")[0] - sleep_end = msg_data.split("-")[1] - start_hour = int(sleep_start.split(":")[0]) - start_min = int(sleep_start.split(":")[1]) - - end_hour = int(sleep_end.split(":")[0]) - end_min = int(sleep_end.split(":")[1]) - - data = {"start_hour":start_hour,"start_min":start_min,"end_hour":end_hour,"end_min":end_min} - # EventMesh.debug_log("SLEEPTIME".format(data)) - EventMesh.publish("set_storage_config",["sleep_time",data]) - # sleep_time = EventMesh.publish("get_storage_config","sleep_time") - # EventMesh.debug_log("sleep_time:{}".format(sleep_time)) - - EventMesh.publish(constants.PROTOCOL.SLEEPTIME_SEND) - -# 平台发送: -# [XT*YYYYYYYYYYYYYYY*LEN*FACTORY] -# 实例:[XT*334588000000156*0007*FACTORY] -# 终端回复: -# [XT*YYYYYYYYYYYYYYY*LEN*FACTORY] -# 实例:[XT*334588000000156*0007*FACTORY] -# 说明:终端恢复出厂设置,里面设置的号码都将清空 -class FACTORY_ProtoCol(ProtoCol):#睡眠时间 - """ - 实例:[XT*334588000000156*0008*FACTORY] - 终端回复 - [XT*YYYYYYYYYYYYYYY*LEN*WLKEY] - 实例:[XT*334588000000156*0006*FACTORY] - """ - TYPE = "FACTORY" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*0007*FACTORY"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - EventMesh.debug_log("FACTORY:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - - EventMesh.debug_log("FACTORY!!!!!!!!!") - # msg = msg[1:-1] - # msg_tmp = msg.decode('ascii') - # msg_data = msg_tmp.split(',') - # EventMesh.debug_log("WLKEY_ProtoCol".format(msg_data[1])) - # EventMesh.publish("set_storage_config",["WLKEY",msg_data[1]]) - - EventMesh.publish(constants.PROTOCOL.FACTORY_SEND) - EventMesh.publish("app_factory",None)#app恢复出厂 - -# 平台发送: -# [3G*YYYYYYYYYY*LEN*RESET] -# 实例:[3g*5678901234*0005*RESET] -# 终端回复: -# [3G*YYYYYYYYYY*LEN*RESET] -class RESET_ProtoCol(ProtoCol):#重启 - """ - 实例:[XT*334588000000156*0008*RESET] - 终端回复 - [XT*YYYYYYYYYYYYYYY*LEN*RESET] - 实例:[XT*334588000000156*0006*RESET] - """ - TYPE = "RESET" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*0005*RESET"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - EventMesh.debug_log("RESET:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - - EventMesh.debug_log("RESET!!!!!!!!!") - # msg = msg[1:-1] - # msg_tmp = msg.decode('ascii') - # msg_data = msg_tmp.split(',') - # EventMesh.debug_log("WLKEY_ProtoCol".format(msg_data[1])) - # EventMesh.publish("set_storage_config",["WLKEY",msg_data[1]]) - - EventMesh.publish(constants.PROTOCOL.RESET_SEND) - Power.powerRestart() - -# 平台发送: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_SIT,时间间隔(分钟),总开关(0为关,1为开),午间休息时段,晚间休息时段] -# 午间/晚间休息时段格式为:开始时间(hh:mm)-结束时间(hh:mm)-开关(0为关,1为开),例如 12:00-15:00-1。 -# 久坐默认值为20分钟。 -# 午间和晚间默认值,午间12:00-14:00,晚上20:00-07:00。 -# 实例: -# [XT*334588000000156*0026*REMIND_CH_SIT,20,0,12:00-15:00-1,20:00-07:00-1] -# 终端回复: -# [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_SIT] -# 实例:[XT*334588000000156*000E*REMIND_CH_SIT] -class REMIND_CH_SIT_ProtoCol(ProtoCol):#久坐提醒 - """ - 实例:[XT*334588000000156*0008*REMIND_CH_SIT] - 终端回复 - [XT*YYYYYYYYYYYYYYY*LEN*REMIND_CH_SIT] - 实例:[XT*334588000000156*0006*REMIND_CH_SIT] - """ - TYPE = "REMIND_CH_SIT" - - def send(self, event, msg): - send_data = EventMesh.publish("get_new_pid")+"*%s*000d*REMIND_CH_SIT"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - EventMesh.debug_log("REMIND_CH_SIT:send:%s"%(data)) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND, data) - - def recv(self, event, msg): - - EventMesh.debug_log("REMIND_CH_SIT!!!!!!!!!") - # msg = msg[1:-1] - # msg_tmp = msg.decode('ascii') - # msg_data = msg_tmp.split(',') - # EventMesh.debug_log("WLKEY_ProtoCol".format(msg_data[1])) - # EventMesh.publish("set_storage_config",["WLKEY",msg_data[1]]) - - EventMesh.publish(constants.PROTOCOL.REMIND_CH_SIT_SEND) - -# 平台发送: -# [XT*YYYYYYYYYYYYYYY*LEN*SCREEN_SHOW,参数内容] -# 实例:[XT*334588000000156*0016*SCREEN_SHOW,00000000000] -# 参数内容用 unicode 编码呈现,设备收到后需解码。 -# 解码后的参数内容可能有多个参数,不同参数用逗号分隔,设备自己决定如何在屏幕中显示这些参数。比如 "SCREEN_SHOW,深圳第一中学,三年1班、张三" -# 终端回复: -# [XT*YYYYYYYYYYYYYYY*LEN*SCREEN_SHOW] -# 实例:[XT*334588000000156*000B*SCREEN_SHOW] - -class SCREEN_SHOWProtoCol(ProtoCol): - """开屏显示""" - TYPE = "SCREEN_SHOW" - def send(self, event, msg): - # [XT*334588000000156*0006*SILENCETIME2] - send_data = EventMesh.publish("get_new_pid")+"*%s*000B*SCREEN_SHOW"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - # print("KEYBRD:data:%s"%(data)) - # print("KEYBRD!!!!!!!!!!!!!",msg) - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - msg_tmp = msg_tmp[1:] - msg_tmp = msg_tmp[0] - print("screen_school_data msg",msg) - - j = 0 - i = 0 - videobook_old = {} - videobook_name_tmp = UnicodeHexStr_To_Utf8Str(msg_tmp) - lst = videobook_name_tmp.split(",") - print("lst data",lst) - - # 0 1 2 3 - screen_school_data = ""#学校 - screen_class_data = ""#班级 - screen_fullname_data = ""#姓名 - if len(lst) != 0: - count = len(lst) - if count > 3: - count = 3 - for i in range(count): - if i == 0: - screen_school_data = lst[0] - EventMesh.publish("set_schoolname", screen_school_data)# - elif i == 1: - screen_class_data = lst[1] - EventMesh.publish("set_classname", screen_class_data)# - else: - screen_fullname_data= lst[2] - EventMesh.publish("set_fullname", screen_fullname_data)# - - # EventMesh.publish("update_screen_show")#更新开屏显示 - - print("screen_school_data!!!:",screen_school_data,screen_class_data,screen_fullname_data) - - EventMesh.publish(constants.PROTOCOL.SCREEN_SHOW_SEND) - -# #小见平台视频通话 -# # [XT*YYYYYYYYYYYYYYY*LEN*JPREG] -# # 实例: -# # [XT*334588000000156*0004*JPREG] -# # 平台回复: -# # [XT*设备ID*LEN*JPREG,账号(整型)] -# # 实例:[XT*334588000000156*0010*JPREG,1087654321] -class JPREG_ProtoCol(ProtoCol):#登入注册 - """""" - TYPE = "JPREG" - def send(self, event, msg): - - # [XT*334588000000156*0005*WLKEY] - send_data = EventMesh.publish("get_new_pid")+"*%s*0005*JPREG"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - # print("WLKEY:data:%s"%(data)) - # print("WLKEY!!!!!!!!!!!!!") - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # b'[SZKW*867387060319166*000A*KEYBRD]' - print("JPREG!!!!!!!!!!!!!",msg,EventMesh.publish("get_new_pid")) - # EventMesh.publish(constants.PROTOCOL.JPREG_SEND) - -# # (声网,佰锐的账号是整型) -# # [XT*YYYYYYYYYYYYYYY*LEN*MEMBERS] -# # 实例:[XT*334588000000156*0007*MEMBERS] -# # 平台回复:[XT*设备ID*LEN*MEMBERS,账号1,昵称1,电话1,账号2,昵称2,电话2.....] -# # 实例: -# # [XT*334588000000156*003F*MEMBERS,d3e2f759f9d5af3858f3530,爸爸,13662313580,妈妈,555] -# # 以上数据部分使用Unicode编码,最终上面示例传到服务端将是: - -class MEMBERS_ProtoCol(ProtoCol):#获取账号列表 - """""" - TYPE = "MEMBERS" - def send(self, event, msg): - - # [XT*334588000000156*0005*WLKEY] - send_data = EventMesh.publish("get_new_pid")+"*%s*0007*MEMBERS"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s]"%(str(MD5_SIGN),send_data) - print("MEMBERS SEND:data:%s"%(data)) - # print("WLKEY!!!!!!!!!!!!!") - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # b'[SZKW*876226002021444*0048*MEMBERS,0036003900350031003800330032002C563F563FD83CDF410020D83CDF42002C]' - print("MEMBERS!!!!!!!!!!!!!",msg,EventMesh.publish("get_new_pid")) - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp = msg_tmp.split(',') - - videobook = {} - msg_tmp = msg_tmp[1:] - msg_tmp = msg_tmp[0] - - j = 0 - i = 0 - videobook_old = {} - videobook_name_tmp = UnicodeHexStr_To_Utf8Str(msg_tmp) - lst = videobook_name_tmp.split(",") - - print("msg_tmp:{} phonebook_oldtmp:{} type:{}".format(msg_tmp,videobook_name_tmp,lst)) - # 0 1 2 3 - for vediobook_tmp in lst: - - if (j % 3) == 0:#账号 - videobook_old = vediobook_tmp - elif (j % 3) == 1:#名称 - i+=1 - videobook_name_cont= "{}:{}".format("%03d"%(i),videobook_old) - videobook[videobook_name_cont] = vediobook_tmp - j+=1 - print("videobook!!!:{}".format(str(videobook))) - # print("phonebook!!!:{}".format(str(phonebook))) - with open("/usr/static/storage_file/videobook.json","w",encoding="utf-8") as fd: - ujson.dump(videobook,fd) - print("创建视频通话账号") - - EventMesh.publish("set_videobook_list",[1,videobook]) - # EventMesh.publish("video_updata_list_show") - # EventMesh.publish(constants.PROTOCOL.MEMBERS_SEND) - -# # [X*Y*LEN*CALLING,呼叫者id,会议id,通话类型,Token] - -# # 设备回复 -# # [X*Y*LEN*CALLING,音视频状态] -# # 音视频状态:为0表示正常可通讯状态,否则填上用音视频返回的状态码,比如200,如果未获取到返回状态码,则填1 -class CALLING_ProtoCol(ProtoCol):#app发起视频通话 - """""" - TYPE = "CALLING" - def send(self, event, msg): - - # [XT*334588000000156*0005*WLKEY] - send_data = EventMesh.publish("get_new_pid")+"*%s*0007*CALLING"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s,%d]"%(str(MD5_SIGN),send_data,msg) - # print("CALLING:send data:%s"%(data)) - # print("WLKEY!!!!!!!!!!!!!") - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # b'[SZKW*867387060319166*000A*CALLING]' - print("CALLING!!!!!!!!!!!!!",msg) - if EventMesh.model == "D10":#D10没有视频通话 - EventMesh.publish(constants.PROTOCOL.CALLING_SEND,1) - return - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp_d = msg_tmp.split(',') - call_id = msg_tmp_d[1]#id - meet_id = msg_tmp_d[2]#会议id - call_type = int(msg_tmp_d[3])#会议id# 0音频 1视频 - tokens_data = msg_tmp_d[4]#tokens - EventMesh.publish("set_vedio_call_id", call_id)# - EventMesh.publish("set_vedio_meet_id", meet_id)# - EventMesh.publish("set_vedio_call_type", call_type)# - EventMesh.publish("set_vedio_tokens_data", tokens_data)# - - flag = EventMesh.publish("get_call_state") - fall_down = EventMesh.publish("get_fall_down_state")#跌倒报警标识 - usb_state = EventMesh.publish("get_usb")# - video_state = EventMesh.publish("get_video_state") - sos_dialing = EventMesh.publish("get_sos_state_out")#正在拨打sos电话 - if flag != 0 or fall_down != 0 or video_state != 0 or sos_dialing != 0: #电话在打入或者 在充电不允许视频通话 - EventMesh.publish(constants.PROTOCOL.CALLING_SEND,1) - else: - EventMesh.publish(constants.PROTOCOL.CALLING_SEND,0) - EventMesh.publish("set_video_state", 6) - EventMesh.publish("set_dev_app_mode", 2)# - EventMesh.publish("close_hr_sop2")# - EventMesh.publish("video_call_in_event",call_type) - -# # [XT*YYYYYYYYYYYYYYY*LEN*RCALLING,呼叫者id,会议id/声网频道名(没有则留空,但位置保留),音频还是视频(0为音频,1为视频)] -# # 实例: -# # [XT*334588000000156*0007*RCALLING,d3e2f759f9d5af3858f3530b,,0] -# # 终端回复: -# # [XT*设备ID*LEN*RCALLING] -# # 实例:[XT*334588000000156*0009*RCALLING] -# # -class RCALLING_ProtoCol(ProtoCol):#设备拒接接听 - """""" - TYPE = "RCALLING" - def send(self, event, msg): - - # [XT*334588000000156*0005*WLKEY] - send_data = EventMesh.publish("get_new_pid")+"*%s*0008*RCALLING"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s,%s,%s,%d]"%(str(MD5_SIGN),send_data,msg[0],msg[1],msg[2]) - # print("RCALLING:send data:%s"%(data)) - # print("WLKEY!!!!!!!!!!!!!") - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - # print("RCALLING:send data end:%s"%(data)) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # b'[SZKW*867387060319166*000A*RCALLING]' - print("RCALLING!!!!!!!!!!!!!") - -class AGTALK_ProtoCol(ProtoCol):#设备发起视频通话与app接听视频通话 - """""" - TYPE = "AGTALK" - def send(self, event, msg): - - # [XT*334588000000156*0005*WLKEY] - send_data = EventMesh.publish("get_new_pid")+"*%s*0006*AGTALK"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s,%d,%s]"%(str(MD5_SIGN),send_data,msg[0],msg[1]) - print("AGTALK:send data:%s"%(data)) - # print("WLKEY!!!!!!!!!!!!!") - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # b'[SZKW*876226002021444*007B*AGTALK,0,01lqlyqzzQHpfwv5g7jFVC2H6Ha2tHFpMlfSm5sd2uo7o55JEcc=OVxewje6MjP0NgEODwsLDwkJCwkLCA0NDQ==,8762260020214441701226895]' - # print("AGTALK!!!!!!!!!!!!!") - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp_d = msg_tmp.split(',') - call_state = int(msg_tmp_d[1])#返回是否可以视频通话 - if call_state == 0:#可正常通话 - if EventMesh.publish("get_video_state") != 0: - # print("AGTALK!!!!!!!!!!!!!answer") - tokens_data = msg_tmp_d[2]#会议id - meet_id = msg_tmp_d[3]#会议id - EventMesh.publish("set_vedio_meet_id", meet_id)# - EventMesh.publish("set_vedio_tokens_data", tokens_data)# - EventMesh.publish("open_vedio_infor") - - else: - #不能视频通话 - tokens_data = msg_tmp_d[2]#token数据 - meet_id = msg_tmp_d[3]#会议id - EventMesh.publish("set_vedio_meet_id", meet_id)# - EventMesh.publish("set_vedio_tokens_data", tokens_data)# - EventMesh.publish("video_newwork_reject_cb",None)#挂断视频通话 - # print("AGTALK no!!!!!!!!!!!!!") - - print("AGTALK!!!!!!!!!!!!!") - - -class RTALK_ProtoCol(ProtoCol):#app拒接视频通话 - """""" - TYPE = "RTALK" - def send(self, event, msg): - - # [XT*334588000000156*0005*WLKEY] - send_data = EventMesh.publish("get_new_pid")+"*%s*0005*RTALK"%(self.imei) - MD5_SIGN = self.pack_MD5_head(send_data) - data = "[%s%s,%d,%s]"%(str(MD5_SIGN),send_data,msg[0],msg[1]) - # print("AGTALK:send data:%s"%(data)) - # print("WLKEY!!!!!!!!!!!!!") - # 通过socket发送到服务器端 - EventMesh.publish(constants.EVENT.CHANNEL_SOCKET_SEND,data) - - def recv(self, event, msg): - """ - @param event: - @param msg: 接收到的下行数据 - """ - # b'[SZKW*867387060319166*000A*RTALK]' - print("RTALK!!!!!!!!!!!!!") - # [SZKW*876226002021444*003A*RTALK,3ef20e64f3abb84b27f7d764,8762260020214441701226522,1]' - msg = msg[1:-1] - msg_tmp = msg.decode('ascii') - msg_tmp_d = msg_tmp.split(',') - EventMesh.publish("app_rtalk_cb",1) - - - - -class ProtoColResolver(object): - def __init__(self): - self.protocol_list = [] - - def add_support(self, prot): - self.protocol_list.append(prot) - prot.initialization() - - def handler(self, data): - # 这里接收数据 - - EventMesh.debug_log("handler -> {}".format(len(data))) - # if data.startswith('Init;'): - # data = data[5:] - # EventMesh.publish(constants.EVENT.SET_KEY, {"value": ujson.loads(data)['KEY']}) - - # elif data.startswith("Update;"): - # data = data[7:] - # EventMesh.publish(constants.PROTOCOL.H04_RECV, data) - try: - if len(data) > 0: - playpolad_data(data) - # EventMesh.publish(ProtoCol_type, data) #返回协议read - except Exception as e: - EventMesh.debug_log("handler:ERROR:") - usys.print_exception(e) - EventMesh.debug_log("handler:error data:{}".format(len(data))) +try: + from qp_log import qp_print as print +except Exception: + pass +import ujson +import utime +import EventMesh +import constants +from common import Abstract +import uhashlib +import ubinascii +from misc import Power +import audio +from machine import Pin +import ujson +import ql_fs +import dataCall +import net +import usocket +import osTimer + +import machine +import urandom +import _thread +import usys +import request +import umqtt +import modem +SHA256 = uhashlib.sha256 +import versions +import version_util +try: + import vc_config as _vc_cfg + _NLP_EXTENDED_CMDS = _vc_cfg.get_bool("NLP_EXTENDED_CMDS", False) +except Exception: + _NLP_EXTENDED_CMDS = False + +from queue import Queue +def unicode_to_utf8(src): + ref = 0xe08080 + result = ref + + #取最后六位 + a = src & 0x3f + + #将最后六位放在ref最后六位的空位置 + result = result | a + src = src >> 6 + + #取接下来的六位 + a = src & 0x3f + + #放在ref对应的六个位置 + result = result | (a << 8) + + #保留最后四位 + src = src >> 6 + + #放在ref对应的四个位置 + result = result | (src << 16) + return result + +def UnicodeHexStr_To_Utf8Str(str): + odata = 0 + + result = bytes() + su =str.upper() + + i = 0 + for c in su: + tmp=ord(c) + if tmp <= ord('9') : + odata = odata << 4 + odata += tmp - ord('0') + + elif ord('A') <= tmp <= ord('F'): + odata = odata << 4 + odata += tmp - ord('A') + 10 + if(i%4==3): #每2字节获取一个int,拿到转换后的unicode int + #print(odata.to_bytes(2, 'big')) + #result.append(odata) + if odata > 255: + tmp = unicode_to_utf8(odata)#拿到转换后的utf8 int + tmp2 = tmp.to_bytes(3, 'big') #转bytes + else : + tmp2 = odata.to_bytes(1, 'big') + #print("tmp2:{}".format(tmp2)) + result = result + tmp2 + odata = 0 + i+=1 + return result.decode('utf-8') #返回utf8的str + +def HMAC(k, m): + SHA1_BLOCK_SIZE = 64 + KEY_BLOCK = k + (b'\0' * (SHA1_BLOCK_SIZE - len(k))) + KEY_INNER = bytes((x ^ 0x36) for x in KEY_BLOCK) + KEY_OUTER = bytes((x ^ 0x5C) for x in KEY_BLOCK) + inner_message = KEY_INNER + m + outer_message = KEY_OUTER + SHA256(inner_message).digest() + return SHA256(outer_message) + + +def get_hmac_sha256(k, m): + data = HMAC(k, m).digest() + # print("HMAC(k, m).digest():", data) + # print("HMAC(k, m).digest():", ubinascii.hexlify(data)) + hex_data = ubinascii.hexlify(data) + hex_data = hex_data.decode('ascii') + return hex_data + +######################IOT###################### +import sms +import voiceCall +import quecgnss +import wifiScan +from misc import Power,USB +def wifiScan_cb(args): + print("wifi_data:{}".format(args)) + EventMesh.publish(constants.EVENT.WIFIINFO, args) + wifiScan.control(0) +#定位thered +class IOTWebLocationManager(Abstract): + + def __init__(self): + self.gps = None + #self.match = GPSMatch() + self.timer_read_GPS_info = osTimer() + self.timer_sos_report_info = osTimer() + self.wifi_data = [] + self.cell_data = [] + self.location_timer_count = 0 + self.location_check_data_timer_count = 0 + self.GGA_count = 0 + self.RMC_A_count = 0 + self.location_type = 0 #定位类型,0:正常定位,有gps数据或60秒关闭;1:测试模式,只有发CLOSE_LOCATION消息才关闭 + self.location_flag = 0 + self.start_location_flag = 0 + self.location_thread_id = 0 + self.location_queue = Queue(100) + self.lpm_fd = None + self.start_agps = 0 + self.sos_report_flag=0 + quecgnss.gnssEnable(0) + + def post_processor_after_instantiation(self): + EventMesh.subscribe(constants.EVENT.START_LOCATION, self.start_location) + EventMesh.subscribe(constants.EVENT.WIFIINFO, self.WIFI_INFO) + EventMesh.subscribe(constants.EVENT.WIFISCANOPEN,self.WIFI_scan_open) + EventMesh.subscribe(constants.EVENT.CLOSE_LOCATION,self.close_location) + EventMesh.subscribe("get_location_flag",self.get_location_flag) + #EventMesh.subscribe(constants.EVENT.GPSCLOSE, self.gps_close) + def get_location_flag(self, event, msg): + return self.location_flag + + def sos_report_info(self): + EventMesh.debug_log("sos_report_info:") + if self.sos_report_flag == 1: + return + self.soswifi_data = [] + self.soscell_data = [] + self.sos_report_flag = 1 + self.timer_sos_report_info.start(3000, 1, self.sos_timer) + + def close_sos(self): + self.soswifi_data = [] + self.soscell_data = [] + self.sos_report_flag=0 + self.timer_sos_report_info.stop() + + + def sos_timer(self, *args, **kwargs): + if self.sos_report_flag == 1: + try : + #检查wifi数据 + if self.soswifi_data == [] or (self.soswifi_data != [] and self.soswifi_data[0] == 0):#and self.wifi_data[0] <= 0: + EventMesh.publish(constants.EVENT.WIFISCANOPEN) + + #检查基站数据 + if self.soscell_data== []: + data_tmp = net.getCellInfo() + if(len(data_tmp)>=2): + for cell in data_tmp: + if cell and cell[0][0] == 0 and len(cell[0]) > 1: + self.soscell_data = cell + break + if self.soscell_data != [] and self.soswifi_data != []: + self.close_sos() + self.Location_send([WEB_CMD_ENUM.SOS_REPORT_S_CMD,self.soswifi_data,self.soscell_data]) + + except Exception as e: + usys.print_exception(e) + + def start_location(self, event, msg): + self.location_queue.put(msg)#测试类型入队 + self.wifi_data = [] + self.cell_data = [] + print("start_location:{} self.location_flag:{}".format(msg,self.location_flag)) + if( self.location_flag == 1): + return + self.location_flag = 1 + _thread.stack_size((9+5)*1024) + self.location_thread_id = _thread.start_new_thread(self.location_thread, ()) + _thread.stack_size(8*1024) + # self.timer_read_GPS_info.start(1000, 1, self.location_timer) + # if msg == WEB_CMD_ENUM.SOS_REPORT_S_CMD: + # self.sos_report_info() + # pass + # elif msg == WEB_CMD_ENUM.GPS_REPORT_S_CMD: + # self.gps_report_init() + # pass + def gps_report_init(self): + + EventMesh.debug_log("start_location:") + if self.location_flag == 1: + return + self.wifi_data = [] + self.cell_data = [] + self.location_timer_count = 0 + self.location_flag = 1 + self.timer_read_GPS_info.start(3000, 1, self.location_timer) + + + def close_location(self,event,msg): + self.location_flag = 0 + if wifiScan.getState() == True: + wifiScan.control(0) + # self.timer_read_GPS_info.stop() + if self.location_queue.empty() == False: + self.location_queue.clear() + if self.location_thread_id != 0: + try: + _thread.stop_thread(self.location_thread_id) + except Exception as e: + pass + self.location_thread_id=0 + print("close_location:!!!!!!!!!") + + + + def WIFI_INFO(self, event, msg): + self.wifi_data = msg + + def WIFI_scan_open(self, event, msg): + + if wifiScan.getState() == False : + self.wifi_data = [] + wifiScan.control(1) + wifiScan.setCfgParam(6, 2, 5, 0) #设置WiFi参数,10秒上层,两轮,最多6个,6秒底层,PS优先 + wifiScan.setCallback(wifiScan_cb) #注册回调函数 + wifiScan.asyncStart() + #EventMesh.debug_log("wifiScan.asyncStart") + + def location_timer(self, *args, **kwargs): + if self.location_flag == 1: + try : + #检查wifi数据 + if self.wifi_data == [] or (self.wifi_data != [] and self.wifi_data[0] == 0):#and self.wifi_data[0] <= 0: + EventMesh.publish(constants.EVENT.WIFISCANOPEN) + + #检查基站数据 + if self.cell_data == []: + data_tmp = net.getCellInfo() + if(len(data_tmp)>=2): + for cell in data_tmp: + if cell and cell[0][0] == 0 and len(cell[0]) > 1: + self.cell_data = cell + break + if self.cell_data != [] and self.wifi_data != []: + self.close_location(0,0) + self.Location_send([WEB_CMD_ENUM.POST_LOCATION_S_CMD,self.wifi_data,self.cell_data]) + + except Exception as e: + usys.print_exception(e) + + def Location_send(self, msg) : + EventMesh.debug_log("Location_send1") + if self.location_queue.empty() == True: + return + EventMesh.debug_log("Location_send2") + print("Location_send1") + while True: + if self.location_queue.empty() == False: + TYPE = self.location_queue.get() + # (5, [('70:2A:D7:63:BD:B4', -38), ('C0:94:AD:80:21:5F', -61), ('D8:38:0D:05:1F:E3', -65), ('08:9E:84:E7:A6:38', -73), ('B0:44:14:BC:13:B0', -73)]) + wifi_tmp = [] + wifi_num = msg[0][0] + print("wifi_num:{}".format(wifi_num)) + if wifi_num == 0: + wifi_tmp = [] + else: #([], [], [(0, 69483137, 1120, 1, 385, 30504, 100, -75, -8), (3, 111950593, 1120, 1, 58, 30504, 1650, -98, -19), (3, 219097617, 1120, 17, 76, 30504, 2452, -106, -23), (3, 102345712, 1120, 1, 58, 30504, 3740, -96, -18)]) + + wifi_tmp_data = msg[0][1] + print(wifi_tmp_data) + for i in range(wifi_num): + list_data = {} + list_data["mac"] = str(wifi_tmp_data[i][0]) + list_data["rsi"] = str(wifi_tmp_data[i][1]) + # list_data = "{},{}".format(wifi_tmp_data[i][0],wifi_tmp_data[i][1]) + + wifi_tmp.append(list_data) + pass + # ([(flag, cid, mcc, mnc, lac, arfcn, bsic, rssi)], [(flag, cid, licd, mcc, mnc, lac, uarfcn, psc, rssi)], [(flag, cid, mcc, mnc, pci, tac, earfcn, rssi, rsrq),...]) + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + celList = [] + if msg[1] != []: + for CELL in msg[1] : + print("CELL:{}".format(CELL)) + CELL_data = {} + CELL_data["mcc"] = str(CELL[2]) + CELL_data["mnc"] = str(CELL[3]) + CELL_data["lac"] = str(CELL[4]) + CELL_data["cid"] = str(CELL[1]) + CELL_data["rsi"] = str(CELL[7]) + + + # CELL_data += str(CELL[2]) +"," #MCC + # CELL_data += str(CELL[3]) +"," #MNC + # CELL_data += str(CELL[4]) +"," #LAC + # CELL_data += str(CELL[7]) +"," #RSSI + celList.append(CELL_data) + s_msg="" + s_cmd = "" + if TYPE == WEB_CMD_ENUM.POST_LOCATION_S_CMD: + s_msg = "上报经纬度" + s_cmd = "post_location" + elif TYPE == WEB_CMD_ENUM.POST_SOS_S_CMD: + s_msg = "上报SOS求救" + s_cmd = "post_sos" + send_data = { + "cmd":s_cmd, + "ts":int(time_tmp), + "msg":s_msg, + "data":{ + "gps": { + "longitude": "0", + "latitude": "0", + "located": "0", + "speed": "0" + }, + "wifiList":wifi_tmp, + "celList":celList + + } + } + print("TYPE{}".format(TYPE)) + EventMesh.publish(TYPE,send_data) + else: + self.location_thread_id=0 + EventMesh.publish(constants.EVENT.CLOSE_LOCATION) + break + + + def location_thread(self): + + self.wifi_data = [] + print("location_thread self.location_flag {}".format(self.location_flag)) + while self.location_flag == 1: + try : + + #检查wifi数据 + if self.wifi_data == [] or (self.wifi_data != [] and self.wifi_data[0] == 0):#and self.wifi_data[0] <= 0: + EventMesh.publish(constants.EVENT.WIFISCANOPEN) + + #检查基站数据 + if self.cell_data == []: + print("location_thread") + data_tmp = net.getCellInfo() + if(len(data_tmp)>=2): + for cell in data_tmp: + if cell and cell[0][0] == 0 and len(cell[0]) > 1: + self.cell_data = cell + break + + if self.cell_data != [] and self.wifi_data != []: + self.Location_send([self.wifi_data,self.cell_data]) + + break + + except Exception as e: + usys.print_exception(e) + EventMesh.publish(constants.EVENT.CLOSE_LOCATION)#关闭定位 + utime.sleep_ms(1000) + wifiScan.control(0) + + + +import uwebsocket +import sim +# 设备主动上报后台返回cmd的字符串 +DEVICE_REPORT_RETURN_STRING = "post_rest" +# 服务器主动下发给设备的cmd的字符串 +IOT_AUTO_REPORT_RETURN_STRING = "dispatch_rest" +MAGIC_STR = len("FFFE") +VERSION_STR =len("03") +TYPE_STR = len("03") +PLAYLOAD_STR = len("0000") +PROTOCLO_LEN = MAGIC_STR+VERSION_STR+TYPE_STR+PLAYLOAD_STR+len('{')+len('}')+len("cmd:")+len("data:")+len("ts:") + +class WEB_CMD_ENUM: + HR_R_CMD = "post_heart_beat_R" + HR_S_CMD = "post_heart_beat_S" + + POST_LOCATION_R_CMD = "post_location_R" + POST_LOCATION_S_CMD = "post_location_S" + + POST_SOS_R_CMD = "post_sos_R" + POST_SOS_S_CMD = "post_sos_S" + + POST_DEVICE_STATE_R_CMD = "post_device_state_R" + POST_DEVICE_STATE_S_CMD = "post_device_state_S" + + POST_ALARM_R_CMD = "post_alarm_R" + POST_ALARM_S_CMD = "post_alarm_S" + + POST_SCHEDULE_R_CMD = "post_schedule_R" + POST_SCHEDULE_S_CMD = "post_schedule_S" + + FIND_DEVCE_R_CMD = "find_device_R" + FIND_DEVCE_S_CMD = "find_device_S" + + LOCATION_NOW_R_CMD = "locate_now_R" + LOCATION_NOW_S_CMD = "locate_now_S" + + LOCATION_DURATION_R_CMD = "locate_duration_R" + LOCATION_DURATION_S_CMD = "locate_duration_S" + + ALARM_CONFIG_R_CMD = "alarm_config_R" + ALARM_CONFIG_S_CMD = "alarm_config_S" + + ALARM_LIST_R_CMD = "alarm_list_R" + ALARM_LIST_S_CMD = "alarm_list_S" + + SCHEDULE_LIST_R_CMD = "schedule_list_R" + SCHEDULE_LIST_S_CMD = "schedule_list_S" + + SOS_LIST_R_CMD ="sos_list_R" + SOS_LIST_S_CMD ="sos_list_S" + + SOS_CONFIG_R_CMD = "sos_config_R" + SOS_CONFIG_S_CMD = "sos_config_S" + + DEVICE_SHAKE_STATE_R_CMD = "device_shake_state_R" + DEVICE_SHAKE_STATE_S_CMD = "device_shake_state_S" + + RING_TONE_URL_R_CMD = "ring_tone_url_R" + RING_TONE_URL_S_CMD = "ring_tone_url_S" + + VOLUME_R_CMD = "volume_R" + VOLUME_S_CMD = "volume_S" + + TEXT_MSG_R_CMD = "text_msg_R" + TEXT_MSG_S_CMD = "text_msg_S" + + VOICE_RECORD_R_CMD = "voice_record_R" + VOICE_RECORD_S_CMD = "voice_record_S" + + GET_RINGTONE_R_CMD = "get_ringtone_R" + GET_RINGTONE_S_CMD = "get_ringtone_S" + + RINGTONE_LIST_R_CMD = "ringtone_list_R" + RINGTONE_LIST_S_CMD = "ringtone_list_S" + + SCHEDULE_R_CMD = "schedule_R" + SCHEDULE_S_CMD = "schedule_S" + + SWITCH_BT_STATE_R_CMD="switch_bt_state_R" + SWITCH_BT_STATE_S_CMD="switch_bt_state_S" + + + BT_SCAN_R_CMD="bt_scan_R" + BT_SCAN_S_CMD="bt_scan_S" + + BT_STATE_R_CMD="bt_state_R" + BT_STATE_S_CMD="bt_state_S" + + BT_SCAN_REST_R_CMD = "bt_scan_rest_R" + BT_SCAN_REST_S_CMD = "bt_scan_rest_S" + + CONNECT_BT_R="connect_bt_R" + CONNECT_BT_S="connect_bt_S" + + DISCONNECT_BT_R="disconnect_bt_R" + DISCONNECT_BT_S="disconnect_bt_S" + + NEW_VOICE_MSG_R = "new_voice_msg_R" + NEW_VOICE_MSG_S = "new_voice_msg_S" + + GET_NEW_VOICE_MSG_R="get_new_voice_msg_R" + GET_NEW_VOICE_MSG_S="get_new_voice_msg_S" + + NEW_VOICE_MSG_LIST_R="new_voice_msg_list_R" + NEW_VOICE_MSG_LIST_S="new_voice_msg_list_S" + + PUT_VOICE_MSG_STATE_R="put_voice_msg_state_R" + PUT_VOICE_MSG_STATE_S="put_voice_msg_state_S" + + GET_BT_STATE_R="get_bt_state_R" + GET_BT_STATE_S="get_bt_state_S" + + GET_BT_CONNECT_RECORD_LIST_R = "get_bt_connect_record_list_R" + GET_BT_CONNECT_RECORD_LIST_S = "get_bt_connect_record_list_S" + + BT_CONNECT_RECORD_LIST_R="bt_connect_record_list_R" + BT_CONNECT_RECORD_LIST_S="bt_connect_record_list_S" + + FENCE_SOS_R="fence_sos_R" + FENCE_SOS_S="fence_sos_S" + + PLAY_CONTROL_R="player_control_R" + PLAY_CONTROL_S="player_control_S" + + GET_CHAT_CONFIG_R="get_chat_config_R" + GET_CHAT_CONFIG_S="get_chat_config_S" + + CHAT_CONFIG_R = "chat_config_R" + CHAT_CONFIG_S = "chat_config_S" + + POST_BT_CONNECT_REST_R = "post_bt_connect_rest_R" + POST_BT_CONNECT_REST_S = "post_bt_connect_rest_S" + + UNBIND_DEV_R="unbind_dev_R" + UNBIND_DEV_S="unbind_dev_S" + + USER_UNBIND_DEV_R="user_unbind_dev_R" + USER_UNBIND_DEV_S="user_unbind_dev_S" + + MUSIC_PLAY_R="music_play_R" + MUSIC_PLAY_S="music_play_S" + + NLP_PLAY_R = "ai_cmd_R" + NLP_PLAY_S = "ai_cmd_S" + + #金三 + GET_DEVICE_STATE_R = "get_device_state_R" + GET_DEVICE_STATE_S = "get_device_state_S" +class IOTSendCmdManager(Abstract): + # + def __init__(self): + self.send_data = "" + self.time_tmp = 0 + def post_processor_after_instantiation(self): + EventMesh.subscribe("GET_IOT_WEB_SEND_CMD", self.get_iot_send_cmd) + print("IOTSendCmdManager") + + def get_iot_send_cmd(self, event, msg): + date_tmp = utime.localtime() + self.time_tmp = utime.mktime(date_tmp) + if msg == WEB_CMD_ENUM.HR_S_CMD: + self.send_data = self.hr_send_data(self.time_tmp) + return self.send_data + elif msg == WEB_CMD_ENUM.POST_LOCATION_S_CMD: + self.send_data = self.gps_send_data() + return self.send_data + elif msg == WEB_CMD_ENUM.POST_SOS_S_CMD: + self.send_data = self.sos_wraing_send_data() + return self.send_data + elif msg == WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD: + self.send_data = self.device_report_state_send_data() + return self.send_data + elif msg == WEB_CMD_ENUM.POST_ALARM_S_CMD: + self.send_data = self.alarm_config_send_data() + elif msg == WEB_CMD_ENUM.POST_SCHEDULE_S_CMD: + self.send_data = self.schedule_config_send_data() + return self.send_data + + # 心率上报数据格式 + def hr_send_data(self,time): + print("hr_send_data") + battery = (EventMesh.publish("screen_get_battery"))[1] + if battery >= 100 or battery == None: + battery = 100 + hr_send_data = { + "cmd":"post_heart_beat", + "ts":time, + "msg":"心跳", + "data":{ + "battery": int(battery) + } + } + return hr_send_data + + # gps上报数据格式 + def gps_send_data(self): + gps_data = { + "cmd":"post_location", + "ts":self.time_tmp, + "data":{ + "gps":{ + "longitude":"W,67", + "latitude":"S,465", + "direction":"FR", + "speed":"45", + "altude":"56", + "count":"3" + }, + "wifiList":[ + "89:78:78:67:89:98,-98" + ], + "celList":[ + "MCC,MNC,LAC,CID,RSSI" + ], + "btList":[ + "D4:78:65:67:89:78" + ] + } + } + return gps_data + + # sos警告上报数据格式 + def sos_wraing_send_data(self): + sos_wraing_data ={ + "cmd":"post_sos", + "ts":self.time_tmp, + "data":{ + "gps":{ + "longitude":"W,67", + "latitude":"S,465", + "direction":"FR", + "speed":"45", + "altude":"56", + "count":"3" + }, + "wifiList":[ + "89:78:78:67:89:98,-98" + ], + "celList":[ + "MCC,MNC,LAC,CID,RSSI" + ], + "btList":[ + "D4:78:65:67:89:78" + ] + } + } + return sos_wraing_data + + #设备上报状态上报数据格式 + def device_report_state_send_data(self): + get_volume = EventMesh.publish("get_storage_config","VC_TTS_volume") + device_report_data ={ + "cmd":"post_device_state", + "ts":self.time_tmp, + "data":{ + "mediaTitle":"haha", + "mediaState":"stop", + "cover":"899", + "volume":get_volume + } + } + # HR_DATA=EventMesh.publish("GET_IOT_WEB_SEND_CMD",WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD) + # print("HR_DATA{}".format(HR_DATA)) + # EventMesh.publish(WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD,HR_DATA) + return device_report_data + + #闹钟配置上报数据格式 + def alarm_config_send_data(self): + alarm_data = { + "cmd":"post_alarm", + "ts":self.time_tmp, + "msg":"上报闹钟", + "data":[ + { + "name":"喝水", + "loop":"0101001", + "state":"0", + "time":"14:34", + "ringTimes":1, + "alarmMode":"ring", + "shakeType":"short" + }, + { + "name":"起床", + "loop":"0101001", + "state":"0", + "time":"07:40", + "ringTimes":1, + "alarmMode":"ring", + "shakeType":"short" + } + ] + } + + return alarm_data + #日程配置上报数据格式 + def schedule_config_send_data(self): + schedule_data = { + "cmd":"post_schedule", + "ts":self.time_tmp, + "msg":"上报日程", + "data":[ + { + "name":"喝水", + "loop":"0101001", + "state":"0", + "time":"14:34", + "ringTimes":1, + "alarmMode":"ring", + "shakeType":"short" + }, + { + "name":"起床", + "loop":"0101001", + "state":"0", + "time":"07:40", + "ringTimes":1, + "alarmMode":"ring", + "shakeType":"short" + } + ] + } + + return schedule_data +#解析收到的数据协议 +def playpolad_data(data): + if(isinstance(data, bytes) == 0 or len(data) <= PROTOCLO_LEN): + return + + if data[0] != 0xff and data[1] !=0xfe and data[2] != 0x03 and data[3] !=0x03: + return + + # print(type(data)) + json_bytes = data[8:] + # print(data[0],data[1],data[2],data[3],data[4],data[5],data[6],data[7]) + # 2. 将二进制JSON转换为字符串(使用UTF-8解码) + json_str = json_bytes.decode('utf-8') + print("解析出的JSON字符串:") + print(json_str) + print("\n" + "-"*50 + "\n") + + # 3. 将JSON字符串转换为Python字典 + try: + data_dict = ujson.loads(json_str) + print("解析后的字典数据:") + print(data_dict) + + # 可以直接访问字典中的字段 + + cmd = data_dict['cmd'] + if cmd != None: + if cmd == DEVICE_REPORT_RETURN_STRING: + data_cmd = data_dict['data'] + if "cmd" in data_cmd: + cmd = data_dict['data']["cmd"] + # {"cmd ":"post_rest","ts":1753870158426,"data":{"cmd":"post_heart_beat","rest":"success","msg":"."}} + print("设备主动上报后台返回的数据 设备不需要回复") + EventMesh.publish(cmd+"_R",data_dict) + else: + print("返回的data里面没有cmd 返回的数据出错了") + else: + # {"cmd":"locate_now","msg":"","data":"876226211472149","ts":1753870224117} + print("后台主动下发给设备 需要设备回复给后台的数据命令") #后台主动下发的数据不为dict类型 + EventMesh.publish(cmd+"_R",data_dict) + else: + print("cmd is None") #下发的指令无不解析 + except Exception as e: + print("解析JSON出错:", str(e)) +date_tmp = utime.localtime() +time_tmp = utime.mktime(date_tmp) +MAGIC = (0xfffe).to_bytes(2,"big") +VERSION = (0x01).to_bytes(1,"big")#"03".encode('utf-8') +TYPE = (0x03).to_bytes(1,"big") +DATA_LEN = (0x00000000).to_bytes(4,"big") +#magic+version+type+playload_len+playload +#普通协议数据格式根据协议指定 +def protocol_send_total_data(data): + MAGIC = (0xfffe).to_bytes(2,"big") + VERSION = (0x01).to_bytes(1,"big")#"03".encode('utf-8') + TYPE = (0x03).to_bytes(1,"big") + HR_JSON = ujson.dumps(data) + GPS_JSON_b = HR_JSON.encode('utf-8') + DATA_LEN = (len(HR_JSON)).to_bytes(4,"big") + heards_infor = b'' + heards_infor = MAGIC+VERSION+TYPE+DATA_LEN+GPS_JSON_b + return heards_infor +#音频流数据格式 +#magic+version+type+msgid+playload_len+playload +def protocol_audio_ji_playload_data(data,msgid,endflag): + MAGIC = (0xfffe).to_bytes(2,"big") + VERSION = (0x01).to_bytes(1,"big")#"03".encode('utf-8') + TYPE = (0x05).to_bytes(1,"big") + MSGID = msgid.to_bytes(4,"big") + ENDFLAG = endflag.to_bytes(1,"big") + DATA_LEN = (len(data)).to_bytes(4,"big") + heards_infor = b'' + heards_infor = MAGIC+VERSION+TYPE+MSGID+ENDFLAG+DATA_LEN+data + return heards_infor + +#语音留言格式 +def protocol_yuyin_voice_playload_data(data,msgid,endflag): + MAGIC = (0xfffe).to_bytes(2,"big") + VERSION = (0x01).to_bytes(1,"big")#"03".encode('utf-8') + TYPE = (0x08).to_bytes(1,"big") + MSGID = msgid.to_bytes(4,"big") + ENDFLAG = endflag.to_bytes(1,"big") + DATA_LEN = (len(data)).to_bytes(4,"big") + heards_infor = b'' + heards_infor = MAGIC+VERSION+TYPE+MSGID+ENDFLAG+DATA_LEN+data + return heards_infor + +#会议纪要格式 +def protocol_meet_playload_data(data,msgid,endflag): + MAGIC = (0xfffe).to_bytes(2,"big") + VERSION = (0x01).to_bytes(1,"big")#"03".encode('utf-8') + TYPE = (0x09).to_bytes(1,"big") + MSGID = msgid.to_bytes(4,"big") + ENDFLAG = endflag.to_bytes(1,"big") + DATA_LEN = (len(data)).to_bytes(4,"big") + heards_infor = b'' + heards_infor = MAGIC+VERSION+TYPE+MSGID+ENDFLAG+DATA_LEN+data + return heards_infor + +#AI翻译格式 +def protocol_trans_playload_data(data,msgid,endflag): + MAGIC = (0xfffe).to_bytes(2,"big") + VERSION = (0x01).to_bytes(1,"big")#"03".encode('utf-8') + TYPE = (0x0A).to_bytes(1,"big") + MSGID = msgid.to_bytes(4,"big") + ENDFLAG = endflag.to_bytes(1,"big") + DATA_LEN = (len(data)).to_bytes(4,"big") + heards_infor = b'' + heards_infor = MAGIC+VERSION+TYPE+MSGID+ENDFLAG+DATA_LEN+data + return heards_infor +#sos紧急电话class +class sosInfoCallManager(Abstract): + def __init__(self): + self.num = 0 + self.num=0 + self.num_len = 0 + self.sos_list = [] + self.sos_flag = 0 + self.sos_timer1 = osTimer() + self.sos_timer2 = osTimer() + self.save_list = [] + self.call_state = 0 + self.sos_config = {} + self.phone110112_flag = 0 + self.phone110112 = "" + if EventMesh.publish("get_storage_config","sos_list") != None: + self.sos_list = EventMesh.publish("get_storage_config","sos_list") + if EventMesh.publish("get_storage_config","sos_config") != None: + self.sos_config= EventMesh.publish("get_storage_config","sos_config") + + # self.sos_list=[{"index":0,"phone":"15521469561","name":"dengxueying0"}, {"name":"dengxueying1","index":0,"phone":"15692025051"},{"name":"dengxueying2","index":0,"phone":"15521469561"}] + EventMesh.subscribe("call_out_cb", self.call_out_cb) + EventMesh.subscribe("get_sos_flag", self.get_sos_flag) + + def post_processor_after_instantiation(self): + self.num = 0 + self.sos_flag = 0 + voiceCall.setCallback(self.voice_callback) + EventMesh.subscribe("set_sos_info_call", self.set_sos_info_call) + EventMesh.subscribe("set_sos_list_info",self.set_sos_list_info) + EventMesh.subscribe("stop_sos_call",self.stop_sos_call) + EventMesh.subscribe("set_sos_config",self.set_sos_config) + def set_sos_config(self,event,msg): + # sos_config:{'enable110': '1', 'pressDuration': 3, 'enableSos': '0'} + print("set_sos_config:{}".format(msg)) + self.sos_config = msg + # self.start_phone110112() + pass + def get_sos_flag(self,event,msg): + if self.call_state != 0: + return 1 + else: + return 0 + # import EventMesh + # EventMesh.publish("set_sos_info_call") + def close_call_timer(self): + self.sos_timer2.stop() + pass + def stop_sos_call(self,event,msg): #停止sos拨打 + EventMesh.publish("publish_AI_btnInfo",{"func":self.stop_sos_call_cb}) + def stop_sos_call_cb(self): + if self.call_state == 0: + return + self.phone110112_flag=0 + self.sos_flag = 0 + self.call_state = 0 + self.num=0 #不管有没有拨打sos都清除 + self.num_len = 0 + self.sos_timer2.stop() + self.sos_timer1.stop() + voiceCall.callEnd() #挂断 + + def start_phone110112(self): + print("start_phone110112:{} self.sos_config:{}".format(self.call_state,self.sos_config)) + if self.sos_config != {}: + if "enableSos" in self.sos_config: + enableSos = self.sos_config["enableSos"] + if enableSos == "1": + if "enable110" in self.sos_config: + p110 = self.sos_config["enable110"] + if p110 == "0": + self.phone110112 = "110" + tts_str = "正在呼叫{}".format(self.phone110112) + EventMesh.publish("stop_voice_record") #电话优先级最高 + EventMesh.publish("stop_AI_record") + EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 + self.sos_timer1.stop() + self.sos_timer1.start(2000, 0, self.phone110112_cb) + elif p110 == "1": + self.phone110112 = "112" + tts_str = "正在呼叫{}".format(self.phone110112) + EventMesh.publish("stop_voice_record") #电话优先级最高 + EventMesh.publish("stop_AI_record") + EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 + self.sos_timer1.stop() + self.sos_timer1.start(2000, 0, self.phone110112_cb) + + else: + return 1 + + def sos_call_out_phone(self, msg):#电话拨出去 + if self.call_state == 0: + ret = voiceCall.callStart(self.sos_phone_num) + if ret == -1: + + tts_str = "请确认卡支持语音拨号及余额充足" + EventMesh.publish("stop_voice_record") #电话优先级最高 + EventMesh.publish("stop_AI_record") + EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 + self.call_state = 0 + self.num=0 #不管有没有拨打sos都清除 + self.num_len = 0 + self.sos_flag = 0 + self.phone110112_flag=0 + return -1 + else: + EventMesh.publish(constants.EVENT.START_LOCATION,WEB_CMD_ENUM.POST_SOS_S_CMD) #开始AL定位 + self.sos_timer2.stop() + self.sos_timer2.start(30000, 0, self.sos_time_out_phone) + return 1 + + def phone110112_cb(self,*argc): + if self.call_state == 0: + ret = voiceCall.callStart(self.phone110112) + if ret != -1: + self.phone110112_flag=1 + else: + self.phone110112_flag=0 + # print("ret:{} {}".format(ret,self.phone110112)) + self.phone110112 = "" + + def set_sos_list_info(self,event,msg): + self.sos_list = msg + + + def call_out_cb(self,event,phonenumber): + + if self.call_state == 0: + self.sos_phone_num = phonenumber + EventMesh.publish("stop_voice_record") #电话优先级最高 + EventMesh.publish("stop_AI_record") + + tts_str = "正在呼叫号码{}".format(self.sos_phone_num) + EventMesh.publish("stop_voice_record") #电话优先级最高 + EventMesh.publish("stop_AI_record") + EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 + self.sos_timer1.stop() + self.sos_timer1.start(2000, 0, self.sos_call_out_phone) + + + + def sos_time_out_phone(self, msg):#30s超时未打通或者信号不好则重新拨打 + + if self.sos_flag == 1: + if self.call_state == 0: + voiceCall.callEnd() #挂断 + EventMesh.publish("set_sos_info_call") #电话优先级最高 + pass #再次拨打 + + def set_sos_info_call(self,event,msg): + if EventMesh.publish("get_yuyinaudio_flag") == 1 or EventMesh.publish("get_voice_record_mode") != 0: #语言留言模式不允许拨打电话 + self.call_state = 0 + self.num=0 #不管有没有拨打sos都清除 + self.num_len = 0 + self.sos_flag = 0 + self.phone110112_flag=0 + return + if sim.getStatus() != 1:#网络正常 + tts_str = "无SIM卡" + EventMesh.publish("stop_voice_record") #电话优先级最高 + EventMesh.publish("stop_AI_record") + EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 + self.call_state = 0 + self.num=0 #不管有没有拨打sos都清除 + self.num_len = 0 + self.sos_flag = 0 + self.phone110112_flag=0 + return + if self.call_state != 0: + return + if len(self.sos_list) > 0: + self.call_state = 0 + self.call_sos_cb(1) + else: + tts_str = "未设置号码" + EventMesh.publish("stop_voice_record") #电话优先级最高 + EventMesh.publish("stop_AI_record") + EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 + self.call_state = 0 + self.num=0 #不管有没有拨打sos都清除 + self.num_len = 0 + self.sos_flag = 0 + self.phone110112_flag=0 + + def call_sos_cb(self,*argc): + self.phone110112_flag=0 + if self.sos_flag == 0: + self.sos_flag = 1 + sos_flag = self.sos_flag + EventMesh.debug_log('voicecall sos_flag',sos_flag) + if sos_flag == 0 : + self.num = 0 + else: + if len(self.sos_list) > 0: + # for list_t in self.sos_list: + # tmp = list_t["phone"] + # self.save_list.append(tmp) + + self.num +=1 + if len(self.sos_list) == 1: + self.num_len = len(self.sos_list)*3#三次 1 + elif len(self.sos_list) == 2: + self.num_len = len(self.sos_list)*3 #1+2+1+2 + elif len(self.sos_list) == 3: + self.num_len = len(self.sos_list)*3 #1+3+5 + # EventMesh.debug_log("number:{} number_len :{} num:{} num_len:{}".format(number,len(number),num,num_len)) + if self.num > self.num_len : + + self.start_phone110112() + EventMesh.debug_log("call phone!!!!!!!") + self.sos_flag = 0 + self.num = 0 + self.num_len = 0 + else: + self.phone110112_flag=0 + EventMesh.debug_log("self.num:{} self.num_len :{}".format(self.num,self.num_len)) + + if len(self.sos_list) == 1: + if len(self.sos_list[0]["phone"]) != 0: + EventMesh.publish("call_out_cb", self.sos_list[0]["phone"]) + # EventMesh.publish("set_callnumer_cb",self.sos_list[0]["pnone"]) + EventMesh.debug_log("phone 1 {}".format(self.sos_list[0]["phone"])) + elif len(self.sos_list) == 2: + # 1 2 + # 3 4 + # 5 6 + if self.num == 1 or self.num == 3 or self.num == 5: #1 2 3 4 5 + if len(self.sos_list[0]) != 0: + EventMesh.debug_log("phone 2 {}".format(self.sos_list[0]["phone"])) + EventMesh.publish("call_out_cb", self.sos_list[0]["phone"]) + # EventMesh.publish("set_callnumer_cb",self.sos_list[1]["pnone"]) + elif self.num == 2 or self.num == 4 or self.num == 6: + if len(self.sos_list[1]) != 0: + EventMesh.debug_log("phone 3 {}".format(self.sos_list[1]["phone"])) + EventMesh.publish("call_out_cb", self.sos_list[1]["phone"]) + # EventMesh.publish("set_callnumer_cb",self.sos_list[0]["pnone"]) + elif len(self.sos_list) == 3: + # 1 2 3 + # 4 5 6 + # 7 8 9 + if self.num == 1 or self.num == 4 or self.num == 7: + if len(self.sos_list[0]) != 0: + EventMesh.publish("call_out_cb", self.sos_list[0]["phone"]) + # EventMesh.publish("set_callnumer_cb",self.sos_list[0]["phone"]) + EventMesh.debug_log("phone 4 {}".format(self.sos_list[0]["phone"])) + elif self.num == 2 or self.num == 5 or self.num == 8: + if len(self.sos_list[1]) != 0: + EventMesh.debug_log("phone 5 {}".format(self.sos_list[1]["phone"])) + EventMesh.publish("call_out_cb", self.sos_list[1]["phone"]) + # EventMesh.publish("set_callnumer_cb",self.sos_list[2]["phone"]) + elif self.num == 3 or self.num == 6 or self.num == 9: + if len(self.sos_list[2]) != 0: + EventMesh.debug_log("phone 6 {}".format(self.sos_list[2]["phone"])) + EventMesh.publish("call_out_cb", self.sos_list[2]["phone"]) + # EventMesh.publish("set_callnumer_cb",self.sos_list[0]["phone"]) + else: #清除表示 + tts_str = "未设置号码" + EventMesh.publish("stop_voice_record") #电话优先级最高 + EventMesh.publish("stop_AI_record") + EventMesh.publish("play_tts",[tts_str,1]) #播放tts闹钟文本内容 + self.sos_flag = 0 + self.num = 0 + self.call_state = 0 + self.num=0 #不管有没有拨打sos都清除 + self.num_len = 0 + self.phone110112_flag=0 + #未设置号码 + + + def voice_callback(self,args): + EventMesh.debug_log('111test voicecall iinfo:{}'.format(args)) + if args[0] == 10: #电话拨进来 + self.sos_flag = 0 + self.call_state = 1 + self.phone110112_flag=0 + elif args[0] == 11: #已接听 + self.call_state = 2 + self.num=0 #不管有没有拨打sos都清除 + self.num_len = 0 + self.sos_flag = 0 + self.phone110112_flag=0 + self.close_call_timer() + + EventMesh.debug_log('voicecall connected, PhoneNO.: ', args[6]) + + elif args[0] == 12: #电话挂断 + self.phone110112_flag=0 + if self.call_state == 2 or self.call_state == 1: #电话拨出去没有接听 就挂断 + pass + else: + + EventMesh.debug_log('voicecall disconnect end !!!') + self.close_call_timer() + self.call_state = 0 + if self.sos_flag == 1: + EventMesh.publish("set_sos_info_call") + pass + self.call_state = 0 + #再次拨打下一个电话号码 + EventMesh.debug_log('voicecall disconnect end') + + + elif args[0] == 13: # + self.call_state = 7 + EventMesh.debug_log('voicecall is waiting, PhoneNO.: ', args[6]) + elif args[0] == 14: #电话拨出去 + EventMesh.debug_log('voicecall dialing, PhoneNO.: ', args[6]) + if self.sos_flag == 1: + self.call_state = 3 #当前是拨打sos号码标识 + else: + self.call_state = 4 + + elif args[0] == 15: + self.call_state = 5 + EventMesh.debug_log('voicecall alerting, PhoneNO.: ', args[6]) + + + elif args[0] == 16: + self.call_state = 6 + EventMesh.debug_log('voicecall holding, PhoneNO.: ', args[6]) + + + + +try: + import usr.ai_alarm as _schedule +except Exception as e: + usys.print_exception(e) + import ai_alarm as _schedule +DEFIEN_HR_REPORT_TIME = 5*60 +DEFINE_TIME_COUNT = 10 +DEFIEN_LOCATION_REPORT_TIME = DEFINE_TIME_COUNT +WEB_CONNECT_MAX_COUNT = 10 #默认重连次数 +class SockMangager(Abstract): + def __init__(self): + self.thread_buf_size = 16*1024 + self.iot_web_send_queue = Queue(100) + self.iot_web_protoclo_recv_id=0 + self.iot_protocol_client = None + self.connect_web_timer = osTimer() + self.connect_web_timer_count = 0 + self.hr_report_timer = osTimer() + self.resolver = None + self.timerHr = 0 + self.timerLocation = 0 + self.send_device_time = 0 + self.vbat_buf = [] + self.network_state = 0 + self.curr_battery_level = 0 + self.iot_web_send_queue_id = 0 + self.bat_count = 0 + self.minute_now_old = 0 + self.old_minute = 0 + self.is_alarm = 0 + self.reconnect_count = 0 #连续重连次数 默认十次 不过就不重连了 等下一个心跳重连 + self.schedule = _schedule.schedule() + self._1S_timer = osTimer() + self._5S_timer = osTimer() + self.timer_id = 0 + self.start_voice_flag = 0 + self.init_send_default=0 + self.play_new_voice_state = 0 + self.stop_play = 0 + self.get_voice_new_msginit = 0 + self.voice_id = "0" + self.bt_connect_list = [] + self.new_voice_list = [] + self.location_info= {} + self.locate_duration = 0 + self.locate_duration_state = 0 + self.location_info = EventMesh.publish("get_storage_config","locate_duration_time") #获取定位频率信息 + + self.temp = 0 + if self.location_info == None: + self.location_info = {} + else: + # location_time["duration"]=locate_duration_time + # location_time["state"]=locate_duration_state + if "duration" in self.location_info: + self.locate_duration = self.location_info["duration"] #定位频率 + if "state" in self.location_info: + self.locate_duration_state = self.location_info["state"] #是否开启频率定位 + pass + EventMesh.subscribe("REPORT_ALARM_TO_IOT",self.report_alarm_to_iot) + EventMesh.subscribe("REPORT_SCHEDULE_TO_IOT",self.report_schedule_to_iot) + + def report_alarm_to_iot(self,event,msg): + EventMesh.publish(WEB_CMD_ENUM.POST_ALARM_S_CMD,msg) + pass + def report_schedule_to_iot(self,event,msg): + EventMesh.publish(WEB_CMD_ENUM.POST_SCHEDULE_S_CMD,msg) + pass + def usb_event_cb(self, state): + status = state + def post_processor_after_instantiation(self): + self.minute_now_old=0 + EventMesh.subscribe("set_curr_battery_level",self.set_curr_battery_level) + EventMesh.subscribe("get_curr_battery_level",self.get_curr_battery_level) + EventMesh.subscribe("screen_get_battery",self.get_battery) + EventMesh.subscribe("CHANNEL_IOT_WEB_SEND",self.user_send) + EventMesh.subscribe("set_temp",self.set_temp) + EventMesh.subscribe("GET_IOT_PROTOCOL_CILENT",self.get_iot_protoclo_client) + EventMesh.subscribe("SEND_JINMOAUDIO_TO_IOT",self.send_jm_audio_to_iot) + EventMesh.subscribe("SEND_YUYINAUDIO_TO_IOT",self.send_yuyin_audio_to_iot) + EventMesh.subscribe("SEND_MEETAUDIO_TO_IOT",self.send_meet_audio_to_iot) + EventMesh.subscribe("SEND_TRANSAUDIO_TO_IOT",self.send_trans_audio_to_iot) + EventMesh.subscribe("open_websocket_timer",self.open_websocket_timer) + EventMesh.subscribe("close_websocket_timer",self.close_websocket_timer) + EventMesh.subscribe("connect_iot_websocket_cb",self.connect_iot_websocket_cb) + EventMesh.subscribe("set_play_new_voice_list",self.set_play_new_voice_list) + EventMesh.subscribe("get_play_new_voice_list",self.get_play_new_voice_list) + + EventMesh.subscribe("send_meet_open_flag_to_iot",self.send_meet_open_flag_to_iot)#会议创建 + EventMesh.subscribe("send_meet_end_flag_to_iot",self.send_meet_end_flag_to_iot)#会议结束 + + EventMesh.subscribe("send_trans_come_flag_to_iot",self.send_trans_come_flag_to_iot)#进入翻译 + EventMesh.subscribe("send_trans_ok_flag_to_iot",self.send_trans_ok_flag_to_iot)#向APP发送进入翻译成功 + EventMesh.subscribe("send_trans_end_flag_to_iot",self.send_trans_end_flag_to_iot)#退出翻译 + # import EventMesh + # EventMesh.publish("play_new_voice_list") + EventMesh.subscribe("play_new_voice_list",self.play_new_voice_list) #播放语音未读信息 + EventMesh.subscribe("update_play_new_voice_info",self.update_play_new_voice_info) #更新语音未读信息状态上报IOT + EventMesh.subscribe("send_bt_scan_list",self.send_bt_scan_list) #上报扫描列表 + EventMesh.subscribe("save_bt_connect_list",self.save_bt_connect_list) #保存耳机连接状态列表 + EventMesh.subscribe("send_bt_connect_list",self.send_bt_connect_list) #发送耳机连接状态列表 + EventMesh.subscribe("set_play_new_voice_state",self.set_play_new_voice_state) #设置播放语音未读状态1正在播放语音未读消息 + EventMesh.subscribe("get_play_new_voice_state",self.get_play_new_voice_state) #获取播放语音未读状态1正在播放语音未读消息 + EventMesh.subscribe("set_stop_play",self.set_stop_play) #外部控制url音频播放列表暂停 + EventMesh.subscribe("get_stop_play",self.get_stop_play) #获取外部控制url音频播放列表暂停情况 + EventMesh.subscribe("send_bt_connect_state_to_iot",self.send_bt_connect_state_to_iot) #发耳机;链接状态->IOT + EventMesh.subscribe("send_unbind_dev_to_iot",self.send_unbind_dev_to_iot) #发送解绑指令到IOT平台 + EventMesh.subscribe("set_location_info",self.set_location_info) #设置定位信息 + EventMesh.subscribe("post_device_state",self.post_device_state) #上报设备状态信息-》IOT + EventMesh.subscribe("send_power_init_data",self.send_power_init_data) #上电初始化上报数据集合 + # count = 0 + # while True: + # count += 1 + # if count > 20 : + # break + # data = dataCall.getInfo(1, 0) + # print("data call INFO {}".format(data)) + # if data[2][0]: + # self.network_state = 1 + # break + # print("web等待网络 {}...".format(count)) + + # utime.sleep_ms(500) + self.timerHr = 0 + self.reconnect_count = 0 + self.voice_id = "0" + self.deviceReadState = "" + self.get_voice_new_msginit = 0 + self.bt_connect_list = [] + self.new_voice_list = [] + self.connect_web_timer.start(2000,0,self.iot_web_connect_timerCb) #开机2s之后连接iot + def connect_iot_websocket(self): + if EventMesh.publish("get_voice_record_flag") == 1: + EventMesh.publish("stop_voice_record") + print("connect_iot_websocket !!!!") + self.iot_close_web_therad() + + if sim.getStatus() != 1 or self.start_voice_flag == 1:#网络正常 + return + + if self.reconnect_count >= WEB_CONNECT_MAX_COUNT: #重连次数大于10次不在进重连只有下发心跳才重新连接 + return + token = EventMesh.publish("get_dev_token") + + print("token:{}".format(token)) + # token = "957119d6b67b89dc1bc533d2e0c058d559" + # token = "951b74498eabe241e8a8c644a4f07531ef" + + try: + if token != None: + self.connect_web_timer_count = 0 + url_t = "{}?bz-dt={}".format(constants.IOT_WS_URL, token) + try: + self.iot_protocol_client=uwebsocket.Client.connect(url_t, debug=True) + self.reconnect_count=0 + except Exception as e: + print("再次连接,iot连接报错是") + print(e) + self.reconnect_count+=1 + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) #self.connect_iot_websocket() + return + + if self.iot_protocol_client != None: + self.reconnect_count = 0 + _thread.stack_size(self.thread_buf_size+8*1024) #_thread.stack_size(16*1024) + self.iot_web_send_queue_id = _thread.start_new_thread(self.websocket_send_queue_thread, ()) + _thread.stack_size(8*1024) + + _thread.stack_size(self.thread_buf_size+8*1024) + self.iot_web_protoclo_recv_id = _thread.start_new_thread(self.websocket_recv_queue_thread, ()) + _thread.stack_size(8*1024) + + else: + print("self.iot_protocol_client init!!!!!!!!!") + self.reconnect_count+=1 + # EventMesh.publish("connect_iot_websocket_cb") + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) #self.connect_iot_websocket() + return + + if self.iot_protocol_client != None: + self.start_voice_flag = 1 + else: + self.start_voice_flag = 0 + + print("self.iot_protocol_client %d self.start_voice_flag:%d",self.iot_protocol_client,self.start_voice_flag) + if self.iot_protocol_client != None: + if self.timer_id == 0: + # self.hr_report_timer.start(DEFINE_TIME_COUNT*1000, 1, self.public_report_event) + self.timer_id=1 + else: + print("没有token!!!!!!!!!!") + self.connect_web_timer_count+=1 + if self.connect_web_timer_count <= 10: + self.connect_web_timer.start(5000,0,self.iot_web_connect_timerCb) #没有token 5s之后再重连 + pass + except Exception as e: + self.iot_protocol_client = None + print("connect_iot_websocket error") + + def post_device_state(self,event,msg): #上报设备状态 + device_state=EventMesh.publish("GET_IOT_WEB_SEND_CMD",WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD) + print("device_state{}".format(device_state)) + EventMesh.publish(WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD,device_state) #上报设备状态 + def set_location_info(self,event,msg): + if "duration" in msg: + self.locate_duration=msg["duration"] + if "state" in msg: + self.locate_duration_state=msg["state"] + + + def save_bt_connect_list(self,event,msg): + print("msg:{}".format(msg)) + # msg = {"event":"bt_connect_single","data":{"name":"yeiwyei","mac":"89:89:78:78:98:89","state":"connected"}} + try: + data = msg["data"] + if "None" in data: #开机没有连接过蓝牙耳机就为kong + return + if isinstance(data,dict) == True: + mac = data["mac"] + state = data["state"] + name = data["name"] + need_save = 0 + for i,_alarm in enumerate(self.bt_connect_list): + mac_tmp = _alarm["mac"] + if mac_tmp == mac: + need_save = 1 + self.bt_connect_list[i]["state"] = state + self.bt_connect_list[i]["name"] = name + break + self.bt_connect_list[i]["state"] ="disconnected" + + if need_save == 0: #列表里面没有才需要保存 + self.bt_connect_list.append(data) + print("self.bt_connect_list:{}".format(self.bt_connect_list)) + except Exception as e: + print("json数据不完整:self.recv_bt_connect_record_list") + usys.print_exception(e) + pass + def send_bt_connect_list(self,event,msg): + + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + bt_connect_record_list = { + "cmd":"bt_connect_record_list", + "ts":time_tmp, + "msg":"上报蓝牙连接列表", + "data":self.bt_connect_list + + } + print("bt_connect_record_list:{}".format(bt_connect_record_list)) + EventMesh.publish(WEB_CMD_ENUM.BT_CONNECT_RECORD_LIST_S,bt_connect_record_list) + pass + + def set_play_new_voice_list(self,event,msg): + self.new_voice_list = msg + pass + + def get_play_new_voice_list(self,event,msg): + return self.new_voice_list + + def set_play_new_voice_state(self,event,msg): + self.play_new_voice_state = msg + pass + + def get_play_new_voice_state(self,event,msg): + return self.play_new_voice_state + + def get_stop_play(self,event,msg): + return self.stop_play + + def set_stop_play(self,event,msg): + self.stop_play = msg + pass + + def _1S_timer_cb(self,*argc): + EventMesh.publish("publish_AI_btnInfo",{"func":self._1S_timer_cb_}) + + def _1S_timer_cb_(self): + #self.iot_web_send_queue.put("ping") + get_VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag") + if get_VC_satrt_flag == None: + get_VC_satrt_flag = 0 + if get_VC_satrt_flag == 0: + self.play_new_voice_list_timer_cb() + self._1S_timer.stop() + + def play_new_voice_list(self,event,msg): + if len(self.new_voice_list) != 0: + self._1S_timer.start(1000, 1, self._1S_timer_cb) + + def play_new_voice_list_timer_cb(self): + print("play_new_voice_list:{}".format(len(self.new_voice_list))) + voice_list = self.new_voice_list + if len(self.new_voice_list) != 0: + + for i,data in enumerate(self.new_voice_list): + if EventMesh.publish("get_history_voice_flag") == 1: + data = voice_list[len(self.new_voice_list)-1-i] + print("data:{}".format(data)) + self.voice_index = i + self.voice_id = str(data["id"]) + url = str(data["url"]) + # url = "http://qipaiai-oss1.tos-cn-beijing.volces.com/qi_pai_ringtone/2025_08_11/742659759219544064test.mp3" + self.play_new_voice_state = 1 + if self.stop_play == 1: + print("结束播放") + self.stop_play = 0 + break + if EventMesh.publish("get_storage_config","is_noread_voice") == 1:#还有未读的(开机) + if EventMesh.publish("get_open_device") == 1:#刚开机 + EventMesh.publish("play_wait_voice",[url,1]) + EventMesh.publish("set_open_device",0) + if EventMesh.publish("get_history_voice_flag") == 1:#播放历史留言 or 播放当前留言 + if EventMesh.publish("get_wait_voice_flag") == 1:#等待留言 + EventMesh.publish("play_wait_voice",[url,1]) + else: + time = str(data["createTimeStr"]) + EventMesh.publish("play_his_voice",[url,1,time]) + if len(EventMesh.publish("get_play_new_voice_list")) == 0:#列表为空才清零 + EventMesh.publish("set_history_voice_flag",0)#修改标志位 + else: + EventMesh.publish("play_new_voice",[url,1]) + break + pass + def update_play_new_voice_info(self,event,msg): #更新播放语音留言信息 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + put_voice_msg_state = { + "cmd":"put_voice_msg_state", + "ts":time_tmp, + "msg":"修改语音消息阅读状态", + "data":self.voice_id + } + if len(self.new_voice_list) != 0: + self.new_voice_list.pop(self.voice_index) + EventMesh.publish(WEB_CMD_ENUM.PUT_VOICE_MSG_STATE_S,put_voice_msg_state) + pass + + def send_bt_scan_list(self,event,msg): + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + try: + # print("type:{}".format(msg)) + data = msg["data"] + bt_scan_rest ={ + "cmd":"bt_scan_rest", + "ts":time_tmp, + "msg":"上报扫描列表", + "data":data + } + EventMesh.publish(WEB_CMD_ENUM.BT_SCAN_REST_S_CMD,bt_scan_rest) + except Exception as e: + usys.print_exception(e) + pass + def send_unbind_dev_to_iot(self,event,msg): + self._5S_timer.start(5000,0,self._5S_timer_cb) + def _5S_timer_cb(self,*argc): + EventMesh.publish("publish_AI_btnInfo",{"func":self.send_unbind_dev_to_iot_cb}) + pass + def send_unbind_dev_to_iot_cb(self): + EventMesh.publish(WEB_CMD_ENUM.UNBIND_DEV_S) + pass + def send_bt_connect_state_to_iot(self,event,msg): + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + + try: + # { + # "cmd":"post_bt_connect_rest", + # "ts":1738283829, + # "msg":"上报蓝牙连接结果", + # "data":{ + # "mac":"mac地址", + # "state":"connected/disconnected/connecting/failed" + # } + # } + event = msg["event"] + mac = msg["data"]["mac"] + state = msg["data"]["state"] + json_data = \ + { + + "cmd": event, + "ts":time_tmp, + "msg":"上报蓝牙连接结果", + "data":{ + "mac":mac, + "state":state + } + + } + EventMesh.publish(WEB_CMD_ENUM.POST_BT_CONNECT_REST_S,json_data) + except Exception as e: + usys.print_exception(e) + pass + + def send_meet_open_flag_to_iot(self,event,msg): #创建会议纪要上报到IOT平台 + time_tmp = utime.mktime(date_tmp) + meet_open = { + "msg": "开始会议", + "data": None, + "cmd": "meet_begin", + "ts": time_tmp + } + send_data = protocol_send_total_data(meet_open) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + pass + + def send_meet_end_flag_to_iot(self,event,msg): #结束会议纪要上报到IOT平台 + time_tmp = utime.mktime(date_tmp) + meet_end = { + "msg": "结束会议", + "data": None, + "cmd": "meet_end", + "ts": time_tmp + } + send_data = protocol_send_total_data(meet_end) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + pass + + def send_trans_come_flag_to_iot(self,event,msg): #进入翻译上报到IOT平台 + time_tmp = utime.mktime(date_tmp) + trans_open = { + "msg": "进入翻译模式", + "data": None, + "cmd": "translate_device_enter", + "ts": time_tmp + } + send_data = protocol_send_total_data(trans_open) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + pass + + def send_trans_ok_flag_to_iot(self,event,msg): #成功进入翻译发给APP + time_tmp = utime.mktime(date_tmp) + trans_ok = { + "msg": "进入成功", + "data": None, + "cmd": "translate_device_complete", + "ts": time_tmp + } + send_data = protocol_send_total_data(trans_ok) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + pass + + def send_trans_end_flag_to_iot(self,event,msg): #退出翻译上报到IOT平台 + time_tmp = utime.mktime(date_tmp) + trans_open = { + "msg": "退出翻译", + "data": None, + "cmd": "translate_device_exit", + "ts": time_tmp + } + send_data = protocol_send_total_data(trans_open) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + pass + + def send_jm_audio_to_iot(self,event,msg): #静默录音音频数据上报到IOT平台 + send_data = b'' + send_data = protocol_audio_ji_playload_data(msg[0],msg[1],msg[2]) + # print("jm:{}".format(msg[2])) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + pass + + def send_yuyin_audio_to_iot(self,event,msg): #上报语音留言音频数据到IOT平台 + send_data = b'' + send_data = protocol_yuyin_voice_playload_data(msg[0],msg[1],msg[2]) + # print("yuyin:{}".format(msg[2])) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + pass + + def send_meet_audio_to_iot(self,event,msg): #上报会议纪要音频数据到IOT平台 + send_data = b'' + send_data = protocol_meet_playload_data(msg[0],msg[1],msg[2]) + # print("yuyin:{}".format(msg[2])) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + pass + + def send_trans_audio_to_iot(self,event,msg): #上报AI翻译音频数据到IOT平台 + send_data = b'' + send_data = protocol_trans_playload_data(msg[0],msg[1],msg[2]) + # print("yuyin:{}".format(msg[2])) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + pass + + + #获取电池电量 + def get_battery(self, event=None, msg=None): + """ + 获取电池事件,暂时没支持 + :param event: + :param msg: + :return: + """ + + charge_state = '0' + usb_state = EventMesh.publish("GET_USB_STATE") + if usb_state == None: + usb_state = 0 + + if usb_state == 1: + charge_state = '1' + else: + charge_state = '0' + + #temp = Power.getVbatt() + temp = EventMesh.publish("get_curr_battery_level") + vbat = EventMesh.publish("get_battery",temp) + if vbat == None: + vbat = 100 + # EventMesh.debug_log("vbat",vbat) + # if temp != None : + # if temp <= 3505: + # vbat = 0 + # elif temp <= 3699: + # vbat = 20 #"E:/vbat_20.jpg" + # elif temp <= 3772: + # vbat = 40 #"E:/vbat_40.jpg" + # elif temp <= 3882: + # vbat = 60 #"E:/vbat_60.jpg" + # elif temp <= 4070: + # vbat = 80 #"E:/vbat_80.jpg" + # elif temp <= 4200: + # vbat = 90 # + # else: + # vbat = 100 #"E:/vbat_100.jpg" + # else: + # vbat = 100 + + result = [charge_state, vbat] + + return result + def set_curr_battery_level(self, event=None, msg=None): + self.curr_battery_level = msg + + def get_curr_battery_level(self, event=None, msg=None): + return self.curr_battery_level + + def initialization(self, *args): + pass + #EventMesh.debug_log("ProtoCol:{}".format(self.TYPE + "_SEND")) + def connect_iot_websocket_cb(self,event,msg): + self.connect_iot_websocket() + def add_support_resolver(self, resolver): + self.resolver = resolver + def public_report_event(self, *args, **kwargs): + EventMesh.publish("publish_AI_btnInfo",{"func":self.public_report_event_cb}) #self.connect_iot_websocket() + def send_power_init_data(self,event=None,msg=None): + self.public_report_event_cb() + #上报心跳 上报设备状态 检查闹钟 日程 + def public_report_event_cb(self): + """电池平均值""" + temp = Power.getVbatt() + if self.bat_count < 10: + self.vbat_buf.append(temp) + self.bat_count += 1 + else: + for i in range(9): + self.vbat_buf[i] = self.vbat_buf[i+1] + self.vbat_buf[9] = temp + curr_count = len(self.vbat_buf) + bat_total_value = 0 + for i in range(curr_count): + bat_total_value += self.vbat_buf[i] + bat_total_value = bat_total_value//curr_count#一分钟更新一次电量 10个平均值 + # EventMesh.publish("AI_schedule_open")#读取闹钟数组 + # print("AI_schedule_open!!!") + if EventMesh.publish("get_sos_flag") != None: + if EventMesh.publish("get_sos_flag") == 1: + print("get_sos_flag!!!!!!") + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + print("get_sos_flag1!!!!!!") + + EventMesh.publish("AI_alarm_start") #检查是否有闹钟需要提醒 + EventMesh.publish("AI_schedule_open")#检查日程是否需要提醒 + # self.alarm_test() + EventMesh.publish("set_curr_battery_level",bat_total_value) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.hr_report_publish_event,"data":0}) + self.timerHr+=DEFINE_TIME_COUNT + if self.timerHr >= DEFIEN_HR_REPORT_TIME: + self.timerHr= 0 + self.reconnect_count = 0 + self.connect_web_timer_count=0 + EventMesh.publish("publish_AI_btnInfo",{"func":self.hr_report_publish_event,"data":0}) + # print("self.locate_duration_state:{} self.locate_duration:{}".format(self.locate_duration_state,self.locate_duration)) + if self.locate_duration_state == 1: + if self.locate_duration == None: + self.timerLocation = 0 + else: + if self.locate_duration != 0: + self.timerLocation+=DEFINE_TIME_COUNT + if self.timerLocation >= self.locate_duration: + self.timerLocation= 0 + EventMesh.publish(constants.EVENT.START_LOCATION,WEB_CMD_ENUM.POST_LOCATION_S_CMD) #定位上报 + else: + self.timerLocation= 0 + else: + self.timerLocation= 0 + + + if self.send_device_time < DEFIEN_LOCATION_REPORT_TIME : #上电初始化上报一次设备状态信息 + self.send_device_time+=DEFINE_TIME_COUNT + else: + if self.get_voice_new_msginit == 0: + self.get_voice_new_msginit=1 + EventMesh.publish("publish_AI_btnInfo",{"func":self.hr_report_publish_event,"data":0}) + device_state=EventMesh.publish("GET_IOT_WEB_SEND_CMD",WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD) + print("device_state{}".format(device_state)) + EventMesh.publish(WEB_CMD_ENUM.POST_DEVICE_STATE_S_CMD,device_state) #上报设备状态 + # if dev_type == "7PIN_1_5":#二代开机获取留言 + # EventMesh.publish("set_open_device",1) + # EventMesh.publish("set_history_voice_flag",1) + # EventMesh.publish("set_wait_voice_flag",1) + # EventMesh.publish(WEB_CMD_ENUM.GET_NEW_VOICE_MSG_S)#向IOT获取语音未读消息 + + + # http://220.160.104.34:30800/ws-bus/test/voice-record/876226211472149?state=1, + # http://220.160.104.34:30800/ws-bus/test/860132066107046?testType=alarmList + def hr_report_publish_event(self,data): + + HR_DATA = EventMesh.publish("GET_IOT_WEB_SEND_CMD",WEB_CMD_ENUM.HR_S_CMD) + print("HR_DATA{}".format(HR_DATA)) + EventMesh.publish(WEB_CMD_ENUM.HR_S_CMD,HR_DATA) + pass + + def iot_web_connect_timerCb(self,*argc): + self.connect_iot_websocket() + + + def open_websocket_timer(self,event,msg): + self.VC_pingping_timerout = 0 + # self._1S_timer.start(500,1,self._1S_timer_cb) + + def close_websocket_timer(self,event,msg): + self._1S_timer.stop() + + + + def websocket_send_queue_thread(self): + while True: + data = self.iot_web_send_queue.get() + + if data != None: + # EventMesh.debug_log("websocket:send data1 len:{}".format(len(data))) + try: + # EventMesh.debug_log("websocket:send data1 len:{} {}".format(len(data),self.iot_protocol_client)) + if self.iot_protocol_client != None: + self.iot_protocol_client.send(data) + self.reconnect_count = 0 + else: + # EventMesh.publish("connect_iot_websocket_cb") + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) + EventMesh.debug_log("websocket:send data2 len:{}".format(len(data),self.iot_protocol_client)) + except Exception as e: + EventMesh.debug_log("websocket:send error") + # EventMesh.publish("connect_iot_websocket_cb") + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) + break + + + def websocket_recv_queue_thread(self): + while True: + # 死循环接收数据 + try: + recv_data = self.iot_protocol_client.recv() + print("recv_data = {}".format(len(recv_data))) + if recv_data == "websocket close": + # 服务器关闭连接或客户端关闭连接 + print("对端 服务器强制关闭 cli close") + # EventMesh.publish("connect_iot_websocket_cb") + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) + break + elif recv_data == "": + print("下发的空数据 不做处理") + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) + break + else: + if len(recv_data) != 0: + self.reconnect_count = 0 + self.resolver.handler(recv_data) + + except Exception as e: + print("cli break") + # EventMesh.publish("connect_iot_websocket_cb") + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) + break + self.iot_web_protoclo_recv_id =0 + + def iot_web_send_queue_close(self): + if self.iot_web_send_queue_id != 0: + if self.iot_web_send_queue.empty()!=True: + self.iot_web_send_queue.clear() + + try: + _thread.stop_thread(self.iot_web_send_queue_id) + except Exception as e: + print("iot_web_send_queue_close !!!!!!!!") + print("iot_web_send_queue_close 1111!!!!!!!!") + self.iot_web_send_queue_id = 0 + + def iot_web_protoclo_recv_close_therad(self): + if self.iot_web_protoclo_recv_id != 0: + try: + _thread.stop_thread(self.iot_web_protoclo_recv_id) + except Exception as e: + print("self.iot_web_protoclo_recv_id !!!!!!!!") + self.iot_web_protoclo_recv_id = 0 + if self.iot_protocol_client != None: + try: + print("关闭websocke") + self.iot_protocol_client.close() + except Exception as e: + print("关闭websocke失败") + self.iot_protocol_client = None + + + + def iot_close_web_therad(self): + try: + self.iot_web_send_queue_close() + except Exception as e: + print("iot_web_send_queue_close error!!!!!!!!") + + try: + self.iot_web_protoclo_recv_close_therad() + except Exception as e: + print("iot_web_protoclo_recv_close_therad error!!!!!!!!") + self.start_voice_flag = 0 + print("iot_close_web_therad") + + def get_iot_protoclo_client(self,event,msg): + return self.iot_protocol_client + def set_temp(self,event,data): + if self.temp == 0: + self.temp = 1 + else: + self.temp = 0 + def user_send(self,event,data): + # EventMesh.debug_log("TCP:send data:{}".format(msg)) + # EventMesh.debug_log("TCP:send data:{}".format(msg)) + # sim_state = sim.getStatus() + if sim.getStatus() != 1:#网络不正常 + print("无 sim卡") + return + # if self.temp == 1: + # print("没信号") + else : + try: + if self.start_voice_flag == 0: #如果关闭了就需要重新再 + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) + return + if data != None: + if len(data) != 0: + self.iot_web_send_queue.put(data) + # EventMesh.debug_log("self.socket_send_queue.put:success:{}".format(len(data))) + except Exception as e: + EventMesh.debug_log("self.socket_send_queue.put:ERROR:") + usys.print_exception(e) + + return + sim_state = sim.getStatus() + if sim_state != 1 or self.network_state == 0: #没sim,则不发送 ,没联网 + return + else : + try: + if self.iot_protocol_client != None: + EventMesh.publish("publish_AI_btnInfo",{"func":self.iot_protocol_send_cb,"data":data}) + else: + print("reconnect1 {}".format(data)) + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) + + except Exception as e: + print("senc data err reconnect{}".format(data)) + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) + + def iot_protocol_send_cb(self,data): + try: + if data != None: + print("websocket connect success:{} self.iot_protocol_client:{}".format(data,self.iot_protocol_client)) + if self.iot_protocol_client != None: + self.iot_protocol_client.send(data) + self.reconnect_count = 0 + print("websocket send success") + else: + print("reconnect2 {}".format(data)) + # EventMesh.publish("connect_iot_websocket_cb") + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) + except Exception as e: + print("发送失败 重新连接") + self.reconnect_count+=1 + # EventMesh.publish("connect_iot_websocket_cb") + EventMesh.publish("publish_AI_btnInfo",{"func":self.connect_iot_websocket}) + + + + +class ProtoCol(Abstract): + TYPE = "" + HEADER = "#@{}@#;" + SEPARATOR = ";" + END = ";" + chr(0x01) + #PID = "SZKW" + #PID = "qAELPTKmFanR4RHe" + md5_key = '' + PID = '' + btstate = 0 + blestate = 0 + + def __init__(self): + pass + + def initialization(self,*args): + EventMesh.subscribe(self.TYPE+"_S", self.handler) + EventMesh.subscribe(self.TYPE+"_R", self.handler) + pass + + def handler(self, event, msg): + print("handler {}".format(event)) + if event.endswith("_S"): + self.send(event, msg) + else: + self.recv(event, msg) + + def send(self, event, msg): + + pass + + def recv(self, event, msg): + pass +#上报蓝牙连接状态 +class IOTWeb_post_bt_connect_restProtoCol(ProtoCol): + """post_bt_connect_rest""" + TYPE = "post_bt_connect_rest" + + def send(self, event, msg): + # print("send post_bt_connect_rest{}".format(msg)) + send_data = b'' + send_data = protocol_send_total_data(msg) + print("send post_bt_connect_rest{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event: 上报蓝牙耳机连接状态 + @param msg: 接收到的下行数据 + """ + print("recv post_bt_connect_rest{}".format(msg)) +#电子围栏触发SOS警告 +class IOTWeb_fence_sosProtoCol(ProtoCol): + """fence_sos""" + TYPE = "fence_sos" + + def send(self, event, msg): + # print("send fence_sos{}".format(msg)) + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + # 通过websocket发送到服务器端 + fence_sos = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"fence_sos", + "rest":"success", + "msg":"收到紧急呼叫指令" + } + } + send_data = b'' + send_data = protocol_send_total_data(fence_sos) + print("send fence_sos{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event: 电子围栏警告 + @param msg: 接收到的下行数据 + """ + EventMesh.publish(WEB_CMD_ENUM.FENCE_SOS_S) + sos_list = EventMesh.publish("get_storage_config","sos_list") + if sos_list != None: + if len(sos_list) > 0: + get_sos_flag = EventMesh.publish("get_sos_flag") + if get_sos_flag == 0: + EventMesh.publish("set_sos_info_call") + pass + + print("recv fence_sos{}".format(msg)) +#上报心跳 +class IOTWeb_HRProtoCol(ProtoCol): + """post_heart_beat""" + TYPE = "post_heart_beat" + + def send(self, event, msg): + print("send hr{}".format(msg)) + # 通过websocket发送到服务器端 + send_data = b'' + send_data = protocol_send_total_data(msg) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event: 心跳成功 + @param msg: 接收到的下行数据 + """ + # { + # "cmd":"post_rest", + # "ts":1738283829, + # "msg":"消息收到响应", + # "data":{ + # "cmd":"post_heart_beat", + # "rest":"bind/unbind", + # "msg":"上报成功/格式错误" + # } + # } + print("recv hr{}".format(msg)) + if "data" in msg: + data = msg["data"] + rest = data["rest"] + if rest == "unbind": #解绑需要开启蓝牙广播 + self.btstate = 0 + if self.blestate == 0: + EventMesh.publish("set_storage_config",["switch_ble_state","1"]) + EventMesh.publish("send_ble_switch_toble","1") + + EventMesh.publish("set_storage_config",["switch_bt_state","1"]) + EventMesh.publish("send_bt_switch_toble","1") + self.blestate=1 + + elif rest == "bind": #绑定需要关闭蓝牙广播 + self.blestate =0 + if self.btstate == 0: + EventMesh.publish("set_storage_config",["switch_ble_state","0"]) + EventMesh.publish("send_ble_switch_toble","0") + self.btstate=1 + +#定位周期 +class IOTWeb_locatedurationProtoCol(ProtoCol): + """locate_duration""" + TYPE = "locate_duration" + + def send(self, event, msg): + print("send locate_duration{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + locatio = { + "cmd":"dispatch_rest", + "ts":int(time_tmp), + "msg":"消息收到响应", + "data":{ + "cmd":"locate_duration", + "rest":"success", + "msg":"收到定位周期指令" + } + } + send_data = b'' + send_data = protocol_send_total_data(locatio) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + + def recv(self, event, msg): + """ + + @param event:定位周期 + @param msg: 接收到的下行数据 + """ + # locate_now = { + # "cmd":"locate_duration", + # "ts":1738283829, + # "msg":"下发定位周期", + # "data":300,0表示关 + # } + # {"cmd":"locate_duration","msg":"下发定位周期","data":{"duration":300,"state":"1"},"ts":1756089226052,"targetDeviceId":"860132064633266"} + #location_time + location_time = {} + if "data" in msg: + data = msg["data"] + if "duration" in data: + locate_duration_time = int(data["duration"]) + locate_duration_state = int(data["state"]) + location_time["duration"]=locate_duration_time + location_time["state"]=locate_duration_state + + EventMesh.publish("set_storage_config",["locate_duration_time",location_time]) + EventMesh.publish("set_location_info",location_time) + locate_duration_c = EventMesh.publish("get_storage_config","locate_duration_time") + print("locate_duration_c:{}".format(locate_duration_c)) + +#上报SOS告警信息 +class IOTWeb_post_sosProtoCol(ProtoCol): + """post_sos""" + TYPE = "post_sos" + + def send(self, event, msg): + print("send post_sos{}".format(msg)) + # 通过websocket发送到服务器端 + + send_data = b'' + send_data = protocol_send_total_data(msg) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + + def recv(self, event, msg): + """ + @param event:msg + @param msg: 接收到的下行数据 + """ + # locate_now = { + # "cmd":"locate_duration", + # "ts":1738283829, + # "msg":"下发定位周期", + # "data":300,0表示关 + # } + print("recv post_sos{}".format(msg)) + +#上报设备状态 +class IOTWeb_post_device_stateProtoCol(ProtoCol): + """post_device_state""" + TYPE = "post_device_state" + + def send(self, event, msg): + print("send post_device_state{}".format(msg)) + # 通过websocket发送到服务器端 + + send_data = b'' + send_data = protocol_send_total_data(msg) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + + def recv(self, event, msg): + """ + + @param event:mpost_device_state + @param msg: 接收到的下行数据 + """ + # locate_now = { + # "cmd":"locate_duration", + # "ts":1738283829, + # "msg":"下发定位周期", + # "data":300,0表示关 + # } + print("send post_device_state{}".format(msg)) + +#上报闹钟列表 +class IOTWeb_post_alarmProtoCol(ProtoCol): + """post_alarm""" + TYPE = "post_alarm" + + def send(self, event, msg): + print("send post_alarm{}".format(msg)) + # 通过websocket发送到服务器端 + + send_data = b'' + send_data = protocol_send_total_data(msg) + print("send send_data{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + + def recv(self, event, msg): + """ + + @param event:post_alarm + @param msg: 接收到的下行数据 + """ + print("recv post_alarm{}".format(msg)) + +#查找设备 +class IOTWeb_FindDeviceProtoCol(ProtoCol): + """find_device""" + TYPE = "find_device" + + def send(self, event, msg): + # print("send locafind_devicetion{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + find_device = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"find_device", + "rest":"success", + "msg":"收到查找设备指令" + } + } + send_data = b'' + send_data = protocol_send_total_data(find_device) + print("send_data find_device{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:定位信息下发 + @param msg: 接收到的下行数据 + """ + alarm_TTS_str="我在这呢" + base_sentence = "{}".format(alarm_TTS_str) + repeat_sentence = (base_sentence + ",") * 3 # 重复5次并加逗号分隔 + final_sentence = repeat_sentence.rstrip(",") + + EventMesh.publish("play_tts",[final_sentence,1]) #播放tts闹钟文本内容 + print("recv find_device{}".format(msg)) + EventMesh.publish(WEB_CMD_ENUM.FIND_DEVCE_S_CMD) + +#立即定位 +class IOTWeb_LocateNowProtoCol(ProtoCol): + """locate_now""" + TYPE = "locate_now" + def send(self, event, msg): + print("send locate_now{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + locate_now = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"locate_now", + "rest":"success", + "msg":"收到立即定位指令" + } + } + send_data = b'' + send_data = protocol_send_total_data(locate_now) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + + def recv(self, event, msg): + """ + + @param event:立即定位 + @param msg: 接收到的下行数据 + """ + + EventMesh.publish(WEB_CMD_ENUM.LOCATION_NOW_S_CMD) + print("recv locate_now{}".format(msg)) + EventMesh.publish(constants.EVENT.START_LOCATION,WEB_CMD_ENUM.POST_LOCATION_S_CMD) #开始定位 定位完之后上报定位数据 + + +#上报定位信息 +class IOTWeb_post_locationProtoCol(ProtoCol): + """post_location""" + TYPE = "post_location" + + def send(self, event, msg): + # 通过websocket发送到服务器端 + send_data = b'' + send_data = protocol_send_total_data(msg) + print("send_data post_location{}".format(msg)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:定位信息下发 + @param msg: 接收到的下行数据 + """ + print("recv post_location{}".format(msg)) +#闹钟列表 +class IOTWeb_alarm_listProtoCol(ProtoCol): + """alarm_list""" + TYPE = "alarm_list" + + def send(self, event, msg): + print("send alarm_list {}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + rest = "error" + if msg == 1: + rest = "success" + alarm_list ={ + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"alarm_list", + "rest":rest, + "msg":"闹钟配置成功." + } + } + send_data = b'' + send_data = protocol_send_total_data(alarm_list) + print("send_data alarm_list {}".format(msg)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发闹钟配置 + @param msg: 接收到的下行数据 + """ + # 定义要添加的新元素 + # new_item = {'time': '11:30','index': 1} + # data = [{"index":1,"name":"hihihi1"},{"index":2,"name":"hihihi2"},{"index":0,"name":"hihihi0"}] + # len_t = len(data) + # j = 0 + # for i in range(len_t): + # if data[j]['index'] == 1: + # removed_item = data.pop(j) + # print(data) + # j=0 + # continue + # j+=1 + # len_t = len(data) + # print(len_t) + # for i in range(len(data)): + # data[i]["index"] = i + + # url = http://220.160.104.34:30800/ws-bus/test/voice-record/876226211472149?state=1 + # url = http://220.160.104.34:30800/ws-bus/test/876226211472149?testType = alarmList + + alarm_list=[] + ret = 1 + if "data" in msg: + data = msg["data"] + if isinstance(data,list) == True: + for i in range(len(data)): + data_list = data[i] + data_list["index"] = i + print("data_list:{}".format(data_list)) + alarm_list.append(data_list) + alarm_list.sort(key=lambda x: x['index']) + EventMesh.publish("set_storage_config",["alarm_list",alarm_list]) + EventMesh.publish("set_alarm_info",alarm_list) + alarm_list = EventMesh.publish("get_storage_config","alarm_list") + print("alarm_list:{}".format(alarm_list)) + else: + ret = 0 + + EventMesh.publish(WEB_CMD_ENUM.ALARM_LIST_S_CMD,ret) + + +#闹钟配置下发 + +class IOTWeb_alarm_configProtoCol(ProtoCol): + """alarm_config""" + TYPE = "alarm_config" + + def send(self, event, msg): + # print("send alarm_config{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + alarm_config ={ + "cmd":"dispatch_rest", + "ts":int(time_tmp), + "msg":"消息收到响应", + "data":{ + "cmd":"alarm_config", + "rest":"success", + "msg":"闹钟配置成功." + } + } + send_data = b'' + send_data = protocol_send_total_data(alarm_config) + print("send_data alarm_config{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发闹钟配置 + @param msg: 接收到的下行数据 + """ + print("recv alarm_config{}".format(msg)) + EventMesh.publish(WEB_CMD_ENUM.ALARM_CONFIG_S_CMD) + +#SOS联系人下发 +class IOTWeb_sos_listProtoCol(ProtoCol): + """sos_list""" + TYPE = "sos_list" + + def send(self, event, msg): + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + sos_list ={ + "cmd":"dispatch_rest", + "ts":int(time_tmp), + "msg":"消息收到响应", + "data":{ + "cmd":"sos_list", + "rest":"success", + "msg":"收到SOS联系人列表." + } + } + send_data = b'' + send_data = protocol_send_total_data(sos_list) + print("send_data sos_list{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发sos列表 + @param msg: 接收到的下行数据 + """ + #{'msg': 'SOS', 'data': [{'name': '0', 'createTime': 1754031896067, 'updateTime': 1754031896067, 'phone': '12345678900', 'id': '1754031896067', 'deviceId': '876226211472149'}, + # {'name': '1', 'createTime': 1754031896067, 'updateTime': 1754031896067, 'phone': '12345678901', 'id': '1754031896067', 'deviceId': '876226211472149'}, + # {'name': '2', 'createTime': 1754031896067, 'updateTime': 1754031896067, 'phone': '12345678902', 'id': '1754031896067', 'deviceId': '876226211472149'}, + # {'name': '3', 'createTime': 1754031896067, 'updateTime': 1754031896067, 'phone': '12345678903', 'id': '1754031896067', 'deviceId': '876226211472149'}, + # {'name': '4', 'createTime': 1754031896067, 'updateTime': 1754031896067, 'phone': '12345678904', 'id': '1754031896067', 'deviceId': '876226211472149'}], + # 'ts': 1754031896067, 'cmd': 'sos_list'} + print("recv sos_list{}".format(msg)) + sos_list = [] + if "data" in msg: + data = msg["data"] + if isinstance(data,list) == True: + sos_list_len = len(data) + if sos_list_len >= 3: + sos_list_len = 3 + for i in range(sos_list_len): + save_data = {} + # EventMesh.publish("set_storage_config",["SOS_NUMBER{}".format(i+1),data[i+1]]) + # sos_list.append(str(data[i+1])) + save_data["index"] = i + save_data["name"] = data[i]["name"] + save_data["phone"] = data[i]["phone"] + sos_list.append(save_data) + # EventMesh.publish("set_storage_config",["sos_list",sos_list]) + sos_list.sort(key=lambda x: x['index']) + EventMesh.publish("set_storage_config",["sos_list",sos_list]) + EventMesh.publish("set_sos_list_info",sos_list) + read_soslist = EventMesh.publish("get_storage_config","sos_list") + print(read_soslist) + EventMesh.publish(WEB_CMD_ENUM.SOS_LIST_S_CMD) +#NLP指令处理 +def ai_cmd_handle(cmd): + # print("") + if "查询" in cmd:#当指令中有查询时,截取查询操作 + EventMesh.publish("find_cmd",cmd) + elif "讲话" in cmd:#当指令中有讲话时,截取语速操作 + voice_start_index = cmd.index("讲话") + len("讲话")#动态判断截取部分 + find = cmd[voice_start_index:]#查询截取 + if "å¿«" in find: + EventMesh.publish("rate_move",1) + volume = "变快" + elif "慢" in find: + EventMesh.publish("rate_move",0) + volume = "变慢" + tts_play_str = "语速已经" + volume + EventMesh.publish("play_tts",[tts_play_str,0]) + elif "声音改为" in cmd:#当指令中有声音改为时,进入音色修改 + voice_start_index = cmd.index("声音改为") + len("声音改为")#动态判断截取部分 + find = cmd[voice_start_index:]#查询截取 + EventMesh.publish("change_voice",find) + elif "结束对话" in cmd:#当指令中有结束对话时,结束当前对话 + EventMesh.publish("Close_VC") + elif "音量" in cmd:#当指令中有音量改为时,音量改为具体数值 + if "调节" in cmd : + voice_start_index = cmd.index("音量调节:") + len("音量调节:")#动态判断截取部分 + find = cmd[voice_start_index:]#查询截取 + print("find = {}".format(find)) + volume = int(find) + volume += 100#设置音量以100-200为参数 + result = EventMesh.publish("change_volume",volume) + if result == 1: + tts_play_str = "音量已经改为" + find + EventMesh.publish("play_tts",[tts_play_str,0]) + else: + tts_play_str = "设置失败,请重新设置0~100的音量大小" + EventMesh.publish("play_tts",[tts_play_str,0]) + if "加大" in cmd: + result = EventMesh.publish("change_volume",1) + if result == 1: + tts_play_str = "音量已经加大" + EventMesh.publish("play_tts",[tts_play_str,0]) + else: + tts_play_str = "当前音量已经是最大了" + EventMesh.publish("play_tts",[tts_play_str,0]) + elif "减小" in cmd: + result = EventMesh.publish("change_volume",0) + if result == 1: + tts_play_str = "音量已经减小" + EventMesh.publish("play_tts",[tts_play_str,0]) + else: + tts_play_str = "当前音量已经是最小了" + EventMesh.publish("play_tts",[tts_play_str,0]) + + elif "语速倍速设置为:" in cmd:#修改对话模式 + voice_start_index = cmd.index("语速倍速设置为:") + len("语速倍速设置为:")#动态判断截取部分 + find = cmd[voice_start_index:]#查询截取 + print("find = {}".format(find)) + if float(find) > 2.0 or float(find) < 0.5: + tts_play_str = "抱歉,语速仅支持0.5-2倍,请重新设置" + EventMesh.publish("play_tts",[tts_play_str,0]) + return + EventMesh.publish("SET_SPEECH_RATE",float(find)) + tts_play_str = "语速已经改为" + find + EventMesh.publish("play_tts",[tts_play_str,0]) + elif "聆听" in cmd:#聆听快/慢一些 + if "å¿«" in cmd: + tts_play_str = EventMesh.publish("NLP_set_vad_silence",1) + elif "慢" in cmd: + tts_play_str = EventMesh.publish("NLP_set_vad_silence",0) + EventMesh.publish("play_tts",[tts_play_str,0]) + elif "这个不支持哦,请换一个" in cmd: + tts_play_str = "抱歉呀,暂不支持该方言,请换一个" + EventMesh.publish("play_tts",[tts_play_str,0]) + elif "不支持的操作" in cmd: + tts_play_str = "抱歉呀,暂不支持该命令" + EventMesh.publish("play_tts",[tts_play_str,0]) + + if not _NLP_EXTENDED_CMDS: + return + if "会议纪要" in cmd: + print("cmd:已进入会议") + EventMesh.publish("come_meet_mode") + elif "翻译" in cmd: + print("cmd:已进入翻译") + EventMesh.publish("come_trans_mode") + elif "查看历史留言" in cmd: + EventMesh.publish(WEB_CMD_ENUM.GET_NEW_VOICE_MSG_S) + EventMesh.publish("set_history_voice_flag", 1) + EventMesh.publish("clean_unread_vocie") + EventMesh.publish("set_storage_config", ["is_noread_voice", 0]) + +#NLP指令 +class IOTWeb_Nlp_ConfigProtoCol(ProtoCol): + """ai_cmd""" + TYPE = "ai_cmd" + + def send(self, event, msg): + # print("ai_cmd{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + ai_cmd = { + "cmd":"ai_cmd", + "ts":time_tmp, + "msg":"AI指令", + "data":None + } + send_data = b'' + send_data = protocol_send_total_data(ai_cmd) + print("send_data ai_cmd{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + @param event:上行下发push_music + @param msg: 接收到的下行数据 + """ + #提取指令名称 + cmdName = msg["data"] + print("提取到的AI指令为{}".format(cmdName)) + ai_cmd_handle(cmdName) +#设备进入翻译,小程序返回在线情况 +class IOTWeb_translate_device_enterProtoCol(ProtoCol): + """translate_device_enter""" + TYPE = "translate_device_enter" + + def send(self, event, msg): + # print("translate_device_enter{}".format(msg)) + # 通过websocket发送到服务器端 + print("设备进入翻译,小程序返回在线情况") + + def recv(self, event, msg): + """ + @param event:上行下发translate_device_enter + @param msg: 接收到的下行数据 + """ + print("设备进入翻译获取状态:{}".format(msg["data"]["msg"])) + status = msg["data"]["rest"] + + if status == "success": + EventMesh.publish("set_trans_flag",1)#能进入AI翻译 + exit_tts = "已进入实时翻译模式" + EventMesh.publish("play_tts",[exit_tts,1]) + elif status == "error": + EventMesh.publish("set_trans_flag",0)#不能进入AI翻译 + exit_tts = msg["data"]["msg"] + EventMesh.publish("play_tts",[exit_tts,0]) + print("recv translate_device_enter") + +#接收小程序控制进入翻译 +class IOTWeb_come_transProtoCol(ProtoCol): + """translate_app_enter""" + TYPE = "translate_app_enter" + + def send(self, event, msg): + # print("translate_app_enter{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + come_trans = { + "cmd":"translate_device_complete", + "ts":time_tmp, + "msg":"进入成功", + "data":None + } + send_data = b'' + send_data = protocol_send_total_data(come_trans) + print("send_data translate_app_enter{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + @param event:上行下发translate_app_enter + @param msg: 接收到的下行数据 + """ + EventMesh.publish("come_trans_mode")#进入AI翻译 + print("recv translate_app_enter{}".format(msg)) + +#接收小程序控制退出翻译 +class IOTWeb_exit_transProtoCol(ProtoCol): + """translate_app_exit""" + TYPE = "translate_app_exit" + + def send(self, event, msg): + # print("translate_app_enter{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + come_trans = { + "msg": "退出翻译", + "data": None, + "cmd": "translate_device_exit", + "ts": time_tmp + } + send_data = b'' + send_data = protocol_send_total_data(come_trans) + print("send_data translate_app_enter{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + @param event:上行下发translate_app_enter + @param msg: 接收到的下行数据 + """ + EventMesh.publish("exit_trans_mode")#退出AI翻译 + print("recv translate_app_exit{}".format(msg)) + +#接收小程序控制进入会议 +class IOTWeb_appcontrol_meetProtoCol(ProtoCol): + """meet_control""" + TYPE = "meet_control" + def send(self, event, msg): + # print("translate_app_enter{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + come_trans = { + "msg": "开始会议", + "data": None, + "cmd": "meet_begin", + "ts": 1761121597 + } + send_data = b'' + send_data = protocol_send_total_data(come_trans) + print("send_data translate_app_enter{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + @param event:上行下发meeting_app_enter + @param msg: 接收到的下行数据 + """ + data = msg["data"]#判断是开启还是关闭 + if data == "begin": + EventMesh.publish("come_meet_mode")#进入会议纪要 + elif data == "end": + EventMesh.publish("exit_meet_mode")#退出会议纪要 + print("APP Meeting Control") + +#翻译播放TTS +class IOTWeb_play_trans_TTSProtoCol(ProtoCol): + """tts_audio_url""" + TYPE = "tts_audio_url" + + def send(self, event, msg): + # print("tts_audio_url{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + tts_audio_url = { + "cmd":"tts_audio_url_complete", + "ts":time_tmp, + "msg":"返回状态", + "data":None + } + send_data = b'' + send_data = protocol_send_total_data(tts_audio_url) + print("send_data tts_audio_url{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + @param event:上行下发push_music + @param msg: 接收到的下行数据 + """ + #提取录音播放名称 + voiceUrl = msg["data"] + print("提取到的翻译路径为{}".format(voiceUrl)) + EventMesh.publish("play_trans_tts",voiceUrl) + print("recv tts_audio_url{}".format(msg)) + +#音乐播放 +class IOTWeb_play_MusicProtoCol(ProtoCol): + """push_music""" + TYPE = "push_music" + + def send(self, event, msg): + # print("push_music{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + push_music = { + "cmd":"push_music", + "ts":time_tmp, + "msg":"音乐播放", + "data":None + } + send_data = b'' + send_data = protocol_send_total_data(push_music) + print("send_data push_music{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + @param event:上行下发push_music + @param msg: 接收到的下行数据 + """ + #提取音乐播放名称,地址 + print("recv push_music{}".format(msg)) + musicName = msg["data"]["musicName"] + musicUrl = msg["data"]["musicUrl"] + print("提取到的音乐名称为{},路径为{}".format(musicName,musicUrl)) + EventMesh.publish("play_music",[musicName,musicUrl,1]) + + +#音量下发 +class IOTWeb_volumeProtoCol(ProtoCol): + """volume""" + TYPE = "volume" + + def send(self, event, msg): + print("send volume{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + volume = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"volume", + "rest":"success", + "msg":"收到音量设置." + } + } + send_data = b'' + send_data = protocol_send_total_data(volume) + print("send_data volume{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发volume + @param msg: 接收到的下行数据 + """ + if "data" in msg: + r_volume = msg["data"] + print("recv volume{} type:{}".format(r_volume,type(msg))) + + EventMesh.publish("SET_TTS_VOLUME",int(r_volume)) + EventMesh.publish("adjust_volume_toBle",r_volume) + EventMesh.publish(WEB_CMD_ENUM.VOLUME_S_CMD) +#下发SOS配置 +class IOTWeb_sos_configProtoCol(ProtoCol): + """sos_config""" + TYPE = "sos_config" + + def send(self, event, msg): + print("send sos_config{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + sos_config = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"sos_config", + "rest":"success", + "msg":"SOS配置成功." + } + } + send_data = b'' + send_data = protocol_send_total_data(sos_config) + print("send_data sos_config{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发sos_config + @param msg: 接收到的下行数据 + """ + print("recv sos_config{}".format(msg)) + if "data" in msg: + sos_config = msg["data"] + print("recv sos_config{} type:{}".format(sos_config,type(msg))) + EventMesh.publish("set_storage_config",["sos_config",sos_config]) + EventMesh.publish("set_sos_config",sos_config) + sos_config = EventMesh.publish("get_storage_config","sos_config") + # print("sos_config:{} type:{}".format(sos_config,sos_config["enable110"])) + EventMesh.publish(WEB_CMD_ENUM.SOS_CONFIG_S_CMD) +#闹钟铃声获取 + +class IOTWeb_get_ringtoneProtoCol(ProtoCol): + """get_ringtone""" + TYPE = "get_ringtone" + + def send(self, event, msg): + print("send get_ringtone{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + get_ringtone ={ + "cmd":"get_ringtone", + "ts":time_tmp, + "msg":"请求下发铃声列表", + "data":None + } + send_data = b'' + send_data = protocol_send_total_data(get_ringtone) + print("send_data get_ringtone{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发get_ringtone + @param msg: 接收到的下行数据 + """ + print("recv get_ringtone{}".format(msg)) + +#闹钟铃声列表 +class IOTWeb_ringtone_listProtoCol(ProtoCol): + """ringtone_list""" + TYPE = "ringtone_list" + + def send(self, event, msg): + print("send ringtone_list{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + ringtone_list ={ + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"ringtone_list", + "rest":"success", + "msg":"收到闹钟铃声列表." + } + } + send_data = b'' + send_data = protocol_send_total_data(ringtone_list) + print("send_data ringtone_list{}".format(msg)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发ringtone_list + @param msg: 接收到的下行数据 + """ + print("recv ringtone_list{}".format(msg)) + + +#下发静默录音 +class IOTWeb_voice_recordProtoCol(ProtoCol): + """voice_record""" + TYPE = "voice_record" + start_ai = 0 + def send(self, event, msg): + print("send voice_record{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + s_rest = "0" + s_msg = "停止录音." + if msg == "1": + print("设备允许录音") + s_rest = "1" + s_msg = "开始录音." + + voice_record = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"voice_record", + "rest":s_rest, + "msg":s_msg + } + } + send_data = b'' + send_data = protocol_send_total_data(voice_record) + print("send_data voice_record{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发voice_record + @param msg: 接收到的下行数据 + """ + print("recv voice_record{}".format(msg)) + if "data" in msg: + data = msg["data"] + s_rest = "0" + if data == "0": + s_rest = "0" + + record_mode = EventMesh.publish("get_voice_record_mode") + if record_mode == None: + record_mode = 0 + if record_mode != 2:#语音留言不允许打断 + EventMesh.publish("stop_voice_record") + print("停止录音") + EventMesh.publish(WEB_CMD_ENUM.VOICE_RECORD_S_CMD,s_rest) + + elif data == "1": + + get_sos_flag = EventMesh.publish("get_sos_flag") + if get_sos_flag != 0: + s_rest = "0" + print("正在电话") + EventMesh.publish(WEB_CMD_ENUM.VOICE_RECORD_S_CMD,s_rest) + return + record_mode = EventMesh.publish("get_voice_record_mode") + if record_mode == None: + record_mode = 0 + get_VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag") + if get_VC_satrt_flag == None: + get_VC_satrt_flag = 0 + + if record_mode == 2 or get_VC_satrt_flag == 1:# 如果在AI对话 或者语音留言不允许静默录音 + s_rest = "0" + EventMesh.publish(WEB_CMD_ENUM.VOICE_RECORD_S_CMD,s_rest) + EventMesh.publish("stop_voice_record") + else: + s_rest = "1" + EventMesh.publish(WEB_CMD_ENUM.VOICE_RECORD_S_CMD,s_rest) + EventMesh.publish("start_voice_record",1) + print("开始录音") + +#设备解绑 +class IOTWeb_unbind_devProtoCol(ProtoCol): + """unbind_dev""" + TYPE = "unbind_dev" + unbind_devtimer = osTimer() + init_flag = 0 + def send(self, event, msg): + print("send unbind_dev{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + unbind_dev={ + "cmd":"unbind_dev", + "ts":time_tmp, + "msg":"主动解绑设备", + "data":None + } + send_data = b'' + send_data = protocol_send_total_data(unbind_dev) + print("unbind_dev{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + self.init_flag=1 + self.unbind_devtimer.start(2000,0,self.unbind_devtimer_cb) + def unbind_devtimer_cb(self,*argc): + if self.init_flag == 1: + play_str = "设备解绑失败" + EventMesh.publish("play_tts",[play_str,1]) + + def recv(self, event, msg): + """ + + @param event:上行下发unbind_dev + @param msg: 接收到的下行数据 + """ + # { + # "cmd":"post_rest", + # "ts":1738283829, + # "msg":"消息收到响应", + # "data":{ + # "cmd":"unbind_dev", + # "rest":"success|error", + # "msg":"收到主动解绑设备指令" + # } + # } + self.btstate = 0 + self.blestate = 0 + self.unbind_devtimer.stop() + self.init_flag=0 + if "data" in msg: + data = msg["data"] + rest = data["rest"] + if rest == "success": + play_str = "设备解绑成功" + EventMesh.publish("set_storage_config",["switch_ble_state","1"]) + EventMesh.publish("send_ble_switch_toble","1") + + + EventMesh.publish("set_storage_config",["switch_bt_state","1"]) + EventMesh.publish("send_bt_switch_toble","1") + EventMesh.publish("play_tts",[play_str,1]) + # ret = 0 + # ret = ql_fs.path_exists("/usr/static/storage_file")#配置文件 + # if ret == True: + # ql_fs.rmdirs("/usr/static/storage_file") + # ql_fs.mkdirs("/usr/static/storage_file") + # utime.sleep_ms(2000) + # Power.powerRestart() + pass + else: + play_str = "设备解绑失败" + EventMesh.publish("play_tts",[play_str,1]) + pass + print("recv unbind_dev{}".format(msg)) +#用户解绑 +class IOTWeb_user_unbind_devProtoCol(ProtoCol): + """user_unbind_dev""" + TYPE = "user_unbind_dev" + def send(self, event, msg): + print("send user_unbind_dev{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + user_unbind_dev={ + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"user_unbind_dev", + "rest":"success", + "msg":"收到用户解绑设备指令" + } + } + send_data = b'' + send_data = protocol_send_total_data(user_unbind_dev) + print("user_unbind_dev{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发user_unbind_dev + @param msg: 接收到的下行数据 + """ + # { + # "cmd":"post_rest", + # "ts":1738283829, + # "msg":"消息收到响应", + # "data":{ + # "cmd":"user_unbind_dev", + # "rest":"success|error", + # "msg":"收到主动解绑设备指令" + # } + # } + self.btstate = 0 + self.blestate = 0 + EventMesh.publish("set_storage_config",["switch_ble_state","1"]) #用户解绑设备需要开启广播 开启经典蓝牙 + EventMesh.publish("send_ble_switch_toble","1") + + EventMesh.publish("set_storage_config",["switch_bt_state","1"]) + EventMesh.publish("send_bt_switch_toble","1") + EventMesh.publish(WEB_CMD_ENUM.USER_UNBIND_DEV_S) + tts_play_str = "设备已解绑" + EventMesh.publish("play_tts",[tts_play_str,1]) + # ret = 0 + # ret = ql_fs.path_exists("/usr/static/storage_file")#配置文件 + # if ret == True: + # ql_fs.rmdirs("/usr/static/storage_file") + # ql_fs.mkdirs("/usr/static/storage_file") + # utime.sleep_ms(100) + # Power.powerRestart() + print("recv user_unbind_dev{}".format(msg)) +#下发位子留言 +class IOTWeb_text_msgProtoCol(ProtoCol): + """text_msg""" + TYPE = "text_msg" + + def send(self, event, msg): + print("send text_msg{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + + rest = "error" + if msg == 1: + rest = "success" + text_msg = { + "cmd":"post_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"text_msg", + "rest":rest, + "msg":"收到文本消息." + } + } + send_data = b'' + send_data = protocol_send_total_data(text_msg) + print("send_data text_msg{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发voice_record + @param msg: 接收到的下行数据 + """ + print("recv time_tmp{}".format(msg)) + recv_state = 0 + if "data" in msg: + play_tts_text = msg["data"] + try: + EventMesh.publish("play_tts",[play_tts_text,1]) + recv_state = 1 + except Exception as e: + print("下发文字留言出错了") + EventMesh.publish(WEB_CMD_ENUM.TEXT_MSG_S_CMD,recv_state) + +#AI对话音色配置 +class IOTWeb_chat_configProtoCol(ProtoCol): + """chat_config""" + TYPE = "chat_config" + + def send(self, event, msg): + # print("send chat_config{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + chat_config = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"chat_config", + "rest":"success", + "msg":"收到AI对话配置" + } + } + send_data = b'' + send_data = protocol_send_total_data(chat_config) + print("send_data chat_config{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发chat_config + @param msg: 接收到的下行数据 + """ + print("recv chat_config{}".format(msg)) + # { + # "cmd":"chat_config", + # "ts":1738283829, + # "msg":"下发AI对话配置", + # "data":{ + # "speed":1.5, + # "vadDuration":600, + # "voiceId":"音色ID,火山音色或者科大讯飞音色ID" + # } + # } + # {'msg': '测试聊天配置.', 'data': {'botName': '柒乐乐', 'vadDuration': 600, 'speed': '1.0', 'provider': 'iflytek', 'voiceId': 'x3_linlin'}, + # 'ts': 1755600807553, 'cmd': 'chat_config'} + + if "data" in msg: + data = msg["data"] + speed = None + try: + speed = float(data["speed"]) + except Exception as e: + usys.print_exception(e) + vadDuration = data["vadDuration"] + voiceId = data["voiceId"] + provider = data["provider"] + botName = data["botName"] + asr = data["asr"] + if speed != None: + EventMesh.publish("SET_SPEECH_RATE",speed) + if vadDuration != None: + #if len(vadDuration) != 0: + EventMesh.publish("set_vad_silence",vadDuration) + tts_type = {} + if voiceId != None and len(voiceId) != 0: + tts_type["voice"] = voiceId + if provider != None and len(provider) != 0: + tts_type["outbound_attributess_provider"] = provider + if botName != None and len(botName) != 0: + tts_type["bot_name"] = botName + if voiceId != None and len(voiceId) != 0: + tts_type["voice"] = voiceId + if asr != None and len(asr) != 0: + tts_type["inbound_attributess_provider"] = asr + if len(tts_type)>0: + print("tts_type:{}".format(tts_type)) + EventMesh.publish("set_VC_TTS_voice_type",tts_type) + EventMesh.publish(WEB_CMD_ENUM.CHAT_CONFIG_S) +#获取AI对话音色配置 +class IOTWeb_get_chat_configProtoCol(ProtoCol): + """get_chat_config""" + TYPE = "get_chat_config" + + def send(self, event, msg): + # print("send chat_config{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + get_chat_config = { + "cmd":"get_chat_config", + "ts":time_tmp, + "msg":"获取AI对话配置", + "data":None + } + send_data = b'' + send_data = protocol_send_total_data(get_chat_config) + print("send_data chat_config{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发get_chat_config + @param msg: 接收到的下行数据 + """ + print("recv get_chat_config{}".format(msg)) + +#上报日程列表 +class IOTWeb_post_scheduleProtoCol(ProtoCol): + """post_schedule""" + TYPE = "post_schedule" + + def send(self, event, msg): + print("send post_schedule{}".format(msg)) + # 通过websocket发送到服务器端 + + send_data = b'' + send_data = protocol_send_total_data(msg) + print("send send_data{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + + def recv(self, event, msg): + """ + + @param event:post_schedule + @param msg: 接收到的下行数据 + """ + print("recv post_schedule{}".format(msg)) + + +#下发日程 +class IOTWeb_scheduleProtoCol(ProtoCol): + """schedule""" + TYPE = "schedule" + + def send(self, event, msg): + print("send schedule{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + schedule = { + "cmd":"post_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"schedule", + "rest":"success", + "msg":"收到日程列表." + } + } + send_data = b'' + send_data = protocol_send_total_data(schedule) + print("send_data schedule{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发schedule + @param msg: 接收到的下行数据 + """ + # { + # "cmd":"text_msg", + # "ts":1738283829, + # "msg":"下发文本消息", + # "data":"文本内容" + # } + print("recv time_tmp{}".format(msg)) + # { + # "cmd":"schedule", + # "ts":1738283829, + # "msg":"下发日程列表", + # "data":[ + # "title":"日程名称", + # "description":"提醒内容", + # "repeat":1~4播报重复次数, + # "date":"日期,yyyy-MM-dd", + # "时间":"HH:mm" + # ] + # } + schedule = [] + if "data" in msg: + data = msg["data"] + for i,sh in enumerate(data): + sh["index"] = i + schedule.append(sh) + schedule.sort(key=lambda x: x['index']) + EventMesh.publish("set_storage_config",["schedule",schedule]) + + schedule = EventMesh.publish("get_storage_config","schedule") + EventMesh.publish("set_schedule_info",schedule)# + print(schedule) + EventMesh.publish(WEB_CMD_ENUM.SCHEDULE_S_CMD) +#下发新的语音未读留言 +class IOTWeb_new_voice_msgProtoCol(ProtoCol): + """new_voice_msg""" + TYPE = "new_voice_msg" + + def send(self, event, msg): + print("send new_voice_msg{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + new_voice_msg = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"new_voice_msg", + "rest":"success", + "msg":"收到新语音消息指令." + } + } + send_data = b'' + send_data = protocol_send_total_data(new_voice_msg) + print("send send_data{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + EventMesh.publish(WEB_CMD_ENUM.GET_NEW_VOICE_MSG_S) + def recv(self, event, msg): + """ + + @param event:new_voice_msg + @param msg: 接收到的下行数据 + """ + EventMesh.publish(WEB_CMD_ENUM.NEW_VOICE_MSG_S) + + print("recv new_voice_msg{}".format(msg)) + +#设备主动查询未读的语音消息列表 +class IOTWeb_find_new_voice_msgProtoCol(ProtoCol): + """find_voice_msg_list""" + TYPE = "find_voice_msg_list" + def __init__(self): + super().__init__() + self.history_board_flag = 0#第一次进去要广播 + self.open_device = 0#开机播报未读留言 + self.has_processed_history_voice = 0 #播完列表不许再拨“历史未读留言为空” + EventMesh.subscribe("set_history_board_flag",self.set_history_board_flag) + EventMesh.subscribe("set_open_device",self.set_open_device) + EventMesh.subscribe("get_open_device",self.get_open_device) + def send(self, event, msg): + print("send find_voice_msg_list") + + def recv(self, event, msg): + """ + + @param event:find_voice_msg_list + @param msg: 接收到的下行数据 + """ + print("recv find_voice_msg_list{}".format(msg)) + + new_voice_list = []#字典列表 + voice_list = msg["data"] + print("msg[\"data\"] = {}".format(voice_list)) + if EventMesh.publish("get_history_voice_flag") == 1: + if len(voice_list) == 0:#为空 + if EventMesh.publish("get_wait_voice_flag") == 0:#未听历史留言 + tts_play_str = "已无更多历史留言" + EventMesh.publish("play_tts",[tts_play_str,0]) + self.history_board_flag = 0 + self.has_processed_wait_voice = 0 + self.wait_first_flag = 0 + # if self.has_processed_history_voice == 0: + # tts_play_str = "已无更多历史留言" + # EventMesh.publish("play_tts",[tts_play_str,0]) + # else: + # self.has_processed_history_voice = 0#读完了置为0 + # self.history_board_flag = 0#广播置为0 + # self.history_board_flag = 0 + # self.has_processed_wait_voice = 0 + # self.wait_first_flag = 0 + else:#等待历史留言 + self.has_processed_wait_voice = 0 + self.wait_first_flag = 0 + EventMesh.publish("set_recv_voice",0) #设置留言播放状态 + EventMesh.publish("set_history_voice_flag",0) + EventMesh.publish("set_wait_voice_flag",0) + else:#有历史留言 + if EventMesh.publish("get_wait_voice_flag") == 0:#历史留言 + print("历史广播状态!!!!! = {}".format(self.history_board_flag)) + if self.history_board_flag == 0: + tts_play_str = "您共有" + str(len(voice_list)) + "条历史留言,正在为您播放" + EventMesh.publish("play_tts",[tts_play_str,1]) + EventMesh.publish("set_pwk_notice_flag",1) + EventMesh.publish("set_pwk_cnt_voice",len(voice_list))#设置多,防止进入留言播完判断 + self.history_board_flag = 1 + self.has_processed_history_voice = 1 + for da in voice_list: + new_voice_list.append(da) + if len(new_voice_list) > 0: + pass + else:#未读留言 + if self.open_device == 1 or EventMesh.publish("get_60S_voice_flag") == 1: + EventMesh.publish("set_pwk_cnt_voice",len(voice_list))#设置未读次数,由于是开机一起查看的 + EventMesh.publish("set_60S_voice_flag",0) + for da in voice_list: + new_voice_list.append(da) + if len(new_voice_list) > 0: + pass + EventMesh.publish("set_play_new_voice_list",new_voice_list) + EventMesh.publish("play_new_voice_list") + else:#正常留言 + if EventMesh.publish("get_1M_flag") == 0: + if EventMesh.publish("get_pwk_cnt_voice") >= 0: + for da in voice_list: + new_voice_list.append(da) + if len(new_voice_list) > 0: + pass + EventMesh.publish("set_play_new_voice_list",new_voice_list) + EventMesh.publish("play_new_voice_list") + def set_history_board_flag(self,event,msg): + self.history_board_flag = msg + def set_open_device(self,event,msg): + self.open_device = msg + def get_open_device(self,event,msg): + return self.open_device +#请求未读的语音消息列表 +class IOTWeb_get_new_voice_msgProtoCol(ProtoCol): + """get_new_voice_msg""" + TYPE = "get_new_voice_msg" + + def send(self, event, msg): + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + # { + # "cmd":"get_new_voice_msg", + # "ts":1738283829, + # "msg":"请求未读的语音消息列表", + # "data":null + # } + get_new_voice_msg = { + "cmd":"get_new_voice_msg", + "ts":time_tmp, + "msg":"请求未读的语音消息列表", + "data":None + } + + send_data = b'' + send_data = protocol_send_total_data(get_new_voice_msg) + print("send get_new_voice_msg{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + + def recv(self, event, msg): + """ + + @param event:get_new_voice_msg + @param msg: 接收到的下行数据 + """ + # get_new_voice_msg = { + # "cmd":"post_rest", + # "ts":time_tmp, + # "msg":"消息收到响应", + # "data":{ + # "cmd":"get_new_voice_msg", + # "rest":"success", + # "msg":"收到请求新语音列表." + # } + # } + print("recv get_new_voice_msg{}".format(msg)) +# 1播放 0暂停 2上一首 3下一首 +class IOTWeb_player_controlProtoCol(ProtoCol): + """player_control""" + TYPE = "player_control" + + def send(self, event, msg): + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + paly_str = "停止" + paly_data = "stop" + if msg == 1: + paly_str = "播放" + paly_data = "play" + pass + elif msg == 0: + paly_str = "停止" + paly_data = "stop" + pass + elif msg == 2: + paly_str = "上一首" + paly_data = "pre" + pass + elif msg == 3: + paly_str = "下一首" + paly_data = "next" + pass + player_control = { + "cmd":"player_control", + "ts":time_tmp, + "msg":paly_str, + "data":paly_data + } + + send_data = b'' + send_data = protocol_send_total_data(player_control) + print("send player_control{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",msg) + + def recv(self, event, msg): + """ + + @param event:get_new_voice_msg + @param msg: 接收到的下行数据 + """ + # { + # "cmd":"post_rest", + # "ts":1738283829, + # "msg":"消息收到响应", + # "data":{ + # "cmd":"player_control", + # "rest":"success|error", + # "msg":"收到播放/停止/暂停指令/下一首/上一首" + # } + # } + paly_state = 0 + if "data" in msg: + data = msg["data"] + msg_str = data["msg"] + if msg_str.find("播放") != -1: + paly_state=1 + pass + elif msg_str.find("停止") != -1: + paly_state=0 + pass + elif msg_str.find("下一首") != -1: + paly_state = 3 + pass + elif msg_str.find("上一首") != -1: + paly_state = 2 + pass + + EventMesh.publish(WEB_CMD_ENUM.PLAY_CONTROL_S,paly_state) + print("recv send_data{}".format(msg)) +class IOTWeb_new_voice_msg_listProtoCol(ProtoCol): + """new_voice_msg_list""" + TYPE = "new_voice_msg_list" + def __init__(self): + super().__init__() + self._1M_flag = 0#判断一分钟定时器是否开启中 + self._1M_nowplay_flag = 0#判断当前留言播放是否结束去影响新留言播放的标志位 + self.app_new_voice_flag = 0#app是否下发留言 + self._1M_timer = osTimer()#多发留言间隙时间60S + self._60S_voice_flag = 0 + self.is_listen_wait = 0#是否收听未听留言 + self.close_timer_cnt = 0 #若两次未听定时器关闭 + EventMesh.subscribe("get_app_new_voice_flag",self.get_app_new_voice_flag) + EventMesh.subscribe("set_app_new_voice_flag",self.set_app_new_voice_flag) + EventMesh.subscribe("get_1M_flag",self.get_1M_flag) + EventMesh.subscribe("set_1M_nowplay_flag",self.set_1M_nowplay_flag) + EventMesh.subscribe("set_is_listen_wait",self.set_is_listen_wait) + EventMesh.subscribe("get_60S_voice_flag",self.get_60S_voice_flag) + EventMesh.subscribe("set_60S_voice_flag",self.set_60S_voice_flag) + + def send(self, event, msg): + print("send new_voice_msg_list{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + new_voice_msg_list = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"new_voice_msg_list", + "rest":"success", + "msg":"收到新语音列表." + } + } + send_data = b'' + send_data = protocol_send_total_data(new_voice_msg_list) + print("send send_data{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + + def recv(self, event, msg): + """ + + @param event:new_voice_msg_list + @param msg: 接收到的下行数据 + """ + self.app_new_voice_flag = 1#标志位为1 + new_voice_list = [] + if "data" in msg: + data = msg["data"] + for da in data: + new_voice_list.append(da) + if len(new_voice_list) > 0: + pass + EventMesh.publish("set_play_new_voice_list",new_voice_list) + print("是否收到留言:{},self._1M_flag = {}".format(EventMesh.publish("get_recv_voice"),self._1M_flag)) + if EventMesh.publish("get_recv_voice") == 0 and self._1M_flag == 0 and self._1M_nowplay_flag == 0:#当没有音频在播放时,再播放新的,否则保存下来不播放,等60S后一起播放 + EventMesh.publish("set_recv_voice",1) #设置留言播放状态 + EventMesh.publish("play_new_voice_list") + self._1M_nowplay_flag == 1 + else:#当有音频在播放时,存下来开始倒计时 + pass + if self._1M_flag == 0 : + print("等待留言60S倒计时开启") + self._1M_timer.start(60000, 0, self._1M_timer_cb) + self._1M_flag = 1 + EventMesh.publish(WEB_CMD_ENUM.NEW_VOICE_MSG_LIST_S) + print("recv new_voice_msg_list{}".format(msg)) + def _1M_timer_cb(self,*argc): + EventMesh.publish("publish_AI_btnInfo",{"func":self._1M_timer_cb_}) + + def _1M_timer_cb_(self): + #self.iot_web_send_queue.put("ping") + self._1M_flag = 0 + self._60S_voice_flag = 1 + get_VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag") + if get_VC_satrt_flag == None: + get_VC_satrt_flag = 0 + if get_VC_satrt_flag == 0: + print("收到多条信息,统一播放") + EventMesh.publish("set_history_voice_flag",1)#修改历史留言标志位 + EventMesh.publish("set_wait_voice_flag",1)#修改60S标志位 + EventMesh.publish(WEB_CMD_ENUM.GET_NEW_VOICE_MSG_S) + + def get_app_new_voice_flag(self,event,msg): + return self.app_new_voice_flag + def set_app_new_voice_flag(self,event,msg): + self.app_new_voice_flag = msg + def get_1M_flag(self,event,msg): + return self._1M_flag + def get_60S_voice_flag(self,event,msg): + return self._60S_voice_flag + def set_60S_voice_flag(self,event,msg): + self._60S_voice_flag = msg + def set_1M_nowplay_flag(self,event,msg): + self._1M_nowplay_flag = msg + def set_is_listen_wait(self,event,msg): + self.is_listen_wait = msg +class IOTWeb_put_voice_msg_stateProtoCol(ProtoCol): + """put_voice_msg_state""" + TYPE = "put_voice_msg_state" + + def send(self, event, msg): + print("send put_voice_msg_state{}".format(msg)) + # 通过websocket发送到服务器端 + + send_data = b'' + send_data = protocol_send_total_data(msg) + print("send send_data{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + + def recv(self, event, msg): + """ + + @param event:put_voice_msg_state + @param msg: 接收到的下行数据 + """ + print("recv put_voice_msg_state{}".format(msg)) +#收到蓝牙连接记录获取请求 +class IOTWeb_get_bt_connect_record_listProtoCol(ProtoCol): + """get_bt_connect_record_list""" + TYPE = "get_bt_connect_record_list" + + def send(self, event, msg): + print("send get_bt_connect_record_list{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + get_bt_connect_record_list = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"get_bt_connect_record_list", + "rest":"success", + "msg":"收到蓝牙连接记录获取请求" + } + } + send_data = b'' + send_data = protocol_send_total_data(get_bt_connect_record_list) + print("send send_data{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND",send_data) + + def recv(self, event, msg): + """ + + @param event:get_bt_connect_record_list + @param msg: 接收到的下行数据 + """ + # { + # "cmd":"get_bt_connect_record_list", + # "ts":1738283829, + # "msg":"上报蓝牙连接列表", + # "data": None + # } + EventMesh.publish("send_get_connect_bt_state") + EventMesh.publish(WEB_CMD_ENUM.GET_BT_CONNECT_RECORD_LIST_S) + print("recv get_bt_connect_record_list{}".format(msg)) + +#蓝牙开关 +class IOTWeb_switch_bt_stateProtoCol(ProtoCol): + """switch_bt_state""" + TYPE = "switch_bt_state" + + def send(self, event, msg): + # print("send switch_bt_state{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + # { + # "cmd":"dispatch_rest", + # "ts":1738283829, + # "msg":"消息收到响应", + # "data":{ + # "cmd":"switch_bt_state", + # "rest":"success|error", + # "msg":"收到蓝牙已打开/蓝牙已关闭指令" + # } + # } + s_msg = "蓝牙已关闭指令" + if msg == "1": + s_msg = "收到蓝牙已打开" + elif msg == "0": + s_msg = "蓝牙已关闭指令" + switch_bt_state = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"switch_bt_state", + "rest":"success", + "msg":s_msg + } + } + send_data = b'' + send_data = protocol_send_total_data(switch_bt_state) + print("send_data switch_bt_state{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发switch_bt_state + @param msg: 接收到的下行数据 + """ + s_state = "1" + if "data" in msg: + data = msg["data"] + if data == "0": + s_state = "0" + EventMesh.publish("set_storage_config",["switch_bt_state","0"]) + EventMesh.publish("send_bt_switch_toble","0") + pass + elif data == "1": + s_state = "1" + EventMesh.publish("set_storage_config",["switch_bt_state","1"]) + EventMesh.publish("send_bt_switch_toble","1") + pass + print("recv switch_bt_state{}".format(msg)) + + EventMesh.publish(WEB_CMD_ENUM.SWITCH_BT_STATE_S_CMD,s_state) + +#蓝牙扫描 +class IOTWeb_bt_scanProtoCol(ProtoCol): + """bt_scan""" + TYPE = "bt_scan" + + def send(self, event, msg): + print("send schedule{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + s_msg = "" + if msg == "1": + s_msg = "开始扫描附近设备" + elif msg == "0": + s_msg = "停止扫描附近设备" + bt_scan = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"bt_scan", + "rest":"success", + "msg":s_msg + } + } + send_data = b'' + send_data = protocol_send_total_data(bt_scan) + print("send_data bt_scan{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发bt_scan + @param msg: 接收到的下行数据 + """ + # { + # "cmd":"bt_scan", + # "ts":1738283829, + # "msg":"开始扫描附近设备/停止扫描附近设备", + # "data":"0/1" + # } + s_ret = "1" + if "data" in msg: + data = msg["data"] + if data == "1": + s_ret = "1" + EventMesh.publish("send_ble_scan","1") + elif data == "0": + s_ret = "0" + EventMesh.publish("send_ble_scan","0") + + pass + + print("recv bt_scan{}".format(msg)) + EventMesh.publish(WEB_CMD_ENUM.BT_SCAN_S_CMD,s_ret) + +#蓝牙当前状态 +class IOTWeb_get_bt_stateProtoCol(ProtoCol): + """get_bt_state""" + TYPE = "get_bt_state" + + def send(self, event, msg): + print("send schedule{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + bt_state ={ + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"get_bt_state", + "rest":msg, + "msg":"上报蓝牙开关状态" + } + } + send_data = b'' + send_data = protocol_send_total_data(bt_state) + print("send_data bt_state{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发bt_state + @param msg: 接收到的下行数据 + """ + print("recv bt_state{}".format(msg)) + switch_bt_state = EventMesh.publish("get_storage_config","switch_bt_state") + if switch_bt_state == None: + switch_bt_state = "1" + + EventMesh.publish(WEB_CMD_ENUM.GET_BT_STATE_S,str(switch_bt_state)) + +#蓝牙扫描列表 +class IOTWeb_bt_scan_restProtoCol(ProtoCol): + """bt_scan_rest""" + TYPE = "bt_scan_rest" + + def send(self, event, msg): + print("send bt_scan_rest{}".format(msg)) + # 通过websocket发送到服务器端 + + send_data = b'' + send_data = protocol_send_total_data(msg) + print(" bt_scan_rest{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发bt_scan_rest + @param msg: 接收到的下行数据 + """ + print("recv bt_scan_rest{}".format(msg)) + + +#连接目标蓝牙设备 +class IOTWeb_connect_btProtoCol(ProtoCol): + """connect_bt""" + TYPE = "connect_bt" + + def send(self, event, msg): + print("send connect_bt{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + s_ret = "success" + if msg == 0: + s_ret = "error" + connect_bt = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"connect_bt", + "rest":s_ret, + "msg":"收到蓝牙连接请求" + } + } + send_data = b'' + send_data = protocol_send_total_data(connect_bt) + print("send_data bt_state{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发connect_bt + @param msg: 接收到的下行数据 + """ + # { + # "cmd":"connect_bt", + # "ts":1738283829, + # "msg":"连接蓝牙设备", + # "data":"目标设备mac地址" + # } + ok = 1 + if "data" in msg: + mac = msg["data"] + # mac = "36:72:cc:e6:dd:e8"#deep + # mac = "61:E8:6D:CD:7F:17" #Q36 + # mac = "75:E8:8C:62:9A:DF" Fineblue Clip + if len(mac) == 17: + ok = 1 + EventMesh.publish("send_connect_bt_mac",mac) + else: + ok = 0 + EventMesh.publish(WEB_CMD_ENUM.CONNECT_BT_S,ok) + print("recv connect_bt{}".format(msg)) +#上报连接记录 +class IOTWeb_bt_connect_record_listProtoCol(ProtoCol): + """bt_connect_record_list""" + TYPE = "bt_connect_record_list" + + def send(self, event, msg): + print("send bt_connect_record_list{}".format(msg)) + # 通过websocket发送到服务器端 + send_data = b'' + send_data = protocol_send_total_data(msg) + print("send_data bt_state{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发bt_connect_record_list + @param msg: 接收到的下行数据 + """ + # { + # "cmd":"post_rest", + # "ts":1738283829, + # "msg":"消息收到响应", + # "data":{ + # "cmd":"bt_connect_record_list", + # "rest":"success|error", + # "msg":"收到蓝牙连接记录列表" + # } + # } + print("recv bt_connect_record_list{}".format(msg)) +#断开设备蓝牙连接 +class IOTWeb_disconnect_btProtoCol(ProtoCol): + """disconnect_bt""" + TYPE = "disconnect_bt" + + def send(self, event, msg): + print("send disconnect_bt{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + disconnect_bt = { + "cmd":"dispatch_rest", + "ts":time_tmp, + "msg":"消息收到响应", + "data":{ + "cmd":"disconnect_bt", + "rest":"success", + "msg":"断开蓝牙设备连接请求" + } + } + send_data = b'' + send_data = protocol_send_total_data(disconnect_bt) + print("send_data bt_state{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + + @param event:上行下发disconnect_bt + @param msg: 接收到的下行数据 + """ + #{ + # 'msg': '断开设备蓝牙', + # 'data': 'AB:12:34:56:78', + # 'ts': 1754555005787, + # 'cmd': 'disconnect_bt'} + if "data" in msg: + mac = msg["data"] + EventMesh.publish("send_disconnect_bt_mac",mac) + print("recv disconnect_bt{}".format(msg)) + EventMesh.publish(WEB_CMD_ENUM.DISCONNECT_BT_S) +class IOTWeb_find_deviceProtoCol(ProtoCol): + """find_device""" + TYPE = "find_device" + + def send(self, event, msg): + # print("c{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + come_trans = { + "msg": "查找设备", + "data": None, + "cmd": "find_device", + "ts": time_tmp + } + send_data = b'' + send_data = protocol_send_total_data(come_trans) + print("send_data find_device{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + @param event:上行下发translate_app_enter + @param msg: 接收到的下行数据 + """ + tts_str = "我在这呀,我在这呀,我在这呀 " + EventMesh.publish("play_tts",[tts_str,1]) + +#接收小程序控制上传设备状态 +class IOTWeb_get_device_stateProtoCol(ProtoCol): + """get_device_state""" + TYPE = "get_device_state" + + def send(self, event, msg): + battery_state = (EventMesh.publish("screen_get_battery"))[0] + battery = (EventMesh.publish("screen_get_battery"))[1] + if battery >= 100 or battery == None: + battery = 100 + VC_TTS_volume = EventMesh.publish("get_storage_config","VC_TTS_volume")#音量配置 + if VC_TTS_volume == None: + VC_TTS_volume =100 + Timeversion = version_util.get_unified_version() + ts = utime.mktime(utime.localtime()) + come_trans = { + "data":{ + "version":Timeversion,#版本号 + "charge":battery_state,#0表示未充电,1表示充电中 + "currentRoleName":"",#当前音色的name + "volume":str(VC_TTS_volume),#百分比数值 + "wifiRssi":str(net.csqQueryPoll()//6),#wifi强弱:0,1,2,3,4,5。转成这几个梯度 + "battery":str(battery),#电量 + "bargein": "0",#不能手动设置打断模式 + "deviceId":modem.getDevImei(),#设备号 + }, + "cmd":"device_info", + "msg": "上报设备信息", + "ts": ts + } + send_data = b'' + send_data = protocol_send_total_data(come_trans) + print("send_data get_device_state{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + @param event:get_device_state + @param msg: 接收到的下行数据 + """ + EventMesh.publish(WEB_CMD_ENUM.GET_DEVICE_STATE_S) + +#接收小程序控制设备音量 +class IOTWeb_switch_volumeProtoCol(ProtoCol): + """switch_volume""" + TYPE = "switch_volume" + + def send(self, event, msg): + # print("c{}".format(msg)) + # 通过websocket发送到服务器端 + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + come_trans = { + "msg": "设置音量", + "data": None, + "cmd": "switch_volume", + "ts": time_tmp + } + send_data = b'' + send_data = protocol_send_total_data(come_trans) + print("send_data switch_volume{}".format(send_data)) + EventMesh.publish("CHANNEL_IOT_WEB_SEND", send_data) + + def recv(self, event, msg): + """ + @param event:switch_volume + @param msg: 接收到的下行数据 + """ + volume = int(msg["data"]["target"]) + 100 + print("音量修改为:{}".format(volume-100)) + EventMesh.publish("change_volume",volume) + + +class ProtoColResolver(object): + def __init__(self): + self.protocol_list = [] + + def add_support(self, prot): + self.protocol_list.append(prot) + prot.initialization() + + def handler(self, data): + # 这里接收数据 + + EventMesh.debug_log("handler -> {}".format(len(data))) + # if data.startswith('Init;'): + # data = data[5:] + # EventMesh.publish(constants.EVENT.SET_KEY, {"value": ujson.loads(data)['KEY']}) + + # elif data.startswith("Update;"): + # data = data[7:] + # EventMesh.publish(constants.PROTOCOL.H04_RECV, data) + try: + if len(data) > 0: + playpolad_data(data) + # EventMesh.publish(ProtoCol_type, data) #返回协议read + except Exception as e: + EventMesh.debug_log("handler:ERROR:") + usys.print_exception(e) + EventMesh.debug_log("handler:error data:{}".format(len(data))) diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_auth.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_auth.py new file mode 100644 index 00000000..398b1272 --- /dev/null +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_auth.py @@ -0,0 +1,476 @@ +# -*- coding: utf-8 -*- +"""设备鉴权与 token。""" +from common import Abstract +try: + from qp_log import qp_print as print +except Exception: + pass +import EventMesh +import usys +import utime +import uhashlib +import ubinascii +import ujson +import osTimer +import modem +import net +import sim +import machine +import base64 +import constants +import ucryptolib +import urandom as random +import request +try: + import vc_config +except Exception: + vc_config = None + +class device_res_author(Abstract): + + def __init__(self) : + print("device_res_author __init__ start !!!!!!!!!!!!!!!!!") + self.deviceId = modem.getDevImei()#"876226002022574" + self.SHA1_BLOCK_SIZE = 64 + self.PAD_BLOCK_SIZE = 16 + self.SHA256 = uhashlib.sha256 + self.pad = lambda s: s + chr(self.PAD_BLOCK_SIZE - len(s) % self.PAD_BLOCK_SIZE) * (self.PAD_BLOCK_SIZE - len(s) % self.PAD_BLOCK_SIZE) + # 除去补16字节的多余字符 + self.unpad = lambda s: s[:-s[-1]] + # 设备鉴权:优先 default_config / usr/default_config 的 PRODUCT_PID、PRODUCT_KEY + self.pid = "cqxJZi5CNgzjS3mX21Zw" + self.key = b"uKLrChwrxA1LklgBc3VwawYjUkbT3BgX" + try: + if "vc_config" in globals() and vc_config: + _pid = vc_config.get_attr("PRODUCT_PID", None) + if _pid: + self.pid = str(_pid) + _key = vc_config.get_attr("PRODUCT_KEY", None) + if _key is not None and _key != "": + self.key = _key if isinstance(_key, bytes) else str(_key).encode() + except Exception: + pass + self.dev_res_timer = osTimer() + self.dev_res_timer.start(20000, 1, self.dev_timer_cb) + self.dev_res_timerCount = 0 + self.ud_token_timer = osTimer() + self.ud_token_timer.start(10000, 1, self.ud_token_timer_cb) + self.ud_token_timer_Count = 0 + self.dev_token = None + self.res_error_count = 0 + self.ntptime_flag = 0 + EventMesh.subscribe("get_dev_token", self.get_dev_token) + EventMesh.subscribe("update_dev_token", self.update_dev_token) + EventMesh.subscribe("dev_active", self.dev_active) + EventMesh.subscribe("dev_sign_in", self.dev_sign_in) + EventMesh.subscribe("dev_NTP", self.dev_NTP) + EventMesh.subscribe("post_iccid_user", self.post_iccid_user) + # self.post_iccidd_token_timer = osTimer() + # self.post_iccidd_token/_timer.start(10000, 0, self.post_iccidd_token_timer_cb) + self.connect_web_flag = 0 + #EventMesh.publish_async("dev_NTP",thread_size = 8192) + + # count = 0 + # while True: + # if self.ntptime_flag == 1: + # break + # utime.sleep_ms(1) + # count+=1 + # if count>600: + # break + # EventMesh.publish_async("dev_active",thread_size = 8192) + # EventMesh.publish_async("dev_sign_in",thread_size = 8192) + + self.dev_NTP()#获取时间 + self.dev_active() #设备激活 + self.dev_sign_in() #设备登入 + + + print("device_res_author __init__ end !!!!!!!!!!!!!!!!!") + def post_iccidd_token_timer_cb(self,*argc): + EventMesh.publish("post_iccid_user") + + def get_dev_token(self, event, msg): + return self.dev_token + #网络校时 + def dev_NTP(self, event=None, msg=None): + response = None + try: + print("dev_NTP1") + url = constants.DMP_BASE + "/dev/time" + print("dev_NTP:{}".format(url)) + response = request.get(url,timeout=5) + data = response.json() + print("dev_NTP:{}".format(data)) + if data: + try: + timeSecond = data["data"]["timestamp"]//1000 + + next_alarm_time = utime.localtime(timeSecond) + print("timeSecond {} next_alarm_time {}".format(timeSecond,next_alarm_time)) + machine.RTC().datetime((next_alarm_time[0],next_alarm_time[1],next_alarm_time[2],next_alarm_time[6],next_alarm_time[3],next_alarm_time[4],next_alarm_time[5],0)) + self.ntptime_flag = 1 + except Exception as e: + usys.print_exception(e) + except Exception as e: + usys.print_exception(e) + if response != None: + response.close() + print("dev_NTP end") + def dev_timer_cb(self, *args): #设备资源管理鉴权平台定时器 + EventMesh.publish("publish_AI_btnInfo",{"func":self.dev_timer_cb_}) + + def dev_timer_cb_(self): + #if EventMesh.DEV_RES_MANAGE_AUTHEN_PLATFORM == 1: + try: + if self.dev_token == None: + if self.ntptime_flag == 0: + self.ntptime_flag = 1 + EventMesh.publish_async("dev_NTP",thread_size = 8192) + #EventMesh.publish("publish_AI_btnInfo",{"func":self.dev_NTP}) + print("timer:{}!!!!!!!!\r\n".format(utime.localtime())) + EventMesh.publish_async("dev_active",thread_size = 8192) + EventMesh.publish_async("dev_sign_in",thread_size = 8192) + # self.dev_active() #设备激活 + # self.dev_sign_in() #设备登入 + self.dev_res_timerCount+=1 + if self.dev_res_timerCount > 3: #开机一分钟内没有获取到密码和token 等下一次更新令牌在走登入 激活流程 + self.dev_res_timerCount=0 + self.dev_res_timer.stop() + except Exception as e: + usys.print_exception(e) + def ud_token_timer_cb(self, *args): + EventMesh.publish("publish_AI_btnInfo",{"func":self.ud_token_timer_cb_}) + + def ud_token_timer_cb_(self): + try: + #print("ud_token_timer_!!!!") + self.ud_token_timer_Count+=1 + if self.ud_token_timer_Count >= 720: #定时10*3000s时更新token + self.ud_token_timer_Count = 0 + EventMesh.publish_async("dev_sign_in",thread_size = 8192) + except Exception as e: + usys.print_exception(e) + def get_sim_Status(self): + return sim.getStatus() + def HMAC(self,k, m): + + KEY_BLOCK = k + (b'\0' * (self.SHA1_BLOCK_SIZE - len(k))) + KEY_INNER = bytes((x ^ 0x36) for x in KEY_BLOCK) + KEY_OUTER = bytes((x ^ 0x5C) for x in KEY_BLOCK) + inner_message = KEY_INNER + m + outer_message = KEY_OUTER + self.SHA256(inner_message).digest() + return self.SHA256(outer_message) + def get_hmac_sha256(self,k, m): + data = self.HMAC(k, m).digest() + # print("HMAC(k, m).digest():", data) + # print("HMAC(k, m).digest():", ubinascii.hexlify(data)) + hex_data = ubinascii.hexlify(data) + hex_data = hex_data.decode('ascii') + return hex_data + # 加密函数 + def aes_CBC_Encrypt(self,data, key, iv): # CBC模式的加密函数,data为明文,key为16字节密钥,iv为16字节的偏移量 + key = key.encode('utf-8') + iv = iv.encode('utf-8') # CBC 模式下的偏移量 + data = self.pad(data) # 补位 + data = data.encode('utf-8') + aes = ucryptolib.aes(key,ucryptolib.MODE_CBC,iv) + result = base64.b64encode(aes.encrypt(data)) + return str(result, 'utf-8') # 以字符串的形式返回 + + # 解密函数 + def aes_CBC_Decrypt(self,data, key, iv): # CBC模式的解密函数,data为密文,key为16字节密钥 + key = key.encode('utf-8') + iv = iv.encode('utf-8') + + aes = ucryptolib.aes(key,ucryptolib.MODE_CBC,iv) #CBC模式 + + # decrypt AES解密 B64decode为base64 转码 + result = aes.decrypt(base64.b64decode(data)) + result = self.unpad(result) # 除去补16字节的多余字符 + return str(result, 'utf-8') # 以字符串的形式返回 + #激活接口 没有密码走激活 + def dev_active(self, event=None, msg=None): + signal = net.csqQueryPoll() + if signal == -1 or self.get_sim_Status() != 1:#网络不正常 + print("dev_active 无 SIM卡!!!!!!!!\r\n") + return + print("dev_active 1\r\n") + # 签名内容: = deviceId+pid+timeStamp + dev_pass = "" + dev_pass = EventMesh.publish("get_storage_config","dev_pass") + print("dev_pass:{}\r\n".format(dev_pass)) + if dev_pass == None: + dev_pass = "" + if dev_pass == "" or dev_pass == None: + try: + date_tmp = utime.localtime() #获取当前时间 + print("date_tmp:{}\r\n".format(date_tmp)) #转换时间戳s + timeStamp = (utime.mktime(date_tmp))*1000#获取时间戳ms + deviceId = self.deviceId #"876226002022574" #以imei号 + pid = self.pid + key = self.key + sign_data = "{}{}{}".format(deviceId,pid,timeStamp) #签名内容 + print("sign_data:{}\r\n".format(sign_data)) + sha256_hex = self.get_hmac_sha256(key,sign_data) + print("sha256_hex:{}\r\n".format(sha256_hex)) + + data = { + "deviceId":"{}".format(deviceId), #设备名 + "pid":pid, #产品PID + "sign":"{}".format(sha256_hex), #HMACSHA256签名后的数据 + "timeStamp":timeStamp, #要以ms的形式 + "signMethod":"HMACSHA256" #签名方式 HMACSHA256 + } + print(ujson.dumps(data)) + headers_T ={"Content-Type":"application/json"} + url = constants.DMP_BASE + "/dev/auth/active" + print("dev_active:{}".format(url)) + data =ujson.dumps(data) + response = None + response_result = 0 + try: + response = request.put(url,data=data,headers = headers_T,timeout=5) + response_result = 1 + except Exception as e: + usys.print_exception(e) + print("put error!!!!!\r\n") + response_result = 0 + + IV = "" #偏移量 + PASSWORD= "" #服务器返回后的加密内容 密码 + if response_result == 1: #访问成功ip + if response.status_code == 200: + print("headers:{}".format(response.headers)) + content = response.content + c = next(content) + # "api.xcloudtech.com" + # d_server_port = 9000 + data = ujson.loads(c) + # data = response.json() + print("data-{}".format(data)) + if data != None: + if "success" in data: + if data['success'] == True: + if "data" in data: + data_tmp = data["data"] + if "nonce" in data_tmp: #偏移量 + IV = data_tmp["nonce"] + if "password" in data_tmp: + PASSWORD = data_tmp["password"] + # EventMesh.publish("set_storage_config",["dev_pass",PASS]) + if data["code"] == 20001:#鉴权成功 + pass + elif data["code"] > 50000:# + self.res_error_count+=1 + if self.res_error_count>=3: + self.res_error_count = 0 + + EventMesh.publish("paly_device_res_error") + if data["code"] == 50003:#时间错误 + self.ntptime_flag = 0 + if PASSWORD != None and IV != None: + if len(PASSWORD) != 0 and len(IV) != 0: + password = PASSWORD + iv = IV + #解密 之后保存本地下一次登入做使用 + decrypt_data = self.aes_CBC_Decrypt(password, self.key, iv) #对服务器下发的密码进行解密 + print("decrypt_data {}".format(decrypt_data)) + EventMesh.publish("set_storage_config",["dev_pass",decrypt_data]) + print("IV:{}\r\n PASS:{}".format(IV,PASSWORD)) + if response != None: + response.close() + except Exception as e: + usys.print_exception(e) + print("dev_active end\r\n") + return [response_result,PASSWORD,IV] + #随机生成16个字节的字符串作为加密的IV 偏移量 + def generate_random_string(self,length=16): + random_str = "" + chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' + for i in range(length): + random_str+=chars[random.randint(0,length)] + return random_str + + #设备登入 没有Token 走登入获取token + def dev_sign_in(self, event=None, msg=None): + + + signal = net.csqQueryPoll() + if signal == -1 or self.get_sim_Status() != 1:#网络不正常 + print("dev_sign_in 无 SIM卡!!!!!!!!\r\n") + return + print("dev_sign_in 1") + token = "" + try: + token = self.dev_token + dev_pass = EventMesh.publish("get_storage_config","dev_pass") + #print("token:{}\r\n".format(token)) + if token == None: + token = "" + if dev_pass == None: + dev_pass = "" + if len(dev_pass) == 0:#没有密码 + return + #if token == None or token == "": + if 1: #返回结果成功 + #获取本地保存的密码 + decrypt_data=EventMesh.publish("get_storage_config","dev_pass") + # decrypt_data = self.aes_CBC_Decrypt(password, self.key, iv) #对服务器下发的密码进行解密 + print("decrypt_data {}".format(decrypt_data)) + #随机生成16个字符串 设备生成做加密使用 + random_string = self.generate_random_string(16) #随机的16个字符串 + enc_iv = random_string #加密的IV偏移量 + #设备端进行密码加密 + encrypt_data = self.aes_CBC_Encrypt(decrypt_data, self.key, enc_iv) + print("encrypt_data {}".format(encrypt_data)) + + #登入数据 + deviceId = self.deviceId + sign_in_data = { + "deviceId":"{}".format(deviceId), #设备ID + "password":"{}".format(encrypt_data), #加密的数据 + "nonce":"{}".format(random_string) #随机的16个字符串 + } + print(ujson.dumps(sign_in_data)) + sign_url =None + sign_url = constants.DMP_BASE + "/dev/auth/sign_in" + #获取验证码 + #url = "http://dmp.qipai.com.cn:30461/dev/device/bind-code/860132066060377" + # token = EventMesh.publish("get_dev_token") + # headers_T = {"Content-Type":"application/x-www-form-urlencoded","Accept":"*/*","bz-dt":"{}".format(token)} + # response = request.get(url,headers = headers_T,timeout=5) + # data = response.json() + print("dev_sign_in:{}".format(sign_url)) + headers_T ={"Content-Type":"application/json"} + response_result = 0 + response = 0 + try: + response = request.post(sign_url,data=ujson.dumps(sign_in_data),headers = headers_T,timeout=5) + response_result = 1 + except Exception as e: + usys.print_exception(e) + print("post error!!!!!\r\n") + response_result = 0 + # {'msg': '', 'code': 20001, 'success': True, + # 'data': {'deviceId': '876226002022574', + # 'token': '95436a9e09fd0ff89feb8825257dca982f'} + # } + + #self.dev_token = "" + if response_result == 1: + if response.status_code == 200: + content = response.content + c = next(content) + data = ujson.loads(c) + print("data-{}".format(data)) + if data != None: + if "success" in data: + if data['success'] == True: #登入成功 + if "data" in data: + data_tmp = data["data"] + if "token" in data_tmp: #token + self.dev_token = data_tmp["token"] + else:#报错清空密钥 + print("登录失败,清空密钥") + EventMesh.publish("set_storage_config",["dev_pass",""]) + if data["code"] == 50003:#时间错误 + self.ntptime_flag = 0 + + print("token:{}\r\n".format(self.dev_token)) + if response != None: + response.close() + pass + except Exception as e: + print("post error!!!!!\r\n") + response_result = 0 + if EventMesh.publish("get_dev_token") != None: + if len(EventMesh.publish("get_dev_token")) != 0: + if self.connect_web_flag == 0: + EventMesh.publish("post_iccid_user") + self.connect_web_flag = 1 + print("dev_sign_in end") + #ICCID上传 + def post_iccid_user(self,event=None,msg=None): + response = None + try: + iccid = sim.getIccid() + if iccid != -1 and len(iccid) > 5: + print("pots_ICCID:{}".format(iccid)) + #Headers = {'Transfer-Encodong':'chunked'} + token = EventMesh.publish("get_dev_token") + headers_T = None + if token: + headers_T = {"bz-dt":"{}".format(token)} + #http://dmp.raisound.com/bz-api/dev/iccid?deviceId=866610001099679&iccid=89860002192307889057 + url =None + url = (constants.DMP_BASE + "/dev/iccid?deviceId={}&iccid={}").format(modem.getDevImei(),iccid) + print("{} H:{}".format(url,headers_T)) + response = request.post(url,headers = headers_T) + if response.status_code == 200: + data = response.json() + print("pots_ICCID:{}".format(data)) + else: + print("pots_ICCID error!!!!") + for i in response.text: + print(i) + response.close() + except Exception as e: + usys.print_exception(e) + if response != None: + response.close() + #获取新的令牌 更新token + def update_dev_token(self, event=None, msg=None): + print("update_dev_token") + try: + dev_pass = "" + dev_pass = EventMesh.publish("get_storage_config","dev_pass") + + if dev_pass == None: + dev_pass = "" + if len(dev_pass) == 0: #没有密码走激活 + print("no dev_pass:{}\r\n".format(dev_pass)) + EventMesh.publish("dev_active") + token = self.dev_token + if token == None: + token = "" + if len(token) == 0: #没有token走登入 + print("no dev_token:{}\r\n".format(token)) + #self.dev_sign_in() + EventMesh.publish("dev_sign_in") + dev_tokenip =None + dev_tokenip = constants.DMP_BASE + "/dev/auth/token" + + token = self.dev_token + if token == None: + token = "" + if len(token) != 0: + response_result=0 + response = None + headers_T = {"bz-dt":"{}".format(token)} + try: + response = request.get(dev_tokenip,headers = headers_T,timeout=5) + response_result = 1 + except Exception as e: + print("get error!!!!!\r\n") + response_result = 0 + if response_result == 1: + if response.status_code == 200: + try: + headers_data = response.headers + # print("headers_data!!!!! headers_data:{}\r\n".format(headers_data)) + if "bz-dt" in headers_data: #bz-dt字段是否在头部数据里面 如果有就需要更新 + bz_dt = headers_data["bz-dt"] + self.dev_token = bz_dt.replace(" ", "") #去掉所有的空格符 + print("update_dev_token:{}".format(self.dev_token)) + except Exception as e: + print("headers_data!!!!! error\r\n") + if response != None: + response.close() + except Exception as e: + usys.print_exception(e) + print("update_dev_token end") + + + + diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_buttons.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_buttons.py new file mode 100644 index 00000000..bf7ac804 --- /dev/null +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_buttons.py @@ -0,0 +1,1102 @@ +# -*- coding: utf-8 -*- +"""按键、电源、USB、按键队列。""" +from common import Abstract +try: + from qp_log import qp_print as print +except Exception: + pass +import EventMesh +import usys +import utime +import audio +import osTimer +import dataCall +import sim +from machine import Pin, ExtInt +from machine import WDT +import modem +import ntptime +import _thread +from queue import Queue +from misc import Power, USB, PowerKey +import constants +try: + import vc_config +except Exception: + vc_config = None +from vc_common import dev_type +from vc_hw import set_ipv4_datacall +from vc_uart import AI_UART + +audio_buf = ['chaobingfatis', 'chenggong', 'didianguanji', 'IMEIhao', 'jianquancuowu', 'key_prompt', 'liuliangtishi', 'Low_power_warning', 'R_CHARGE_FULL', 'R_CHARGE_IN', 'R_CHARGE_OUT', 'R_READ_TIME_0', 'R_READ_TIME_1', 'R_READ_TIME_2', 'R_READ_TIME_3', 'R_READ_TIME_4', 'R_READ_TIME_5', 'R_READ_TIME_6', 'R_READ_TIME_7', 'R_READ_TIME_8', 'R_READ_TIME_9', 'R_READ_TIME_A', 'R_READ_TIME_B', 'R_READ_TIME_C', 'R_READ_TIME_D', 'R_READ_TIME_E', 'R_READ_TIME_F', 'R_READ_TIME_G', 'R_READ_TIME_H', 'R_READ_TIME_I', 'R_READ_TIME_J', 'R_READ_TIME_K', 'R_READ_TIME_L', 'R_READ_TIME_M', 'R_READ_TIME_N', 'R_READ_TIME_O', 'R_READ_TIME_P', 'R_READ_TIME_Q', 'R_READ_TIME_R', 'R_READ_TIME_S', 'R_READ_TIME_T', 'R_READ_TIME_U', 'R_READ_TIME_V', 'R_READ_TIME_W', 'R_READ_TIME_X', 'R_READ_TIME_Y', 'R_READ_TIME_Z', 'send_tone', 'shouhou', 'xinhaoruo1', 'zhengzaiguanji'] +class AI_dev(Abstract): + # AI + def __init__(self): + # if(dev_type != "7PIN"): + # set_ipv4_datacall() + self.is_wdt = 0 #是否开启看门狗 + #AT+QDAI=1,0,0,1,0,0,1,1PCM音频输出设置为主模式、短同步、16 kHz采样率和256 kHz时钟频率,采样位深为16bit,使用的时间间隙数量为1,并且使用第一个时间间隙。 + try: + bt_pm_en = False + pin_bt_on = None + pin_mot = None + qdai_cfg = None + if "vc_config" in globals() and vc_config: + bt_pm_en = bool(vc_config.get_attr("BT_POWER_MOTOR_ENABLED", False)) + pin_bt_on = vc_config.get_attr("PIN_BT_ON_GPIO", None) + pin_mot = vc_config.get_attr("PIN_MOT_GPIO", None) + qdai_cfg = vc_config.get_attr("AUDIO_QDAI_CFG", None) + except Exception: + bt_pm_en = False + pin_bt_on = None + pin_mot = None + qdai_cfg = None + + if bt_pm_en: + try: + if pin_bt_on: + self.PIN_BT_ON = Pin(getattr(Pin, pin_bt_on), Pin.OUT, Pin.PULL_DISABLE, 0) + if pin_mot: + self.PIN_MOT = Pin(getattr(Pin, pin_mot), Pin.OUT, Pin.PULL_DISABLE, 1) + except Exception: + pass + try: + utime.sleep_ms(200) + if hasattr(self, "PIN_MOT"): + self.PIN_MOT.write(0) + if hasattr(self, "PIN_BT_ON"): + self.PIN_BT_ON.write(1) + except Exception: + pass + try: + if isinstance(qdai_cfg, (tuple, list)) and len(qdai_cfg) == 8: + audio.set_qdai_cfg(*qdai_cfg) # 用iis做音频输入输出 + except Exception: + pass + self.KeyAI_status = 0 + self.KeyAI_timer_count = 0 + self.KeyAI_long_press = 0 + self.KeySos_long_press = 0 + self.KeyAI_press = 0 + self.last_ai_key_time = 0#让ai按键不要频繁调用 + if self.is_wdt == 1: + self.wdt = WDT(30) #开启看门狗 + # self.wdt_timer = osTimer() + # self.wdt_timer.start(5000, 1, self.wdt_timer_cb) + + count = 0 + while True: + count += 1 + if count > 20 : + break + data = dataCall.getInfo(1, 0) + print("data call INFO {}".format(data)) + if data[2][0]: + self.network_state = 1 + break + print("等待网络 {}...".format(count)) + utime.sleep_ms(500) + self.KeyAI_timer = osTimer() + self.KeySOS_timer = osTimer() + self.play_imei_timer = osTimer() + self.play_imei_timer_count=0 + self.KeySOS_timer_count = 0 + self.KeySOS_timer_flag = 0 + self._single_power_ai_merged = False + self._pwk_shutdown_ticks = 30 # 100ms 节拍,默认 3s + try: + ai_gpio = "GPIO22" + pwk_gpio = "GPIO26" + bt_wakeup_en = False + wakeup_cta1_gpio = None + wakeup_bt_gpio = None + if "vc_config" in globals() and vc_config: + raw_ai = vc_config.get_attr("EXTINT_AI_GPIO", "GPIO22") + if raw_ai is None or raw_ai == "": + ai_gpio = None + else: + ai_gpio = raw_ai + pwk_gpio = vc_config.get_attr("PWK_KEY_GPIO", "GPIO26") or "GPIO26" + bt_wakeup_en = bool(vc_config.get_attr("BT_WAKEUP_4G_ENABLED", False)) + wakeup_cta1_gpio = vc_config.get_attr("EXTINT_WAKEUP_CTA1_GPIO", None) + wakeup_bt_gpio = vc_config.get_attr("PIN_WAKEUP_BT_GPIO", None) + try: + self._single_power_ai_merged = bool( + vc_config.get_bool("SINGLE_POWER_KEY_AI_MERGED", False) + ) or (ai_gpio is None) + except Exception: + self._single_power_ai_merged = ai_gpio is None + try: + _pwk_ms = int(vc_config.get_int("PWK_LONG_PRESS_SHUTDOWN_MS", 3000)) + except Exception: + _pwk_ms = 3000 + self._pwk_shutdown_ticks = max(1, (_pwk_ms + 99) // 100) + except Exception: + ai_gpio = "GPIO22" + pwk_gpio = "GPIO26" + bt_wakeup_en = False + wakeup_cta1_gpio = None + wakeup_bt_gpio = None + + if ai_gpio: + self.extint_AI = ExtInt( + getattr(ExtInt, ai_gpio), + ExtInt.IRQ_RISING_FALLING, + ExtInt.PULL_PU, + self.keyAI_extint_cb, + ) + self.extint_AI.enable() + else: + self.extint_AI = None + + self.pwk_key = ExtInt(getattr(ExtInt, pwk_gpio), ExtInt.IRQ_RISING_FALLING, ExtInt.PULL_PU, self.pwk_extint_cb) + + if bt_wakeup_en: + try: + if wakeup_cta1_gpio: + self.extint_wakeup_cta1 = ExtInt(getattr(ExtInt, wakeup_cta1_gpio), ExtInt.IRQ_RISING, ExtInt.PULL_DISABLE, self.wakeup_extint_cb) + if wakeup_bt_gpio: + self.PIN_wakeup_bt = Pin(getattr(Pin, wakeup_bt_gpio), Pin.OUT, Pin.PULL_DISABLE, 0) + self.extint_wakeup_cta1_timer = osTimer() + if hasattr(self, "extint_wakeup_cta1"): + self.extint_wakeup_cta1.enable() + except Exception: + pass + + self.pwk_key_status = 0 + self.pwk_key_timer_count = 0 + self.pwk_key_long_press = 0 + self.pwk_key_press = 0 + self.powerKey_status = 0 + self.pwk_key_timer = osTimer() + self.pwk_is_double_timer = osTimer() + self.double_click_threshold = 1000#双击间隔时间 + self.pwk_press_count = 0#按下次数 + self.last_key_time = 0 # 上一次有效触发时间 + self.key_debounce_interval = 20 # 防抖窗口20ms,标准值 + #self.pwk_key = ExtInt(ExtInt.GPIO26, ExtInt.IRQ_RISING_FALLING, ExtInt.PULL_DISABLE, self.pwk_extint_cb) + + self.pwk_key.enable() + print("AI_dev:1") + self.btnInfo_queue = Queue(100) + self.btnInfo_queue_count = 0 + _thread.stack_size(16*1024) + self.btnInfo_queue_thread_id = _thread.start_new_thread(self.btnInfo_queue_thread, ()) + _thread.stack_size(8*1024) + EventMesh.subscribe("publish_AI_btnInfo",self.publish_AI_btnInfo) + EventMesh.subscribe("get_battery",self.get_battery) + EventMesh.subscribe("audio_montage_play",self.audio_montage_play) + # EventMesh.subscribe("paly_iccid_imei",self.paly_iccid_imei) + EventMesh.subscribe("paly_device_res_error",self.paly_device_res_error) + EventMesh.subscribe("Power_off",self.pwk_key_Long_Press) + EventMesh.subscribe("wakeup_BT",self.wakeup_BT) + EventMesh.subscribe("sleep_BT",self.sleep_BT) + EventMesh.subscribe("sys_sleep_cb",self.sys_sleep_cb)#系统休眠前回调 + EventMesh.subscribe("sys_up_cb",self.sys_up_cb)#系统唤醒后回调 + + EventMesh.subscribe("GET_USB_STATE",self.get_usb_state) + EventMesh.subscribe("SET_SPEECH_RATE",self.set_speech_rate) + EventMesh.subscribe("rest_TB",self.rest_TB) + + print("AI_dev:2") + self.pk = PowerKey() + self.pk.powerKeyEventRegister(self.powerKey_callback) + self.usb = USB() + self.usb.setCallback(self.usb_event_cb) + self.usb_status = self.usb.getStatus() + self.usb_charge_flag = 0 + self._usb_evt_last_ms = 0 + self._usb_evt_last_state = -1 + self.Low_power_warning_timer = 0 + self.AI_dev_timer = osTimer() + self.AI_dev_timer.start(10000, 1, self.AI_dev_timer_cb) + print("AI_dev:AI_dev_timer") + self.aud = audio.Audio(0) + self.Low_power_warning_flag = 0 + self._low_warn_last_ms = 0 + self._empty_shutdown_done = 0 + self.VC_TTS_volume = EventMesh.publish("get_storage_config","VC_TTS_volume")#音量配置 + if self.VC_TTS_volume == None: + self.VC_TTS_volume = 100 + self.aud.setVolume(self.VC_TTS_volume //10) + EventMesh.publish("set_storage_config",["VC_TTS_volume",self.VC_TTS_volume]) + #self.aud.play(1,0,'U:/static/xianyikaiji.mp3')#开机音频 + #EventMesh.publish("audio_montage_play",["xianyikaiji.mp3"]) + #EventMesh.publish("publish_AI_btnInfo",{"func":self.ntptime_cb}) + self.check_rest_timer_count = 0 + try: + if "vc_config" in globals() and vc_config and vc_config.uart_supported(): + self.ai_uart = AI_UART() + except Exception: + pass + + self.Keycall1_press_count = 0 + self.ble_switch_flag_time = osTimer() + self.ble_switch_flag_time.start(20000, 0, self.ble_switch_flag_time_init) + # AI_hw_status 先于本模块初始化,开机已插 USB 时需补同步一次 + try: + if int(self.usb_status or 0) == 1: + EventMesh.publish("hw_usb_state", 1) + EventMesh.publish("publish_AI_btnInfo", {"func": self.usb_event, "data": 1}) + except Exception: + pass + def ble_switch_flag_time_init(self,*args): + EventMesh.publish("publish_AI_btnInfo",{"func":self.ble_switch_flag_time_cb}) + def ble_switch_flag_time_cb(self,*args): + bt_switch_flag = EventMesh.publish("get_storage_config","switch_bt_state")#bt开关 + if bt_switch_flag == None: + bt_switch_flag = "1" + EventMesh.publish("send_bt_switch_toble",str(bt_switch_flag)) + # EventMesh.publish("send_imei_to_ble") + ble_switch_flag = EventMesh.publish("get_storage_config","switch_ble_state")#ble开关 + if ble_switch_flag == None: + ble_switch_flag = "1" + EventMesh.publish("send_ble_switch_toble",str(ble_switch_flag)) + vc_tts_volume = EventMesh.publish("get_storage_config","VC_TTS_volume") + if vc_tts_volume == None: + vc_tts_volume=100 + # EventMesh.publish("adjust_volume_toBle",str(vc_tts_volume)) + + pass + print("ble_switch_flag_time_cb!!!!!!!") + def ntptime_cb(self): + #ntptime.settime(8) + print("timer:{}!!!!!!!!\r\n".format(utime.localtime())) + def usb_event_cb(self, state): + # USB 插拔抖动过滤(日志中 powerKey 与 usb_evt 0/1 连跳会导致误播 OUT/IN) + try: + now = utime.ticks_ms() + last_ms = getattr(self, "_usb_evt_last_ms", 0) + last_st = getattr(self, "_usb_evt_last_state", -1) + if last_ms and utime.ticks_diff(now, last_ms) < 800 and last_st != state: + return + self._usb_evt_last_ms = now + self._usb_evt_last_state = state + except Exception: + pass + # 转发给精简硬件监控(AI_hw_status),避免 USB callback 被覆盖导致“后面失效” + try: + EventMesh.publish("hw_usb_state", state) + except Exception: + pass + EventMesh.publish("publish_AI_btnInfo",{"func":self.usb_event,"data":state}) + + def usb_event(self, state): + status = state + self.usb_charge_flag = 0 + if status == 1:#插入USB + print("charge R_CHARGE_in") + self.usb_status = 1 + VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag")#获取vc是否在运行 + if VC_satrt_flag == None: + VC_satrt_flag = 0 + + voice_record_satrt_flag = EventMesh.publish("get_voice_record_flag")#获取静默或者语音留言是否在运行 + if voice_record_satrt_flag == None: + voice_record_satrt_flag = 0 + if VC_satrt_flag == 0 and voice_record_satrt_flag == 0: + self.aud.stop() + #EventMesh.publish("flicker_led_red")#充电闪灯 + #self.aud.play(1,1,'U:/static/R_CHARGE_IN.mp3') + try: + if not ("vc_config" in globals() and vc_config and vc_config.get_bool("USE_MIN_HW_STATUS", True)): + EventMesh.publish("set_led_state", {"charging": "charging"}) + except Exception: + pass + EventMesh.publish("audio_montage_play",["R_CHARGE_IN.mp3"]) + self.Low_power_warning_flag = 0 + try: + self._empty_shutdown_done = 0 + except Exception: + pass + pass + elif status == 0:#拨出usbs + self.usb_status = 0 + VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag")#获取vc是否在运行 + if VC_satrt_flag == None: + VC_satrt_flag = 0 + voice_record_satrt_flag = EventMesh.publish("get_voice_record_flag")#获取静默或者语音留言是否在运行 + if voice_record_satrt_flag == None: + voice_record_satrt_flag = 0 + if VC_satrt_flag == 0 and voice_record_satrt_flag == 0: + self.aud.stop() + try: + if not ("vc_config" in globals() and vc_config and vc_config.get_bool("USE_MIN_HW_STATUS", True)): + EventMesh.publish("set_led_state", {"charging": "none"}) + except Exception: + pass + EventMesh.publish("audio_montage_play",["R_CHARGE_OUT.mp3"]) + #self.aud.play(1,1,'U:/static/R_CHARGE_OUT.mp3') + def get_usb_state(self,event,msg): + return self.usb_status + + def set_speech_rate(self,event,msg): + #msg的取值是0.5~2.0 + #而语速初始值是0,范围是-50~50 + rate = 0 + if msg == 0.5: + rate = -50 + elif msg < 1: + rate = (msg - 1)* 100 + elif msg >= 1: + rate = (msg - 1)* 50 + rate = round(rate) + print("速率已经设置为:{}".format(rate)) + EventMesh.publish("rate_change",rate) + return rate + def btnInfo_queue_thread(self): + while True: + data = self.btnInfo_queue.get() + self.btnInfo_queue_count-=1 + #EventMesh.debug_log("Info_queue:data = {}".format(data)) + if data != None: + try: + if "data" in data : + data["func"](data["data"]) + else : + data["func"]() + except Exception as e: + #EventMesh.debug_log("btnInfo_queue:ERROR:{}".format(e)) + if "data" in data : + print(data["data"]) + usys.print_exception(e) + def publish_AI_btnInfo(self,event,msg): + #print("publish_AI_btnInfo 1") + self.btnInfo_queue.put(msg) + self.btnInfo_queue_count+=1 + if self.btnInfo_queue_count > 80: + self.btnInfo_queue_count =0 + print("事件处理线程队列出问题,重启!!!!!!!!!!!!!!") + print("线程:{} 队列:{}".format(_thread.threadIsRunning(self.btnInfo_queue_thread_id),self.btnInfo_queue.size())) + # if _thread.threadIsRunning(self.btnInfo_queue_thread_id): + # _thread.stop_thread(self.btnInfo_queue_thread_id) + # _thread.stack_size(10*1024) + # self.btnInfo_queue_thread_id = _thread.start_new_thread(self.btnInfo_queue_thread, ()) + # _thread.stack_size(8*1024) + # self.btnInfo_queue.clear() + # self.btnInfo_queue = Queue(100) + EventMesh.publish("VC_error") #播放报错 + utime.sleep_ms(1000) + self.aud.stopPlayStream() + self.aud.stopAll() + Power.powerRestart() + EventMesh.publish("set_storage_config",["powerRestart",1]) + #print("publish_AI_btnInfo 2") + + def sys_sleep_cb(self,event=None,msg=None):#休眠前回调 + try: + if not ("vc_config" in globals() and vc_config and vc_config.get_bool("BT_WAKEUP_4G_ENABLED", False)): + return + except Exception: + return + print("sys_sleep_cb!!!!!!!!!!!!!!!") + EventMesh.publish("sleep_BT")#休眠蓝牙 + self.PIN_wakeup_bt.write(0) + def sys_up_cb(self,event=None,msg=None):#唤醒前回调 + try: + if not ("vc_config" in globals() and vc_config and vc_config.get_bool("BT_WAKEUP_4G_ENABLED", False)): + return + except Exception: + return + print("sys_up_cb!!!!!!!!!!!!!!!") + self.wakeup_BT() + self.PIN_wakeup_bt.write(1) + def rest_TB(self,event=None,msg=None): + print("rest_TB!!!!!!!!!!!!!!!") + self.PIN_BT_ON.write(0) + utime.sleep_ms(100) + self.PIN_BT_ON.write(1) + def wakeup_BT(self,event=None,msg=None):#4G唤醒蓝牙 + try: + if not ("vc_config" in globals() and vc_config and vc_config.get_bool("BT_WAKEUP_4G_ENABLED", False)): + return + except Exception: + return + print("wakeup_BT!!!!!!!!!!!!!!!") + wakeup_cta1_io = self.extint_wakeup_cta1.read_level() + if wakeup_cta1_io == 1: + return 1 + self.PIN_wakeup_bt.write(0) + count = 5 + while(True): + count-=1 + if count<=0: + return -1 + wakeup_cta1_io = self.extint_wakeup_cta1.read_level() + print("bt io状态:{}".format(wakeup_cta1_io)) + if wakeup_cta1_io == 1: + self.PIN_wakeup_bt.write(1) + return 1 + utime.sleep_ms(100) + if count%2 == 0: + self.PIN_wakeup_bt.write(1) + else: + self.PIN_wakeup_bt.write(0) + + + def sleep_BT(self,event=None,msg=None):#使蓝牙休眠 + try: + if not ("vc_config" in globals() and vc_config and vc_config.get_bool("BT_WAKEUP_4G_ENABLED", False)): + return + except Exception: + return + print("sleep_BT!!!!!") + print("sleep_BT!!!!!") + print("sleep_BT!!!!!") + print("sleep_BT!!!!!") + self.PIN_wakeup_bt.write(0) + count = 300 + while(True): + count-=1 + if count<=0: + return -1 + if self.extint_wakeup_cta1.read_level() == 0: + return 1 + utime.sleep_ms(1) + + + def wakeup_extint_cb(self,args):#蓝牙唤醒4g + try: + if not ("vc_config" in globals() and vc_config and vc_config.get_bool("BT_WAKEUP_4G_ENABLED", False)): + return + except Exception: + return + print("wakeup_extint_cb:!!!!!!!!!") + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + self.PIN_wakeup_bt.write(1) + EventMesh.publish("publish_AI_btnInfo",{"func":self.extint_wakeup_cta1_cb}) + def extint_wakeup_cta1_cb(self): + #self.extint_wakeup_cta1_timer.start(100, 1, self.extint_wakeup_cta1_timer_cb) + pass + def extint_wakeup_cta1_timer_cb(self, *args, **kwargs): + if self.extint_wakeup_cta1.read_level() == 0: + self.extint_wakeup_cta1_timer.stop() + #self.PIN_wakeup_bt.write(0) + + def powerKey_callback(self, status): + try: + EventMesh.debug_log("==pwr:", status) + except Exception: + print("powerKey_callback:{}".format(status)) + self.powerKey_status = status + if self.pwk_key_press == 1: + return + + def pwk_extint_cb(self,args):#pwk + current_time = 0 + interval = 0 + + try: + #按键电平判断 + press_level = 0 + current_level = self.pwk_key.read_level() + if current_level != press_level: + return # 非按下状态,直接返回 + + #消抖处理 + current_time = utime.ticks_ms() + if utime.ticks_diff(current_time, self.last_key_time) < self.key_debounce_interval: + return + self.last_key_time = current_time # 更新最后按键时间 + print("pwk 按键按下") + + #仅处理"按下瞬间"事件(避免重复触发) + if len(args) < 2: + print("args参数不足,跳过") + return + self.pwk_key_status = int(args[1]) + if self.pwk_key_status != 1: + return + + #双击计数逻辑 + self.pwk_press_count += 1 + current_time = utime.ticks_ms() + + #第一次按下(启动长按定时器+双击等待) + if self.pwk_press_count == 1: + self.pwk_first_press_time = current_time # 记录首次按下时间 + self.pwk_key_long_press = 0 # 重置长按标记 + self.pwk_key_timer_count = 0 # 重置定时器计数 + self.pwk_key_press = 1 # 标记按键按下 + + # 停止旧定时器,启动长按/单击判定定时器(100ms单次触发,循环靠回调自启) + self.pwk_key_timer.stop() + self.pwk_key_timer.start(100, 1, self.pwk_key_timer_cb) + print("第一次按下,等待第二次按下") + if getattr(self, "_single_power_ai_merged", False): + self._merged_pwk_ai_press_start() + + #第二次按下(双击判断) + elif self.pwk_press_count == 2: + # 计算两次按下间隔 + interval = utime.ticks_diff(current_time, self.pwk_first_press_time) + + #符合双击阈值:执行双击逻辑 + if interval <= self.double_click_threshold: + self.pwk_key_timer.stop() # 停止长按定时器(关键:避免触发长按) + self.pwk_key_double_press() # 执行双击业务 + print("双击生效") + # 重置所有状态 + self.pwk_press_count = 0 + self.pwk_key_press = 0 + self.pwk_key_timer_count = 0 + + #间隔过长:视为新的第一次按下 + else: + self.pwk_first_press_time = current_time # 更新首次按下时间 + self.pwk_press_count = 1 # 重置为第一次按下 + print("间隔过长,视为新的第一次按下") + + #按下次数超过2次(防计数溢出) + else: + self.pwk_press_count = 0 + self.pwk_key_timer.stop() + print("按键次数过多,重置计数") + + except Exception as e: + # 异常时重置所有状态,避免卡死 + self.pwk_press_count = 0 + self.pwk_key_press = 0 + self.pwk_key_timer_count = 0 + self.pwk_key_timer.stop() + usys.print_exception(e) + + + def pwk_is_double_judge(self, *args): + # if self.press_count == 1: # 仅一次按下,判定为单击 + + # 重置计数(无论何种情况,超时后清零) + self.pwk_press_count = 0 + + + def pwk_key_timer_cb(self, *args, **kwargs): + EventMesh.publish("set_wait_first_flag",0) + EventMesh.publish("set_history_board_flag",0) + EventMesh.publish("set_play_new_voice_state",0) + EventMesh.publish("set_history_voice_flag",0) + EventMesh.publish("set_wait_voice_flag",0) + EventMesh.publish("set_stop_play",0) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.pwk_key_timer_cb_}) + # def pwk_key_timer_cb_(self) : + try: + self.pwk_key_status = self.pwk_key.read_level() + self.pwk_key_timer_count +=1 + if self.pwk_key_timer_count >= self._pwk_shutdown_ticks: + #print("pwk_key_long_press {}".format(self.pwk_key_long_press)) + if self.pwk_key_long_press == 0: + self.pwk_key_long_press = 1 + #EventMesh.publish("AI_key_long_press") + # EventMesh.publish("audio_montage_play",["zhengzaiguanji.mp3"]) + self.aud.play(1,0,'U:/static/zhengzaiguanji.mp3')#关机音频 + EventMesh.publish("set_led_state", {"battery": "empty"}) + utime.sleep_ms(2000) + self.aud.stopPlayStream() + self.aud.stopAll() + Power.powerDown() + #print("AI_key_release") + #EventMesh.publish("pwk_key_long_press") + #EventMesh.publish("publish_AI_btnInfo",{"func":self.pwk_key_Long_Press}) + if self.pwk_key_status == 1:#松开 + #print("pwk_key_timer_cb:{}".format(self.pwk_key_timer_count)) + try: + key_count = vc_config.get_int("KEY_RELEASE_DEBOUNCE_COUNT", 5) + except Exception: + key_count = 5 + if self.pwk_key_timer_count >= key_count: #防抖200ms + #EventMesh.publish("AI_key_release") + EventMesh.publish("publish_AI_btnInfo",{"func":self.pwk_key_release}) + self.pwk_key.enable() + self.pwk_key_timer_count = 0 + self.pwk_key_press = 0 + self.pwk_press_count = 0#按下次数 + self.pwk_key_timer.stop() + + except Exception as e: + usys.print_exception(e) + + def _ai_int_disable_for_hold(self): + if self.extint_AI is not None: + self.extint_AI.disable() + + def _ai_int_enable_after_hold(self): + if self.extint_AI is not None: + self.extint_AI.enable() + + def _key_ai_read_level(self): + if self.extint_AI is not None: + return self.extint_AI.read_level() + return self.pwk_key.read_level() + + def _merged_pwk_ai_press_start(self): + """单电源键:按下瞬间走原 AI 键按下链路(松手走 AI_key_release,不按电源短按 Close)。""" + try: + current_time = utime.ticks_ms() + if utime.ticks_diff(current_time, self.last_ai_key_time) < 1: + return + self.last_ai_key_time = current_time + if EventMesh.publish("get_wait_voice_flag") == 1: + print("播放未读") + EventMesh.publish("set_wait_voicerupt_flag", 1) + return + if EventMesh.publish("get_recv_voice") == 1: + print("收到留言") + EventMesh.publish("set_voicerupt_flag", 1) + return + if self.KeyAI_press == 0: + EventMesh.publish(constants.EVENT.SYSTEM_UP) + if EventMesh.publish("get_play_new_voice_state") == 1: + EventMesh.publish("set_play_new_voice_state", 0) + EventMesh.publish("set_VC_stop_play_audio_flag", 1) + self._ai_int_disable_for_hold() + self.KeyAI_press = 1 + self.KeyAI_long_press = 0 + self.KeyAI_timer.start(100, 1, self.KeyAI_timer_cb) + self.KeyAI_timer_count = 0 + EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_key_press}) + except Exception as e: + usys.print_exception(e) + + def keyAI_extint_cb(self,args):#call 2 + try: + if self.extint_AI is None: + return + #print("AI_btn:keyAI_extint_cb{}".format(args[1])) + #定义最短触发间隔 + MIN_TRIGGER_INTERVAL = 1 # 可根据需求调整,单位ms + #记录上次按键触发时间(初始化放类的__init__里:self.last_ai_key_time = 0) + current_time = utime.ticks_ms() # 获取当前毫秒时间 + #重复按下,直接返回 + if utime.ticks_diff(current_time, self.last_ai_key_time) < MIN_TRIGGER_INTERVAL: + print("AI按键触发过频,忽略本次操作") + return + #更新上次触发时间 + self.last_ai_key_time = current_time + + self.KeyAI_status = int(args[1]) + if self.KeyAI_status == 0: + self.KeyAI_status = 1 + else: + self.KeyAI_status = 0 + + if self.KeyAI_status == 0:#按下 + if EventMesh.publish("get_wait_voice_flag") == 1: + print("播放未读") + EventMesh.publish("set_wait_voicerupt_flag",1) + return + if EventMesh.publish("get_recv_voice") == 1:#当处于收到留言的时候 + print("收到留言") + EventMesh.publish("set_voicerupt_flag",1) + return + if self.KeyAI_press == 0 : + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + if EventMesh.publish("get_play_new_voice_state") == 1: + EventMesh.publish("set_play_new_voice_state",0) + + EventMesh.publish("set_VC_stop_play_audio_flag",1)#按键按下不允许播放之前下发的音频数据 + self._ai_int_disable_for_hold() + self.KeyAI_press = 1 + self.KeyAI_long_press = 0 + self.KeyAI_timer.start(100, 1, self.KeyAI_timer_cb) + self.KeyAI_timer_count = 0 + #EventMesh.publish("AI_key_press") + # if dev_type == "7PIN": + # self.aud.aud_tone_play(10,100)#播放按键音 + + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_key_press}) + except Exception as e: + usys.print_exception(e) + def KeyAI_timer_cb(self, *args, **kwargs): + # EventMesh.publish("publish_AI_btnInfo",{"func":self.KeyAI_timer_cb_}) + # def KeyAI_timer_cb_(self): + try: + self.KeyAI_status = self._key_ai_read_level() + self.KeyAI_timer_count +=1 + if self.KeyAI_timer_count >= 10*1 : #长按1s + + if self.KeyAI_long_press == 0: + self.KeyAI_long_press = 1 + #EventMesh.publish("AI_key_long_press") + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_key_long_press}) + try: + _sos_on = vc_config.get_bool("SOS_ENABLED", True) + except Exception: + _sos_on = True + if _sos_on and self.KeyAI_timer_count >= 10*3 : #长按3s 上一次是短按则拨打sos紧急联系人 + print("long sos phone:{}".format(self.KeySOS_timer_flag)) + + if self.KeySOS_timer_flag == 1: + self.KeySOS_timer_flag=0 + self.KeySos_long_press = 1 + self.KeySOS_timer.stop() + get_sos_flag = EventMesh.publish("get_sos_flag") + if get_sos_flag == 0: + EventMesh.publish("set_sos_info_call") + pass + + elif self.KeyAI_timer_count >= 10*8:#关机 + try: + allow_power_off = vc_config.get_bool("AI_LONG_PRESS_POWER_OFF_ENABLED", False) + except Exception: + allow_power_off = False + if allow_power_off: + EventMesh.publish("audio_montage_play",["zhengzaiguanji.mp3"]) + #self.aud.play(1,0,'U:/static/zhengzaiguanji.mp3')#关机音频 + EventMesh.publish("set_led_state", {"battery": "empty"}) + utime.sleep_ms(2000) + self.aud.stopPlayStream() + print("stopAll:8") + self.aud.stopAll() + Power.powerDown() + if self.KeyAI_status == 1:#松开 + #print("KeyAI_timer_cb:{}".format(self.KeyAI_timer_count)) + try: + key_count = vc_config.get_int("KEY_RELEASE_DEBOUNCE_COUNT", 5) + except Exception: + key_count = 5 + if self.KeyAI_timer_count >= key_count: #防抖200ms + if self.KeySOS_timer_flag == 1: + self.KeySOS_timer_flag = 0 + self.KeySos_long_press = 0 + self.KeySOS_timer.stop() + print("KeySOS_timer_flag!!!!!!!!!!!!!!!") #短按两次需要清除标识 + + if self.KeyAI_long_press == 0: + try: + _sos_on = vc_config.get_bool("SOS_ENABLED", True) + except Exception: + _sos_on = True + if _sos_on and self.KeySOS_timer_flag == 0: + self.KeySOS_timer_flag = 1 + self.KeySos_long_press = 0 + self.KeySOS_timer.start(5000, 0, self.KeySos_timer_cb) #短按开启5s定时器 5s之内没有长按3s则清除标识 长按3s拨打sos紧急联系人 + else: + self.KeySOS_timer_flag = 0 + self.KeySos_long_press = 0 + self.KeySOS_timer.stop() + + #EventMesh.publish("AI_key_release") + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_key_release}) + self._ai_int_enable_after_hold() + self.KeyAI_timer.stop() + self.KeyAI_timer_count = 0 + self.KeyAI_press = 0 + self.KeyAI_long_press=0 + + except Exception as e: + usys.print_exception(e) + + def AI_key_press(self): + # EventMesh.publish("wakeup_BT") + + # if dev_type == "7PIN" or dev_type == "7PIN_coze" : + # # self.aud.aud_tone_play(10,60)#播放按键音 + # EventMesh.publish("audio_montage_play",["chenggong.mp3"]) + self.check_rest_timer_count = 0 + print("AI_key_press") + EventMesh.publish("AI_key_press") + def KeySos_timer_cb(self,*argc): + print("KeySos_timer_cb!!!!!!!!!!{} {}".format(self.KeySos_long_press,self.KeySOS_timer_flag)) + + if self.KeySos_long_press == 1 and self.KeySOS_timer_flag == 1: #清空sos标识 + self.KeySos_long_press=0 + self.KeySOS_timer_flag=0 + + self.KeySos_long_press=0 + self.KeySOS_timer_flag = 0 + return + self.KeySOS_timer_flag = 0 + if self.KeySOS_timer_count >= 3: + self.KeySOS_timer_count = 0 + get_sos_flag = EventMesh.publish("get_sos_flag") + if get_sos_flag == 0: + EventMesh.publish("set_sos_info_call") + self.KeySOS_timer_count = 0 + pass + + def AI_key_long_press(self): + self.check_rest_timer_count = 0 + print("AI_key_long_press") + + EventMesh.publish("AI_key_long_press") + + def AI_key_release(self): + print("AI_key_release") + # self.KeySOS_timer_count +=1 + # print("self.KeySOS_timer_count:{} {}".format(self.KeySOS_timer_count,self.KeySOS_timer_flag)) + # if self.KeySOS_timer_flag == 0: + # self.KeySOS_timer_flag = 1 + # self.KeySOS_timer.start(5000, 0, self.KeySos_timer_cb) + EventMesh.publish("AI_key_release") + + def pwk_key_double_press(self): + self.check_rest_timer_count = 0 + try: + if getattr(self, "_single_power_ai_merged", False): + try: + self.KeyAI_timer.stop() + except Exception: + pass + self.KeyAI_timer_count = 0 + self.KeyAI_press = 0 + self.KeyAI_long_press = 0 + self._ai_int_enable_after_hold() + except Exception: + pass + print("pwk_key_double_press") + EventMesh.publish("pwk_key_double_press") + + def pwk_key_Long_Press(self,event=None,msg=None): + self.check_rest_timer_count = 0 + print("pwk_key_long_press!!!!!!!") + print("pwk_key_long_press!!!!!!!") + + # EventMesh.publish("paly_iccid_imei") + # return + EventMesh.publish("audio_montage_play",["zhengzaiguanji.mp3"]) + #self.aud.play(1,0,'U:/static/zhengzaiguanji.mp3')#关机音频 + EventMesh.publish("set_led_state", {"battery": "empty"}) + utime.sleep_ms(3000) + self.aud.stopPlayStream() + self.aud.stopAll() + Power.powerDown() + #print("AI_key_release") + #EventMesh.publish("pwk_key_long_press") + + def pwk_key_release(self): + self.check_rest_timer_count = 0 + #print("pwk_key_release") + EventMesh.publish("pwk_key_release") + + + def wdt_timer_cb(self, *args, **kwargs): + if self.is_wdt == 1: + self.wdt.feed() + + def AI_dev_timer_cb(self, *args, **kwargs): + #print("AI_dev_timer_cb") + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_dev_timer_event}) + def _battery_empty_shutdown_seq(self): + """REQ-BAT-03:红快闪 3 次 → 灭灯 → 关机提示音 → 关机(btn 线程执行)。""" + try: + EventMesh.publish("battery_priority_mute_vc") + except Exception: + pass + try: + n, ms = 3, 500 + if "vc_config" in globals() and vc_config: + n = int(vc_config.get_int("BATTERY_EMPTY_FLASH_COUNT", 3)) + ms = int(vc_config.get_int("BATTERY_EMPTY_FLASH_PERIOD_MS", 500)) + EventMesh.publish("set_led_state", {"alert": "red_flash_3"}) + utime.sleep_ms(n * ms * 2 + 200) + EventMesh.publish("set_led_state", {"ai_state": "idle", "battery": "normal", "charging": "none"}) + except Exception: + pass + try: + EventMesh.publish("audio_montage_play", ["didianguanji.mp3"]) + utime.sleep_ms(4500) + self.aud.stopPlayStream() + self.aud.stopAll() + except Exception: + pass + try: + Power.powerDown() + except Exception: + pass + + def AI_dev_timer_event(self): + #EventMesh.publish("audio_montage_play",["liuliangtishi.mp3"]) + EventMesh.publish("send_power_init_data") + if self.is_wdt == 1: + self.wdt.feed() + VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag")#获取vc是否在运行 + if VC_satrt_flag == None: + VC_satrt_flag = 0 + voice_record_satrt_flag = EventMesh.publish("get_voice_record_flag")#获取静默或者语音留言是否在运行 + if voice_record_satrt_flag == None: + voice_record_satrt_flag = 0 + Vbatt = Power.getVbatt() + if self.usb_status == 1 and VC_satrt_flag == 0 and voice_record_satrt_flag == 0: + try: + if not ("vc_config" in globals() and vc_config and vc_config.get_bool("USE_MIN_HW_STATUS", True)): + EventMesh.publish("set_led_state", {"charging": "charging"})#充电红灯常亮 + except Exception: + pass + _bcfg = _BatteryCfg.instance() + if self.usb_status == 1 and Vbatt >= _bcfg.charge_full_mv and VC_satrt_flag == 0 and voice_record_satrt_flag == 0: + try: + if not ("vc_config" in globals() and vc_config and vc_config.get_bool("USE_MIN_HW_STATUS", True)): + EventMesh.publish("set_led_state", {"charging": "full"}) + except Exception: + pass + if self.usb_charge_flag == 0 : + EventMesh.publish("audio_montage_play",["R_CHARGE_FULL.mp3"]) + #self.aud.play(1,0,'U:/static/R_CHARGE_FULL.mp3')#充电已满 + self.usb_charge_flag = 1 + try: + low_soc = int(_bcfg.low_soc) + empty_soc = int(_bcfg.empty_soc) + except Exception: + low_soc, empty_soc = 30, 10 + warn_interval_ms = 300000 + try: + if "vc_config" in globals() and vc_config: + warn_interval_ms = int(vc_config.get_int("BATTERY_LOW_WARN_INTERVAL_MS", 300000)) + except Exception: + pass + soc = None + try: + soc = self.get_battery() + except Exception: + soc = None + if self.usb_status == 0 and soc is not None: + empty_by_soc = soc < empty_soc + empty_by_mv = Vbatt is not None and Vbatt < int(_bcfg.empty_mv) + if empty_by_soc or empty_by_mv: + if getattr(self, "_empty_shutdown_done", 0) == 0: + self._empty_shutdown_done = 1 + try: + print("[BAT] empty shutdown soc={} vbatt={}".format(soc, Vbatt)) + except Exception: + pass + EventMesh.publish("publish_AI_btnInfo", {"func": self._battery_empty_shutdown_seq}) + elif soc < low_soc: + if voice_record_satrt_flag == 0: + try: + now = utime.ticks_ms() + last = getattr(self, "_low_warn_last_ms", 0) + if (not last) or utime.ticks_diff(now, last) >= warn_interval_ms: + self._low_warn_last_ms = now + try: + print("[BAT] low warn soc={} interval_ms={}".format(soc, warn_interval_ms)) + except Exception: + pass + EventMesh.publish("audio_montage_play", ["Low_power_warning.mp3"]) + except Exception: + pass + else: + try: + self._low_warn_last_ms = 0 + self._empty_shutdown_done = 0 + except Exception: + pass + if VC_satrt_flag == 0 and Vbatt is not None and Vbatt >= int(_bcfg.low_mv): + try: + if not ("vc_config" in globals() and vc_config and vc_config.get_bool("USE_MIN_HW_STATUS", True)): + EventMesh.publish("set_led_state", {"charging": "none", "battery": "normal"}) + except Exception: + pass + + self.check_rest_timer_count += 1 + if self.check_rest_timer_count >= 8640:#24小时没按键,则关机 + self.pwk_key_Long_Press() + + def get_battery(self, event=None, msg=None): + # 返回 int 型 SoC(0~100):便于 TTS/上报/比较,且 OCV 插值本身比裸电压稳;不做小数避免频繁跳变 + try: + Vbatt = Power.getVbatt() + except Exception: + return 0 + soc = _BatteryCfg.instance().soc_from_vbatt(Vbatt) + if soc is None: + return 0 + if soc < 0: + return 0 + if soc > 100: + return 100 + return int(soc) + def _audio_is_battery_priority(self, msg): + try: + for i in (msg or []): + n = str(i) + if ("Low_power" in n) or ("didianguanji" in n): + return True + except Exception: + pass + return False + + def audio_montage_play(self,event,msg): + self.wakeup_BT() + bat_pri = self._audio_is_battery_priority(msg) + if bat_pri: + try: + EventMesh.publish("battery_priority_mute_vc") + except Exception: + pass + else: + # 仅服务端 TTS PCM 在播时跳过;低电/关机提示优先级更高(见上) + try: + if EventMesh.publish("get_vc_tts_playback_active") == 1: + print("[audio-arb] skip local montage_play during tts_pcm") + return + except Exception: + pass + self.aud.stopPlayStream() + self.aud.stopAll() + str_data='mp3files=' + for i in msg: + try: + str_tmp = i.replace(".mp3","") + str_data+="{}.mp3+".format(audio_buf.index(str_tmp)) + except Exception as e: + usys.print_exception(e) + str_data = str_data[:-1] + print("audio_montage_play:{}".format(str_data)) + self.aud.montage_play('U:/static/audio.bin',1,1,str_data) + self.play_imei_timer_count=1 + self.play_imei_timer.stop() + self.play_imei_timer.start(1000,1,self.play_imei_timer_cb) + + def paly_iccid_imei(self,event,msg): + + self.wakeup_BT() + self.aud.stopPlayStream() + self.aud.stopAll() + str_data = 'mp3files={}.mp3+'.format(audio_buf.index("liuliangtishi"))#流量提示 + imei = modem.getDevImei() + iccid = sim.getIccid() + print("paly_iccid_imei:ICCID:{}".format(iccid)) + if iccid != -1 and len(iccid) > 5: + iccid = iccid.upper() + if len(iccid) <20: + for i in range(20-len(iccid)): + iccid = "0"+iccid + str_data += '{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+'.format(audio_buf.index("R_READ_TIME_{}".format(iccid[0]))\ + ,audio_buf.index("R_READ_TIME_{}".format(iccid[1])),audio_buf.index("R_READ_TIME_{}".format(iccid[2])),audio_buf.index("R_READ_TIME_{}".format(iccid[3])),\ + audio_buf.index("R_READ_TIME_{}".format(iccid[4])),audio_buf.index("R_READ_TIME_{}".format(iccid[5])),audio_buf.index("R_READ_TIME_{}".format(iccid[6])),audio_buf.index("R_READ_TIME_{}".format(iccid[7])),\ + audio_buf.index("R_READ_TIME_{}".format(iccid[8])),audio_buf.index("R_READ_TIME_{}".format(iccid[9])),audio_buf.index("R_READ_TIME_{}".format(iccid[10])),audio_buf.index("R_READ_TIME_{}".format(iccid[11])),\ + audio_buf.index("R_READ_TIME_{}".format(iccid[12])),audio_buf.index("R_READ_TIME_{}".format(iccid[13])),audio_buf.index("R_READ_TIME_{}".format(iccid[14])),audio_buf.index("R_READ_TIME_{}".format(iccid[15])),\ + audio_buf.index("R_READ_TIME_{}".format(iccid[16])),audio_buf.index("R_READ_TIME_{}".format(iccid[17])),audio_buf.index("R_READ_TIME_{}".format(iccid[18])),audio_buf.index("R_READ_TIME_{}".format(iccid[19]))) + str_data += '{}.mp3+'.format(audio_buf.index("shouhou"))#可联系售后解决,联系方式见售后客服电话是400-886-8777或到微信公众号柒牌SEVENBRAND自行充值: + self.aud.montage_play('U:/static/audio.bin',1,1,str_data) + #开定时器不让系统休眠 + self.play_imei_timer_count=1 + self.play_imei_timer.stop() + self.play_imei_timer.start(1000,1,self.play_imei_timer_cb) + + def play_imei_timer_cb(self,event=None,msg=None): + if self.play_imei_timer_count == 1: + if EventMesh.publish("get_audio_play_state") == 0: + print("音频文件播放完成") + self.play_imei_timer_count=0 + self.play_imei_timer.stop() + return + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + + #鉴权出错,播放imei号 + def paly_device_res_error(self,event,msg): + self.wakeup_BT() + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + str_data = 'mp3files={}.mp3+'.format(audio_buf.index("jianquancuowu"))#AI功能异常,请联系客户查询,设备号为: + imei = modem.getDevImei() + imei = imei.upper() + str_data += '{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3+{}.mp3'.format(audio_buf.index("R_READ_TIME_{}".format(imei[0]))\ + ,audio_buf.index("R_READ_TIME_{}".format(imei[1])),audio_buf.index("R_READ_TIME_{}".format(imei[2])),audio_buf.index("R_READ_TIME_{}".format(imei[3])),\ + audio_buf.index("R_READ_TIME_{}".format(imei[4])),audio_buf.index("R_READ_TIME_{}".format(imei[5])),audio_buf.index("R_READ_TIME_{}".format(imei[6])),audio_buf.index("R_READ_TIME_{}".format(imei[7])),\ + audio_buf.index("R_READ_TIME_{}".format(imei[8])),audio_buf.index("R_READ_TIME_{}".format(imei[9])),audio_buf.index("R_READ_TIME_{}".format(imei[10])),audio_buf.index("R_READ_TIME_{}".format(imei[11])),\ + audio_buf.index("R_READ_TIME_{}".format(imei[12])),audio_buf.index("R_READ_TIME_{}".format(imei[13])),audio_buf.index("R_READ_TIME_{}".format(imei[14]))) + #print("str_data:{}".format(str_data)) + self.aud.montage_play('U:/static/audio.bin',1,1,str_data) + self.play_imei_timer_count=1 + self.play_imei_timer.stop() + self.play_imei_timer.start(1000,1,self.play_imei_timer_cb) + diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_common.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_common.py new file mode 100644 index 00000000..af3d7c7b --- /dev/null +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_common.py @@ -0,0 +1,112 @@ +# -*- coding: utf-8 -*- +"""VC 公共配置与轻量工具(无业务类)。""" +from common import Abstract, LogAdapter +try: + from qp_log import qp_print as print +except Exception: + pass +import EventMesh +import uwebsocket +import usys +import gc +import urandom +import uhashlib +import ubinascii +import ujson +import osTimer +import _thread +from queue import Queue +import utime +import audio +import uos +from machine import Pin +from machine import ExtInt +from machine import WDT +import modem +import sim +import net +import base64 +import ucryptolib +import urandom as random +import request +import ql_fs +from misc import Power,USB,PowerKey +import ntptime +import machine +import dataCall +import constants +import versions +import version_util +from machine import UART +try: + import vc_config +except Exception as e: + usys.print_exception(e) +try: + import vc_text_intent +except Exception: + vc_text_intent = None +try: + import VAD +except Exception as e: + usys.print_exception(e) +try: + import usr.ai_alarm as _ai_alarm +except Exception as e: + usys.print_exception(e) + import ai_alarm as _ai_alarm +PRESS_AUDIO_MODE = 0 #是按键音 0 是使用音频文件 +# dev_type = "G10" +#dev_type = "7PIN_BK"#小瑞 +#dev_type = "7PIN_BK_coze"#k扣子小瑞 +#dev_type = "7PIN_BK_EU"#英文小瑞 +#dev_type = "7PIN_coze"#柒牌扣子 +dev_type = "7PIN"#柒牌(默认值,实际优先从 usr/default_config.py 覆盖) +# dev_type = "7PIN_1_5"#柒牌1.5 +#dev_type = 'momo'#食梦貘 +#dev_type = 'sjgq'#四季甘泉 +#dev_type = 'szbst'#深圳百事通 +#dev_type = 'jswm'#金三完美 +#dev_type = 'jswm_BOX'#金三完美柒牌盒子 +#dev_type = 'csh'#慈善会 + +# 统一从配置加载 DEV_TYPE(usr 覆盖优先) +try: + if "vc_config" in globals() and vc_config: + dev_type = vc_config.get_dev_type(dev_type) +except Exception: + pass + +# 1=打印 VC 周期性保活专用行「VC: ping ping」;默认 0(WS send 明细里已跳过 ping,无需再开此项) +VC_HEARTBEAT_LOG = 0 +# 1=打印音频帧级日志:Record_len / send_data len / 下发音频长度;默认 0(只看 uplink START/END) +# VC_AUDIO_FRAME_LOG = 0 +# 1=启用我们新增的协议层“严格门控/缓冲/兜底”行为;0=临时排除这些改动(用于定位协议影响) +# 受控项(与对端 V2.5+ 能力对齐;关=回退旧端行为): +# - 上行:收到 started 前缓冲/丢弃 PCM;PTT 模式下 audio.create 前禁发 PCM +# - 兜底:llm.completed 后若久无 tts.started,端侧可重启会话(不做 force_tts) +# 注意:audio.played 不受此开关控制——金三 V2.5 服务端依赖端侧播完后上报才能进入下一轮 ASR; +# 若误关会导致问候结束后首轮用户说话无 vad/asr。 +VC_PROTO_GUARDS = 0 +# 1=打印 VC WebSocket 每条收发的完整文本(流量大、易刷屏);0 仅一行 event 摘要(llm.delta 省略) +VC_WS_IO_LOG = 0 +# 长对话「无业务下行」idle:下列 JSON event/type 及裸文本 ping/pong 不重置 close_timerout。 +# 帧级 WebSocket PING/PONG 在 uwebsocket.recv() 内已处理,不会进入 AI_client_VC_recv。 +# 金三 V2.5 文档示例:WebSocket 文本帧 "ping" 保活;对端可能回 pong / heartbeat 类事件。 +_VC_KEEPALIVE_RX_EVENTS = frozenset(("pong", "ping", "heartbeat", "keepalive")) + + +def _vc_out_json_event_meta(s): + """解析 WS 出站 JSON:event 可能在 header 或顶层(Start/audio.create vs synthesis)。""" + try: + jd = ujson.loads(s) + h = jd.get("header") + if isinstance(h, dict): + ev = h.get("event") or "" + if ev: + return ev, h.get("trace_id") or "" + ev = jd.get("event") or "" + return ev, "" + except Exception: + return "", "" + diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_fsm.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_fsm.py new file mode 100644 index 00000000..80c62289 --- /dev/null +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_fsm.py @@ -0,0 +1,300 @@ +# -*- coding: utf-8 -*- +"""VC 会话状态机。""" +try: + from qp_log import qp_print as print +except Exception: + pass + +VC_record_TYPE_PCM = 0 +VC_record_TYPE_AMR = 1 +VC_play_TYPE_PCM = 0 +VC_play_TYPE_MP3 = 1 + +#websocket发送数据编号 +# AI_client_asr_num = 0 +# AI_client_chat_num = 1 +# AI_client_tts_num = 2 +AI_client_VC_num = 3 +AI_client_asr_num = 4 + + +class VCSessionState: + Disconnected = 0 + Connecting = 1 + Starting = 2 + Started = 3 + + +class VCTurnState: + Idle = 0 + Listening = 1 + SpeechStreaming = 2 + SpeechEnded = 3 + Thinking = 4 + TtsReceiving = 5 + Playing = 6 + AwaitPlayedAck = 7 + + +class VC_TtsRxPhase: + Idle = 0 + Started = 1 + ServerCompleted = 2 + + +class VCMode: + Realtime = 0 # VAD on(长对话,协议:无需 audio.create/completed) + PushToTalk = 1 # VAD off(单对话,协议:必须 audio.create/completed 包裹音频帧) + + +class VCStateMachine: + # 轻量状态机骨架(先落地骨架与动作接口,后续逐步把旧 if/flag 逻辑收敛进来)。 + # + # 约束来自《金三服务-语音对话V2.5》: + # - 收到 started 后才允许发音频帧(否则 session not initialized) + # - VAD 开启:不需要 audio.create/completed + # - VAD 关闭:必须 audio.create → frames → audio.completed + # - 播放结束 audio.played 是进入下一轮的门控信号之一(尤其关键词打断场景) + + def __init__(self, vc): + self.vc = vc + self.session = VCSessionState.Disconnected + self.turn = VCTurnState.Idle + self.last_llm_ms = 0 + self.last_tts_pending_ms = 0 + self.await_played = False + self.last_tts_completed_ms = 0 + self.tts_rx_phase = VC_TtsRxPhase.Idle + self.speech_uplink_open = 0 + + def reset_rx_phases(self): + self.tts_rx_phase = VC_TtsRxPhase.Idle + self.speech_uplink_open = 0 + + def mark_tts_rx_idle(self): + self.tts_rx_phase = VC_TtsRxPhase.Idle + + def on_server_asr_started(self): + if self.session == VCSessionState.Started: + self.speech_uplink_open = 1 + self.log("server.asr.started") + + def _turn_name(self, t): + try: + return { + VCTurnState.Idle: "Idle", + VCTurnState.Listening: "Listening", + VCTurnState.SpeechStreaming: "SpeechStreaming", + VCTurnState.SpeechEnded: "SpeechEnded", + VCTurnState.Thinking: "Thinking", + VCTurnState.TtsReceiving: "TtsReceiving", + VCTurnState.Playing: "Playing", + VCTurnState.AwaitPlayedAck: "AwaitPlayedAck", + }.get(t, str(t)) + except Exception: + return str(t) + + def _session_name(self, s): + try: + return { + VCSessionState.Disconnected: "Disconnected", + VCSessionState.Connecting: "Connecting", + VCSessionState.Starting: "Starting", + VCSessionState.Started: "Started", + }.get(s, str(s)) + except Exception: + return str(s) + + def log(self, reason=""): + # 只用于关键节点的单行状态日志,便于串口快速定位状态漂移 + try: + print("[FSM] {} session={} turn={} await_played={} tts_rx={} speech_up={}".format( + reason, + self._session_name(self.session), + self._turn_name(self.turn), + 1 if self.await_played else 0, + int(self.tts_rx_phase), + 1 if self.speech_uplink_open else 0, + )) + except Exception: + pass + + def mode(self): + try: + return VCMode.Realtime if self.vc.VC_mode == 0 else VCMode.PushToTalk + except Exception: + return VCMode.Realtime + + def on_session_start_request(self): + self.session = VCSessionState.Starting + self.turn = VCTurnState.Idle + # 新会话开始时必须清空上一轮“待播完”状态,否则会出现: + # - 切换模式/重连后 await_played 仍为 True → play_end 逻辑抢跑/不触发 → 无法自动开麦 + self.await_played = False + self.last_tts_completed_ms = 0 + self.last_llm_ms = 0 + self.reset_rx_phases() + + def on_server_started(self): + self.reset_rx_phases() + self.session = VCSessionState.Started + # 新会话 started:确保进入 Listening,且清理上轮播放门控 + self.await_played = False + self.last_tts_completed_ms = 0 + if self.turn in (VCTurnState.Idle, VCTurnState.AwaitPlayedAck): + self.turn = VCTurnState.Listening + self.log("server.started") + + def on_server_vad_silence(self): + if self.session == VCSessionState.Started: + self.turn = VCTurnState.SpeechEnded + self.log("server.vad.silence") + + def on_server_llm_created(self): + if self.session == VCSessionState.Started: + self.turn = VCTurnState.Thinking + try: + self.last_llm_ms = utime.ticks_ms() + except Exception: + self.last_llm_ms = 1 + self.log("server.llm.created") + + def on_server_tts_started(self): + if self.session == VCSessionState.Started: + self.turn = VCTurnState.TtsReceiving + self.tts_rx_phase = VC_TtsRxPhase.Started + self.log("server.tts.started") + + def on_server_tts_completed(self): + if self.session == VCSessionState.Started: + # tts.completed 只表示服务端音频下发结束,本地可能仍在播放;保持 Playing,由 await_played 门控播完上报 + self.await_played = True + self.tts_rx_phase = VC_TtsRxPhase.ServerCompleted + try: + self.last_tts_completed_ms = utime.ticks_ms() + except Exception: + self.last_tts_completed_ms = 1 + self.log("server.tts.completed") + + def on_server_canceled(self): + if self.session == VCSessionState.Started: + self.turn = VCTurnState.Listening + self.reset_rx_phases() + self.log("server.canceled") + + def on_server_completed(self): + if self.session == VCSessionState.Started: + # 本轮结束:默认回到 Listening;是否自动开麦由 action 门控决定 + # 若仍在等待本地播完(await_played),completed 不应把 turn 拉回 Listening, + # 否则会触发 completed→uplink_start 抢跑,打断播放链路。 + if not self.await_played and self.turn != VCTurnState.AwaitPlayedAck: + self.turn = VCTurnState.Listening + self.log("server.completed") + + def on_server_event(self, event, jsload_data=None): + # 事件→动作(action list)适配层。 + # 现阶段只产出“可幂等的控制动作”(uplink start/stop),避免和旧逻辑重复触发播放等副作用。 + # 后续会逐步把更多行为(play_start/audio.played/ptt create/completed)迁移到这里。 + actions = [] + try: + # LLM 阶段必须驱动 FSM.turn,否则 turn 会停在 SpeechEnded,导致: + # - _fsm_timeout_table_tick 里「Thinking 超时 / synthesis 超时」永不命中 + # - 与注释和 force_tts 前置条件不一致 + if event == "llm.created": + self.on_server_llm_created() + return actions + if event == "llm.completed": + # 若对端偶发跳过 llm.created,用 completed 把 turn 补到 Thinking,避免兜底全失效 + try: + if self.session == VCSessionState.Started: + if self.turn in (VCTurnState.SpeechEnded, VCTurnState.Listening): + self.turn = VCTurnState.Thinking + except Exception: + pass + return actions + if event == "started": + self.on_server_started() + return actions + if event == "vad.silence": + self.on_server_vad_silence() + actions.append(("uplink_stop", "vad.silence")) + return actions + if event == "tts.started": + self.on_server_tts_started() + actions.append(("uplink_stop", "tts.started")) + actions.append(("play_start", "tts.started")) + return actions + if event == "tts.completed": + self.on_server_tts_completed() + # 播放可能已在 started 时开启,这里不再强制 play_start,避免反复 open PCM + return actions + if event == "canceled": + self.on_server_canceled() + # canceled:仅长对话允许“立刻回到聆听并尝试开麦”(由 _uplink_request_start 门控保证安全幂等) + try: + if self.mode() == VCMode.Realtime and getattr(self.vc, "Close_flag", 0) == 0: + actions.append(("uplink_start", "canceled")) + except Exception: + pass + return actions + if event == "completed": + self.on_server_completed() + # completed:若不在等待 audio.played(说明本轮无 TTS),仅长对话可直接进入下一轮聆听 + try: + if self.mode() == VCMode.Realtime and getattr(self.vc, "Close_flag", 0) == 0: + if (not getattr(self, "await_played", False)) and self.turn != VCTurnState.AwaitPlayedAck: + actions.append(("uplink_start", "completed")) + except Exception: + pass + return actions + except Exception: + pass + return actions + + def on_local_audio_played(self): + # 本地播报完成后:长对话回到 Listening,等待下一轮输入 + if self.session == VCSessionState.Started: + self.turn = VCTurnState.Listening + self.await_played = False + self.last_tts_completed_ms = 0 + self.mark_tts_rx_idle() + self.log("local.audio_played") + + def on_local_play_start(self): + if self.session == VCSessionState.Started: + self.turn = VCTurnState.Playing + self.log("local.play_start") + + def on_local_synthesis_sent(self): + # synthesis 发出后到 tts.started 之间:视为 TTS 接收阶段,禁止开麦 + if self.session == VCSessionState.Started: + self.turn = VCTurnState.TtsReceiving + self.log("local.synthesis_sent") + + def on_local_play_end(self): + actions = [] + try: + if self.session != VCSessionState.Started: + return actions + if self.await_played: + actions.append(("send_audio_played", "local.play_end")) + # 长对话模式:对话过程中的 TTS 播完后,自动开麦继续当前对话。 + # 例外: + # - 刚切到长对话模式的提示音(mode_switch_long)不应自动开麦 + # - 其它提示/提醒(prompt)不应自动开麦 + # 实际开麦是否允许由 _uplink_request_start 的门控(FSM.turn / record_state 等)保证安全幂等。 + try: + if self.mode() == VCMode.Realtime and getattr(self.vc, "Close_flag", 0) == 0: + k = getattr(self.vc, "_last_tts_kind", "") or "" + if k == "dialog": + # 用户短按打断(RequestToSpeak):等 canceled 再开麦,勿 play_end 抢跑 + if getattr(self.vc, "_vc_pending_interrupt_uplink", 0) != 1: + actions.append(("uplink_start", "local.play_end")) + except Exception: + pass + self.on_local_audio_played() + except Exception: + pass + self.log("local.play_end") + return actions + diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_hw.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_hw.py new file mode 100644 index 00000000..0c48038c --- /dev/null +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_hw.py @@ -0,0 +1,344 @@ +# -*- coding: utf-8 -*- +"""充电/电量/信号采集。""" +from common import Abstract +try: + from qp_log import qp_print as print +except Exception: + pass +import EventMesh +import utime +import osTimer +import net +import dataCall +from misc import USB, Power +try: + import vc_config +except Exception: + vc_config = None +from vc_common import dev_type + +def _default_battery_ocv_map(): + # 与 mgr.py BATTERY_OCV_TABLE nix_coy_mnzo2 默认一致;可被 default_config.BATTERY_OCV_MAP 覆盖 + return { + 4290: 100, 4237: 95, 4179: 90, 4123: 85, 4070: 80, 4020: 75, 3963: 70, 3919: 65, 3882: 60, 3847: 55, + 3818: 50, 3794: 45, 3772: 40, 3752: 35, 3733: 30, 3716: 25, 3699: 20, 3677: 15, 3652: 10, 3629: 5, 3400: 0, + } + + +def _parse_battery_ocv_map(raw): + """解析 default_config.BATTERY_OCV_MAP:dict 或 [(mv, soc), ...]。""" + if raw is None: + return None + out = {} + try: + if isinstance(raw, dict): + pairs = raw.items() + elif isinstance(raw, (tuple, list)): + pairs = ((p[0], p[1]) for p in raw if isinstance(p, (tuple, list)) and len(p) >= 2) + else: + return None + for k, v in pairs: + out[int(k)] = int(v) + return out if out else None + except Exception: + return None + + +def _soc_from_vbatt_mv(volt_mv, ocv_map, ocv_volts, vbatt_simple=None): + """由电压(mV) 算 SoC(0~100);配置来自 default_config / _BatteryCfg。""" + try: + v = int(volt_mv) + except Exception: + return None + try: + if vbatt_simple: + _full, _v20, _v5 = vbatt_simple + if v >= int(_full): + return 100 + if v <= int(_v5): + return 0 + if v <= int(_v20): + return 10 + return 60 + except Exception: + pass + try: + volts = ocv_volts + if not volts: + return None + pre_volt = 0 + volt_not_under = 0 + soc1 = 0 + soc2 = 0 + for vv in volts: + if v > vv: + volt_not_under = 1 + soc1 = ocv_map.get(vv, 0) + soc2 = ocv_map.get(pre_volt, 0) + break + else: + pre_volt = vv + if pre_volt == 0: + return soc1 + if volt_not_under == 0: + return 0 + return int(soc2 - (soc2 - soc1) * (pre_volt - v) // (pre_volt - vv)) + except Exception: + return None + + +class _BatteryCfg(object): + """从 vc_config(default_config) 加载电池参数,供 AI_hw_status / get_battery / AI_dev 共用。""" + _inst = None + + def __init__(self): + self.ocv_map = _default_battery_ocv_map() + self.ocv_volts = sorted(self.ocv_map.keys(), reverse=True) + self.charge_full_mv = 4290 + self.low_mv = 3699 + self.empty_mv = 3629 + self.low_soc = 20 + self.empty_soc = 5 + self.vbatt_simple = None + try: + if "vc_config" in globals() and vc_config: + self.charge_full_mv = int(vc_config.get_int("VBATT_CHARGE_FULL_MV", 4290)) + self.low_mv = int(vc_config.get_int("VBATT_LOW_MV", 3699)) + self.empty_mv = int(vc_config.get_int("VBATT_EMPTY_MV", 3629)) + self.low_soc = int(vc_config.get_int("BATTERY_LOW_SOC_PERCENT", 30)) + self.empty_soc = int(vc_config.get_int("BATTERY_EMPTY_SOC_PERCENT", 10)) + _ocv_cfg = _parse_battery_ocv_map(vc_config.get_attr("BATTERY_OCV_MAP", None)) + if _ocv_cfg: + self.ocv_map = _ocv_cfg + self.ocv_volts = sorted(self.ocv_map.keys(), reverse=True) + if vc_config.get_bool("USE_SIMPLE_VBATT_THRESH", False): + self.vbatt_simple = (self.charge_full_mv, self.low_mv, self.empty_mv) + except Exception: + pass + + @classmethod + def instance(cls): + if cls._inst is None: + cls._inst = cls() + return cls._inst + + def soc_from_vbatt(self, volt_mv): + return _soc_from_vbatt_mv(volt_mv, self.ocv_map, self.ocv_volts, self.vbatt_simple) + + +class AI_hw_status(Abstract): + # 精简硬件状态采集:仅维护 LED 状态机所需的 charging/battery/signal + def __init__(self): + # 无条件首行:确认是否进入初始化(避免被异常/注册顺序误判) + try: + print("[HW] __init__ enter") + except Exception: + pass + _bcfg = _BatteryCfg.instance() + self._bcfg = _bcfg + self._ocv_map = _bcfg.ocv_map + self._ocv_volts = _bcfg.ocv_volts + self._charge_full_mv = _bcfg.charge_full_mv + self._low_soc = _bcfg.low_soc + self._empty_soc = _bcfg.empty_soc + self._vbatt_simple = _bcfg.vbatt_simple + # cached state (avoid repeat publish) + self._chg = None + self._bat = None + self._sig = None + self._usb = None + self._charge_full_latched = 0 + self._charge_full_streak = 0 + self._charge_full_streak_need = 2 + self._timer = osTimer() + self._period_ms = 10000 + self._weak_csq = 10 + self._dbg = 0 + try: + if "vc_config" in globals() and vc_config: + self._period_ms = vc_config.get_int("HW_STATUS_PERIOD_MS", 10000) or 10000 + self._weak_csq = vc_config.get_int("SIGNAL_WEAK_CSQ", 10) or 10 + self._dbg = 1 if vc_config.get_bool("HW_STATUS_DEBUG", False) else 0 + except Exception: + pass + # 若开启了 LED/按键调试,也一并打开硬件监控打印,便于关联分析 + try: + if "vc_config" in globals() and vc_config: + if vc_config.get_bool("LED_DEBUG", False) or vc_config.get_bool("KEY_LED_DEBUG", False): + self._dbg = 1 + except Exception: + pass + + # 注意:AI_dev 也会注册 USB callback;USB 模块通常只支持一个 callback,后注册会覆盖先注册。 + # 为避免“刚开始有效,后面失效”,这里不直接 setCallback,改为订阅 AI_dev 转发的 USB 状态事件, + # 并在 10 秒轮询中再读一次 GET_USB_STATE 作兜底。 + try: + EventMesh.subscribe("hw_usb_state", self._on_usb_state) + except Exception: + pass + + # 初始化:先立即刷新一次,再开启周期轮询(避免 one-shot/覆盖差异导致只跑一次) + try: + self._update() + except Exception: + pass + try: + self._timer.start(self._period_ms, 1, self._tick_cb) + except Exception: + pass + # 初始化打印(一次) + try: + print("[HW] init ok period_ms={} weak_csq={} usb_init={} algo={}".format( + self._period_ms, + self._weak_csq, + self._usb, + "simple" if getattr(self, "_vbatt_simple", None) else "ocv")) + if self._dbg == 1: + print("[HW] init detail vbatt_simple={}".format(getattr(self, "_vbatt_simple", None))) + except Exception: + pass + + def _on_usb_state(self, event=None, msg=None): + try: + state = msg + prev = self._usb + self._usb = 1 if state == 1 else 0 + if self._usb == 1 and prev != 1: + self._charge_full_latched = 0 + self._charge_full_streak = 0 + if self._usb == 0: + self._charge_full_latched = 0 + self._charge_full_streak = 0 + except Exception: + pass + try: + if self._dbg == 1: + print("[HW] usb_evt state={} usb={}".format(state, self._usb)) + except Exception: + pass + try: + self._update() + except Exception: + pass + + def _tick_cb(self, *args, **kwargs): + # 定时器回调直接刷新(不依赖 AI_dev 的事件线程) + try: + self._tick() + except Exception: + pass + + def _tick(self, *args, **kwargs): + self._update() + + def _update(self): + # 每次刷新都“同步推送一次”当前硬件基础态到 LED(不依赖变化检测) + # 目的:保证 10 秒轮询和 USB callback 都能及时刷新充电/电量/信号的基础态显示 + # charging/battery + try: + v = Power.getVbatt() + except Exception: + v = None + soc = None + csq = None + rssi = None + usb_status = None + chg = "none" + bat = "normal" + sig = "normal" + try: + usb_status = self._usb + if usb_status is None: + try: + usb_status = EventMesh.publish("GET_USB_STATE") + if usb_status is None: + usb_status = 0 + except Exception: + usb_status = 0 + # charging:插入后先红灯;勿用瞬时 vbatt≥满电阈值直接判满(充电器接入电压即偏高) + if usb_status == 1: + if getattr(self, "_charge_full_latched", 0) == 1: + chg = "full" + else: + if v is not None and v >= int(self._charge_full_mv): + self._charge_full_streak = int(getattr(self, "_charge_full_streak", 0)) + 1 + need = int(getattr(self, "_charge_full_streak_need", 2) or 2) + if self._charge_full_streak >= need: + self._charge_full_latched = 1 + chg = "full" + else: + chg = "charging" + else: + self._charge_full_streak = 0 + chg = "charging" + # battery level for LED table + if usb_status != 1 and v is not None: + soc = self._bcfg.soc_from_vbatt(v) + if soc is not None: + if soc < int(self._empty_soc): + bat = "empty" + elif soc < int(self._low_soc): + bat = "low" + self._chg = chg + self._bat = bat + except Exception: + pass + + # signal + try: + csq = net.csqQueryPoll() + # csqQueryPoll 返回 (rssi, ber) 或 int,按保守取 rssi + try: + if isinstance(csq, (tuple, list)) and len(csq) > 0: + rssi = csq[0] + else: + rssi = csq + except Exception: + rssi = csq + sig = "weak" if (rssi is not None and int(rssi) >= 0 and int(rssi) < int(self._weak_csq)) else "normal" + self._sig = sig + except Exception: + pass + + # 统一推送一次当前基础态(一次调用带齐字段,减少来回切换) + try: + EventMesh.publish("set_led_state", {"charging": chg, "battery": bat, "signal": sig}) + except Exception: + pass + + # 轮询调试打印(每次 10s tick 一次):硬件采样一行 + LED 聚合状态一行内打完 + try: + if self._dbg == 1: + led = None + try: + led = EventMesh.publish("get_led_debug_state") + except Exception: + pass + print("[HW] usb={} vbatt_mv={} soc={} chg={} bat={} csq={} rssi={} sig={} period_ms={} led={}".format( + usb_status, v, soc, self._chg, self._bat, csq, rssi, self._sig, self._period_ms, led)) + except Exception: + pass + + def _soc_from_vbatt(self, volt_mv): + return self._bcfg.soc_from_vbatt(volt_mv) + + +def set_ipv4_datacall(): + # 获取第一路的APN,ipType 等信息 + pdpCtx = dataCall.getPDPContext(1) + if pdpCtx != -1: + if pdpCtx[0] != 0: + # 如果不是用户需要的ipv4,使用如下方式配置,使用如下方式配置 + ret = dataCall.setPDPContext(1, 0,pdpCtx[1],pdpCtx[2],pdpCtx[3],pdpCtx[4]) + if ret == 0: + print('配置成功。') + # cfun0/1后按照配置的信息进行拨号 + net.setModemFun(0) + net.setModemFun(1) + else: + print('配置失败。') + else: + print('已经配置为IPv4') + else: + print('获取PDP Context失败。') + diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_led.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_led.py new file mode 100644 index 00000000..bea629c3 --- /dev/null +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_led.py @@ -0,0 +1,446 @@ +# -*- coding: utf-8 -*- +"""三色 LED 状态机。""" +from common import Abstract +try: + from qp_log import qp_print as print +except Exception: + pass +import EventMesh +import utime +import osTimer +from machine import Pin +try: + import vc_config +except Exception: + vc_config = None +from vc_common import dev_type + +class AI_led(Abstract): + def __init__(self): + try: + if "vc_config" in globals() and vc_config: + r_gpio, g_gpio, b_gpio = vc_config.led_gpio_tuple() + else: + r_gpio, g_gpio, b_gpio = Pin.GPIO36, Pin.GPIO21, Pin.GPIO19 + except Exception: + r_gpio, g_gpio, b_gpio = Pin.GPIO36, Pin.GPIO21, Pin.GPIO19 + self._led_gpio_r = r_gpio + self._led_gpio_g = g_gpio + self._led_gpio_b = b_gpio + self.led_red = Pin(r_gpio, Pin.OUT, Pin.PULL_PD, 0) + self.led_green = Pin(g_gpio, Pin.OUT, Pin.PULL_PD, 1) + self.led_blue = Pin(b_gpio, Pin.OUT, Pin.PULL_PD, 1) + print("AI_led GPIO dev_type={} red={} green={} blue={}".format( + dev_type, r_gpio, g_gpio, b_gpio)) + + # 统一输入状态(充电/电量/信号/AI对话),按优先级表输出 + self._st_ai = "idle" # idle/listening/thinking/playing + self._st_charging = "none" # none/charging/full + self._st_battery = "normal" # normal/low/empty + self._st_signal = "normal" # normal/weak + self._st_alert = None # None / rb_flash_2(REQ-ERR-01/03) + + self._timer = osTimer() + self._mode = "off" # off/red_on/green_on/green_blink/blue_blink/red_blink/bg_alt/alert_rb + self._alert_steps_left = 0 + self._alert_rb_phase = 0 + self._alert_period_ms = 250 + self._blink_on = 0 + self._bg_phase = 0 + self._period_ms = 1000 + self._gb_on_ms = 1000 + self._gb_off_ms = 1000 + + EventMesh.subscribe("set_led_state", self.set_led_state) + EventMesh.subscribe("get_led_debug_state", self.get_led_debug_state) + # LED 电平极性:有些板子是低电平点亮(active-low),也可能仅某一路反相 + self._inv_r = 0 + self._inv_g = 0 + self._inv_b = 0 + try: + if "vc_config" in globals() and vc_config: + inv = vc_config.get_attr("LED_ACTIVE_LOW_RGB", None) + if isinstance(inv, (tuple, list)) and len(inv) == 3: + self._inv_r = 1 if inv[0] else 0 + self._inv_g = 1 if inv[1] else 0 + self._inv_b = 1 if inv[2] else 0 + else: + all_inv = 1 if vc_config.get_bool("LED_ACTIVE_LOW", False) else 0 + self._inv_r = all_inv + self._inv_g = all_inv + self._inv_b = all_inv + except Exception: + pass + try: + self._apply_by_table() + except Exception: + try: + self._set_off() + except Exception: + pass + + def _timer_stop(self): + try: + self._timer.stop() + except Exception: + pass + + def _set_rgb(self, r, g, b): + try: + rr = 1 if r else 0 + gg = 1 if g else 0 + bb = 1 if b else 0 + if self._inv_r: + rr = 0 if rr else 1 + if self._inv_g: + gg = 0 if gg else 1 + if self._inv_b: + bb = 0 if bb else 1 + self.led_red.write(rr) + self.led_green.write(gg) + self.led_blue.write(bb) + except Exception: + pass + + def _set_off(self): + self._mode = "off" + self._timer_stop() + self._set_rgb(0, 0, 0) + + def _set_red_on(self): + self._mode = "red_on" + self._timer_stop() + self._set_rgb(1, 0, 0) + + def _set_green_on(self): + self._mode = "green_on" + self._timer_stop() + self._set_rgb(0, 1, 0) + + def _set_green_blink(self, on_ms=1000, off_ms=None): + """待机/空闲:绿灯慢闪(替代呼吸灯,数字 GPIO 整颗亮灭)。off_ms 缺省则与 on_ms 对称。""" + on_ms = int(on_ms or 1000) + if off_ms is None: + off_ms = on_ms + else: + off_ms = int(off_ms or on_ms) + try: + if (self._mode == "green_blink" and int(self._gb_on_ms) == int(on_ms) + and int(self._gb_off_ms) == int(off_ms)): + return + except Exception: + pass + self._mode = "green_blink" + self._gb_on_ms = on_ms + self._gb_off_ms = off_ms + self._period_ms = on_ms + self._blink_on = 1 + self._timer_stop() + self._set_rgb(0, 1, 0) + self._timer.start(self._gb_on_ms, 0, self._timer_cb) + + def _set_blue_blink(self, period_ms=1000): + self._mode = "blue_blink" + self._period_ms = period_ms or 1000 + self._blink_on = 1 + self._timer_stop() + self._set_rgb(0, 0, 1) + self._timer.start(self._period_ms, 1, self._timer_cb) + + def _set_red_blink(self, period_ms=1000): + self._mode = "red_blink" + self._period_ms = period_ms or 1000 + self._blink_on = 1 + self._timer_stop() + self._set_rgb(1, 0, 0) + self._timer.start(self._period_ms, 1, self._timer_cb) + + def _set_bg_alt(self, period_ms=1000): + self._mode = "bg_alt" + self._period_ms = period_ms or 1000 + self._bg_phase = 0 + self._timer_stop() + self._set_rgb(0, 0, 1) + self._timer.start(self._period_ms, 1, self._timer_cb) + + def _set_alert_rb_flash(self, cycles=2, period_ms=250): + """REQ-ERR-01/03:红蓝快闪(约 2 次/秒),结束后回到 _apply_by_table。""" + try: + cycles = int(cycles or 2) + except Exception: + cycles = 2 + if cycles < 1: + cycles = 1 + try: + period_ms = int(period_ms or 250) + except Exception: + period_ms = 250 + if period_ms < 100: + period_ms = 100 + self._mode = "alert_rb" + self._st_alert = "rb_flash_2" + self._alert_period_ms = period_ms + self._alert_steps_left = cycles * 2 + self._alert_rb_phase = 0 + self._timer_stop() + self._set_rgb(1, 0, 0) + self._timer.start(self._alert_period_ms, 0, self._timer_cb) + + def _set_red_flash_limited(self, flashes=3, period_ms=500): + """REQ-BAT-03:红快闪若干次后熄灭(约 2 次/秒)。""" + try: + flashes = int(flashes or 3) + except Exception: + flashes = 3 + if flashes < 1: + flashes = 1 + try: + period_ms = int(period_ms or 500) + except Exception: + period_ms = 500 + if period_ms < 100: + period_ms = 100 + self._mode = "red_flash_n" + self._red_flash_steps_left = flashes * 2 + self._alert_period_ms = period_ms + self._blink_on = 1 + self._timer_stop() + self._set_rgb(1, 0, 0) + self._timer.start(self._alert_period_ms, 0, self._timer_cb) + + def _timer_cb(self, *args, **kwargs): + EventMesh.publish("publish_AI_btnInfo", {"func": self._timer_cb_}) + + def _timer_cb_(self): + if self._mode == "green_blink": + self._blink_on = 0 if self._blink_on else 1 + self._set_rgb(0, 1 if self._blink_on else 0, 0) + next_ms = self._gb_on_ms if self._blink_on else self._gb_off_ms + try: + self._timer.start(next_ms, 0, self._timer_cb) + except Exception: + pass + return + if self._mode == "blue_blink": + self._blink_on = 0 if self._blink_on else 1 + self._set_rgb(0, 0, 1 if self._blink_on else 0) + return + if self._mode == "red_blink": + self._blink_on = 0 if self._blink_on else 1 + self._set_rgb(1 if self._blink_on else 0, 0, 0) + return + if self._mode == "red_flash_n": + self._red_flash_steps_left -= 1 + if self._red_flash_steps_left <= 0: + self._set_off() + return + self._blink_on = 0 if self._blink_on else 1 + self._set_rgb(1 if self._blink_on else 0, 0, 0) + try: + self._timer.start(self._alert_period_ms, 0, self._timer_cb) + except Exception: + pass + return + if self._mode == "bg_alt": + if self._bg_phase == 0: + self._set_rgb(0, 1, 0) + self._bg_phase = 1 + else: + self._set_rgb(0, 0, 1) + self._bg_phase = 0 + return + if self._mode == "alert_rb": + self._alert_steps_left -= 1 + if self._alert_steps_left <= 0: + self._st_alert = None + try: + self._apply_by_table() + except Exception: + self._set_off() + return + if self._alert_rb_phase == 0: + self._set_rgb(0, 0, 1) + self._alert_rb_phase = 1 + else: + self._set_rgb(1, 0, 0) + self._alert_rb_phase = 0 + try: + self._timer.start(self._alert_period_ms, 0, self._timer_cb) + except Exception: + pass + return + + def set_led_state(self, event=None, msg=None): + # msg: dict {ai_state, charging, battery, signal, alert} + try: + if isinstance(msg, dict): + if "alert" in msg: + aval = msg.get("alert") + if aval in ("rb_flash_2", "rb_flash", "rb_flash2"): + self._set_alert_rb_flash(2, 250) + return + if aval in ("red_flash_3", "red_flash3"): + try: + n = 3 + ms = 500 + if "vc_config" in globals() and vc_config: + n = int(vc_config.get_int("BATTERY_EMPTY_FLASH_COUNT", 3)) + ms = int(vc_config.get_int("BATTERY_EMPTY_FLASH_PERIOD_MS", 500)) + self._set_red_flash_limited(n, ms) + except Exception: + self._set_red_flash_limited(3, 500) + return + if aval in (None, "", "none", "clear"): + self._st_alert = None + if "ai_state" in msg: + self._st_ai = msg.get("ai_state") or "idle" + if "charging" in msg: + self._st_charging = msg.get("charging") or "none" + if "battery" in msg: + self._st_battery = msg.get("battery") or "normal" + if "signal" in msg: + self._st_signal = msg.get("signal") or "normal" + except Exception: + pass + if self._mode in ("alert_rb", "red_flash_n"): + return + if self._led_state_debug_enabled() and isinstance(msg, dict): + try: + print("[LED] set_led_state in={}".format(msg)) + except Exception: + pass + self._apply_by_table(msg) + + def _led_state_debug_enabled(self): + try: + if "vc_config" in globals() and vc_config: + return vc_config.get_bool("LED_STATE_DEBUG", True) + except Exception: + pass + return True + + def _led_log(self, tag, extra=""): + if not self._led_state_debug_enabled(): + return + try: + print("[LED] {} ai={} chg={} bat={} sig={} -> mode={} period_ms={} {}".format( + tag, self._st_ai, self._st_charging, self._st_battery, self._st_signal, + self._mode, self._period_ms, extra)) + except Exception: + pass + + def _apply_by_table(self, _msg=None): + if self._mode in ("alert_rb", "red_flash_n"): + return + _prev_mode = self._mode + # P0:低电/空电(高于 AI 交互灯;充电中不显示,走充电灯态) + try: + if self._st_charging not in ("charging", "full"): + if self._st_battery == "empty": + try: + ms = 500 + if "vc_config" in globals() and vc_config: + ms = int(vc_config.get_int("BATTERY_EMPTY_FLASH_PERIOD_MS", 500)) + self._set_red_blink(ms) + except Exception: + self._set_red_blink(500) + return + if self._st_battery == "low": + self._set_red_blink(1000) + return + except Exception: + pass + # P1~P3:AI 阶段(播放 > 思考 > 聆听) + try: + if self._st_ai == "playing": + self._set_green_on() + return + if self._st_ai == "thinking": + # 蓝绿「交替」:约 0.5 s 换一色(`period_ms` 为换色间隔) + self._set_bg_alt(500) + return + if self._st_ai == "listening": + # 长对话蓝慢闪(≈1s);单对话 PTT 蓝快闪(≈2次/s,REQ-AI-07) + period_ms = 1000 + try: + if EventMesh.publish("get_storage_config", "VC_mode") == 1: + period_ms = 250 + except Exception: + pass + self._set_blue_blink(period_ms) + return + except Exception: + pass + # AI 交互中(聆听/思考/播放/流播)不让充电灯抢占;交互结束 idle 后恢复充电红/充满绿(REQ 冲突规避) + _skip_charge_led = 0 + try: + if self._st_ai in ("listening", "thinking", "playing"): + _skip_charge_led = 1 + except Exception: + pass + if not _skip_charge_led: + try: + if EventMesh.publish("get_vc_stream_active") == 1: + _skip_charge_led = 1 + except Exception: + pass + # 基础态:充电/电量/信号 + try: + if not _skip_charge_led: + if self._st_charging == "full": + self._set_green_on() + return + if self._st_charging == "charging": + self._set_red_on() + return + if self._st_signal == "weak": + # 弱网:蓝绿「慢交替」,与 thinking 同节拍定义(约 1 s 换一色) + self._set_bg_alt(1000) + return + except Exception: + pass + # 空闲待机:默认灭灯(REQ-BAT-01 / GAP-005 方案甲);LED_IDLE_MODE=green_blink 可恢复旧工程绿灯慢闪 + try: + idle_mode = "off" + if "vc_config" in globals() and vc_config: + _im = vc_config.get_attr("LED_IDLE_MODE", None) + if _im is not None: + idle_mode = str(_im).strip().lower() + if idle_mode in ("green_blink", "blink", "green"): + idle_on = 1000 + if "vc_config" in globals() and vc_config: + idle_on = int(vc_config.get_int("LED_IDLE_GREEN_BLINK_MS", 1000)) + idle_off = idle_on + if "vc_config" in globals() and vc_config: + _ioff = vc_config.get_attr("LED_IDLE_GREEN_BLINK_OFF_MS", None) + if _ioff is not None: + try: + idle_off = int(_ioff) + except Exception: + idle_off = idle_on + if idle_on < 400: + idle_on = 400 + if idle_off < 400: + idle_off = 400 + self._set_green_blink(idle_on, idle_off) + else: + self._set_off() + except Exception: + self._set_off() + if self._mode != _prev_mode: + self._led_log("apply_table", "in={}".format(_msg)) + + def get_led_debug_state(self, event=None, msg=None): + # 供按键/状态机打印当前 LED 聚合状态与最终模式 + try: + return { + "ai": self._st_ai, + "charging": self._st_charging, + "battery": self._st_battery, + "signal": self._st_signal, + "mode": self._mode, + "period_ms": self._period_ms, + } + except Exception: + return {"mode": getattr(self, "_mode", "?")} + diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_ota.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_ota.py new file mode 100644 index 00000000..168903dc --- /dev/null +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_ota.py @@ -0,0 +1,244 @@ +# -*- coding: utf-8 -*- +"""FOTA / DMP 上报。""" +try: + from qp_log import qp_print as print +except Exception: + pass +import EventMesh +import usys +import utime +import osTimer +import net +import sim +import modem +import request +import constants +import version_util +from misc import Power + +class OTA(): + def __init__(self): + #EventMesh.subscribe("min_sys_ota",self.min_sys_ota) + self.downloadFileVoURL1= "" + self.downloadFileVoURL2= "" + self.downloadFileVoURLBT="" + self.BT_ota_size =0 + self.ota_task_id = None + #蓝牙升级升级,返回0,没有升级;返回,1有升级,正在执行升级;返回2,电压不够 + def bt_ota(self,event=None,msg=None): + print("bt_ota_ota") + signal = net.csqQueryPoll() + if signal == -1 or sim.getStatus() != 1:#网络不正常 + print("bt_ota 无 SIM卡!!!!!!!!\r\n") + return 0 + # return 0 + response = None + ret_str =0 + + self.downloadFileVoURLBT="" + try: + #sourceVersion = versions.Build_Time + # sourceVersion = "7PIN-"+sourceVersion[:10]+"-"+sourceVersion[11:-3] + # print("sourceVersion:{}".format(sourceVersion)) + # sourceVersion = sourceVersion.replace(" ", "0") + # print("sourceVersion2:{}".format(sourceVersion)) + sourceVersion = version_util.get_unified_version() + print("sourceVersion:{}".format(sourceVersion)) + bt_versions = EventMesh.publish("get_bt_versions") + if bt_versions == None: + bt_versions="" + print("bt_versions:{}".format(bt_versions)) + #url = "http://iot.api.thisiot.com/ota/task?deviceId=860132060348836&sourceVersion=2024-12-20-15:35-1" + url =None + url = "https://dmp-api.qipai.com/qp-dmp-api/ota/task?deviceId={}&sourceVersion={}".format(modem.getDevImei(),sourceVersion) + print(url) + response = request.get(url,timeout=5) + if response.status_code == 200: + data = response.json() + print("OTA升级请求:{}".format(data)) + + if data: + try: + if data["code"] == 20007:#有新版本 + if data["data"]["targetVersion"] != bt_versions and "fileList"in data["data"]: + #if len(data["data"]["fileList"]) == 2: + for fileList in data["data"]["fileList"]: + + if fileList["fileName"] == "update.ufw": + self.BT_ota_size = fileList["size"] + self.downloadFileVoURLBT = fileList["url"] + if data["code"] == 20006:#已是最新版本 + ret_str = 0 + + except Exception as e: + usys.print_exception(e) + else: + print("bt_ota get error") + response.close() + except Exception as e: + usys.print_exception(e) + if response != None: + response.close() + + print("downloadFileVoURLBT{}".format(self.downloadFileVoURLBT)) + print("bt_ota end") + if self.downloadFileVoURLBT !="": + battery = EventMesh.publish("get_battery") + if battery>=40: + self.ota_timer = osTimer() + #self.ota_timer.start(15000, 0, self.ota_timer_cb) + if self.ota_task_id ==None: + self.ota_task_id = _thread.start_new_thread(self.ota_task,()) + ret_str = 1 + else: + ret_str = 2 + return ret_str + #小系统升级,返回0,没有升级;返回,1有升级,正在执行升级;返回2,电压不够 + def min_sys_ota(self,event=None,msg=None): + print("min_sys_ota") + signal = net.csqQueryPoll() + if signal == -1 or sim.getStatus() != 1:#网络不正常 + print("min_sys_ota 无 SIM卡!!!!!!!!\r\n") + return 0 + # return 0 + response = None + ret_str =0 + self.downloadFileVoURL1= "" + self.downloadFileVoURL2= "" + #self.downloadFileVoURLBT="" + try: + sourceVersion = version_util.get_unified_version() + print("sourceVersion:{}".format(sourceVersion)) + url =None + url = "https://dmp-api.qipai.com/qp-dmp-api/ota/task?deviceId={}&sourceVersion={}".format(modem.getDevImei(),sourceVersion) + print(url) + response = request.get(url,timeout=5) + if response.status_code == 200: + data = response.json() + print("OTA升级请求:{}".format(data)) + + if data: + try: + if data["code"] == 20007:#有新版本 + if "fileList"in data["data"]: + #if len(data["data"]["fileList"]) == 2: + for fileList in data["data"]["fileList"]: + if fileList["fileName"] == "dfota_2.bin": + self.downloadFileVoURL2 = fileList["url"] + elif fileList["fileName"] == "dfota_1.bin": + self.downloadFileVoURL1 = fileList["url"] + elif fileList["fileName"] == "update.ufw": + self.BT_ota_size = fileList["size"] + self.downloadFileVoURLBT = fileList["url"] + if data["code"] == 20006:#已是最新版本 + ret_str = 0 + + except Exception as e: + usys.print_exception(e) + else: + print("min_sys_ota get error") + response.close() + except Exception as e: + usys.print_exception(e) + if response != None: + response.close() + + print("min_sys_ota:downloadFileVoURL1:{},\ndownloadFileVoURL2:{},\ndownloadFileVoURLBT{}".format(self.downloadFileVoURL1,self.downloadFileVoURL2,self.downloadFileVoURLBT)) + print("min_sys_ota end") + if self.downloadFileVoURL2!="" and self.downloadFileVoURL1!="" : + battery = EventMesh.publish("get_battery") + if battery>=40: + self.ota_timer = osTimer() + #self.ota_timer.start(15000, 0, self.ota_timer_cb) + if self.ota_task_id ==None: + self.ota_task_id = _thread.start_new_thread(self.ota_task,()) + ret_str = 1 + else: + ret_str = 2 + return ret_str + + # def ota_timer_cb(self, *args, **kwargs): + # EventMesh.publish("publish_AI_btnInfo",{"func":self.usb_event}) + + def ota_task(self): + import fota + utime.sleep_ms(15000) + count = 30#最多等待30秒 + while 1: + count-=1 + VC_satrt_flag = EventMesh.publish("get_VC_satrt_flag")#获取vc是否在运行 + if VC_satrt_flag == None: + VC_satrt_flag = 0 + voice_record_satrt_flag = EventMesh.publish("get_voice_record_flag")#获取静默或者语音留言是否在运行 + if voice_record_satrt_flag == None: + voice_record_satrt_flag = 0 + if VC_satrt_flag == 0 or count<=0 or voice_record_satrt_flag == 0: + break + utime.sleep_ms(100) + if self.downloadFileVoURLBT != "" and self.BT_ota_size != 0: + count = 5 #重试5次 + while 1: + count -=1 + ret = EventMesh.publish("bt_ota_run",[self.downloadFileVoURLBT,self.BT_ota_size ]) #升级蓝牙 + + if ret == 0 or count <=0: + break + utime.sleep_ms(1000) + utime.sleep_ms(1000) + #EventMesh.publish("rest_TB") #复位蓝牙 + utime.sleep_ms(50000) + if self.downloadFileVoURL1 == "" and self.downloadFileVoURL2 == "": #cat1没有升级的时候,要重启设备 + Power.powerRestart() + if self.downloadFileVoURL1 != "" and self.downloadFileVoURL2 != "": + fota_obj = fota() + fota_obj.httpDownload(url1=self.downloadFileVoURL1,url2=self.downloadFileVoURL2) + #ota升级结果返回,开机时调用一次即可 + def ota_result(self,event=None,msg=None): + response = None + try: + currentVersion = version_util.get_unified_version() + print("currentVersion:{}".format(currentVersion)) + url = (constants.DMP_BASE + "/ota/result?deviceId={}¤tVersion={}").format(modem.getDevImei(),currentVersion) + print(url) + response = request.post(url,timeout=5) + if response.status_code == 200: + data = response.json() + print("OTA升级结果上报:{}".format(data)) + else: + print("ota_result error!!!!") + for i in response.text: + print(i) + response.close() + except Exception as e: + usys.print_exception(e) + if response != None: + response.close() + #ICCID上传 + def pots_ICCID(self,event=None,msg=None): + response = None + try: + iccid = sim.getIccid() + if iccid != -1 and len(iccid) > 5: + print("pots_ICCID:{}".format(iccid)) + #Headers = {'Transfer-Encodong':'chunked'} + token = EventMesh.publish("get_dev_token") + headers_T = None + if token: + headers_T = {"bz-dt":"{}".format(token)} + #http://dmp.raisound.com/bz-api/dev/iccid?deviceId=866610001099679&iccid=89860002192307889057 + url =None + url = (constants.DMP_BASE + "/dev/iccid?deviceId={}&iccid={}").format(modem.getDevImei(),iccid) + print("{} H:{}".format(url,headers_T)) + response = request.post(url,headers = headers_T,timeout=5) + if response.status_code == 200: + data = response.json() + print("pots_ICCID:{}".format(data)) + else: + print("pots_ICCID error!!!!") + for i in response.text: + print(i) + response.close() + except Exception as e: + usys.print_exception(e) + if response != None: + response.close() diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_session.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_session.py new file mode 100644 index 00000000..a9a34418 --- /dev/null +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_session.py @@ -0,0 +1,5886 @@ +# -*- coding: utf-8 -*- +"""语音对话 WebSocket / ASR / TTS 主业务。""" +from common import Abstract +try: + from qp_log import qp_print as print +except Exception: + pass +import EventMesh +import uwebsocket +import usys +import gc +import urandom +import uhashlib +import ubinascii +import ujson +import osTimer +import _thread +from queue import Queue +import utime +import audio +import uos +from machine import Pin +import modem +import sim +import net +import base64 +import request +import ql_fs +from misc import Power +import ntptime +import machine +import dataCall +import constants +import versions +import version_util +try: + import vc_config +except Exception: + vc_config = None +try: + import vc_text_intent +except Exception: + vc_text_intent = None +try: + import VAD +except Exception: + pass +try: + import usr.ai_alarm as _ai_alarm +except Exception: + import ai_alarm as _ai_alarm +from vc_common import ( + dev_type, + VC_HEARTBEAT_LOG, + VC_PROTO_GUARDS, + VC_WS_IO_LOG, + _VC_KEEPALIVE_RX_EVENTS, + _vc_out_json_event_meta, + PRESS_AUDIO_MODE, +) +from vc_fsm import ( + VCSessionState, + VCTurnState, + VC_TtsRxPhase, + VCMode, + VCStateMachine, + VC_record_TYPE_PCM, + VC_record_TYPE_AMR, + VC_play_TYPE_PCM, + VC_play_TYPE_MP3, + AI_client_VC_num, + AI_client_asr_num, +) +from vc_ota import OTA +from vc_utils import num_str_to_chinese + +class AI_VC(Abstract): + def get_vc_tts_playback_active(self, *args, **kwargs): + try: + return 1 if self._vc_tts_playback_active() else 0 + except Exception: + return 0 + + def get_vc_stream_active(self, *args, **kwargs): + # 供本地提示音侧查询:TTS 流播放期间本地不可抢占。 + try: + if self._vc_tts_playback_active(): + return 1 + except Exception: + pass + try: + if getattr(self.fsm, "await_played", False): + return 1 + except Exception: + pass + try: + if getattr(self.fsm, "tts_rx_phase", VC_TtsRxPhase.Idle) != VC_TtsRxPhase.Idle: + return 1 + except Exception: + pass + try: + t = getattr(self.fsm, "turn", VCTurnState.Idle) + if t in (VCTurnState.TtsReceiving, VCTurnState.Playing, VCTurnState.AwaitPlayedAck): + return 1 + except Exception: + pass + try: + if getattr(self, "_tts_pending", 0) == 1: + return 1 + except Exception: + pass + return 0 + + def _vc_fsm_log_snip(self): + try: + f = self.fsm + return "sess={} turn={} tts_rx={} await={} sp_up={}".format( + int(getattr(f, "session", -1)), + int(getattr(f, "turn", -1)), + int(getattr(f, "tts_rx_phase", -1)), + 1 if getattr(f, "await_played", False) else 0, + 1 if getattr(f, "speech_uplink_open", 0) else 0, + ) + except Exception: + return "fsm=?" + + def _vc_tts_pipeline_busy(self): + try: + if getattr(self.fsm, "await_played", False): + return True + if getattr(self.fsm, "tts_rx_phase", VC_TtsRxPhase.Idle) != VC_TtsRxPhase.Idle: + return True + t = getattr(self.fsm, "turn", VCTurnState.Idle) + if t in (VCTurnState.TtsReceiving, VCTurnState.Playing, VCTurnState.AwaitPlayedAck): + return True + if getattr(self, "_tts_pending", 0) == 1: + return True + if getattr(self, "VC_Play_flag", 0) == 1: + return True + if self._vc_local_play_busy(): + return True + except Exception: + pass + return False + + def _vc_local_play_busy(self): + try: + if getattr(self, "_vc_local_play_active", 0) != 1: + return False + until = getattr(self, "_vc_local_play_until_ms", 0) + if until and utime.ticks_diff(until, utime.ticks_ms()) > 0: + return True + self._vc_local_play_active = 0 + self._vc_local_play_until_ms = 0 + except Exception: + pass + return False + + def _vc_capture_allowed(self, reason=""): + """采集/上行开麦前:下行播放或本地短音未结束则禁止。""" + try: + if self._vc_tts_pipeline_busy(): + return False + if self._vc_local_play_busy(): + return False + except Exception: + return False + return True + + def _vc_stop_downlink_for_interrupt(self, reason="interrupt"): + """详设 DES-SCN-003:打断/取消时先本地停播清队列,再允许开麦。""" + try: + if getattr(self, "AI_audio_queue", None) is not None: + if self.AI_audio_queue.empty() != True: + self.AI_audio_queue.clear() + except Exception: + pass + try: + if self._vc_tts_pipeline_busy() or getattr(self, "is_ruppt_tts", 0) == 1: + self._drop_tts_audio = 1 + except Exception: + pass + try: + self._ptt_hard_mute_local_playback() + except Exception: + pass + try: + if getattr(self, "VC_Play_flag", 0) == 1 or getattr(self, "play_pcm", None) is not None: + self.VC_Play_End() + except Exception: + pass + try: + self._vc_local_play_active = 0 + self._vc_local_play_until_ms = 0 + except Exception: + pass + try: + self.fsm.await_played = False + self._need_audio_played_ack = 0 + self._tts_pending = 0 + self._tts_pending_ms = 0 + except Exception: + pass + try: + pending_intr = getattr(self, "_vc_pending_interrupt_uplink", 0) == 1 + if ( + not pending_intr + and getattr(self, "VC_record_state_flag", 0) != 0 + and getattr(self, "record_data_save_flag", 0) == 0 + ): + self._uplink_request_stop("stop_downlink/" + str(reason)) + except Exception: + pass + + def _vc_local_play(self, path, play_mode=1, loop=1, hold_ms=650): + """本地 MP3 短音:播前停录/停流式 TTS,避免与麦克风采集叠用。""" + try: + if getattr(self, "record_data_save_flag", 0) == 0 and getattr(self, "VC_record_state_flag", 0) != 0: + self._uplink_request_stop("local_play") + utime.sleep_ms(30) + except Exception: + pass + try: + if self._vc_tts_playback_active(): + self._ptt_hard_mute_local_playback() + try: + self.VC_Play_End() + except Exception: + pass + except Exception: + pass + try: + hold = int(hold_ms) + if hold < 200: + hold = 200 + self._vc_local_play_active = 1 + self._vc_local_play_until_ms = utime.ticks_add(utime.ticks_ms(), hold) + except Exception: + pass + try: + self.aud.play(play_mode, loop, path) + except Exception as e: + try: + self._vc_local_play_active = 0 + self._vc_local_play_until_ms = 0 + except Exception: + pass + usys.print_exception(e) + + def _vc_tts_playback_active(self): + """仅服务端 TTS/PCM 流在播(VC_Play_Start);aud.play 本地短音不算。""" + try: + if getattr(self, "VC_Play_flag", 0) == 1: + return True + except Exception: + pass + try: + if getattr(self, "play_pcm", None) is not None: + return True + except Exception: + pass + return False + + def _vc_vad_uplink_stop_eligible(self): + # 对应旧 VC_state >= SpeechStart + try: + if getattr(self.fsm, "speech_uplink_open", 0): + return True + t = getattr(self.fsm, "turn", VCTurnState.Idle) + return t not in (VCTurnState.Idle, VCTurnState.Listening) + except Exception: + return False + + def _vc_long_idle_active_speech_or_later(self): + # 对应旧 VC_state >= SpeechIng + try: + t = getattr(self.fsm, "turn", VCTurnState.Idle) + return t not in (VCTurnState.Idle, VCTurnState.Listening) + except Exception: + return False + + def _vc_is_business_idle(self): + # Level2:LED ai_state 为 idle,且无录音/流播/音乐闹钟等(与 WS ping 无关) + try: + if getattr(self, "music_play_flag", 0) == 1: + return False + if getattr(self, "alarm_play_flag", 0) == 1: + return False + except Exception: + pass + try: + if getattr(self, "VC_record_state_flag", 0) != 0: + return False + if getattr(self, "VC_Play_flag", 0) != 0: + return False + except Exception: + pass + try: + if EventMesh.publish("get_vc_stream_active") == 1: + return False + except Exception: + pass + try: + st = EventMesh.publish("get_led_debug_state") + if st and isinstance(st, dict): + if (st.get("ai") or "") != "idle": + return False + except Exception: + return False + return True + + def _vc_idle_deep_sleep_impl(self): + try: + _need_close = (self.VC_satrt_flag == 1) or (getattr(self, "AI_client_VC_isconnect", 0) == 1) + print("[VC][idle_L2] 触发休眠流程 need_Close={} VC_mode={}".format( + 1 if _need_close else 0, getattr(self, "VC_mode", -1))) + except Exception: + pass + try: + if self.VC_satrt_flag == 1 or getattr(self, "AI_client_VC_isconnect", 0) == 1: + try: + print("[VC][idle_L2] 调用 Close 断开会话/链路") + except Exception: + pass + self.Close() + except Exception: + pass + try: + utime.sleep_ms(100) + except Exception: + pass + try: + print("[VC][idle_L2] 发布 Hsensor_set_sleep") + EventMesh.publish("Hsensor_set_sleep") + except Exception: + pass + try: + print("[VC][idle_L2] 发布 SYSTEM_SLEEP ({})".format(constants.EVENT.SYSTEM_SLEEP)) + EventMesh.publish(constants.EVENT.SYSTEM_SLEEP) + except Exception: + pass + + def _vc_rx_resets_long_idle_timer(self, recv_data): + """是否应因本帧下行清零 close_timerout(排除保活/心跳,含 V2.5 文本 ping 对端应答)。""" + try: + if recv_data is None: + return False + if isinstance(recv_data, (bytes, bytearray)): + return True + if not isinstance(recv_data, str): + return False + t = recv_data.strip() + if not t: + return False + tl = t.lower() + if tl in ("ping", "pong"): + return False + try: + jd = ujson.loads(t) + except Exception: + return True + ev = jd.get("event") + if ev is None or ev == "": + ev = jd.get("type") + if ev is None: + return True + if str(ev).strip().lower() in _VC_KEEPALIVE_RX_EVENTS: + return False + return True + except Exception: + return True + + # ------------------------------------------------------------------------- + # 电源键 / AI 键:按 VC_mode 拆函数,便于阅读与修改(长对话 vs 单对话 PTT) + # ------------------------------------------------------------------------- + def _handle_pwk_short_release(self, event=None, msg=None, merged_ai_tail=False): + """电源键短按:读留言标志、退会议/翻译;否则 Close,或单键模式下走 AI_key_release。""" + print("pwk_key_release") + if EventMesh.publish("get_recv_voice") == 1: + EventMesh.publish("update_play_new_voice_info") + self.recv_voice = 0 + EventMesh.publish("set_history_voice_flag", 0) + self.AI_new_voice_url = "" + if self.trans_flag == 1 or self.meet_flag == 1: + EventMesh.publish("exit_meet_mode") + EventMesh.publish("exit_trans_mode") + return + if merged_ai_tail: + self.AI_key_release(event, msg) + else: + EventMesh.publish("publish_AI_btnInfo", {"func": self.Close}) + EventMesh.publish("stop_sos_call") + + def _ai_key_press_ptt_mode(self): + """单对话 PTT:短按仅停 TTS(不换 sid);非播放态短按吞掉;长按由 AI_key_long_press→ptt_begin。""" + if self.VC_mode != 1: + return False + try: + playing = False + try: + t = getattr(self.fsm, "turn", VCTurnState.Idle) + playing = t in (VCTurnState.TtsReceiving, VCTurnState.Playing) + except Exception: + playing = False + if playing or self._vc_tts_pipeline_busy(): + self._ptt_short_press_stop = 1 + try: + self._ptt_hard_mute_local_playback() + except Exception: + pass + try: + self.VC_Play_End() + except Exception: + pass + try: + self.fsm.await_played = False + except Exception: + pass + try: + self._ptt_led_lock_listening = 0 + self._ptt_interrupt_active = 0 + self.is_ruppt_tts = 0 + except Exception: + pass + try: + self._vc_publish_led("idle", "ptt_short_press/stop_tts") + except Exception: + pass + return True + except Exception: + pass + return True + + def _ai_key_press_long_dialogue_interrupt(self): + """长对话:TTS/播放忙或 turn==Thinking 时短按 → RequestToSpeak(先 cancel,canceled 后再开麦)。""" + if self.VC_mode != 0: + return False + try: + if getattr(self, "AI_client_VC_isconnect", 0) != 1 or getattr(self, "_vc_session_started", 0) != 1: + return False + except Exception: + return False + need_cancel = False + if self._vc_tts_pipeline_busy(): + need_cancel = True + else: + try: + if getattr(self.fsm, "turn", VCTurnState.Idle) == VCTurnState.Thinking: + need_cancel = True + except Exception: + pass + if not need_cancel: + return False + print("[KEY] long interrupt -> RequestToSpeak (cancel then uplink)") + self.is_ruppt_tts = 1 + try: + self._vc_pending_interrupt_uplink = 1 + except Exception: + pass + EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_RequestToSpeak}) + return True + + def _ai_key_press_long_dialogue_wake(self): + """长对话未连接或未 started:直接 AI_VC_start,勿 Close+sleep。""" + if self.VC_mode != 0: + return False + if getattr(self, "voice_flag", 0) == 1 or getattr(self, "meet_flag", 0) == 1 or getattr(self, "trans_flag", 0) == 1: + return False + try: + if getattr(self, "AI_client_VC_isconnect", 0) == 1 and getattr(self, "_vc_session_started", 0) == 1: + return False + except Exception: + pass + print("[KEY] long wake AI_VC_start (no Close)") + try: + self.Close_flag = 0 + except Exception: + pass + self.get_config_timer_count = 0 + self.key_state = 1 + self.AI_TTS_str = "" + self.AI_ASR_str = "" + self.AI_ASR_str_tmp = "" + self.AI_VC_start() + self.AI_key_long_press_flag = 0 + return True + + def _vc_led_alert_rb_flash(self): + try: + EventMesh.publish("set_led_state", {"alert": "rb_flash_2"}) + except Exception: + pass + + def _vc_clear_stale_record_state(self, reason=""): + """record_state_flag 非 0 但可能无真实录音时先停录复位,避免 uplink 门控拒开麦。""" + try: + if getattr(self, "VC_record_state_flag", 0) != 0: + self.VC_Speech_End() + except Exception: + try: + self.VC_record_state_flag = 0 + except Exception: + pass + + def _ai_key_press_long_dialogue_when_session_ready(self): + """长对话已连接且 session å·² started:非播放态直接开麦续聊(勿 idle 时发 cancel)。""" + if self.VC_mode != 0: + return False + try: + if getattr(self, "AI_client_VC_isconnect", 0) == 1 and getattr(self, "_vc_session_started", 0) == 1: + # 模式切换提示音会置 Close_flag=1;用户再短按须恢复可开麦 + try: + self.Close_flag = 0 + except Exception: + pass + self._vc_clear_stale_record_state("key.press/continue") + self.request_uplink_start("key.press/continue") + return True + except Exception: + pass + return False + + def _ai_key_release_handle_ptt(self): + """单对话 PTT 松手:仅在上行录音中时 end_speech;禁止误触发 AI_VC_start(否则新 sid / cancel 竞态)。""" + if self.AI_key_long_press_flag == 1: + if getattr(self, "VC_record_state_flag", 0) != 0: + try: + gap = 200 + if "vc_config" in globals() and vc_config: + gap = int(vc_config.get_int("PTT_RELEASE_END_SPEECH_DELAY_MS", 200)) + if gap > 0: + utime.sleep_ms(gap) + except Exception: + pass + print("[PTT] release end_speech asr_str={!r} rec={}".format( + self.AI_ASR_str, getattr(self, "VC_record_state_flag", -1))) + self.AI_client_VC_end_speech() + else: + self.AI_key_long_press_flag = 0 + try: + if getattr(self, "_ptt_short_press_stop", 0) == 1: + self._ptt_short_press_stop = 0 + except Exception: + pass + print("[PTT] release_no_uplink_skip") + return + try: + if getattr(self, "_ptt_short_press_stop", 0) == 1: + self._ptt_short_press_stop = 0 + except Exception: + pass + print("[PTT] release_short_no_long") + + def _ai_key_long_press_route(self, event=None, msg=None): + """长按:翻译先 Close 再开翻译录音;单对话→ptt_begin;长对话→与短按一致走 AI_key_press 链路。""" + if self.trans_flag == 1: + self.Close() + EventMesh.publish("start_voice_record", 4) + self.AI_key_long_press_flag = 1 + if self.VC_mode == 1: + EventMesh.publish("publish_AI_btnInfo", {"func": self.ptt_begin}) + else: + EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_key_press}) + + # ===== Push-To-Talk(单对话模式)===== + # 语义: + # - 长按:无论当前处于 TTS/Thinking/任何中间态,立即打断并进入新一轮录音上行 + # - 松开:停止录音并等待服务端返回(由 AI_client_VC_end_speech 触发) + def _ptt_end_led_suppress(self): + # 保持 _vc_play_end_suppress_idle_led / _ptt_led_lock_listening 直至真开录(VC_Speech_Start) + pass + + def _ptt_should_drop_inbound_tts(self): + """单对话打断窗口:丢弃迟到 TTS 帧 / 禁止起播。""" + try: + if getattr(self, "VC_mode", -1) != 1: + return False + if getattr(self, "_drop_tts_audio", 0) == 1: + return True + if getattr(self, "is_ruppt_tts", 0) == 1: + return True + if getattr(self, "_ptt_interrupt_active", 0) == 1: + return True + if getattr(self, "_vc_serial_canceling", 0) == 1: + return True + if getattr(self, "_ptt_pending_arm", 0) == 1: + return True + except Exception: + pass + return False + + def _ptt_should_skip_remote_playback(self): + return self._ptt_should_drop_inbound_tts() + + def _ptt_enter_interrupt_arm(self): + try: + self._ptt_arm_epoch = int(getattr(self, "_ptt_arm_epoch", 0)) + 1 + self._ptt_interrupt_active = 1 + self._ptt_led_lock_listening = 1 + self._vc_play_end_suppress_idle_led = 1 + self._drop_tts_audio = 1 + self.is_ruppt_tts = 1 + except Exception: + pass + + def _ptt_abort_interrupt_arm(self): + try: + self._ptt_interrupt_active = 0 + self._ptt_led_lock_listening = 0 + self._vc_play_end_suppress_idle_led = 0 + self._drop_tts_audio = 0 + self.is_ruppt_tts = 0 + except Exception: + pass + + def _ptt_on_record_hardware_started(self): + """真开录后:允许新 TTS、放开灯锁;旧 TTS 已在打断期丢弃。""" + try: + self._ptt_interrupt_active = 0 + self._ptt_led_lock_listening = 0 + self._vc_play_end_suppress_idle_led = 0 + self._drop_tts_audio = 0 + self.is_ruppt_tts = 0 + except Exception: + pass + + def _ptt_hard_mute_local_playback(self): + try: + self.VC_Play_flag = 0 + if getattr(self, "VC_play_TYPE", None) == VC_play_TYPE_PCM: + if getattr(self, "play_pcm", None) is not None: + try: + self.play_pcm.stream_stop() + self.play_pcm.close() + except Exception: + pass + self.play_pcm = None + try: + self.aud.stopPlayStream() + self.aud.stopAll() + except Exception: + pass + if getattr(self, "AI_audio_queue", None) is not None: + try: + if self.AI_audio_queue.empty() != True: + self.AI_audio_queue.clear() + except Exception: + pass + except Exception: + pass + try: + gap = 0 + if "vc_config" in globals() and vc_config: + gap = int(vc_config.get_int("PTT_POST_MUTE_MS", 30)) + if gap > 0: + utime.sleep_ms(gap) + except Exception: + pass + + def battery_priority_mute_vc(self, event=None, msg=None): + """低电/关机提示音:短暂停 VC TTS,避免被流式播放挡住。""" + try: + self._ptt_hard_mute_local_playback() + except Exception: + pass + try: + if getattr(self, "VC_Play_flag", 0) == 1 or getattr(self, "play_pcm", None) is not None: + self.VC_Play_End() + except Exception: + pass + + def _vc_enqueue_btn(self, func, data=None): + """统一经 publish_AI_btnInfo 入队,在 btnInfo_queue_thread 串行执行。""" + try: + if data is not None: + EventMesh.publish("publish_AI_btnInfo", {"func": func, "data": data}) + else: + EventMesh.publish("publish_AI_btnInfo", {"func": func}) + except Exception: + pass + + def _vc_ptt_cancel_wait_ms(self): + try: + if "vc_config" in globals() and vc_config: + return int(vc_config.get_int("PTT_CANCEL_WAIT_MS", 2500)) + except Exception: + pass + return 2500 + + def _vc_ptt_need_interrupt_wait(self): + """单对话:服务端仍在思考/收 TTS/播报时,开录前须先 cancel 并等 canceled。""" + try: + if getattr(self, "VC_mode", -1) != 1: + return False + turn = getattr(self.fsm, "turn", VCTurnState.Idle) + if self._vc_tts_pipeline_busy(): + return True + if turn in ( + VCTurnState.Thinking, VCTurnState.TtsReceiving, + VCTurnState.Playing, VCTurnState.AwaitPlayedAck, + ): + return True + except Exception: + pass + return False + + def _vc_begin_ptt_cancel_wait(self, reason="ptt_interrupt"): + try: + self._ptt_pending_arm = 1 + self._vc_serial_canceling = 1 + try: + self._vc_cancel_deadline_ms = utime.ticks_add( + utime.ticks_ms(), self._vc_ptt_cancel_wait_ms()) + except Exception: + self._vc_cancel_deadline_ms = 0 + json_data = {"event": "cancel", "payload": {}} + self.AI_client_VC_send(ujson.dumps(json_data)) + try: + self.fsm.reset_rx_phases() + except Exception: + pass + self._drop_tts_audio = 1 + print("[VC][serial] cancel_wait reason={} turn={}".format( + reason, getattr(getattr(self, "fsm", None), "turn", "?"))) + except Exception: + pass + + def _vc_on_ptt_cancel_settled(self, via="canceled"): + """btn 线程:cancel 已收尾,执行挂起的 PTT 开录。""" + try: + if not getattr(self, "_vc_serial_canceling", 0) and not getattr(self, "_ptt_pending_arm", 0): + return + self._vc_serial_canceling = 0 + self._vc_cancel_deadline_ms = 0 + if getattr(self, "_ptt_pending_arm", 0): + self._ptt_pending_arm = 0 + print("[VC][serial] ptt_arm after {}".format(via)) + self._ptt_arm_uplink() + except Exception: + pass + + def _vc_tick_serial_cancel_timeout(self): + if not getattr(self, "_vc_serial_canceling", 0): + return + try: + dl = getattr(self, "_vc_cancel_deadline_ms", 0) + if dl and utime.ticks_diff(utime.ticks_ms(), dl) >= 0: + print("[VC][serial] cancel_wait timeout -> arm") + self._vc_on_ptt_cancel_settled(via="timeout") + except Exception: + pass + + def _vc_fsm_dispatch_cb(self, data): + """btn 线程:处理服务端 FSM 事件(单对话串行)。""" + try: + ev, jsload = data + except Exception: + return + try: + actions = self.fsm.on_server_event(ev, jsload) + self._fsm_apply_actions(actions) + except Exception as e: + usys.print_exception(e) + if ev == "canceled": + self._vc_on_ptt_cancel_settled(via="canceled") + + def _vc_dispatch_server_event(self, ev, jsload_data=None): + try: + if ev == "canceled": + self._vc_stop_downlink_for_interrupt("canceled") + except Exception: + pass + if getattr(self, "VC_mode", -1) == 1: + self._vc_enqueue_btn(self._vc_fsm_dispatch_cb, (ev, jsload_data)) + else: + try: + self._fsm_apply_actions(self.fsm.on_server_event(ev, jsload_data)) + except Exception: + pass + + def _ptt_arm_uplink(self, t0=0): + """btn 线程:cancel 已结束或无需 cancel 时,发 audio.create 并开麦。""" + try: + self.is_ruppt_tts = 0 + except Exception: + pass + try: + self._ptt_audio_created = 0 + except Exception: + pass + try: + self.AI_ASR_flag = 0 + except Exception: + pass + try: + self.AI_client_VC_send_speech() + except Exception: + pass + try: + self.VC_Speech_Start_now() + except Exception: + pass + try: + self._vc_publish_led("listening", "ptt_begin/recording") + except Exception: + pass + self._ptt_end_led_suppress() + try: + if t0: + print("[PTT] record started (ms={})".format(utime.ticks_ms() - t0)) + except Exception: + pass + + def ptt_begin(self, reason="key.long_press"): + try: + if self.VC_mode != 1: + return + except Exception: + return + + # PTT 新一轮开始:清理模式切换/提示音退出标志 + try: + self.Close_flag = 0 + self._ptt_await_server = 0 + except Exception: + pass + + t0 = 0 + try: + t0 = utime.ticks_ms() + except Exception: + t0 = 0 + + self._ptt_enter_interrupt_arm() + try: + self._vc_publish_led("listening", "ptt_begin/pre") + except Exception: + pass + + # 本地先硬静音再 VC_init,避免 DMA/队列尾包漏播 + try: + self._ptt_hard_mute_local_playback() + except Exception: + pass + try: + self.VC_init() + except Exception: + pass + try: + if getattr(self, "VC_Play_flag", 0) == 1 or getattr(self, "play_pcm", None) is not None: + self.VC_Play_End() + except Exception: + pass + try: + self._ptt_hard_mute_local_playback() + except Exception: + pass + + # 若上一轮录音尚未结束,先强制停掉,避免 record_thread 复用出错 + try: + if getattr(self, "VC_record_state_flag", 0) != 0: + self.VC_Speech_End() + except Exception: + pass + + # 已在等 canceled:只刷新「待开录」,勿重复 audio.create + try: + if getattr(self, "_vc_serial_canceling", 0): + self._ptt_pending_arm = 1 + print("[PTT] cancel in progress, refresh pending arm") + self._ptt_end_led_suppress() + return + except Exception: + pass + + # 会话未连接:先建链并发 Start,等待 started 后再自动开麦 + try: + if getattr(self, "AI_client_VC_isconnect", 0) != 1: + self._ptt_force_record_on_started = 1 + self.AI_VC_start() + return + except Exception: + self._ptt_force_record_on_started = 1 + try: + self.AI_VC_start() + except Exception: + pass + return + + # 会话还没 initialized:先发 Start,请求服务端进入 started + if getattr(self, "_vc_session_started", 0) != 1: + self._ptt_force_record_on_started = 1 + try: + try: + if t0: + print("[PTT] wait server.started (ms={})".format(utime.ticks_ms() - t0)) + except Exception: + pass + self.AI_client_VC_start_requset() + except Exception: + pass + return + + # 服务端仍在思考/TTS:先 cancel,在 btn 队列收到 canceled 后再开录(方案甲串行) + if self._vc_ptt_need_interrupt_wait(): + self._vc_begin_ptt_cancel_wait("ptt_begin") + return + + self._ptt_arm_uplink(t0) + + def _vc_effective_asr_text(self): + try: + t = (getattr(self, "AI_ASR_str", "") or "").strip() + if t: + return t + return (getattr(self, "_last_asr_result_text", "") or "").strip() + except Exception: + return "" + + def _vc_long_dialogue_auto_uplink_ok(self, reason): + """长对话 canceled/completed 自动开麦:须有有效 ASR,且防抖/勿在 LLM 无 TTS 后抢开。""" + try: + if reason not in ("canceled", "completed", "local.play_end", "audio.played"): + return True + if getattr(self, "Close_flag", 0) == 1: + return False + if reason == "canceled" and getattr(self, "_vc_pending_interrupt_uplink", 0) == 1: + pass + elif not self._vc_effective_asr_text(): + try: + print("[VC][uplink_ctrl] Deny START by {} cause=empty_asr".format(reason)) + except Exception: + pass + return False + if reason == "completed": + if getattr(self, "_turn_has_tts_started", 0) == 0: + try: + if getattr(self, "_llm_completed_ms", 0) or getattr(self.fsm, "turn", None) == VCTurnState.Thinking: + print("[VC][uplink_ctrl] Deny START by completed cause=llm_without_tts") + return False + except Exception: + pass + try: + now = utime.ticks_ms() + last = getattr(self, "_vc_last_auto_uplink_ms", 0) + if last and utime.ticks_diff(now, last) < 1200: + print("[VC][uplink_ctrl] Deny START by {} cause=auto_uplink_debounce".format(reason)) + return False + self._vc_last_auto_uplink_ms = now + except Exception: + pass + except Exception: + pass + return True + + def _vc_led_state_debug_enabled(self): + try: + if "vc_config" in globals() and vc_config: + return vc_config.get_bool("LED_STATE_DEBUG", True) + except Exception: + pass + return True + + def _vc_led_should_standby_idle(self): + """未录音且非 TTS 播中:待机/提示音/模式切换/单对话轮间应灭灯(DES-L3 idle),勿被 FSM.turn=Listening 误导。""" + try: + if getattr(self, "VC_record_state_flag", 0) != 0: + return False + if self._vc_tts_playback_active(): + return False + if getattr(self, "_ptt_await_server", 0) == 1: + return False + t = getattr(self.fsm, "turn", VCTurnState.Idle) + if t in ( + VCTurnState.Thinking, + VCTurnState.TtsReceiving, + VCTurnState.Playing, + VCTurnState.AwaitPlayedAck, + ): + return False + if getattr(self, "Close_flag", 0) == 1: + return True + if getattr(self, "VC_mode", -1) == 1: + return True + k = getattr(self, "_last_tts_kind", "") or "" + if k in ("prompt", "boot_prompt", "mode_switch_long", "mode_switch_ptt"): + return True + except Exception: + pass + return False + + def _vc_publish_led(self, ai_state, reason=""): + try: + if getattr(self, "_ptt_led_lock_listening", 0) == 1: + if ai_state != "listening": + if self._vc_led_state_debug_enabled(): + print("[LED] publish blocked ai={} reason={} (ptt_arm_lock)".format( + ai_state, reason)) + return + except Exception: + pass + try: + if self._vc_led_state_debug_enabled(): + print("[LED] publish ai={} reason={} VC_mode={} rec={} turn={}".format( + ai_state, reason, getattr(self, "VC_mode", -1), + getattr(self, "VC_record_state_flag", -1), + getattr(getattr(self, "fsm", None), "turn", "?"))) + except Exception: + pass + try: + EventMesh.publish("set_led_state", {"ai_state": ai_state}) + except Exception: + pass + + def _vc_led_sync_from_fsm(self): + """按 FSM/真实 TTS 播放恢复 AI 灯;本地 aud.play 短音不参与。""" + try: + if getattr(self, "_ptt_led_lock_listening", 0) == 1: + if getattr(self, "VC_record_state_flag", 0) == 0: + return + except Exception: + pass + try: + if self._vc_led_should_standby_idle(): + self._vc_publish_led("idle", "fsm_sync/standby_idle") + return + except Exception: + pass + try: + if getattr(self, "VC_mode", -1) == 1 and getattr(self, "_ptt_await_server", 0) == 1: + self._vc_publish_led("thinking", "fsm_sync/ptt_await") + return + except Exception: + pass + try: + if self._vc_tts_playback_active(): + self._vc_publish_led("playing", "fsm_sync/tts") + return + except Exception: + pass + try: + t = getattr(self.fsm, "turn", VCTurnState.Idle) + if t in (VCTurnState.Thinking, VCTurnState.TtsReceiving): + self._vc_publish_led("thinking", "fsm_sync/thinking") + elif t in (VCTurnState.Playing, VCTurnState.AwaitPlayedAck): + # 服务端已标记播放态但本地 PCM 尚未起播:仍显示思考,勿误绿灯 + self._vc_publish_led("thinking", "fsm_sync/await_tts") + elif t in (VCTurnState.Listening, VCTurnState.SpeechEnded, VCTurnState.SpeechStreaming): + self._vc_publish_led("listening", "fsm_sync/listening") + else: + self._vc_publish_led("idle", "fsm_sync/idle") + except Exception: + try: + self._vc_publish_led("idle", "fsm_sync/fallback") + except Exception: + pass + + def _uplink_request_start(self, reason="", *args, **kwargs): + # 统一入口:避免多处同时 start 造成冲突(尤其是 TTS/播放阶段误开麦导致回声触发与状态紊乱) + try: + if self.Close_flag == 1: + try: + if reason in ("canceled", "local.play_end", "audio.played", "key.press/continue"): + print("[VC][uplink_ctrl] Deny START by {} cause=Close_flag".format(reason)) + except Exception: + pass + return + except Exception: + return + + # 连续对话才允许“自动开新一轮上行” + try: + if self.VC_mode != 0: + try: + if reason in ("canceled", "local.play_end", "audio.played"): + print("[VC][uplink_ctrl] Deny START by {} cause=VC_mode({})".format(reason, self.VC_mode)) + except Exception: + pass + return + except Exception: + return + + try: + if self.VC_mode == 0 and not self._vc_long_dialogue_auto_uplink_ok(reason): + return + except Exception: + pass + + try: + if not self._vc_capture_allowed(reason): + try: + print("[VC][uplink_ctrl] Deny START by {} cause=playback_busy".format(reason)) + except Exception: + pass + return + except Exception: + return + + # ===== 业务门控(收敛到 FSM turn/session)===== + try: + if getattr(self.fsm, "session", VCSessionState.Disconnected) != VCSessionState.Started: + try: + if reason in ("canceled", "local.play_end", "audio.played"): + print("[VC][uplink_ctrl] Deny START by {} cause=fsm.session".format(reason)) + except Exception: + pass + return + turn = getattr(self.fsm, "turn", VCTurnState.Idle) + # 仅 Listening/SpeechEnded 允许开麦进入下一轮;其它阶段一律拒绝(避免抢跑打乱链路) + if turn not in (VCTurnState.Listening, VCTurnState.SpeechEnded): + try: + if reason in ("canceled", "local.play_end", "audio.played"): + print("[VC][uplink_ctrl] Deny START by {} cause=fsm.turn({})".format(reason, turn)) + except Exception: + pass + return + except Exception: + return + + # 若当前不在录音上行,才允许开新一轮 + if getattr(self, "VC_record_state_flag", 0) != 0: + # canceled 打断后:可能是僵死 flag=2(started 未真开麦),先停录复位再开麦 + if reason == "canceled": + try: + self._vc_clear_stale_record_state("uplink_start/canceled") + except Exception: + pass + if getattr(self, "VC_record_state_flag", 0) != 0: + try: + if reason in ("canceled", "local.play_end", "audio.played"): + print("[VC][uplink_ctrl] Deny START by {} cause=VC_record_state_flag({})".format( + reason, getattr(self, "VC_record_state_flag", 0))) + except Exception: + pass + return + + # 新一轮开始录音:允许上行(上一轮可能已置 silence 闸门) + if getattr(self, "record_data_save_flag", 0) == 0: + self._server_vad_silence = 0 + + print("[VC][uplink_ctrl] Request START by {}".format(reason)) + try: + if reason == "canceled" and getattr(self, "_vc_pending_interrupt_uplink", 0) == 1: + self._vc_pending_interrupt_uplink = 0 + print("[VC][uplink_ctrl] interrupt_uplink armed -> start (canceled)") + except Exception: + pass + self.VC_Speech_Start_now() + + def _uplink_request_stop(self, reason="", *args, **kwargs): + # 统一入口:避免多处同时 stop 造成冲突 + if getattr(self, "record_data_save_flag", 0) == 0: + self._server_vad_silence = 1 + + if getattr(self, "VC_record_state_flag", 0) == 0: + try: + if getattr(self, "VC_mode", 0) != 1: + print("[VC][long_idle] _uplink_request_stop skip (record_state==0) reason={}".format(reason)) + except Exception: + pass + return + + try: + if getattr(self, "VC_mode", 0) != 1: + print("[VC][long_idle] step=uplink_stop_exec reason={} VC_record_state_flag={}".format( + reason, getattr(self, "VC_record_state_flag", -1))) + except Exception: + pass + print("[VC][uplink_ctrl] Request STOP by {}".format(reason)) + self.VC_Speech_End() + + def _uplink_mark_start(self, first_len=0): + try: + if self._uplink_active == 1: + return + self._uplink_active = 1 + self._uplink_bytes = 0 + self._uplink_start_ms = utime.ticks_ms() + print("[VC][uplink] START len={} {} VC_record_state_flag={}".format( + first_len, self._vc_fsm_log_snip(), self.VC_record_state_flag)) + except Exception: + pass + + def _uplink_mark_end(self, reason=""): + try: + if self._uplink_active != 1: + return + dur = utime.ticks_diff(utime.ticks_ms(), self._uplink_start_ms) if self._uplink_start_ms else -1 + print("[VC][uplink] END reason={} bytes={} dur_ms={} {} VC_record_state_flag={}".format( + reason, self._uplink_bytes, dur, self._vc_fsm_log_snip(), self.VC_record_state_flag)) + except Exception: + pass + self._uplink_active = 0 + self._uplink_bytes = 0 + self._uplink_start_ms = 0 + + # 最近一次 TTS 的语义来源(dialog/prompt/mode_switch_long/mode_switch_ptt/...) + self._last_tts_kind = "" + # 下一个“无 dialog_id 的提示音 TTS”在 tts.started 时采用该 kind + self._pending_tts_kind = "" + + # ===== 状态机动作接口(骨架)===== + # 说明:统一把“副作用动作”收敛到这些方法,便于后续把 recv/timer 的 if/flag 逻辑迁移到 VCStateMachine。 + def request_send_start(self): + EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_start_requset}) + + def request_send_audio_create(self): + EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_send_speech}) + + def request_send_audio_completed(self): + EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_end_speech}) + + def request_send_audio_played(self): + EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_audio_played}) + + def request_send_synthesis(self, text): + try: + self.AI_TTS_str = text + except Exception: + pass + EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_synthesis}) + + def request_uplink_start(self, reason=""): + EventMesh.publish("publish_AI_btnInfo", {"func": self._uplink_request_start, "data": reason}) + + def request_uplink_stop(self, reason=""): + EventMesh.publish("publish_AI_btnInfo", {"func": self._uplink_request_stop, "data": reason}) + + def _fsm_apply_actions(self, actions): + try: + for a in actions: + if not a: + continue + if a[0] == "uplink_stop": + self.request_uplink_stop(a[1] if len(a) > 1 else "") + elif a[0] == "uplink_start": + self.request_uplink_start(a[1] if len(a) > 1 else "") + elif a[0] == "play_start": + if getattr(self, "VC_mode", -1) == 1: + if self._ptt_should_skip_remote_playback(): + if self._vc_led_state_debug_enabled(): + print("[VC][serial] skip play_start (ptt_interrupt)") + continue + try: + self.fsm.on_local_play_start() + except Exception: + pass + EventMesh.publish("publish_AI_btnInfo", {"func": self.VC_Play_Start}) + elif a[0] == "send_audio_played": + self.request_send_audio_played() + except Exception: + pass + + # AI + + def _print_hw_diag(self): + # 仅诊断使用:打印当前分支能看到的麦克/音频相关配置和运行时状态。 + # JSWM2 老分支没有 device_profile/_PROFILE,这里宽容处理 + try: + profile = _PROFILE if isinstance(_PROFILE, dict) else {} + except Exception: + profile = {} + dev_type_profile = profile.get("dev_type", None) + flags = profile.get("flags", {}) + hardware = profile.get("hardware", {}) + product_cfg = profile.get("product_cfg", {}) + vc_cfg = {} + try: + vc_cfg = product_cfg.get("vc", {}) + except Exception: + vc_cfg = {} + + print("[hw-diag] ===== AI_VC hardware diag =====") + print("[hw-diag] profile.dev_type =", dev_type_profile) + print("[hw-diag] profile.flags =", flags) + print("[hw-diag] profile.hardware =", hardware) + print("[hw-diag] product_cfg.vc =", vc_cfg) + + try: + print("[hw-diag] VC_record_samplerate =", self.VC_record_samplerate) + print("[hw-diag] VC_play_samplerate =", self.VC_play_samplerate) + except Exception: + pass + try: + print("[hw-diag] vad_silence_runtime =", self.vad_silence) + except Exception: + pass + try: + print("[hw-diag] record_data_save_flag =", self.record_data_save_flag) + print("[hw-diag] record_max_bytes =", self.record_max_bytes) + except Exception: + pass + + try: + g = self.record.gain_get() + except Exception as e: + g = "gain_get_error: {}".format(e) + print("[hw-diag] record.gain_get =", g) + try: + print("[hw-diag] type(record) =", type(self.record)) + print("[hw-diag] type(aud) =", type(self.aud)) + except Exception: + pass + print("[hw-diag] ===== end of hw-diag =====") + + def __init__(self): + # import voiceCall + # voiceCall.callStart("10086") + # return + self.http_get_config_flag = 0 + if self.http_get_config_flag == 1: + try: + self.http_get_config()#获取/更新配置参数 + except Exception as e: + usys.print_exception(e) + self.VC_30S_timer = osTimer() + self.ai_key_press_audio = osTimer() + self.VC_30S_timer.start(30000, 1, self.VC_30S_timer_cb) + self.online_kws = 0#是否开启线上关键词 + self.get_config_timer_count = 0 + self.record_data_save_flag = 0 #是否保存录音文件 + self.play_data_save_flag = 0#是否保存tts数据到文件 + self.record_fd = None + self.play_fd = None + self.is_VC_ACK = 0 #是否开启ack应答模式 + self.is_long_record = 0 #是否开启长录音模式(语音打断模式) + self.ai_OTA = OTA() + self.is_auth = 1 #是否开启dmp鉴权 + #self.VC_mode = 1 #0:短按连续问答模式 1:长按录音模式 + self.VC_mode = EventMesh.publish("get_storage_config","VC_mode")# + if self.VC_mode == None : + self.VC_mode = 0 + self.is_up_error= 0 #是否上传错误id + self.Close_flag =0 #=1时,连续对话播放完音频就退出 + # 服务端 VAD 结束标志:收到 vad.silence 后置 1,用于硬闸门禁止继续上行音频 + # (避免仍持续上行导致服务端推迟/抑制后续 TTS,从而一直停留在思考态蓝绿闪) + self._server_vad_silence = 0 + # 单轮状态标志:用于判定 completed 是否应自动进入下一轮 + self._turn_has_asr_completed = 0 + self._turn_has_vad_silence = 0 + self._turn_has_tts_started = 0 + # VC 会话是否已进入 started(服务端 session initialized) + # 重要:在收到 started 前发送音频帧会触发服务端 "session not initialized" → failed → 端侧重连死循环 + self._vc_session_started = 0 + # 开机/提示音:本轮 started 不自动开麦(避免问候 TTS 前误进 listening) + self._skip_started_uplink_once = 0 + self._last_tts_kind = "" + self._pending_tts_kind = "" + # Push-To-Talk(VAD关闭/单对话)是否已发送 audio.create + # 重要:协议要求 disable_vad=true 时必须 audio.create→音频帧→audio.completed + self._ptt_audio_created = 0 + # PTT:松手等服务器期间保持 thinking 勿回蓝闪聆听 + self._ptt_await_server = 0 + # 方案甲 btnInfo 串行:打断时先 cancel,收到 canceled(或超时)后再 audio.create + self._vc_serial_canceling = 0 + self._ptt_pending_arm = 0 + self._vc_cancel_deadline_ms = 0 + # PTT 打断:轮次号 + 开录前丢弃旧 TTS / 灯只显示聆听 + self._ptt_arm_epoch = 0 + self._ptt_interrupt_active = 0 + self._ptt_led_lock_listening = 0 + # 端侧主动发 synthesis 后到收到 tts.started 之间的窗口标志,用于阻止误开下一轮录音上行 + self._tts_pending = 0 + self._tts_pending_ms = 0 + # 各类兜底的节流时间戳(避免“兜底过激/连续触发”) + self._fb_last_thinking_restart_ms = 0 + self._fb_last_tts_pending_timeout_ms = 0 + # thinking 兜底:部分情况下服务端只返回 llm.completed 而不触发 tts.started/completed, + # 会导致一直停留在 thinking。此时超时自动回到聆听。 + self._llm_completed_ms = 0 + # 语音上行统计(真实发送到 VC 发送队列的开始/结束) + self._uplink_active = 0 + self._uplink_bytes = 0 + self._uplink_start_ms = 0 + + # 状态机骨架(后续逐步把旧逻辑迁移进来) + self.fsm = VCStateMachine(self) + + self.play_new_voice_flag =0 #播放文字留言flag + + self.wait_first_flag = 0#是否进入未读判定(只播报一次) + self.wait_first_timer = osTimer()#如果播放未读留言的时候未读则不要播放了 + self.wait_first_cnt = 0 + + self.wait_sort = 0#未读留言正在播第几条 + EventMesh.subscribe("play_tts",self.play_tts) + EventMesh.subscribe("play_new_voice",self.play_new_voice) + EventMesh.subscribe("play_his_voice",self.play_his_voice) + EventMesh.subscribe("play_wait_voice",self.play_wait_voice) + EventMesh.subscribe("play_single_voice",self.play_single_voice)#不带tts的播放(闹钟) + EventMesh.subscribe("play_music",self.play_music)#播放音乐 + EventMesh.subscribe("play_trans_tts",self.play_trans_tts)#接收翻译tts + EventMesh.subscribe("set_vad_silence",self.set_vad_silence) + EventMesh.subscribe("set_wait_first_flag",self.set_wait_first_flag) + EventMesh.subscribe("kws_even",self.kws_even) + EventMesh.subscribe("change_volume",self.change_volume) + EventMesh.subscribe("find_cmd",self.find_cmd) #查询命令 + EventMesh.subscribe("NLP_set_vad_silence",self.NLP_set_vad_silence) #设置聆听间隔 + EventMesh.subscribe("change_voice",self.change_voice) #改变音色 + EventMesh.subscribe("Close_VC",self.Close_VC) #外部结束对话 + EventMesh.subscribe("get_recv_voice",self.get_recv_voice) #获取留言播放状态 + EventMesh.subscribe("set_recv_voice",self.set_recv_voice) #设置留言播放状态 + self.voice_timer = osTimer()#语音留言10S按下按键确定是否播放的判断定时器 + self.voice_cnt = 0 #留言定时器计数 + self.voicerupt_flag = 0#留言选择按键接听标志位 + self.wait_voicerupt_flag = 0#未读留言选择按键接听标志位 + self.recv_voice = 0 #是否收到留言 + self.voice_notice = 0 #收到留言并播放则置为1,接下来要播放提示词 + self.voice_choose_flag = 0#当tts和语音留言冲突时,为了防止语音地址被误清,做标志位在close做判断 + + self.revoice_timer = osTimer()#语音留言10S按下按键确定是否回复的判断定时器 + self.revoice_cnt = 0 #回复留言定时器计数 + self.revoicerupt_flag = 0#留言选择按键接听标志位 + + self.music_timer = osTimer()#音乐播放定时器,等待tts播完才放音乐 + self.music_cnt = 0 #留言定时器计数 + + self.his_timer = osTimer()#历史留言播放定时器,等待tts播完再播留言 + self.his_cnt = 0#历史留言定时器技术 + + self.wait_timer = osTimer()#未读留言播放定时器,等待tts播完再播留言 + self.wait_cnt = 0#未读留言定时器技术 + + self.error_data = [] + self.asr_taskid = "" + self.VC_taskid = "" + self.VC_server = 2 #0柒牌正式服务器,1:测试服务器1,2:测试服务器2 + self.voice_flag = 0 + + #会议记录标志 + self.meet_flag = 0 + #AI翻译标志 + self.trans_flag = 0 + + #历史留言查看判断标志 + self.history_voice_flag = 0#公共变量判断 + #60S等待留言判断标志 + self.wait_voice_flag = 0#公共变量判断 + EventMesh.publish("AI_alarm_open")# + self.server_ip = EventMesh.publish("get_storage_config","server_ip")# + if self.server_ip == None: + if self.VC_server == 1:# + self.server_ip="118.145.158.207:22083" + elif self.VC_server == 2:# + self.server_ip="118.145.158.207:22082" + elif self.VC_server == 4:# + self.server_ip="118.145.158.207:22084" + elif self.VC_server == 5:# + self.server_ip="amp-wss.raisound.com" + elif self.VC_server == 6:# + self.server_ip="118.145.158.207:22080" + elif self.VC_server == 7:# + self.server_ip="118.145.158.207:22080" + elif self.VC_server == 8:# + self.server_ip="115.190.94.208:22080" + elif self.VC_server == 86:# + self.server_ip="118.145.158.207:22086" + else: + #self.server_ip="sevenpin.qipai.com.cn" + self.server_ip="118.145.158.207:22082" + # 默认走 constants.AMP_SERVER(由 default_srv.py 统一管理,可在 usr 覆盖) + try: + if constants.AMP_SERVER: + self.server_ip = constants.AMP_SERVER + except Exception: + pass + + #开机提示音 + self.power_on_tone = EventMesh.publish("get_storage_config","power_on_tone")# + try: + if "vc_config" in globals() and vc_config: + if not vc_config.get_bool("POWER_ON_TONE_ENABLED", True): + self.power_on_tone = None + except Exception: + pass + if self.power_on_tone == None: + try: + if "vc_config" in globals() and vc_config: + self.power_on_tone = vc_config.get_attr("POWER_ON_TONE_DEFAULT", None) + except Exception: + pass + + self.VC_record_TYPE = VC_record_TYPE_PCM #0:PCM 1:AMR + self.VC_play_TYPE = VC_play_TYPE_PCM #0:PCM 1:mp3 + self.VC_record_samplerate = 16000 + self.VC_play_samplerate = 16000 + self.VAD_data_len = 640 #10ms数据 + self.is_VAD = 0#=1打开vad打断 + # ARG_sample_rate, // 采样率 + # ARG_channel, // 通道数 + # ARG_frame_size, // 每帧字节数 + # ARG_energy_threshold, // 能量阈值 + # ARG_voice_time, // 语音持续时间阈值(ms) + # ARG_silence_time, // 静音持续时间阈值(ms) + # ARG_silence_start_threshold, // 静音开始阈值(ms) + self.vad = None + if self.is_VAD == 1: + self.vad = VAD.VAD(self.VC_record_samplerate ,1,self.VAD_data_len,600,100,300,100) + self.speech_rate = EventMesh.publish("get_storage_config","speech_rate")#语速配置 + if self.speech_rate == None: + self.speech_rate = 0 + + self.vad_silence = EventMesh.publish("get_storage_config","vad_silence")#语速配置 + if self.vad_silence == None: + try: + if "vc_config" in globals() and vc_config: + self.vad_silence = vc_config.get_int("VAD_SILENCE_DEFAULT", 300) + else: + self.vad_silence = 300 + except Exception: + self.vad_silence = 300 + + self.VC_TTS_voice_type = EventMesh.publish("get_storage_config","VC_TTS_voice_type")#音色配置 + if self.VC_TTS_voice_type == None : + try: + if "vc_config" in globals() and vc_config: + cfg = vc_config.get_attr("VC_TTS_VOICE_TYPE_DEFAULT", None) + if isinstance(cfg, dict): + self.VC_TTS_voice_type = cfg + except Exception: + pass + # 若配置未提供,沿用原逻辑内置兜底(保持兼容) + if self.VC_TTS_voice_type == None: + self.VC_TTS_voice_type = { + "女":{"bot_name":"柒妹妹","voice":"zh_female_wanwanxiaohe_moon_bigtts","lang":"zh-cn"}, + "男":{"bot_name":"柒先生","voice":"zh_male_wennuanahu_moon_bigtts","lang":"zh-cn"}, + "ç«¥":{"bot_name":"柒乐乐","voice":"ICL_zh_female_huoponvhai_tob","lang":"zh-cn"}, + } + + # self.VC_TTS_voice_type = {0:"zh_female_xinlingjitang_moon_bigtts",1:"zh_male_wennuanahu_moon_bigtts",2:"ICL_zh_female_huoponvhai_tob", + # 3:"aliyun::longjiaxin",4:"zh_female_daimengchuanmei_moon_bigtts"} + #self.VC_TTS_voice_type = ["aliyun::longxiaoxia","aliyun::longnan","aliyun::longling"] + + #EventMesh.publish("set_storage_config",["VC_TTS_voice_type",self.VC_TTS_voice_type]) + + try: + self.VC_TTS_voice_key = EventMesh.publish("get_storage_config","VC_TTS_voice_key")#音色配置 + if self.VC_TTS_voice_key == None : + try: + if "vc_config" in globals() and vc_config: + self.VC_TTS_voice_key = vc_config.get_attr("VC_TTS_VOICE_KEY_DEFAULT", None) + except Exception: + pass + if not self.VC_TTS_voice_key: + self.VC_TTS_voice_key = "男" + if self.VC_TTS_voice_key in self.VC_TTS_voice_type: + pass + else: + self.VC_TTS_voice_key = list(self.VC_TTS_voice_type.keys())[0] + print("音色配置:{}:{}\r\n\r\n".format(self.VC_TTS_voice_key,self.VC_TTS_voice_type[self.VC_TTS_voice_key])) + self.VC_TTS_volume = EventMesh.publish("get_storage_config","VC_TTS_volume")#音量配置 + except Exception as e: + usys.print_exception(e) + #配置词 + self.commands_mode = 2 #=1用大模型判断命令,=0用服务器本地插件判断命令 =2用设备本地判断方式 + self.commands = [] + if self.VC_TTS_volume == None: + self.VC_TTS_volume = 100 + #可ota配置的参数变量 + self.emotion = EventMesh.publish("get_storage_config","emotion")#情绪 + if self.emotion == None: + self.emotion = "" + self.ota_update_notice = EventMesh.publish("get_storage_config","ota_update_notice")#OTA升级提示语 + if self.ota_update_notice == None: + self.ota_update_notice = "" + self.AI_audio_queue_thread_id = 0 + self.AI_audio_queue_thread_lock = 0 + self.AI_VC_send_data_queue_thread_id = 0 + self.AI_VC_send_data_queue_thread_lock = 0 + self.AI_ASR_send_data_queue_thread_id = 0 + self.AI_ASR_send_data_queue_thread_lock = 0 + self.AI_record_tmp_queue = Queue(100) + self.AI_ASR_send_data_queue = Queue(200) + self.AI_VC_send_data_queue = Queue(100) + self.AI_audio_queue = Queue(100) + #self.AI_WS_connect_Queue = Queue(100) #释放用 + self.AI_TTS_str = "" + self.AI_ASR_str = "" + self.AI_ASR_flag = 0 + self.AI_ASR_str_tmp = "" + self.AI_ASR_send_buf_len = 0 + self.AI_ASR_text_tmp = "" + self.AI_ASR_text_repet_count = 0 + # #开始线程 + # if self.AI_audio_queue_thread_id ==0: + # self.AI_audio_queue_thread_id = _thread.start_new_thread(self.AI_audio_queue_thread, ()) + # else: + # print("AI_VC_start :AI_audio_queue_thread_id != 0 ") + # if self.AI_VC_send_data_queue_thread_id == 0: + # self.AI_VC_send_data_queue_thread_id = _thread.start_new_thread(self.AI_VC_send_data_queue_thread, ()) + + self.VC_dialog_id = ""# + self.VC_message_id=None + self.VC_task_id =None + self.AI_client_VC = None + self.AI_client_VC_recv_id = 0 + self.AI_client_VC_recv_lock = 0 + self.AI_client_VC_isconnect =0 + self.AI_client_VC_debug =0 + + self.asr_state = 0 # 预留:ASR 侧状态(当前未参与分支) + self.key_state = 0 + + self.AI_client_asr = None + self.AI_client_asr_recv_id = 0 + self.AI_client_asr_recv_lock = 0 + self.AI_client_asr_isconnect =0 + self.AI_ASR_timer = osTimer() + self.AI_ASR_timer_count = 0 + self.AI_client_asr_debug =0 + self.AI_ASR_check_timeout = 0 + self.AI_ASR_error_timeout = 0 + self.VC_Thinking_timerout = 0 + + + self.record_pcm = None + self.play_pcm = None + self.VC_record_thread_id = 0 + self.VC_record_thread_lock = 0 + self.VC_pingping_timerout = 0 + self.asr_pingping_timerout = 0 + self.factory_timer = osTimer() + self._1S_timer = osTimer() + self.Audio_buf_size = 20*1024 + self.record = audio.Record() + #self.record.end_callback(self.record_callback) + self.record.gain_set(4,10) + #self.record.gain_set(4,12) + self.aud = audio.Audio(0) + self.aud.setCallback(self.audio_cb) + # 确保 record/aud 已创建后再打印硬件/音频诊断信息(需要 gain_get/type(record)) + try: + self._print_hw_diag() + except Exception as e: + usys.print_exception(e) + #self.aud.setVolume(self.VC_TTS_volume //10) + self.audio_play_state = 1 #音频播放完状态 + self.close_timerout = 0 + self._idle_deep_sec = 0 + self._vc_idle_deep_armed_suppress = 0 + self.LocalRespondingEnded_timerout = 0 + + self.VC_Play_flag = 0 + self.play_flag = 0 + self.VC_record_state_flag = 0 + self.AI_key_long_press_flag = 0 + self._asr_completed_tone_done = 0 # asr.completed 本地 think 提示每轮仅一次 + self._last_asr_result_text = "" # 保留最近 asr.result,防 VC_Speech_End 早于 asr.completed 清空 + self._vc_last_auto_uplink_ms = 0 # 长对话自动开麦防抖 + self._vc_local_play_active = 0 # aud.play 本地短音占用(与采集互斥) + self._vc_local_play_until_ms = 0 + self._vc_pending_interrupt_uplink = 0 # 长对话短按打断:等 canceled 再开麦 + self.websocket_error_count = 0 + self.AI_new_voice_url = "" #未读语音地址/音乐播放地址/闹钟播放标志 + self.music_play_flag = 0 #音乐播放标志 + self.alarm_play_flag = 0 #闹钟播放标志 + self.tts_in_data_len = 0 + self.tts_out_data_len = 0 + self._tts_first_write_ms = 0 + self._tts_play_check_log_ms = 0 + self._need_audio_played_ack = 0#tts.completed 后需在本地播报结束后上报 audio.played;勿用 tts_in/out:tts.completed 会再调 VC_Play_Start 把二者清零 + self.VC_satrt_flag = 0 + self.VC_stop_play_audio_flag = 0 + self.VC_satrt_kws_flag = 0 + self.session_id = "" + self.stop_voice_timer=osTimer() + self.pwk_cnt_voice = 0 #不听语音后,积攒的次数,需要一起播放 + self.pwk_notice_flag = 0#是否处于积攒留言状态(用来实现单次提醒) + self.is_noread_voice = EventMesh.publish("get_storage_config","is_noread_voice") + if self.is_noread_voice == None: + self.is_noread_voice = 0#是否还有未读留言(开机判断) + self.is_ruppt_tts = 0#是否已经打断tts播放 + EventMesh.subscribe("AI_key_press", self.AI_key_press) + EventMesh.subscribe("BT_AI_key_press", self.BT_AI_key_press) + EventMesh.subscribe("stop_AI_record", self.stop_AI_record) + EventMesh.subscribe("AI_key_release", self.AI_key_release) + EventMesh.subscribe("pwk_key_double_press", self.pwk_key_double_press) + EventMesh.subscribe("AI_key_long_press", self.AI_key_long_press) + EventMesh.subscribe("pwk_key_release", self.pwk_key_release) + EventMesh.subscribe("get_AI_audio_queue", self.get_AI_audio_queue) + EventMesh.subscribe("get_VC_satrt_flag", self.get_VC_satrt_flag) + EventMesh.subscribe("get_vc_stream_active", self.get_vc_stream_active) + EventMesh.subscribe("get_vc_tts_playback_active", self.get_vc_tts_playback_active) + EventMesh.subscribe("battery_priority_mute_vc", self.battery_priority_mute_vc) + EventMesh.subscribe("VC_error", self.VC_error) + EventMesh.subscribe("audio_test", self.audio_test) + EventMesh.subscribe("audio_test_close", self.audio_test_close) + EventMesh.subscribe("set_record_data_save_flag", self.set_record_data_save_flag) + EventMesh.subscribe("set_VC_TTS_voice_type", self.set_VC_TTS_voice_type) + EventMesh.subscribe("SET_TTS_VOLUME", self.set_tts_volume) + EventMesh.subscribe("set_VC_stop_play_audio_flag", self.set_VC_stop_play_audio_flag) + EventMesh.subscribe("set_yuyinaudio_flag",self.set_yuyinaudio_flag) + EventMesh.subscribe("get_yuyinaudio_flag",self.get_yuyinaudio_flag) + EventMesh.subscribe("set_pwk_cnt_voice",self.set_pwk_cnt_voice) + EventMesh.subscribe("get_pwk_cnt_voice",self.get_pwk_cnt_voice) + EventMesh.subscribe("exit_voice_message_mode",self.exit_voice_message_mode) #超时未退出语音留言模式 + EventMesh.subscribe("come_meet_mode",self.come_meet_mode)#进入会议模式 + EventMesh.subscribe("exit_meet_mode",self.exit_meet_mode)#退出会议模式 + EventMesh.subscribe("come_trans_mode",self.come_trans_mode)#进入翻译模式 + EventMesh.subscribe("exit_trans_mode",self.exit_trans_mode)#退出翻译模式 + EventMesh.subscribe("set_trans_flag",self.set_trans_flag)#设置翻译状态 + EventMesh.subscribe("get_audio_play_state",self.get_audio_play_state) + EventMesh.subscribe("set_voicerupt_flag",self.set_voicerupt_flag)#按键中断触发留言播放 + EventMesh.subscribe("set_wait_voicerupt_flag",self.set_wait_voicerupt_flag)#按键中断触发未读留言播放 + EventMesh.subscribe("get_recv_voice",self.get_recv_voice)#获取是否收到留言 + EventMesh.subscribe("rate_change",self.rate_change)#直接修改语速 + EventMesh.subscribe("get_history_voice_flag",self.get_history_voice_flag)#获取查找历史留言状态 + EventMesh.subscribe("set_history_voice_flag",self.set_history_voice_flag)#修改查找历史留言状态 + EventMesh.subscribe("clean_unread_vocie",self.clean_unread_vocie)#修改查找历史留言状态 + EventMesh.subscribe("get_wait_voice_flag",self.get_wait_voice_flag)#获取60S等待留言播放定时器状态 + EventMesh.subscribe("set_wait_voice_flag",self.set_wait_voice_flag)#修改60S等待留言播放定时器状态 + EventMesh.subscribe("rate_move",self.rate_move)#NLP修改语速 + EventMesh.subscribe("find_rate",self.find_rate)#NLP查询语速 + EventMesh.subscribe("set_pwk_notice_flag",self.set_pwk_notice_flag)#设置未读状态 + # EventMesh.subscribe("get_voicerupt_flag",self.get_voicerupt_flag)#获取留言播放状态 + EventMesh.subscribe("chenggong_tone",self.chenggong_tone) + + pass + EventMesh.publish("set_led_state", {"ai_state": "idle"}) + self.dialog_attributes_provider = EventMesh.publish("get_storage_config","dialog_attributes_provider")#智能体类型配置 + if self.dialog_attributes_provider == None: + self.dialog_attributes_provider = "coze" + self.outbound_attributess_provider = EventMesh.publish("get_storage_config","outbound_attributess_provider")#tts服务选择 + if self.outbound_attributess_provider == None: + self.outbound_attributess_provider = "volcengine" + self.inbound_attributess_provider = EventMesh.publish("get_storage_config","inbound_attributess_provider")#tts服务选择 + if self.inbound_attributess_provider == None: + self.inbound_attributess_provider = "volcengine" + + self.bot_name = "柒先生" + try: + if "bot_name" in self.VC_TTS_voice_type[self.VC_TTS_voice_key]: + self.bot_name = self.VC_TTS_voice_type[self.VC_TTS_voice_key]["bot_name"] + else: + print("not bot_name!!!!!!!!!!!!!!!!!!!!!!") + except Exception as e: + usys.print_exception(e) + try: + bt_ota_flag=0 + try: + if "vc_config" in globals() and vc_config and vc_config.get_bool("BT_OTA_ENABLED", False): + bt_ota_flag = self.ai_OTA.bt_ota()#先检查蓝牙是否要升级 + except Exception: + pass + + cat1_ota_flag = self.ai_OTA.min_sys_ota() + if bt_ota_flag==1 or cat1_ota_flag == 1: + #if 0: + if self.ota_update_notice != "": + self.AI_TTS_str = self.ota_update_notice + else: + self.AI_TTS_str = "检测到有新版本需要升级啦!升级时可千万别断电或乱按哦,升级完成后设备会自动重启,重启就代表升级成功" + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + self.Close_flag = 1 + # if self.ai_OTA.min_sys_ota()==2: + # self.AI_TTS_str = "检测到有新版本需要升级啦!电量低于40%,无法保证升级正常执行,请充满电后重启设备再次升级!" + # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + else: + try: + self.ai_OTA.ota_result() + self.ai_OTA.pots_ICCID() + except Exception as e: + usys.print_exception(e) + #开机提示语,如果联网失败,直接提示信号弱 + + + #EventMesh.publish("get_storage_config",["powerRestart",1]) + powerRestart = EventMesh.publish("get_storage_config","powerRestart")#=1非正常重启 + if powerRestart == None: + powerRestart = 0 + if powerRestart == 1: + EventMesh.publish("set_storage_config",["powerRestart",0]) + + else: + pass + self.AI_TTS_str = self.power_on_tone.replace("bot_name", self.bot_name) + print("开机提示:{}".format(self.AI_TTS_str)) + try: + self._pending_tts_kind = "boot_prompt" + self._last_tts_kind = "boot_prompt" + self._skip_started_uplink_once = 1 + except Exception: + pass + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + + except Exception as e: + usys.print_exception(e) + def set_VC_stop_play_audio_flag(self,event = None, msg = None): + self.VC_stop_play_audio_flag = msg + print("self.VC_stop_play_audio_flag:{}".format(self.VC_stop_play_audio_flag)) + + def rate_change(self,event,msg):#直接设置语速(数字) + self.speech_rate = msg + EventMesh.publish("set_storage_config",["speech_rate",self.speech_rate]) + print("set_speech_rate:{}".format(self.speech_rate)) + def rate_move(self,event,msg):#设置语速(模糊(å¿«/慢一点)) + if msg == 1 and self.speech_rate<50:#增大 + self.speech_rate += 10 + else:#减小 + if self.speech_rate >-50: + self.speech_rate -= 10 + EventMesh.publish("set_storage_config",["speech_rate",self.speech_rate]) + print("set_speech_rate:{}".format(self.speech_rate)) + def find_rate(self,event,msg):#查询APP语速 + print("查询语速处理:原始数据为:{}".format(self.speech_rate)) + y = 1 + if self.speech_rate >= -50 and self.speech_rate < 0:#负数 + y = 1 - (-0.01 * self.speech_rate) + elif self.speech_rate <= 50 and self.speech_rate >= 0:#正数 + y = 0.02 * self.speech_rate + 1.0 + print("处理后数据为{}".format(y)) + rate = str(y) + "倍" + return rate + def set_pwk_notice_flag(self,event,msg):#设置未读状态 + self.pwk_notice_flag = msg + #获取60S等待留言播放定时器状态 + def get_wait_voice_flag(self,event,msg): + return self.wait_voice_flag + def set_wait_voice_flag(self,event,msg): + # print("未读留言标记修改为:{}".format(msg)) + self.wait_voice_flag = msg + + #查看历史留言状态修改 + def get_history_voice_flag(self,event,msg): + return self.history_voice_flag + def set_history_voice_flag(self,event,msg): + # print("历史留言标记修改为:{}".format(msg)) + self.history_voice_flag = msg + def clean_unread_vocie(self,event,msg): + self.pwk_cnt_voice = 0 + self.pwk_notice_flag = 0 + EventMesh.publish("set_app_new_voice_flag",0) + def audio_test(self,event = None, msg = None): + self._uplink_request_start("audio_test") + self.VC_Play_Start() + def audio_test_close(self,event = None, msg = None): + self._uplink_request_stop("audio_test_close") + self.VC_Play_End() + if self.record_data_save_flag == 1: + if self.record_fd : + self.record_fd.close() + self.record_fd = None + def set_record_data_save_flag(self,event = None, msg = None): + self.record_data_save_flag = msg + def post_processor_after_instantiation(self): + pass + def get_AI_audio_queue(self, event, msg): + return self.AI_audio_queue + def get_VC_satrt_flag(self, event, msg): + return self.VC_satrt_flag + #检查有没有意外打开ai部分功能 + def check_vc_start(self): + print("check_vc_start:{} {} {} {}".format(self.VC_satrt_flag,self.VC_record_state_flag,self.AI_client_VC_isconnect,self.VC_Play_flag)) + if self.VC_satrt_flag == 0: + if self.VC_record_state_flag != 0 or self.AI_client_VC_isconnect == 1 or self.VC_Play_flag == 1: + self.Close() + def VC_30S_timer_cb(self, *args): # + EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_30S_timer_cb_}) + + def VC_30S_timer_cb_(self): + self.get_config_timer_count += 1 + if self.get_config_timer_count >= 2*60*2:#2小时更新配置参数 + self.get_config_timer_count = 0 + if self.http_get_config_flag == 1: + try: + self.http_get_config()#获取/更新配置参数 + except Exception as e: + usys.print_exception(e) + self.check_vc_start()#30秒检查一下有没有关闭 + #提交错误id + def ud_error(self,error_data): + response = None + if self.is_up_error == 1: + try: + # currentVersion = versions.Build_Time + # currentVersion = "7PIN-"+currentVersion[:10]+"-"+currentVersion[11:-3] + url = "http://{}/health?task_id={}".format(self.server_ip,error_data[0]+error_data[1]) + print(url) + response = request.get(url,timeout=5) + if response.status_code == 200: + + print("ud_error:ok") + else: + print("ud_error error!!!!") + for i in response.text: + print(i) + response.close() + except Exception as e: + usys.print_exception(e) + if response != None: + response.close() + #获取配置参数 + def http_get_config(self, event=None, msg=None): + print("http_get_config") + + response = None + #http://180.184.206.142/bz-api/dev/config?deviceId=xxx + try: + url = (constants.DMP_BASE + "/dev/config/{}").format(modem.getDevImei()) + + + print(url) + response = request.get(url,timeout=5) + if response.status_code == 200: + data = response.json() + print("http_get_config:{}".format(data)) + + if data: + try: + if data["code"] == 20000:#有配置 + if data["data"] : + if "version" in data["data"]: + config_version = EventMesh.publish("get_storage_config","config_version")#音量配置 + if config_version != data["data"]["version"]: + config_version = data["data"]["version"] + EventMesh.publish("set_storage_config",["config_version",config_version]) + if "VC_TTS_voice_type" in data["data"]: + self.VC_TTS_voice_type = data["data"]["VC_TTS_voice_type"] + EventMesh.publish("set_storage_config",["VC_TTS_voice_type",self.VC_TTS_voice_type]) + print("配置VC_TTS_voice_type成功") + if "server_ip" in data["data"]: + self.server_ip = data["data"]["server_ip"] + EventMesh.publish("set_storage_config",["server_ip",self.server_ip]) + print("配置server_ip成功") + if "power_on_tone" in data["data"]: + self.power_on_tone = data["data"]["power_on_tone"] + EventMesh.publish("set_storage_config",["power_on_tone",self.power_on_tone]) + print("配置power_on_tone成功") + if "dialog_attributes_provider" in data["data"]: + self.dialog_attributes_provider = data["data"]["dialog_attributes_provider"] + EventMesh.publish("set_storage_config",["dialog_attributes_provider",self.dialog_attributes_provider]) + if "outbound_attributess_provider" in data["data"]: + self.outbound_attributess_provider = data["data"]["outbound_attributess_provider"] + EventMesh.publish("set_storage_config",["outbound_attributess_provider",self.outbound_attributess_provider]) + print("配置outbound_attributess_provider成功") + if "inbound_attributess_provider" in data["data"]: + self.inbound_attributess_provider = data["data"]["inbound_attributess_provider"] + EventMesh.publish("set_storage_config",["inbound_attributess_provider",self.inbound_attributess_provider]) + print("配置inbound_attributess_provider成功") + if "speech_rate" in data["data"]: + self.speech_rate = EventMesh.publish("SET_SPEECH_RATE",float(data["data"]["speech_rate"])) + EventMesh.publish("set_storage_config",["speech_rate",self.speech_rate]) + print("配置speech_rate成功") + if "emotion" in data["data"]: + self.emotion = data["data"]["emotion"] + EventMesh.publish("set_storage_config",["emotion",self.emotion]) + print("配置emotion成功") + if "ota_update_notice" in data["data"]: + self.ota_update_notice =data["data"]["ota_update_notice"] + EventMesh.publish("set_storage_config",["ota_update_notice",self.ota_update_notice]) + print("配置ota_update_notice成功") + if "VC_TTS_volume" in data["data"]: + self.VC_TTS_volume =int(data["data"]["VC_TTS_volume"]) + EventMesh.publish("set_storage_config",["VC_TTS_volume",self.VC_TTS_volume]) + print("配置VC_TTS_volume成功") + + except Exception as e: + usys.print_exception(e) + else: + print("status_code:{}".format(response.status_code)) + print("http_get_config:error!!!!!!!!!!") + response.close() + except Exception as e: + usys.print_exception(e) + if response != None: + response.close() + + def audio_cb(self,event): + if event == 0: + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + self.audio_play_state =1 + #EventMesh.publish("set_audio_state", 1) + print('audio-play start.') + # 本地 aud.play(开录/思考短音等)不改动 AI 灯,仅 TTS 流由 VC_Play_Start 控绿灯 + if self._vc_led_state_debug_enabled(): + print("[LED] audio_cb start skip (local_play)") + return + elif event == 7: + self.audio_play_state = 0 + #EventMesh.publish("set_audio_state", 0) + print('audio-play finish.!!!!!!') + try: + if getattr(self, "VC_mode", -1) == 1 and getattr(self, "_ptt_await_server", 0) == 1: + self._vc_publish_led("thinking", "audio_cb/finish/ptt_await") + return + except Exception: + pass + try: + if getattr(self, "VC_record_state_flag", 0) != 0: + self._vc_publish_led("listening", "audio_cb/finish/recording") + return + except Exception: + pass + try: + if self._vc_tts_playback_active(): + if self._vc_led_state_debug_enabled(): + print("[LED] audio_cb finish skip (tts_playback)") + return + except Exception: + pass + try: + if getattr(self, "_ptt_led_lock_listening", 0) == 1: + return + except Exception: + pass + self._vc_led_sync_from_fsm() + def set_tts_volume(self,event,msg): + EventMesh.publish("set_storage_config",["VC_TTS_volume",msg])#音量配置 + self.VC_TTS_volume = msg + print("set_tts_volume:{} {}".format(self.VC_TTS_volume,msg)) + if int(msg) <= 10: #最小一定要设置1要不然没有声音 + volume = 10 //10 + self.aud.setVolume(volume) + else: + self.aud.setVolume(self.VC_TTS_volume //10) + pass + def get_yuyinaudio_flag(self, event=None, msg = None): + return self.voice_flag + + def set_yuyinaudio_flag(self, event=None, msg = None): + self.voice_flag=0 + + #留言主动拒听 + def get_pwk_cnt_voice(self, event=None, msg = None): + return self.pwk_cnt_voice + + def set_pwk_cnt_voice(self, event=None, msg = None): + self.pwk_cnt_voice=msg + print("设置未读次数为:{}".format(self.pwk_cnt_voice)) + + #会议 + def get_meetaudio_flag(self, event=None, msg = None): + return self.meet_flag + def set_meetaudio_flag(self, event=None, msg = None): + self.meet_flag = 0 + + def get_audio_play_state(self, event=None, msg = None): + return self.audio_play_state + + + + def come_meet_mode(self, event=None, msg = None): + if self.meet_flag == 0: #还是为0就提示进入会议纪要模式 + EventMesh.publish("send_meet_open_flag_to_iot")#创建会议 + self.meet_flag=1 + exit_tts ="开启会议纪要,正在录音" + EventMesh.publish("play_tts",[exit_tts,1]) + utime.sleep_ms(4000) + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_key_press}) + pass + def exit_meet_mode(self, event=None, msg = None): + if self.meet_flag == 1: #还是为1就提示已退出会议纪要模式 + EventMesh.publish("send_meet_end_flag_to_iot")#结束会议 + self.meet_flag=0 + EventMesh.publish("stop_voice_record") #关闭语音录音 + exit_tts ="录音结束,已退出会议纪要模式" + EventMesh.publish("play_tts",[exit_tts,1]) + pass + def come_trans_mode(self, event=None, msg = None): + if self.trans_flag == 0: #还是为0就提示进入AI翻译模式 + EventMesh.publish("send_trans_come_flag_to_iot")#进入翻译 + pass + def exit_trans_mode(self, event=None, msg = None): + if self.trans_flag == 1: #还是为1就提示已退出AI翻译模式 + EventMesh.publish("stop_voice_record") #关闭语音录音 + exit_tts ="已退出AI翻译模式" + EventMesh.publish("play_tts",[exit_tts,1]) + self.trans_flag=0 + EventMesh.publish("send_trans_end_flag_to_iot")#退出翻译 + pass + def set_trans_flag(self,event,msg): + self.trans_flag = msg + def exit_voice_message_mode(self, event=None, msg = None): + if self.voice_flag == 1: #还是为1就提示已退出会议纪要模式 + self.voice_flag=0 + exit_tts ="已退出语音留言模式" + EventMesh.publish("play_tts",[exit_tts,1]) + pass + def ai_key_press_audio_cb1(self, event=None, msg = None): + print("播放按键音!!!!!!!!!!") + # EventMesh.publish("audio_montage_play",["chenggong.mp3"]) + # self.chenggong_tone() + self.aud.aud_tone_play(10,400)#播放按键音 + def ai_key_press_audio_cb2(self, event=None, msg = None): + EventMesh.publish("audio_montage_play",["chenggong.mp3"]) + # self.chenggong_tone() #播放按键音 + + def AI_key_press(self, event=None, msg = None):#按键按下 + # 按键时打印 LED 聚合状态/最终模式(用于定位“绿灯不灭/闪一下”) + try: + if "vc_config" in globals() and vc_config and vc_config.get_bool("KEY_LED_DEBUG", False): + try: + led = EventMesh.publish("get_led_debug_state") + except Exception: + led = None + try: + t = getattr(self.fsm, "turn", None) + except Exception: + t = None + print("[KEY] AI_key_press VC_mode={} {} turn={} led={}".format( + getattr(self, "VC_mode", -1), self._vc_fsm_log_snip(), t, led)) + except Exception: + pass + # 单对话 PTT / 长对话分支(按键短按提示音已关,避免与开录等提示音叠在一起) + try: + if self._ai_key_press_ptt_mode(): + return + except Exception: + pass + if self._ai_key_press_long_dialogue_interrupt(): + return + try: + if self._ai_key_press_long_dialogue_when_session_ready(): + return + except Exception: + pass + try: + if self._ai_key_press_long_dialogue_wake(): + EventMesh.publish(constants.EVENT.SYSTEM_UP) + return + except Exception: + pass + + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + if EventMesh.publish("get_sos_flag")== 1: + return + if self.voice_flag == 1: #语音留言模式 + self.stop_voice_timer.stop() #关闭语音留言模式定时器 + # EventMesh.publish("stop_AI_record") + self.Close() + self.revoice_cnt = 0 + self.revoice_timer.stop()#回复定时器关闭,计数器清零 + EventMesh.publish("start_voice_record",2)#开始语音录音 + return + elif self.meet_flag == 1:#会议纪要模式 + self.Close() + EventMesh.publish("start_voice_record",3)#开始会议录音 + return + elif self.trans_flag == 1:#AI翻译模式 + return + + EventMesh.publish("stop_voice_record") #关闭语音录音 + print("AI_key_press: unhandled mode branch") + self.AI_key_long_press_flag = 0 + def BT_AI_key_press(self, event=None, msg = None):#按键按下 + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + if EventMesh.publish("get_sos_flag")== 1: + return + if self.voice_flag == 1: #语音留言模式 + tts_str = "语音留言模式,长按按钮开始录音,松开按钮结束" + EventMesh.publish("play_tts",[tts_str,1]) + return + + EventMesh.publish("stop_voice_record") #开启静默需要关闭 + print("AI_key_press") + self.get_config_timer_count = 0 + self.key_state = 1 + # EventMesh.publish("audio_montage_play",["chenggong.mp3"]) + # self.chenggong_tone() #播放按键音 + # 长对话:蓝牙按键同单按 + # - 对话进行中:打断/续聊(复用 dialog_id) + # - 已停止/未 started:走新会话流程 + try: + if self.VC_mode == 0: + if getattr(self, "AI_client_VC_isconnect", 0) == 1 and getattr(self, "_vc_session_started", 0) == 1: + EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_client_VC_RequestToSpeak}) + return + except Exception: + pass + + self.Close() + + if self.VC_mode == 1: #长按对话模式 接收到蓝牙耳机按键事件 + + tts_str = "请您按住圆形的按钮开始讲话,讲完松开即可 " + EventMesh.publish("play_tts",[tts_str,1]) + return + # if dev_type == "7PIN": + # self.chenggong_tone() + #self.aud.aud_tone_play(10,200)#播放按键音 + self.AI_TTS_str = ""#"你好,有什么吩咐" + self.AI_ASR_str = "" + self.AI_ASR_str_tmp = "" + # if self.VC_state != VC_State_None: + # EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) + # EventMesh.publish("publish_AI_btnInfo",{"func":utime.sleep_ms,'data':(10)}) + self._uplink_request_stop("BT_AI_key_press/compat2") + self._uplink_request_start("BT_AI_key_press/compat2") + + self.AI_VC_start() + + + # self.Close() + # self.AI_VC_start() + self.AI_key_long_press_flag = 0 + def stop_AI_record(self, event=None, msg=None):#停止AI对话 + if self.VC_satrt_flag == 1: + self.get_config_timer_count = 0 + self.AI_TTS_str = ""#"你好,有什么吩咐" + self.AI_ASR_str = "" + self.AI_ASR_str_tmp = "" + self.AI_key_long_press_flag = 0 + EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) + self.request_uplink_stop("stop_AI_record") + count = 0 + while True: + if count >= 20 or self.VC_satrt_flag == 0: + break + count+=1 + utime.sleep_ms(500) + + def exit_voice_message_timer(self,*argc): + EventMesh.publish("exit_voice_message_mode") + + def AI_key_release(self, event=None, msg=None):#按键弹起 + try: + if "vc_config" in globals() and vc_config and vc_config.get_bool("KEY_LED_DEBUG", False): + try: + led = EventMesh.publish("get_led_debug_state") + except Exception: + led = None + try: + t = getattr(self.fsm, "turn", None) + except Exception: + t = None + print("[KEY] AI_key_release VC_mode={} {} turn={} led={}".format( + getattr(self, "VC_mode", -1), self._vc_fsm_log_snip(), t, led)) + except Exception: + pass + print("AI_key_release123") + self.key_state = 0 + if self.trans_flag == 1:#AI翻译模式 + EventMesh.publish("stop_voice_record") #关闭语音录音 + return + if self.voice_flag == 1: + self.stop_voice_timer.stop() + self.voice_flag=0 + if EventMesh.publish("GET_USB_STATE") == 0: + EventMesh.publish("set_led_state", {"ai_state": "idle"}) + EventMesh.publish("stop_voice_record")#停止静默录音 + tts_play_str = "留言已发送" + self.voice_flag=0 + EventMesh.publish("play_tts",[tts_play_str,1]) + return + if self.VC_mode == 1: + self._ai_key_release_handle_ptt() + return + def pwk_key_double_press(self,event,msg):#pwk双击 + print("come 双击") + # 切换模式:必须打断当前对话/录音/播放,回到 idle,等待按键触发新一轮 + try: + self.Close_flag = 1 + except Exception: + pass + try: + EventMesh.publish("publish_AI_btnInfo", {"func": self.Close}) + except Exception: + pass + if self.VC_mode == 0: + self.VC_mode = 1 + tts_str = "已切换到单对话模式" + # 下一次“无 dialog_id 的提示音 TTS”归类为模式切换提示(单对话) + try: + self._pending_tts_kind = "mode_switch_ptt" + self._last_tts_kind = "mode_switch_ptt" + except Exception: + pass + else: + self.VC_mode = 0 + tts_str = "已切换到长对话模式" + # 下一次“无 dialog_id 的提示音 TTS”归类为模式切换提示(长对话) + try: + self._pending_tts_kind = "mode_switch_long" + self._last_tts_kind = "mode_switch_long" + except Exception: + pass + # 提示音播完即退出(Close_flag=1),保持 idle,等待按键 + EventMesh.publish("play_tts",[tts_str,1]) + EventMesh.publish("set_storage_config",["VC_mode",self.VC_mode]) + + def AI_key_long_press(self, event, msg):#按键长按 + try: + if "vc_config" in globals() and vc_config and vc_config.get_bool("KEY_LED_DEBUG", False): + try: + led = EventMesh.publish("get_led_debug_state") + except Exception: + led = None + try: + t = getattr(self.fsm, "turn", None) + except Exception: + t = None + print("[KEY] AI_key_long_press VC_mode={} {} turn={} led={}".format( + getattr(self, "VC_mode", -1), self._vc_fsm_log_snip(), t, led)) + except Exception: + pass + print("AI_key_long_press") + self._ai_key_long_press_route(event, msg) + + def pwk_key_release(self, event, msg):#电源键短按,关闭 + try: + if "vc_config" in globals() and vc_config: + _m = bool(vc_config.get_bool("SINGLE_POWER_KEY_AI_MERGED", False)) + if not _m: + _ai = vc_config.get_attr("EXTINT_AI_GPIO", "GPIO22") + _m = _ai is None or _ai == "" + else: + _m = False + except Exception: + _m = False + if _m: + self._handle_pwk_short_release(event, msg, merged_ai_tail=True) + return + self._handle_pwk_short_release(event, msg, merged_ai_tail=False) + + + def key_prompt(self): + EventMesh.publish("audio_montage_play",["key_prompt.mp3"]) + #self.aud.play(1,1,'U:/static/key_prompt.mp3')#提示请您按住中间的按钮开始讲话,讲完松开即可 + def VC_error(self, event, msg):#错误播报 + self.websocket_error() + #EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error}) + def set_VC_record_state_flag(self,data): + + self.VC_record_state_flag = data + print("[VC] record_state_flag={}".format(self.VC_record_state_flag)) + #开始录音,不缓存,直接发 + def VC_Speech_Start_now(self): + # 新一轮开始录音:允许上行(上一轮可能已置 silence 闸门) + self._server_vad_silence = 0 + self.VC_Speech_Start() + if self.VC_record_state_flag == 1: + self.VC_record_state_flag =2 + print("[VC] Speech_Start_now record_state={}".format(self.VC_record_state_flag)) + #开始录音,先缓存到buf,等服务器响应再发 + def VC_Speech_Start(self): + try: + + #self.VC_state = VC_State_SpeechStart + print("[VC] Speech_Start record_state={} AI_ASR_flag={}".format(self.VC_record_state_flag,self.AI_ASR_flag)) + if self.AI_ASR_flag != 0: + return + + if self.VC_record_state_flag == 0: + # 新一轮开始录音:允许上行(上一轮可能已置 silence 闸门) + self._server_vad_silence = 0 + self.AI_ASR_str = "" + self.AI_ASR_str_tmp = "" + self._last_asr_result_text = "" + try: + self._asr_completed_tone_done = 0 + except Exception: + pass + + self.VC_record_thread_close()#关闭录音线程 + _thread.stack_size(self.Audio_buf_size+8*1024) + self.VC_record_thread_id = _thread.start_new_thread(self.VC_record_thread,()) + _thread.stack_size(8*1024) + print("self.VC_record_thread_id:{}".format(self.VC_record_thread_id)) + try: + + if self.VC_record_TYPE == VC_record_TYPE_AMR : + try : + self.record.stream_start(audio.Record.AMRNB, self.VC_record_samplerate, 0) + except Exception as e: + usys.print_exception(e) + elif self.VC_record_TYPE == VC_record_TYPE_PCM: + print("record_pcm.stream_read_start()!!!!!!!!!!!!") + self.record.stream_start(audio.Record.WAV, self.VC_record_samplerate, 0) + # if self.record_pcm != None: + # try: + # self.record_pcm.stream_stop() + # self.record_pcm.close() + # self.record_pcm = None + # except Exception as e: + # usys.print_exception(e) + # if self.record_pcm == None: + # ret = self.record_pcm = audio.Audio.PCM(0,1,16000,0,1,2) + # print("audio.Audio.PCM:ret:{}".format(ret)) + # ret = self.record_pcm.stream_read_start() + # print("self.record_pcm.stream_read_start:ret:{}".format(ret)) + except Exception as e: + usys.print_exception(e) + + self.VC_record_state_flag = 1 + # 录音硬件已起:协议侧应对应 audio.create(PTT)或长对话直传 PCM + try: + print("[VC][proto] record_start sr={} pcm_type={} VC_mode={} session_started={} " + "ptt_audio_created={} thread_id={}".format( + self.VC_record_samplerate, self.VC_record_TYPE, self.VC_mode, + getattr(self, "_vc_session_started", -1), + getattr(self, "_ptt_audio_created", -1), + self.VC_record_thread_id)) + except Exception: + pass + # 录音开始:聆听灯(PTT 蓝快闪 / 长对话蓝慢闪) + try: + self._vc_publish_led("listening", "VC_Speech_Start") + except Exception: + pass + try: + if getattr(self, "VC_mode", -1) == 1: + self._ptt_on_record_hardware_started() + except Exception: + pass + + except Exception as e: + self.websocket_error() + self.VC_record_state_flag = 1 + self.VC_Speech_End()#关闭录音 + usys.print_exception(e) + #停止录音 + def VC_Speech_End(self): + try: + self._uplink_mark_end("VC_Speech_End") + self.AI_ASR_flag = 0 + self.AI_ASR_str_tmp = "" + # 勿在此清空 AI_ASR_str:vad.silence 停麦常早于 asr.completed,清空会导致 check_text:len:0 连播 think 音 + #EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_asr}) + + if self.VC_record_state_flag == 0: + return + #self.AI_client_asr_StopTranscription_requset()#请求停止 + if self.VC_record_TYPE == VC_record_TYPE_AMR: + self.record.stream_stop() + self.record.stop()#关闭录音 + Record_read_buf = bytearray(self.Audio_buf_size) + for i in range(10): + Record_len = self.record.stream_read(Record_read_buf, self.Audio_buf_size) + # if VC_AUDIO_FRAME_LOG: + # print("Record_len:!!!!!!{}".format(Record_len)) + if Record_len <= 0: + break + elif self.VC_record_TYPE == VC_record_TYPE_PCM: + self.record.stream_stop() + self.record.stop()#关闭录音 + Record_read_buf = bytearray(self.Audio_buf_size) + for i in range(10): + Record_len = self.record.stream_read(Record_read_buf, self.Audio_buf_size) + # if VC_AUDIO_FRAME_LOG: + # print("Record_len:!!!!!!{}".format(Record_len)) + if Record_len <= 0: + break + # if self.record_pcm != None: + # try: + # self.record_pcm.stream_stop() + # self.record_pcm.close() + # self.record_pcm = None + # except Exception as e: + # usys.print_exception(e) + except Exception as e: + usys.print_exception(e) + self.VC_record_thread_close()#关闭录音线程 + if self.AI_record_tmp_queue.empty() != True: + self.AI_record_tmp_queue.clear() + + self.VC_record_state_flag = 0 + # PTT 一轮录音结束:清长按语义,避免后续短按松手误走 end_speech 而不播提示音 + try: + if getattr(self, "VC_mode", -1) == 1: + self.AI_key_long_press_flag = 0 + except Exception: + pass + # 录音停止:单对话进入思考态(等服务器),勿继续蓝闪聆听 + try: + if getattr(self, "VC_mode", -1) == 1: + self._ptt_await_server = 1 + self._vc_publish_led("thinking", "VC_Speech_End/ptt") + else: + self._ptt_await_server = 0 + self._vc_publish_led("idle", "VC_Speech_End") + except Exception: + pass + print("[VC] Speech_End record_state={}".format(self.VC_record_state_flag)) + #关闭录音线程 + def VC_record_thread_close(self): + if self.VC_record_thread_id != 0: + count =50 + while True: + count -= 1 + if self.VC_record_thread_lock == 1: + self.VC_record_thread_lock = 2 + if self.VC_record_thread_lock == 0 or count <=0: + break + else: + print("VC_record_thread_thread going:{}...".format(count)) + utime.sleep_ms(1) + self.VC_record_thread_lock = 0 + print("VC_record_thread_thread_close") + if self.VC_record_thread_id != 0: + try: + _thread.stop_thread(self.VC_record_thread_id) + except Exception as e: + usys.print_exception(e) + self.VC_record_thread_id = 0 + #获取录音数据 + + def VC_record_thread(self): + ##return + print("[VC] record_thread start") + count = 0 + RequestToSpeak_flag = 0 + # try: + # uos.remove('/usr/test.pcm') + # except Exception as e: + # usys.print_exception(e) + #record_data_queue = Queue(100) + #fd = open('/usr/test.pcm','w') + # if self.VC_record_TYPE == 1: + # h_data= [0x23,0x21 ,0x41 ,0x4D ,0x52 ,0x0A ] + # h_data_b = bytes(h_data) + # self.AI_client_VC_send(h_data_b) + AI_record_tmp_queue_len = 0 + vad_buf = bytearray() + VAD_data_count = 0 + vad_state = 0 + self.VAD_count = 0 + while True: + try: + if self.VC_record_thread_lock == 2: + break + self.VC_record_thread_lock = 1 + # 死循环接收数据 + #if self.VC_state == VC_State_SpeechStart: + Record_read_buf = bytearray(self.Audio_buf_size) + + Record_len = 0 + if self.VC_record_TYPE == VC_record_TYPE_PCM:#pcm + Record_len = self.record.stream_read(Record_read_buf, self.Audio_buf_size) + #utime.sleep_ms(60) + + else : + + Record_len = self.record.stream_read(Record_read_buf, self.Audio_buf_size) + utime.sleep_ms(600) + + #Record_len = 1024*2 + #print("VC_record_thread:{}".format(Record_len)) + if Record_len > 0: + read_buf = bytes(Record_read_buf[:Record_len]) + # if VC_AUDIO_FRAME_LOG: + # print("Record_len:{}".format(Record_len)) + if self.record_data_save_flag == 1: + if self.record_fd == None: + try: + uos.remove('/usr/record_data.pcm') + except Exception as e: + pass + # 二进制写入,避免文本模式导致数据异常 + self.record_fd = open('/usr/record_data.pcm', 'wb') + self.record_fd_len = 0 + if self.record_fd != None: + try: + self.record_fd.write(read_buf) + print("write record:{}".format(len(read_buf))) + self.record_fd_len = self.record_fd_len+len(read_buf) + if self.record_fd_len >=500*1024: + EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) + except Exception as e: + usys.print_exception(e) + EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) + # count+=Record_len + # if count >150*1024: + # self.record_fd.close() + #self.record_fd = None + #VAD检测 + if self.is_VAD == 1: + vad_state = 0 + tmp_c = 1000 + while True: + tmp_c -=1 + if tmp_c <=0: + break + #vad_buf_len = len(vad_buf) + if VAD_data_count == 0:#没数据的时候 + vad_buf = Record_read_buf[:self.VAD_data_len] + VAD_data_count = self.VAD_data_len + self.vad.custom_vad_reset()#重置vad + else:#有数据的时候 + if Record_len - VAD_data_count < self.VAD_data_len :#剩余长度不够一次填充,先保留,但不填算法 + vad_buf = Record_read_buf[VAD_data_count:Record_len] + VAD_data_count = 0 + break + else:#剩余长度够填一次数据 + vad_buf = Record_read_buf[VAD_data_count:VAD_data_count+self.VAD_data_len] + VAD_data_count += len(vad_buf) + #print("vad_buf_len:{},{}".format(len(vad_buf),VAD_data_count)) + + if len(vad_buf) >= self.VAD_data_len: + vad_state = self.vad.custom_vad_process(vad_buf,len(vad_buf))#给vad数据 + #print("vad_state:{}".format(vad_state)) + + if VAD_data_count >= Record_len: + VAD_data_count = 0 + break + if vad_state == 1: + print("检测到VAD=1!!!!!!!!!!!!!") + + # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_RequestToSpeak})#打断 + # RequestToSpeak_flag = 1 + + self.VAD_count+=1 + if self.VAD_count >= 20: + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_RequestToSpeak})#打断 + + else: + if self.VAD_count != 0: + EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Play_Pause,"data":0})#恢复音频播放 + self.VAD_count = 0 + if self._vc_tts_pipeline_busy(): + pass + if vad_state == 1: + self.AI_record_tmp_queue.put(read_buf)#入缓存队列 + AI_record_tmp_queue_len += len(read_buf) + + + else: + VAD_data_count = 0 + if self.VC_record_state_flag <=1 :#没发送前先存起来 + self.AI_record_tmp_queue.put(read_buf) + AI_record_tmp_queue_len += len(read_buf) + else: + AI_record_tmp_queue_len = 0 + while True: + if self.AI_record_tmp_queue.empty() == True: + break + else: + pass + # 协议门控:会话未 started 前禁止上行音频(否则 session not initialized) + if VC_PROTO_GUARDS and self._vc_session_started != 1 and self.record_data_save_flag == 0: + _ = self.AI_record_tmp_queue.get() + AI_record_tmp_queue_len = max(0, AI_record_tmp_queue_len - len(_)) + continue + if not (self._server_vad_silence == 1 and self.record_data_save_flag == 0): + self.AI_client_VC_send(self.AI_record_tmp_queue.get()) #发送之前存的数据 + else: + self._uplink_mark_end("silence_gate(drop_queue)") + _ = self.AI_record_tmp_queue.get() + # 当前帧发送前协议门控(可用 VC_PROTO_GUARDS 临时关闭): + # - session started 前禁止发帧 + # - Push-To-Talk 在 audio.create 前禁止发帧 + if VC_PROTO_GUARDS and ( + (self._vc_session_started != 1 and self.record_data_save_flag == 0) or + (self.VC_mode == 1 and self._ptt_audio_created != 1 and self.record_data_save_flag == 0) + ): + # 未 ready:转入缓存,等待 started 再发(并做容量控制) + try: + self.AI_record_tmp_queue.put(read_buf) + AI_record_tmp_queue_len += len(read_buf) + while AI_record_tmp_queue_len > 128*1024 and self.AI_record_tmp_queue.empty() != True: + d = self.AI_record_tmp_queue.get() + AI_record_tmp_queue_len = max(0, AI_record_tmp_queue_len - len(d)) + except Exception: + pass + else: + if not (self._server_vad_silence == 1 and self.record_data_save_flag == 0): + self.AI_client_VC_send(read_buf) + else: + self._uplink_mark_end("silence_gate(drop_buf)") + + + + + + + #print("VC_record_thread:{}".format(len(read_buf))) + #count+=len(read_buf) + # if count>=500*1024:#结束 + # self.AI_client_VC_end_speech() + # break + # count+=1 + # if count %10 == 0: + # print("VC_record_thread on line {} asr recv :{} send:{}".format(self.VC_record_state_flag,_thread.threadIsRunning(self.AI_client_asr_recv_id),_thread.threadIsRunning(self.AI_VC_send_data_queue_thread_id))) + + # if self.VC_state >= VC_State_SpeechEnd: + # break + + except Exception as e: + usys.print_exception(e) + #break + self.VC_record_thread_lock = 0 + if self.record_data_save_flag == 1: + utime.sleep_ms(1) + else: + utime.sleep_ms(100) + #fd.close() + print("VC_record_thread:exit") + self.VC_record_thread_id = 0#线程退出 + self.VC_record_thread_lock = 0 +###################################################### VC ################################################################################### + def websocket_connect_VC(self): + print("websocket_connect_VC:{} e:{}!!!!!!!!!!!!!!!!!!!!!".format(self.AI_client_VC_isconnect,self.AI_client_VC_debug)) + if self.AI_client_VC_isconnect == 1: + return + # 创建VC客户端 + try: + signal = net.csqQueryPoll() + if signal == -1 or sim.getStatus() != 1:#无蜂窝/无卡等:弱网提示 + self.websocket_error(play_weak_signal_tone=True) + return + + req_str = "" + ack = "" + if self.is_VC_ACK == 1: + ack = "ack=1" + lang = "lang=zh-cn"#普通话测试环境 + try: + if "lang" in self.VC_TTS_voice_type[self.VC_TTS_voice_key]: + lang = self.VC_TTS_voice_type[self.VC_TTS_voice_key]["lang"] + else: + print("not lang!!!!!!!!!!!!!!!!!!!!!!") + except Exception as e: + usys.print_exception(e) + + #女,男,童,粤,川 + self.bot_name = "柒先生" + try: + if "bot_name" in self.VC_TTS_voice_type[self.VC_TTS_voice_key]: + self.bot_name = self.VC_TTS_voice_type[self.VC_TTS_voice_key]["bot_name"] + else: + print("not bot_name!!!!!!!!!!!!!!!!!!!!!!") + except Exception as e: + usys.print_exception(e) + + # Headers = {'X-NLS-Token':'0c8b8f05a46f423096be64a9c4c46310'} + # self.AI_client_VC = uwebsocket.Client.connect('wss://nls-gateway-cn-beijing.aliyuncs.com/ws/v1',headers=Headers, debug=False) + token = EventMesh.publish("get_dev_token") + print("token:{}".format(token)) + + req_str = "vc/chat?is_auth={}".format(self.is_auth) + try: + if "vc_config" in globals() and vc_config: + base = vc_config.get_attr("VC_WS_PATH", "vc/chat") or "vc/chat" + req_str = "{}?is_auth={}".format(base, self.is_auth) + except Exception: + pass + if(token): + url = 'ws://{}/{}&dt={}'.format(self.server_ip,req_str,token) + else: + url = 'ws://{}/{}'.format(self.server_ip,req_str) + #url = 'ws://115.190.94.208:22080/vc/coze?dt={}'.format(token) + + if VC_WS_IO_LOG: + print("[VC] ws url {}".format(url)) + self.AI_client_VC = uwebsocket.Client.connect(url, debug=False) + #self.AI_WS_connect_Queue.put(self.AI_client_VC) + + + print("[VC] websocket_connect ok") + except Exception as e: #网络连接异常(已通过 CSQ/SIM,多为 URL/握手/服务端问题,勿播「信号弱」) + usys.print_exception(e) + print("websocket_connect_VC error!!!\r\n") + try: + print("[VC] ws connect exc: {}".format(e)) + except Exception: + pass + str_error = str(e) + if str_error.find("[Errno 22] EINVAL")!= -1: + print("[VC] websocket EINVAL: {}".format(str_error)) + self.websocket_error(play_weak_signal_tone=False) + EventMesh.publish("set_storage_config",["powerRestart",1])#调试临时关闭 + utime.sleep_ms(1000) + self.aud.stopPlayStream() + self.aud.stopAll() + Power.powerRestart() + + self.websocket_error(play_weak_signal_tone=False) + return + #print("AI_client_VC:{}".format(self.AI_client_VC)) + if self.AI_client_VC == None: + EventMesh.publish("update_dev_token")#获取token + self.websocket_error(play_weak_signal_tone=False) + return + self.AI_client_VC_debug+=1 + utime.sleep_ms(10) + self.AI_client_VC.settimeout(5) + #self.AI_VC_send_data_queue_thread_close()#关闭发送线程 + if self.AI_VC_send_data_queue_thread_id == 0: + _thread.stack_size(self.Audio_buf_size+8*1024) + self.AI_VC_send_data_queue_thread_id = _thread.start_new_thread(self.AI_VC_send_data_queue_thread, ()) + _thread.stack_size(8*1024) + print("AI_VC_start :AI_VC_send_data_queue_thread_id :{} ".format(self.AI_VC_send_data_queue_thread_id)) + self.AI_client_VC_recv_thread_close()#关闭读线程 + # 线程接收数据 + _thread.stack_size(self.Audio_buf_size+8*1024) + self.AI_client_VC_recv_id = _thread.start_new_thread(self.AI_client_VC_recv, (self.AI_client_VC,)) + _thread.stack_size(8*1024) + self.AI_client_VC_isconnect = 1 + print("websocket_connect_VC end") + + def websocket_error(self, TTS_str=None, play_weak_signal_tone=False): + """play_weak_signal_tone=True 时播 xinhaoruo1(弱网/无卡类);连接/协议失败请传 False,避免误报「信号弱」。""" + try: + if getattr(self, "VC_mode", 0) != 1: + print("[VC][long_idle] websocket_error enter VC_mode={} TTS_str={} weak_hint={} -> Close then uplink_stop(async)".format( + getattr(self, "VC_mode", -1), TTS_str, play_weak_signal_tone)) + except Exception: + pass + print("websocket_error1!!!!") + utime.sleep_ms(800) + self.Close() + print("websocket_error1.1!!!!") + print("websocket_error!!!!") + print("websocket_error!!!!") + self.VC_Thinking_timerout = 0 + self.VC_satrt_flag = 0 + self.AI_TTS_str = "" + self.AI_ASR_str = "" + self.AI_ASR_str_tmp = "" + self.AI_ASR_flag = 0 + # 可能由收包线程/其它线程触发:统一切回主线程执行 stop,避免“异常→重试→再异常”的循环 + try: + if getattr(self, "VC_mode", 0) != 1: + print("[VC][long_idle] websocket_error schedule uplink_stop reason=websocket_error + set_led_idle") + except Exception: + pass + EventMesh.publish("publish_AI_btnInfo",{"func":self._uplink_request_stop, "data":"websocket_error"}) + EventMesh.publish("set_led_state", {"ai_state": "idle"}) + if TTS_str == None: + self.websocket_error_count+=1 + # if self.websocket_error_count <=3: + # EventMesh.publish("audio_montage_play",["xinhaoruo1.mp3"]) + if play_weak_signal_tone and self.websocket_error_count < 10: + EventMesh.publish("audio_montage_play", ["xinhaoruo1.mp3"]) + elif self.websocket_error_count >=10: + # EventMesh.publish("paly_iccid_imei") + EventMesh.publish("audio_montage_play", ["xinhaoruo1.mp3"]) + # else: + # EventMesh.publish("audio_montage_play",["R_SIM_FAILE.mp3"]) + #self.aud.play(1,1,'U:/static/R_SIM_FAILE.mp3') + if self.websocket_error_count <3 : + utime.sleep_ms(3000) + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + else: + print("websocket_error:tts:{}".format(TTS_str)) + self.AI_TTS_str = TTS_str + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + self.Close_flag = 1 + print("websocket_error2{}".format(self.websocket_error_count)) + + + print("websocket_error4") + + + def websocket_deconnect_VC(self): + #print("websocket_deconnect_VC {}!!!!!!!".format(self.AI_client_VC_isconnect)) + print("websocket_deconnect_VC!!!!!!!") + self._vc_session_started = 0 + # 断链/模式切换时必须重置“上行录音状态”,否则会被 _uplink_request_start 的幂等门控挡住, + # 表现为:切回长对话后无法自动开麦,按键 cancel/canceled 也无法恢复。 + try: + self.VC_record_state_flag = 0 + except Exception: + pass + try: + self._server_vad_silence = 0 + except Exception: + pass + try: + self._ptt_audio_created = 0 + except Exception: + pass + try: + # 若录音线程/录音设备仍在占用,先停掉 + self.VC_Speech_End() + except Exception: + try: + self.VC_record_thread_close() + except Exception: + pass + # 断链必须同步重置 FSM,否则 await_played/turn 残留会导致动态切换后“无法自动开麦/按键无效” + try: + self.fsm.session = VCSessionState.Disconnected + self.fsm.turn = VCTurnState.Idle + self.fsm.await_played = False + self.fsm.last_tts_completed_ms = 0 + self.fsm.last_llm_ms = 0 + self.fsm.reset_rx_phases() + except Exception: + pass + self.AI_client_VC_recv_thread_close()#关闭读线程 + self.AI_client_VC_recv_id = 0 + if self.AI_client_VC_isconnect == 1 or self.AI_client_VC!=None: + try: + self.AI_client_VC_isconnect = 0 + count = 500 + while True:#等待发完数据 + count-=1 + if count <=0 or self.AI_VC_send_data_queue_thread_lock == 0: + break + utime.sleep_ms(1) + + try: + self.AI_client_VC.close() + self.AI_client_VC_debug -=1 + #self.AI_client_VC=None + except Exception as e: + usys.print_exception(e) + try : + self.AI_client_VC.close() + self.AI_client_VC_debug -=1 + #self.AI_client_VC=None + except Exception as e: + usys.print_exception(e) + try : + self.AI_client_VC._close() + except Exception as e: + usys.print_exception(e) + self.AI_client_VC = None + except Exception as e: + usys.print_exception(e) + print("websocket_deconnect_VC {}!!!!!!!".format(self.AI_client_VC_isconnect)) + #websocket发送ping + def AI_client_VC_ping(self): + pass + if self.AI_client_VC_isconnect == 1: + #self.AI_client_VC.write_frame(0x09,b'ping ping') + self.AI_client_VC_send("ping") + if VC_HEARTBEAT_LOG: + print("VC: ping ping") + + #websocket发送数据 + def AI_client_VC_send(self,data_str): + # if isinstance(data_str, str):#是字符串 + # print("AI_client_VC_send:{}".format(data_str)) + # pass + # else: + # print("AI_client_VC_send:len{}".format(len(data_str))) + # 统计“语音上行”真实开始/结束:这里只统计二进制音频(非 ping/字符串) + try: + if not isinstance(data_str, str): + l = len(data_str) + if l > 0: + self._uplink_mark_start(l) + self._uplink_bytes += l + except Exception: + pass + self.AI_VC_send_data_queue.put([AI_client_VC_num,data_str]) + #关闭数据发送线程 + def AI_VC_send_data_queue_thread_close(self): + if self.AI_VC_send_data_queue_thread_id != 0: + # 先清空待发队列,避免关闭过程持续堆积/刷屏 + if self.AI_VC_send_data_queue.empty() != True: + self.AI_VC_send_data_queue.clear() + # 通知发送线程退出;若发送线程正阻塞在底层 send,则只能等超时或强制 stop_thread + self.AI_VC_send_data_queue_thread_lock = 2 + count = 500 + while True: + count -= 1 + if self.AI_VC_send_data_queue_thread_lock == 0 or count <=0: + break + else: + # 该打印只是“等待线程停下来”的调试心跳,默认关闭以免影响系统实时性 + if VC_HEARTBEAT_LOG and (count % 50 == 0 or count > 490): + print("AI_VC_send_data_queue_thread going:{}...".format(count)) + utime.sleep_ms(5) + self.AI_VC_send_data_queue_thread_lock = 0 + if VC_HEARTBEAT_LOG: + print("AI_VC_send_data_queue_thread_close") + if self.AI_VC_send_data_queue_thread_id != 0: + try: + # 线程可能已自然退出/被复用,stop_thread 会抛 ValueError + try: + running = _thread.threadIsRunning(self.AI_VC_send_data_queue_thread_id) + except Exception: + running = True + if running: + _thread.stop_thread(self.AI_VC_send_data_queue_thread_id) + except Exception as e: + usys.print_exception(e) + self.AI_VC_send_data_queue_thread_id = 0 + #发送数据队列 + def AI_VC_send_data_queue_thread(self): + print("AI_VC_send_data_queue_thread:start") + count_t = 0 + while True: + if self.AI_VC_send_data_queue_thread_lock == 2: + print("AI_VC_send_data_queue_thread,该退出了") + break + if self.AI_VC_send_data_queue.empty() :#不能去掉,去掉会卡死 + self.AI_VC_send_data_queue_thread_lock = 0 + count_t+=1 + # if count_t%200 == 0: + # print("self.AI_VC_send_data_queue.empty") + utime.sleep_ms(1) + else: + #data[0]是websocket连接对象编号,data[1]是数据 + #print("AI_VC_send_data_queue_thread:get") + + self.AI_VC_send_data_queue_thread_lock = 1 + data = self.AI_VC_send_data_queue.get() + + + #print("AI_VC_send_data_queue_thread:num{} len{}".format(data[0],len(data[1]))) + if data != None: + try: + #data["func"](data["data"]) + # 发送数据 + #bytes_data = bytes(data[1]) + #data[0].send(data[1]) + #print("AI_VC_send_data_queue_thread:len:{}".format(len(data[1]))) + #print("AI_VC_send_data_queue_thread on line asr recv :{} vc recv:{}".format(_thread.threadIsRunning(self.AI_client_asr_recv_id),_thread.threadIsRunning(self.AI_client_VC_recv_id))) + if data[0] == AI_client_VC_num: + + if isinstance(data[1], str):#是字符串 + self.VC_pingping_timerout = 0 + s = data[1] + if s is None: + s = "" + # 保活 ping 不落 WS IO 日志(避免周期性刷屏) + if VC_WS_IO_LOG and s != "ping": + try: + if len(s) > 240: + preview = s[:160] + " ... " + s[-60:] + else: + preview = s + print("[VC] WS send_txt len={} txt={}".format(len(s), preview)) + except Exception: + try: + print("[VC] WS send_txt {}".format(data[1])) + except Exception: + pass + elif not VC_WS_IO_LOG: + if s == "ping": + pass + else: + try: + ev, tid = _vc_out_json_event_meta(s) + if ev: + print("[VC][proto] tx event={} trace_id={} len={} ws_conn={}".format( + ev, tid or "-", len(s), self.AI_client_VC_isconnect)) + else: + print("[VC][proto] tx unknown_json len={} ws_conn={}".format( + len(s), self.AI_client_VC_isconnect)) + except Exception: + print("[VC][proto] tx parse_fail len={}".format(len(s))) + else: + pass + # if VC_AUDIO_FRAME_LOG: + # print("VC: send_data len:{}".format(len(data[1]))) + # print("self.AI_client_VC = {} , self.AI_client_VC_isconnect = {}".format(self.AI_client_VC,self.AI_client_VC_isconnect)) + if self.AI_client_VC != None and self.AI_client_VC_isconnect == 1: + self.AI_client_VC.send(data[1]) + else: + try: + if isinstance(data[1], str) and data[1] != "ping": + _ev, _tid = _vc_out_json_event_meta(data[1]) + print("VC VC: send_data:ERROR len={} event={} trace_id={}".format( + len(data[1]), _ev or "?", _tid or "-")) + else: + print("VC VC: send_data:ERROR:{}".format(len(data[1]))) + except Exception: + print("VC VC: send_data:ERROR:{}".format(len(data[1]))) + #print("VC: send_data end") + + #print("VC: send_data end") + except Exception as e: + print("DeviceInfo_queue_thread:ERROR:{}".format(e)) + usys.print_exception(e) + if data[0] == AI_client_VC_num: + #self.websocket_error() + pass + + + try: + if self.AI_client_VC_isconnect != 0: + print("VC发送数据错误,退出") + EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error,"data":"VC发送出错了,请重试"}) + self.ud_error(["VC",self.VC_taskid]) + except Exception as e: #网络连接异常 + usys.print_exception(e) + + utime.sleep_ms(1) + self.AI_VC_send_data_queue_thread_lock = 0 + #VC关闭读线程 + def AI_client_VC_recv_thread_close(self): + if self.AI_client_VC_recv_id != 0: + count =500 + while True: + count -= 1 + if self.AI_client_VC_recv_lock == 1: + self.AI_client_VC_recv_lock = 2 + if self.AI_client_VC_recv_lock == 0 or count <=0: + break + else: + print("AI_client_VC_recv_thread going:{}...".format(count)) + utime.sleep_ms(1) + self.AI_client_VC_recv_lock = 0 + print("AI_client_VC_recv_thread_close") + if self.AI_client_VC_recv_id!= 0: + try: + _thread.stop_thread(self.AI_client_VC_recv_id) + except Exception as e: + usys.print_exception(e) + self.AI_client_VC_recv_id = 0 + #Ai VC websocket返回数据 + def AI_client_VC_recv(self,cli): + count = 0 + asr_led_flag = 0 + tts_led_flag = 0 + while True: + try: + # 死循环接收数据 + self.AI_client_VC_recv_lock = 0 + + count+=1 + if self._vc_tts_pipeline_busy() and (self.tts_in_data_len - self.tts_out_data_len > 64*1024 or count>5): + count = 0 + utime.sleep_ms(1) + if not self._vc_tts_pipeline_busy(): + utime.sleep_ms(1) + recv_data = cli.recv() + if self.AI_client_VC_recv_lock == 2: + print("AI_client_VC_recv 该退出了") + break + self.AI_client_VC_recv_lock = 1 + if self._vc_rx_resets_long_idle_timer(recv_data): + self.close_timerout = 0 + if recv_data == "websocket close": + # 服务器关闭连接 + print("VC 服务器关闭连接!!!!!!!!!!!!!!!!!!!!!!!!!!!") + #self.AI_client_VC_isconnect = 0 + #self.websocket_deconnect_VC() + #EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_asr}) + # #EventMesh.publish("publish_AI_btnInfo",{"func":utime.sleep_ms,'data':(1500)}) + #EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_connect_asr}) + EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error,"data":"VC连接断开了,请重试"}) + self.ud_error(["VC",self.VC_taskid]) + break + elif recv_data == "":#客户端关闭连接 + print("VC VC :客户端关闭连接") + EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error,"data":"VC连接断开了,请重试"}) + self.ud_error(["VC",self.VC_taskid]) + break + try: + self.websocket_error_count = 0 + if isinstance(recv_data, str):#返回的是字符串 + jsload_data = ujson.loads(recv_data) + if VC_WS_IO_LOG: + print("[VC] recv raw:{}".format(recv_data)) + else: + try: + _rev = jsload_data.get("event", "") + if _rev != "llm.delta": + print("[VC] rx event={} sid={}".format( + _rev, jsload_data.get("sid", "") or "-")) + except Exception: + pass + #print("AI_client_VC_recv jsload_data = {}".format(jsload_data)) + try: + self._cur_sid = jsload_data.get("sid", "") or "" + except Exception: + self._cur_sid = "" + try: + ev = jsload_data.get("event", "") + if ev: + self._vc_dispatch_server_event(ev, jsload_data) + except Exception: + pass + + if jsload_data["event"] == "started" :#会话开始 + #payload = jsload_data["payload"] + # self.VC_dialog_id = payload["dialog_id"] + # self.VC_task_id = jsload_data["task_id"] + # started 仅表示链路就绪:不在这里提前切灯态; + # 聆听灯只在“录音真正开始/停止”处更新(VC_Speech_Start/End)。 + # 会话开始:允许上行;连续对话模式下直接进入“可发送音频”状态 + self._vc_session_started = 1 + # started 时不要强制清 Close_flag: + # - Close_flag==1:提示音/模式切换播完退出 + # - Close_flag==0:正常对话流程 + self._ptt_audio_created = 0 + self._server_vad_silence = 0 + self._turn_has_asr_completed = 0 + self._turn_has_vad_silence = 0 + self._turn_has_tts_started = 0 + self._llm_completed_ms = 0 + if self.VC_mode == 0 and getattr(self, "Close_flag", 0) == 0: + _skip_uplink = 0 + try: + if getattr(self, "_skip_started_uplink_once", 0) == 1: + self._skip_started_uplink_once = 0 + _skip_uplink = 1 + print("[VC][uplink_ctrl] Deny START by server.started cause=boot_prompt") + except Exception: + pass + if not _skip_uplink: + # 会话已 started:若此时上行未启动,则在这里补一次开麦,避免“Start 前开麦被 Close 停掉”后 + # 出现首轮无音频上行导致“第一次对话没有”的情况。 + if self.record_data_save_flag == 0: + if self.VC_record_state_flag == 0: + self.request_uplink_start("server.started") + else: + self._vc_clear_stale_record_state("server.started") + self.request_uplink_start("server.started/retry") + + print("VC:Started!!!!!!!!\r\n") + print("TTS音频为:{},语音下发地址为:{}".format(self.AI_TTS_str,self.AI_new_voice_url)) + if self.AI_new_voice_url != "" and self.alarm_play_flag == 1 :#有闹钟,先响闹钟 + self.AI_VC_start() + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放铃声 + self.alarm_play_flag = 0 + self.voice_choose_flag = 1 + elif self.AI_TTS_str == "" and self.AI_new_voice_url != "" and self.music_play_flag == 0: + # if EventMesh.publish("get_history_voice_flag") == 1: + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 + # self.recv_voice = 0#当留言列表为空,留言播放标志清空 + # else: + # self.voice_cnt = 0 + # print("定时器开启222222222") + # self.voice_timer.start(1000,1,self.voice_judge) + elif self.AI_TTS_str != "" and self.AI_new_voice_url == "": + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_synthesis}) + + elif self.AI_TTS_str != "" and self.AI_new_voice_url != "" and self.music_play_flag == 0:#遇到tts和语音留言冲突时(并且没有音乐播放) + if EventMesh.publish("get_history_voice_flag") == 1:#播放历史留言 + if EventMesh.publish("get_wait_voice_flag") == 0: + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_synthesis}) + self.his_cnt = 0 + self.his_timer.start(1000,1,self.history_play) + self.voice_choose_flag = 1 + else:#等待留言 + if self.pwk_notice_flag == 0: + self.wait_cnt = 0 + print("未听播放1") + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_synthesis}) + self.wait_timer.start(1000,1,self.wait_play) + self.voice_choose_flag = 1 + + else:#已确定播放 + print("未听播放2") + self.wait_cnt = 0 + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_synthesis}) + self.wait_timer.start(1000,1,self.wait_single_play) + self.voice_choose_flag = 1 + else:#播放正常留言 + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_synthesis}) + self.voice_cnt = 0 + print("定时器开启111111111") + self.voice_timer.start(1000,1,self.voice_judge) + self.voice_choose_flag = 1 + # self.AI_TTS_str = "" + elif self.AI_TTS_str != "" and self.AI_new_voice_url != "" and self.music_play_flag == 1 :#遇到tts和语音留言冲突时(并且有音乐播放) + self.music_play_flag = 0 + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_synthesis}) + self.music_timer.start(1000,1,self.music_play) + self.voice_choose_flag = 1 + else: + #开始上传语音 + try: + if "vc_config" in globals() and vc_config and vc_config.get_bool("UPLOAD_START_TONE_ENABLED", False): + self.chenggong_tone() + except Exception: + pass + if self.VC_mode == 0: + self.request_uplink_start("server.started/idle") + else: + # Push-To-Talk:必须先同步发出 audio.create,再开麦。 + # request_send_audio_create() èµ° EventMesh 异步队列,会导致录音线程先于 + # audio.create 上行,深度睡眠唤醒后首轮 ASR 空结果、第二轮才正常。 + try: + self.AI_client_VC_send_speech() + except Exception: + self.request_send_audio_create() + # 若长按发生在“未 started”窗口,收到 started 后立即开麦进入新一轮 PTT + try: + if getattr(self, "_ptt_force_record_on_started", 0) == 1: + self._ptt_force_record_on_started = 0 + self.VC_Speech_Start_now() + except Exception: + pass + + + # if self.AI_new_voice_url != "": + # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice}) + + elif jsload_data["event"] == "config.update" :#配置更新中,需要停止录音 + try: + self.fsm.reset_rx_phases() + except Exception: + pass + self.request_uplink_stop("config.update") + + elif jsload_data["event"] == "asr.started" :#asr开始 + try: + self.fsm.on_server_asr_started() + except Exception: + pass + self._server_vad_silence = 0 + self._turn_has_asr_completed = 0 + self._turn_has_vad_silence = 0 + self._turn_has_tts_started = 0 + self._llm_completed_ms = 0 + try: + self._asr_completed_tone_done = 0 + except Exception: + pass + # 单对话:_drop_tts_audio 在 VC_Speech_Start 真开录后再清 0,避免 canceled 后仍播旧 TTS + try: + if getattr(self, "VC_mode", 0) != 1: + self._drop_tts_audio = 0 + except Exception: + pass + # 单对话:勿在 asr.started ç½® flag=2(未真开麦会僵死);由 VC_Speech_Start 维护 + self.LocalRespondingEnded_timerout = 0 + elif jsload_data["event"] == "asr.result" :#asr结果 + try: + self.AI_ASR_str = jsload_data["payload"]["content"] + except Exception: + try: + self.AI_ASR_str = jsload_data.get("payload", {}).get("text", "") or "" + except Exception: + self.AI_ASR_str = "" + print("[VC][ASR] {}".format(self.AI_ASR_str)) + try: + if (self.AI_ASR_str or "").strip(): + self._last_asr_result_text = self.AI_ASR_str + except Exception: + pass + if self.AI_ASR_str == "":#没识别到 + self.AI_TTS_str = "我没听清,请再说一遍" + self._vc_led_alert_rb_flash() + elif jsload_data["event"] == "asr.completed" :#asr 终止的标识 + self._turn_has_asr_completed = 1 + try: + if not (self.AI_ASR_str or "").strip(): + self.AI_ASR_str = getattr(self, "_last_asr_result_text", "") or "" + except Exception: + pass + # 本地 think 提示:仅在有有效 ASR 文本时播,避免空轮次连播「滴」 + try: + if getattr(self, "_asr_completed_tone_done", 0) == 1: + pass + elif not self._vc_effective_asr_text(): + try: + print("[VC][ASR] completed empty, skip think tone") + except Exception: + pass + else: + stream_on = False + try: + stream_on = EventMesh.publish("get_vc_tts_playback_active") == 1 + except Exception: + stream_on = False + pcm_busy = getattr(self, "play_pcm", None) is not None + playing = getattr(self, "VC_Play_flag", 0) == 1 + try: + thinking = getattr(self.fsm, "turn", VCTurnState.Idle) == VCTurnState.Thinking + except Exception: + thinking = False + if (not stream_on) and (not pcm_busy) and (not playing) and (not thinking): + self._vc_local_play('U:/static/think.mp3', 1, 1, 650) + self._asr_completed_tone_done = 1 + except Exception: + pass + print("self.commands_mode:{}".format(self.commands_mode)) + if self.commands_mode ==2: + ret= self.check_text(self._vc_effective_asr_text()) + if ret == 0:#配置指令 + + if self.AI_VC_send_data_queue.empty()!=True: + self.AI_VC_send_data_queue.clear() + if self.AI_ASR_send_data_queue.empty()!=True: + self.AI_ASR_send_data_queue.clear() + self.AI_ASR_send_buf_len = 0 + EventMesh.publish("set_led_state", {"ai_state": "thinking"}) + try: + + self.request_uplink_stop("asr.completed/config_cmd") + EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + except Exception as e: + usys.print_exception(e) + continue + elif ret == 1:#不处理 + pass + #self.VC_Speech_End() + #EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) + + # if self.record_data_save_flag == 1: + # if self.record_fd : + # self.record_fd.close() + # self.record_fd = None + + # if dev_type != "7PIN" : + #EventMesh.publish("publish_AI_btnInfo",{"func":self.send_tone}) + elif jsload_data["event"] == "vad.active" :#VAD 检测到说话→聆听 + try: + if "vc_config" in globals() and vc_config and vc_config.get_bool("VAD_ACTIVE_BLUE_LED", True): + self._server_vad_silence = 0 + except Exception: + pass + elif jsload_data["event"] == "vad.silence" :#VAD 终止的标识 + # 说明服务端已判定“本轮说话结束”;此时继续上行只会干扰后续(例如推迟 TTS)。 + # 仅在“正在录音上行”的语音阶段才停止,避免误触发。 + self._server_vad_silence = 1 + self._turn_has_vad_silence = 1 + self._uplink_mark_end("vad.silence") + if self.record_data_save_flag == 0: + if self.VC_record_state_flag != 0 and self._vc_vad_uplink_stop_eligible(): + self.request_uplink_stop("vad.silence") + elif jsload_data["event"] == "llm.created" :#思考(LLM) + try: + self._ptt_await_server = 0 + except Exception: + pass + try: + EventMesh.publish("set_led_state", {"ai_state": "thinking"}) + except Exception: + pass + self._llm_completed_ms = 0 + # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Play_Start}) + pass + elif jsload_data["event"] == "llm.completed" :#llm完成(可能无 tts) + try: + self._llm_completed_ms = utime.ticks_ms() + except Exception: + # 勿用 1:ticks_diff(now,1)≈开机至今,会瞬间满足 thinking 超时误以为该重启 + self._llm_completed_ms = 0 + try: + self._cur_dialog_id = jsload_data.get("payload", {}).get("dialog_id", "") or "" + except Exception: + self._cur_dialog_id = "" + try: + self.fsm.log("server.llm.completed") + except Exception: + pass + pass + elif jsload_data["event"] == "llm.delta" :#llm返回数据 + if self.is_VAD == 0 and self.record_data_save_flag == 0: + if self.VC_record_state_flag == 1:#llm返回数据还没停止,就先停止 + self.request_uplink_stop("llm.delta") + elif jsload_data["event"] == "tts.started" :#tts开始 + try: + self._ptt_await_server = 0 + except Exception: + pass + self._server_vad_silence = 1 + self._turn_has_tts_started = 1 + self._tts_pending = 0 + self._tts_pending_ms = 0 + self._uplink_mark_end("tts.started") + # stop uplink + play_start 由 FSM action list 统一触发,避免多处重复调用 + # 标记本轮 TTS 来源: + # - 有 payload.dialog_id:认为是“对话回复 TTS” + # - 无 dialog_id:认为是“提示/提醒 TTS”(模式切换可通过 _pending_tts_kind 标记) + try: + did = jsload_data.get("payload", {}).get("dialog_id", "") or "" + except Exception: + did = "" + # 注意:无 dialog_id 的 tts.started 很可能是提示音/提醒音; + # 但“模式切换提示音”的 kind 可能已在 synthesis 时写入 _last_tts_kind, + # 这里不应无条件覆盖为 prompt,否则播完无法触发下一轮开麦。 + try: + if did: + self._last_tts_kind = "dialog" + else: + k = getattr(self, "_pending_tts_kind", "") or "" + if k: + self._last_tts_kind = k + else: + cur = getattr(self, "_last_tts_kind", "") or "" + if cur in ( + "mode_switch_long", + "mode_switch_ptt", + "boot_prompt", + ): + pass + elif not cur: + self._last_tts_kind = "prompt" + except Exception: + pass + try: + self._pending_tts_kind = "" + except Exception: + pass + # 播放灯由阶段事件控制 + elif jsload_data["event"] == "tts.completed" :#tts结束 + # tts.completed 仅表示下发结束,本地可能仍在播放;绿灯关闭在本地播放结束时处理 + try: + # server event 已在统一入口 on_server_event 触发 FSM,这里避免重复驱动 + pass + except Exception: + pass + try: + try: + sid = getattr(self, "_cur_sid", "") + did = getattr(self, "_cur_dialog_id", "") + except Exception: + sid, did = "", "" + print("[TTS] completed in_len={} out_len={} q_empty={} play_pcm={} sid={} did={}".format( + getattr(self, "tts_in_data_len", 0), + getattr(self, "tts_out_data_len", 0), + 1 if self.AI_audio_queue.empty() else 0, + 1 if self.play_pcm != None else 0, + sid, + did, + )) + except Exception: + pass + # 播完上报完全由 FSM await_played + play_end 驱动,这里不再使用 LocalRespondingEnded_timerout/_need_audio_played_ack + pass + + elif jsload_data["event"] == "canceled" :#打断 + # 停播/清队列已在 _vc_dispatch_server_event(canceled) 完成;此处仅收尾标志 + self.is_ruppt_tts = 0#设备上发中断tts,收到服务器返回的cancel后置为0 + self._need_audio_played_ack = 0 + self._tts_pending = 0 + self._tts_pending_ms = 0 + # 开麦由 FSM action list 统一触发(uplink_start:canceled),避免重复调用 + if self.AI_audio_queue_thread_id ==0: + _thread.stack_size(24*1024) + self.AI_audio_queue_thread_id = _thread.start_new_thread(self.AI_audio_queue_thread, ()) + _thread.stack_size(8*1024) + print("AI_VC_start :AI_audio_queue_thread_id :{} ".format(self.AI_audio_queue_thread_id)) + else: + print("AI_VC_start :AI_audio_queue_thread_id != 0 ") + elif jsload_data["event"] == "completed" :#会话结束(可能无 TTS,如 ASR 空) + # 重要:服务端常在 tts.completed 后紧跟 completed。 + # 若本轮存在 TTS/待发 audio.played,则不能在此清零 LocalRespondingEnded_timerout/_need_audio_played_ack, + # 否则会掐断「播完→audio.played→下一轮录音」链路,表现为最多 1~2 轮就停。 + if self._need_audio_played_ack == 1 or self._vc_tts_pipeline_busy() or self.VC_Play_flag == 1: + pass + else: + # 无 TTS 的 completed:清状态并按需进入下一轮(见下方条件) + self.LocalRespondingEnded_timerout = 0 + self._need_audio_played_ack = 0 + try: + self.fsm.reset_rx_phases() + except Exception: + pass + # 连续对话:若本轮没有 TTS(或服务端直接 completed),需要立刻回到聆听状态 + # 否则会出现“有时 1 è½®/2 轮后就停住”的现象(服务端 completed 但端侧未重新开录音上行)。 + if self.VC_mode == 0 and self.Close_flag == 0: + # 仅当“本轮已结束一次语句(vad.silence 或 asr.completed)且从未进入 tts.started” + # 才自动进入下一轮;避免把真正的“会话结束/异常结束”误当成下一轮入口。 + if (self._turn_has_tts_started == 0) and (self._turn_has_vad_silence == 1 or self._turn_has_asr_completed == 1): + self._server_vad_silence = 0 + # 开麦由 FSM action list 统一触发(uplink_start:completed),这里不再重复调用 + pass + + elif jsload_data["event"] == "command" :#命令匹配 + ret = -1 + try: + if self.commands_mode ==1: + if len(jsload_data["payload"]["functions"])>0: + + cmd = jsload_data["payload"]["functions"][0]["function"]["arguments"] + print("llm:cmd:{}".format(cmd)) + cmd = ujson.loads(cmd) + ret = self.check_text_llm(cmd) + elif self.commands_mode ==0: + + cmd = jsload_data["payload"] + print("server:cmd:{}".format(cmd)) + ret = self.check_text_server(cmd) + else: + cmd = jsload_data["payload"]["functions"][0]["function"]["arguments"] + print("llm:cmd:{}".format(cmd)) + cmd = ujson.loads(cmd) + ret = self.check_text_llm(cmd) + except Exception as e: + usys.print_exception(e) + if ret == -1: + if self.AI_TTS_str =="": + self.AI_TTS_str = "抱歉,没找到对应操作,可以换个说法试试" + #EventMesh.publish("publish_AI_btnInfo",{"func":utime.sleep_ms,'data':(10)}) + self.request_uplink_stop("command") + EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + elif jsload_data["event"] == "failed":#接口返回错误 + if jsload_data["payload"]["error"] == "concurrency_limit_exceeded": + EventMesh.publish("publish_AI_btnInfo",{"func":self.concurrency_limit_exceeded_tone}) + + #utime.sleep_ms(4000) + + else: + EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error,"data":jsload_data["payload"]["message"]}) + pass + + elif jsload_data["event"] == "audio.convert.completed":#音频转成完成没有出错 接口 + # self.VC_state = VC_State_End + print("未读留言初始数值:{}".format(self.pwk_cnt_voice)) + if self.pwk_notice_flag == 1:#只有在播放的时候才会去计数 + if self.pwk_cnt_voice > 0: + self.pwk_cnt_voice -= 1#减1 + print("未读留言又-1,当前留言剩余数量为:{}".format(self.pwk_cnt_voice)) + else: + print("未读列表为空") + if self.pwk_cnt_voice == 0:#积累的留言已经全部播完 + self.pwk_notice_flag = 0 + self.wait_sort = 0 + self.wait_first_flag = 0#让下次未读播放提示恢复 + self.voicerupt_flag = 0 #标志位置0 + self.wait_voicerupt_flag = 0 + self.voice_notice = 1#开启回复 + if self.history_voice_flag == 1 and self.wait_voice_flag == 0: + self.voice_notice = 0 + EventMesh.publish("set_storage_config",["is_noread_voice",0]) + + if self.voice_choose_flag == 1:#播放完语音后,有冲突的时候关闭 + self.voice_choose_flag = 0 + self.Close_flag = 1 + self.LocalRespondingEnded_timerout = 8 + # EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) + if self.voice_notice == 1:#已收到留言,进行该留言回复逻辑处理 + tts_play_str = "如需回复,长按确认键开始录制,松开发送。十秒内无操作自动退出" + EventMesh.publish("play_tts",[tts_play_str,1]) + self.voice_notice = 0#标志位清空 + self.voice_timer.stop() + self.revoice_cnt = 0 + self.voice_flag = 1 + print("语音留言回复停止") + self.revoice_timer.start(1000,1,self.revoice_judge) + print("是否未读:{},是否播完:{}".format(EventMesh.publish("get_wait_voice_flag"),len(EventMesh.publish("get_play_new_voice_list")))) + # if EventMesh.publish("get_wait_voice_flag") == 1 and len(EventMesh.publish("get_play_new_voice_list")) ==0:#还在未读并且列表为空 + # self.wait_first_flag = 0 + # print("未读留言播完,清空标志") + # EventMesh.publish("set_wait_voice_flag",0) + # EventMesh.publish("set_history_voice_flag",0) + EventMesh.publish("update_play_new_voice_info")#更新已读状态 + self.recv_voice = 0 + print("不开启定时器标志位置0") + self.LocalRespondingEnded_timerout = 8 + try: + self.fsm.reset_rx_phases() + except Exception: + pass + + pass + elif jsload_data["event"] == "audio.convert.failed": #音频转换失败了 + pass + + + else:#下发音频数据 + #self.AI_ASR_str = ""#下发音频后清掉asr结果 + + + # PTT 打断后丢弃残留 TTS 音频帧,避免“断断续续继续播” + try: + if self._ptt_should_drop_inbound_tts(): + continue + except Exception: + pass + if self.VC_stop_play_audio_flag == 0: + print("新的AI对话不允许以前的播放数据:") + break + + # if VC_AUDIO_FRAME_LOG: + # print("下发音频数据1:{}".format(len(recv_data))) + + if self.play_data_save_flag == 1: + if self.play_fd ==None: + try: + uos.remove('/usr/play_data.pcm') + except Exception as e: + usys.print_exception(e) + self.play_fd = open('/usr/play_data.pcm','w') + if self.play_fd !=None: + self.play_fd.write(recv_data) + # print("下发音频数据2:{}".format(len(recv_data))) + #if self.VC_state >= VC_State_TTSStart: + #self.put_record_data([recv_data,""]) + if self.is_ruppt_tts == 0:#被设备主动打断打断则不播放,知道收到服务器返回的cancel才修改标志位 + if self.VC_play_TYPE == VC_play_TYPE_PCM: + if self.play_pcm != None: + try: + #print("play_pcm.stream_write !!!") + buf_len = self.play_pcm.stream_get_buf_len() + count_tmp = 1000 + while buf_len < len(recv_data): + utime.sleep_ms(1) + count_tmp-=1 + if count_tmp<=0 or self.play_pcm == None: + break + buf_len = self.play_pcm.stream_get_buf_len() + #print("buf已满,等待释放:buf_len:{},count_tmp:{}".format(buf_len,count_tmp)) + except Exception as e: + usys.print_exception(e) + EventMesh.publish("publish_AI_btnInfo",{"func":self.put_record_data,"data":[recv_data,""]}) + else: + print("被打断不播放") + utime.sleep_ms(1) + except Exception as e: + usys.print_exception(e) + + except Exception as e: + error_str = str(e) + #print("VC VC:e:{}".format(error_str)) + if error_str.find("[Errno 110] ETIMEDOUT")!= -1 or error_str.find("[Errno 115] ")!= -1: + if VC_HEARTBEAT_LOG: + print("VC VC:recv 正常报错:{}".format(error_str)) + # recv 超时仅表示本周期无帧,链路仍可能在等 LLM/TTS;清零避免误触发「接口无响应」 + self.close_timerout = 0 + pass + else: + usys.print_exception(e) + #self.websocket_error() + print("VC VC :读数据报错,退出") + EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error,"data":"VC出错了,请重试"}) + self.ud_error(["VC",self.VC_taskid]) + break + self.AI_client_VC_isconnect = 0 + self.AI_client_VC_recv_id = 0 + self.AI_client_VC_recv_lock = 0 + print("[VC] recv_thread exit") + + def voice_play_10S(self,event,msg):#按键开启留言播放等待10S定时器判断 + self.voice_timer.start(1000,1,self.voice_judge) + #语音留言定时器 + def voice_judge(self, *args, **kwargs): + EventMesh.publish("publish_AI_btnInfo",{"func":self.voice_judger_cb}) + def voice_judger_cb(self): + self.voice_cnt += 1 + # print("语音留言定时器cnt = {}".format(self.voice_cnt)) + if self.voicerupt_flag == 1:#按键被按下 + self.pwk_notice_flag = 1#表示进入收听未听留言 + self.AI_VC_start() + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 + self.voice_cnt = 0 + # self.recv_voice = 0#进入留言定时器,留言清空 + print("进入留言定时器1,留言清空!!!!!") + EventMesh.publish("update_play_new_voice_info")#更新已读状态 + self.voice_timer.stop() + if self.pwk_cnt_voice > 0: + EventMesh.publish("get_new_voice_msg_S") + if self.voice_cnt > 15:#10S后退出播放,5S是为了给语音提示留时间 + self.voice_cnt = 0 + self.recv_voice = 0#进入留言定时器,留言清空 + if self.voice_choose_flag == 1:#播放完语音后,有冲突的时候关闭 + self.voice_choose_flag = 0 + print("进入留言定时器2,留言清空!!!!!") + self.voice_timer.stop() + #语音留言回复定时器 + def revoice_judge(self, *args, **kwargs): + EventMesh.publish("publish_AI_btnInfo",{"func":self.revoice_judger_cb}) + def revoice_judger_cb(self): + self.revoice_cnt += 1 + # print("语音留言回复定时器cnt = {}".format(self.revoice_cnt)) + if self.revoice_cnt > 20:#10S后退出播放,10S是为了给语音提示留时间 + self.revoice_cnt = 0 + self.voice_flag=0 + EventMesh.publish("set_1M_nowplay_flag",0) + print("语音留言回复停止") + self.revoice_timer.stop() + #音乐播放定时器 + def music_play(self, *args, **kwargs): + EventMesh.publish("publish_AI_btnInfo",{"func":self.music_play_cb}) + def music_play_cb(self): + self.music_cnt += 1 + if self.music_cnt > 4:#4S后播放音乐,4S是为了给语音提示留时间 + self.AI_VC_start() + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 + self.music_cnt = 0 + self.music_timer.stop() + #历史留言播放定时器 + def history_play(self, *args, **kwargs): + EventMesh.publish("publish_AI_btnInfo",{"func":self.history_play_cb}) + def history_play_cb(self): + self.his_cnt += 1 + if self.his_cnt > 4:#4S后播放,4S是为了给语音提示留时间 + self.AI_VC_start() + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 + self.his_cnt = 0 + self.his_timer.stop() + #未读留言是否播放定时器 + def wait_play(self, *args, **kwargs): + EventMesh.publish("publish_AI_btnInfo",{"func":self.wait_play_cb}) + def wait_play_cb(self): + self.wait_cnt += 1 + if self.wait_voicerupt_flag == 1:#按键被按下 + EventMesh.publish("set_is_listen_wait",1)#已听 + self.pwk_notice_flag = 1#表示进入收听未听留言 + self.AI_VC_start() + self.wait_sort = 1 + self.AI_TTS_str = "第"+str(self.wait_sort)+"条" + # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 + # self.wait_cnt = 0 + # print("进入留言定时器1,留言清空!!!!!") + # EventMesh.publish("update_play_new_voice_info")#更新已读状态 + self.wait_timer.stop() + if self.wait_cnt > 10:#10S后退出播放,5S是为了给语音提示留时间 + self.wait_cnt = 0 + self.recv_voice = 0#进入留言定时器,留言清空 + EventMesh.publish("set_history_voice_flag",0) + EventMesh.publish("set_wait_voice_flag",0) + EventMesh.publish("set_open_device",0)#开机不听 + self.wait_first_flag = 0 + if self.voice_choose_flag == 1:#播放完语音后,有冲突的时候关闭 + self.voice_choose_flag = 0 + print("进入留言定时器2,留言清空!!!!!") + self.wait_timer.stop() + #未读留言一条条播放定时器 + def wait_single_play(self, *args, **kwargs): + EventMesh.publish("publish_AI_btnInfo",{"func":self.wait_single_play_cb}) + def wait_single_play_cb(self): + self.wait_cnt += 1 + if self.wait_cnt > 2:#2S后播放,2S是为了给语音提示留时间 + self.AI_VC_start() + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 + self.wait_cnt = 0 + self.wait_timer.stop() + def set_voicerupt_flag(self,event,msg):#按键控制当前留言播放 + self.voicerupt_flag = msg + def set_wait_voicerupt_flag(self,event,msg):#按键控制未读留言播放 + self.wait_voicerupt_flag = msg + def get_voicerupt_flag(self,event,msg):#获取按键控制留言播放状态 + return self.voicerupt_flag + def get_recv_voice(self,event,msg): + return self.recv_voice + def set_recv_voice(self,event,msg): + return self.recv_voice + def put_record_data(self,data): + try: + if self._ptt_should_drop_inbound_tts(): + return + except Exception: + pass + #print("下发音频数据1:{}".format(len(data[0]))) + ret=self.AI_audio_queue.put(data) + try: + if self.tts_in_data_len == 0 and data and len(data[0]) > 0: + try: + sid = getattr(self, "_cur_sid", "") + did = getattr(self, "_cur_dialog_id", "") + except Exception: + sid, did = "", "" + print("[TTS] first_chunk len={} play_pcm={} VC_Play_flag={} sid={} did={}".format( + len(data[0]), + 1 if self.play_pcm != None else 0, + getattr(self, "VC_Play_flag", 0), + sid, + did, + )) + except Exception: + pass + self.tts_in_data_len += len(data[0]) + #print("下发音频数据2:{},{}".format(len(data[0]),self.tts_in_data_len)) + #msg[0]是tts播放字符串,msg[1]是是否关闭当前tts + def play_tts(self,event = None,msg= None): + EventMesh.publish("publish_AI_btnInfo",{"func":self.play_tts_cb,"data":msg}) + + def play_tts_cb(self,data): + try: + tts_str = data[0] + is_close_VC = data[1] + if tts_str != "": + self.AI_TTS_str = tts_str + if is_close_VC != None: + self.Close_flag = is_close_VC + if self.AI_VC_send_data_queue.empty()!=True: + self.AI_VC_send_data_queue.clear() + if self.AI_ASR_send_data_queue.empty()!=True: + self.AI_ASR_send_data_queue.clear() + try: + if is_close_VC: + EventMesh.publish("set_led_state", {"ai_state": "idle"}) + else: + EventMesh.publish("set_led_state", {"ai_state": "thinking"}) + except Exception: + pass + self.AI_ASR_send_buf_len = 0 + self._uplink_request_stop("play_tts_cb") + self.websocket_deconnect_VC() + self.AI_VC_start() + + # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + except Exception as e: + usys.print_exception(e) + pass + def check_text_server(self,cmd): + return -1 + + def _apply_vc_text_intent(self, text, cmd, llm_style_tts): + """表驱动文本意图(vc_text_intent)。未命中返回 None;命中返回 0/1/-1。""" + if vc_text_intent is None: + return None + try: + try: + _btv = bool("vc_config" in globals() and vc_config and vc_config.get_bool("BT_VOICE_FEATURES_ENABLED", False)) + except Exception: + _btv = False + r = vc_text_intent.match_vc_command(text, {"bt_voice": _btv}) + except Exception as e: + usys.print_exception(e) + return None + if not r: + return None + return self._apply_vc_text_intent_matched(r, cmd, llm_style_tts) + + def _apply_vc_text_intent_matched(self, r, cmd, llm_style_tts): + if r.get("needs_payload") and cmd is None: + return None + it = r["intent"] + if r.get("tts"): + self.AI_TTS_str = r["tts"] + if r.get("close_flag"): + self.Close_flag = 1 + if it == "goodbye": + return 0 + if it == "shutdown": + if llm_style_tts: + EventMesh.publish("publish_AI_btnInfo", {"func": self.Close}) + EventMesh.publish("Power_off") + return 1 + try: + close_only = ("vc_config" in globals() and vc_config and vc_config.get_bool("POWEROFF_CMD_CLOSE_ONLY", True)) + except Exception: + close_only = True + if close_only: + EventMesh.publish("publish_AI_btnInfo", {"func": self.Close}) + return 1 + EventMesh.publish("publish_AI_btnInfo", {"func": self.Close}) + EventMesh.publish("Power_off") + return 1 + if it == "toggle_dialog_mode": + if self.VC_mode == 0: + self.VC_mode = 1 + if llm_style_tts: + self.AI_TTS_str = "了解,已切换到单对话模式" + else: + try: + self.AI_TTS_str = vc_config.get_attr("CMD_TOGGLE_MODE_TO_PTT_TTS", "已切换到单对话模式") + except Exception: + self.AI_TTS_str = "已切换到单对话模式" + else: + self.VC_mode = 0 + if llm_style_tts: + self.AI_TTS_str = "了解,已切换到长对话模式" + else: + try: + self.AI_TTS_str = vc_config.get_attr("CMD_TOGGLE_MODE_TO_LONG_TTS", "已切换到长对话模式") + except Exception: + self.AI_TTS_str = "已切换到长对话模式" + EventMesh.publish("set_storage_config", ["VC_mode", self.VC_mode]) + return 0 + if it == "volume_set": + try: + pj = ujson.loads(cmd["command"]["params_json"]) + volume = pj["n"] + except Exception as e: + usys.print_exception(e) + try: + self.AI_TTS_str = cmd["command"]["fail_prompt"] + except Exception: + self.AI_TTS_str = "" + return -1 + if volume > 100 or volume < 0: + self.AI_TTS_str = "音量调整超出范围了,只能0-100哦" + else: + self.VC_TTS_volume = volume + self.aud.setVolume(self.VC_TTS_volume // 10) + EventMesh.publish("set_storage_config", ["VC_TTS_volume", self.VC_TTS_volume]) + self.AI_TTS_str = "了解,音量已经调整,当前音量是{}".format(self.VC_TTS_volume) + EventMesh.publish("post_device_state") + EventMesh.publish("adjust_volume_toBle", self.VC_TTS_volume) + return 0 + if it == "volume_up": + if llm_style_tts: + try: + print("self.VC_TTS_volume add:{}".format(self.VC_TTS_volume)) + except Exception: + pass + self.VC_TTS_volume += 10 + if self.VC_TTS_volume > 100: + self.AI_TTS_str = "音量已经是最大啦" if llm_style_tts else "音量已最大" + self.VC_TTS_volume = 100 + else: + self.aud.setVolume(self.VC_TTS_volume // 10) + EventMesh.publish("set_storage_config", ["VC_TTS_volume", self.VC_TTS_volume]) + EventMesh.publish("post_device_state") + EventMesh.publish("adjust_volume_toBle", self.VC_TTS_volume) + self.AI_TTS_str = ( + "了解,音量已经调大了,当前音量是{}".format(self.VC_TTS_volume) + if llm_style_tts + else "配置成功,当前音量 {}".format(self.VC_TTS_volume) + ) + return 0 + if it == "volume_down": + if llm_style_tts: + try: + print("self.VC_TTS_volume end:{}".format(self.VC_TTS_volume)) + except Exception: + pass + self.VC_TTS_volume -= 10 + if self.VC_TTS_volume < 0: + self.VC_TTS_volume = 0 + self.AI_TTS_str = "音量已经是最小啦" if llm_style_tts else "音量已最小" + else: + self.aud.setVolume(self.VC_TTS_volume // 10) + EventMesh.publish("set_storage_config", ["VC_TTS_volume", self.VC_TTS_volume]) + EventMesh.publish("post_device_state") + EventMesh.publish("adjust_volume_toBle", self.VC_TTS_volume) + self.AI_TTS_str = ( + "好嘞,音量已经调小了,当前音量是{}".format(self.VC_TTS_volume) + if llm_style_tts + else "配置成功,当前音量 {}".format(self.VC_TTS_volume) + ) + return 0 + if it in ("speech_faster", "speech_rate_up"): + self.speech_rate += 10 + if self.speech_rate > 50: + self.AI_TTS_str = "语速已经是最快了" + self.speech_rate = 50 + else: + EventMesh.publish("set_storage_config", ["speech_rate", self.speech_rate]) + self.AI_TTS_str = "明白,语速已经调快了,当前语速是{}".format(self.speech_rate) + return 0 + if it in ("speech_slower", "speech_rate_down"): + self.speech_rate -= 10 + if self.speech_rate < -50: + self.speech_rate = -50 + self.AI_TTS_str = "语速已经是最慢了" + else: + EventMesh.publish("set_storage_config", ["speech_rate", self.speech_rate]) + self.AI_TTS_str = "明白,语速已经调慢了,当前语速是{}".format(self.speech_rate) + return 0 + if it == "vad_faster": + self.vad_silence -= 200 + if self.vad_silence < 100: + self.AI_TTS_str = "已经是最快了" + self.vad_silence = 100 + else: + self.AI_TTS_str = "配置成功,当前语音断句速度是{}毫秒".format(self.vad_silence) + EventMesh.publish("set_storage_config", ["vad_silence", self.vad_silence]) + return 0 + if it == "vad_slower": + self.vad_silence += 200 + if self.vad_silence > 2000: + self.vad_silence = 2000 + self.AI_TTS_str = "已经是最慢了" + else: + self.AI_TTS_str = "配置成功,当前语音断句速度是{}毫秒".format(self.vad_silence) + EventMesh.publish("set_storage_config", ["vad_silence", self.vad_silence]) + return 0 + if it == "query_signal": + signal = net.csqQueryPoll() + self.AI_TTS_str = "当前信号{}".format(signal) + return 0 + if it == "query_battery": + battery = EventMesh.publish("get_battery") + try: + print("电量查询:{}".format(battery)) + except Exception: + pass + self.AI_TTS_str = "电量剩余百分之{}".format(battery) + return 0 + if it == "query_voltage": + Vbatt = Power.getVbatt() + self.AI_TTS_str = "当前电池电压{}".format(Vbatt) + return 0 + if it == "query_iccid": + iccid = sim.getIccid() + if iccid != -1 and len(iccid) > 5: + try: + print("iccid{}".format(iccid)) + except Exception: + pass + self.AI_TTS_str = "当前卡号为:{}".format(iccid) + else: + self.AI_TTS_str = "卡号查询失败" + return 0 + if it == "query_imei": + imei = modem.getDevImei() + if imei != -1 and len(imei) > 5: + try: + print("IMEI:{}".format(imei)) + except Exception: + pass + self.AI_TTS_str = "当前设备号为:{}".format(imei) + else: + self.AI_TTS_str = "设备号查询失败" + return 0 + if it == "query_version": + bt_versions = EventMesh.publish("get_bt_versions") + if bt_versions is None or bt_versions == "": + self.AI_TTS_str = "当前版本号:" + version_util.get_tts_version() + else: + self.AI_TTS_str = "当前版本号:" + version_util.get_tts_version(bt_versions) + return 0 + if it == "query_server": + self.AI_TTS_str = "服务器地址" + self.server_ip + return 0 + if it == "ota_check": + if self.ai_OTA.min_sys_ota() == 1: + self.AI_TTS_str = "有新版本,准备开始升级,升级过程会多次重启设备,请保证流量充足及电量充足" + else: + self.AI_TTS_str = "当前已是最新版本" + return 0 + if it == "factory_mode": + self.factory_mode() + return 1 + if it == "alarm_set": + try: + alarm = ujson.loads(cmd["command"]["params_json"]) + try: + print("params_json:{}".format(cmd["command"]["params_json"])) + except Exception: + pass + add_alarm_tmp = EventMesh.publish("AI_add_alarm", alarm["alarm"]) + if add_alarm_tmp[0] > 0: + self.AI_TTS_str = add_alarm_tmp[1] + return 1 + except Exception as e: + usys.print_exception(e) + try: + self.AI_TTS_str = cmd["command"]["fail_prompt"] + except Exception: + self.AI_TTS_str = "" + return -1 + if it == "alarm_clear": + try: + self.AI_TTS_str = EventMesh.publish("AI_delete_alarm") + except Exception as e: + usys.print_exception(e) + return 0 + if it == "alarm_list": + EventMesh.publish("read_alarm_list") + return 0 + if it == "voice_message_mode": + self.voice_flag = 1 + self.AI_TTS_str = "已进入语音留言模式,长按按钮开始录音,松开按钮结束" + self.Close_flag = 1 + self.stop_voice_timer.stop() + self.stop_voice_timer.start(30 * 1000, 0, self.exit_voice_message_timer) + try: + print("语音留言!!!!!!!!!!") + except Exception: + pass + return 0 + if it == "bt_ble_adv_on": + self.AI_TTS_str = "正在开蓝牙广播" + EventMesh.publish("AI_send_bt_ble_switch_toble", 1) + try: + print("正在开蓝牙广播") + except Exception: + pass + return 0 + if it == "bt_ble_adv_off": + self.AI_TTS_str = "正在关蓝牙广播" + EventMesh.publish("AI_send_bt_ble_switch_toble", 2) + try: + print("正在关蓝牙广播") + except Exception: + pass + return 0 + if it == "bt_classic_on": + self.AI_TTS_str = "正在开经典蓝牙" + EventMesh.publish("AI_send_bt_ble_switch_toble", 3) + try: + print("正在开经典蓝牙") + except Exception: + pass + return 0 + if it == "bt_classic_off": + self.AI_TTS_str = "正在关经典蓝牙" + EventMesh.publish("AI_send_bt_ble_switch_toble", 4) + try: + print("正在关经典蓝牙") + except Exception: + pass + return 0 + if it == "unbind_confirm": + self.AI_TTS_str = "设备解绑中...." + self.Close_flag = 1 + EventMesh.publish("send_unbind_dev_to_iot") + try: + print("解绑设备!!!!!!!!!!!!!!") + except Exception: + pass + return 0 + if it == "clear_earphone": + self.AI_TTS_str = "正在删除" + EventMesh.publish("clear_earphone_record") + try: + print("正在删除") + except Exception: + pass + return 0 + return None + + def check_text_llm(self,cmd): + try: + desc = cmd["command"]["chinese_description"] + if desc.find("切换音色") != -1: + text = cmd["command"]["params_json"] + try: + for key in self.VC_TTS_voice_type: + if text.find(key) != -1: + self.VC_TTS_voice_key = key + EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) + self.AI_TTS_str = "了解,声音已切换" + return 0 + except Exception as e: + usys.print_exception(e) + if text.find("男") != -1: + if "男" in self.VC_TTS_voice_type: + self.VC_TTS_voice_key = "男" + EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) + self.AI_TTS_str = "了解,声音已切换" + return 0 + if text.find("女") != -1: + if "女" in self.VC_TTS_voice_type: + self.VC_TTS_voice_key = "女" + EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) + self.AI_TTS_str = "了解,声音已切换" + return 0 + if text.find("同声") != -1 or text.find("ç«¥") != -1: + if "ç«¥" in self.VC_TTS_voice_type: + self.VC_TTS_voice_key = "ç«¥" + EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) + self.AI_TTS_str = "了解,声音已切换" + return 0 + if text.find("粤") != -1 or text.find("越野") != -1 or text.find("广东话") != -1: + if "粤" in self.VC_TTS_voice_type: + self.VC_TTS_voice_key = "粤" + EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) + self.AI_TTS_str = "了解,声音已切换" + return 0 + if text.find("四川") != -1: + if "四川" in self.VC_TTS_voice_type: + self.VC_TTS_voice_key = "四川" + EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) + self.AI_TTS_str = "了解,声音已切换" + return 0 + if text.find("个性") != -1: + if "个性" in self.VC_TTS_voice_type: + self.VC_TTS_voice_key = "个性" + EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) + self.AI_TTS_str = "了解,声音已切换" + return 0 + if text.find("英语") != -1: + if "英语" in self.VC_TTS_voice_type: + self.VC_TTS_voice_key = "英语" + EventMesh.publish("set_storage_config", ["VC_TTS_voice_key", self.VC_TTS_voice_key]) + self.AI_TTS_str = "了解,声音已切换" + return 0 + self.AI_TTS_str = "很抱歉,没找到对应声音" + return 0 + ret = self._apply_vc_text_intent(desc, cmd, True) + if ret is not None: + return ret + except Exception as e: + usys.print_exception(e) + try: + self.AI_TTS_str = cmd["command"]["fail_prompt"] + except Exception: + self.AI_TTS_str = "" + return -1 + + def check_text(self, text): + print("check_text:len:{}".format(len(text))) + if len(text) > 60: + return -1 + try: + if not isinstance(text, str): + return -1 + ret = self._apply_vc_text_intent(text, None, False) + if ret is not None: + return ret + except Exception as e: + usys.print_exception(e) + return -1 + + #获取trace_id + def get_trace_id(self): + t = utime.mktime(utime.localtime()) + t = t*1000+urandom.randint(0, 1000) + md5_data = "{}".format(t)#md5签名 + sign_data = "" + hash_obj = uhashlib.md5(md5_data) + res = hash_obj.digest() + + # 转成十六进制表示 + sign_data = ubinascii.hexlify(res) + sign_data = sign_data.decode('ascii') + return sign_data + + def get_session_id(self,data = "session_id"): + t = utime.mktime(utime.localtime()) + t = t*1000+urandom.randint(0, 1000) + md5_data = "{}{}".format(data,t)#md5签名 + sign_data = "" + hash_obj = uhashlib.md5(md5_data) + res = hash_obj.digest() + + # 转成十六进制表示 + sign_data = ubinascii.hexlify(res) + sign_data = sign_data.decode('ascii') + return sign_data + + def get_new_message_id(self): + message_id = self.get_trace_id()+"aaa"+self.get_trace_id() + self.VC_message_id=message_id[:32] + return self.VC_message_id + def get_new_task_id(self): + task_id = self.get_trace_id()+"aaa"+self.get_trace_id() + self.VC_task_id=task_id[:32] + return self.VC_task_id + #开始播放声音 + def VC_Play_Start(self): + try: + if self._ptt_should_skip_remote_playback(): + if self._vc_led_state_debug_enabled(): + print("[VC] VC_Play_Start skip (ptt_interrupt)") + return + except Exception: + pass + self.tts_in_data_len = 0 + self.tts_out_data_len = 0 + self._tts_first_write_ms = 0 + if self.VC_Play_flag == 0: + try: + _ptt_fast_open = False + try: + if getattr(self, "VC_mode", -1) == 1: + _ptt_fast_open = True + except Exception: + pass + # 本地提示音/文件播放可能占用同一音频通道,导致 TTS 流播放“写入成功但无声”; + # 在开启 TTS PCM 前先停掉本地播放,避免资源冲突。 + try: + self.aud.stopPlayStream() + self.aud.stopAll() + except Exception: + pass + # 资源互斥:播放前确保录音上行已停止(无 AEC 场景) + try: + if self.record_data_save_flag == 0 and self.VC_record_state_flag != 0: + self._uplink_request_stop("VC_Play_Start") + utime.sleep_ms(30) + except Exception: + pass + if self.VC_play_TYPE == VC_play_TYPE_PCM: + if self.play_pcm == None: + # 刚停采集时延后打开播音 PCM,且失败时重试,降低「pcm open write only fail」 + last_err = None + _open_retries = 1 if _ptt_fast_open else 3 + for i in range(_open_retries): + try: + if not _ptt_fast_open: + utime.sleep_ms(80 + i * 60) + self.play_pcm = audio.Audio.PCM(0, 1, self.VC_play_samplerate, 1, 1, 2) + ret = self.play_pcm.stream_write_start() + print("self.record_pcm.stream_write_start:ret:{}".format(ret)) + last_err = None + break + except Exception as e: + last_err = e + try: + if self.play_pcm != None: + self.play_pcm.close() + except Exception: + pass + self.play_pcm = None + if last_err: + raise last_err + elif self.VC_play_TYPE == VC_play_TYPE_MP3: + pass + self.VC_Play_flag = 1 + try: + if not self._ptt_should_skip_remote_playback(): + self._vc_publish_led("playing", "VC_Play_Start/tts") + except Exception: + pass + except Exception as e: + self.VC_Play_End() + usys.print_exception(e) + #暂停/继续声音播放 Pause_flag = 1暂停 + def VC_Play_Pause(self,Pause_flag): + try: + if self.VC_play_TYPE == VC_play_TYPE_PCM: + if self.play_pcm != None: + self.play_pcm.stream_pause(Pause_flag) + except Exception as e: + self.VC_Play_End() + usys.print_exception(e) + #停止播放声音 + def VC_Play_End(self): + self.VC_Play_flag = 0 + if self.VC_play_TYPE == VC_play_TYPE_PCM: + if self.VC_play_samplerate == 8000: + self.aud.stopPlayStream() + self.aud.stopAll() + elif self.VC_play_samplerate == 16000: + if self.play_pcm != None: + print("Close:play_pcm.close()!!!!!!!!!!!!!!!!!!") + try: + self.play_pcm.stream_stop() + self.play_pcm.close() + except Exception as e: + usys.print_exception(e) + self.play_pcm=None + + self.aud.stopPlayStream() + self.aud.stopAll() + elif self.VC_play_TYPE == VC_play_TYPE_MP3: + self.aud.stopPlayStream() + self.aud.stopAll() + if self.AI_audio_queue.empty()!=True: + self.AI_audio_queue.clear() + + # TTS 播完:按真实 FSM/录音态恢复灯;本地短音不走此路径 + try: + if getattr(self, "_vc_play_end_suppress_idle_led", 0): + if self._vc_led_state_debug_enabled(): + print("[LED] VC_Play_End skip led (suppress)") + elif getattr(self, "VC_record_state_flag", 0) != 0: + self._vc_publish_led("listening", "VC_Play_End/recording") + else: + self._vc_led_sync_from_fsm() + except Exception: + pass + # 关键日志:定位“tts 结束绿灯仍不灭”究竟是 base 态(如 charging/full)还是 playing 未退出 + try: + if "vc_config" in globals() and vc_config and ( + vc_config.get_bool("LED_DEBUG", False) or vc_config.get_bool("KEY_LED_DEBUG", False) or vc_config.get_bool("HW_STATUS_DEBUG", False) + ): + try: + led = EventMesh.publish("get_led_debug_state") + except Exception: + led = None + print("[VC] VC_Play_End led={}".format(led)) + except Exception: + pass + + + def _vc_tts_bytes_per_sec(self): + try: + return int(self.VC_play_samplerate) * 2 + except Exception: + return 32000 + + def _vc_try_play_end_tick(self): + """tts.completed 后判定本地 PCM 是否真正播完,避免过早 VC_Play_End 截断 TTS。""" + try: + if not getattr(self.fsm, "await_played", False): + return False + in_len = getattr(self, "tts_in_data_len", 0) + out_len = getattr(self, "tts_out_data_len", 0) + try: + q_empty = self.AI_audio_queue.empty() + except Exception: + q_empty = True + tts_done_ms = getattr(self.fsm, "last_tts_completed_ms", 0) + if not (in_len > 0 and out_len >= in_len and q_empty and tts_done_ms): + return False + + is_pcm_empty = (self.play_pcm is None) or ( + self.play_pcm is not None and self.play_pcm.stream_is_empty() == 1 + ) + pcm_buf = 0 + if self.play_pcm is not None: + try: + pcm_buf = self.play_pcm.stream_get_buf_len() + except Exception: + pcm_buf = -1 + + now = utime.ticks_ms() + bps = self._vc_tts_bytes_per_sec() + total_ms = int(out_len * 1000 // bps) if bps > 0 else 0 + fw = getattr(self, "_tts_first_write_ms", 0) or tts_done_ms + try: + elapsed_at_done = utime.ticks_diff(tts_done_ms, fw) + except Exception: + elapsed_at_done = 0 + if elapsed_at_done < 0: + elapsed_at_done = 0 + remain_ms = max(400, total_ms - elapsed_at_done) if total_ms > 0 else 400 + try: + since_done = utime.ticks_diff(now, tts_done_ms) + except Exception: + since_done = 0 + tail_ok = since_done >= remain_ms + pcm_ok = is_pcm_empty and (pcm_buf == 0 or pcm_buf == -1) + too_long = since_done > max(remain_ms + 2000, 12000) + + ready = (pcm_ok and tail_ok) or too_long + if not ready: + try: + if not getattr(self, "_tts_play_check_log_ms", 0) or utime.ticks_diff( + now, self._tts_play_check_log_ms + ) >= 800: + self._tts_play_check_log_ms = now + print( + "[TTS] play_end_wait since_done={}ms remain~{}ms total~{}ms " + "in={} out={} q_empty={} pcm_buf={} pcm_empty={}".format( + since_done, remain_ms, total_ms, in_len, out_len, + 1 if q_empty else 0, pcm_buf, 1 if is_pcm_empty else 0, + ) + ) + except Exception: + pass + return False + + try: + print( + "[TTS] play_end_fire since_done={}ms remain~{}ms total~{}ms " + "in={} out={} pcm_buf={} too_long={}".format( + since_done, remain_ms, total_ms, in_len, out_len, pcm_buf, 1 if too_long else 0, + ) + ) + except Exception: + pass + EventMesh.publish("publish_AI_btnInfo", {"func": self.VC_Play_End}) + self._fsm_apply_actions(self.fsm.on_local_play_end()) + try: + self.fsm.reset_rx_phases() + except Exception: + pass + self._tts_first_write_ms = 0 + return True + except Exception as e: + usys.print_exception(e) + return False + + def _1S_timer_cb(self,*argc): + EventMesh.publish("publish_AI_btnInfo",{"func":self._1S_timer_cb_}) + + def _fsm_timeout_table_tick(self, now_ms): + # 兜底统一入口:按“状态机状态 + 超时阈值 + 节流”驱动,避免兜底抢跑/多入口打架。 + # 只在对应 turn/session 状态触发动作。 + try: + turn = getattr(self.fsm, "turn", VCTurnState.Idle) + except Exception: + turn = VCTurnState.Idle + + # 1) synthesis→tts.started 超时:只在 Thinking 期允许清 pending 并重启会话 + # 根因:服务端未返回 tts.started 时,继续“回到聆听/开麦”会把本轮状态机拉扯得更乱; + # 这里直接重启会话,让协议重新进入 started→下一轮流程。 + try: + if turn == VCTurnState.Thinking and self._tts_pending == 1 and self._tts_pending_ms: + try: + if self._fb_last_tts_pending_timeout_ms: + tts_gap_ok = utime.ticks_diff(now_ms, self._fb_last_tts_pending_timeout_ms) > 15000 + else: + tts_gap_ok = True + except Exception: + tts_gap_ok = True + if utime.ticks_diff(now_ms, self._tts_pending_ms) > 8000 and tts_gap_ok: + self._fb_last_tts_pending_timeout_ms = now_ms + print("[VC][tts_pending] timeout, clear pending and restart session") + self._tts_pending = 0 + self._tts_pending_ms = 0 + if self.VC_mode == 0 and self.Close_flag == 0: + try: + self.fsm.reset_rx_phases() + except Exception: + pass + EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_VC_start}) + except Exception: + pass + + # 2) thinking 超时:只在「llm.completed 已收到但 tts.started 长时间未到」时才重启会话 + # 注意:不能以 llm.created 的时间戳作为超时基准,否则会在 LLM 正常推理期间误判超时(notts 场景)。 + try: + if self.VC_mode == 0 and self.Close_flag == 0: + llm_completed_ms = getattr(self, "_llm_completed_ms", 0) + # 只有在 llm.completed 之后才允许超时重启 + if turn == VCTurnState.Thinking and llm_completed_ms and self._turn_has_tts_started == 0: + age_ms = utime.ticks_diff(now_ms, llm_completed_ms) + restart_threshold_ms = 20000 + # _fb_last_thinking_restart_ms==0 时 ticks_diff(now,0) 非预期(间隔被误判极大), + # 会在 llm.completed 后首批 tick 就触发 AI_VC_start → notts(无 tts.started);首次仅信任 age 门限 + try: + if self._fb_last_thinking_restart_ms: + gap_ok = utime.ticks_diff(now_ms, self._fb_last_thinking_restart_ms) > 15000 + else: + gap_ok = True + except Exception: + gap_ok = True + if (age_ms > restart_threshold_ms and gap_ok and + self._tts_pending == 0 and self._need_audio_played_ack == 0 and self.VC_Play_flag == 0): + self._fb_last_thinking_restart_ms = now_ms + self._llm_completed_ms = 0 + self._server_vad_silence = 0 + try: + self.fsm.reset_rx_phases() + except Exception: + pass + try: + print("[VC][thinking_timeout] AI_VC_start age_ms={} thr={}".format( + age_ms, restart_threshold_ms)) + except Exception: + pass + self._vc_led_alert_rb_flash() + # thinking_timeout 策略:重启会话(而不是抢开麦),避免干扰服务端 LLM/TTS 流程 + EventMesh.publish("publish_AI_btnInfo", {"func": self.AI_VC_start}) + except Exception: + pass + + def _1S_timer_cb_(self): + try: + self._vc_tick_serial_cancel_timeout() + except Exception: + pass + + EventMesh.publish("system_sleep_rest")#防止黑屏 + # --- Level 1:仅长对话(VC_mode==0),无业务下行 idle_sec 后结束会话(需再次按键进入) + # close_timerout:保活清除策略见 _vc_rx_resets_long_idle_timer(排除文本 ping/pong、heartbeat 等) + # 与浅休眠/灭屏解耦;单对话不走本计时 + idle_sec = 20 + try: + if "vc_config" in globals() and vc_config: + idle_sec = int(vc_config.get_int("VC_IDLE_AUTO_CLOSE_SEC", 20)) + except Exception: + idle_sec = 20 + if idle_sec < 5: + idle_sec = 5 + if self.VC_mode == 0 and self.VC_satrt_flag == 1 and getattr(self, "AI_client_VC_isconnect", 0) == 1: + self.close_timerout += 1 + else: + self.close_timerout = 0 + if self.VC_mode == 0 and idle_sec > 0 and self.close_timerout > idle_sec: + try: + print("[VC][idle_L1] 长对话无业务下行超时: elapsed={}s thr={}s VC_mode={} VC_satrt={} ws_conn={} {}".format( + self.close_timerout, idle_sec, self.VC_mode, + getattr(self, "VC_satrt_flag", -1), getattr(self, "AI_client_VC_isconnect", -1), + self._vc_fsm_log_snip())) + except Exception: + pass + if self._vc_long_idle_active_speech_or_later(): + try: + print("[VC][idle_L1] 处理=websocket_error(对话中无服务端响应)") + except Exception: + pass + print("[VC][long_idle] branch=speech_no_rsp -> websocket_error + ud_error") + print("接口无响应,超时退出") + EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_error,"data":"接口超时了。请重试"}) + self.ud_error(["VC",self.VC_taskid]) + else: + try: + print("[VC][idle_L1] 处理=Close(结束对话,需再次按键进入)") + except Exception: + pass + print("[VC][long_idle] branch=idle_timeout -> Close") + EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) + #EventMesh.publish("sleep_BT")#休眠蓝牙 + + # --- Level 2:长对话+单对话,业务 idle 累计 VC_IDLE_DEEP_SLEEP_SEC → 断开(若仍连)+ 深睡事件 + deep_sec = 60 + deep_en = 1 + try: + if "vc_config" in globals() and vc_config: + deep_sec = int(vc_config.get_int("VC_IDLE_DEEP_SLEEP_SEC", 60)) + deep_en = int(vc_config.get_int("VC_IDLE_DEEP_SLEEP_ENABLE", 1)) + except Exception: + deep_sec, deep_en = 60, 1 + active_deep = bool(deep_en) and deep_sec > 0 + if active_deep and deep_sec < 10: + deep_sec = 10 + if not active_deep: + self._idle_deep_sec = 0 + elif not self._vc_is_business_idle(): + self._idle_deep_sec = 0 + try: + self._vc_idle_deep_armed_suppress = 0 + except Exception: + pass + elif getattr(self, "_vc_idle_deep_armed_suppress", 0): + pass + else: + self._idle_deep_sec += 1 + if active_deep and self._idle_deep_sec > deep_sec: + try: + print("[VC][idle_L2] 业务idle累计超时: elapsed={}s thr={}s VC_mode={} VC_satrt={} ws_conn={}".format( + self._idle_deep_sec, deep_sec, self.VC_mode, + getattr(self, "VC_satrt_flag", -1), getattr(self, "AI_client_VC_isconnect", -1))) + except Exception: + pass + try: + self._vc_idle_deep_armed_suppress = 1 + except Exception: + pass + self._idle_deep_sec = 0 + EventMesh.publish("publish_AI_btnInfo", {"func": self._vc_idle_deep_sleep_impl}) + + # 统一兜底表(按状态机状态触发) + try: + self._fsm_timeout_table_tick(utime.ticks_ms()) + except Exception: + pass + + # (不做额外兜底)动态切换依赖 FSM 状态正确清理后,由 server.tts.completed→local.play_end 驱动自动开麦 + + # print("get_audio_state !!!!!{} {} {}\r\n".format(self.play_end,EventMesh.publish("get_audio_state"),self.play_flag)) + # ===== 播放结束检测:用 FSM 驱动 play_end 动作链 ===== + try: + self._vc_try_play_end_tick() + except Exception: + pass + if self.online_kws == 1: + if self.VC_satrt_kws_flag == 1 and self.VC_satrt_flag == 0:#要打开录音上传数据实现kws + pass + + # thinking/llm→tts 等兜底已在 _fsm_timeout_table_tick 内统一处理 + # VC WebSocket 文本 ping:间隔由 default_config 的 VC_WS_PING_INTERVAL_SEC 控制(秒),<=0 表示关闭 ping + try: + _ping_iv = int(vc_config.get_int("VC_WS_PING_INTERVAL_SEC", 4)) + except Exception: + _ping_iv = 4 + if _ping_iv > 0: + self.VC_pingping_timerout += 1 + if self.VC_pingping_timerout >= _ping_iv: + self.VC_pingping_timerout = 0 + self.AI_client_VC_ping() + # self.asr_pingping_timerout += 1 + # if self.asr_pingping_timerout>3: + # self.asr_pingping_timerout=0 + # self.AI_client_asr_ping() + # 历史遗留:VC_Thinking_timerout 的重启通道会在 llm.created 后抢跑触发 AI_VC_start, + # 直接打断本轮 LLM→TTS(notts 日志:llm.created 后立刻 AI_VC_start,导致无 tts.started)。 + # 现在统一由 _fsm_timeout_table_tick(Thinking 超时) 负责重启会话,因此禁用该旧通道。 + if self.VC_Thinking_timerout > 0 and self.VC_Thinking_timerout < 100: + self.VC_Thinking_timerout = 100 + # def AI_kws_start(self): + # print("AI_kws_start") + # EventMesh.publish("wakeup_BT") + # EventMesh.publish("system_sleep_rest")#防止黑屏 + # url="ws://118.145.158.207:22080/wakeup/ws" + # self.AI_client_kws = uwebsocket.Client.connect(url, debug=True) + # self.AI_client_VC_recv_id = _thread.start_new_thread(self.AI_client_VC_recv, (self.AI_client_VC,)) + # self.VC_Speech_Start()#开启线程获取录音数据 + #VC开始 + def AI_VC_start(self): + print("AI_VC_start1") + + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + self.VC_satrt_flag = 1 + self.VC_stop_play_audio_flag = 1 + self.VC_satrt_kws_flag = 1 + if self.VC_Thinking_timerout < 100: + self.VC_Thinking_timerout = 0 + # 不在这里提前切灯态:播放/聆听灯只在“播放/录音真正开始/停止”处更新 + EventMesh.publish("wakeup_BT") + #self.VC_Play_End()#关闭音频播放 + print("AI_VC_start2") + #self.aud.aud_tone_play(16,200)#播放按键音 + self.LocalRespondingEnded_timerout = 0 + self.close_timerout = 0 + self._idle_deep_sec = 0 + self._vc_idle_deep_armed_suppress = 0 + + # + if self.AI_client_VC_isconnect == 0:#没连接的时候请求开始 + self.websocket_connect_VC()#连接服务器 + + #utime.sleep_ms(50) + else: + print("AI_VC_start2 (already connected) {}".format(self._vc_fsm_log_snip())) + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + if self.AI_client_VC_isconnect == 0: + #self.Close() + return + # 单对话 PTT:会话已 started 时勿重复发 Start(否则第二轮起上行/ASR 易乱) + _ptt_skip_start = False + try: + if self.VC_mode == 1 and getattr(self, "_vc_session_started", 0) == 1: + _ptt_skip_start = True + print("[PTT] AI_VC_start skip duplicate Start (session active)") + except Exception: + pass + if not _ptt_skip_start: + self.AI_client_VC_start_requset() + print("AI_VC_start3") + + if (not _ptt_skip_start) and self.AI_TTS_str == "" and self.AI_new_voice_url == "": + self._uplink_request_start("AI_VC_start") + + + # pass + print("AI_VC_start4") + + #self.VC_Play_Start() + #EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Play_Start}) + #开始线程 + + # if self.AI_VC_send_data_queue_thread_id == 0: + # _thread.stack_size(self.Audio_buf_size+8*1024) + # self.AI_VC_send_data_queue_thread_id = _thread.start_new_thread(self.AI_VC_send_data_queue_thread, ()) + # _thread.stack_size(8*1024) + # print("AI_VC_start :AI_VC_send_data_queue_thread_id :{} ".format(self.AI_VC_send_data_queue_thread_id)) + + if self.AI_audio_queue_thread_id ==0: + _thread.stack_size(24*1024) + self.AI_audio_queue_thread_id = _thread.start_new_thread(self.AI_audio_queue_thread, ()) + _thread.stack_size(8*1024) + print("AI_VC_start :AI_audio_queue_thread_id :{} ".format(self.AI_audio_queue_thread_id)) + else: + print("AI_VC_start :AI_audio_queue_thread_id != 0 ") + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + self._1S_timer.start(1000,1,self._1S_timer_cb) + # self.VC_pingping_timerout = 0 + # self.asr_pingping_timerout = 0 + print("AI_VC_start5") + #开始会话 + def AI_client_VC_start_requset(self): + voice="zh_male_wennuanahu_moon_bigtts" + try: + voice = self.VC_TTS_voice_type[self.VC_TTS_voice_key]["voice"] + except Exception as e: + usys.print_exception(e) + #voice="zh_female_dongmanxiaoyang_moon_bigtts" + token = EventMesh.publish("get_dev_token") + if token ==None: + token = "" + # bot_option:优先从 default_config(usr 可覆盖)读取 VC_BOT_ID / VC_WORKFLOW_ID,允许空字符串 + _def_bot_id = "7462617881076351002" + _def_workflow_id = "7462621757327196187" + try: + if "vc_config" in globals() and vc_config: + _vc_bot_id = vc_config.get_attr("VC_BOT_ID", _def_bot_id) + _vc_workflow_id = vc_config.get_attr("VC_WORKFLOW_ID", _def_workflow_id) + if _vc_bot_id is None: + _vc_bot_id = "" + if _vc_workflow_id is None: + _vc_workflow_id = "" + else: + _vc_bot_id, _vc_workflow_id = _def_bot_id, _def_workflow_id + except Exception: + _vc_bot_id, _vc_workflow_id = _def_bot_id, _def_workflow_id + json_data = \ + { + "header": { + "event": "Start", + "trace_id": self.get_trace_id(), + }, + "payload": { + "simple_response": True,#不返回llm过程文本 + "device_id": modem.getDevImei(), + + "user_agent": "AudioClient/1.0", + "dialog_attributes": { + #"provider": "doubao", + # "provider": "coze", + + "cfg": { + "disable_empty_prompt" : True,#拦截空字符请求大模型 + "custom_variables": { + "name": self.bot_name, + "devToken":token, + }, + "greeting":{ + "enabled":False, + "content":"" + }, + "openai_option":{ + }, + + "bot_option": { + "use_workflow": True, + #"input_format": "text", + "bot_id": _vc_bot_id, + "workflow_id": _vc_workflow_id, + + } + } + }, + "outbound_attributes": { + "provider": "volcengine", + "cfg": { + "format": "pcm", + "sample_rate": self.VC_play_samplerate, + "bit_rate": 16, + "channel": 1, + "volume": self.VC_TTS_volume, + "emotion": self.emotion, + #"voice": "7426720361732980745", + "voice": voice,#音色 + "speech_rate": self.speech_rate,#语速 + "chunk_interval_ms": 10, # 配置每段音频入队间隔时间 100ms + } + }, + "inbound_attributes": { + # "provider": "xun-fei",# + "provider": "volcengine", + "disable_vad":True, + "vad_silence":self.vad_silence, + "interrupt_options":{ + "concatenate_results":True, + "thinking_time_only": True, + #"keyword_interrupt": false, // 启用打断词功能 (打断词只有在思考以及之后才能打断) + #"interrupt_keywords": ["停止", "打断","打断一下", "等一下", "别说了", "打住"] // 配置打断词列表 + }, + "cfg": { + "format": "pcm", + "sample_rate": self.VC_record_samplerate, + "bit_rate": 16, + "channel": 1, + "enable_punctuation": True, + "enable_itn":True, + "keywords":[] + } + }, + "commands":self.commands, + } + } + # "bot_id":"7481164663863459850",#鸭子 + # "workflow_id":"7480485093845991443", + # "bot_id":"7462617881076351002",#语言助手 + # "workflow_id":"7462621757327196187", + # "bot_id":"7469964258592931852",#深圳百事通 + # "workflow_id":"7469968083063603241", + # "bot_id":"7411071327182438412",#柒牌 + # "workflow_id":"7469049680291610636", + # if self.online_kws == 1:#打开 + # json_data["payload"]["inbound_attributes"]["interrupt_options"]["keyword_interrupt"] = True + # json_data["payload"]["inbound_attributes"]["interrupt_options"]["interrupt_keywords"] = ["你好,柒先生","你好"] + #如果 + json_data["payload"]["outbound_attributes"]["provider"]=self.outbound_attributess_provider#TTS音色服务选择,火山 + json_data["payload"]["inbound_attributes"]["provider"]=self.inbound_attributess_provider#ASR服务选择,火山 + #优先选择self.VC_TTS_voice_type[self.VC_TTS_voice_key]里得参数 + # if "inbound_attributess_provider" in self.VC_TTS_voice_type[self.VC_TTS_voice_key]: + # if len(self.VC_TTS_voice_type[self.VC_TTS_voice_key]["inbound_attributess_provider"])>0: + # json_data["payload"]["inbound_attributes"]["provider"]=self.VC_TTS_voice_type[self.VC_TTS_voice_key]["inbound_attributess_provider"] + # print("inbound_attributess_provider是VC_TTS_voice_type里的:{}".format(self.VC_TTS_voice_type[self.VC_TTS_voice_key]["inbound_attributess_provider"])) + if "outbound_attributess_provider" in self.VC_TTS_voice_type[self.VC_TTS_voice_key]: + if len(self.VC_TTS_voice_type[self.VC_TTS_voice_key]["outbound_attributess_provider"])>0: + json_data["payload"]["outbound_attributes"]["provider"]=self.VC_TTS_voice_type[self.VC_TTS_voice_key]["outbound_attributess_provider"] + print("outbound_attributess_provider是VC_TTS_voice_type里的:{}".format(self.VC_TTS_voice_type[self.VC_TTS_voice_key]["outbound_attributess_provider"])) + + # 根据配置设置默认 bot/workflow,并允许 VC_TTS_voice_type 覆盖 + try: + if "vc_config" in globals() and vc_config: + opt = vc_config.get_attr("DIALOG_BOT_OPTION", None) + if isinstance(opt, dict): + if "bot_id" in opt: + json_data["payload"]["dialog_attributes"]["cfg"]["bot_option"]["bot_id"] = opt.get("bot_id") or "" + if "workflow_id" in opt: + json_data["payload"]["dialog_attributes"]["cfg"]["bot_option"]["workflow_id"] = opt.get("workflow_id") or "" + if "use_workflow" in opt: + json_data["payload"]["dialog_attributes"]["cfg"]["bot_option"]["use_workflow"] = bool(opt.get("use_workflow")) + except Exception: + pass + + try: + # voice 配置可覆盖 bot_id/workflow_id/openai_option + cur = self.VC_TTS_voice_type[self.VC_TTS_voice_key] + if isinstance(cur, dict): + if cur.get("bot_id"): + json_data["payload"]["dialog_attributes"]["cfg"]["bot_option"]["bot_id"] = cur.get("bot_id") + if "workflow_id" in cur: + json_data["payload"]["dialog_attributes"]["cfg"]["bot_option"]["workflow_id"] = cur.get("workflow_id") or "" + if cur.get("openai_option"): + json_data["payload"]["dialog_attributes"]["cfg"]["openai_option"] = cur.get("openai_option") + except Exception: + pass + + try: + _bo = json_data["payload"]["dialog_attributes"]["cfg"]["bot_option"] + if not _bo.get("bot_id", "") or not _bo.get("workflow_id", ""): + _bo["use_workflow"] = False + except Exception: + pass + try: + if "vc_config" in globals() and vc_config: + p = vc_config.get_attr("DIALOG_PROVIDER_DEFAULT", None) + if p: + json_data["payload"]["dialog_attributes"]["provider"] = p + except Exception: + pass + + try: + if "vc_config" in globals() and vc_config and vc_config.get_bool("KEYWORDS_FROM_VOICE_TYPE", False): + keywords = [] + for key in self.VC_TTS_voice_type: + keywords.append(key) + json_data["payload"]["inbound_attributes"]["cfg"]["keywords"] = keywords + except Exception: + pass + if self.dialog_attributes_provider != None: + json_data["payload"]["dialog_attributes"]["provider"] = self.dialog_attributes_provider + if self.commands_mode !=0 : + json_data["payload"]["commands"] = [] + if self.VC_mode == 0 :#and dev_type != "7PIN_BK_coze":#长对话模式打开vad + json_data["payload"]["inbound_attributes"]["disable_vad"] = False + # 新会话开始:在收到服务端 started 前,禁止发送音频帧(避免 session not initialized) + self._vc_session_started = 0 + try: + self.fsm.on_session_start_request() + except Exception: + pass + self.AI_client_VC_send(ujson.dumps(json_data)) + try: + _tid = json_data.get("header", {}).get("trace_id", "") + print("[VC][proto] Start enqueued trace_id={} VC_mode={} disable_vad={} ws_conn={}".format( + _tid or "-", + self.VC_mode, + json_data.get("payload", {}).get("inbound_attributes", {}).get("disable_vad", "?"), + self.AI_client_VC_isconnect)) + except Exception: + pass + + #开始上传语音 + def AI_client_VC_send_speech(self): + json_data = \ + { + "header": { + "event": "audio.create", + + }, + } + + if self.VC_mode == 1:#单对话模式 + # 协议门控(可用 VC_PROTO_GUARDS 临时关闭) + if (not VC_PROTO_GUARDS) or (self._vc_session_started == 1): + self._ptt_audio_created = 1 + try: + print("[VC][proto] audio.create send session_started={} proto_guards={}".format( + self._vc_session_started, VC_PROTO_GUARDS)) + except Exception: + pass + self.AI_client_VC_send(ujson.dumps(json_data)) + else: + try: + print("[VC][proto] audio.create SKIP session_started={} VC_PROTO_GUARDS={} " + "(等对端 started 后再开麦)".format(self._vc_session_started, VC_PROTO_GUARDS)) + except Exception: + pass + + + + + + + try: + self.fsm.on_server_asr_started() + except Exception: + pass + #结束上传语音 + def AI_client_VC_end_speech(self): + json_data = \ + { + "header": { + "event": "audio.completed", + + }, + } + self.AI_client_VC_send(ujson.dumps(json_data)) + self._ptt_audio_created = 0 + EventMesh.publish("publish_AI_btnInfo",{"func":self._uplink_request_stop, "data":"audio.completed"}) + + + #客户端打断对话 + def AI_client_VC_RequestToSpeak(self): + json_data = \ + { + "event": "cancel", + "payload": {} + } + print("AI_client_VC_RequestToSpeak!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") + # 模式切换等场景 play_tts(..., is_close_VC=1) 会置 Close_flag=1;用户再按 AI 键走 cancel→canceled→ + # uplink_start 时,FSM 与 _uplink_request_start 均会因 Close_flag 拒绝开麦,表现为“切回长对话后按键无上行”。 + try: + if self.VC_mode == 0: + self.Close_flag = 0 + self._vc_pending_interrupt_uplink = 1 + self._vc_clear_stale_record_state("RequestToSpeak") + except Exception: + pass + # 只做“打断+清播放队列”,不要在这里关音频队列线程(会导致后续 TTS/提示音不稳定,且触发竞态) + EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_init}) + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_VC_send,"data":ujson.dumps(json_data)}) + utime.sleep_ms(1) + try: + self.fsm.reset_rx_phases() + except Exception: + pass + # #self.Close() + # self.AI_VC_start() + #TTS播放 + def AI_client_VC_synthesis(self): + # 幂等保护:在上一条 synthesis 还未等到 tts.started 前,不重复发(防止多兜底/多入口重复触发) + try: + if self._tts_pending == 1: + return + except Exception: + pass + json_data = \ + { + "event": "synthesis", + "payload": { + "text": self.AI_TTS_str + } + } + # 语义标记只用于“是否自动开麦”的策略。 + # 当前策略:长对话不自动开麦(需按键开始),因此这里不对提示音做自动开麦标记。 + self.AI_TTS_str = "" + # 标记为“等待 tts.started”,避免在 synthesis→tts.started 窗口误开下一轮录音上行 + self._tts_pending = 1 + try: + self._tts_pending_ms = utime.ticks_ms() + except Exception: + self._tts_pending_ms = 0 + try: + self.fsm.on_local_synthesis_sent() + except Exception: + pass + print("AI_client_VC_synthesis!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") + # if self.VC_state == VC_State_RequestToRespond: + # print("AI_client_VC_RequestToRespond 重复 tts_str:{}\nasr_str:{}".format(self.AI_TTS_str,self.AI_ASR_str)) + self.AI_client_VC_send(ujson.dumps(json_data)) + # 金三 V2.5:AudioPlayed — 端侧 TTS 播报结束后上报,服务端进入下一轮对话(轻度重同步,不断开 WebSocket) + def AI_client_VC_audio_played(self): + # V2.5:播完上报;与 VC_PROTO_GUARDS 无关(见文件头说明) + try: + if self.AI_client_VC_isconnect != 1 or self.AI_client_VC == None: + return + json_data = {"event": "audio.played", "payload": {}} + print("AI_client_VC_audio_played") + self.AI_client_VC_send(ujson.dumps(json_data)) + except Exception as e: + usys.print_exception(e) + def kws_even(self,event = None,msg= None): + EventMesh.publish("publish_AI_btnInfo",{"func":self.kws_even_cb}) + def kws_even_cb(self): + if EventMesh.publish("get_sos_flag") == 1: + return + print("kws_even_cb") + if self.VC_mode == 1: + try: + tts_str = vc_config.get_attr("KWS_PTT_HINT", "请您按住圆形的按钮开始讲话,讲完松开即可 ") + except Exception: + tts_str = "请您按住圆形的按钮开始讲话,讲完松开即可 " + EventMesh.publish("play_tts",[tts_str,1]) + return + if self.VC_mode == 0: + if EventMesh.publish("get_voice_record_flag") == 1:#停止静默录音 + if EventMesh.publish("get_voice_record_mode") == 2:#语音留言不能被打断 + return + elif EventMesh.publish("get_voice_record_mode") == 1: #静默录音需要打断 + EventMesh.publish("stop_voice_record")#停止静默录音 + self.aud.aud_tone_play(10,400) + # EventMesh.publish("audio_montage_play",["chenggong.mp3"]) + self.Close() + # if dev_type == "7PIN" : + # self.aud.aud_tone_play(10,400)#播放按键音 + #self.aud.aud_tone_play(10,200)#播放按键音 + self.AI_TTS_str = ""#"你好,有什么吩咐" + self.AI_ASR_str = "" + self.AI_ASR_str_tmp = "" + self._uplink_request_stop("kws_even_cb") + self._uplink_request_start("kws_even_cb") + self.AI_VC_start() + + def set_vad_silence(self,event = None,msg= None): + self.vad_silence = msg + EventMesh.publish("set_storage_config",["vad_silence",self.vad_silence]) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.play_new_voice_cb,"data":msg}) + #设置tts参数 + def set_wait_first_flag(self,event = None,msg= None):#设置未读语音播报初次记录flag + self.wait_first_flag = msg + def set_VC_TTS_voice_type(self,event = None,msg= None): + self.VC_TTS_voice_type["APP_voice"] = msg + #self.VC_TTS_voice_type[self.VC_TTS_voice_key] + self.VC_TTS_voice_key = "APP_voice" + EventMesh.publish("set_storage_config",["VC_TTS_voice_type",self.VC_TTS_voice_type]) + EventMesh.publish("set_storage_config",["VC_TTS_voice_key",self.VC_TTS_voice_key]) + print("[VC] TTS_voice key={} map={}".format(self.VC_TTS_voice_key, self.VC_TTS_voice_type)) + #修改音量大小 msg为1则为加大,msg为0则为减小,返回1成功,返回0失败 + def change_volume(self,event =None, msg =None): + if msg == 0 : + self.VC_TTS_volume -= 10 + if self.VC_TTS_volume < 0: + self.VC_TTS_volume = 0 + return 0 + else: + self.aud.setVolume(self.VC_TTS_volume //10) + EventMesh.publish("set_storage_config",["VC_TTS_volume",self.VC_TTS_volume]) + EventMesh.publish("post_device_state") + EventMesh.publish("adjust_volume_toBle",self.VC_TTS_volume) + return 1 + elif msg == 1: + self.VC_TTS_volume += 10 + if self.VC_TTS_volume > 100: + self.VC_TTS_volume = 100 + return 0 + else: + self.aud.setVolume(self.VC_TTS_volume //10) + EventMesh.publish("set_storage_config",["VC_TTS_volume",self.VC_TTS_volume]) + EventMesh.publish("post_device_state") + EventMesh.publish("adjust_volume_toBle",self.VC_TTS_volume) + return 1 + elif msg >= 100:#修改为具体音量 + if msg > 200: + self.VC_TTS_volume = 100 + return 0 + else: + self.VC_TTS_volume = msg - 100#还原数据 + self.aud.setVolume(self.VC_TTS_volume //10) + EventMesh.publish("set_storage_config",["VC_TTS_volume",self.VC_TTS_volume]) + EventMesh.publish("post_device_state") + EventMesh.publish("adjust_volume_toBle",self.VC_TTS_volume) + return 1 + #调节聆听快慢(以秒为单位) + def NLP_set_vad_silence(self,event,msg): + if msg == 1:#聆听变快 + self.vad_silence -= 1000 + if self.vad_silence < 1000: + AI_TTS_str = "已经是最快了" + self.vad_silence = 1000 + else: + AI_TTS_str = "配置成功,当前语音断句速度是{}秒".format(self.vad_silence/1000) + elif msg == 0:#聆听变慢 + self.vad_silence += 1000 + if self.vad_silence > 4000: + self.vad_silence = 4000 + #EventMesh.publish("audio_montage_play",["yinliangyizuixiao.mp3"]) + #self.aud.play(1,0,'U:/static/yinliangyizuixiao.mp3')#音量已最小 + AI_TTS_str = "已经是最慢了" + else: + AI_TTS_str = "配置成功,当前语音断句速度是{}秒".format(self.vad_silence/1000) + EventMesh.publish("set_storage_config",["vad_silence",self.vad_silence]) + return AI_TTS_str + #查询指令判断 + def find_cmd(self,event ,msg): + if "电量" in msg: + battery = EventMesh.publish("get_battery") + print("电量查询:{}".format(battery)) + tts_play_str = "电量剩余百分之{}".format(battery) + EventMesh.publish("play_tts",[tts_play_str,0]) + # print("电量!!!!!!!!!!!") + elif "信号" in msg: + signal = net.csqQueryPoll() + tts_play_str = "当前信号{}".format(signal) + EventMesh.publish("play_tts",[tts_play_str,0]) + # print("信号!!!!!!!!!!!") + elif "卡号" in msg: + # print("卡号!!!!!!!!!!!") + iccid = sim.getIccid() + iccid = num_str_to_chinese(iccid)#转为中文 + if iccid != -1 and len(iccid) > 5: + print("iccid{}".format(iccid)) + tts_play_str = "当前卡号为:{}".format(iccid) + else: + tts_play_str = "卡号查询失败" + EventMesh.publish("play_tts",[tts_play_str,0]) + elif "设备号" in msg: + imei = modem.getDevImei() + if imei != -1 and len(imei) > 5: + print("IMEI:{}".format(imei)) + tts_play_str = "当前设备号为:{}".format(imei) + else: + tts_play_str = "设备号查询失败" + EventMesh.publish("play_tts",[tts_play_str,0]) + elif "音量" in msg : + tts_play_str = "当前音量是{}".format(self.VC_TTS_volume) + EventMesh.publish("play_tts",[tts_play_str,0]) + elif "语速" in msg : + tts_play_str = "当前语速是{}".format(EventMesh.publish("find_rate")) + EventMesh.publish("play_tts",[tts_play_str,0]) + + #改变音色判断 + def change_voice(self,event ,msg): + voice = msg + # if "男声" in msg or "普通话" in msg: + # self.VC_TTS_voice_key = "男" + # else: + # self.VC_TTS_voice_key = voice + self.outbound_attributess_provider = "xun-fei" + if "男声" in msg or "普通话" in msg: + self.VC_TTS_voice_key = "男" + self.outbound_attributess_provider = "volcengine" + elif "女声" in msg: + self.VC_TTS_voice_key = "女" + self.outbound_attributess_provider = "volcengine" + elif "童声" in msg: + self.VC_TTS_voice_key = "ç«¥" + self.outbound_attributess_provider = "volcengine" + elif "香港粤语" in msg: + self.VC_TTS_voice_key = "香港粤语" + elif "四川话" in msg: + self.VC_TTS_voice_key = "四川" + elif "上海话" in msg: + self.VC_TTS_voice_key = "上海" + elif "安徽合肥话" in msg: + self.VC_TTS_voice_key = "安徽合肥" + elif "山东话" in msg: + self.VC_TTS_voice_key = "山东话" + elif "陕西话" in msg: + self.VC_TTS_voice_key = "陕西" + elif "广东话" in msg: + self.VC_TTS_voice_key = "广东" + elif "成都话" in msg: + self.VC_TTS_voice_key = "成都" + elif "湖南话" in msg: + self.VC_TTS_voice_key = "湖南" + elif "苏州话" in msg: + self.VC_TTS_voice_key = "苏州" + elif "东北话" in msg: + self.VC_TTS_voice_key = "东北" + elif "河南话" in msg: + self.VC_TTS_voice_key = "河南" + elif "湖北话" in msg: + self.VC_TTS_voice_key = "湖北" + elif "闽南话" in msg: + self.VC_TTS_voice_key = "闽南" + elif "内蒙古话" in msg: + self.VC_TTS_voice_key = "内蒙古话" + EventMesh.publish("set_storage_config",["VC_TTS_voice_key",self.VC_TTS_voice_key]) + tts_play_str = "声音已改为{}".format(voice) + EventMesh.publish("play_tts",[tts_play_str,0]) + + #闹钟的url播放接口 + def play_single_voice(self,event = None,msg= None): + EventMesh.publish("publish_AI_btnInfo",{"func":self.play_single_voice_cb,"data":msg}) + def play_single_voice_cb(self,data): + try: + new_voice_url = data[0] #音频地址 + is_close_VC = data[1] + if new_voice_url != "": + self.AI_new_voice_url = new_voice_url + self.alarm_play_flag = 1 + if is_close_VC != None: + self.Close_flag = is_close_VC + if self.AI_VC_send_data_queue.empty()!=True: + self.AI_VC_send_data_queue.clear() + if self.AI_ASR_send_data_queue.empty()!=True: + self.AI_ASR_send_data_queue.clear() + EventMesh.publish("set_led_state", {"ai_state": "thinking"}) + self.AI_ASR_send_buf_len = 0 + self._uplink_request_stop("play_single_voice_cb") + self.websocket_deconnect_VC() + self.AI_VC_start() + self.play_new_voice_flag = 1 + # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + except Exception as e: + usys.print_exception(e) + pass + #语音留言 + def play_new_voice(self,event = None,msg= None): + EventMesh.publish("publish_AI_btnInfo",{"func":self.play_new_voice_cb,"data":msg}) + def play_new_voice_cb(self,data): + try: + new_voice_url = data[0] #音频地址 + is_close_VC = data[1] + if new_voice_url != "": + self.recv_voice = 1#收到留言 + print("已收到最新留言!!!!!") + # EventMesh.publish("set_motor_time",[1,1,5,8,4])#设置马达参数 + self.AI_new_voice_url = new_voice_url + # if self.pwk_cnt_voice == 0 and self.pwk_notice_flag == 0:#并且不在多条留言的状态 + # EventMesh.publish("play_tts",["您有一条语音留言,10秒内按下中间键可以收听",1]) + if self.pwk_notice_flag == 0 and EventMesh.publish("get_app_new_voice_flag"):#不处于播放状态并且是小程序下发的语音而不是请求的 + self.pwk_cnt_voice += 1#加上当前这条 + EventMesh.publish("set_storage_config",["is_noread_voice",1]) + EventMesh.publish("set_app_new_voice_flag",0) + print("voice_add_to = {}".format(self.pwk_cnt_voice)) + print("self.pwk_notice_flag = {}".format(self.pwk_notice_flag)) + if self.pwk_cnt_voice >1: + if self.pwk_notice_flag == 0: + tts_str = "您有" + str(self.pwk_cnt_voice) + "条未读语音留言,10秒内按下中间键可以收听" + EventMesh.publish("play_tts",[tts_str,1]) + elif self.pwk_cnt_voice == 1: + if self.pwk_notice_flag == 0: + tts_str = "您有1条语音留言,10秒内按下中间键可以收听" + EventMesh.publish("play_tts",[tts_str,1]) + if is_close_VC != None: + self.Close_flag = is_close_VC + if self.AI_VC_send_data_queue.empty()!=True: + self.AI_VC_send_data_queue.clear() + if self.AI_ASR_send_data_queue.empty()!=True: + self.AI_ASR_send_data_queue.clear() + EventMesh.publish("set_led_state", {"ai_state": "thinking"}) + self.AI_ASR_send_buf_len = 0 + self._uplink_request_stop("play_new_voice_cb") + self.websocket_deconnect_VC() + self.AI_VC_start() + self.play_new_voice_flag = 1 + # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + except Exception as e: + usys.print_exception(e) + pass + #历史留言查看 + def play_his_voice(self,event = None,msg= None): + EventMesh.publish("publish_AI_btnInfo",{"func":self.play_his_voice_cb,"data":msg}) + def play_his_voice_cb(self,data): + try: + new_voice_url = data[0] #音频地址 + is_close_VC = data[1] + time = data[2] + tts_play_str = time + "收到" + EventMesh.publish("play_tts",[tts_play_str,1]) + if new_voice_url != "": + self.recv_voice = 1#收到留言 + print("历史留言查看") + # tts_play_str = "收到" + # EventMesh.publish("play_tts",[tts_play_str,1]) + # EventMesh.publish("set_motor_time",[1,1,5,8,4])#设置马达参数 + self.AI_new_voice_url = new_voice_url + if is_close_VC != None: + self.Close_flag = is_close_VC + if self.AI_VC_send_data_queue.empty()!=True: + self.AI_VC_send_data_queue.clear() + if self.AI_ASR_send_data_queue.empty()!=True: + self.AI_ASR_send_data_queue.clear() + EventMesh.publish("set_led_state", {"ai_state": "thinking"}) + self.AI_ASR_send_buf_len = 0 + self._uplink_request_stop("play_his_voice_cb") + self.websocket_deconnect_VC() + self.AI_VC_start() + self.play_new_voice_flag = 1 + # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + except Exception as e: + usys.print_exception(e) + pass + #等待未读留言查看 + def play_wait_voice(self,event = None,msg= None): + EventMesh.publish("publish_AI_btnInfo",{"func":self.play_wait_voice_cb,"data":msg}) + def play_wait_voice_cb(self,data): + try: + new_voice_url = data[0] #音频地址 + is_close_VC = data[1] + print("是否为第一次:{}".format(self.wait_first_flag)) + if self.wait_first_flag == 0: + tts_play_str = "您有" + str(len(EventMesh.publish("get_play_new_voice_list"))) + "条未听留言,10秒内按下中间键可以收听" + EventMesh.publish("play_tts",[tts_play_str,1]) + # self.wait_first_timer.start(1000,1,self.noread_wait_vocie) + self.wait_first_flag = 1 + elif self.wait_first_flag == 1: + self.wait_sort += 1 + self.AI_TTS_str = "第"+str(self.wait_sort)+"条" + if new_voice_url != "": + self.recv_voice = 1#收到留言 + print("未读留言查看") + self.AI_new_voice_url = new_voice_url + if is_close_VC != None: + self.Close_flag = is_close_VC + if self.AI_VC_send_data_queue.empty()!=True: + self.AI_VC_send_data_queue.clear() + if self.AI_ASR_send_data_queue.empty()!=True: + self.AI_ASR_send_data_queue.clear() + EventMesh.publish("set_led_state", {"ai_state": "thinking"}) + self.AI_ASR_send_buf_len = 0 + self._uplink_request_stop("play_wait_voice_cb") + self.websocket_deconnect_VC() + self.AI_VC_start() + self.play_new_voice_flag = 1 + # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + except Exception as e: + usys.print_exception(e) + pass + def noread_wait_vocie(self,*args):#如果一直没读 + self.wait_first_cnt += 1 + print("未读留言等待收听中:{}".format(self.wait_first_cnt)) + if EventMesh.publish("get_recv_voice") == 1:#如果检测到留言播放 + self.wait_first_cnt = 0 + self.wait_first_timer.stop() + if self.wait_first_cnt == 15: + EventMesh.publish("set_wait_first_flag",0) + EventMesh.publish("set_history_voice_flag",0) + EventMesh.publish("set_wait_voice_flag",0) + self.wait_first_cnt = 0 + self.wait_first_timer.stop() + #音乐播放 + def play_music(self,event = None,msg= None): + EventMesh.publish("publish_AI_btnInfo",{"func":self.play_music_cb,"data":msg}) + def play_music_cb(self,data): + try: + music_name = data[0] + new_voice_url = data[1] #音频地址 + is_close_VC = data[2] + if new_voice_url != "": + self.AI_new_voice_url = new_voice_url + self.music_play_flag = 1#表示音乐播放开启 + EventMesh.publish("play_tts",["下面为您播放歌曲{}".format(music_name),1]) + if is_close_VC != None: + self.Close_flag = is_close_VC + if self.AI_VC_send_data_queue.empty()!=True: + self.AI_VC_send_data_queue.clear() + if self.AI_ASR_send_data_queue.empty()!=True: + self.AI_ASR_send_data_queue.clear() + EventMesh.publish("set_led_state", {"ai_state": "thinking"}) + self.AI_ASR_send_buf_len = 0 + self._uplink_request_stop("play_music_cb") + self.websocket_deconnect_VC() + self.AI_VC_start() + self.play_new_voice_flag = 1 + # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + except Exception as e: + usys.print_exception(e) + + #AI翻译接收TTS + def play_trans_tts(self,event = None,msg= None): + EventMesh.publish("publish_AI_btnInfo",{"func":self.play_trans_tts_cb,"data":msg}) + def play_trans_tts_cb(self,data): + try: + new_voice_url = data #音频地址 + if new_voice_url != "": + self.AI_new_voice_url = new_voice_url + if self.AI_VC_send_data_queue.empty()!=True: + self.AI_VC_send_data_queue.clear() + if self.AI_ASR_send_data_queue.empty()!=True: + self.AI_ASR_send_data_queue.clear() + EventMesh.publish("set_led_state", {"ai_state": "thinking"}) + self.AI_ASR_send_buf_len = 0 + self._uplink_request_stop("play_trans_tts_cb") + self.websocket_deconnect_VC() + self.AI_VC_start() + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_client_new_voice})#播放留言 + self.play_new_voice_flag = 1 + # EventMesh.publish("publish_AI_btnInfo",{"func":self.VC_Speech_End}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.websocket_deconnect_VC}) + # EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_VC_start}) + except Exception as e: + usys.print_exception(e) + def AI_client_new_voice(self): #上传未读的语音地址/包括音频地址也可以 + json_data = \ + { + "event": "audio.convert", + "payload": { + "audio_convert_request": { + "audio_url": self.AI_new_voice_url, + "bit_depth": 16, + "channels": 1, + "sample_rate": 16000, + "target_format": "pcm" + } + } + } + self.AI_new_voice_url = "" + print("AI_client_new_voice!!!!!!!!!!!!!!!!!!!!!!!!!!!!!{}".format(json_data)) + self.AI_client_VC_send(ujson.dumps(json_data)) + + + def chenggong_tone(self): + EventMesh.publish("audio_montage_play",["chenggong.mp3"]) + # self.aud.aud_tone_play(10,400) + def send_tone(self): + EventMesh.publish("audio_montage_play",["send_tone.mp3"]) + def concurrency_limit_exceeded_tone(self): + self.Close() + EventMesh.publish("audio_montage_play",["chaobingfatis.mp3"]) #播放超并发提醒 + #给服务器回应ack + + + ############################################ + #关闭音频播放线程 + def AI_audio_queue_thread_close(self): + print("AI_audio_queue_thread_close1!!!!!!!!!!!!!!!!!!!!!!") + if self.AI_audio_queue_thread_id != 0: + count =500 + while True: + count -= 1 + if self.AI_audio_queue_thread_lock == 0 or count <=0: + break + else: + self.AI_audio_queue_thread_lock = 2 + print("AI_audio_queue_thread going:{}...".format(count)) + utime.sleep_ms(1) + self.AI_audio_queue_thread_lock = 0 + print("AI_audio_queue_thread_close2!!!!!!!!!!!!!!!!!") + if self.AI_audio_queue_thread_id != 0: + try: + _thread.stop_thread(self.AI_audio_queue_thread_id) + except Exception as e: + usys.print_exception(e) + self.AI_audio_queue_thread_id = 0 + # 音频队列 + def AI_audio_queue_thread(self): + print("AI_audio_queue_thread start") + count = 0 + if self.AI_audio_queue.empty()!=True: + self.AI_audio_queue.clear() + print("音频数据还有剩余!!!!!!!!!!!!") + while True: + try: + if self.AI_audio_queue_thread_lock == 2 or self.VC_stop_play_audio_flag == 0: + print("AI_audio_queue_thread 该退出了") + break + # + count+=1 + if count%500 == 1 and VC_HEARTBEAT_LOG: + print("AI_audio_queue_thread on") + utime.sleep_ms(1) + #print("AI_audio_queue_thread") + if self.AI_audio_queue.empty() : + self.AI_audio_queue_thread_lock = 0 + + + else: + self.AI_audio_queue_thread_lock = 1 + data = self.AI_audio_queue.get() + self.close_timerout = 0 + if self.play_flag == 0: + self.play_flag =1 + #self.AI_client_tts_data_len=len(data) + #print("audio_queue:len:{}".format(len(data[0]))) + + if len(data[0]) > 10: + try: + if self._ptt_should_drop_inbound_tts(): + continue + except Exception: + pass + for i in range(5): + if self.VC_Play_flag == 0: + self.VC_Play_Start() + utime.sleep_ms(2) + else: + break + # 打断后 PCM 偶发未打开:加重试,避免「队列有数据、play_pcm=None、无声」 + if self.VC_play_TYPE == VC_play_TYPE_PCM and self.play_pcm is None and self.VC_stop_play_audio_flag == 1: + for j in range(4): + if self.VC_Play_flag == 0: + self.VC_Play_Start() + utime.sleep_ms(30 + j * 35) + if self.play_pcm is not None: + break + try: + if self.VC_play_TYPE == VC_play_TYPE_PCM: + # if self.VC_play_samplerate == 8000: + # tmp= self.aud.playStream(1,data[0] ) + # else: + write_buf = bytearray(data[0]) + #print("play_pcm.stream_write1 len:{}".format(len(write_buf))) + if self.play_pcm != None: + #print("play_pcm.stream_write !!!") + ret = self.play_pcm.stream_write(write_buf,len(write_buf)) + #ret= self.aud.playStream(1, data[0])#播放音频流 + if self.tts_out_data_len == 0: + try: + self._tts_first_write_ms = utime.ticks_ms() + except Exception: + self._tts_first_write_ms = 0 + try: + sid = getattr(self, "_cur_sid", "") + did = getattr(self, "_cur_dialog_id", "") + except Exception: + sid, did = "", "" + print("[TTS] first_write ret={} len={} sid={} did={}".format( + ret, len(write_buf), sid, did + )) + if ret <= 0: + print("[TTS] stream_write ret={} len={} (no audio?)".format(ret, len(write_buf))) + self.tts_out_data_len += ret + try: + if getattr(self.fsm, "await_played", False) and self.AI_audio_queue.empty(): + self._vc_try_play_end_tick() + except Exception: + pass + #print("play_pcm.stream_write2:{}".format(self.tts_out_data_len)) + elif self.VC_play_TYPE == VC_play_TYPE_MP3: + tmp= self.aud.playStream(3, data[0])#播放音频流 + + if tmp != 0: + print("2playStream:error tmp:{} data len:{}!!!!!!!!!!!!".format(tmp,len(data[0]))) + # # #播放失败的时候填充WAV头 + # data_len = len(data[0]) + # data_len_b = data_len.to_bytes(4,'little')#数据长度转16进制数组 + # h_data = [ 0x52 ,0x49 ,0x46 ,0x46 #,0x24 ,0x00 ,0x00 ,0x00 #文件标识、文件大小 + # ,0x57 ,0x41 ,0x56 ,0x45 ,0x66 ,0x6D ,0x74 ,0x20 #文件格式、格式块标识符 + # ,0x10 ,0x00 ,0x00 ,0x00 ,0x01 ,0x00 ,0x01 ,0x00 ,0x80 ,0x3E ,0x00 ,0x00 #格式块大小4、音频格式2、声道数2、采样率4 + # ,0x80 ,0x3E ,0x00 ,0x00 ,0x02 ,0x00 ,0x10 ,0x00 ,0x64 ,0x61 ,0x74 ,0x61]#每秒字节数4、数据块对齐2、位深度2、data4 + # h_data_b = bytes(h_data) + # file_len = data_len+len(h_data_b)+4+4 + # file_len_b = file_len.to_bytes(4,'little')#文件长度转16进制数组 + # h_data_b = h_data_b[:4]+file_len_b+h_data_b[4:]+data_len_b + # #self.aud.playStream(2, h_data_b)#播放音频流 + # tmp= self.aud.playStream(2, h_data_b+data[0])#播放音频流 + # print("3playStream:tmp:{} !!!!!!!!!!!!".format(tmp)) + + + mem = gc.mem_free() + #print('1剩余内存RAM:{} KB'.format(mem / 1024)) + except Exception as e: + print("AI_audio_queue_thread:ERROR:") + usys.print_exception(e) + + # if self.VC_state == VC_State_TTSEnd and self.AI_audio_queue.empty() :#self.AI_audio_queue为空且tts下发结束 + # #通知服务器播放结束 + # self.AI_client_VC_LocalRespondingEnded() + except Exception as e: + print("AI_audio_queue_thread:ERROR:") + usys.print_exception(e) + self.AI_audio_queue_thread_lock = 0 + self.AI_audio_queue_thread_id = 0 + print("AI_audio_queue_thread:exit") + def factory_mode(self): + print("factory_mode") + EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) + EventMesh.publish("publish_AI_btnInfo",{"func":self.factory_start}) + def factory_start(self): + self.factory_timer.start(3000, 1, self.factory_timer_cb) + def factory_timer_cb(self, *args, **kwargs): + print("factory_timer_cb play......") + #self.aud.play(1,0,'U:/static/peizhichenggong.mp3')#切换成功 + EventMesh.publish("audio_montage_play",["xinhaoruo1.mp3"]) + def Close_VC(self,event,msg): + EventMesh.publish("publish_AI_btnInfo",{"func":self.Close}) + def Close(self): + try: + if getattr(self, "VC_mode", 0) != 1: + print("[VC][long_idle] Close() enter VC_mode={} {} VC_satrt_flag={} VC_record_state_flag={}".format( + getattr(self, "VC_mode", -1), self._vc_fsm_log_snip(), + getattr(self, "VC_satrt_flag", -1), getattr(self, "VC_record_state_flag", -1))) + except Exception: + pass + self.VC_stop_play_audio_flag = 0 + try: + self.AI_key_long_press_flag = 0 + except Exception: + pass + self.VC_Thinking_timerout = 0 + self.AI_ASR_timer.stop() + self._1S_timer.stop() + self.close_timerout = 0 + self._idle_deep_sec = 0 + self.factory_timer.stop() + self.AI_TTS_str = "" + self.AI_ASR_str = "" + self.AI_ASR_str_tmp = "" + self.AI_ASR_flag = 0 + self.AI_ASR_check_timeout = 0 + self.AI_ASR_error_timeout = 0 + self.LocalRespondingEnded_timerout = 0 + self._need_audio_played_ack = 0 + self._tts_pending = 0 + self._tts_pending_ms = 0 + self._vc_session_started = 0 + self._ptt_audio_created = 0 + self._vc_pending_interrupt_uplink = 0 + try: + self._skip_started_uplink_once = 0 + except Exception: + pass + try: + self._ptt_abort_interrupt_arm() + except Exception: + pass + # Power 短按停止对话后,再次按 AI 需要新建 dialog_id:清理本轮 dialog/session 标识 + try: + self._cur_dialog_id = "" + except Exception: + pass + try: + self._cur_sid = "" + except Exception: + pass + self.is_ruppt_tts = 0#打断标志位清零 + print("ai_VC Close1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") + try: + if getattr(self, "VC_mode", 0) != 1: + print("[VC][long_idle] step=set_led_state_idle (first)") + except Exception: + pass + EventMesh.publish("set_led_state", {"ai_state": "idle"}) + self.tts_in_data_len = 0 + self.tts_out_data_len = 0 + #必须先销毁线程,不然线程会容易报错 + + try: + self.AI_audio_queue_thread_close()#销毁音频线程 + except Exception as e: + usys.print_exception(e) + try: + self.AI_VC_send_data_queue_thread_close()#销毁数据发送线程 + except Exception as e: + usys.print_exception(e) + + # Close 可能被不同线程触发:统一切回主线程执行 stop + try: + if getattr(self, "VC_mode", 0) != 1: + print("[VC][long_idle] step=schedule_uplink_stop reason=Close (async publish_AI_btnInfo)") + except Exception: + pass + EventMesh.publish("publish_AI_btnInfo",{"func":self._uplink_request_stop, "data":"Close"}) + + print("ai_VC Close1.1!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") + + print("ai_VC Close1.2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") + + try: + if getattr(self, "VC_mode", 0) != 1: + print("[VC][long_idle] step=websocket_deconnect_VC") + except Exception: + pass + try: + self.websocket_deconnect_VC() + except Exception as e: + usys.print_exception(e) + + + + + + print("ai_VC Close1.3!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") + + print("ai_VC Close2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") + + + + + if self.record_data_save_flag == 1: + if self.record_fd : + self.record_fd.close() + self.record_fd = None + if self.play_data_save_flag == 1: + if self.play_fd !=None: + self.play_fd.close() + self.play_fd = None + print("ai_VC Close3!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n") + + if self.AI_VC_send_data_queue.empty()!=True: + self.AI_VC_send_data_queue.clear() + self.play_new_voice_flag = 0 #语音消息播放状态 + if self.voice_choose_flag == 0: # 如果没有未读语音 + self.AI_new_voice_url = "" # 语音播放地址 + print("关闭并重置语音标志位") + else: + print("关闭但是没有重置语音标志位") + self.AI_ASR_send_buf_len = 0 + #self.AI_WS_connect_Queue.put(self.AI_client_VC) + try: + if getattr(self, "VC_mode", 0) != 1: + print("[VC][long_idle] step=set_led_state_idle (second) -> VC_init VC_satrt_flag=0") + except Exception: + pass + EventMesh.publish("set_led_state", {"ai_state": "idle"}) + self.VC_init() + self.VC_satrt_flag = 0 + # Level2:会话结束后仍可继续 1s 节拍累计 idle 直至深睡(依赖 VC_IDLE_DEEP_SLEEP_SEC) + try: + _dsec = 60 + _den = 1 + if "vc_config" in globals() and vc_config: + _dsec = int(vc_config.get_int("VC_IDLE_DEEP_SLEEP_SEC", 60)) + _den = int(vc_config.get_int("VC_IDLE_DEEP_SLEEP_ENABLE", 1)) + if _den and _dsec > 0: + self._1S_timer.start(1000, 1, self._1S_timer_cb) + except Exception: + pass + # try: + # while True: + # if self.AI_WS_connect_Queue.empty()!=True: + # AI_WS_connect = self.AI_WS_connect_Queue.get() + # if AI_WS_connect : + # try: + # AI_WS_connect._close() + # AI_WS_connect = None + # except Exception as e: + # usys.print_exception(e) + # else: + # print("websocket链接已为空") + # else: + # break + # except Exception as e: + # usys.print_exception(e) + + def VC_init2(self): + self.VC_init() + + print("ai_VC VC_init2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!") + try: + self.websocket_deconnect_VC() + except Exception as e: + usys.print_exception(e) + print("ai_VC Close3!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\r\n") + self._1S_timer.stop() + if self.AI_VC_send_data_queue.empty()!=True: + self.AI_VC_send_data_queue.clear() + + self.AI_ASR_send_buf_len = 0 + if self.AI_audio_queue.empty()!=True: + self.AI_audio_queue.clear() + def VC_init(self): + _q0 = self.AI_audio_queue.size() + self.VC_Play_End() + if self.AI_audio_queue.empty()!=True: + self.AI_audio_queue.clear() + print("[VC] init audio_queue {} -> {}".format(_q0, self.AI_audio_queue.size())) + # num = EventMesh.publish("get_volum_size") + # if num == None: + # num = 8 + # self.aud.setVolume(num)#恢复原始音量 diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_storage.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_storage.py new file mode 100644 index 00000000..b26d1a2f --- /dev/null +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_storage.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +"""持久化配置读写。""" +from common import Abstract +import EventMesh +import ujson +import ql_fs + +class getStorageDataManager(Abstract):#获取内存数据 + def __init__(self): + self.config_data = {} + check = ql_fs.path_exists("/usr/static/storage_file/config.json") + if check == True: + with open("/usr/static/storage_file/config.json","r") as fd: + self.config_data = ujson.load(fd) + else : + self.config_data = {'old_time': 0, 'flip_sw': 0} + pass + + def post_processor_after_instantiation(self): + EventMesh.subscribe("set_storage_data", self.set_storage_data) + EventMesh.subscribe("set_storage_config", self.set_storage_config) + EventMesh.subscribe("get_storage_data", self.get_storage_data) + EventMesh.subscribe("get_storage_config", self.get_storage_config) + + def set_storage_data(self, event, msg): + # EventMesh.debug_log("set_storage_data:{}".format(msg)) + if msg != None: + self.config_data = msg + with open("/usr/static/storage_file/config.json",'w+',encoding="utf-8") as fd: + ujson.dump(self.config_data,fd) + + + def set_storage_config(self,event,msg): + # EventMesh.debug_log("set_storage_key_data1:{}".format(msg)) + if len(msg) >= 2: + self.config_data[msg[0]]=msg[1] + # EventMesh.debug_log("set_storage_key_data2:{}".format(self.config_data[msg[0]])) + EventMesh.publish("publish_message_event_ui",{"func":self.set_storage_save,"data":0}) + def set_storage_save(self, data): + with open("/usr/static/storage_file/config.json",'w+',encoding="utf-8") as fd: + ujson.dump(self.config_data,fd) + def get_storage_data(self, event, msg): + # EventMesh.debug_log("get_storage_data:{}".format(self.config_data)) + return self.config_data + def get_storage_config(self,event,msg): + # EventMesh.debug_log("get_storage_key_data1:{}".format(msg)) + b = msg in self.config_data.keys() + if b == True: + # EventMesh.debug_log("get_storage_key_data2:{}".format(self.config_data[msg])) + return self.config_data[msg] + else : + return None + # try: + # return self.config_data[msg] + # except: + # EventMesh.debug_log("get_storage_key_data:none key:{}",msg) + # return None diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_uart.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_uart.py new file mode 100644 index 00000000..597b7970 --- /dev/null +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_uart.py @@ -0,0 +1,792 @@ +# -*- coding: utf-8 -*- +"""UART 与蓝牙侧通道。""" +try: + from qp_log import qp_print as print +except Exception: + pass +import usys +import ujson +import utime +import osTimer +import _thread +import base64 +import modem +import request +import constants +import EventMesh +from queue import Queue +from machine import UART +try: + import vc_config +except Exception: + vc_config = None + +#HEARDER+TOTALLEN+DATELEN+DATA +HEARDER = "FFFE" +TOTALLEN = "00000000" +DATELEN = "0000" +DATA = {} +class AI_UART(): + #串口命令通道 + def __init__(self): + self.send_count=0 + self.AI_uart = UART(UART.UART4, 230400, 8, 0, 1, 0) + self.AI_uart.set_callback(self.uart_call) + + EventMesh.subscribe("bt_ota_run",self.bt_ota_run)# + EventMesh.subscribe("start_ota",self.start_ota) + EventMesh.subscribe("end_ota",self.end_ota) + EventMesh.subscribe("send_bt_switch_toble",self.send_bt_switch_toble) + EventMesh.subscribe("send_ble_switch_toble",self.send_ble_switch_toble) + EventMesh.subscribe("send_ble_scan",self.send_ble_scan) + EventMesh.subscribe("send_connect_bt_mac",self.send_connect_bt_mac) + EventMesh.subscribe("send_disconnect_bt_mac",self.send_disconnect_bt_mac) + EventMesh.subscribe("send_imei_to_ble",self.send_imei_to_ble) + EventMesh.subscribe("send_get_connect_bt_state",self.send_get_connect_bt_state) + EventMesh.subscribe("recv_bt_connect_rest",self.recv_bt_connect_rest) + EventMesh.subscribe("AI_send_bt_ble_switch_toble",self.AI_send_bt_ble_switch_toble) + EventMesh.subscribe("get_bt_versions",self.get_bt_versions) + EventMesh.subscribe("adjust_volume_toBle",self.adjust_volume_toBle) + EventMesh.subscribe("clear_earphone_record",self.clear_earphone_record) + self.bt_switch = 0 + self.ble_switch = 0 + self.uart_timer = osTimer() + self.get_bt_connect_timer = osTimer() + self.get_bt_connect_flag = 0 + self.uart_timer_count = 0 + self.bt_ota_flag = 0 + self.read_ack_flag = 0 + self.ping_json_str = {} + self.ping_json_str_tmp ="" + self.read_uart_data = "" + self.bt_versions="" + try: + uart_ok = False + if "vc_config" in globals() and vc_config: + uart_ok = vc_config.uart_supported() + except Exception: + uart_ok = False + if uart_ok: + self.uart_send_queue = Queue(100) + self.uart_send_queue_thread_id = _thread.start_new_thread(self.uart_send_queue_thread, ()) + + self.uart_recv_queue = Queue(100) + self.uart_recv_queue_thread_id = _thread.start_new_thread(self.uart_recv_queue_thread, ()) + self.send_get_versions()#获取蓝牙版本 + + def uart_send_queue_thread(self): + while True: + data = self.uart_send_queue.get() + + #EventMesh.debug_log("Info_queue:data = {}".format(data)) + if data != None: + try: + if len(data) != 0: + print("send_queu_uart_data2:{} data:{}".format(data,data["data"])) + # json_str =ujson.dumps(data) + if data["event"] == "uart_send_data": + print("send_queu_uart_data sucess data:{} {}".format(data["data"],type(data["data"]))) + self.uart_queue_send_data(data["data"]) + if "resend_flag" in data: + print("send_reset_cb:{} {}".format(data["resend_flag"],data["ACK_time"])) + if data["resend_flag"] == 1: + self.send_count = 5#重试5次 + ack_timer = 0 #单次最大等待时间 + count = 0 + if "ACK_time" in data: + if data["ACK_time"] == None: + ack_timer = 100#最大重发5次,没ack或者返回error重发 + else: + ack_timer = data["ACK_time"] + if ack_timer > 0: + while self.send_count: + + if self.send_count <=0 or self.read_ack_flag == 1: + print("4G串口重发机制退出:{} self.read_ack_flag:{} json_str:{}".format(self.send_count,self.read_ack_flag,data["data"])) + break + if count >= ack_timer: + count = 0 + print("串口异常,重发:") + self.uart_queue_send_data(data["data"]) + self.send_count-=1 + count += 1 + utime.sleep_ms(1) + + except Exception as e: + usys.print_exception(e) + utime.sleep_ms(10) + + def uart_recv_queue_thread(self): + while True: + data = self.uart_recv_queue.get() + + #EventMesh.debug_log("Info_queue:data = {}".format(data)) + if data != None: + try: + if len(data) != 0: + print("recv_queu_uart_data2:{}".format(data)) + # json_str =ujson.dumps(data) + # queue_data={"event":"uart_recv_data"} + if data["event"] == "uart_recv_data": + print("recv_queu_uart_data sucess data:{}".format(type(data["event"]))) + self.pares_uart_data() + except Exception as e: + usys.print_exception(e) + + def send_imei_to_ble(self,event,msg): + EventMesh.publish("publish_AI_btnInfo",{"func":self.send_imei_to_ble_cb}) + + def send_imei_to_ble_cb(self): + self.read_ack_flag=0 + imei = modem.getDevImei() + json_data = \ + { + + "event": "imei", + "data":str(imei) + + } + json_str =ujson.dumps(json_data) + print("send imei:{}".format(json_str)) + self.send_data(json_str) + pass + def send_connect_bt_mac(self,event,msg): + self.read_ack_flag=0 + json_data = \ + { + "event": "connect_bt", + "data":msg + } + json_str =ujson.dumps(json_data) + self.send_data(json_str,1,2000) + # self.send_reset_cb(json_str,0,2000) #每2s发送一次 10ms为单位 + pass + def send_disconnect_bt_mac(self,event,msg): + self.read_ack_flag=0 + json_data = \ + { + "event": "disconnect_bt", + "data":msg + } + json_str =ujson.dumps(json_data) + self.send_data(json_str,1) + # self.send_reset_cb(json_str) + + pass + def uart_timer_cb(self, *args, **kwargs): + self.uart_timer_count+=1 + if self.uart_timer_count >40: + self.read_uart_data = "" + if self.uart_timer_count >200: + self.read_uart_data = "" + self.uart_timer.stop() + def split_combined_json(self,combined_str): + # 将连续的JSON字符串分割为多个独立的JSON字符串列表 + # + # 参数: + # combined_str: 多个JSON拼接的字符串 + # 返回: + # 分割后的 + json_list = [] + start_index = 0 + brace_count = 0 # 用于匹配{}括号 + print("combined_str:{}".format(combined_str)) + for i, char in enumerate(combined_str): + if char == '{': + brace_count += 1 + elif char == '}': + brace_count -= 1 + + # 当括号完全匹配(count=0)时,说明一个完整的JSON对象结束 + if brace_count == 0: + # 提取从start_index到当前位置的子串(包含当前}) + json_str = combined_str[start_index:i+1].strip() + if json_str: # 避免空字符串 + json_list.append(json_str) + start_index = i + 1 # 下一个JSON的起始位置 + print("json_list{}".format(json_list)) + + return json_list + + def uart_call(self,para): + queue_data={"event":"uart_recv_data"} + + self.uart_recv_queue.put(queue_data) + # print("uart_call start") + # EventMesh.publish("publish_AI_btnInfo",{"func":self.pares_uart_data}) + # print("uart_call end") + #print("ai_uart:{}".format(para)) + #[{"event":""},{},{}] + def pares_uart_data(self): + uart_data = self.AI_uart.read() + self.uart_timer.start(10, 1, self.uart_timer_cb) + self.uart_timer_count = 0 + ack_flag = 0#是否要返回ack + print("uart_data:{}",uart_data) + uart_data_str = uart_data.decode('utf-8') + + self.read_uart_data = self.read_uart_data +uart_data_str + try: + ujson.loads(self.read_uart_data) + except Exception as e: + print("json数据不完整:self.read_uart_data{}".format(self.read_uart_data)) + usys.print_exception(e) + return + self.uart_timer.stop() + self.uart_timer_count = 0 + self.playload_data(self.read_uart_data) + + # 分割JSON + # json_strings = self.split_combined_json(uart_data_str) + + # # 逐个解析并处理 + # for i, json_str in enumerate(json_strings, 1): + # print("第{}个JSON{}".format(i,json_str)) + # try: + # data = ujson.loads(json_str) + + # # 在这里添加你的业务逻辑,比如根据event处理不同事件 + # self.uart_timer.stop() + # self.uart_timer_count = 0 + # self.playload_data(self.read_uart_data) + # # if data.get("event") == "bt_connect_single": + + # # elif data.get("event") == "bt_scan_rest": + + # except Exception as e: + # print("JSON解析错误:原始字符串:") + # print("-" * 50) + + + + # print("uart_data_str:{}",uart_data_str) + # if len(uart_data_str) < (HEARDER+1+1): + # return + # head = uart_data_str[0:4] + # pack_flag = uart_data_str[4:5] + # recvflag = uart_data_str[5:6] + + + # if head == HEARDER: + # if pack_flag == "1": #分包数据 + # if recvflag == "1": #未发送完成 + # self.ping_json_str_tmp+=uart_data_str[6:] + # pass + # else: + # self.playload_data(self.ping_json_str_tmp) + # pass + # else: + # self.playload_data(uart_data_str[6:]) + + # else: + # print("uart is not FFFE:") + + def playload_data(self,msg): + ack_flag=0 + try: + print("playload_data:{}".format(msg)) + jsload_data = ujson.loads(msg) + #print("AI_client_VC_recv jsload_data = {}".format(jsload_data)) + + if jsload_data["event"] == "AI_KEY_PRESS" :#AI按键按下 + EventMesh.publish("publish_AI_btnInfo",{"func":self.AI_key_press}) + ack_flag = 1 + elif jsload_data["event"] == "BT_AI_KEY_PRESS" :#蓝牙耳机key ai对话模式不支持长按对话模式 只支持短按按键对话模式 重新定义按键 + EventMesh.publish("publish_AI_btnInfo",{"func":self.BT_AI_key_press}) + ack_flag = 1 + elif jsload_data["event"] == "AI_PWR_PRESS" :#电源按键按下 + EventMesh.publish("publish_AI_btnInfo",{"func":self.pwk_key_release}) + ack_flag = 1 + elif jsload_data["event"] == "bt_scan_rest":#扫描列表 #蓝牙停止扫描后主动向4g发送扫描列表需要发送ack + EventMesh.publish("send_bt_scan_list",jsload_data) + ack_flag=1 + elif jsload_data["event"]=="bt_connect_single": #收到蓝牙主动发送过来的单个连接蓝牙耳机的连接状态 需要发送ack + EventMesh.publish("save_bt_connect_list",jsload_data) + ack_flag=1 + + elif jsload_data["event"]=="need_send_bt_connect_list": #发送连接蓝牙连接记录列表 不需要发送ack + self.get_bt_connect_timer.stop() + self.get_bt_connect_flag = 0 + EventMesh.publish("save_bt_connect_list",jsload_data) #先保存连接记录再发送连接耳机列表to IOT + EventMesh.publish("send_bt_connect_list") + + elif jsload_data["event"]=="get_ble_switch": + ble_switch_flag = EventMesh.publish("get_storage_config","switch_ble_state")#ble开关 不需要发送ack + if ble_switch_flag == None: + ble_switch_flag = "1" + EventMesh.publish("send_ble_switch_toble",str(ble_switch_flag)) + + pass + elif jsload_data["event"]=="get_bt_switch": + bt_switch_flag = EventMesh.publish("get_storage_config","switch_bt_state")#bt开关 不需要发送ack + if bt_switch_flag == None: + bt_switch_flag = "1" + EventMesh.publish("send_bt_switch_toble",bt_switch_flag) + + pass + elif jsload_data["event"]=="get_cat1_imei": #蓝牙主动向4G获取imei 4g不需要发送ack 蓝牙收到imei需要回ack给4改 + EventMesh.publish("send_imei_to_ble") + ack_flag=1 + pass + elif jsload_data["event"] == "post_bt_connect_rest": #蓝牙主动蓝牙耳机连接状态 需要发送ack给蓝牙 + EventMesh.publish("recv_bt_connect_rest",jsload_data) + ack_flag=1 + + elif jsload_data["event"] == "clear_earphone_record_ok":#清除蓝牙耳机连接记录 + tts_play_str = "删除成功" + EventMesh.publish("play_tts",[tts_play_str,1]) + self.read_ack_flag = 1 + + elif jsload_data["event"] == "versions": + self.bt_versions = jsload_data["data"] + print("当前蓝牙版本是:{}".format(self.bt_versions)) + self.read_ack_flag = 1 + + # elif jsload_data["event"] == "kws" :# + # EventMesh.publish("kws_even") + elif jsload_data["event"] == "ack" :# + self.read_ack_flag = 1 + if ack_flag == 1: + self.send_ack() + self.read_uart_data = "" + except Exception as e: + usys.print_exception(e) + self.read_uart_data = "" + def recv_bt_connect_rest(self,event,msg): + date_tmp = utime.localtime() + time_tmp = utime.mktime(date_tmp) + if "data" in msg: + EventMesh.publish("send_bt_connect_state_to_iot",msg) + pass + def get_bt_versions(self,event=None,msg=None): + return self.bt_versions + def clear_earphone_record(self,event,msg): #清楚所有耳机记录 + + json_data = \ + { + "event": "clear_earphone_record", + } + json_str =ujson.dumps(json_data) + print("clear_earphone_record{}".format(json_str)) + self.send_data(json_str,1) + def adjust_volume_toBle(self,event,msg): # + self.read_ack_flag=0 + volume = int(msg) + if volume <= 10: + volume = 10 + json_data = \ + { + "event": "adjust_volume", + "data":str(volume) + } + json_str =ujson.dumps(json_data) + print("adjust_volume_toBle{}".format(json_str)) + self.send_data(json_str,1) + + def AI_send_bt_ble_switch_toble(self,event,msg): + # // { + # // "event":"switch_bt_state", + # // "data":{ + # // "switch_bt_state":"0" + # // } + # // } #格式 + + self.read_ack_flag=0 + if msg == 1: #AI对话开启蓝牙 + json_data = \ + { + + "event": "switch_ble_state", + "data":{ + "switch_ble_state":"1" + } + + } + json_str =ujson.dumps(json_data) + print("switch_ble_state{}".format(json_str)) + self.send_data(json_str,1) + + elif msg == 2: #AI对话开启蓝牙 + json_data = \ + { + + "event": "switch_ble_state", + "data":{ + "switch_ble_state":"0" + } + + } + json_str =ujson.dumps(json_data) + print("switch_ble_state{}".format(json_str)) + self.send_data(json_str,1) + elif msg == 3: #AI对话开启蓝牙 + json_data = \ + { + + "event": "switch_bt_state", + "data":{ + "switch_bt_state":"1" + } + } + json_str =ujson.dumps(json_data) + print("switch_bt_state{}".format(json_str)) + self.send_data(json_str,1) + + elif msg == 4: #AI对话开启蓝牙 + json_data = \ + { + + "event": "switch_bt_state", + "data":{ + "switch_bt_state":"0" + } + + } + json_str =ujson.dumps(json_data) + print("switch_bt_state{}".format(json_str)) + self.send_data(json_str,1) + + def send_bt_switch_toble(self,event,msg): + EventMesh.publish("publish_AI_btnInfo",{"func":self.send_bt_switch_toble_cb,"data":msg}) + def send_bt_switch_toble_cb(self,data): + # // { + # // "event":"switch_bt_state", + # // "data":{ + # // "switch_bt_state":"0" + # // } + # // } #格式 + self.read_ack_flag=0 + json_data = \ + { + + "event": "switch_bt_state", + "data":{ + "switch_bt_state":str(data) + } + + } + json_str =ujson.dumps(json_data) + print("send_bt_switch_toble{}".format(json_str)) + self.send_data(json_str,1) + + def send_ble_switch_toble(self,event,msg): + EventMesh.publish("publish_AI_btnInfo",{"func":self.send_ble_switch_toble_cb,"data":msg}) + def send_ble_switch_toble_cb(self,data): + # // { + # // "event":"switch_ble_state", + # // "data":{ + # // "switch_ble_state":"0" + # // } + # // } #格式 + + self.read_ack_flag=0 + json_data = \ + { + "event": "switch_ble_state", + "data":{ + "switch_ble_state":str(data) + } + } + json_str =ujson.dumps(json_data) + print("send_ble_switch_toble{}".format(json_str)) + self.send_data(json_str,1) + + # 重发机制 + def send_reset_cb(self,json_str,need_resend = None,timeOut=None): + print("send_reset_cb:{} {} {}".format(json_str,need_resend,timeOut)) + if timeOut == None: + self.send_count = 100#最大重发5次,没ack或者返回error重发 + else: + self.send_count = timeOut + + while self.send_count: + self.send_count-=1 + utime.sleep_ms(10) + if self.send_count <=0 or self.read_ack_flag == 1: + print("4G串口重发机制退出:{} self.read_ack_flag:{} json_str:{}".format(self.send_count,self.read_ack_flag,json_str)) + break + if self.send_count%(self.send_count//10) == 0: + print("串口异常,重发:") + self.send_data(json_str) + + pass + def send_get_connect_bt_state(self,event,msg): + json_data = \ + { + + "event": "get_connect_bt_state" + } + json_str =ujson.dumps(json_data) + print("send_get_connect_bt_state{}".format(json_str)) + self.send_data(json_str) + self.get_bt_connect_flag = 1 + self.get_bt_connect_timer.start(2000,0,self.get_bt_connect_timer_cb) + def get_bt_connect_timer_cb(self,*agrc): + if self.get_bt_connect_flag == 1: + EventMesh.publish("send_bt_connect_list") + pass + self.get_bt_connect_flag = 0 + pass + def send_ble_scan(self,event,msg): + self.read_ack_flag=0 + # // { + # // "event":"send_ble_scan", + # // "data":{ + # // "send_ble_scan":"0" + # // } + # // } #格式 + json_data = \ + { + + "event": "bt_scan", + "data":msg + + } + json_str =ujson.dumps(json_data) + print("send_ble_scan{}".format(json_str)) + self.send_data(json_str,1) + # self.send_reset_cb(json_str) + + print("bt_send_end") + def send_data(self,str_data,need_resend = None,ACK_time =None): + try: + if not ("vc_config" in globals() and vc_config and vc_config.uart_supported()): + return + except Exception: + return + if self.bt_ota_flag == 1: + print("ota 中不需要上传") + return + print("send_data{} {} {}".format(str_data,need_resend,ACK_time)) + queue_data={"event":"uart_send_data","data":str_data} + if need_resend != None: + queue_data={"event":"uart_send_data","data":str_data,"resend_flag":1,"ACK_time":ACK_time} + + self.uart_send_queue.put(queue_data) + def uart_queue_send_data(self,str_data): + if self.bt_ota_flag == 1: + return + #EventMesh.publish("wakeup_BT") + print("bt_seuart_queue_send_data start {}".format(str_data)) + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + self.AI_uart.write(str_data) + print("bt_seuart_queue_send_data end {}".format(str_data)) + def led_r_open(self,event=None,msg=None): + json_data = \ + { + + "event": "led_r_open", + + + } + json_str =ujson.dumps(json_data) + print("led_r_open{}".format(json_str)) + self.send_data(json_str) + def led_g_open(self,event=None,msg=None): + json_data = \ + { + + "event": "led_g_open", + + } + json_str =ujson.dumps(json_data) + print("led_g_open{}".format(json_str)) + self.send_data(json_str) + def led_b_open(self,event=None,msg=None): + json_data = \ + { + + "event": "led_b_open", + + } + json_str =ujson.dumps(json_data) + print("led_b_open{}".format(json_str)) + self.send_data(json_str) + def led_rgb_open(self,event=None,msg=None): + json_data = \ + { + + "event": "led_rgb_open", + + } + json_str =ujson.dumps(json_data) + print("led_rgb_open{}".format(json_str)) + self.send_data(json_str) + def led_close(self,event=None,msg=None): + json_data = \ + { + + "event": "led_close", + + } + json_str =ujson.dumps(json_data) + print("led_close{}".format(json_str)) + self.send_data(json_str) + + def send_ack(self): + json_data = \ + { + + "event": "ack", + + } + json_str =ujson.dumps(json_data) + print("send_ack{}".format(json_str)) + self.send_data(json_str) + def AI_key_press(self): + self.check_rest_timer_count = 0 + print("AI_key_press") + EventMesh.publish("AI_key_press") + def BT_AI_key_press(self): + self.check_rest_timer_count = 0 + print("BT_AI_key_press") + EventMesh.publish("BT_AI_key_press") + + def pwk_key_release(self): + self.check_rest_timer_count = 0 + #print("pwk_key_release") + EventMesh.publish("pwk_key_release") + def send_get_versions(self,event=None,msg=None): + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + #self.read_ack_flag = 0 + json_data = \ + { + + "event": "get_versions", + } + json_str =ujson.dumps(json_data) + print("send_get_versions {}".format(json_str)) + self.send_data(json_str,1) + #self.AI_uart.write(json_str) + count =1000 + while True: + count -=1 + if count <=0 or self.read_ack_flag == 1: + break + utime.sleep_ms(1) + + pass + def start_ota(self,event=None,msg=None):#通过串口给蓝牙升级 + #EventMesh.publish("wakeup_BT") + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + print("start_ota!!!") + print("start_ota!!!") + json_data = \ + { + "event": "ota_start", + } + json_str =ujson.dumps(json_data) + self.AI_uart.write(json_str) + print(json_str) + count =100 + while True: + count -=1 + if count <=0 or self.read_ack_flag == 1: + break + utime.sleep_ms(1) + print("start_ota count:{}!!!".format(count)) + + def end_ota(self,event=None,msg=None):#通过串口给蓝牙升级 + #EventMesh.publish("wakeup_BT") + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + print("end_ota!!!") + print("end_ota!!!") + json_data = \ + { + "event": "ota_end", + } + json_str =ujson.dumps(json_data) + self.AI_uart.write(json_str) + print(json_str) + count =100 + while True: + count -=1 + if count <=0 or self.read_ack_flag == 1: + break + utime.sleep_ms(1) + print("end_ota count:{}!!!".format(count)) + + def bt_ota_run(self,event=None,msg=None):#通过串口给蓝牙升级 + # EventMesh.publish("wakeup_BT") + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + print("bt_ota_run!!!:{}".format(msg)) + print("bt_ota_run!!!") + print("bt_ota_run!!!") + self.bt_ota_flag = 1 + self.read_ack_flag = 0 + + json_data = \ + { + "event": "ota_start", + } + json_str =ujson.dumps(json_data) + self.AI_uart.write(json_str) + print(json_str) + count =2000#等20秒,擦除flash + while True: + count -=1 + if count <=0 or self.read_ack_flag == 1: + break + utime.sleep_ms(10) + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + print("ota_start:count:{}".format(count)) + url = msg[0] + bt_size = msg[1] + headers_T = {"Content-Type":"application/octet-stream"} + response = request.get(url = url,headers=headers_T,timeout = 120,decode = False) + offset = 0 + checksum = 0 + for i in response.content: + EventMesh.publish(constants.EVENT.SYSTEM_UP)#唤醒休眠 + + + data = base64.b64encode(i) + send_count = 5#最大重发5次,没ack或者返回error重发 + while 1: + send_count-=1 + self.read_ack_flag = 0 + checksum = 0 + # 对每个字节进行异或运算 + for byte in i: + checksum ^= byte # 等价于 checksum = checksum ^ byte + json_data = \ + { + "event": "ota_data", + "update":data, + "offset":offset, + "checksum":checksum + } + print("offset:{},checksum:{}".format(offset,checksum)) + json_str =ujson.dumps(json_data) + self.AI_uart.write(json_str) + #utime.sleep_ms(100) + count =300 + while True: + count -=1 + if count <=0 or self.read_ack_flag != 0 : + break + utime.sleep_ms(10) + print("ota_data:{} count:{}".format(len(json_str),count)) + if self.read_ack_flag == 1: + break + if send_count <=0 : + return -1 + offset+=len(i) + if offset < bt_size: + return -1 + self.read_ack_flag = 0 + json_data = \ + { + "event": "ota_end", + } + json_str =ujson.dumps(json_data) + self.AI_uart.write(json_str) + + count =100 + while True: + count -=1 + if count <=0 or self.read_ack_flag == 1: + break + utime.sleep_ms(1) + self.bt_ota_flag = 0 + return 0 diff --git a/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_utils.py b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_utils.py new file mode 100644 index 00000000..cbffa2e4 --- /dev/null +++ b/2.sdk/project/7PIN_1_5/sdk/services/microPython/ports/quectel/core/vc_utils.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +"""VC 杂项工具函数。""" + +def num_str_to_chinese(num_str): + # 数字与中文映射(含零) + num_map = { + '0': '零', '1': '一', '2': '二', '3': '三', '4': '四', + '5': '五', '6': '六', '7': '七', '8': '八', '9': '九' + } + chinese = '' + # 逐位转换,拼接单位 + for char in num_str: + if char in num_map: + digit = num_map[char] + chinese += digit + else: + chinese += char + + return chinese +