Category Archives: Engineering

Allowing digest auth with CORS on Chrome and LIGHTTPD

LigHTTPD is a very popular web server to use nowadays and it powers a lot of devices. I’ve mainly seen it in rasberry pi’s and other smaller servers but it’s powerful nonetheless.

Today I did some fiddling around with CORS to get data passed through another domain than the site was residing on. It all seemed fairly easy as it’s just a simple header to add to your server response, as described on enable cors

Turned out it wasn’t that easy. The server with the APIs are armed with Digest authentication for authorization and chrome has made it a must to send HTTP OPTIONS to check some parameters from the server.

Chrome needs in order to authenticate:

  • Access-Control-Allow-Origin: *
  • Access-Control-Expose-Headers: WWW-Authenticate
  • Access-Control-Allow-Headers: Authorization
  • Access-Control-Allow-Methods: POST, GET, OPTIONS

That’s easy enough with LigHTTPDs extra environment header in the server configuration:

setenv.add-response-header = (
"Access-Control-Allow-Origin" => "*",
"Access-Control-Expose-Headers" => "WWW-Authenticate",
"Access-Control-Allow-Headers" => "Authorization",
"Access-Control-Allow-Methods" => "POST, GET, OPTIONS"

)

But, that isn’t enough. If you have set up a rule in your server configuration that you require to authenticate, Chrome will fail when it gets 401 on it’s OPTION request and will not continue. In order to get that worked around I had to do some modifications in mod_auth to make sure it doesn’t authenticate on HTTP OPTIONS. It was an easy hack to implement once you figured out the functions and where to look.

All the needed code was:

//Hack to make OPTIONS pass through AUTH filter with just sending the headers needed by CORS on Chrome
const char *htm = get_http_method_name(con->request.http_method);
if(strcmp(htm,"OPTIONS") == 0){
con->http_status = 200;
con->mode = DIRECT;
return HANDLER_FINISHED;
}

I’ve attached the modified .c file for some convenience. I used the latest (to date) official release 1.4.39. Hope this helps anyone out there who needs to use digest over CORS with LigHTTPD.

mod_auth

/Marcus

Running Python on Axis devices

Axis’ platform for running applications on the camera itself is really great. For you who haven’t seen ACAP share I really suggest you click yourself over there to see what it’s all about.

As mentioned in other posts, Axis have created an embedded platform where you can install applications on the different devices to add features to the edge. This can be analytics that is looking for something unique like a people counter or advanced cross line-type detection. It can also be for simpler tasks like sending a message using a service platform similar to the pushbullet integration that I published here some time ago.

The only downside is that to develop these applications you have to know C which can be tough for the average script kiddie who just wants to perform something small. So, in order to solve some of the more simple scripting that you can be interested in doing. On ACAP Share there’s a PHP application that allows someone to write a PHP script that can be executed on a camera. This is a great application if you’re looking to use smaller APIs for your task.

PHP is extremely popular and allows you to do a number of great things. Python is another extremely popular language. Soo.. Enough said, below is the compiled ACAP and how to build it if you have the environment.

Build script:

buildpython

The python binary itself:

Python_2.7.1_1_0_mipsisa32r2el

Recommended FW: 5.80+. The reason is that the package is pretty big and will need some space. We’ve seen problems with 5.60 and 5.70 firmware that doesn’t allow the package to go through due to it’s size. After 5.80 it seems fine though. (Yes, python is a big library)

It’s build to run on the MIPS architecture, meaning it will work on ARTPEC-4 and ARTPEC-5 devices but that’s it. Python version is 2.7.1 with some limitation built to it (see buildpython.sh)

When you upload the binary there’s an attached python script in that folder. Edit that and make sure you run a continuous loop on it to perform whatever you want to achieve.

ACAP is actually so powerful that you can create your own applications that are essentially script that runs off another application. To start a python script from the command line, use:

LD_LIBRARY_PATH=/usr/local/packages/python_2_7_1/lib /usr/local/packages/python_2_7_1/bin/python /path/to/script.py

This can be called from system(); as well in your C application.

Hopefully this can save some time for people who’re interested in developing in python on Axis devices.

/Marcus

Pushbullet integration for Axis devices

Beta 0.1 available here: Push_trigger_0_1_mipsisa32r2el (Artpec-4 devices only)

IMG_0283[1]

Device image sent through PushBullet

Hi everyone,

Some time ago (last month according to picture) I needed to have images pushed out to my phone from an Axis device. There are multiple ways of doing this already like using a VMS mobile software.

