made some good changes

This commit is contained in:
2026-05-16 09:53:25 +02:00
parent 6bd51ff38b
commit 5176c9fc41
9 changed files with 344 additions and 178 deletions
+52 -68
View File
@@ -1,69 +1,53 @@
const CMD='/data/adb/modules/virtualcam/control.sh';
function log(m,t){const e=document.getElementById('console');const d=new Date();
e.innerHTML+=`<div class="${t||'log'}">[${d.toLocaleTimeString()}] ${m}</div>`;e.scrollTop=e.scrollHeight}
async function exec(c){try{return await KSU.exec(c)}catch(e){log('exec error','err');return null}}
async function loadConfig(){
const r=await exec(CMD+' get_config');if(!r)return;
try{const c=JSON.parse(r);
document.querySelector(`.src-btn[data-src="${c.source_type}"]`).click();
document.getElementById('rtsp-url').value=c.rtsp_url||'';
document.getElementById('mp4-path').value=c.mp4_path||'';
document.getElementById('res-w').value=c.res_w||1920;
document.getElementById('res-h').value=c.res_h||1080;
document.getElementById('fps').value=c.fps||30;
}catch(e){}
}
async function status(){
const r=await exec(CMD+' status');if(!r)return;
try{
const s=JSON.parse(r);const d=document.getElementById('status-dash');
const items=[['Provider',s.provider_pid>0?'green':'red',s.provider_pid>0?'Running':'Stopped'],
['Feeder',s.feeder_pid>0?'green':'red',s.feeder_pid>0?'Running':'Stopped'],
['Service',s.service_registered==1?'green':'red',s.service_registered==1?'Registered':'Not found'],
['v4l2',s.v4l2_loaded==1?'green':'red',s.v4l2_loaded==1?'Loaded':'Missing'],
['Device','yellow',s.video0||'N/A']];
d.innerHTML=items.map(i=>`<div class="stat"><span class="l">${i[0]}</span><span class="v ${i[1]}">${i[2]}</span></div>`).join('');
log('Status updated','ok');
}catch(e){log('Parse error','err')}
}
document.addEventListener('DOMContentLoaded',()=>{
loadConfig();
status();
document.querySelectorAll('.src-btn').forEach(b=>{
b.addEventListener('click',()=>{
document.querySelectorAll('.src-btn').forEach(x=>x.classList.remove('active'));
b.classList.add('active');
document.getElementById('rtsp-config').classList.toggle('hidden',b.dataset.src!=='rtsp');
document.getElementById('mp4-config').classList.toggle('hidden',b.dataset.src!=='mp4');
});
});
document.getElementById('btn-save').addEventListener('click',async()=>{
const src=document.querySelector('.src-btn.active').dataset.src;
const rtsp=document.getElementById('rtsp-url').value;
const mp4=document.getElementById('mp4-path').value;
const w=document.getElementById('res-w').value||1920;
const h=document.getElementById('res-h').value||1080;
const f=document.getElementById('fps').value||30;
await exec(`echo '{"source_type":"${src}","rtsp_url":"${rtsp}","mp4_path":"${mp4}","res_w":${w},"res_h":${h},"fps":${f}}' > /data/local/tmp/virtualcam_config.json`);
log('Config saved','ok');
});
document.getElementById('btn-start').addEventListener('click',async()=>{
log('Starting provider...','info');await exec(CMD+' start');log('Provider started','ok');setTimeout(status,3000);
});
document.getElementById('btn-stop').addEventListener('click',async()=>{
log('Stopping...','info');await exec(CMD+' stop');log('Stopped','ok');status();
});
document.getElementById('btn-restart').addEventListener('click',async()=>{
log('Restarting provider...','info');await exec(CMD+' restart');log('Provider restarted','ok');setTimeout(status,3000);
});
document.getElementById('btn-feed').addEventListener('click',async()=>{
log('Starting feeder...','info');await exec(CMD+' start_feeder');log('Feeder started','ok');setTimeout(status,2000);
});
document.getElementById('btn-unfeed').addEventListener('click',async()=>{
log('Stopping feeder...','info');await exec(CMD+' stop_feeder');log('Feeder stopped','ok');status();
});
document.getElementById('btn-refresh').addEventListener('click',status);
});
let lc=0, _ps='', _pp=0, _pf=0;
function log(m,t){const e=document.getElementById('console');if(!e)return;const d=new Date();e.innerHTML+=`<div class="${t||'log'}">[${d.toLocaleTimeString()}] ${m}</div>`;e.scrollTop=e.scrollHeight;lc++;const l=document.getElementById('log-count');if(l)l.textContent=`(${lc})`}
function run(a){try{ksu.exec(CMD+' '+a);log(a+' OK','ok')}catch(e){log(a+' error: '+e.message,'err')}updateDash()}
function updateDash(){try{
const r=typeof ksu!=='undefined'?ksu.exec(CMD+' status'):null;
if(!r||typeof r!=='string'){document.getElementById('status-dash').innerHTML='<div class="stat" style="grid-column:1/3;color:#f85149">No data</div>';return}
const s=JSON.parse(r);
const d=document.getElementById('status-dash');if(!d)return;
const sc=s.status==='active'?'green':(s.status==='partial'?'yellow':'red');
const st=s.status==='active'?'ACTIVE':(s.status==='partial'?'PARTIAL':'STOPPED');
let v=[];
if(s.camera2_level) v=[['camera2','green',s.camera2_level],['Checks',s.checks&&s.checks.includes('provider_ok')?'green':'red',s.checks||'?']];
d.innerHTML=[['Status',sc,st],['Provider',s.provider_pid>0?'green':'red',s.provider_pid>0?'PID '+s.provider_pid:'Stopped'],['Feeder',s.feeder_pid>0?'green':'red',s.feeder_pid>0?'PID '+s.feeder_pid:'Stopped'],['Service',s.service_registered==1?'green':'red',s.service_registered==1?'Registered':'Missing'],['v4l2',s.v4l2_loaded==1?'green':'red',s.v4l2_loaded==1?'Loaded':'Missing'],['Device','yellow',s.video0||'N/A'],...v].map(i=>`<div class="stat"><span class="l">${i[0]}</span><span class="v ${i[1]}">${i[2]}</span></div>`).join('');
document.getElementById('subtitle').textContent=s.status==='active'?'Feeding video to /dev/video0':'Physical cameras NOT replaced';
if(s.status!==_ps||s.provider_pid!==_pp||s.feeder_pid!==_pf){_ps=s.status;_pp=s.provider_pid;_pf=s.feeder_pid;log('Status: '+st+' pid='+s.provider_pid+' feeder='+s.feeder_pid,'ok')}
}catch(e){log('Dash error: '+e.message,'err')}}
function loadDeviceInfo(){try{
const r=ksu.exec(CMD+' device_info');
document.getElementById('device-info').innerHTML=r?'<pre style="background:#0d1117;border:1px solid #30363d;border-radius:4px;padding:8px;font-size:10px;overflow-x:auto;white-space:pre-wrap">'+r.replace(/</g,'&lt;').replace(/>/g,'&gt;')+'</pre>':'<p class="muted">No data</p>';
log('Device info loaded','ok');
}catch(e){log('DevInfo error: '+e.message,'err')}}
document.getElementById('btn-start')?.addEventListener('click',()=>run('start'));
document.getElementById('btn-stop')?.addEventListener('click',()=>run('stop'));
document.getElementById('btn-restart')?.addEventListener('click',()=>run('restart'));
document.getElementById('btn-feed')?.addEventListener('click',()=>run('start_feeder'));
document.getElementById('btn-unfeed')?.addEventListener('click',()=>run('stop_feeder'));
document.getElementById('btn-save')?.addEventListener('click',()=>{
const src=document.querySelector('.src-btn.active')?.dataset.src||'mp4';
const json=JSON.stringify({source_type:src,rtsp_url:document.getElementById('rtsp-url').value,mp4_path:document.getElementById('mp4-path').value,res_w:parseInt(document.getElementById('res-w').value)||1920,res_h:parseInt(document.getElementById('res-h').value)||1080,fps:parseInt(document.getElementById('fps').value)||30,rotation:parseInt(document.getElementById('rotation').value)||0});
ksu.exec("echo '"+json+"' > /data/local/tmp/virtualcam_config.json");
log('Config saved','ok');
});
document.getElementById('btn-refresh')?.addEventListener('click',updateDash);
document.getElementById('btn-deviceinfo')?.addEventListener('click',loadDeviceInfo);
document.querySelectorAll('.src-btn').forEach(b=>b.addEventListener('click',()=>{
document.querySelectorAll('.src-btn').forEach(x=>x.classList.remove('active'));
b.classList.add('active');
document.getElementById('rtsp-config').classList.toggle('hidden',b.dataset.src!=='rtsp');
document.getElementById('mp4-config').classList.toggle('hidden',b.dataset.src!=='mp4');
}));
updateDash();
setInterval(updateDash,5000);
document.getElementById('btn-refresh')?.addEventListener('click',updateDash);
document.getElementById('btn-deviceinfo')?.addEventListener('click',loadDeviceInfo);
document.querySelectorAll('.src-btn').forEach(b=>b.addEventListener('click',()=>{
document.querySelectorAll('.src-btn').forEach(x=>x.classList.remove('active'));
b.classList.add('active');
document.getElementById('rtsp-config').classList.toggle('hidden',b.dataset.src!=='rtsp');
document.getElementById('mp4-config').classList.toggle('hidden',b.dataset.src!=='mp4');
}));
updateDash();
setInterval(updateDash,5000);