1

这是代码,但我仍然不知道该怎么做,只能发送一次消息:

if(temperature>25){
      //for sending the message to the email
      var mailOptions = {
        from: 'sender@gmail.com',
        to: 'receiver@tssb.com.my',
        subject: 'Sending Email using Node.js',
        text: 'The device with topic ' + topic + ' was higher than the temperature of 25 which is ' + temperature 
      };

      transporter.sendMail(mailOptions, function(error, info){
        if(error){
          console.log(error);
        } else {
          console.log('Email sent: ' + info.response);
        }
      });          

    }else if(temperature<19){
      var mailOptions = {
        from: 'sender@gmail.com',
        to: 'receiver@tssb.com.my',
        subject: 'Sending Email using Node.js',
        text: 'The device with topic ' + topic + ' was smaller than the temperature of 19 which is ' + temperature 
      };

      transporter.sendMail(mailOptions, function(error, info){
        if(error){
          console.log(error);
        } else {
          console.log('Email sent: ' + info.response);
        }
      });    

      const from = 'Nexmo';
      const to = 'phone number';
      const text = 'The device with topic ' + topic + ' was smaller than the temperature of 19 which is ' + temperature;

      nexmo.message.sendSms(from, to, text, (error, response) => {
        if(error) {
          throw error;
        } else if(response.messages[0].status != '0') {
          console.error(response);
          throw 'Nexmo returned back a non-zero status';
        } else {
          console.log(response);
        }
      });

    }

情况是,当mqtt发布温度超过25的主题时,它会向电子邮件或电话号码发送通知消息,但只发生一次,这意味着如果温度超过25,则意味着下次发布到mqtt代理它不会再次发送消息,直到温度低于 19,然后它会再次发送通知电子邮件消息,同样只发生一次。请帮忙,谢谢。

4

0 回答 0