However, I didn’t need a full featured solution that can view recordings or anything like that. I just wanted to see what was going on by the representation of One image. I did some investigation on what can be done on iPhones and Androids, and there’s some various choices available.

Pushover.net is easy. Free on the web and the app is a couple of bucks. Their API support HTTP as well which makes it easy to integrate in an ACAP environment. The only downside of pushover is that it can’t send images. Only text and HTTP links. The only one I found who can push images is pushbullet.com – hence the integration.

Feel free to use it, I haven’t tested it too much but it seems to work good 🙂

Installation steps:

  1. Download push bullet on your phone and set up an account at pushbullet.com
  2. Take note of your Access Token (once registered) here https://www.pushbullet.com/account
  3. Install the ACAP app on a ARTPEC-4 with firmware version 5.60+ on it
  4. Enter the Access Token into the User token field in the App settings
  5. Star the application
  6. Create an event (Motion detection or any trigger you’re interested in) that sends an internal HTTP message to: 127.0.0.1/axis-cgi/io/virtualinput.cgi?action=6:/\
  7. Test the trigger. Do you receive a picture in your pushbullet app?

 

Axis Parameter Class for Python

A couple of days ago I was in need of making some scripts that removed duplicate parameter groups on a couple of Axis devices. For those of you who don’t know, Axis creates Digital Surveillance Cameras with IP technology, which in practice means that It’s surveillance cameras running Linux.

All Axis devices offers an open API for developers who want to develop  that uses Axis devices. The most frequent API used is the Parameter API, that allows to change basic settings, create motion detection windows, events with triggers and so on. And this is the API that I had to implement a class for.

The class is written in Python and is pretty much an object oriented interface. Example

param1 = AxisParameter("Motion.M0.Left","1120")
#param1.name = Motion.M0.Left, param1.value = "1120"
param2 = AxisParameter("Properties.Resolution")
#param2.name = Properties.Resolution, param2.value = None

Here we have two parameters that can be accessed from the class. param1 contains a parameter with an assigned value to it, this can be used to modify an already existing parameter. param2 contains a parameter name, but have no idea of the value of it, better used to receive the parameters

To use the class, you must first import the data from it.

from AxisParameterClass import * #import both the working class and the information class

paramclass = AxisParameterClass("some.ip","username","password","vapixversion")

All arguments are optional and can be set later with the following functions

  • setIP
  • setCredentials
  • setVapixVersion

Axis currently have two API’s for handling vapix parameters. The only big difference is the URL, but still. The class will automatically try to determine the vapix version if not set before.

Receive parameters

To get parameters from a device, simply create a parameter that you’re interesting, both groups and subgroups are fine to use:

paramclass.getParameters([AxisParameter('Motion'), AxisParameter('Properties'), AxisParameter('NonExistent')])

The class will return a list of all parameters that matches the search. Is a parameter not found, the Parameter will be returned with the value None. All parameters that are found have their value as a string.

Remove parameters

Just as add. Create your list, and send to

paramclass.removeParameters([AxisParameter('Motion')])

Parameters returned have a status parameter that refers if the parameter was removed or not:

AxisParameter.status = "OK" #removed
AxisParameter.status = "Error" #not removed.

Update Parameters (Implementation still needs some work)

To update, pass the list into

paramclass.updateParameters([AxisParameter('Motion.M0.Left','1123')] )

If all works, the parameters returned will contain the same result as what you passed in.

 

Download

It can all be downloaded from github, licensed under MIT, so feel free to do whatever you like with it 🙂
https://github.com/marcusfollrud/AxisParameterPython 

Developing with Android

Sunday of text

Sunday of text

It’s been a while since I’ve had any needs of sitting by the computer and write any code. I just haven’t felt the need of doing it, or have I thought that it would’ve been fun. Idk, I guess that I’ve just lost the fun part of developing.

Anyways, some days ago I received a new phone from the company I work for. I’ve had an iPhone 3GS since it was released and it is starting to fall apart. This time I got to choose an Android phone, and the choice came down to HTC Incredible S, which is absolutely a cool phone. And Android is such a cool operating system. I really get what people mean when they say that they feel locked in with Apples iOS. Without jail breaking it, you don’t get all the nice stuff you might want, like the widgets, bit torrent client, SCP data transfer and all the media codecs. The list comes quite long if we’re suppose to write everything down what Android has and what iOS doesn’t, so lets skip it.

I still like iOS though, it’s really a great operating system as well.

