1 package org.rpi.plugin.lirc;
2
3 import java.io.File;
4 import java.util.Observable;
5 import java.util.Observer;
6 import java.util.concurrent.ConcurrentHashMap;
7
8 import javax.xml.parsers.DocumentBuilder;
9 import javax.xml.parsers.DocumentBuilderFactory;
10
11 import net.xeoh.plugins.base.annotations.PluginImplementation;
12 import net.xeoh.plugins.base.annotations.events.Shutdown;
13
14 import org.apache.log4j.Logger;
15 import org.rpi.os.OSManager;
16 import org.rpi.player.PlayManager;
17 import org.rpi.player.events.EventBase;
18 import org.rpi.player.events.EventSourceChanged;
19 import org.rpi.player.events.EventStandbyChanged;
20 import org.rpi.plugingateway.PluginGateWay;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23 import org.w3c.dom.Node;
24 import org.w3c.dom.NodeList;
25
26 @PluginImplementation
27 public class LircIntegrationImpl implements LircIntegrationInterface, Observer {
28
29 Logger log = Logger.getLogger(LircIntegrationImpl.class);
30 ConcurrentHashMap<String, LIRCCommand> commands = new ConcurrentHashMap<String, LIRCCommand>();
31 LIRCWorkQueue wq = null;
32
33 public LircIntegrationImpl() {
34 log.debug("Starting LircIntegrationImpl");
35 try {
36 wq = new LIRCWorkQueue();
37 wq.start();
38 } catch (Exception e) {
39 log.error("Error: Starting WorkQueue");
40 }
41 getConfig();
42
43
44 PlayManager.getInstance().observVolumeEvents(this);
45 PlayManager.getInstance().observeProductEvents(this);
46
47 PluginGateWay.getInstance().addObserver(this);
48 }
49
50 @Override
51 public void update(Observable o, Object event) {
52 LIRCCommand command = null;
53 EventBase base = (EventBase) event;
54 switch (base.getType()) {
55 case EVENTREQUESTVOLUMEINC:
56 command = commands.get("VolumeInc");
57 wq.put(command.getCommand());
58 break;
59 case EVENTREQUESTVOLUMEDEC:
60 command = commands.get("VolumeDec");
61 wq.put(command.getCommand());
62 break;
63 case EVENTSOURCECHANGED:
64 EventSourceChanged es = (EventSourceChanged) event;
65 String name = es.getName();
66 log.debug("Source Changed: " + name);
67 command = commands.get("SourceChanged@" + name);
68 if (command != null) {
69 wq.put(command.getCommand());
70 } else {
71 log.debug("Could Not Find Command for SourceChanged@" + name);
72 }
73 break;
74 case EVENTSTANDBYCHANGED:
75 EventStandbyChanged esb = (EventStandbyChanged) event;
76 if(esb !=null)
77 {
78 if(esb.isStandby())
79 {
80 command = commands.get("StandbyChanged@true");
81 }
82 else
83 {
84 command = commands.get("StandbyChanged@false");
85 }
86 if (command != null)
87 wq.put(command.getCommand());
88 }
89 break;
90 }
91 }
92
93
94
95
96
97 private void getConfig() {
98 try {
99 String class_name = this.getClass().getName();
100 log.debug("Find Class, ClassName: " + class_name);
101 String path = OSManager.getInstance().getFilePath(this.getClass(), false);
102 log.debug("Getting LIRCConfig.xml from Directory: " + path);
103 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
104 DocumentBuilder builder = factory.newDocumentBuilder();
105 Document doc = builder.parse(new File(path + "LIRCConfig.xml"));
106 NodeList mappings = doc.getElementsByTagName("Mapping");
107 int i = 1;
108 for (int s = 0; s < mappings.getLength(); s++) {
109 String event = null;
110 String command = null;
111 String name = null;
112 Node mapping = mappings.item(s);
113 if (mapping.getNodeType() == Node.ELEMENT_NODE) {
114 Element element = (Element) mapping;
115 event = getElementTest(element, "Event");
116 command = getElementTest(element, "Command");
117 name = getElementTest(element, "Name");
118 String key = event;
119 if (name != null && !name.equalsIgnoreCase(""))
120 key += "@" + name;
121 addToCommands(key, command, name);
122 }
123 }
124 } catch (Exception e) {
125 log.error("Error Reading LIRCConfig.xml");
126 }
127 }
128
129 private void addToCommands(String event, String command, String name) {
130 if (!commands.containsKey(event)) {
131 LIRCCommand cmd = new LIRCCommand(command, name);
132 commands.put(event, cmd);
133 }
134 }
135
136
137
138
139
140
141
142 private String getElementTest(Element element, String name) {
143 String res = "";
144 NodeList nid = element.getElementsByTagName(name);
145 if (nid != null) {
146 Element fid = (Element) nid.item(0);
147 if (fid != null) {
148 res = fid.getTextContent();
149 return res;
150 }
151 }
152 return res;
153 }
154
155 @Shutdown
156 public void bye() {
157 log.debug("ShutDown Called");
158 if (wq != null) {
159 wq.clear();
160 wq = null;
161 }
162 }
163
164 }