1 package diku
.distlab
.motecontrolclientlib
.client
;
2 import java
.util
.ArrayList
;
4 import diku
.distlab
.motecontrolclientlib
.protocols
.MsgPayload
;
5 import diku
.distlab
.motecontrolclientlib
.protocols
.motecontrol
.*;
7 public class SimpleMoteManager
extends MoteManager
{
9 public SimpleMoteManager() {
13 protected void grantMoteControl(long mote_id
) {
15 mote
= (SimpleMote
)moteList
.get(mote_id
);
18 mote
= new SimpleMote(mote_id
,this);
21 mote
.getControlStatus().grantControl();
22 // automatically detect new mote status
23 status(toArray(mote
.getId()));
26 protected void revokeMoteControl(long mote_id
) {
28 mote
= (SimpleMote
) moteList
.remove(mote_id
);
31 mote
.getControlStatus().revokeControl();
32 mote
.getStatus().setStatus(SimpleMoteStatus
.UNAVAILABLE
);
37 protected void revokeAllMoteControls()
39 Object
[] motes
= moteList
.getCollection().toArray();
41 for (int i
=0;i
<motes
.length
;i
++)
43 revokeMoteControl(((Mote
)motes
[i
]).getId());
47 protected void confirm(long mote_id
, MsgPayload moteMsg
)
49 MoteMsg msg
= new MoteMsg();
52 msg
.setBytes(moteMsg
.getData());
59 mote
= (SimpleMote
)moteList
.get(mote_id
);
62 switch (msg
.getType().getValue())
64 case MoteMsgType
.CONFIRM
:
65 mote
.getStatus().update(msg
.getConfirm());
67 case MoteMsgType
.DATA
:
68 mote
.dataOut(msg
.getData());
71 // TODO:take corrective action here
72 //throw new Exception("Invalid mote message!");
77 public void requestMotes(long[] moteids
)
79 session
.requestControl(moteids
);
82 public void releaseMotes(long[] moteids
) {
84 for (int i
= 0; i
< moteids
.length
; i
++)
86 mote
= moteList
.remove(moteids
[i
]);
87 if (mote
!= null) mote
.getControlStatus().releaseControl();
89 session
.releaseControl(moteids
);
92 public void releaseAllMotes()
94 // TODO: release all mote controls
97 public void cancelProgramming(long[] moteids
) {
98 requestCommand(MsgCommand
.CANCELPROGRAMMING
, moteids
);
101 public void status(long[] moteids
) {
102 requestCommand(MsgCommand
.STATUS
, moteids
);
105 public void start(long[] moteids
) {
106 requestCommand(MsgCommand
.START
, moteids
);
109 public void stop(long[] moteids
) {
110 requestCommand(MsgCommand
.STOP
, moteids
);
113 public void reset(long[] moteids
) {
114 requestCommand(MsgCommand
.RESET
, moteids
);
117 public void program(long[] moteids
, byte[] flashImage
) {
118 MoteMsg msg
= new MoteMsg();
119 msg
.getType().setValue(MoteMsgType
.REQUEST
);
120 msg
.getRequest().getCommand().setValue(MsgCommand
.PROGRAM
);
121 msg
.getRequest().getFlashImage().setData(flashImage
);
122 session
.sendMoteMsg(msg
,filterReady(moteids
,msg
));
125 protected void sendData(long moteid
, byte[] data
) {
126 MoteMsg msg
= new MoteMsg();
127 msg
.getType().setValue(MoteMsgType
.DATA
);
128 msg
.getData().setData(data
);
129 session
.sendMoteMsg(msg
,toArray(moteid
));
132 private long[] filterReady(long[] moteids
,MoteMsg msg
)
136 ArrayList list
= new ArrayList();
137 for (i
=0; i
< moteids
.length
; i
++)
139 mote
= (SimpleMote
)moteList
.get(moteids
[i
]);
140 if (mote
!=null && mote
.getStatus().isReady())
142 list
.add(new Long(moteids
[i
]));
143 mote
.getStatus().update(msg
.getRequest());
146 long[] res
= new long[list
.size()];
147 for (i
=0; i
< list
.size(); i
++)
149 res
[i
] = ((Long
)list
.get(i
)).longValue();
154 private void requestCommand(short command
,long[] moteids
)
156 MoteMsg msg
= new MoteMsg();
157 msg
.getType().setValue(MoteMsgType
.REQUEST
);
158 msg
.getRequest().getCommand().setValue(command
);
159 session
.sendMoteMsg(msg
,filterReady(moteids
,msg
));
162 private long[] toArray(long value
)
164 long[] res
= new long[1];