So, after using the Android OS for a while I decided to look into the development environments of it. I knew it was based on Java, which I’m not too fond of, not due to it’s language, but to the performance, if it wasn’t running on Windows, it was terribly slow. But for Android, I was completely wrong, It responds fast, and it reminds a lot of C# (duh). So today I decided to do something for Android, just to see how it works. I chose to make an Tabata, or Interval sprint timer, that give you a honk on the horn when to sprint or to rest. From idea to working app in the phone was about 5 hours. I’m amazed how fast it went, for not having done too much development in the last months.

Below you can find a video of the result (P.s my writing feels like crap, it’s been a while since I expressed my mind on the keyboard 🙂 D.S):

IMG_0002

 

 

 

 

 

 

Adding bit.ly support to mu-feeder

My twitterfeed haven’t been working for  a while now. The last update was 2009-11-25, so I went out googling on “twitterfeed replacement” and found mu-feeder. A small python script that publish your blog entrys (or whatever RSS feed that you might have) to twitter. It’s very sweet but i missed the bit.ly support.

But, since it’s open source, there’s nothing that you can’t do about it :). I went out hunting and i found python-bitly. A library for the bit.ly API!

After some tweaking in the bitly file I got it working.

Step 1: Download mu-feeder and unpack it.
Step 2: Download the modified bitly file and put it in mu-feeder/shorteners (diff here)
Step 3:  Edit your mu-feeder settings.py. set URL_SHORTENING_SERVICE to “bitly” and URL_SHORTENER_LOGIN plus URL_SHORTENER_PASSWORD to your bit.ly username and the API key that you have on bit.ly.

That’s it. Now it should work just fine :).

Have a nice weekend.
Marcus

Getting Drupal Modules using Python

A good friend of mine wrote yesterday a small python script that allows the user to search and download Drupal modules directly via the official ftp.
This was made since it is quite boring/irritating to first browse through the module page on drupal.org and then copy the link to a shell where you download the module you want on your web server.

Instead, Jonas (that’s my friends name, btw) wrote this program that directly scans through the drupal module directory on the ftp and returns the output in a shell where the user just enter the choice that he/she wants. Example:

jonas@thinkpad61:~$ ./idm.py hello
Found 32436 files!
[0] hellomobile-5.x-1.0.tar.gz
[1] hellomobile-5.x-1.1.tar.gz
[2] hellomobile-5.x-1.x-dev.tar.gz
[3] hellotxt-6.x-1.x-dev.tar.gz
Which module do you want to download? 1
Ok, lets get hellomobile-5.x-1.1.tar.gz
jonas@thinkpad61:~$ ls -l hellomobile-5.x-1.1.tar.gz
-rw-r--r-- 1 jonas jonas 824473 2009-10-22 22:34 hellomobile-5.x-1.1.tar.gz

I believe that this is quite usefull and interesting. But, as Jonas mentions in this post. It does not contain the functions that you might need, such as: version filter and dev filter. Meaning that you don’t want to see results for drupal 5 if you are running six. Nor do you want to use developer packages when you are in a critical installation.

I had some time so I made some improvements on the original:

tomburk@linux-e9bm:~> python idm-mf.py --help
Usage: idm-mf.py [options]

Options:
-h, --help            show this help message and exit
-f version, --filter=version
Filter the results in version numbers.
-d, --dev             Show developer packages

This allows the user to filter between the version, of course “all” can be applied, if one wants to show all the results. Plus, It’s possible to toggle between showing the developer packages or not.

Final result:

tomburk@linux-e9bm:~> python idm-mf.py -f 5 hello
Found a total of 32451 files!
Files containing "hello"
 Based on filter: 5
  Not showing developer packages
[0] hellomobile-5.x-1.0.tar.gz
[1] hellomobile-5.x-1.1.tar.gz
Which module do you want to download? 1
Ok, lets get hellomobile-5.x-1.1.tar.gz
tomburk@linux-e9bm:~> ls -s hellomobile-5.x-1.1.tar.gz
812 hellomobile-5.x-1.1.tar.gz

The file is available here: http://marcusfollrud.net/wp-content/idm-mf.py
Or at github: http://github.com/marcusfollrud/idm
See you!

A small update

2009-10-24 – Added support for multiple file download, Available on both github and in wp-content

#MonoTouch how to – Drag and Drop Image

My blog posts are commonly in Swedish, but since this might interest users outside the borders of Sweden I decided to write this post in English.

Today, I’ll show how easy it is to Drag an Image around the screen using MonoTouch. Bear with me though, it’s my first how to :).

