簡體   English   中英

Electric IMP + PubNub:代理撥打額外電話

[英]Electric IMP + PubNub: Agent making extra calls

我正在從事一個IoT項目,該項目使用Electric Imp和PubNub進行通信,並使用Parse進行數據記錄,雲代碼等。

由於某種原因,當我只希望事件廣播一次時,我在IMP IDE中的代碼會多次調用PubNub通道。

這是代碼:

#require "PubNub.class.nut:1.1.0"
#require "Parse.class.nut:1.0.1"
// Log the URLs we need
server.log("");
server.log("------------------- RESTART CYCLE ------------------")
server.log("Agent Loaded. Agent URL: " + http.agenturl());

local impID = imp.configparams.deviceid;
local reqCount = 0;
local myResponse = [];


// Connect to PubNub
const publishKey = "#####################";
const subscribeKey = "#####################";
pubNub <- PubNub(publishKey, subscribeKey);


// Subscribe to PubNub Channel
pubNub.subscribe([impID, "iPlugComm"], function(err, result, timetoken) {
    if (err != null) {
        server.log(err);
        return;
    }
    parseComm(result,timetoken);
});

// Connect to Parse.com
const APP_ID = "#####################";
const REST_API_KEY = "#####################";
parse <- Parse(APP_ID, REST_API_KEY);

server.log("imp id = "+impID);
server.log("----------------------------------------------------")
server.log("");

// Process URI Requests
function requestHandler(request, response) {
  try {
    // check if the user myCommand led as a query parameter
    if ("myCommand" in request.query) {
        local myVar = request.query.myCommand;
        response.header("Access-Control-Allow-Origin", "*");
        myResponse.append(response);
        device.send("myCommand", myVar);
    }else{
        response.send(500, "Invalid Command Sent");
    }
  } 
  catch (ex) {
    response.send(500, "Invalid Command Sent: " + ex);
  }
}

// Parse Incoming PubNub Message
function parseComm(result, timetoken){
  local time = timetoken;
  local idx = 1;
  local deviceID,
        deviceName, 
        deviceMode, 
        deviceMessage, 
        deviceAction, 
        deviceChannel;
  foreach (channel, data in result) {
      // logstr += ("Channel: " + channel + ": " + data["deviceName"]);
      deviceID =  data["deviceID"];
      deviceName = data["deviceName"];
      deviceMode = data["deviceMode"];
      deviceMessage = data["deviceMessage"];
      deviceAction = data["deviceAction"];
      deviceChannel = channel;
      if (idx++ < result.len()) {
          logstr += ", ";
      }
  }
  server.log("COMM on channel '"+deviceChannel+"' from "+ deviceName +": "+deviceMessage+".");
}

// Log Device Connection to Parse.com
local logINIT = parse.createObject("EventLog", {
  "deviceID" : impID,
  "deviceType" : "Electric Imp",
  "deviceMode" : "Slave",
  "deviceState" : "Online",
  "deviceName": "Agent-"+impID,
  "deviceMessage": "Imp restarted.",
  "deviceURL": http.agenturl()
})
logINIT.save(function(err, data) {
  if (err != null) server.log ("Could not update object: " + err.error)
});

device.onconnect(function() {
  server.log("Device CONNECTED to agent.");
  // Publish Connection
  pubNub.publish("iPlugComm", { 
      deviceID = impID,
      deviceName = "Agent-"+impID,
      deviceMode = "Slave",
      deviceMessage = "Imp Agent Restarted",
      deviceAction = "null",
  });
});

device.ondisconnect(function() {
    server.log("Device DISCONNECTED from agent");
    server.log("Device CONNECTED to agent.");
    // Publish Connection
    pubNub.publish("iPlugComm", { 
        deviceID = impID,
        deviceName = "Agent-"+impID,
        deviceMode = "Slave",
        deviceMessage = "Imp Device Disconnected",
        deviceAction = "null",
    });
});

// register the HTTP handler
http.onrequest(requestHandler);

我想要的只是廣播每個連接/事件一次...。但是正如您在我的server.log上看到的那樣,似乎每個新的“ Build and Run”命令都廣播了兩次。

------------------- RESTART CYCLE ------------------
2015-10-29 10:31:21 UTC-3   [Agent] Agent Loaded. Agent URL: https://agent.electricimp.com/#####
2015-10-29 10:31:21 UTC-3   [Agent] imp id = #########
2015-10-29 10:31:21 UTC-3   [Agent] ----------------------------------------------------
2015-10-29 10:31:21 UTC-3   [Agent] 
2015-10-29 10:31:21 UTC-3   [Agent] Device CONNECTED to agent.
2015-10-29 10:31:21 UTC-3   [Agent] COMM on channel '(null : (nil))' from (null : (nil)): (null : (nil)).
2015-10-29 10:31:22 UTC-3   [Agent] Device CONNECTED to agent.
2015-10-29 10:31:22 UTC-3   [Agent] Published data at 14461254820532282
2015-10-29 10:31:22 UTC-3   [Agent] COMM on channel 'iPlugComm' from Agent-237f476938a609ee: Imp Agent Restarted.
2015-10-29 10:31:22 UTC-3   [Agent] Published data at 14461254821522215
2015-10-29 10:31:22 UTC-3   [Agent] COMM on channel 'iPlugComm' from Agent-237f476938a609ee: Imp Agent Restarted.
2015-10-29 10:31:22 UTC-3   [Status]    Device connected
2015-10-29 10:31:22 UTC-3   [Device]    IMP Device Started

有人看到我的錯誤在哪里嗎? 這可能是一個愚蠢的錯誤,因為我不熟練使用Squirrel。

忽略第一個COMM消息(這很奇怪),問題似乎只是device.onconnect事件觸發了兩次。 這是我們的已知問題(僅在“生成並運行”之后發生。)

解決該問題的一種方法可能是設置一個簡短的“反跳”計時器:

local debounce = false;
device.onconnect(function() {
    if (debounce) return;
    // Do stuff
    debounce = true;
    imp.wakeup(1, function() {
        debounce = false;
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM