如何在脚本中使用其地址获取节点的位置?

时间:2019-06-13 04:28:18

标签: groovy location unetstack

我编写了一个传输cmd,该cmd将地址和数据作为输入并将数据发送到该节点。我试图使用它的地址来获取节点的位置,但是它显示空值,但是数据已成功传输。
为什么会发生这种情况,解决方案是什么?

示例:

transmit 2,[1,2,3]   //transmit data [1,2,3] to node-2

transmit = { addr, data ->
    println "TRANSMIT $addr, $data"
    println phy << new DatagramReq(to: addr, protocol: Protocol.MAC, data: data)
    NodeInfo n = new NodeInfo(addr)
    println 'location = '+n.getLocation()
    println 'Address :'+addr+'\nData :'+data

    def txNtf = receive(TxFrameNtf, 1000)

    println txNtf  
}

Model.groovy:

class Model extends UnetAgent {

int neighbor, addr;
float neighbor_distance;
def ranging

def dist;
def data


public void startup() {
AgentID phy = agentForService(Services.PHYSICAL);
subscribe(topic(phy));

ranging = agentForService Services.RANGING;
subscribe topic(ranging);

def nodeInfo = agentForService Services.NODE_INFO;
addr = nodeInfo.address;

}

void processMessage(Message msg) {
if (msg instanceof DatagramNtf && msg.protocol == Protocol.MAC)
{     
    neighbor = msg.from;
    println " BEACON RECEIVED FROM:" +neighbor
    data = msg.getData()

    def bits=32

    System.out.println "number of bits sent :"+bits*data.size()

    ranging << new RangeReq(to: neighbor);
  }
  else if (msg instanceof RangeNtf )
  {   
    float neighbor_distance = msg.getRange();
    println( "\n Distance between node "+addr + " and neighbor " +neighbor+ " is " + neighbor_distance+"m.\n")


    }
  }

  void setup() {


       }

   }

1 个答案:

答案 0 :(得分:0)

您的代码:

NodeInfo n = new NodeInfo(addr)
println 'location = '+n.getLocation()

创建一个节点信息对象,并尝试从中获取一个location。默认情况下,这样的对象没有任何location,因此您得到一个null值。您创建的对象不引用在节点上运行的节点信息代理。

您可能想要的是从节点信息代理获取信息,您需要查找该信息而不是尝试在本地创建。示例代码段:

def n = agentForService org.arl.unet.Services.NODE_INFO
println 'location = '+n.location