Archive

Author Archive

Publishing to CometD/Bayeux

December 16th, 2011 Ed No comments

After following one of my two previous blog posts, you should be in a state ready to pub/sub to/from your web pages. Here’s a tiny bit of code that shows how you can push messages:

Thread t = new Thread(){
	int count = 0;
	@Override
	public void run() {
		while (true) {
			Mutable msg = bayeux.newMessage();
			msg.setChannel("/test");
			HashMap map = new HashMap();
			map.put("key1", count + "");
			msg.setData(map);

			ServerChannel c = bayeux.getChannel("/test");
			if (c != null) {
				c.publish(null, msg);
			}
			count++;
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
};
t.start();

I put that in the init() call of BayeuxInitialiser. Now when you subscribe to /test, you will get a message with an ever increasing value.

Very easy, very powerful. Would make a nice bridge between MQTT and the web, especially as it doesn’t rely on websockets etc.

Categories: Web Tags: , , ,

OSGi, Jetty, CometD, Bayeux and Dojo

December 15th, 2011 Ed No comments

How many keywords can I stuff into that title!! I succeeded to get Jetty, CometD and Dojo working together, without OSGi in the last blog post. However, moving that into an OSGi environment was sadly not as easy as it should have been, but I got there! This post should help you get things up and running – there are bits I understand, and bits I don’t!! It is as cut down as I could make it. Here goes:

In eclipse, make yourself a new Plugin Project, mine’s called com.jellard.comet, choosing the Equinox OSGi framework. Edit MANIFEST.MF to look like below:

Add this to the MANIFEST.MF as well:
Web-ContextPath: /comettest

Put these files in your project, and add them to the build path: cometd-java-server-2.3.1.jar, bayeux-api-2.3.1.jar, cometd-java-common-2.3.1.jar

Copy this file into a new class, com.jellard.comet.BayeuxInitialiser:

package com.jellard.comet;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.cometd.bayeux.server.BayeuxServer;
import org.cometd.bayeux.server.ServerChannel;
import org.cometd.bayeux.server.ServerMessage;
import org.cometd.bayeux.server.ServerSession;
import org.cometd.server.DefaultSecurityPolicy;

public class BayeuxInitialiser extends GenericServlet
{
	private static final long serialVersionUID = 4696913071422149884L;

	public void init() throws ServletException
    {
        BayeuxServer bayeux = (BayeuxServer)getServletContext().getAttribute(BayeuxServer.ATTRIBUTE);
        bayeux.setSecurityPolicy(new DefaultSecurityPolicy(){

			@Override
			public boolean canCreate(BayeuxServer server,
					ServerSession session, String channelId,
					ServerMessage message) {
				return true;
			}

			@Override
			public boolean canHandshake(BayeuxServer server,
					ServerSession session, ServerMessage message) {
				return true;
			}

			@Override
			public boolean canPublish(BayeuxServer server,
					ServerSession session, ServerChannel channel,
					ServerMessage message) {
				return true;
			}

			@Override
			public boolean canSubscribe(BayeuxServer server,
					ServerSession session, ServerChannel channel,
					ServerMessage message) {
				return true;
			}

        });
        new HelloService(bayeux);
    }

    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
    {
        throw new ServletException();
    }
}

Make a new class, com.jellard.comet.HelloService, and paste this in:

package com.jellard.comet;

import java.util.HashMap;
import java.util.Map;

import org.cometd.bayeux.Message;
import org.cometd.bayeux.server.BayeuxServer;
import org.cometd.bayeux.server.ServerSession;
import org.cometd.server.AbstractService;

public class HelloService extends AbstractService
{
    public HelloService(BayeuxServer bayeux)
    {
        super(bayeux, "hello");
        addService("/service/hello", "processHello");
    }

    public void processHello(final ServerSession remote, Message message)
    {
        Map input = message.getDataAsMap();
        String name = (String)input.get("name");

        Map output = new HashMap();
        output.put("greeting", "Hello, " + name);
        remote.deliver(getServerSession(), "/hello", output, null);

        Thread t = new Thread(){
        	int count = 0;
			@Override
			public void run() {
				while (true) {
					remote.deliver(getServerSession(), "/reply", count + "", null);
					System.out.println(count);
					count++;
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

        };
        t.start();
    }
}

Create folders ~/jetty/etc/ and place this file in, as jetty.xml:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">

<!-- =============================================================== -->
<!-- Configure the Jetty Server                                      -->
<!--                                                                 -->
<!-- Documentation of this file format can be found at:              -->
<!-- http://docs.codehaus.org/display/JETTY/jetty.xml                -->
<!--                                                                 -->
<!-- =============================================================== -->

<Configure id="Server" class="org.eclipse.jetty.server.Server">

    <!-- =========================================================== -->
    <!-- Set connectors                                              -->
    <!-- =========================================================== -->

    <Call name="addConnector">
      <Arg>
          <New class="org.eclipse.jetty.server.nio.SelectChannelConnector">
            <Set name="port"><SystemProperty name="jetty.port" default="8080"/></Set>
          </New>
      </Arg>
    </Call>

    <!-- =========================================================== -->
    <!-- Set handler Collection Structure                            -->
    <!-- =========================================================== -->

    <Set name="handler">
      <New id="Handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
        <Set name="handlers">
         <Array type="org.eclipse.jetty.server.Handler">
           <Item>
				<New id="Contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection">

			</New>
           </Item>
           <Item>
             <New id="DefaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler"/>
           </Item>
           <Item>
             <New id="RequestLog" class="org.eclipse.jetty.server.handler.RequestLogHandler"/>
           </Item>
         </Array>
        </Set>
      </New>
    </Set>
    <!-- =========================================================== -->
    <!-- Configure the deployment manager                            -->
    <!--                                                             -->
    <!-- Sets up 2 monitored dir app providers that are configured   -->
    <!-- to behave in a similaraly to the legacy ContextDeployer     -->
    <!-- and WebAppDeployer from previous versions of Jetty.         -->
    <!-- =========================================================== -->
    <Call name="addBean">
      <Arg>
        <New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">
          <Set name="contexts">
            <Ref id="Contexts" />
          </Set>
          <Call name="setContextAttribute">
            <Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg>
            <Arg>.*/jsp-api-[^/]*\.jar$|.*/jsp-[^/]*\.jar$</Arg>
          </Call>
          <!-- Providers of OSGi Apps -->
          <Call name="addAppProvider">
            <Arg>
              <New class="org.eclipse.jetty.osgi.boot.OSGiAppProvider">
              <!--
                <Set name="defaultsDescriptor"><Property name="jetty.home" default="."/>/etc/webdefault.xml</Set>
              -->
                <Set name="scanInterval">5</Set>
                <Set name="contextXmlDir"><Property name="jetty.home" default="." />/contexts</Set>
                <!-- comma separated list of bundle symbolic names that
                    contain custom tag libraries (*.tld files)
                    if those bundles don't exist or can't be loaded no errors or warning will be issued!
                    this default value is to plug the tld files of the reference implementation of JSF -->
                <Set name="tldBundles"><Property name="org.eclipse.jetty.osgi.tldsbundles"
                     default="javax.faces.jsf-impl" /></Set>
              </New>
            </Arg>
          </Call>

        </New>
      </Arg>
    </Call>

    <!-- =========================================================== -->
    <!-- extra options                                               -->
    <!-- =========================================================== -->
    <Set name="stopAtShutdown">true</Set>
    <Set name="sendServerVersion">true</Set>
    <Set name="sendDateHeader">true</Set>
    <Set name="gracefulShutdown">1000</Set>

</Configure>

Create a XML file, WEB-INF/web.xml, in your project:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<servlet>
		<servlet-name>cometd</servlet-name>
		<servlet-class>org.cometd.server.CometdServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>cometd</servlet-name>
		<url-pattern>/cometd/*</url-pattern>
	</servlet-mapping>
	<servlet>
		<servlet-name>initializer</servlet-name>
		<servlet-class>com.jellard.comet.BayeuxInitialiser</servlet-class>
		<load-on-startup>2</load-on-startup>
	</servlet>
	<filter>
		<filter-name>cross-origin</filter-name>
		<filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>cross-origin</filter-name>
		<url-pattern>/cometd/*</url-pattern>
	</filter-mapping>
</web-app>

Create a html folder in the root of your project. In there, create a js/dojo folder, and extract a build of dojo into there (so you will have a html/dojo/dojox folder, for example).

Also in the html folder, make a file, comet.html, and paste in this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script type="text/javascript">
        dojo.require("dojox.cometd");
        dojo.addOnLoad(function() {
            // Disconnect when the page unloads
            dojo.addOnUnload(function() {
               dojox.cometd.disconnect(true);
            });

            var cometURL = "/comettest/cometd";
            dojox.cometd.init(cometURL);
        });
        function subscribe() {
        	dojox.cometd.subscribe("/*", console, "log");
        }
        function publish() {
        	dojox.cometd.publish('/service/hello', { name: 'World' })
        }
    </script>
</head>
<body>
    <input type="button" onclick="subscribe()" value="1. Subscribe" />
    <input type="button" onclick="publish();" value="2. Publish" />
</body>
</html>

Right-click your project, Run, Run As… and make a new configuration under OSGi Framework. Select the following bundles, and anything else they require (I ended up with 55 selected!)

In the arguments tab, you need to add this to your VM args:
-Djetty.home=/Users/ed/jetty (if your home dir is /Users/ed)

Run the OSGi framework, and point your browser to http://localhost:8080/comettest/html/comet.html

Open up developer tools/firefox, and you should see a pending request. Click the subscribe and publish button, and as if by magic, every 5 seconds, you should see an object appear in your console.

Categories: Web Tags: , , , ,

Dojo, Comet and Jetty

December 9th, 2011 Ed No comments

There’s not a great deal of up to date documentation of how to get dojo and cometd working with the Bayeux protocol. This quick blog post should get you up and running, and is based off the “Primer” sample code that you can get here (but you don’t need it for this): http://cometd.org/documentation/howtos/primer

The following code will show you how to set everything up and then publish a message from the webpage. This will kick off a thread in Jetty that publishes an ever-increasing number every 5 seconds that will be shown on screen.

Prereqs:
Jetty 8.x server runtime setup and installed in eclipse (well documented elsewhere)
Dojo 1.7

Create a Dynamic Web Project in eclipse, pointing it at Jetty.

Add to your build path:
bayeux-api-2.3.1.jar
cometd-java-common-2.3.1.jar
cometd-java-server-2.3.1.jar

Create a GenericServlet, calling it test.BayeuxInitialiser, paste in this:

/*
 * Copyright (c) 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package test;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.cometd.bayeux.server.BayeuxServer;
import org.cometd.bayeux.server.ServerChannel;
import org.cometd.bayeux.server.ServerMessage;
import org.cometd.bayeux.server.ServerSession;
import org.cometd.server.DefaultSecurityPolicy;

public class BayeuxInitialiser extends GenericServlet
{
	private static final long serialVersionUID = -9089442901563633963L;

	public void init() throws ServletException
    {
        BayeuxServer bayeux = (BayeuxServer)getServletContext().getAttribute(BayeuxServer.ATTRIBUTE);
        bayeux.setSecurityPolicy(new DefaultSecurityPolicy(){

			@Override
			public boolean canCreate(BayeuxServer server,
					ServerSession session, String channelId,
					ServerMessage message) {
				return true;
			}

			@Override
			public boolean canHandshake(BayeuxServer server,
					ServerSession session, ServerMessage message) {
				return true;
			}

			@Override
			public boolean canPublish(BayeuxServer server,
					ServerSession session, ServerChannel channel,
					ServerMessage message) {
				return true;
			}

			@Override
			public boolean canSubscribe(BayeuxServer server,
					ServerSession session, ServerChannel channel,
					ServerMessage message) {
				return true;
			}

        });
        new HelloService(bayeux);
    }

    public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
    {
        throw new ServletException();
    }
}

Create a test.HelloService class, and paste in this:

/*
 * Copyright (c) 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package test;

import java.util.Map;
import java.util.HashMap;

import org.cometd.bayeux.Message;
import org.cometd.bayeux.server.BayeuxServer;
import org.cometd.bayeux.server.ServerSession;
import org.cometd.server.AbstractService;

public class HelloService extends AbstractService
{
    public HelloService(BayeuxServer bayeux)
    {
        super(bayeux, "hello");
        addService("/service/hello", "processHello");
        System.out.println("New HelloService");
    }

    public void processHello(final ServerSession remote, Message message)
    {
        Map input = message.getDataAsMap();
        String name = (String)input.get("name");
        Map output = new HashMap();
        output.put("greeting", "Hello, " + name);
        remote.deliver(getServerSession(), "/hello", output, null);

        Thread t = new Thread(){
        	int count = 0;
			@Override
			public void run() {
				while (true) {
					remote.deliver(getServerSession(), "/reply", count + "", null);
					System.out.println(count);
					count++;
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}

        };
        t.start();
    }
}

Now, in your WebContent directory:

Unzip dojo into js/dojo (so you will have js/dojo/dojo/* js/dojo/dijit/* etc.)

Create index.jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <script type="text/javascript" src="js/dojo/dojo/dojo.js"></script>
    <script type="text/javascript">
        dojo.require("dojox.cometd");
        dojo.addOnLoad(function() {
            // Disconnect when the page unloads
            dojo.addOnUnload(function() {
               dojox.cometd.disconnect(true);
            });

            var cometURL = "cometd";
            dojox.cometd.init(cometURL);
        });
        function subscribe() {
        	dojox.cometd.subscribe("/*", console, "log");
        }
        function publish() {
        	dojox.cometd.publish('/service/hello', { name: 'World' })
        }
    </script>
</head>
<body>
    <input type="button" onclick="subscribe()" value="1. Subscribe" />
    <input type="button" onclick="publish();" value="2. Publish" />
</body>
</html>

Replace web.xml with this:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

    <servlet>
        <servlet-name>cometd</servlet-name>
        <servlet-class>org.cometd.server.CometdServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>

        <servlet-name>cometd</servlet-name>
        <url-pattern>/cometd/*</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>initializer</servlet-name>
        <servlet-class>test.BayeuxInitialiser</servlet-class>

        <load-on-startup>2</load-on-startup>
    </servlet>

    <filter>
        <filter-name>cross-origin</filter-name>
        <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
    </filter>
    <filter-mapping>

        <filter-name>cross-origin</filter-name>
        <url-pattern>/cometd/*</url-pattern>
    </filter-mapping>

</web-app>

Now point your browser to index.jsp, click the subscribe button, and click the publish button. You should see messages appear on the screen and in your console.

Hope this helps someone!

Categories: Web Tags: , ,

Streaming from linux to ipad

October 11th, 2011 Ed 1 comment

I’ve taken the plunge and bought an iPad – very different experience to android, and so far, I still prefer androids – they’re more transparent. Didn’t like having to immediately install itunes (which needs windows/mac) to turn the iPad on, then didn’t like having to fill in a credit card number to register an account, and then didn’t like the fact that you can’t try out apps that cost (you get 15mins on android to uninstall and get a refund, no questions asked).

Anyway, I wanted to be able to:
Stream MythTV to iPad
Stream music to iPad

MythTV to iPad – really cool, using AirVideo (£1.99) and an AirVideo server that transcodes your recordings in realtime on-demand on your linux server, and spouts them out for the app on the iPad to read. Instructions here: http://wiki.birth-online.de/know-how/hardware/apple-iphone/airvideo-server-linux – except you need to download the JAR from here: http://www.inmethod.com/forum/posts/list/1856.page

Music to iPad, via “Music Player Daemon” (MPD): mpd is a daemon that can play music on local speakers, to an icecast server, and as a http stream. You can control it via a variety of plugins, inc a firefox one, bitMPC on androids, and MPoD/MPaD on i-devices. If you have “lame” setup, add this to /etc/mpd.conf:

audio_output {
type "httpd"
name "Jukebox"
encoder "lame" # optional, vorbis or lame
port "8001"
quality "6.0" # do not define if bitrate is defined
# bitrate "192" # do not define if quality is defined
format "44100:16:2"
}

Then download FStream and whack in http://IPADDRESS:8001 – tada – it works!

Categories: Uncategorized Tags:

Dojo – checking every AJAX request and error handling

August 22nd, 2011 Ed No comments

I want to check every AJAX request for a “is logged out” flag. I also want to do something nice for every AJAX error, globally (in this case, I am just going to make a dialog showing the stacktrace from tomcat).

I could do something in every load: and error:, but this does it globally:

Error handling (assumes something is subscribed to the errors topic that displays a dialog, or whatever):

(function(){
   var oldxhr = dojo.xhr;
   dojo.xhr = function(){
      return oldxhr.apply(dojo, arguments).addErrback(function(e, v){
      dojo.publish("errors", [{text:e.responseText}]);
      });
   };
})();

Checking all JSON (assumes something has subscribed to the checklogin topic to do the logic):

dojo.contentHandlers.json = function(xhr) {
   var json = dojo.fromJson(xhr.responseText || null);
   dojo.publish("checklogin", [{j:json}]);
   return json;
}

Categories: Web Tags:

Installing an Acer Revo with Ubuntu & MythTV

February 10th, 2011 Ed 3 comments

This post is not intended to be interesting, just a list of useful steps/links for setting up an Acer Revo with Ubuntu and MythTV (and is probably already out of date)

I have two TV cards – a Hauppage WinTV NOVA TD with Diversity (dual tuner freeview) and a TeVii S660 Freesat receiver (primarily for HD).

Installing
Downloaded the latest Ubuntu ISO (10.10) and used existing Ubuntu to turn a USB disk into a startup disk. Worked a treat.

Fed up with sudo password…
sudo vi /etc/sudoers, and change the line:
%admin ALL=(ALL) ALL
to
%admin ALL=NOPASSWD:ALL
This gives me sudo access with no need for a password.

Non-free drivers
System > Administration > Additional drivers
Installed both the nvidia and DVB drivers.

Installing packages
System > Adminstration > Synaptic Package Manager
Search for mythtv, mythweb, mythvideo and install them all.
I also grabbed phpmyadmin for managing the mysql database.

TeVii S660
http://ubuntuforums.org/showpost.php?p=9927773&postcount=32 – that has a link to a modified driver that works on Ubuntu 10.10. Unzip the zip, and then execute run “bash README” which executes all the commands in the README file. Turn off the machine (must turn it off, not restart it), wait a few seconds, then turn it on.

Consistent USB Device numbers
Depending on all things random, a device that reports as /dev/dvb/adapter0 now may end up as /dev/dvb/adapter1 on the next reboot (assuming you have dual tuners/multiple cards). I found udev rules didn’t work – it’d link two new adapters to the second adapter each time. However, creating /etc/modprobe.d/dvb.conf and putting “options dvb_usb_dib0700 adapter_nr=5,6″ means that the freeview tuners will always be adapter5 and adapter6, leaving the freesat tuner as adapter0 (by default).

MythTV
First, the system taskbar doesn’t vanish when running mythtv in fullscreen. To fix this – System > Pref > Appearance > Visual Effect = None

Run mythtv-setup. If it can’t connect to database, the password can be found in ~mythtv/.mythtv/mysql.txt

grano.la
Grano.la is a beautifully simple way to save money by scaling CPU appropriately – http://grano.la/

Categories: Uncategorized Tags:

Perl Serial Comms on Ubuntu 10.10

February 10th, 2011 Ed No comments

Thanks to @ceejay, here’s some code to set up serial parameters and read from the serial device. Bizarrely the usual method stopped working after an update.
#!/usr/bin/perl
use strict;
use warnings;
use Device::SerialPort;

my $serport="/dev/ttyUSB0";

my $dev = tie (*SERIAL, 'Device::SerialPort', $serport) || die "Can't tie: $!";

$dev->baudrate(4800);
$dev->databits(8);
$dev->parity("none");
$dev->stopbits(1);

#wait for device to exist
while (1) {
my $val = ;
last if $val;
}

while (1) {
my $val = ;
next unless $val;
chomp $val;
print $val . "\n";
#sleep 1;
}
# die if port closes/goes away

Categories: Uncategorized Tags:

How to calculate the average angle of bearings

August 26th, 2010 Ed 2 comments

This is going to be a tediously dull post, but given the amount of faffing I did, I don’t want to forget it!

Firstly, this is mostly not my maths – this is thanks to @helenbowyer, @bluerhinos and a parent of a scout who saw a link to my site on @rmappleby’s blog and then saw my tweet asking for help!

The problem is what happens when you cross the 0/360 boundary – how to average 5 degrees and 355 degrees to get 0 degrees. A “mean” would give 180 degrees – oops!

The answer is trigonometry, treating the angles as vectors.

First get the SIN of the angle, and divide by the COS of it. Then take the “ATAN2″ of it to get the final result. Tada!

For my application, I am putting the last 40 values into an ArrayList and calculating a weighted average (in Utilities.weightedAverage()) – you can replace this with any averaging function. Here’s the Java:

sinTotals.add(Math.sin(angleInRadians));
cosTotals.add(Math.cos(angleInRadians));

if (sinTotals.size() > 40) {
	sinTotals.remove(0);
	cosTotals.remove(0);
}
double sinAverage = Utilities.weightedAverage(sinTotals);
double cosAverage = Utilities.weightedAverage(cosTotals);

double direction =
     (Math.toDegrees(Math.atan2(sinAverage, cosAverage)) + 360) % 360;
Categories: Uncategorized Tags:

Developing RSA plugins using Eclipse

May 14th, 2010 Ed No comments

If you’re developing plugins for RSA, you’ll probably have one RSA open and a runtime RSA, both eating huge amounts of memory. To reduce the memory footprint, I’m using Eclipse to develop, but still launching a runtime RSA.

To do this, install RSA and eclipse (separately), then in your eclipse:

Install the JRE:
1. Windows > Preferences > Java > Installed JREs
2. Add c:\Program Files\IBM\SDP\jdk\jre
3. Tick its tick-box

Set the Target Platform:
1. Windows > Preferences > Plugin-in Development > Target Platform
2. Browse for c:\Program Files\IBM\SDP
3. Tick “Build target platform based on the target’s installed plugins”
4. Click “Reload”

Running as RSA
1. Run > Run Configurations
2. Change the Run as product to: com.ibm.rational.rsa4ws.product.v75.ide (or equivalent)

Tada! I’ve found I get far less out of memory errors and things are generally quicker.

Categories: Uncategorized Tags: , ,

Wild Camping and Night Photography by Torchlight

April 25th, 2010 Ed 2 comments

On the 17th April I set off to the Black Mountains for a weekend solo hike, wild-camping overnight, to gain more experience for the “Scout Terrain 1 Assessment” and “Walking Group Leader Assessment”.

Day 1 was a 25km route (horizontally), scarily that doesn’t include 1.1km ascent and 800m descent – very knackering, but I did rescue a stray dog on route!

After eating my home-made boil in the bags and waiting for it to get dark enough, I put my camera on my jumper on a rock (there was no way I was going to carry a 3kg tripod around the place), and these were the (edited) results:

For those that care, the waterfall image was taken first, with an exposure of 30s at F9, ISO 100.

The tree was taken 20mins later after much experimenting, with an exposure of 4 minutes, F9, ISO 100. This meant there was long enough for the stars to streak across the sky. I felt this distracted from the photo, so removed them – you can see them in the original out-of-the-camera image on the right (there were even more of them after tweaking the brightness levels, but you get the idea). I didn’t have the patience to enable the noise reduction function on the camera – the one where it takes the photo, then takes another photo of black for the same length of time – in hindsight, I wish I had, but hey!

Both images involved painting the area that I wanted to show up with a super-bright LED head-torch. With the waterfall image, I set the exposure to 30s, and quickly walked above the waterfall to light the area around the top, then quickly came back to light the actual waterfall. The tree shot meant using a (tempermental) shutter-release cable (£6 from ebay, so can’t expect much more), and sitting behind the camera moving the torch evenly over the tree.

So that was fun. I went to bed and woke up in the morning feeling sunburnt and knackered. Day 2’s route was a mere 16km, this time descending a total of 820m and ascending 435m.

Really can’t complain waking up here: