Permalink
Browse files

dragonfly: add button and module commands back into simulator.

  • Loading branch information...
1 parent d039da5 commit 3e0ff38ef211ca2b9b26a031a988f07ea042926c @kgilmer kgilmer committed Aug 5, 2011
@@ -0,0 +1,113 @@
+/*******************************************************************************
+ * Copyright (c) 2008, 2009 Bug Labs, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * - Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * - Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * - Neither the name of Bug Labs, Inc. nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ *******************************************************************************/
+package com.buglabs.bug.simulator;
+
+import java.io.BufferedWriter;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.knapsack.shell.pub.IKnapsackCommand;
+import org.osgi.framework.BundleContext;
+
+/**
+ * A helper base class for commands for the command line. Refer to ICommand for
+ * details of how to write commands for OSGi shell.
+ *
+ * @author kgilmer
+ *
+ */
+public abstract class AbstractCommand implements IKnapsackCommand {
+
+ protected List<String> arguments;
+
+ protected BufferedWriter outWriter;
+
+ protected OutputStream err;
+
+ protected BundleContext context;
+
+ protected OutputStream out;
+
+ public void initialize(List<String> arguments, BundleContext context) {
+ this.out = System.out;
+ if (arguments != null) {
+ this.arguments = arguments;
+ } else {
+ this.arguments = new ArrayList<String>();
+ }
+
+ this.outWriter = new BufferedWriter(new OutputStreamWriter(out));
+ this.err = System.err;
+ this.context = context;
+ }
+
+ @Override
+ public List<String> getArguments() {
+ return arguments;
+ }
+
+ /**
+ * Print message to error stream.
+ *
+ * @param message
+ * @throws IOException
+ */
+ protected void printError(String message) throws IOException {
+ err.write(message.getBytes());
+ err.flush();
+ }
+
+ /**
+ * Print line to standard out.
+ *
+ * @param message
+ * @throws IOException
+ */
+ protected void println(String message) throws IOException {
+ outWriter.write(message + "\n");
+ outWriter.flush();
+ }
+
+ /* (non-Javadoc)
+ * @see com.buglabs.osgi.shell.ICommand#isValid()
+ */
+ public boolean isValid() {
+ return true;
+ }
+
+ public String getUsage() {
+ return "";
+ }
+
+ public String getDescription() {
+ return "No help available for this command.";
+ }
+}
@@ -33,6 +33,7 @@
import javax.servlet.ServletException;
+import org.knapsack.shell.pub.IKnapsackCommand;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
@@ -54,6 +55,7 @@
import com.buglabs.bug.bmi.api.IModlet;
import com.buglabs.bug.bmi.api.IModletFactory;
import com.buglabs.bug.bmi.pub.Manager;
+import com.buglabs.bug.buttons.IButtonEventProvider;
import com.buglabs.bug.module.camera.CameraActivator;
import com.buglabs.bug.module.gps.GPSActivator;
import com.buglabs.bug.module.lcd.LCDActivator;
@@ -63,6 +65,7 @@
import com.buglabs.bug.module.vonhippel.VHActivator;
import com.buglabs.bug.simulator.controller.Server;
import com.buglabs.bug.simulator.ui.ShellIOThread;
+import com.buglabs.bug.simulator.ui.SimulatorModuleCommands;
import com.buglabs.support.SupportInfoTextFormatter;
import com.buglabs.support.SupportInfoXMLFormatter;
import com.buglabs.util.osgi.LogServiceUtil;
@@ -128,6 +131,8 @@
private ShellIOThread shellThread;
+ private List<ServiceRegistration> btnServices;
+
public void start(final BundleContext context) throws Exception {
// Basic setup ********************************************
this.context = context;
@@ -207,9 +212,42 @@ public void run(Map<String, Object> services) {
shellThread = new ShellIOThread(Integer.parseInt(System.getProperty("org.knapsack.shell.port")));
shellThread.start();
+
+ ShellButtonAdapter userBtn = new ShellButtonAdapter("user");
+ ShellButtonAdapter powerBtn = new ShellButtonAdapter("power");
+ IKnapsackCommand [] cmds = {userBtn, powerBtn};
+
+ btnServices = new ArrayList<ServiceRegistration>();
+ btnServices.add(context.registerService(IButtonEventProvider.class.getName(), userBtn, getUserButtonProperties()));
+ btnServices.add(context.registerService(IKnapsackCommand.class.getName(), userBtn, null));
+
+ btnServices.add(context.registerService(IButtonEventProvider.class.getName(), powerBtn, getPowerButtonProperties()));
+ btnServices.add(context.registerService(IKnapsackCommand.class.getName(), powerBtn, null));
+
+ SimulatorModuleCommands smc = new SimulatorModuleCommands(bmiManager);
+
+ for (IKnapsackCommand cmd : smc.getCommands())
+ btnServices.add(context.registerService(IKnapsackCommand.class.getName(), cmd, null));
+ }
+
+ private Dictionary<String, String> getPowerButtonProperties() {
+ Dictionary<String, String> d = new Hashtable<String, String>();
+ d.put("Provider", this.getClass().getName());
+ d.put("Button", "Power");
+ return d;
+ }
+
+ private Dictionary<String, String> getUserButtonProperties() {
+ Dictionary<String, String> d = new Hashtable<String, String>();
+ d.put("Provider", this.getClass().getName());
+ d.put("Button", "User");
+ return d;
}
- public void stop(BundleContext context) throws Exception {
+ public void stop(BundleContext context) throws Exception {
+ for (ServiceRegistration sr : btnServices)
+ sr.unregister();
+
shellThread.shutdown();
if (controllerServer != null) {
@@ -0,0 +1,97 @@
+/* Copyright (c) 2011 Bug Labs, Inc.
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License version
+ * 2 only, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License version 2 for more details (a copy is
+ * included at http://www.gnu.org/licenses/old-licenses/gpl-2.0.html).
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this work; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+package com.buglabs.bug.simulator;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.knapsack.shell.pub.IKnapsackCommand;
+
+import com.buglabs.bug.buttons.ButtonEvent;
+import com.buglabs.bug.buttons.IButtonEventListener;
+import com.buglabs.bug.buttons.IButtonEventProvider;
+
+/**
+ * Allows for 'virtual' buttons on device that can be triggered from OSGi shell.
+ *
+ * @author kgilmer
+ *
+ */
+public class ShellButtonAdapter extends AbstractCommand implements IButtonEventProvider, IKnapsackCommand {
+
+ private List<IButtonEventListener> listeners;
+ private final String name;
+ private int buttonId;
+
+ /**
+ * @param name must be "POWER" or "USER".
+ *
+ */
+ public ShellButtonAdapter(String name) {
+ this.name = name;
+
+ if (name.toUpperCase().equals("POWER")) {
+ buttonId = ButtonEvent.BUTTON_BUG20_POWER;
+ } else if (name.toUpperCase().equals("USER")) {
+ buttonId = ButtonEvent.BUTTON_BUG20_USER;
+ } else {
+ throw new IllegalArgumentException("Unknown button type: " + name);
+ }
+ }
+
+
+ public void addListener(IButtonEventListener listener) {
+ if (listeners == null) {
+ listeners = new ArrayList<IButtonEventListener>();
+ }
+
+ if (!listeners.contains(listener)) {
+ listeners.add(listener);
+ }
+ }
+
+
+ public void removeListener(IButtonEventListener listener) {
+ if (listeners != null && listeners.contains(listener)) {
+ listeners.remove(listener);
+ }
+ }
+
+
+ public String execute() throws Exception {
+ if (listeners != null) {
+ for (IButtonEventListener listener: listeners) {
+ listener.buttonEvent(new ButtonEvent(buttonId));
+ }
+ }
+
+ return "";
+ }
+
+
+ public String getName() {
+ return "btn-" + name;
+ }
+
+
+ public String getDescription() {
+ return "Trigger the " + name + " button.";
+ }
+}
Oops, something went wrong.

0 comments on commit 3e0ff38

Please sign in to comment.