Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
15 arune 1
/*
2
 * File created by arune
3
 * at 2004-dec
4
 */
5
package Macbeth.Modules.modCallerID;
6
 
7
import java.util.*;
8
import java.net.*;
9
import java.io.*;
10
import java.io.InputStreamReader;
11
import java.io.BufferedReader;
12
 
13
import Macbeth.Modules.modVagnen.StopObject;
14
import Macbeth.System.*;
15
import Macbeth.Utilities.*;
16
import Macbeth.XML.XMLDataHandler;
17
 
18
/**
19
 *
20
 *
21
 * @author arune
22
 */
23
public class modCallerID extends MbModule {
24
    //The callerID bytes that we receive via XML
25
    private UByte[] receivedBytes;
26
 
27
    private ArrayList servers = new ArrayList();    //a list of servers
28
 
29
    private String menuMiddleId;
30
    //Are we currently parsing valid ARNE remote control XML?
31
    private boolean parsingARNEXML;
32
    //menusystem specific bool, if this submenu is activated
33
    private boolean activated;
34
    //list of calls and their variables
35
    private ArrayList calls = new ArrayList();           //a map of CallObjects
36
 
37
    /**
38
     * Creates a new instance of modTestMenu.
39
     */
40
    public modCallerID() {
41
        //construct MbModule
42
        super();
43
 
44
        calls = new ArrayList();
45
        //initial values
46
        receivedBytes = new UByte[15];
47
        menuMiddleId = "0";
48
        activated = false;
49
        //register our packet data handler
50
        setPacketDataHandler(new PacketDataHandler());
51
 
52
 
53
    }
54
 
55
    public String name() {
56
        return "CallerID";
57
    }
58
 
59
    public String description() {
60
        return "Receives CallerID information from ARNE-network";
61
    }
62
 
63
    /**
64
     * This method sets default values on all _required_ data
65
     * fields in the data repository.
66
     */
67
    public void initDataFields() {
68
        //set default values on all REQUIRED option fields before starting up subsystems
69
//        options.putField("host", "localhost");
70
//        options.putField("port", "2050");
71
    }
72
 
73
    public void startup() throws MbStartupException {
74
        //startup MbModule
75
        super.startup();
76
 
77
        //get all servers from the data repository
78
        DataRepository.DataList list = dataRepository.getList("servers");
79
        if (list!=null) {
80
            Iterator it = list.items();
81
            while (it.hasNext()) {
82
                DataRepository.DataListItem item = (DataRepository.DataListItem)it.next();
83
                String serverlocation = item.getField("serverlocation");
84
                servers.add(serverlocation);
85
            }
86
        }
87
 
88
        sendToMenuSys("<registersubmod displayname=\"CallerID\" displaydesc=\"Shows the last incoming phonecalls\" visible=\"true\" />");
89
    }
90
 
91
    /**
92
     * Shuts down this module.
93
     */
94
    public void shutdown() {
95
        sendToMenuSys("<unregistersubmod />");
96
        //shut down MbModule        
97
        super.shutdown();
98
    }
99
 
100
    private void sendToMenuSys(String dataToSend) {
101
        MbPacket p = new MbPacket();
102
        p.setDestination(new MbLocation("self", "MenuSystem"));
103
        p.appendContents(dataToSend);
104
        sendPacket(p);
105
    }
106
    /**
107
     * Takes care of successfully received bytes from the callerID node.
108
     * @param bytes The bytes that have been received.
109
     */
110
    private void handleBytes(UByte[] bytes) {
111
        String callerIDdata = "";
112
        for (int i = 0; i < bytes.length; i++) {
113
            callerIDdata = callerIDdata + parseByte(bytes[i]) + "";
114
        }
115
        _debug.println("Data " + callerIDdata);
116
 
117
        calls.add(new CallObject(bytes, servers));
118
 
119
        if (activated) {
120
            sendMain();
121
        } else {
122
            sendActivationReq();    //ska be om tillåtelse att få menyn aktiverad
123
        }
124
    }
125
 
126
    private void sendActivationReq() {
127
//      ska be om tillåtelse att få menyn aktiverad
128
        String dataToSend = "<request />";
129
 
130
        //dataToSend = dataToSend + "</packet>";
131
        sendToMenuSys(dataToSend);
132
    }
133
 
134
    private void sendMain() {
135
        String dataToSend = "<packet menuid=\"MainMenu\"><data>";
136
        dataToSend = dataToSend + "<middleid>" + menuMiddleId + "</middleid>";
137
        dataToSend = dataToSend + "<list id=\"menudata\">";
138
        for (int i = 0; i < calls.size(); i++) {
139
            CallObject call = (CallObject)calls.get(calls.size()-i-1);
140
 
141
            String datetime = call.getDate();
142
            String name = call.getName();
143
            String address = call.getAddress();
144
            String number = call.getPhoneNr();
145
            String ortsnamn = call.getOrtsnamn();
146
 
147
            if (name.length() != 0) {
148
                if (address.length() != 0) {
149
                    dataToSend = dataToSend + "<item id=\"" + i + "\" title=\"" + datetime + " - " + name + "\" desc=\"" + number + "&#10;&#10;" + address + "\" />";
150
                } else if (ortsnamn.length() != 0) {
151
                    dataToSend = dataToSend + "<item id=\"" + i + "\" title=\"" + datetime + " - " + name + "\" desc=\"" + number + "&#10;&#10;" + ortsnamn + "\" />";
152
                } else {
153
                    dataToSend = dataToSend + "<item id=\"" + i + "\" title=\"" + datetime + " - " + name + "\" desc=\"" + number + "\" />";
154
                }
155
            } else {
156
                dataToSend = dataToSend + "<item id=\"" + i + "\" title=\"" + datetime + " - " + number + "\" desc=\"" + ortsnamn + "\" />";
157
            }
158
        }
159
        if (calls.size() == 0) {
160
            dataToSend = dataToSend + "<item id=\"" + 0 + "\" title=\"" + "Inga inkomna samtal" + "\" desc=\"\" />";
161
        }
162
        dataToSend = dataToSend + "</list>";
163
        dataToSend = dataToSend + "</data></packet>";
164
        sendToMenuSys(dataToSend);     
165
    }
166
 
167
    private String parseByte(UByte dobyte) {
168
        byte[] dummyByte = new byte[1];
169
        dummyByte[0] = dobyte.byteValue();
170
 
171
        return new String(dummyByte);
172
 
173
    }
174
 
175
    /**
176
     * Takes care of XML-data found in incoming packets.
177
     */
178
    private class PacketDataHandler implements XMLDataHandler {
179
        public void XMLstartElement(String element, HashMap attributes) {
180
            /* Ex: <arnepacket bytes="2">
181
             * This is the start of an ARNE packet. We receive those from
182
             * ARNE remote controls when buttons have been pressed on them. */
183
            if (element.equals("arnepacket") && attributes.containsKey("bytes")) {
184
                int bytes = Integer.parseInt((String)attributes.get("bytes"));
185
                receivedBytes = new UByte[bytes];
186
                //we should always get three bytes from an ARNE remote control (1 status + 2 ident)
187
                //if (bytes==3) {
188
                    parsingARNEXML = true;
189
                    //receivedNrOfBytes = 0;
190
                //} else {
191
                //    _errors.println("Invalid number of bytes in ARNE packet");
192
                //}
193
            }
194
            /* Ex: <byte id="1" value="5" />
195
             * This is a byte inside an ARNE packet. */
196
            else if (element.equals("byte") && attributes.containsKey("id") && attributes.containsKey("value")) {
197
                if (parsingARNEXML) {
198
                    short id = Short.parseShort(attributes.get("id").toString());
199
                    //remember byte
200
                    receivedBytes[id-1] = UByte.parseUByte((String)attributes.get("value"));
201
                } else {
202
                    _errors.println("Byte-attribute found outside arnepacket-attribute");
203
                }
204
            } else if (element.equals("activate")) {
205
                //menusystem activates this submenu
206
                //we should send a menu to display
207
                activated = true;
208
                sendMain();
209
 
210
            } else if (element.equals("deactivate")) {
211
                //menusystem deactivates this submenu
212
                activated = false;
213
 
214
            } else if (element.equals("refresh")) {
215
                //menusystem wants a refreshed menu
216
                sendMain();
217
 
218
            } else if (element.equals("padLeft") && attributes.containsKey("state") && attributes.containsKey("level")) {
219
                //user pressed padleft on his remote
220
                //we should traverse back one step or deactivate
221
                //if we are in submenu root
222
                String state = (String)attributes.get("state");
223
                int level = Integer.parseInt((String)attributes.get("level"));
224
 
225
                if (level >= 2) {
226
                    if (state.equals("DOWN")) {
227
                        activated = false;
228
 
229
                        sendToMenuSys("<deactivates />");
230
                    }
231
                }
232
            } else if (element.equals("padRight") && attributes.containsKey("state") && attributes.containsKey("level")) {
233
                //user pressed padright on his remote
234
                //we should traverse forward one step 
235
                String state = (String)attributes.get("state");
236
                int level = Integer.parseInt((String)attributes.get("level"));
237
 
238
                if (level >= 2) {
239
                    if (state.equals("DOWN")) {
240
                    }
241
                }
242
 
243
            } else if (element.equals("padOk") && attributes.containsKey("state") && attributes.containsKey("level")) {
244
                //user pressed padOk on his remote
245
                // 
246
                String state = (String)attributes.get("state");
247
                int level = Integer.parseInt((String)attributes.get("level"));
248
 
249
                if (level >= 3) {
250
                    if (state.equals("DOWN")) {
251
                    }
252
                }
253
 
254
            } else if (element.equals("padUp") && attributes.containsKey("state") && attributes.containsKey("level")) {
255
                //user pressed padright on his remote
256
                //we should traverse forward one step 
257
                String state = (String)attributes.get("state");
258
                int level = Integer.parseInt((String)attributes.get("level"));
259
 
260
                if (level >= 1) {
261
                    if (state.equals("DOWN")) {
262
                        sendToMenuSys("<packet menuid=\"MainMenu\"><command><scroll>1</scroll></command></packet>");
263
                    } else {
264
                        sendToMenuSys("<packet menuid=\"MainMenu\"><command><scroll>0</scroll></command></packet>");
265
                    }
266
                }
267
 
268
            } else if (element.equals("padDown") && attributes.containsKey("state") && attributes.containsKey("level")) {
269
                //user pressed padright on his remote
270
                //we should traverse forward one step 
271
                String state = (String)attributes.get("state");
272
                int level = Integer.parseInt((String)attributes.get("level"));
273
 
274
                if (level >= 1) {
275
                    if (state.equals("DOWN")) {
276
                        sendToMenuSys("<packet menuid=\"MainMenu\"><command><scroll>-1</scroll></command></packet>");
277
                    } else {
278
                        sendToMenuSys("<packet menuid=\"MainMenu\"><command><scroll>0</scroll></command></packet>");
279
                    }
280
                }
281
 
282
            } else if (element.equals("middleid") && attributes.containsKey("value") && attributes.containsKey("menuid") && attributes.containsKey("userlevel")) {
283
                String middleId = (String)attributes.get("value");
284
                String menuId = (String)attributes.get("menuid");
285
                int userlevel = 0;
286
                try {
287
                    userlevel = Integer.parseInt((String)attributes.get("userlevel"));
288
                } catch (NumberFormatException ex) {
289
                    _errors.println("There was a faulty userlevel sent to me: " + (String)attributes.get("userlevel"));
290
                }
291
 
292
                if (userlevel >= 2 && menuId.equals("MainMenu")) {
293
                    menuMiddleId = middleId;
294
                }              
295
            }
296
 
297
        }
298
 
299
        public void XMLendElement(String element) {
300
            /* Ex: </arnepacket>
301
             * This is the end of an ARNE packet. This means we have now
302
             * (probably) received an ARNE remote control button press. */
303
            if (element.equals("arnepacket")) {
304
                if (parsingARNEXML) {
305
                    parsingARNEXML = false;
306
                    //handle our received bytes now
307
                    handleBytes(receivedBytes);
308
                }
309
            }
310
        }
311
        public void XMLelementData(String data) {}
312
        public void XMLdocumentStart() {}
313
        public void XMLdocumentEnd() {}
314
    }
315
 
316
}