It’s actually really simple, and you won’t need the Interface Builder.

What we need to do is to create an UIImageView class that overrides the functions TouchesBegan, TouchesMoved and TouchesEnded. It’s looking like this:

	public class myDraggableImage : UIImageView {
		
		//Store locations for remembering the last positions, and counting the future ones.
		PointF Location;
		PointF StartLocation;
		
		bool haveBeenTouchedOnce = false;
				
		public myDraggableImage ( RectangleF frame ){
		
					//Set the position of the frame with RectangleF (Replacement of CGRectangle)
					this.Frame = frame;
					StartLocation = this.Frame.Location;
		}
				
		//This event occurs when you just touch the object
		public override void TouchesBegan (MonoTouch.Foundation.NSSet touches, MonoTouch.UIKit.UIEvent e)
		{
			Console.WriteLine("Touched the object");
			Location = this.Frame.Location;
			
			var touch = (UITouch) e.TouchesForView (this).AnyObject;
			var bounds = Bounds;

			StartLocation = touch.LocationInView(this);
			this.Frame = new RectangleF(Location,bounds.Size);
			
		}
		//This event occurs when you drag it around
		public override void TouchesMoved (MonoTouch.Foundation.NSSet touches, MonoTouch.UIKit.UIEvent e)
		{
			Console.WriteLine("Dragged the object");
			var bounds = Bounds;
			var touch = (UITouch) e.TouchesForView (this).AnyObject;

			//Always refer to the StartLocation of the object that you've been dragging.
			Location.X += touch.LocationInView(this).X - StartLocation.X;
			Location.Y += touch.LocationInView(this).Y - StartLocation.Y;
		
			
			this.Frame = new RectangleF(Location,bounds.Size);
			
			
			haveBeenTouchedOnce = true;
		}
		
		public override void TouchesEnded (MonoTouch.Foundation.NSSet touches, MonoTouch.UIKit.UIEvent e)
		{
			StartLocation = Location;
		}


	}

What’s being done here is that as soon as we Touch the object it will run “TouchesBegan” to start keeping track of the Object. And when we are moving it around, it constantly calls “TouchesMoved” where we are updating the position of the UIImageView on the screen.

When you’ve implemented the class class in your project, all you need to do is to create the object within “public override bool FinishedLaunching”.
It can for instance look like this:

myDraggableImage img = new myDraggableImage(new RectangleF(64,64,64,64));
			img.UserInteractionEnabled = true;
			img.BackgroundColor = UIColor.Green;
			img.Hidden = false;

			window.AddSubview(img);

The result will be a UIImageView that can be dragged around the screen.

Publish to WordPress plasmoid v0.02

Igårkväll satt jag och skissade lite på hur framtida versioner av wordpress-plasmoiden skulle kunna se ut.

Jag använde mig utav QT Designer för att göra själva moc-upen och det blev såhär:

Wordpress Plasmoid

Wordpress Plasmoid

Plasmoiden blir lite större, men känns mer komplett, med möjligheterna att:

  • Lägga till taggar
  • Markera flera kategorier
  • Editera i redan skapade bloggposter
  • Förhandsvisa material

Jag tror det kan bli riktigt bra!
Funderar även på hur mycket jobb det skulle vara att lägga in bilder med drag-n-dropfunktionalitet. WordPress libbet som är integrerat stöder det, men det är nog lite jobb att ställa in storlek med en extra dialogruta. Vi får helt enkelt se.

Publish to WordPress Plasmoid

Vill du blogga på din wordpressblogg utan att aktivt gå in via din webbläsare?

Isåfall har jag en lösning att erbjuda dig 🙂

Publish to WordPress Plasmoid är en plasmoid till KDE4 som enkelt låter dig skriva ett blogginlägg utan att gå in direkt på själva bloggsidan.

Den är väldigt enkelt skriven och är baserad på pyKDE och wordpresslib.

Just nu erbjuder den inget annat än det mest simpla. Att skapa ett inlägg med enbart en titel och text.

Funktioner som skulle kunna integreras är taggar,kategorier och inmatning av bilder. Men, det har jag inte gjort i denna release.

Vill du testa den?
Ladda ner här

Kolla källkoden på github (GPL licensierad):
http://github.com/marcusfollrud/Publish-to-Wordpress-plasmoid/tree/master

Kontaka mig gärna ifall du skulle vilja hjälpa till med något!
mail: marcus.follrud<canelbull>gmail.com