Go to most recent revision | Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 15 | arune | 1 | /* |
| 2 | * File created by arune |
||
| 3 | * at 2004-jan-02 18:00:08 |
||
| 4 | */ |
||
| 5 | package Macbeth.Modules.modSmallDisplays; |
||
| 6 | |||
| 7 | import javax.swing.*; |
||
| 8 | import java.awt.*; |
||
| 9 | import java.awt.event.*; |
||
| 10 | import java.util.HashMap; |
||
| 11 | import java.util.Vector; |
||
| 12 | import java.util.Iterator; |
||
| 13 | |||
| 14 | import Macbeth.Utilities.*; |
||
| 15 | import Macbeth.System.*; |
||
| 16 | import Macbeth.XML.XMLDataHandler; |
||
| 17 | |||
| 18 | |||
| 19 | /** |
||
| 20 | * |
||
| 21 | * |
||
| 22 | * |
||
| 23 | * |
||
| 24 | * |
||
| 25 | * |
||
| 26 | * @author arune |
||
| 27 | * @version 1.0 |
||
| 28 | */ |
||
| 29 | public class modSmallDisplays extends MbModule { //implements MbModuleGUI { |
||
| 30 | //The sensordata that we receive via XML |
||
| 31 | //private UByte[] receivedBytes; |
||
| 32 | |||
| 33 | //Interval between refreshing display / queue |
||
| 34 | //private int timerInterval = 0; |
||
| 35 | //our refresh-display-timer |
||
| 36 | private Timer refreshDisplayTimer; |
||
| 37 | private boolean parsingARNEXML; |
||
| 38 | //A GUI for our module. |
||
| 39 | private GUI gui; |
||
| 40 | //Is our GUI currently visible or not? |
||
| 41 | private boolean guiVisible = false; |
||
| 42 | //A list of known display objects. |
||
| 43 | private Vector displayObjects = new Vector(); |
||
| 44 | |||
| 45 | private boolean parsingConfXML; |
||
| 46 | private DisplayObject currentDisplay; |
||
| 47 | /*private String target; |
||
| 48 | private String info; |
||
| 49 | private int width; |
||
| 50 | //private int[] rowAddresses; |
||
| 51 | private Vector rowAddresses = new Vector(); |
||
| 52 | private Vector moduleUsers = new Vector();*/ |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Creates a new instance of modRoomControl. |
||
| 56 | */ |
||
| 57 | public modSmallDisplays() { |
||
| 58 | //construct MbModule |
||
| 59 | super(); |
||
| 60 | //receivedBytes = new UByte[6]; |
||
| 61 | //displayObjects = new HashMap(32); |
||
| 62 | parsingARNEXML = false; |
||
| 63 | gui = new GUI(); |
||
| 64 | //we depend on the ARNE-module |
||
| 65 | addDependency("ARNE"); |
||
| 66 | //register our configuration data handler |
||
| 67 | //setConfigDataHandler(new ConfigDataHandler()); |
||
| 68 | //register our packet data handler |
||
| 69 | setPacketDataHandler(new PacketDataHandler()); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Gets the name of the component. |
||
| 74 | * @return The name of the component. |
||
| 75 | */ |
||
| 76 | public String name() { |
||
| 77 | return "SmallDisplays"; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Gets the description of the component. |
||
| 82 | * @return The description of the component. |
||
| 83 | */ |
||
| 84 | public String description() { |
||
| 85 | return "Handles small LCD-displays on ARNE bus"; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * This method sets default values on all _required_ data |
||
| 90 | * fields in the data repository. |
||
| 91 | */ |
||
| 92 | protected void initDataFields() { |
||
| 93 | //this module doesn't require any data fields |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Starts up this module. |
||
| 98 | */ |
||
| 99 | public void startup() throws MbStartupException { |
||
| 100 | //start up MbModule |
||
| 101 | super.startup(); |
||
| 102 | TimerListener lyssnare = new TimerListener(); |
||
| 103 | refreshDisplayTimer = new Timer(1000, lyssnare); |
||
| 104 | refreshDisplayTimer.start(); |
||
| 105 | |||
| 106 | //get the display list from the data repository |
||
| 107 | DataRepository.DataList displays = dataRepository.getList("displays"); |
||
| 108 | //for each defined display... |
||
| 109 | Iterator it = displays.items(); |
||
| 110 | while (it.hasNext()) { |
||
| 111 | Vector rowAddresses = new Vector(); |
||
| 112 | Vector moduleUsers = new Vector(); |
||
| 113 | |||
| 114 | //get its properties |
||
| 115 | DataRepository.DataListItem display = (DataRepository.DataListItem) it.next(); |
||
| 116 | String target = display.getField("target"); |
||
| 117 | String info = display.getField("info"); |
||
| 118 | int width = Integer.parseInt(display.getField("width")); |
||
| 119 | |||
| 120 | //get the row addresses for this display |
||
| 121 | String[] addresses = display.getField("rowaddresses").split(","); |
||
| 122 | //for each specified address |
||
| 123 | for (int i=0; i<addresses.length; i++) { |
||
| 124 | //check if valid... |
||
| 125 | if (Integer.parseInt(addresses[i]) >= 0 && Integer.parseInt(addresses[i]) < 256) { |
||
| 126 | //and add to rowAddresses-list |
||
| 127 | rowAddresses.addElement(addresses[i]); |
||
| 128 | } else { |
||
| 129 | _errors.println("Invalid display address specified in '" + configFile + "' !"); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | //get the users of this display |
||
| 134 | String[] users = display.getField("users").split(","); |
||
| 135 | //add each user to the moduleUsers-list |
||
| 136 | for (int i = 0; i < users.length; i++) { |
||
| 137 | moduleUsers.addElement(users[i]); |
||
| 138 | } |
||
| 139 | |||
| 140 | //if no important properties were missing for this display, |
||
| 141 | if (rowAddresses.size() > 0) { |
||
| 142 | //add the display to the real display-list |
||
| 143 | int rows = rowAddresses.size(); |
||
| 144 | displayObjects.addElement(new DisplayObject(target, info, width, rows, rowAddresses, moduleUsers)); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Shuts down this module. |
||
| 151 | */ |
||
| 152 | public void shutdown() { |
||
| 153 | if (refreshDisplayTimer!=null) { |
||
| 154 | if (refreshDisplayTimer.isRunning()) { |
||
| 155 | refreshDisplayTimer.stop(); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | //shut down MbModule |
||
| 159 | super.shutdown(); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Takes care of XML-data found in incoming packets. |
||
| 164 | */ |
||
| 165 | private class PacketDataHandler implements XMLDataHandler { |
||
| 166 | /** |
||
| 167 | * Called when start of a new element is found in the XML-data. |
||
| 168 | * Ex: <name attr1="value1" attr2="value2"> |
||
| 169 | * Element name would then be "name" and attribute list |
||
| 170 | * would contain "value1" and "value2" mapped to the attribute |
||
| 171 | * names "attr1" and "attr2". |
||
| 172 | * @param element The name of the element. |
||
| 173 | * @param attributes The element attributes. |
||
| 174 | */ |
||
| 175 | public void XMLstartElement(String element, HashMap attributes) { |
||
| 176 | // saker som lcd-backligth ska kunna stallas in genom att ange en nyckel i paketet |
||
| 177 | // sånna saker ska inte skickas till DisplayObject? låt sendData ta hand om det? |
||
| 178 | //dä ska visst till displayobject, så den kan halla reda på vad som är installt |
||
| 179 | //http://deep.csbnet.se/arne/doc.arne/doc.arne.nod.lcd.html |
||
| 180 | |||
| 181 | //bygg om, behover inte innehalla ttl, prio, id osv, satt till default annars |
||
| 182 | |||
| 183 | if (element.equals("lcdpacket") && attributes.containsKey("sender") |
||
| 184 | && attributes.containsKey("type") && attributes.containsKey("data")) { |
||
| 185 | String sSender = (String)attributes.get("sender"); |
||
| 186 | String sType = (String)attributes.get("type"); |
||
| 187 | String sData = (String)attributes.get("data"); |
||
| 188 | |||
| 189 | //_debug.println("got package"); |
||
| 190 | |||
| 191 | int iPrio = 1; |
||
| 192 | if (attributes.containsKey("prio")) { |
||
| 193 | iPrio = Integer.parseInt((String)attributes.get("prio")); |
||
| 194 | } |
||
| 195 | |||
| 196 | int timeToLive = 0; |
||
| 197 | if (attributes.containsKey("ttl")) { |
||
| 198 | timeToLive = Integer.parseInt((String)attributes.get("ttl")); |
||
| 199 | } |
||
| 200 | |||
| 201 | int id = 0; |
||
| 202 | if (attributes.containsKey("id")) { |
||
| 203 | id = Integer.parseInt((String)attributes.get("id")); |
||
| 204 | } |
||
| 205 | |||
| 206 | String rowDefine = ""; |
||
| 207 | if (attributes.containsKey("row")) { |
||
| 208 | rowDefine = (String)attributes.get("row"); |
||
| 209 | } |
||
| 210 | |||
| 211 | |||
| 212 | //sok igenom varje displayobject och fraga om de vill ha data fran sSender |
||
| 213 | for (int i = 0; i < displayObjects.size(); i++) { |
||
| 214 | DisplayObject dispObjDummy = (DisplayObject)displayObjects.elementAt(i); |
||
| 215 | |||
| 216 | if (dispObjDummy.displayModuleData(sSender)) { //om datan skall skickas till denna display |
||
| 217 | //_debug.println("add row to queue: " + sData + " " + iPrio + " " + timeToLive + " " + id + " " + rowDefine); |
||
| 218 | dispObjDummy.addRowToQueue(sData, iPrio, timeToLive, false, id, rowDefine); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Called when end of an element was found in the XML-data. |
||
| 226 | * Ex: </name> or <test attr="value" /> |
||
| 227 | * @param element The name of the element. |
||
| 228 | */ |
||
| 229 | public void XMLendElement(String element) {} |
||
| 230 | public void XMLelementData(String data) {} |
||
| 231 | public void XMLdocumentStart() {} |
||
| 232 | public void XMLdocumentEnd() {} |
||
| 233 | } |
||
| 234 | |||
| 235 | |||
| 236 | //TODO: da modulen startas upp ska displayen rensas |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * Will be called when our GUI should be shown. |
||
| 241 | */ |
||
| 242 | public void renderGUI(Container drawArea) { |
||
| 243 | gui.render(drawArea); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * A class for our GUI. |
||
| 248 | */ |
||
| 249 | class GUI implements ActionListener { |
||
| 250 | |||
| 251 | public void render(Container renderArea) { |
||
| 252 | //renderArea.setPreferredSize(150, 200); |
||
| 253 | renderArea.setLayout(new GridLayout(200,1)); |
||
| 254 | |||
| 255 | /*btnLampButtons = new JButton[roomObjects.size()]; |
||
| 256 | Iterator it = roomObjects.keySet().iterator(); |
||
| 257 | int i=0; |
||
| 258 | while (it.hasNext()) { |
||
| 259 | RoomObject o = (RoomObject)roomObjects.get((String)it.next()); |
||
| 260 | if (o!=null) { |
||
| 261 | btnLampButtons[i] = new JButton(o.name); |
||
| 262 | btnLampButtons[i].setActionCommand("roomobject:" + o.name); |
||
| 263 | btnLampButtons[i].addActionListener(this); |
||
| 264 | renderArea.add(btnLampButtons[i]); |
||
| 265 | i++; |
||
| 266 | } |
||
| 267 | }*/ |
||
| 268 | } |
||
| 269 | |||
| 270 | public void actionPerformed(ActionEvent e) { |
||
| 271 | //was this a room object-button press? |
||
| 272 | /*if (e.getActionCommand().startsWith("roomobject:")) { |
||
| 273 | //split its actioncommand to find out which lamp it was for |
||
| 274 | String[] tokens = e.getActionCommand().split("roomobject:"); |
||
| 275 | //try to find a room object with this name |
||
| 276 | RoomObject o = (RoomObject)roomObjects.get(tokens[1]); |
||
| 277 | //if found |
||
| 278 | if (o!=null) { |
||
| 279 | //toggle corresponding room object |
||
| 280 | toggleRoomObject(o.byteValue.shortValue()); |
||
| 281 | } |
||
| 282 | }*/ |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | //En lyssnare för timern |
||
| 287 | private class TimerListener implements ActionListener { |
||
| 288 | public void actionPerformed(ActionEvent e) { |
||
| 289 | //loopa igenom alla displayObjects och kor manageQueues, getDataToSend and then send data, if any |
||
| 290 | for (int i = 0; i < displayObjects.size(); i++) { |
||
| 291 | DisplayObject dispDummy = (DisplayObject)displayObjects.elementAt(i); |
||
| 292 | dispDummy.manageQueues(); |
||
| 293 | //_debug.println("managing queues"); |
||
| 294 | Vector vDataToSend = dispDummy.getDataToSend(); |
||
| 295 | //MbPacket p; |
||
| 296 | for (int j = 0; j < vDataToSend.size(); j++) { |
||
| 297 | MbPacket p = (MbPacket)vDataToSend.elementAt(j); |
||
| 298 | p.setDestination(new MbLocation("self","ARNE")); |
||
| 299 | sendPacket(p); |
||
| 300 | //_debug.println("apan bepa"); |
||
| 301 | //_debug.println(p.getContents()); |
||
| 302 | |||
| 303 | } |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | } |