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 2005-jan-09 16:54:44 |
||
| 4 | */ |
||
| 5 | package Macbeth.Modules.modMailcheck; |
||
| 6 | |||
| 7 | import Macbeth.System.*; |
||
| 8 | import Macbeth.Utilities.*; |
||
| 9 | import Macbeth.XML.XMLDataHandler; |
||
| 10 | import Macbeth.Utilities.*; |
||
| 11 | |||
| 12 | import java.util.*; |
||
| 13 | import javax.swing.Timer; |
||
| 14 | import java.awt.event.*; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * |
||
| 18 | * |
||
| 19 | * @author arune |
||
| 20 | */ |
||
| 21 | public class modMailcheck extends MbModule { |
||
| 22 | |||
| 23 | //list of tv-channels and their variables |
||
| 24 | private HashMap accounts; //a map of AccountObjects |
||
| 25 | private int timerInterval = 0; |
||
| 26 | private Timer requestDataTimer = null; |
||
| 27 | private String mailMode; |
||
| 28 | private String displayText; |
||
| 29 | |||
| 30 | //todo: mer utförliga felmeddelanden |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Creates a new instance of modMailcheck. |
||
| 34 | */ |
||
| 35 | public modMailcheck() { |
||
| 36 | //construct MbModule |
||
| 37 | super(); |
||
| 38 | //initial values |
||
| 39 | |||
| 40 | accounts = new HashMap(32); |
||
| 41 | //we depend on the RemoteControl-module |
||
| 42 | addDependency("ARNE"); |
||
| 43 | |||
| 44 | //register our packet data handler |
||
| 45 | setPacketDataHandler(new PacketDataHandler()); |
||
| 46 | } |
||
| 47 | |||
| 48 | public String name() { |
||
| 49 | return "Mailcheck"; |
||
| 50 | } |
||
| 51 | |||
| 52 | public String description() { |
||
| 53 | return "Module for mail, checks mail and prints a message on display"; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * This method sets default values on all _required_ data |
||
| 58 | * fields in the data repository. |
||
| 59 | */ |
||
| 60 | public void initDataFields() { |
||
| 61 | //set default values on all REQUIRED option fields before starting up subsystems |
||
| 62 | options.putField("mode", "simple"); |
||
| 63 | options.putField("checkinterval", "5"); |
||
| 64 | options.putField("displaytext", "New mail"); |
||
| 65 | // options.putField("port", "2050"); |
||
| 66 | } |
||
| 67 | |||
| 68 | public void startup() throws MbStartupException { |
||
| 69 | //startup MbModule |
||
| 70 | super.startup(); |
||
| 71 | |||
| 72 | //get all accounts from the data repository |
||
| 73 | DataRepository.DataList list = dataRepository.getList("accounts"); |
||
| 74 | if (list!=null) { |
||
| 75 | Iterator it = list.items(); |
||
| 76 | while (it.hasNext()) { |
||
| 77 | DataRepository.DataListItem item = (DataRepository.DataListItem)it.next(); |
||
| 78 | String server = item.getField("server"); |
||
| 79 | String accountname = item.getField("accountname"); |
||
| 80 | String password = item.getField("password"); |
||
| 81 | int port = Integer.parseInt(item.getField("port")); |
||
| 82 | accounts.put(accountname + server, new AccountObject(server, port, accountname, password)); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | mailMode = options.getField("mode"); |
||
| 87 | displayText = options.getField("displaytext"); |
||
| 88 | |||
| 89 | timerInterval = Integer.parseInt(options.getField("checkinterval")); |
||
| 90 | if (timerInterval > 0) { |
||
| 91 | TimerListener lyssnare = new TimerListener(); |
||
| 92 | requestDataTimer = new Timer(1000*60 * timerInterval, lyssnare); |
||
| 93 | requestDataTimer.start(); |
||
| 94 | } |
||
| 95 | |||
| 96 | |||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Shuts down this module. |
||
| 101 | */ |
||
| 102 | public void shutdown() { |
||
| 103 | //stop timer if it's running |
||
| 104 | if (requestDataTimer!=null) { |
||
| 105 | if (requestDataTimer.isRunning()) { |
||
| 106 | requestDataTimer.stop(); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | //shut down MbModule |
||
| 110 | super.shutdown(); |
||
| 111 | } |
||
| 112 | |||
| 113 | private void simpleSendToDisplay() { |
||
| 114 | MbPacket p = new MbPacket(); |
||
| 115 | p.setDestination(new MbLocation("self","SmallDisplays")); |
||
| 116 | //ARNE packet in XML-format |
||
| 117 | p.setContents(" <lcdpacket sender=\"" + name() |
||
| 118 | + "\" type=\"textrow\" prio=\"1\" ttl=\"20\"" |
||
| 119 | + " id=\"" + 100559325 + "\"" |
||
| 120 | + " data=\"" + displayText + "\"" |
||
| 121 | + " />"); |
||
| 122 | sendPacket(p); |
||
| 123 | } |
||
| 124 | |||
| 125 | private void sendToDisplay(int nrOfNewMails) { |
||
| 126 | MbPacket p = new MbPacket(); |
||
| 127 | p.setDestination(new MbLocation("self","SmallDisplays")); |
||
| 128 | //ARNE packet in XML-format |
||
| 129 | String displayTextReplaced = displayText.replaceAll("#", "" + nrOfNewMails); |
||
| 130 | p.setContents(" <lcdpacket sender=\"" + name() |
||
| 131 | + "\" type=\"textrow\" prio=\"1\" ttl=\"20\"" |
||
| 132 | + " id=\"" + 100559325 + "\"" |
||
| 133 | + " data=\"" + displayTextReplaced + "\"" |
||
| 134 | + " />"); |
||
| 135 | sendPacket(p); |
||
| 136 | } |
||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | /** |
||
| 141 | * Takes care of XML-data found in incoming packets. |
||
| 142 | */ |
||
| 143 | private class PacketDataHandler implements XMLDataHandler { |
||
| 144 | public void XMLstartElement(String element, HashMap attributes) {} |
||
| 145 | public void XMLendElement(String element) {} |
||
| 146 | public void XMLelementData(String data) {} |
||
| 147 | public void XMLdocumentStart() {} |
||
| 148 | public void XMLdocumentEnd() {} |
||
| 149 | } |
||
| 150 | //En lyssnare för timern |
||
| 151 | private class TimerListener implements ActionListener { |
||
| 152 | |||
| 153 | public void actionPerformed(ActionEvent e) { |
||
| 154 | if (!accounts.isEmpty()) { |
||
| 155 | Iterator it = accounts.values().iterator(); |
||
| 156 | boolean newMess = false; |
||
| 157 | int nrOfMess = 0; |
||
| 158 | while (it.hasNext()) { |
||
| 159 | //_debug.println("timerevent, looping through sensorNodes"); |
||
| 160 | AccountObject o = (AccountObject)it.next(); |
||
| 161 | //String f = (String) it.next(); |
||
| 162 | //String f = (String) sensorNodes.get(key); |
||
| 163 | if (o != null && !newMess) { |
||
| 164 | try { |
||
| 165 | |||
| 166 | if (mailMode.equals("simple")) { |
||
| 167 | newMess = o.hasNewMessages(); |
||
| 168 | } else if (mailMode.equals("verbose")) { |
||
| 169 | nrOfMess = nrOfMess + o.nrOfMessages(); |
||
| 170 | } |
||
| 171 | //_info.println("New mail: " + newMess); |
||
| 172 | } catch (Exception e2) { |
||
| 173 | _errors.println(e2); |
||
| 174 | } |
||
| 175 | //sleep(200); |
||
| 176 | |||
| 177 | } |
||
| 178 | } |
||
| 179 | if (newMess && mailMode.equals("simple")) { |
||
| 180 | simpleSendToDisplay(); |
||
| 181 | } else if (nrOfMess > 0 && mailMode.equals("verbose")) { |
||
| 182 | sendToDisplay(nrOfMess); |
||
| 183 | } |
||
| 184 | //_info.println("nr of mails " + nrOfMess); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |