NEWS
ehemalige Historydaten finden
-
// ******************************** // findOldHistoryDPs v1.0.5 // Copyright ©MCU // ******************************** // v1.0.1 directDelete-> true Möglichkeit die DatenDateien direkt löschen // v1.0.2 aliasId berücksichtigt bei enabledDPs // v1.0.3 zusätzliche Funktion listHistoryDPsWithaliasId('history.0'); // v1.0.4 zusätzlich: fillAlias -> alle aktiven history-DPs mit alias in Array withAliasIdInEnabled // fillEnabled_DPs -> nur die akitven History DPs in Array füllen -> enabled_DPs // useAliasIdInEnabled -> true -> AliasId wird in enabled_DPs verwendet (was richtig ist, da die history-Werte mit alias geschrieben werden) // v1.0.5 zusätzliche Funktion: alias-Suche in allen ioBroker-DPs (unabhängig von history) let findOldDP = '0_userdata.0.history.findOldDPs'; let findOldDPs_Old = findOldDP + '.old_DPs'; let findOldDPs_Enabled = findOldDP + '.enabled_DPs'; let findOldDPs_InDB = findOldDP + '.inDB_DPS'; let findOldDPs_BeginTS = findOldDP + '.dirStart'; let findOldDPs_EndTS = findOldDP + '.dirEnd'; let findOldDPs_Start = findOldDP + '.start'; let findOldDPs_DirectDel = findOldDP + '.directDelete'; let findOldDPs_fillEnabled = findOldDP + '.fillEnabled_DPs'; let findOldDPs_fillAlias = findOldDP + '.fillWithAliasId'; let findoldDPs_withAliasId = findOldDP + '.withAliasId_DPs'; let findOldDPs_useAliasInEnabled = findOldDP + '.useAliasIdInEnabled'; let findOldDPs_aliasDPs = findOldDP + '.aliasDPs'; let findOldDPs_aliasDPs_search = findOldDP + '.aliasDPs_search'; createStateAsync(findOldDPs_aliasDPs, {read: true, write: true, name: "ioBroker-DPs mit aliasId (unabhängig history)", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_aliasDPs_search, {read: true, write: true, name: "Start Suche aliasIds in all ioBroker DPs", type: "boolean", role: "button", def: false }); let dbNamespace = 'history.0'; //createStateAsync(findOldDPs_Old, {read: true, write: true, name: "JSON für nicht genutzte DPs noch in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_Enabled, {read: true, write: true, name: "Aktive History-DPs in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findoldDPs_withAliasId, {read: true, write: true, name: "Aktive History-DPs mit aliasId", type: "string", role: "text", def: JSON.stringify([]) }); //createStateAsync(findOldDPs_InDB, {read: true, write: true, name: "Genutzte DPs in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_BeginTS, {read: true, write: true, name: "Startverzeichnis", type: "number", role: "", def: 0 }); createStateAsync(findOldDPs_EndTS, {read: true, write: true, name: "Endverzeichnis", type: "number", role: "", def: 10 }); //new Date().getTime() createStateAsync(findOldDPs_Start, {read: true, write: true, name: "Start Suche", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_fillEnabled, {read: true, write: true, name: "enabled_DPs suchen", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_fillAlias, {read: true, write: true, name: "withAliasId suchen", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_DirectDel, {read: true, write: true, name: "!!! Daten direkt löschen !!!", type: "boolean", role: "", def: false }); createStateAsync(findOldDPs_useAliasInEnabled, {read: true, write: true, name: "AliasId in Enabled_DPs", type: "boolean", role: "", def: false }); on({id: findOldDPs_Start, change: "any"}, function (obj) { let value = obj.state.val; if (value){ //setState(findOldDPs_EndTS,parseInt(new Date().getTime()),false); getEnabledHistoryDPs(dbNamespace); let fromDir = getState(findOldDPs_BeginTS).val; let toDir = getState(findOldDPs_EndTS).val; setTimeout(function(){ searchDir(fromDir,toDir); },5000); setStateDelayed(findOldDPs_Start,false,3000,false); } }); on({id: findOldDPs_fillEnabled, change: "any"}, function (obj) { let value = obj.state.val; if (value){ getEnabledHistoryDPs(dbNamespace); setStateDelayed(findOldDPs_fillEnabled,false,3000,false); } }); on({id: findOldDPs_fillAlias, change: "any"}, function (obj) { let value = obj.state.val; if (value){ listHistoryDPsWithaliasId(dbNamespace); setStateDelayed(findOldDPs_fillAlias,false,3000,false); } }); on({id: findOldDPs_aliasDPs_search, change: "any"}, function (obj) { let value = obj.state.val; if (value){ findDPsWithAliasId(); setStateDelayed(findOldDPs_aliasDPs_search,false,3000,false); } }); //listHistoryDPsWithaliasId('history.0'); function listHistoryDPsWithaliasId(adp){ let arr =[]; sendTo(adp, 'getEnabledDPs', {}, function (result) { let count = 0; for ( let dp in result){ let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId; if (dpHistoryAlias != '' && dpHistoryAlias != undefined){ arr.push({'dpId':dp,'aliasId':dpHistoryAlias}); count++; } } log('Anzahl der aktiv geloggten DPs mit aliasId: '+count); // setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false); //log(JSON.stringify(arr)); setState(findoldDPs_withAliasId,JSON.stringify(arr),false); }); } //findDPsWithAliasId(); function findDPsWithAliasId(){ let allDPs = $('*'); let arr = []; log('Anzahl der States: '+allDPs.length); for (let i=0;i<allDPs.length;i++){ if (existsObject(allDPs[i])){ if (getObject(allDPs[i]).common != undefined){ if (getObject(allDPs[i]).common.alias!= undefined){ let aliasId = getObject(allDPs[i]).common.alias.id; if (aliasId != undefined){ //log(allDPs[i] +' mit aliasId: '+aliasId); //break; arr.push({'id':allDPs[i],'aliasId':aliasId}); } } } } } //log(arr); setState(findOldDPs_aliasDPs,JSON.stringify(arr),false); } /* setTimeout(function(){ getEnabledHistoryDPs(db); getHistoryDPinDB(db); },3000); */ // getEnabledHistoryDPs('history.0'); function getEnabledHistoryDPs(adp){ let arr =[]; let useAliasIdInenabled = getState(findOldDPs_useAliasInEnabled).val; sendTo(adp, 'getEnabledDPs', {}, function (result) { let count = 0; for ( let dp in result){ let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId; if (dpHistoryAlias != '' && dpHistoryAlias != undefined && useAliasIdInenabled){ arr.push(dpHistoryAlias); }else{ arr.push(dp); } count++; } log('Anzahl der aktiv geloggten DPs: '+count); setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false); }); } // getHistoryDPinDB('history.0'); function getHistoryDPinDB(db){ //Die 50 zuletzt gespeicherte Ereignisse für alle IDs holen: let beginTS = getState(findOldDPs_BeginTS).val; if (beginTS == 0){ beginTS = parseInt(new Date().getTime()) - 30*24*60*60*1000; } let endTS = getState(findOldDPs_EndTS).val; if(endTS == 0){ endTS = parseInt(new Date().getTime()); } sendTo(db, 'getHistory', { id: '*', options: { begin: beginTS, end: endTS, /* count: 50,*/ addId: true } }, function (result) { let dpArr =[]; let count = 0; for (var i = 0; i < result.result.length; i++) { if (!isObjInArray(dpArr,result.result[i].id)){ dpArr.push(result.result[i].id); count++; } //console.log(JSON.stringify(result.result[i]) + ' ' + new Date(result.result[i].ts).toISOString()); } dpArr = dpArr.sort(function(a, b) { return a > b; }); setStateAsync(findOldDPs_InDB,JSON.stringify(dpArr),false); //log('Anzahl der DPs in dem durchsuchten Bereich:'+count); log(count+' unterschiedliche DPs in "inDB" von '+ formatDate(beginTS,'DD.MM.YYYY hh:mm:ss')+' - '+formatDate(endTS,'DD.MM.YYYY hh:mm:ss')); //Vergleich der DPs let nonActiveArr = []; let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val); for (let i=0; i< dpArr.length;i++){ // dpArr[i] let vorhanden = false; for (let j=0; j< activeDPs.length;j++ ){ if (dpArr[i] === activeDPs[j]){ vorhanden = true; break; // vorhanden } } if (!vorhanden){ // wird nicht mehr geloggt von active nonActiveArr.push(dpArr[i]); } } setState(findOldDPs_Old,JSON.stringify(nonActiveArr),false); }); } function isObjInArray(arr,itemObj){ if (arr.length>0){ for (let x=0;x<arr.length-1;x++){ if (JSON.stringify(arr[x]) === JSON.stringify(itemObj)){ return true; } } return false; }else{ return false; } } // searchDir(0,10); function searchDir(fromDir, toDir){ let fs = require('fs'); let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val); let notActiveDPs = []; let sysAdaptDir = getObject('system.adapter.'+dbNamespace).native.storeDir; fs.readdir(sysAdaptDir , function (erro, file) { log(file.length+' Verzeichnisse wurde(n) gelesen!'); log('fromDir und toDir dürfen nur im Bereich von 0 bis '+file.length +' sein! Bitte in 10er-Schritten verwenden!'); log('Es wurde ein Bereich von '+fromDir +' bis '+ toDir+' gewählt-> es dauert '+(toDir-fromDir)*2 +' Sekunden'); if (fromDir <0 || toDir >file.length){ }else{ for (let j=fromDir; j<toDir; j++){ //log(file[j]); if (file[j].indexOf('.')>=0 && file[j].length >8){ }else{ fs.readdir(sysAdaptDir+'/'+file[j] , function (error, files) { log(files.length+' DPs im Verzeichnis: '+file[j]); for (let i=0;i< files.length; i++){ let dpFiles = files[i].substring(8,files[i].length-5); if (isObjInArray(activeDPs,dpFiles)){ }else{ notActiveDPs.push({'id':dpFiles,'dir':file[j]}); } } }); //fs.unlinkSync('/opt/iobroker/iobroker-data/files/vis.0/'+jarvisDirName+'/'+files[i]+'/'+file[j]); } } setTimeout(function(){ log('Nicht aktive DPs in den ausgewählten Verzeichnissen: '+notActiveDPs.length); let olddir= ''; let directDelete = getState(findOldDPs_DirectDel).val; for (let i=0;i<notActiveDPs.length;i++){ if (directDelete){ // Direkt löschen fs.unlinkSync(sysAdaptDir+'/'+notActiveDPs[i].dir+'/history.'+notActiveDPs[i].id+'.json'); } if (notActiveDPs[i].dir != olddir){ if (directDelete){ log('Im Verzeichnis: '+notActiveDPs[i].dir +' folgende Dateien sind gelöscht!:','warn'); }else{ log('Im Verzeichnis: '+notActiveDPs[i].dir +' können folgende Dateien gelöscht werden:','warn'); } olddir = notActiveDPs[i].dir; } log(notActiveDPs[i].id); } },2000 * (toDir-fromDir)); // file.length } }); }@matt77che da steht kein length an der genannten Stelle
@matt77che sagte in ehemalige Historydaten finden:
let fs = require('fs');
kann es sein, dass du fs noch als npm modul hinzufügen musst?
-
@matt77che sagte in ehemalige Historydaten finden:
@homoran
dirStart 0
dirEnd 10 ?Ja
ich weiss nicht mehr alles, bitte zeigen!
auch was in
@matt77che sagte in ehemalige Historydaten finden:
script.js.common.Alte_history_dp_löschen:239:18
steht
-
@matt77che da steht kein length an der genannten Stelle
@matt77che sagte in ehemalige Historydaten finden:
let fs = require('fs');
kann es sein, dass du fs noch als npm modul hinzufügen musst?
@homoran sagte in ehemalige Historydaten finden:
@matt77che da steht kein length an der genannten Stelle
@matt77che sagte in ehemalige Historydaten finden:
let fs = require('fs');
kann es sein, dass du fs noch als npm modul hinzufügen musst?
habe ich gerade kontrolliert - ja dem ist so, das fehlt noch! - was muss ich da genau eintragen?
-
@homoran sagte in ehemalige Historydaten finden:
@matt77che da steht kein length an der genannten Stelle
@matt77che sagte in ehemalige Historydaten finden:
let fs = require('fs');
kann es sein, dass du fs noch als npm modul hinzufügen musst?
habe ich gerade kontrolliert - ja dem ist so, das fehlt noch! - was muss ich da genau eintragen?
-
@homoran
fs hinzugefügt, adapter neu gestartet;14.10.2024, 20:41:24.871 [error]: javascript.0 (623441) script.js.common.Alte_history_dp_löschen: TypeError: Cannot read properties of undefined (reading 'length') 14.10.2024, 20:41:24.872 [error]: javascript.0 (623441) at script.js.common.Alte_history_dp_löschen:238:18 14.10.2024, 20:41:25.376 [error]: host.iobroker Caught by controller[0]: at script.js.common.Alte_history_dp_löschen:239:18 -
@matt77che da steht kein length an der genannten Stelle
@matt77che sagte in ehemalige Historydaten finden:
let fs = require('fs');
kann es sein, dass du fs noch als npm modul hinzufügen musst?
@homoran sagte in ehemalige Historydaten finden:
@matt77che da steht kein length an der genannten Stelle
Das habe ich auch festgestellt. Die Angegebenen falschen Zeilen stimmen nicht überein
-
@homoran sagte in ehemalige Historydaten finden:
@matt77che da steht kein length an der genannten Stelle
Das habe ich auch festgestellt. Die Angegebenen falschen Zeilen stimmen nicht überein
@matt77che hast du Scripte in global?
-
@matt77che hast du Scripte in global?
-
@matt77che sind die zeilennummern hier im Post denn identisch mit denen von deinem Script?
-
@matt77che sind die zeilennummern hier im Post denn identisch mit denen von deinem Script?
-
@matt77che sagte in ehemalige Historydaten finden:
Ja - Also im Script stimmt es auch nicht überein
dann zeig doch bitte die Zeilen deines Skripts (+/- 3) die angemeckert werden
-
@matt77che Hab es gerade mal probiert und es läuft ohne Probleme.
Zeig mal bitte die History-Einstellungen.
Auch mit
History-AdapterV3.0.1
Javascript-AdapterV8.8.3Es gibt in der 237 ein file.length

