0

I have been working on a problem for the past few days. I have searched the forum for answers using my limited knowledge but I was not able to solve my problem.

I'm working on an application that can connect to an ELK alarm system. I'm able to connect to the alarm panel and console.log the status of the panel using this code:

function establishConnection (elkCl) {
elkCl
  .connect()
  .then(() => client.getArmingStatus())
  .then((events) => {
    const area1 = events.getAreaStatus(1);
    const state = area1.armUpState;
    switch (state) {
      case 49:
        console.log("The system is disarmed");
        break;
      case 51:
        console.log("Exit Delay");
        break;
      case 52:
        console.log("ARMED - NO BYPASS");
        break;
      case 54:
        console.log("ARMED - ZONES BYPASSED");
        break;
      case 53:
        console.log("ARMED - FORCED");
        break;
      default:
    }
  })
  .then(() => client.disconnect())
  .then(() => {
    console.log('Done!');
  })
  .catch((err) => {
    console.error(err);
  });
}

Instead of console logging the status of the alarm panel, I'm trying set a boolean value to "true/false" depending on the status of the system. I want to use this value in a React application and graphically show alarm systems that are armed/disarmed (red/green).

I have tried:

armingStatus;

switch (state) {
      case 49:
        armingStatus = false;

but it doesn't work. I have also tried to return the strings instead of console logging them but that doesn't work either. What am I doing wrong? I apologize if this question has already been answered somewhere in the forums. I was not able to find the answer for the past two days.

Thank you all for your help!

Mike B.
  • 123
  • 8
  • https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Manos Kounelakis Jan 08 '20 at 08:24
  • 3
    What do you mean "it doesn't work"? There's two things to keep in mind: "where" and "when" (scope and time). If you declare `armingStatus` inside `establishConnection`, then the changes won't be visible from outside that function's scope. And if you do `elkCl.connect().then(/*...*/).then(() => armingStatus = false); console.log(armingStatus);`, then the `console.log` will print the original value, because it'll be executed _before_ the assignment. – mbojko Jan 08 '20 at 08:25
  • please provide more code how you tried with boolean – Dmitry Reutov Jan 08 '20 at 08:28
  • Thank you both! I tried to change the boolean value to "true" if the system is armed. I declared armingStatus globally. I armed the alarm system and console logged to make sure I'm able to communicate with the alarm panel. The console.log was correct and it showed the system was armed. Then I switched console log with case 52: armingStatus = true; I then called establishConnection() and immediately after calling the function I console logged armingStatus - which did not show "true" as I hoped. – Mike B. Jan 08 '20 at 08:53

0 Answers0