iobroker.javascript
Version:
Rules Engine for ioBroker
9 lines • 4.55 kB
JavaScript
function e(e){let t=[];for(let n of Object.keys(e)){let r=e[n];if((r==null?void 0:r.type)!==`script`)continue;let i=r.common;if(!(i!=null&&i.source))continue;let a;a=typeof i.name==`object`&&i.name?i.name.en||i.name.de||n:i.name||n,t.push({id:n,name:a,source:i.source,engineType:i.engineType||`Javascript/js`,enabled:i.enabled??!1})}return t}var t=[{regex:e=>RegExp(`getState\\s*\\(\\s*['"\`]${n(e)}['"\`]`,`g`),type:`read`},{regex:e=>RegExp(`setState\\s*\\(\\s*['"\`]${n(e)}['"\`]`,`g`),type:`write`},{regex:e=>RegExp(`setStateChanged\\s*\\(\\s*['"\`]${n(e)}['"\`]`,`g`),type:`write`},{regex:e=>RegExp(`setStateDelayed\\s*\\(\\s*['"\`]${n(e)}['"\`]`,`g`),type:`write`},{regex:e=>RegExp(`on\\s*\\(\\s*['"\`]${n(e)}['"\`]`,`g`),type:`subscribe`},{regex:e=>RegExp(`on\\s*\\(\\s*\\{[^}]*id\\s*:\\s*['"\`]${n(e)}['"\`]`,`g`),type:`subscribe`},{regex:e=>RegExp(`subscribe\\s*\\(\\s*['"\`]${n(e)}['"\`]`,`g`),type:`subscribe`},{regex:e=>RegExp(`existsState\\s*\\(\\s*['"\`]${n(e)}['"\`]`,`g`),type:`exists`},{regex:e=>RegExp(`existsObject\\s*\\(\\s*['"\`]${n(e)}['"\`]`,`g`),type:`exists`},{regex:e=>RegExp(`=\\s*['"\`]${n(e)}['"\`]`,`g`),type:`read`}];function n(e){return e.replace(/[.*+?^${}()|[\]\\]/g,`\\$&`)}function r(e,n){let r=[];for(let a of e){if(!a.source.includes(n))continue;let e=a.source.split(`
`);for(let o of t){let t=o.regex(n),s;for(;(s=t.exec(a.source))!==null;){var i;let t=a.source.substring(0,s.index).split(`
`).length,n=((i=e[t-1])==null?void 0:i.trim())||``;if(!r.some(e=>e.scriptId===a.id&&e.lineNumber===t&&e.usageType===o.type)){let e;e=typeof a.name==`string`?a.name:a.id,r.push({scriptId:a.id,scriptName:e,usageType:o.type,lineNumber:t,line:n})}}}}return r}var i=2e5;function a(e){let t=e.match(/\n\/\/((?:[A-Za-z0-9+/=]){50,})$/);if(t){let n=e.substring(0,e.length-t[0].length).trim();if(n&&!n.startsWith(`<xml`)&&!n.startsWith(`<block`))return n}let n=e.split(`
`);for(let e=n.length-1;e>=0;e--){let t=n[e].trim();if(t.startsWith(`//`)&&t.length>200&&/^\/\/[A-Za-z0-9+/=%]+$/.test(t)){let t=n.slice(0,e).join(`
`).trim();if(t&&!t.startsWith(`<xml`)&&!t.startsWith(`<block`))return t}}let r=e.trim();return!r.startsWith(`<xml`)&&!r.startsWith(`<block`)&&!r.startsWith(`<?xml`)?r:null}function o(e){let t=[],n=/<field name="OID">([^<]+)<\/field>/gi,r=new Set,i;for(;(i=n.exec(e))!==null;)r.add(i[1]);r.size>0&&t.push(`Datapoints used: ${[...r].join(`, `)}`);let a=/<field name="(?:VALUE|TEXT|NUM|DELAY_MS|CRON)">([^<]+)<\/field>/gi,o=[],s;for(;(s=a.exec(e))!==null;)s[1].trim()&&o.push(`${s[1].trim()}`);o.length>0&&t.push(`Values/parameters: ${o.join(`, `)}`);let c=/<block type="([^"]+)"/gi,l=new Set,u;for(;(u=c.exec(e))!==null;)l.add(u[1]);let d=[];for(let e of l)(e.includes(`on_ext`)||e.includes(`on_source`))&&d.push(`subscribes to state changes (trigger)`),(e.includes(`set`)||e.includes(`update`))&&d.push(`sets state values`),e.includes(`get`)&&d.push(`reads state values`),(e.includes(`cron`)||e.includes(`schedule`))&&d.push(`uses time schedule/cron`),(e.includes(`telegram`)||e.includes(`sendto`))&&d.push(`sends messages (sendTo)`),(e.includes(`timeout`)||e.includes(`delay`))&&d.push(`uses delays/timeouts`),(e.includes(`log`)||e.includes(`debug`))&&d.push(`logging`),(e.includes(`if`)||e.includes(`logic`)||e.includes(`controls_if`))&&d.push(`conditional logic (if/else)`);d.length>0&&t.push(`Actions: ${[...new Set(d)].join(`, `)}`);let f=/<comment[^>]*>([^<]+)<\/comment>/gi,p=[],m;for(;(m=f.exec(e))!==null;)m[1].trim()&&p.push(m[1].trim());return p.length>0&&t.push(`Comments: ${p.join(`; `)}`),t.length>0?t.join(`
`):`No details could be extracted from the Blockly XML.`}function s(e,t=!1){if(e.length===0)return`No scripts found.`;let n=[`All scripts in this ioBroker installation:
`],r=0,s=!1;for(let c of e){let e=c.enabled?`active`:`inactive`,l=c.engineType===`Blockly`||c.engineType===`Rules`,u=c.engineType.includes(`TypeScript`),d=l?`Blockly`:u?`TS`:`JS`,f=`### ${c.id.replace(/^script\.js\./,``).replace(/\./g,`/`)} [${d}, ${e}] (${c.id})`,p;if(l){let e=a(c.source);p=e?`${f}\nThis is a Blockly script. Compiled JavaScript:\n\`\`\`javascript\n${e}\n\`\`\`\n`:`${f}\nThis is a Blockly (visual programming) script. Extracted information:\n${o(c.source)}\n`}else p=`${f}\n${`\`\`\`${u?`typescript`:`javascript`}\n${c.source}\n\`\`\``}\n`;!t&&r+p.length>i?(s=!0,n.push(`${f}\n(source too large to include)\n`)):(n.push(p),r+=p.length)}return s&&n.push(`
Note: Some script sources were omitted due to size. The user can ask about specific scripts by name.`),n.join(`
`)}export{e as i,a as n,r,s as t};