Verwendest du eine andere History-Instanz?

NEUE VERSION
v1.0.6reingestellt -> automatische Suche der History-Instanzen. -
Ja, ich habe zwei instanzen; history.0 und eine history.1
Also ich habe mir die v1.0.6 gezogen und es noch einmal versucht:
Fehlermeldung:16.10.2024, 17:36:27.155 [error]: javascript.0 (1767186) script.js.common.Alte_history_dp_löschen: Fehler beim Lesen des Verzeichnisses: Error: ENOENT: no such file or directory, scandir '' -
Ja, ich habe zwei instanzen; history.0 und eine history.1
Also ich habe mir die v1.0.6 gezogen und es noch einmal versucht:
Fehlermeldung:16.10.2024, 17:36:27.155 [error]: javascript.0 (1767186) script.js.common.Alte_history_dp_löschen: Fehler beim Lesen des Verzeichnisses: Error: ENOENT: no such file or directory, scandir ''@matt77che Was steht im Log?
Ablage für History -
Im >Script zeigt es mit bei den Zeilen
148
214
218
238
einen Fehler an// ******************************** // findOldHistoryDPs v1.0.6 // Copyright ©MCU // ******************************** // v1.0.1 directDelete-> true Möglichkeit die DatenDateien direkt löschen // v1.0.2 aliasId berücksichtigt bei enabledDPs // v1.0.3 zusätzliche Funktion listHistoryDPsWithaliasId('history.0'); // v1.0.4 zusätzlich: fillAlias -> alle aktiven history-DPs mit alias in Array withAliasIdInEnabled // fillEnabled_DPs -> nur die akitven History DPs in Array füllen -> enabled_DPs // useAliasIdInEnabled -> true -> AliasId wird in enabled_DPs verwendet (was richtig ist, da die history-Werte mit alias geschrieben werden) // v1.0.5 zusätzliche Funktion: alias-Suche in allen ioBroker-DPs (unabhängig von history) // v1.0.6 error-Handling bei searchDir(),activeHistory, DP-Auswahl History let findOldDP = '0_userdata.0.history.findOldDPs'; let findOldDPs_Old = findOldDP + '.old_DPs'; let findOldDPs_Enabled = findOldDP + '.enabled_DPs'; let findOldDPs_InDB = findOldDP + '.inDB_DPS'; let findOldDPs_BeginTS = findOldDP + '.dirStart'; let findOldDPs_EndTS = findOldDP + '.dirEnd'; let findOldDPs_Start = findOldDP + '.start'; let findOldDPs_DirectDel = findOldDP + '.directDelete'; let findOldDPs_fillEnabled = findOldDP + '.fillEnabled_DPs'; let findOldDPs_fillAlias = findOldDP + '.fillWithAliasId'; let findoldDPs_withAliasId = findOldDP + '.withAliasId_DPs'; let findOldDPs_useAliasInEnabled = findOldDP + '.useAliasIdInEnabled'; let activeHistoryDP = findOldDP + '.activeHistory'; let findOldDPs_aliasDPs = findOldDP + '.aliasDPs'; let findOldDPs_aliasDPs_search = findOldDP + '.aliasDPs_search'; createStateAsync(findOldDPs_aliasDPs, {read: true, write: true, name: "ioBroker-DPs mit aliasId (unabhängig history)", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_aliasDPs_search, {read: true, write: true, name: "Start Suche aliasIds in all ioBroker DPs", type: "boolean", role: "button", def: false }); let dbNamespace = 'history.0'; //createStateAsync(findOldDPs_Old, {read: true, write: true, name: "JSON für nicht genutzte DPs noch in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_Enabled, {read: true, write: true, name: "Aktive History-DPs in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findoldDPs_withAliasId, {read: true, write: true, name: "Aktive History-DPs mit aliasId", type: "string", role: "text", def: JSON.stringify([]) }); //createStateAsync(findOldDPs_InDB, {read: true, write: true, name: "Genutzte DPs in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_BeginTS, {read: true, write: true, name: "Startverzeichnis", type: "number", role: "", def: 0 }); createStateAsync(findOldDPs_EndTS, {read: true, write: true, name: "Endverzeichnis", type: "number", role: "", def: 10 }); //new Date().getTime() createStateAsync(findOldDPs_Start, {read: true, write: true, name: "Start Suche", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_fillEnabled, {read: true, write: true, name: "enabled_DPs suchen", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_fillAlias, {read: true, write: true, name: "withAliasId suchen", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_DirectDel, {read: true, write: true, name: "!!! Daten direkt löschen !!!", type: "boolean", role: "", def: false }); createStateAsync(findOldDPs_useAliasInEnabled, {read: true, write: true, name: "AliasId in Enabled_DPs", type: "boolean", role: "", def: false }); setTimeout(function(){ findHistoryInstance() },3000) on({id: findOldDPs_Start, change: "any"}, function (obj) { let value = obj.state.val; if (value){ //setState(findOldDPs_EndTS,parseInt(new Date().getTime()),false); let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ getEnabledHistoryDPs(dbNamespace); let fromDir = getState(findOldDPs_BeginTS).val; let toDir = getState(findOldDPs_EndTS).val; setTimeout(function(){ searchDir(fromDir,toDir); },5000); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } setStateDelayed(findOldDPs_Start,false,3000,false); } }); on({id: findOldDPs_fillEnabled, change: "any"}, function (obj) { let value = obj.state.val; if (value){ let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ getEnabledHistoryDPs(dbNamespace); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } setStateDelayed(findOldDPs_fillEnabled,false,3000,false); } }); on({id: findOldDPs_fillAlias, change: "any"}, function (obj) { let value = obj.state.val; if (value){ let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ listHistoryDPsWithaliasId(dbNamespace); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } setStateDelayed(findOldDPs_fillAlias,false,3000,false); } }); on({id: findOldDPs_aliasDPs_search, change: "any"}, function (obj) { let value = obj.state.val; if (value){ findDPsWithAliasId(); setStateDelayed(findOldDPs_aliasDPs_search,false,3000,false); } }); //listHistoryDPsWithaliasId('history.0'); function listHistoryDPsWithaliasId(adp){ let arr =[]; sendTo(adp, 'getEnabledDPs', {}, function (result) { let count = 0; for ( let dp in result){ let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId; if (dpHistoryAlias != '' && dpHistoryAlias != undefined){ arr.push({'dpId':dp,'aliasId':dpHistoryAlias}); count++; } } log('Anzahl der aktiv geloggten DPs mit aliasId: '+count); // setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false); //log(JSON.stringify(arr)); setState(findoldDPs_withAliasId,JSON.stringify(arr),false); }); } function findHistoryInstance(){ let historyDPs = $('system.adapter.history.*.alive') let arr = [] for(let i=0;i< historyDPs.length;i++){ let instance = historyDPs[i].split('.')[3] arr.push(instance) //console.log(historyDPs[i]) } console.log('Gefundene History Instanzen: '+arr) let obj = {'':'History auswählen'} for (let i=0;i<arr.length;i++){ obj['history.'+arr[i]] = 'history.'+arr[i] } // console.log(obj) let firstHistory = 'history.'+arr[0] if(existsState(activeHistoryDP)){ let histObj = getObject(activeHistoryDP) histObj.common.states = obj setObject(activeHistoryDP,histObj) }else{ createStateAsync(activeHistoryDP, {read: true, write: true, name: "Auswahl aktive History", type: "string", role: "", def: firstHistory, states: JSON.stringify(obj) }); } } //findDPsWithAliasId(); function findDPsWithAliasId(){ let allDPs = $('*'); let arr = []; log('Anzahl der States: '+allDPs.length); for (let i=0;i<allDPs.length;i++){ if (existsObject(allDPs[i])){ if (getObject(allDPs[i]).common != undefined){ if (getObject(allDPs[i]).common.alias!= undefined){ let aliasId = getObject(allDPs[i]).common.alias.id; if (aliasId != undefined){ //log(allDPs[i] +' mit aliasId: '+aliasId); //break; arr.push({'id':allDPs[i],'aliasId':aliasId}); } } } } } //log(arr); setState(findOldDPs_aliasDPs,JSON.stringify(arr),false); } /* setTimeout(function(){ getEnabledHistoryDPs(db); getHistoryDPinDB(db); },3000); */ // getEnabledHistoryDPs('history.0'); function getEnabledHistoryDPs(adp){ let arr =[]; let useAliasIdInenabled = getState(findOldDPs_useAliasInEnabled).val; sendTo(adp, 'getEnabledDPs', {}, function (result) { let count = 0; for ( let dp in result){ let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId; if (dpHistoryAlias != '' && dpHistoryAlias != undefined && useAliasIdInenabled){ arr.push(dpHistoryAlias); }else{ arr.push(dp); } count++; } log('Anzahl der aktiv geloggten DPs: '+count); setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false); }); } // getHistoryDPinDB('history.0'); function getHistoryDPinDB(db){ //Die 50 zuletzt gespeicherte Ereignisse für alle IDs holen: let beginTS = getState(findOldDPs_BeginTS).val; if (beginTS == 0){ beginTS = parseInt(new Date().getTime()) - 30*24*60*60*1000; } let endTS = getState(findOldDPs_EndTS).val; if(endTS == 0){ endTS = parseInt(new Date().getTime()); } sendTo(db, 'getHistory', { id: '*', options: { begin: beginTS, end: endTS, /* count: 50,*/ addId: true } }, function (result) { let dpArr =[]; let count = 0; for (var i = 0; i < result.result.length; i++) { if (!isObjInArray(dpArr,result.result[i].id)){ dpArr.push(result.result[i].id); count++; } //console.log(JSON.stringify(result.result[i]) + ' ' + new Date(result.result[i].ts).toISOString()); } dpArr = dpArr.sort(function(a, b) { return a > b; }); setStateAsync(findOldDPs_InDB,JSON.stringify(dpArr),false); //log('Anzahl der DPs in dem durchsuchten Bereich:'+count); log(count+' unterschiedliche DPs in "inDB" von '+ formatDate(beginTS,'DD.MM.YYYY hh:mm:ss')+' - '+formatDate(endTS,'DD.MM.YYYY hh:mm:ss')); //Vergleich der DPs let nonActiveArr = []; let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val); for (let i=0; i< dpArr.length;i++){ // dpArr[i] let vorhanden = false; for (let j=0; j< activeDPs.length;j++ ){ if (dpArr[i] === activeDPs[j]){ vorhanden = true; break; // vorhanden } } if (!vorhanden){ // wird nicht mehr geloggt von active nonActiveArr.push(dpArr[i]); } } setState(findOldDPs_Old,JSON.stringify(nonActiveArr),false); }); } function isObjInArray(arr,itemObj){ if (arr.length>0){ for (let x=0;x<arr.length-1;x++){ if (JSON.stringify(arr[x]) === JSON.stringify(itemObj)){ return true; } } return false; }else{ return false; } } // searchDir(0,10); function searchDir(fromDir, toDir){ let fs = require('fs'); let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val); let notActiveDPs = []; let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ let sysAdaptDir = getObject('system.adapter.'+dbNamespace).native.storeDir; console.warn('Ablage für History: '+dbNamespace+' im Verzeichnis: '+sysAdaptDir) fs.readdir(sysAdaptDir , function (erro, file) { if (erro) { log('Fehler beim Lesen des Verzeichnisses: ' + erro, 'error'); return; } log(file.length+' Verzeichnisse wurde(n) gelesen!'); log('fromDir und toDir dürfen nur im Bereich von 0 bis '+file.length +' sein! Bitte in 10er-Schritten verwenden!'); log('Es wurde ein Bereich von '+fromDir +' bis '+ toDir+' gewählt-> es dauert '+(toDir-fromDir)*2 +' Sekunden'); if (fromDir <0 || toDir >file.length){ }else{ for (let j=fromDir; j<toDir; j++){ //log(file[j]); if (file[j].indexOf('.')>=0 && file[j].length >8){ }else{ fs.readdir(sysAdaptDir+'/'+file[j] , function (error, files) { log(files.length+' DPs im Verzeichnis: '+file[j]); for (let i=0;i< files.length; i++){ let dpFiles = files[i].substring(8,files[i].length-5); if (isObjInArray(activeDPs,dpFiles)){ }else{ notActiveDPs.push({'id':dpFiles,'dir':file[j]}); } } }); //fs.unlinkSync('/opt/iobroker/iobroker-data/files/vis.0/'+jarvisDirName+'/'+files[i]+'/'+file[j]); } } setTimeout(function(){ log('Nicht aktive DPs in den ausgewählten Verzeichnissen: '+notActiveDPs.length); let olddir= ''; let directDelete = getState(findOldDPs_DirectDel).val; for (let i=0;i<notActiveDPs.length;i++){ if (directDelete){ // Direkt löschen fs.unlinkSync(sysAdaptDir+'/'+notActiveDPs[i].dir+'/history.'+notActiveDPs[i].id+'.json'); } if (notActiveDPs[i].dir != olddir){ if (directDelete){ log('Im Verzeichnis: '+notActiveDPs[i].dir +' folgende Dateien sind gelöscht!:','warn'); }else{ log('Im Verzeichnis: '+notActiveDPs[i].dir +' können folgende Dateien gelöscht werden:','warn'); } olddir = notActiveDPs[i].dir; } log(notActiveDPs[i].id); } },2000 * (toDir-fromDir)); // file.length } }); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } } -
Im >Script zeigt es mit bei den Zeilen
148
214
218
238
einen Fehler an// ******************************** // findOldHistoryDPs v1.0.6 // Copyright ©MCU // ******************************** // v1.0.1 directDelete-> true Möglichkeit die DatenDateien direkt löschen // v1.0.2 aliasId berücksichtigt bei enabledDPs // v1.0.3 zusätzliche Funktion listHistoryDPsWithaliasId('history.0'); // v1.0.4 zusätzlich: fillAlias -> alle aktiven history-DPs mit alias in Array withAliasIdInEnabled // fillEnabled_DPs -> nur die akitven History DPs in Array füllen -> enabled_DPs // useAliasIdInEnabled -> true -> AliasId wird in enabled_DPs verwendet (was richtig ist, da die history-Werte mit alias geschrieben werden) // v1.0.5 zusätzliche Funktion: alias-Suche in allen ioBroker-DPs (unabhängig von history) // v1.0.6 error-Handling bei searchDir(),activeHistory, DP-Auswahl History let findOldDP = '0_userdata.0.history.findOldDPs'; let findOldDPs_Old = findOldDP + '.old_DPs'; let findOldDPs_Enabled = findOldDP + '.enabled_DPs'; let findOldDPs_InDB = findOldDP + '.inDB_DPS'; let findOldDPs_BeginTS = findOldDP + '.dirStart'; let findOldDPs_EndTS = findOldDP + '.dirEnd'; let findOldDPs_Start = findOldDP + '.start'; let findOldDPs_DirectDel = findOldDP + '.directDelete'; let findOldDPs_fillEnabled = findOldDP + '.fillEnabled_DPs'; let findOldDPs_fillAlias = findOldDP + '.fillWithAliasId'; let findoldDPs_withAliasId = findOldDP + '.withAliasId_DPs'; let findOldDPs_useAliasInEnabled = findOldDP + '.useAliasIdInEnabled'; let activeHistoryDP = findOldDP + '.activeHistory'; let findOldDPs_aliasDPs = findOldDP + '.aliasDPs'; let findOldDPs_aliasDPs_search = findOldDP + '.aliasDPs_search'; createStateAsync(findOldDPs_aliasDPs, {read: true, write: true, name: "ioBroker-DPs mit aliasId (unabhängig history)", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_aliasDPs_search, {read: true, write: true, name: "Start Suche aliasIds in all ioBroker DPs", type: "boolean", role: "button", def: false }); let dbNamespace = 'history.0'; //createStateAsync(findOldDPs_Old, {read: true, write: true, name: "JSON für nicht genutzte DPs noch in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_Enabled, {read: true, write: true, name: "Aktive History-DPs in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findoldDPs_withAliasId, {read: true, write: true, name: "Aktive History-DPs mit aliasId", type: "string", role: "text", def: JSON.stringify([]) }); //createStateAsync(findOldDPs_InDB, {read: true, write: true, name: "Genutzte DPs in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_BeginTS, {read: true, write: true, name: "Startverzeichnis", type: "number", role: "", def: 0 }); createStateAsync(findOldDPs_EndTS, {read: true, write: true, name: "Endverzeichnis", type: "number", role: "", def: 10 }); //new Date().getTime() createStateAsync(findOldDPs_Start, {read: true, write: true, name: "Start Suche", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_fillEnabled, {read: true, write: true, name: "enabled_DPs suchen", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_fillAlias, {read: true, write: true, name: "withAliasId suchen", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_DirectDel, {read: true, write: true, name: "!!! Daten direkt löschen !!!", type: "boolean", role: "", def: false }); createStateAsync(findOldDPs_useAliasInEnabled, {read: true, write: true, name: "AliasId in Enabled_DPs", type: "boolean", role: "", def: false }); setTimeout(function(){ findHistoryInstance() },3000) on({id: findOldDPs_Start, change: "any"}, function (obj) { let value = obj.state.val; if (value){ //setState(findOldDPs_EndTS,parseInt(new Date().getTime()),false); let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ getEnabledHistoryDPs(dbNamespace); let fromDir = getState(findOldDPs_BeginTS).val; let toDir = getState(findOldDPs_EndTS).val; setTimeout(function(){ searchDir(fromDir,toDir); },5000); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } setStateDelayed(findOldDPs_Start,false,3000,false); } }); on({id: findOldDPs_fillEnabled, change: "any"}, function (obj) { let value = obj.state.val; if (value){ let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ getEnabledHistoryDPs(dbNamespace); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } setStateDelayed(findOldDPs_fillEnabled,false,3000,false); } }); on({id: findOldDPs_fillAlias, change: "any"}, function (obj) { let value = obj.state.val; if (value){ let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ listHistoryDPsWithaliasId(dbNamespace); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } setStateDelayed(findOldDPs_fillAlias,false,3000,false); } }); on({id: findOldDPs_aliasDPs_search, change: "any"}, function (obj) { let value = obj.state.val; if (value){ findDPsWithAliasId(); setStateDelayed(findOldDPs_aliasDPs_search,false,3000,false); } }); //listHistoryDPsWithaliasId('history.0'); function listHistoryDPsWithaliasId(adp){ let arr =[]; sendTo(adp, 'getEnabledDPs', {}, function (result) { let count = 0; for ( let dp in result){ let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId; if (dpHistoryAlias != '' && dpHistoryAlias != undefined){ arr.push({'dpId':dp,'aliasId':dpHistoryAlias}); count++; } } log('Anzahl der aktiv geloggten DPs mit aliasId: '+count); // setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false); //log(JSON.stringify(arr)); setState(findoldDPs_withAliasId,JSON.stringify(arr),false); }); } function findHistoryInstance(){ let historyDPs = $('system.adapter.history.*.alive') let arr = [] for(let i=0;i< historyDPs.length;i++){ let instance = historyDPs[i].split('.')[3] arr.push(instance) //console.log(historyDPs[i]) } console.log('Gefundene History Instanzen: '+arr) let obj = {'':'History auswählen'} for (let i=0;i<arr.length;i++){ obj['history.'+arr[i]] = 'history.'+arr[i] } // console.log(obj) let firstHistory = 'history.'+arr[0] if(existsState(activeHistoryDP)){ let histObj = getObject(activeHistoryDP) histObj.common.states = obj setObject(activeHistoryDP,histObj) }else{ createStateAsync(activeHistoryDP, {read: true, write: true, name: "Auswahl aktive History", type: "string", role: "", def: firstHistory, states: JSON.stringify(obj) }); } } //findDPsWithAliasId(); function findDPsWithAliasId(){ let allDPs = $('*'); let arr = []; log('Anzahl der States: '+allDPs.length); for (let i=0;i<allDPs.length;i++){ if (existsObject(allDPs[i])){ if (getObject(allDPs[i]).common != undefined){ if (getObject(allDPs[i]).common.alias!= undefined){ let aliasId = getObject(allDPs[i]).common.alias.id; if (aliasId != undefined){ //log(allDPs[i] +' mit aliasId: '+aliasId); //break; arr.push({'id':allDPs[i],'aliasId':aliasId}); } } } } } //log(arr); setState(findOldDPs_aliasDPs,JSON.stringify(arr),false); } /* setTimeout(function(){ getEnabledHistoryDPs(db); getHistoryDPinDB(db); },3000); */ // getEnabledHistoryDPs('history.0'); function getEnabledHistoryDPs(adp){ let arr =[]; let useAliasIdInenabled = getState(findOldDPs_useAliasInEnabled).val; sendTo(adp, 'getEnabledDPs', {}, function (result) { let count = 0; for ( let dp in result){ let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId; if (dpHistoryAlias != '' && dpHistoryAlias != undefined && useAliasIdInenabled){ arr.push(dpHistoryAlias); }else{ arr.push(dp); } count++; } log('Anzahl der aktiv geloggten DPs: '+count); setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false); }); } // getHistoryDPinDB('history.0'); function getHistoryDPinDB(db){ //Die 50 zuletzt gespeicherte Ereignisse für alle IDs holen: let beginTS = getState(findOldDPs_BeginTS).val; if (beginTS == 0){ beginTS = parseInt(new Date().getTime()) - 30*24*60*60*1000; } let endTS = getState(findOldDPs_EndTS).val; if(endTS == 0){ endTS = parseInt(new Date().getTime()); } sendTo(db, 'getHistory', { id: '*', options: { begin: beginTS, end: endTS, /* count: 50,*/ addId: true } }, function (result) { let dpArr =[]; let count = 0; for (var i = 0; i < result.result.length; i++) { if (!isObjInArray(dpArr,result.result[i].id)){ dpArr.push(result.result[i].id); count++; } //console.log(JSON.stringify(result.result[i]) + ' ' + new Date(result.result[i].ts).toISOString()); } dpArr = dpArr.sort(function(a, b) { return a > b; }); setStateAsync(findOldDPs_InDB,JSON.stringify(dpArr),false); //log('Anzahl der DPs in dem durchsuchten Bereich:'+count); log(count+' unterschiedliche DPs in "inDB" von '+ formatDate(beginTS,'DD.MM.YYYY hh:mm:ss')+' - '+formatDate(endTS,'DD.MM.YYYY hh:mm:ss')); //Vergleich der DPs let nonActiveArr = []; let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val); for (let i=0; i< dpArr.length;i++){ // dpArr[i] let vorhanden = false; for (let j=0; j< activeDPs.length;j++ ){ if (dpArr[i] === activeDPs[j]){ vorhanden = true; break; // vorhanden } } if (!vorhanden){ // wird nicht mehr geloggt von active nonActiveArr.push(dpArr[i]); } } setState(findOldDPs_Old,JSON.stringify(nonActiveArr),false); }); } function isObjInArray(arr,itemObj){ if (arr.length>0){ for (let x=0;x<arr.length-1;x++){ if (JSON.stringify(arr[x]) === JSON.stringify(itemObj)){ return true; } } return false; }else{ return false; } } // searchDir(0,10); function searchDir(fromDir, toDir){ let fs = require('fs'); let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val); let notActiveDPs = []; let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ let sysAdaptDir = getObject('system.adapter.'+dbNamespace).native.storeDir; console.warn('Ablage für History: '+dbNamespace+' im Verzeichnis: '+sysAdaptDir) fs.readdir(sysAdaptDir , function (erro, file) { if (erro) { log('Fehler beim Lesen des Verzeichnisses: ' + erro, 'error'); return; } log(file.length+' Verzeichnisse wurde(n) gelesen!'); log('fromDir und toDir dürfen nur im Bereich von 0 bis '+file.length +' sein! Bitte in 10er-Schritten verwenden!'); log('Es wurde ein Bereich von '+fromDir +' bis '+ toDir+' gewählt-> es dauert '+(toDir-fromDir)*2 +' Sekunden'); if (fromDir <0 || toDir >file.length){ }else{ for (let j=fromDir; j<toDir; j++){ //log(file[j]); if (file[j].indexOf('.')>=0 && file[j].length >8){ }else{ fs.readdir(sysAdaptDir+'/'+file[j] , function (error, files) { log(files.length+' DPs im Verzeichnis: '+file[j]); for (let i=0;i< files.length; i++){ let dpFiles = files[i].substring(8,files[i].length-5); if (isObjInArray(activeDPs,dpFiles)){ }else{ notActiveDPs.push({'id':dpFiles,'dir':file[j]}); } } }); //fs.unlinkSync('/opt/iobroker/iobroker-data/files/vis.0/'+jarvisDirName+'/'+files[i]+'/'+file[j]); } } setTimeout(function(){ log('Nicht aktive DPs in den ausgewählten Verzeichnissen: '+notActiveDPs.length); let olddir= ''; let directDelete = getState(findOldDPs_DirectDel).val; for (let i=0;i<notActiveDPs.length;i++){ if (directDelete){ // Direkt löschen fs.unlinkSync(sysAdaptDir+'/'+notActiveDPs[i].dir+'/history.'+notActiveDPs[i].id+'.json'); } if (notActiveDPs[i].dir != olddir){ if (directDelete){ log('Im Verzeichnis: '+notActiveDPs[i].dir +' folgende Dateien sind gelöscht!:','warn'); }else{ log('Im Verzeichnis: '+notActiveDPs[i].dir +' können folgende Dateien gelöscht werden:','warn'); } olddir = notActiveDPs[i].dir; } log(notActiveDPs[i].id); } },2000 * (toDir-fromDir)); // file.length } }); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } }@matt77che Die angezeigten Fehler sind nicht das Problem.
Es geht um das Verzeichnis in dem die Daten liegen.
Was steht im Log?
-> Ablage für History
Welche Instanz wurde ausgewähltWie sind die Einstellungen in den History-Instanzen.
-
Im >Script zeigt es mit bei den Zeilen
148
214
218
238
einen Fehler an// ******************************** // findOldHistoryDPs v1.0.6 // Copyright ©MCU // ******************************** // v1.0.1 directDelete-> true Möglichkeit die DatenDateien direkt löschen // v1.0.2 aliasId berücksichtigt bei enabledDPs // v1.0.3 zusätzliche Funktion listHistoryDPsWithaliasId('history.0'); // v1.0.4 zusätzlich: fillAlias -> alle aktiven history-DPs mit alias in Array withAliasIdInEnabled // fillEnabled_DPs -> nur die akitven History DPs in Array füllen -> enabled_DPs // useAliasIdInEnabled -> true -> AliasId wird in enabled_DPs verwendet (was richtig ist, da die history-Werte mit alias geschrieben werden) // v1.0.5 zusätzliche Funktion: alias-Suche in allen ioBroker-DPs (unabhängig von history) // v1.0.6 error-Handling bei searchDir(),activeHistory, DP-Auswahl History let findOldDP = '0_userdata.0.history.findOldDPs'; let findOldDPs_Old = findOldDP + '.old_DPs'; let findOldDPs_Enabled = findOldDP + '.enabled_DPs'; let findOldDPs_InDB = findOldDP + '.inDB_DPS'; let findOldDPs_BeginTS = findOldDP + '.dirStart'; let findOldDPs_EndTS = findOldDP + '.dirEnd'; let findOldDPs_Start = findOldDP + '.start'; let findOldDPs_DirectDel = findOldDP + '.directDelete'; let findOldDPs_fillEnabled = findOldDP + '.fillEnabled_DPs'; let findOldDPs_fillAlias = findOldDP + '.fillWithAliasId'; let findoldDPs_withAliasId = findOldDP + '.withAliasId_DPs'; let findOldDPs_useAliasInEnabled = findOldDP + '.useAliasIdInEnabled'; let activeHistoryDP = findOldDP + '.activeHistory'; let findOldDPs_aliasDPs = findOldDP + '.aliasDPs'; let findOldDPs_aliasDPs_search = findOldDP + '.aliasDPs_search'; createStateAsync(findOldDPs_aliasDPs, {read: true, write: true, name: "ioBroker-DPs mit aliasId (unabhängig history)", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_aliasDPs_search, {read: true, write: true, name: "Start Suche aliasIds in all ioBroker DPs", type: "boolean", role: "button", def: false }); let dbNamespace = 'history.0'; //createStateAsync(findOldDPs_Old, {read: true, write: true, name: "JSON für nicht genutzte DPs noch in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_Enabled, {read: true, write: true, name: "Aktive History-DPs in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findoldDPs_withAliasId, {read: true, write: true, name: "Aktive History-DPs mit aliasId", type: "string", role: "text", def: JSON.stringify([]) }); //createStateAsync(findOldDPs_InDB, {read: true, write: true, name: "Genutzte DPs in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_BeginTS, {read: true, write: true, name: "Startverzeichnis", type: "number", role: "", def: 0 }); createStateAsync(findOldDPs_EndTS, {read: true, write: true, name: "Endverzeichnis", type: "number", role: "", def: 10 }); //new Date().getTime() createStateAsync(findOldDPs_Start, {read: true, write: true, name: "Start Suche", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_fillEnabled, {read: true, write: true, name: "enabled_DPs suchen", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_fillAlias, {read: true, write: true, name: "withAliasId suchen", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_DirectDel, {read: true, write: true, name: "!!! Daten direkt löschen !!!", type: "boolean", role: "", def: false }); createStateAsync(findOldDPs_useAliasInEnabled, {read: true, write: true, name: "AliasId in Enabled_DPs", type: "boolean", role: "", def: false }); setTimeout(function(){ findHistoryInstance() },3000) on({id: findOldDPs_Start, change: "any"}, function (obj) { let value = obj.state.val; if (value){ //setState(findOldDPs_EndTS,parseInt(new Date().getTime()),false); let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ getEnabledHistoryDPs(dbNamespace); let fromDir = getState(findOldDPs_BeginTS).val; let toDir = getState(findOldDPs_EndTS).val; setTimeout(function(){ searchDir(fromDir,toDir); },5000); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } setStateDelayed(findOldDPs_Start,false,3000,false); } }); on({id: findOldDPs_fillEnabled, change: "any"}, function (obj) { let value = obj.state.val; if (value){ let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ getEnabledHistoryDPs(dbNamespace); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } setStateDelayed(findOldDPs_fillEnabled,false,3000,false); } }); on({id: findOldDPs_fillAlias, change: "any"}, function (obj) { let value = obj.state.val; if (value){ let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ listHistoryDPsWithaliasId(dbNamespace); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } setStateDelayed(findOldDPs_fillAlias,false,3000,false); } }); on({id: findOldDPs_aliasDPs_search, change: "any"}, function (obj) { let value = obj.state.val; if (value){ findDPsWithAliasId(); setStateDelayed(findOldDPs_aliasDPs_search,false,3000,false); } }); //listHistoryDPsWithaliasId('history.0'); function listHistoryDPsWithaliasId(adp){ let arr =[]; sendTo(adp, 'getEnabledDPs', {}, function (result) { let count = 0; for ( let dp in result){ let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId; if (dpHistoryAlias != '' && dpHistoryAlias != undefined){ arr.push({'dpId':dp,'aliasId':dpHistoryAlias}); count++; } } log('Anzahl der aktiv geloggten DPs mit aliasId: '+count); // setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false); //log(JSON.stringify(arr)); setState(findoldDPs_withAliasId,JSON.stringify(arr),false); }); } function findHistoryInstance(){ let historyDPs = $('system.adapter.history.*.alive') let arr = [] for(let i=0;i< historyDPs.length;i++){ let instance = historyDPs[i].split('.')[3] arr.push(instance) //console.log(historyDPs[i]) } console.log('Gefundene History Instanzen: '+arr) let obj = {'':'History auswählen'} for (let i=0;i<arr.length;i++){ obj['history.'+arr[i]] = 'history.'+arr[i] } // console.log(obj) let firstHistory = 'history.'+arr[0] if(existsState(activeHistoryDP)){ let histObj = getObject(activeHistoryDP) histObj.common.states = obj setObject(activeHistoryDP,histObj) }else{ createStateAsync(activeHistoryDP, {read: true, write: true, name: "Auswahl aktive History", type: "string", role: "", def: firstHistory, states: JSON.stringify(obj) }); } } //findDPsWithAliasId(); function findDPsWithAliasId(){ let allDPs = $('*'); let arr = []; log('Anzahl der States: '+allDPs.length); for (let i=0;i<allDPs.length;i++){ if (existsObject(allDPs[i])){ if (getObject(allDPs[i]).common != undefined){ if (getObject(allDPs[i]).common.alias!= undefined){ let aliasId = getObject(allDPs[i]).common.alias.id; if (aliasId != undefined){ //log(allDPs[i] +' mit aliasId: '+aliasId); //break; arr.push({'id':allDPs[i],'aliasId':aliasId}); } } } } } //log(arr); setState(findOldDPs_aliasDPs,JSON.stringify(arr),false); } /* setTimeout(function(){ getEnabledHistoryDPs(db); getHistoryDPinDB(db); },3000); */ // getEnabledHistoryDPs('history.0'); function getEnabledHistoryDPs(adp){ let arr =[]; let useAliasIdInenabled = getState(findOldDPs_useAliasInEnabled).val; sendTo(adp, 'getEnabledDPs', {}, function (result) { let count = 0; for ( let dp in result){ let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId; if (dpHistoryAlias != '' && dpHistoryAlias != undefined && useAliasIdInenabled){ arr.push(dpHistoryAlias); }else{ arr.push(dp); } count++; } log('Anzahl der aktiv geloggten DPs: '+count); setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false); }); } // getHistoryDPinDB('history.0'); function getHistoryDPinDB(db){ //Die 50 zuletzt gespeicherte Ereignisse für alle IDs holen: let beginTS = getState(findOldDPs_BeginTS).val; if (beginTS == 0){ beginTS = parseInt(new Date().getTime()) - 30*24*60*60*1000; } let endTS = getState(findOldDPs_EndTS).val; if(endTS == 0){ endTS = parseInt(new Date().getTime()); } sendTo(db, 'getHistory', { id: '*', options: { begin: beginTS, end: endTS, /* count: 50,*/ addId: true } }, function (result) { let dpArr =[]; let count = 0; for (var i = 0; i < result.result.length; i++) { if (!isObjInArray(dpArr,result.result[i].id)){ dpArr.push(result.result[i].id); count++; } //console.log(JSON.stringify(result.result[i]) + ' ' + new Date(result.result[i].ts).toISOString()); } dpArr = dpArr.sort(function(a, b) { return a > b; }); setStateAsync(findOldDPs_InDB,JSON.stringify(dpArr),false); //log('Anzahl der DPs in dem durchsuchten Bereich:'+count); log(count+' unterschiedliche DPs in "inDB" von '+ formatDate(beginTS,'DD.MM.YYYY hh:mm:ss')+' - '+formatDate(endTS,'DD.MM.YYYY hh:mm:ss')); //Vergleich der DPs let nonActiveArr = []; let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val); for (let i=0; i< dpArr.length;i++){ // dpArr[i] let vorhanden = false; for (let j=0; j< activeDPs.length;j++ ){ if (dpArr[i] === activeDPs[j]){ vorhanden = true; break; // vorhanden } } if (!vorhanden){ // wird nicht mehr geloggt von active nonActiveArr.push(dpArr[i]); } } setState(findOldDPs_Old,JSON.stringify(nonActiveArr),false); }); } function isObjInArray(arr,itemObj){ if (arr.length>0){ for (let x=0;x<arr.length-1;x++){ if (JSON.stringify(arr[x]) === JSON.stringify(itemObj)){ return true; } } return false; }else{ return false; } } // searchDir(0,10); function searchDir(fromDir, toDir){ let fs = require('fs'); let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val); let notActiveDPs = []; let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ let sysAdaptDir = getObject('system.adapter.'+dbNamespace).native.storeDir; console.warn('Ablage für History: '+dbNamespace+' im Verzeichnis: '+sysAdaptDir) fs.readdir(sysAdaptDir , function (erro, file) { if (erro) { log('Fehler beim Lesen des Verzeichnisses: ' + erro, 'error'); return; } log(file.length+' Verzeichnisse wurde(n) gelesen!'); log('fromDir und toDir dürfen nur im Bereich von 0 bis '+file.length +' sein! Bitte in 10er-Schritten verwenden!'); log('Es wurde ein Bereich von '+fromDir +' bis '+ toDir+' gewählt-> es dauert '+(toDir-fromDir)*2 +' Sekunden'); if (fromDir <0 || toDir >file.length){ }else{ for (let j=fromDir; j<toDir; j++){ //log(file[j]); if (file[j].indexOf('.')>=0 && file[j].length >8){ }else{ fs.readdir(sysAdaptDir+'/'+file[j] , function (error, files) { log(files.length+' DPs im Verzeichnis: '+file[j]); for (let i=0;i< files.length; i++){ let dpFiles = files[i].substring(8,files[i].length-5); if (isObjInArray(activeDPs,dpFiles)){ }else{ notActiveDPs.push({'id':dpFiles,'dir':file[j]}); } } }); //fs.unlinkSync('/opt/iobroker/iobroker-data/files/vis.0/'+jarvisDirName+'/'+files[i]+'/'+file[j]); } } setTimeout(function(){ log('Nicht aktive DPs in den ausgewählten Verzeichnissen: '+notActiveDPs.length); let olddir= ''; let directDelete = getState(findOldDPs_DirectDel).val; for (let i=0;i<notActiveDPs.length;i++){ if (directDelete){ // Direkt löschen fs.unlinkSync(sysAdaptDir+'/'+notActiveDPs[i].dir+'/history.'+notActiveDPs[i].id+'.json'); } if (notActiveDPs[i].dir != olddir){ if (directDelete){ log('Im Verzeichnis: '+notActiveDPs[i].dir +' folgende Dateien sind gelöscht!:','warn'); }else{ log('Im Verzeichnis: '+notActiveDPs[i].dir +' können folgende Dateien gelöscht werden:','warn'); } olddir = notActiveDPs[i].dir; } log(notActiveDPs[i].id); } },2000 * (toDir-fromDir)); // file.length } }); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } } -
-
Im >Script zeigt es mit bei den Zeilen
148
214
218
238
einen Fehler an// ******************************** // findOldHistoryDPs v1.0.6 // Copyright ©MCU // ******************************** // v1.0.1 directDelete-> true Möglichkeit die DatenDateien direkt löschen // v1.0.2 aliasId berücksichtigt bei enabledDPs // v1.0.3 zusätzliche Funktion listHistoryDPsWithaliasId('history.0'); // v1.0.4 zusätzlich: fillAlias -> alle aktiven history-DPs mit alias in Array withAliasIdInEnabled // fillEnabled_DPs -> nur die akitven History DPs in Array füllen -> enabled_DPs // useAliasIdInEnabled -> true -> AliasId wird in enabled_DPs verwendet (was richtig ist, da die history-Werte mit alias geschrieben werden) // v1.0.5 zusätzliche Funktion: alias-Suche in allen ioBroker-DPs (unabhängig von history) // v1.0.6 error-Handling bei searchDir(),activeHistory, DP-Auswahl History let findOldDP = '0_userdata.0.history.findOldDPs'; let findOldDPs_Old = findOldDP + '.old_DPs'; let findOldDPs_Enabled = findOldDP + '.enabled_DPs'; let findOldDPs_InDB = findOldDP + '.inDB_DPS'; let findOldDPs_BeginTS = findOldDP + '.dirStart'; let findOldDPs_EndTS = findOldDP + '.dirEnd'; let findOldDPs_Start = findOldDP + '.start'; let findOldDPs_DirectDel = findOldDP + '.directDelete'; let findOldDPs_fillEnabled = findOldDP + '.fillEnabled_DPs'; let findOldDPs_fillAlias = findOldDP + '.fillWithAliasId'; let findoldDPs_withAliasId = findOldDP + '.withAliasId_DPs'; let findOldDPs_useAliasInEnabled = findOldDP + '.useAliasIdInEnabled'; let activeHistoryDP = findOldDP + '.activeHistory'; let findOldDPs_aliasDPs = findOldDP + '.aliasDPs'; let findOldDPs_aliasDPs_search = findOldDP + '.aliasDPs_search'; createStateAsync(findOldDPs_aliasDPs, {read: true, write: true, name: "ioBroker-DPs mit aliasId (unabhängig history)", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_aliasDPs_search, {read: true, write: true, name: "Start Suche aliasIds in all ioBroker DPs", type: "boolean", role: "button", def: false }); let dbNamespace = 'history.0'; //createStateAsync(findOldDPs_Old, {read: true, write: true, name: "JSON für nicht genutzte DPs noch in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_Enabled, {read: true, write: true, name: "Aktive History-DPs in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findoldDPs_withAliasId, {read: true, write: true, name: "Aktive History-DPs mit aliasId", type: "string", role: "text", def: JSON.stringify([]) }); //createStateAsync(findOldDPs_InDB, {read: true, write: true, name: "Genutzte DPs in DB", type: "string", role: "text", def: JSON.stringify([]) }); createStateAsync(findOldDPs_BeginTS, {read: true, write: true, name: "Startverzeichnis", type: "number", role: "", def: 0 }); createStateAsync(findOldDPs_EndTS, {read: true, write: true, name: "Endverzeichnis", type: "number", role: "", def: 10 }); //new Date().getTime() createStateAsync(findOldDPs_Start, {read: true, write: true, name: "Start Suche", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_fillEnabled, {read: true, write: true, name: "enabled_DPs suchen", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_fillAlias, {read: true, write: true, name: "withAliasId suchen", type: "boolean", role: "button", def: false }); createStateAsync(findOldDPs_DirectDel, {read: true, write: true, name: "!!! Daten direkt löschen !!!", type: "boolean", role: "", def: false }); createStateAsync(findOldDPs_useAliasInEnabled, {read: true, write: true, name: "AliasId in Enabled_DPs", type: "boolean", role: "", def: false }); setTimeout(function(){ findHistoryInstance() },3000) on({id: findOldDPs_Start, change: "any"}, function (obj) { let value = obj.state.val; if (value){ //setState(findOldDPs_EndTS,parseInt(new Date().getTime()),false); let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ getEnabledHistoryDPs(dbNamespace); let fromDir = getState(findOldDPs_BeginTS).val; let toDir = getState(findOldDPs_EndTS).val; setTimeout(function(){ searchDir(fromDir,toDir); },5000); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } setStateDelayed(findOldDPs_Start,false,3000,false); } }); on({id: findOldDPs_fillEnabled, change: "any"}, function (obj) { let value = obj.state.val; if (value){ let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ getEnabledHistoryDPs(dbNamespace); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } setStateDelayed(findOldDPs_fillEnabled,false,3000,false); } }); on({id: findOldDPs_fillAlias, change: "any"}, function (obj) { let value = obj.state.val; if (value){ let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ listHistoryDPsWithaliasId(dbNamespace); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } setStateDelayed(findOldDPs_fillAlias,false,3000,false); } }); on({id: findOldDPs_aliasDPs_search, change: "any"}, function (obj) { let value = obj.state.val; if (value){ findDPsWithAliasId(); setStateDelayed(findOldDPs_aliasDPs_search,false,3000,false); } }); //listHistoryDPsWithaliasId('history.0'); function listHistoryDPsWithaliasId(adp){ let arr =[]; sendTo(adp, 'getEnabledDPs', {}, function (result) { let count = 0; for ( let dp in result){ let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId; if (dpHistoryAlias != '' && dpHistoryAlias != undefined){ arr.push({'dpId':dp,'aliasId':dpHistoryAlias}); count++; } } log('Anzahl der aktiv geloggten DPs mit aliasId: '+count); // setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false); //log(JSON.stringify(arr)); setState(findoldDPs_withAliasId,JSON.stringify(arr),false); }); } function findHistoryInstance(){ let historyDPs = $('system.adapter.history.*.alive') let arr = [] for(let i=0;i< historyDPs.length;i++){ let instance = historyDPs[i].split('.')[3] arr.push(instance) //console.log(historyDPs[i]) } console.log('Gefundene History Instanzen: '+arr) let obj = {'':'History auswählen'} for (let i=0;i<arr.length;i++){ obj['history.'+arr[i]] = 'history.'+arr[i] } // console.log(obj) let firstHistory = 'history.'+arr[0] if(existsState(activeHistoryDP)){ let histObj = getObject(activeHistoryDP) histObj.common.states = obj setObject(activeHistoryDP,histObj) }else{ createStateAsync(activeHistoryDP, {read: true, write: true, name: "Auswahl aktive History", type: "string", role: "", def: firstHistory, states: JSON.stringify(obj) }); } } //findDPsWithAliasId(); function findDPsWithAliasId(){ let allDPs = $('*'); let arr = []; log('Anzahl der States: '+allDPs.length); for (let i=0;i<allDPs.length;i++){ if (existsObject(allDPs[i])){ if (getObject(allDPs[i]).common != undefined){ if (getObject(allDPs[i]).common.alias!= undefined){ let aliasId = getObject(allDPs[i]).common.alias.id; if (aliasId != undefined){ //log(allDPs[i] +' mit aliasId: '+aliasId); //break; arr.push({'id':allDPs[i],'aliasId':aliasId}); } } } } } //log(arr); setState(findOldDPs_aliasDPs,JSON.stringify(arr),false); } /* setTimeout(function(){ getEnabledHistoryDPs(db); getHistoryDPinDB(db); },3000); */ // getEnabledHistoryDPs('history.0'); function getEnabledHistoryDPs(adp){ let arr =[]; let useAliasIdInenabled = getState(findOldDPs_useAliasInEnabled).val; sendTo(adp, 'getEnabledDPs', {}, function (result) { let count = 0; for ( let dp in result){ let dpHistoryAlias = getObject(dp).common.custom[adp].aliasId; if (dpHistoryAlias != '' && dpHistoryAlias != undefined && useAliasIdInenabled){ arr.push(dpHistoryAlias); }else{ arr.push(dp); } count++; } log('Anzahl der aktiv geloggten DPs: '+count); setStateAsync(findOldDPs_Enabled,JSON.stringify(arr),false); }); } // getHistoryDPinDB('history.0'); function getHistoryDPinDB(db){ //Die 50 zuletzt gespeicherte Ereignisse für alle IDs holen: let beginTS = getState(findOldDPs_BeginTS).val; if (beginTS == 0){ beginTS = parseInt(new Date().getTime()) - 30*24*60*60*1000; } let endTS = getState(findOldDPs_EndTS).val; if(endTS == 0){ endTS = parseInt(new Date().getTime()); } sendTo(db, 'getHistory', { id: '*', options: { begin: beginTS, end: endTS, /* count: 50,*/ addId: true } }, function (result) { let dpArr =[]; let count = 0; for (var i = 0; i < result.result.length; i++) { if (!isObjInArray(dpArr,result.result[i].id)){ dpArr.push(result.result[i].id); count++; } //console.log(JSON.stringify(result.result[i]) + ' ' + new Date(result.result[i].ts).toISOString()); } dpArr = dpArr.sort(function(a, b) { return a > b; }); setStateAsync(findOldDPs_InDB,JSON.stringify(dpArr),false); //log('Anzahl der DPs in dem durchsuchten Bereich:'+count); log(count+' unterschiedliche DPs in "inDB" von '+ formatDate(beginTS,'DD.MM.YYYY hh:mm:ss')+' - '+formatDate(endTS,'DD.MM.YYYY hh:mm:ss')); //Vergleich der DPs let nonActiveArr = []; let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val); for (let i=0; i< dpArr.length;i++){ // dpArr[i] let vorhanden = false; for (let j=0; j< activeDPs.length;j++ ){ if (dpArr[i] === activeDPs[j]){ vorhanden = true; break; // vorhanden } } if (!vorhanden){ // wird nicht mehr geloggt von active nonActiveArr.push(dpArr[i]); } } setState(findOldDPs_Old,JSON.stringify(nonActiveArr),false); }); } function isObjInArray(arr,itemObj){ if (arr.length>0){ for (let x=0;x<arr.length-1;x++){ if (JSON.stringify(arr[x]) === JSON.stringify(itemObj)){ return true; } } return false; }else{ return false; } } // searchDir(0,10); function searchDir(fromDir, toDir){ let fs = require('fs'); let activeDPs = JSON.parse(getState(findOldDPs_Enabled).val); let notActiveDPs = []; let dbNamespace = getState(activeHistoryDP).val if(dbNamespace !=''){ let sysAdaptDir = getObject('system.adapter.'+dbNamespace).native.storeDir; console.warn('Ablage für History: '+dbNamespace+' im Verzeichnis: '+sysAdaptDir) fs.readdir(sysAdaptDir , function (erro, file) { if (erro) { log('Fehler beim Lesen des Verzeichnisses: ' + erro, 'error'); return; } log(file.length+' Verzeichnisse wurde(n) gelesen!'); log('fromDir und toDir dürfen nur im Bereich von 0 bis '+file.length +' sein! Bitte in 10er-Schritten verwenden!'); log('Es wurde ein Bereich von '+fromDir +' bis '+ toDir+' gewählt-> es dauert '+(toDir-fromDir)*2 +' Sekunden'); if (fromDir <0 || toDir >file.length){ }else{ for (let j=fromDir; j<toDir; j++){ //log(file[j]); if (file[j].indexOf('.')>=0 && file[j].length >8){ }else{ fs.readdir(sysAdaptDir+'/'+file[j] , function (error, files) { log(files.length+' DPs im Verzeichnis: '+file[j]); for (let i=0;i< files.length; i++){ let dpFiles = files[i].substring(8,files[i].length-5); if (isObjInArray(activeDPs,dpFiles)){ }else{ notActiveDPs.push({'id':dpFiles,'dir':file[j]}); } } }); //fs.unlinkSync('/opt/iobroker/iobroker-data/files/vis.0/'+jarvisDirName+'/'+files[i]+'/'+file[j]); } } setTimeout(function(){ log('Nicht aktive DPs in den ausgewählten Verzeichnissen: '+notActiveDPs.length); let olddir= ''; let directDelete = getState(findOldDPs_DirectDel).val; for (let i=0;i<notActiveDPs.length;i++){ if (directDelete){ // Direkt löschen fs.unlinkSync(sysAdaptDir+'/'+notActiveDPs[i].dir+'/history.'+notActiveDPs[i].id+'.json'); } if (notActiveDPs[i].dir != olddir){ if (directDelete){ log('Im Verzeichnis: '+notActiveDPs[i].dir +' folgende Dateien sind gelöscht!:','warn'); }else{ log('Im Verzeichnis: '+notActiveDPs[i].dir +' können folgende Dateien gelöscht werden:','warn'); } olddir = notActiveDPs[i].dir; } log(notActiveDPs[i].id); } },2000 * (toDir-fromDir)); // file.length } }); }else{ console.warn('Es wurde keine History-Instanz ausgewählt!') } }@matt77che Versuch mal v1.0.7.
Anscheinend hast du kein Speicherverzeichnis definiert und somit wurde bislang kein Standardverzeichnis gesetzt.
Ich hatte bei mir immer ein Speicherverzeichnis gesetzt.
Wurde mit v1.0.7 behoben.
Hey! Du scheinst an dieser Unterhaltung interessiert zu sein, hast aber noch kein Konto.
Hast du es satt, bei jedem Besuch durch die gleichen Beiträge zu scrollen? Wenn du dich für ein Konto anmeldest, kommst du immer genau dorthin zurück, wo du zuvor warst, und kannst dich über neue Antworten benachrichtigen lassen (entweder per E-Mail oder Push-Benachrichtigung). Du kannst auch Lesezeichen speichern und Beiträge positiv bewerten, um anderen Community-Mitgliedern deine Wertschätzung zu zeigen.
Mit deinem Input könnte dieser Beitrag noch besser werden 💗
Registrieren Anmelden

