Update README token guide and refine WB rule behavior
This commit is contained in:
@@ -479,6 +479,25 @@ function _enc(v) {{
|
||||
return encodeURIComponent(String(v === undefined || v === null ? "" : v));
|
||||
}}
|
||||
|
||||
function _clampVolume(v, defVal) {{
|
||||
var n = parseInt(v, 10);
|
||||
if (isNaN(n)) n = defVal;
|
||||
if (isNaN(n)) n = 35;
|
||||
if (n < 0) n = 0;
|
||||
if (n > 100) n = 100;
|
||||
return n;
|
||||
}}
|
||||
|
||||
function _ttsRestoreDelayMs(text) {{
|
||||
var clean = String(text || "").trim();
|
||||
if (!clean) return 3500;
|
||||
var chars = clean.length;
|
||||
var words = clean.split(/\\s+/).filter(Boolean).length;
|
||||
var speechSec = Math.max(words / 2.4, chars / 14.0);
|
||||
var delaySec = Math.max(3.5, Math.min(20.0, speechSec + 1.5));
|
||||
return Math.round(delaySec * 1000);
|
||||
}}
|
||||
|
||||
defineVirtualDevice(DEVICE_ID, {{
|
||||
title: DEVICE_TITLE,
|
||||
cells: {{
|
||||
@@ -507,7 +526,18 @@ defineRule(DEVICE_ID + "_tts_send", {{
|
||||
if (!newValue) return;
|
||||
var txt = String(dev[DEVICE_ID + "/tts_text"] || "").trim();
|
||||
if (!txt) return;
|
||||
_apiGet("/api/exec?action=tts&station=" + _enc(STATION_SELECTOR) + "&text=" + _enc(txt) + "&volume=35", function(){{}});
|
||||
var targetVolume = _clampVolume(dev[DEVICE_ID + "/volume"], 35);
|
||||
var prevVolumeRaw = dev[DEVICE_ID + "/current_volume"];
|
||||
var prevVolume = _clampVolume(prevVolumeRaw, targetVolume);
|
||||
_apiGet(
|
||||
"/api/exec?action=tts&station=" + _enc(STATION_SELECTOR) + "&text=" + _enc(txt) + "&volume=" + _enc(targetVolume),
|
||||
function(){{}}
|
||||
);
|
||||
if (prevVolume !== targetVolume) {{
|
||||
setTimeout(function() {{
|
||||
_apiGet("/api/exec?action=volume&station=" + _enc(STATION_SELECTOR) + "&level=" + _enc(prevVolume), function(){{}});
|
||||
}}, _ttsRestoreDelayMs(txt));
|
||||
}}
|
||||
}}
|
||||
}});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user