public class SendMsgToWsn implements MessageListener, ConstantsIfc{
Date daqTimestamp;
MoteIF mote;
FilesManager fManager = new FilesManager();
BufferedWriter wadOutput;
AckReplyMsg ackReplyMsg;
NodeCtrlMsg nodeCtrlMsg;
NodeQueryMsg nodeQueryMsg;
ReceiveMsgFromWsn rmfw;
long ackSeqNum;
long ctrlSeqNum;
long querySeqNum;
String dataString;
static icmsv3MainFrame mf = new icmsv3MainFrame();
boolean doneSending = false;
static short ackSrcMoteId, ackMoteGroup, ackHopCounts, ackType, ackMsgType;
static short nodeCSrcMoteId, nodeCMoteGroup, nodeCHopCounts, nodeCType, nodeCMsgType;
static short nodeQSrcMoteId, nodeQMoteGroup, nodeQHopCounts, nodeQType, nodeQMsgType;
static long ackSeqNo, nodeCSeqNo, nodeQSeqNo;
static int ackSourceAddr, ackOriginAddr, nodeCSourceAddr, nodeCOriginAddr, nodeQSourceAddr, nodeQOriginAddr;
public SendMsgToWsn(){
ackReplyMsg = new AckReplyMsg();
nodeCtrlMsg = new NodeCtrlMsg();
nodeQueryMsg = new NodeQueryMsg();
mote = new MoteIF(PrintStreamMessenger.err, -1);
try{
fManager.createWritableFile(ACKREPLY_FILENAME_SEND);
fManager.createWritableFile(NODECTRL_FILENAME_SEND);
fManager.createWritableFile(NODEQUERY_FILENAME_SEND);
fManager.createWritableFile(ACKREPLY_FILENAME_RECEIVE);
fManager.createWritableFile(NODECTRL_FILENAME_RECEIVE);
fManager.createWritableFile(NODEQUERY_FILENAME_RECEIVE);
}
catch(IOException ioe){
System.err.println("Unable to create files on startup. Error: " + ioe);
}
}
public void messageReceived(int dest_addr,Message msg){
if(msg.amType() == AM_DIAPEREVENTMSG){ //read switch readings
DiaperEventReceived(dest_addr, (DiaperEventMsg)msg);
}
else if(msg.amType() == AM_ATTENTIONEVENTMSG){
AttentionEventReceived(dest_addr, (AttentionEventMsg)msg);
}
else if(msg.amType() == AM_CAREREVENTMSG){
CarerEventReceived(dest_addr, (CarerEventMsg)msg);
}
}
...
public void sendMsg(){
String cmd = mf.getCommand();
int node = mf.getNode();
short msg = mf.getMsgType();
short ack = mf.getAck();
short query = mf.getQuery();
short ctrl = mf.getCtrl();
dataString = new String();
dataString = "(Sending) ";
if(cmd.equalsIgnoreCase(ACKREPLY_CMD)){
ackReplyMsg.set_srcMoteId(TOS_GATEWAY_ADDR);
ackSrcMoteId = ackReplyMsg.get_srcMoteId();
ackReplyMsg.set_moteGroup(CMWAD_MOTEGROUP);
ackMoteGroup = ackReplyMsg.get_moteGroup();
ackReplyMsg.set_sourceAddr(TOS_GATEWAY_ADDR);
ackSourceAddr = ackReplyMsg.get_sourceAddr();
ackReplyMsg.set_originAddr(node);//receive input from user (Node)
ackOriginAddr = ackReplyMsg.get_originAddr();
ackReplyMsg.set_hopCounts((short)0);
ackHopCounts = ackReplyMsg.get_hopCounts();
ackReplyMsg.set_sequenceNo(ackSeqNum++);
ackSeqNo = ackReplyMsg.get_sequenceNo();
ackReplyMsg.set_msgType(msg); //receive input from user (Command)
ackMsgType = ackReplyMsg.get_msgType();
ackReplyMsg.set_ackType(ack); //receive input from user (Argument)
ackType = ackReplyMsg.get_ackType();
dataString += cmd + "\nsrcMoteId: " + TOS_GATEWAY_ADDR + " , moteGroup: " + CMWAD_MOTEGROUP
+ " , sourceAddr: " + TOS_GATEWAY_ADDR + " , originAddr: " + node + " , hopCounts: " + (short)0
+ " , sequenceNo: " + ackSeqNum + " , msgType: " + msg + " , ackType: " + ack;
try{
System.out.println(dataString);
fManager.writeString2File(ACKREPLY_FILENAME_SEND, dataString, true);
doneSending = true;
rmfw = new ReceiveMsgFromWsn();
rmfw.receivingMsg();
}
catch(IOException ioe){
System.err.println("Unable to write data into file at DiaperEventReceived");
}
try{
mote.send(node, ackReplyMsg);
}
catch (IOException e) {
}
}
else if(cmd.equalsIgnoreCase(NODECTRL_CMD)){
nodeCtrlMsg.set_srcMoteId(TOS_GATEWAY_ADDR);
nodeCSrcMoteId = nodeCtrlMsg.get_srcMoteId();
nodeCtrlMsg.set_moteGroup(CMWAD_MOTEGROUP);
nodeCMoteGroup = nodeCtrlMsg.get_moteGroup();
nodeCtrlMsg.set_sourceAddr(TOS_GATEWAY_ADDR);
nodeCSourceAddr = nodeCtrlMsg.get_sourceAddr();
nodeCtrlMsg.set_originAddr(node);//receive input from user (node)
nodeCOriginAddr = nodeCtrlMsg.get_originAddr();
nodeCtrlMsg.set_hopCounts((short)0);
nodeCHopCounts = nodeCtrlMsg.get_hopCounts();
nodeCtrlMsg.set_sequenceNo(ctrlSeqNum++);
nodeCSeqNo = nodeCtrlMsg.get_sequenceNo();
nodeCtrlMsg.set_msgType(msg); //receive input from user (command)
nodeCMsgType = nodeCtrlMsg.get_msgType();
nodeCtrlMsg.set_ctrlType(ctrl); //receive input from user (argument)
nodeCType = nodeCtrlMsg.get_ctrlType();
dataString += cmd + "\nsrcMoteId: " + TOS_GATEWAY_ADDR + " , moteGroup: " + CMWAD_MOTEGROUP
+ " , sourceAddr: " + TOS_GATEWAY_ADDR + " , originAddr: " + node + " , hopCounts: " + (short)0
+ " , sequenceNo: " + ctrlSeqNum + " , msgType: " + msg + " , ctrlType: " + ctrl;
try{
System.out.println(dataString);
fManager.writeString2File(NODECTRL_FILENAME_SEND, dataString, true);
doneSending = true;
rmfw = new ReceiveMsgFromWsn();
rmfw.receivingMsg();
}
catch(IOException ioe){
System.err.println("Unable to write data into file at AttentionEventReceived");
}
try{
mote.send(node, nodeCtrlMsg);
}
catch (IOException e) {
}
}
else if(cmd.equalsIgnoreCase(NODEQUERY_CMD)){
nodeQueryMsg.set_srcMoteId(TOS_GATEWAY_ADDR);
nodeQSrcMoteId = nodeQueryMsg.get_srcMoteId();
nodeQueryMsg.set_moteGroup(CMWAD_MOTEGROUP);
nodeQMoteGroup = nodeQueryMsg.get_moteGroup();
nodeQueryMsg.set_sourceAddr(TOS_GATEWAY_ADDR);
nodeQSourceAddr = nodeQueryMsg.get_sourceAddr();
nodeQueryMsg.set_originAddr(node);//receive input from user (node)
nodeQOriginAddr = nodeQueryMsg.get_originAddr();
nodeQueryMsg.set_hopCounts((short)0);
nodeQHopCounts = nodeQueryMsg.get_hopCounts();
nodeQueryMsg.set_sequenceNo(querySeqNum++);
nodeQSeqNo = nodeQueryMsg.get_sequenceNo();
nodeQueryMsg.set_msgType(msg); //recive input from user (command)
nodeQMsgType = nodeQueryMsg.get_msgType();
nodeQueryMsg.set_queryType(query); //receive input from user (argument)
nodeQType = nodeQueryMsg.get_queryType();
dataString += cmd + "\nsrcMoteId: " + TOS_GATEWAY_ADDR + " , moteGroup: " + CMWAD_MOTEGROUP
+ " , sourceAddr: " + TOS_GATEWAY_ADDR + " , originAddr: " + node + " , hopCounts: " + (short)0
+ " , sequenceNo: " + querySeqNum + " , msgType: " + msg + " , queryType: " + query;
try{
System.out.println(dataString);
fManager.writeString2File(NODEQUERY_FILENAME_SEND, dataString, true);
doneSending = true;
rmfw = new ReceiveMsgFromWsn();
rmfw.receivingMsg();
}
catch(IOException ioe){
System.err.println("Unable to write data into file at CarerEventReceived");
}
try{
mote.send(node, nodeQueryMsg);
}
catch (IOException e) {}
}
}
public static void main(String[] args){
SendMsgToWsn apps = new SendMsgToWsn();
mf.setVisible(true);
}
public String getCommand(){
return mf.getCommand();
}
//------------------------------------------------------
public short getAckSrcMoteId(){
return ackSrcMoteId;
}
public short getAckMoteGroup(){
return ackMoteGroup;
}
public int getAckSourceAddr(){
return ackSourceAddr;
}
public int getAckOriginAddr(){
return ackOriginAddr;
}
public short getAckHopCounts(){
return ackHopCounts;
}
public long getAckSeqNo(){
return ackSeqNo;
}
public short getAckType(){
return ackType;
}
public short getAckMsgType(){
return ackMsgType;
}
//------------------------------------------------------
public short getNodeCSrcMoteId(){
return nodeCSrcMoteId;
}
public short getNodeCMoteGroup(){
return nodeCMoteGroup;
}
public int getNodeCSourceAddr(){
return nodeCSourceAddr;
}
public int getNodeCOriginAddr(){
return nodeCOriginAddr;
}
public short getNodeCHopCounts(){
return nodeCHopCounts;
}
public long getNodeCSeqNo(){
return nodeCSeqNo;
}
public short getNodeCType(){
return nodeCType;
}
public short getNodeCMsgType(){
return nodeCMsgType;
}
//------------------------------------------------------
public short getNodeQSrcMoteId(){
return nodeQSrcMoteId;
}
public short getNodeQMoteGroup(){
return nodeQMoteGroup;
}
public int getNodeQSourceAddr(){
return nodeQSourceAddr;
}
public int getNodeQOriginAddr(){
return nodeQOriginAddr;
}
public short getNodeQHopCounts(){
return nodeQHopCounts;
}
public long getNodeQSeqNo(){
return nodeQSeqNo;
}
public short getNodeQType(){
return nodeQType;
}
public short getNodeQMsgType(){
return nodeQMsgType;
}
//------------------------------------------------------
public boolean doneSending(){
return doneSending;
}
}