|
@@ -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)
|