Wang Tai 8 months ago
parent
commit
84f0fe8648
6 changed files with 133 additions and 14 deletions
  1. 73 12
      background.js
  2. 1 1
      popup.html
  3. 1 1
      popup/src/App.js
  4. 1 0
      static/js/main.52ee3685.js
  5. 57 0
      static/js/main.52ee3685.js.LICENSE.txt
  6. 0 0
      static/js/main.52ee3685.js.map

+ 73 - 12
background.js

@@ -1,5 +1,44 @@
 // background.js
-const wsUrl = 'ws://10.138.3.85:21086/ws?siteId=167037857943322624';
+const numberToChinese = number => {
+  const numbers = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
+  const [integer, fraction] = number.toString().split('.');
+
+  // 整数部分转换
+  let result = integer.split('').map(num => numbers[parseInt(num, 10)]).join('');
+
+  // 小数部分转换
+  if (fraction) {
+    result += '点' + fraction.split('').map(num => numbers[parseInt(num, 10)]).join('');
+  }
+
+  return result;
+};
+// 英文单位到中文单位的映射
+const unitMap = {
+  "m": "米",
+  "kg": "千克",
+  "s": "秒",
+  "m/s": "米每秒",
+  "kwh": "千瓦时",
+  "kw": "千瓦",
+  "kWh": "千瓦时",
+};
+
+const translateSentence = sentence => {
+  // 匹配数字和可选的单位
+  const regex = /(\d+(\.\d+)?)(kwh|kwH|kg|kw|m\/s|m)?/g;
+
+  return sentence.replace(regex, (match, number, fraction, unit) => {
+    const chineseNumber = number;
+    // const chineseNumber = numberToChinese(number); // 转换数字
+    const chineseUnit = unit ? (unitMap[unit.toLowerCase()] || unit) : ''; // 转换单位
+    return chineseNumber + chineseUnit;
+  });
+};
+
+
+// const wsUrl = 'ws://10.138.3.85:21086/ws?siteId=167037857943322624';
+const wsUrl = 'ws://218.13.91.150:21086/ws?siteId=167037857943322624';
 
 const initWebSocket = () => {
   const ws = new WebSocket(wsUrl);
@@ -9,12 +48,15 @@ const initWebSocket = () => {
   };
 
   ws.onmessage = event => {
-    const message = event.data;
+    let message = event.data;
+    message = translateSentence(message);
     console.log('Received message:', message);
     // 假设接收到的报警信息是一个简单的文本字符串
     // 根据你的实际情况,你可能需要解析JSON或进行其他处理
     if (message.includes('报警')) { // 根据实际情况判断是否为报警信息
-      chrome.tts.speak(message, {'lang': 'zh-CN', 'rate': 1.0});
+      // 判断优先级
+      // 判断时间
+      playTts(message);
     }
   };
 
@@ -43,13 +85,32 @@ chrome.tts.getVoices(function (voices) {
   console.log(voiceNames);
   chrome.storage.sync.set({'voiceNames': voiceNames});
 });
+
+const playTts = (message) => {
+  chrome.storage.sync.get(['voiceName', 'voiceVolume'], (obj) => {
+    let volume = obj.voiceVolume;
+    let voiceName = obj.voiceName;
+    if (volume === undefined) {
+      volume = 100;
+    }
+    if (voiceName === undefined) {
+      voiceName = '';
+    }
+    console.log("volume", volume);
+    console.log("voice name", voiceName);
+    chrome.tts.speak(message, {
+      'lang': 'zh-CN',
+      'rate': 1.0,
+      'voiceName': voiceName,
+      'volume': volume / 100
+    });
+  });
+};
 // 初始化WebSocket连接
-// initWebSocket();
-// console.log("123")
-// chrome.tts.speak("哈哈哈哈", {
-//   'lang': 'zh-CN',
-//   'rate': 1.0,
-//   'voiceName': 'Tingting'
-// });
-// chrome.tts.speak('Hello, world.');
-// console.log("456")
+initWebSocket();
+const sentence = "我昨天买了123456789.321kwh苹果和5个橙子,还有0.233445m/s的绳子。";
+const translatedSentence = translateSentence(sentence);
+
+console.log(translatedSentence);
+// playTts(sentence)
+playTts(translatedSentence)

+ 1 - 1
popup.html

@@ -1 +1 @@
-<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><style></style><script defer="defer" src="/static/js/main.68266d6e.js"></script><link href="/static/css/main.f855e6bc.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
+<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><style></style><script defer="defer" src="/static/js/main.52ee3685.js"></script><link href="/static/css/main.f855e6bc.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

+ 1 - 1
popup/src/App.js

@@ -5,7 +5,7 @@ import React, {useEffect, useState} from "react";
 function App() {
   const [options, setOptions] = useState(null)
   const [selectVoice, setSelectVoice] = useState('');
-  const [voiceVolume, setVoiceVolume] = useState(50)
+  const [voiceVolume, setVoiceVolume] = useState(100)
 
   useEffect(() => {
     chrome.storage.sync.get(['voiceNames', 'voiceName', 'voiceVolume'], (obj) => {

File diff suppressed because it is too large
+ 1 - 0
static/js/main.52ee3685.js


+ 57 - 0
static/js/main.52ee3685.js.LICENSE.txt

@@ -0,0 +1,57 @@
+/*!
+	Copyright (c) 2018 Jed Watson.
+	Licensed under the MIT License (MIT), see
+	http://jedwatson.github.io/classnames
+*/
+
+/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
+
+/**
+ * @license React
+ * react-dom.production.min.js
+ *
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+/**
+ * @license React
+ * react-is.production.min.js
+ *
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+/**
+ * @license React
+ * react-jsx-runtime.production.min.js
+ *
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+/**
+ * @license React
+ * react.production.min.js
+ *
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */
+
+/**
+ * @license React
+ * scheduler.production.min.js
+ *
+ * Copyright (c) Facebook, Inc. and its affiliates.
+ *
+ * This source code is licensed under the MIT license found in the
+ * LICENSE file in the root directory of this source tree.
+ */

File diff suppressed because it is too large
+ 0 - 0
static/js/main.52ee3685.js.map


Some files were not shown because too many files changed in this diff