📄 measuremote.java
字号:
package net.tinyos.cr;import net.tinyos.util.*;import java.io.*;import java.util.*;import net.tinyos.message.*;import net.tinyos.packet.*;import net.tinyos.util.*;import net.tinyos.cr.messages.*;public class MeasureMote implements MessageListener, S4Constants { private Receiver receiver; private Date date; private long time; private int id; private int port; private String host; private MoteIF mote; private CommandMessage command; private short sequenceNo = 0; private int updateCount = 0; private short coords[]; private boolean updating = true; //public boolean debug = false; public boolean debug = true; public boolean exitAfterReceive = false; public int cmd_id = 0; /* This creates a new mote listener. It assumes that a mote with * programmed id <i> is connected to a serial forwarder at port * basePort + i, at host host. */ public MeasureMote(int id, String host, int basePort, int cmd_id) { this.id = id; this.port = basePort+id; this.host = host; this.cmd_id = cmd_id; if (debug) System.err.println("Starting connection to mote "+id+" at "+host+":"+port); PhoenixSource source = BuildSource.makePhoenix("sf@"+host+":"+port, PrintStreamMessenger.err); mote = new MoteIF(source); CommandResponseMessage crm = new CommandResponseMessage(); mote.registerListener(crm, this); command = new CommandMessage(); } public void messageReceived(int dest_addr, Message m) { synchronized(this) { if (updating && m instanceof CommandResponseMessage) { CommandResponseMessage crm = (CommandResponseMessage)m; short type = crm.get_type(); if (type == CMD_GET_COUNT){ int src_addr = crm.get_cmd_response_args_count_info_src_addr(); int [] count_arr = crm.get_cmd_response_args_count_info_count(); //System.out.println("source id:"+src_addr+" count: "+count); for (int i=0;i<COUNT_NUM;i++){ System.out.print(count_arr[i]+" "); } System.out.println(); } //added by Feng Wang else if (type == CMD_GET_RSSI){ int src_addr = crm.get_cmd_response_args_rssi_info_src_addr(); int [] rssi_arr = crm.get_cmd_response_args_rssi_info_rssi(); int voltage = crm.get_cmd_response_args_rssi_info_voltage(); //System.out.println("source id:"+src_addr); /* for (int i=0;i<RSSI_NUM;i++){ if (rssi_arr[i] != 0xff) System.out.print(rssi_arr[i]+" "); else System.out.print("NaN "); } System.out.println(); */ for (int i=0;i<RSSI_NUM;i++){ if (rssi_arr[i] != 0xff) { //double Vbat = 3.0; double Vbat = voltage/1000; double ADC_Count = (double)rssi_arr[i]; double rssi_dbm = -50.0*Vbat*ADC_Count/1024.0-45.5; //System.out.format("%.1f ", rssi_dbm); System.out.print(rssi_arr[i]+" "); } else System.out.print("NaN "); } System.out.println(); //System.out.println(voltage); } else if (type == CMD_CLEAR){ //System.out.println("received clear command"); } else if (type==CMD_LED_ON){ System.out.println("led on"); } else if (type==CMD_LED_OFF){ System.out.println("led off"); } else if (type==CMD_IMAGE_VERSION){ long id = crm.get_cmd_response_args_image_id(); System.out.println(Long.toHexString(id)); } if (exitAfterReceive) System.exit(0); } if (updating && m instanceof S4LogMessage) { S4LogMessage lm = (S4LogMessage)m; updateCount++; short type = lm.get_log_msg_type(); if (type == LOG_CHANGE_COORDS) { updateCoordinates(lm); } if (type == LOG_RECEIVE_ROOT_BEACON) { short beacon = lm.get_log_msg_receive_root_beacon_id(); short seqno = lm.get_log_msg_receive_root_beacon_seqno(); short hopcount = lm.get_log_msg_receive_root_beacon_hopcount(); int lasthop = lm.get_log_msg_receive_root_beacon_last_hop(); System.out.println("m "+id+" received a beacon from "+beacon+" [seqno "+seqno+", hopcount "+hopcount+", lasthop "+lasthop+"]"); } if (type == LOG_ROUTE_START) { int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); System.out.println("m "+id+" started routing [src "+src+", dest "+dst+"]"); } if (type == LOG_ROUTE_SENT_NORMAL_OK) { int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); System.out.println("m "+id+" sent a packet [src "+src+", dest "+dst+"]"); } if (type == LOG_ROUTE_FAIL_STUCK) { int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); System.out.println("m "+id+" routing failure [src "+src+", dest "+dst+"]"); } if (type == LOG_ROUTE_RECEIVED_OK) { int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); System.out.println("m "+id+" received a packet [src "+src+", dest "+dst+"]"); } if (type == LOG_ROUTE_SUCCESS) { int src = lm.get_log_msg_route_report_origin_addr(); int dst = lm.get_log_msg_route_report_dest_addr(); System.out.println("m "+id+" delivered a packet [src "+src+", dest "+dst+"]"); } } } } public void stopUpdating() { updating = false; } public void startUpdating() { updating = true; } public boolean isUpdating() { return updating; } public int getId() { return id; } public int getUpdateCount() { return updateCount; } public short[] getCoords() { return coords; } public short countValid() { short count = 0; if (coords != null) { for (int i = 0; i < coords.length; i++) { if (coords[i] != 255) { count++; } } } return count; } public void sendLedCommand(boolean on_or_off) { System.out.print("Sending led command from " + id +on_or_off); short cmd = CMD_LED_OFF; if (on_or_off) cmd = CMD_LED_ON; command.set_type(cmd); sendCommand(); } public void sendCountCommand(int src) { if (debug) System.out.println("Sending count command from " + id + " count src addr=" + src); short cmd = CMD_GET_COUNT; command.set_type(cmd); command.set_cmd_args_src_addr(src); sendCommand(); } //added by Feng Wang public void sendRSSICommand(int src) { if (debug) System.out.println("Sending rssi command from " + id + " rssi src addr=" + src); short cmd = CMD_GET_RSSI; command.set_type(cmd); command.set_cmd_args_src_addr(src); sendCommand(); } public void sendVersionCommand() { if (debug) System.out.println("Sending version command from " + id ); short cmd = CMD_IMAGE_VERSION; command.set_type(cmd); sendCommand(); } public void sendMeasureCommand(int n_packets, int repeat, short power, short seq) { if (debug) System.out.println("Sending measure command from " + id + " npackets=" + n_packets + "interval"+repeat+" power:"+power); short cmd = CMD_START_MEASURE; command.set_type(cmd); command.set_cmd_args_start_measure_n_packets(n_packets); command.set_cmd_args_start_measure_repeat(repeat); command.set_cmd_args_start_measure_power(power); command.set_cmd_args_start_measure_measure_seq(seq); sendCommand(); } public void sendClearCommand() { if (debug) System.out.println("sending clear command"); short cmd = CMD_CLEAR; command.set_type(cmd); sendCommand(); } private void sendCommand() { command.set_gateway_addr(id); command.set_cmd_addr(cmd_id); if (debug) { System.out.print("Sending payload: "); for (int i = 0; i < command.dataLength(); i++) { System.out.print(Integer.toHexString(command.dataGet()[i] & 0xff)+ " "); } System.out.println(); } try { mote.send(id, command); } catch (IOException e) {e.printStackTrace(System.err);} } private void updateCoordinates(S4LogMessage m) { this.coords = m.get_log_msg_update_coordinates_Coords_comps(); System.out.print("Update coordinates: "); this.printCoordinates(); } public void printCoordinates() { System.out.print("m "+id+": ["); if (coords != null) { System.out.print(coords[0]); for (int i = 1; i < coords.length; i++) { System.out.print("," + coords[i]); } } System.out.println("]"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -