Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. htz

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    H
    • Profile
    • Following 0
    • Followers 0
    • Topics 1
    • Posts 2
    • Best 0
    • Groups 0

    htz

    @htz

    0
    Reputation
    4
    Profile views
    2
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    htz Follow

    Latest posts made by htz

    • RE: States und Threadsicherheit

      Erstmal besten Dank für deine Rückmeldung.

      @asgothian said in States und Threadsicherheit:

      die Warteschlange bei jeder Anpassung durch persistState() sichern.

      Das habe ich im Prinzip bereits versucht. In queueMessage wird nach jeder Änderung persistiert, in sendNext ebenfalls. Imho Tritt das Problem auf, wenn während eines Vorgangs auf dem State eine weitere State-Operation ausgeführt wird, etwa so:

      modulA: queueMessage("hallo")
      modulA->notificationService: loadState => state entspricht []
      modulB: queueMessage("hallo2")
      modulB->notificationService: loadState => alter state wird geladen, entspricht []
      modulA->notificationService: persistState => state entspricht ["hallo"]
      modulB->notificationService: persistState => state entspricht ["hallo2"]
      modulC->sendNext() => "hallo2" // vereinfacht
      

      Ich bräuchte gewissermaßen ein Statelock.

      modulA: queueMessage("hallo")
      modulA->notificationService: lockState
      modulA->notificationService: loadState => state entspricht []
      modulB: queueMessage("hallo2")
      modulB->notificationService: lockState => muss warten
      modulA->notificationService: persistState => state entspricht ["hallo"]
      modulA->notificationService: unlockState
      modulB->notificationService: lockState => lock wurde aufgehoben, jetzt geht es hier weiter
      modulB->notificationService: loadState => state wird geladen, entspricht ["hallo"]
      modulB->notificationService: persistState => state entspricht ["hallo", "hallo2"]
      modulB->notificationService: unlockState
      modulC->sendNext() => "hallo" // vereinfacht
      

      @asgothian said in States und Threadsicherheit:

      die Warteschlange als Skript globale Variable im Speicher halten, und nur beim Start des Skriptes aus dem Datenpunkt lesen

      Bitte korrigiere mich, falls ich falsch liege, aber Globale Variablen gibt es doch im JavaScript-Adapter eigentlich nicht? Soweit ich den Adapter verstanden habe, wird der Code der Globale Scripte einfach oberhalb des Codes eines Modules (also pro *.ts, *.js Datei) eingefügt. Demzufolge hätte ich dann ja kein einzelne globale Variable, sondern mehrere. Die würden dann alle unterschiedliche Daten enthalten, oder gibt es hier noch ein Feature das ich nicht kenne?

      posted in JavaScript
      H
      htz
    • States und Threadsicherheit

      Hallo liebe ioBroker Community,
      ich nutze ioBroker und den JS-Controller schon seit einer ganzen Weile.

      Im Moment arbeite ich an einem globalen Service, der Nachrichten aus verschiedenen Modulen sammeln kann und diese bei Bedarf in eingehender Reihenfolge ausgibt.

      class NotificationService {
          private readonly stateDataId = 'javascript.0.NotificationServiceData';
      
          constructor() {
              if(!existsState(this.stateDataId)) {
                  createState(this.stateDataId, JSON.stringify([]));
              }
          }
      
          public queueMessage = async(message: string) => {
              let queue = await this.loadState();
              log(`Got message to queue: "${message}"`);
              if(!this.hasMessage(message, queue)) {
                 queue.push(message);
              } else {
                  log(`NOT QUEUED: "${message}"`);
              }
              await this.persistState(queue);
          }
      
          public removeMessage = async(message: string) => {
              let queue = await this.loadState();
              let exists = this.hasMessage(message, queue);
              if(exists) {
                  queue.splice(exists.idx, 1);
              }
              await this.persistState(queue);
          }
      
          public isEmpty = async() => {
              let queue = await this.loadState();
              return this.isEmptyQueue(queue);
          }
      
          private isEmptyQueue = (queue: string[]) => {
              return queue.length === 0;
          }
      
          public sendNext = async() => {
              let queue = await this.loadState();
              if(this.isEmptyQueue(queue)) {
                  return;
              }
              log(`Queue length is ${queue.length}`);
              let message = queue.splice(0, 1)[0];
              log(`Message is: ${message}`);
              this.sendMessage(message);
              await this.persistState(queue);
          }
      
          private sendMessage = (message: string) => {
              // TODO
          }
      
          private hasMessage = (message: string, queue: string[]) => {
              log(`Queue is ${JSON.stringify(queue)}`);
              const idx = queue.indexOf(message);
              if(idx !== -1) {
                  return {
                      idx: idx
                  }
              }
              return false;
          }
      
          private persistState = async(queue: string[]) => {
              await setStateAsync(this.stateDataId, JSON.stringify(queue));
          }
      
          private loadState = async() => {
              let state = await getStateAsync(this.stateDataId);
              if(state.val) {
                  log("State data is not null; parsing");
                  return JSON.parse(state.val);
              }
              log(`State data is null, state is: ${JSON.stringify(state)}`);
              return [];
          }
      }
      

      Wenn ich den Service nun in unterschiedlichen Modulen benutze, dann kommt es vor, dass einzelne Nachrichten nicht in die Queue gelangen. Das scheint daran zu liegen, dass der State nicht Threadsicher ist.

      Gibt es hier eine Möglichkeit, um das Problem zu umgehen? Oder Nutze ich die States vielleicht einfach falsch? Welche Alternativen gibt es?

      Vielen Dank im Voraus!

      posted in JavaScript
      H
      htz
    Community
    Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
    The ioBroker Community 2014-2023
    logo