【问题标题】:Creating a list of active connections NodeJS创建活动连接列表 NodeJS
【发布时间】:2020-01-21 21:31:01
【问题描述】:

我正在构建一个可以连接到 ELK 警报系统并在反应应用程序中输出布防状态的应用程序。我可以连接到警报面板,并且可以使用我的 Alarmpanel 类控制台记录布防状态:

const elkClient = require('elk-client');

class Alarmpanel {
    constructor(siteName, zoneName, host, port, secure, area) {
        this.siteName = siteName;
        this.zoneName = zoneName;
        this.host = host;
        this.port = port;
        this.secure = secure;
        this.area = area;
        this.armed;
    }

    set armStatus(state) {
        this.armed = state;
        return state;
    }


     connect() {
        const ElkClient = elkClient.ElkClient;

        let client = new ElkClient({
            connection: {
            name: this.zoneName,
            host: this.host,
            port: this.port ,
            secure: this.secure,
            }
        });

        client
            .connect()
            .then(() => client.getArmingStatus())
            .then((data) => {
                const armingdata = data.areas[this.area].armUpState;
                const armBool = this.armingState(armingdata);
                const panelInfo = {
                    name: this.siteName,
                    armed: armBool
                }
                console.log(panelInfo);
                return panelInfo;  
            })

            .catch((err) => console.log("----Connection Error --------"));

    }

            armingState(data) {
            if(data === 49) {
                this.armStatus = false;
                return this.armed;
            } else if (data === 50) {
                this.armStatus = false;
                return this.armed;
            } else if (data === 51) {
                this.armStatus = true;
                return this.armed;
            } else if (data === 52) {
                this.armStatus = true;
                return this.armed;
            } else if (data === 53) {
                this.armStatus = true;
                return this.armed;
            } else if (data === 54) {
                this.armStatus = true;
                return this.armed;
            } else if (data === 48) {
                this.armStatus = false;
                return this.armed;
            }
        }
    }

const site1 = new Alarmpanel("Site1", "Main House", IP , PORT, true, 0);
const site2 = new Alarmpanel("Site2", "Exterior", IP, PORT, true, 1);
const site3 = new Alarmpanel("Site3", "Exterior", IP, PORT, true, 2);
const site4 = new Alarmpanel("Site4", "Guest House", IP , PORT, true, 3);
const site5 = new Alarmpanel("Site5", "Main House", IP, PORT, true, 0);
const site6 = new Alarmpanel("Site6", "Exterior", IP, PORT, true, 1);
const site7 = new Alarmpanel("Site7", "Main House", IP, PORT, true, 0);

site1.connect();
site2.connect();
site3.connect();
site4.connect();
site5.connect();
site6.connect();
site7.connect();

当我在控制台登录时,我会为 7 个站点中的每一个都获得正确的“待命状态”:

但是,我正在尝试访问每个站点的返回值并将输出放入动态数组中。连接数组应该保存在我的 express 服务器上,我的 react 应用程序应该从那里获取每个站点的状态。

如何将 Alarmpanel.connect() 的返回值放入我可以访问的数组中。当我声明一个全局数组时,我只能访问它并从 .then() 回调中获取值。如果我从其他任何地方访问它,那么我只会得到 []。

【问题讨论】:

  • 你没有在 connect() 中返回你的 Promise,你应该这样做! return client.[...]。然后,您可以在异步函数中连续获取每个警报系统的状态:await site1.connect(); await site2.connect(); 等。然后您可以将变量存储在 then 中(如果您想要但不理想,可以在全局范围内),或者只是将它们返回到然后将它们分配到外面。

标签: javascript node.js express asynchronous promise


【解决方案1】:

正如我在第一条评论中所述,您的连接函数没有返回任何承诺。这意味着您不能等待连接函数之外的数据。即使您在then 中全局设置数据,也只能从此时开始访问它,并且我假设您的全局变量console.log() 已经打印,因为您没有等待建立连接。

这是对您的代码的一个非常粗略的修复:

const elkClient = require('elk-client');

class Alarmpanel {
    constructor(siteName, zoneName, host, port, secure, area) {
        this.siteName = siteName;
        this.zoneName = zoneName;
        this.host = host;
        this.port = port;
        this.secure = secure;
        this.area = area;
        this.armed;
    }

    set armStatus(state) {
        this.armed = state;
        return state;
    }


     connect() {
        const ElkClient = elkClient.ElkClient;

        let client = new ElkClient({
            connection: {
            name: this.zoneName,
            host: this.host,
            port: this.port ,
            secure: this.secure,
            }
        });

        return client
            .connect()
            .then(() => client.getArmingStatus())
            .then((data) => {
                const armingdata = data.areas[this.area].armUpState;
                const armBool = this.armingState(armingdata);
                const panelInfo = {
                    name: this.siteName,
                    armed: armBool
                }
                return panelInfo;  
            })
            .catch((err) => console.log("----Connection Error --------"));

    }

    armingState(data) {
        if(data === 49) {
            this.armStatus = false;
            return this.armed;
        } else if (data === 50) {
            this.armStatus = false;
            return this.armed;
        } else if (data === 51) {
            this.armStatus = true;
            return this.armed;
        } else if (data === 52) {
            this.armStatus = true;
            return this.armed;
        } else if (data === 53) {
            this.armStatus = true;
            return this.armed;
        } else if (data === 54) {
            this.armStatus = true;
            return this.armed;
        } else if (data === 48) {
            this.armStatus = false;
            return this.armed;
        }
    }
}

const site1 = new Alarmpanel("Site1", "Main House", IP , PORT, true, 0);
const site2 = new Alarmpanel("Site2", "Exterior", IP, PORT, true, 1);
const site3 = new Alarmpanel("Site3", "Exterior", IP, PORT, true, 2);
const site4 = new Alarmpanel("Site4", "Guest House", IP , PORT, true, 3);
const site5 = new Alarmpanel("Site5", "Main House", IP, PORT, true, 0);
const site6 = new Alarmpanel("Site6", "Exterior", IP, PORT, true, 1);
const site7 = new Alarmpanel("Site7", "Main House", IP, PORT, true, 0);

async function connect() {
    const statuses = [
        await site1.connect(),
        await site2.connect(),
        await site3.connect(),
        await site4.connect(),
        await site5.connect(),
        await site6.connect(),
        await site7.connect(),
    ];
    console.log(statuses);
}

connect();

我不是故意使用任何花哨的承诺的东西,但你可以看看 Promise.all 来改进这段代码!

【讨论】:

  • 太棒了。非常感谢!我试图投票,但因为我还是新人,所以不允许投票。到了15分我就补上!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-22
  • 2017-09-17
  • 2018-06-04
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
相关资源
最近更新 更多