Thursday, 25 February 2016

Oracle APEX - Updating data in a report using AJAX

Overview

I have a simple report showing hundreds of rows of config type information which I wanted to be able to alter quickly. I don't want to open each row in a form, or have to submit a tabular form to update any changes,.. it's all too slow.

What I want instead is a checkbox type column in the report which when clicked causes an AJAX request will be fired off to the server, which in turn runs an update query.

Sounds simple enough!

Setting up the Report

Let's start by looking at the report..
 


I've added a column called 'Ignore' which looks like a radio field/item. When clicked this will run an update to toggle the value in the database table.

Here's SQL that creates this in the report (notice I'm using a graphic of a radio button instead or a HTML form elements).

CASE WHEN sm.method = '3' THEN
         '<img name='||sd.personkey||' src="#IMAGE_PREFIX#pkt_rb_chk.gif" title="Ignored, click to remove" onclick="clicked(this)">'
       ELSE
         '<img name='||sd.personkey||' src="#IMAGE_PREFIX#pkt_rb.gif" title="Click to ignore" onclick="clicked(this)">'
       END ignore,

My database table stores an ignore as a '3' in the 'method' column. And, the reason why I'm using images is because I wanted to store the record key against the images using its name attribute. (It simplifies the javascript.)

Adding the Javascript

Next I added the following code to the Function and Global Variable Declaration on the report page. It uses some handy JQuery functions to make the code easier.

function clicked(obj){
  var personkey = obj.name;
  var img = obj.src.replace(/http:+\/.+\/+/,'');
  var checked;
  if (img == 'pkt_rb.gif') {
    checked = 1;
    var match = $(obj).closest('td').prev('td').text();
    if (match == 'Automatic' || match == 'Manual'){
      // Prompt for confirmation..
      var msg=confirm("This action will remove the current match, continue?");
      if (msg!=true){
        return;
      }
    }
  }
  else { checked = 0; }
  var ajax = new htmldb_Get(null,$v('pFlowId'),
            'APPLICATION_PROCESS=ToggleIgnore',0);
  ajax.addParam('x01',personkey);
  ajax.addParam('x02',checked);
  ajax.get();
  ajax = null;
  $(obj).closest('td').next('td').text('');
  if (checked == 1){
    $(obj).closest('td').prev('td').text('Ignore');
    obj.src = obj.src.replace(/.gif$/,'') + '_chk.gif';
  }
  else {
    $(obj).closest('td').prev('td').text('-');
    obj.src = obj.src.replace(/_chk.gif$/,'') + '.gif';
  }
}

The function takes the image object and uses a regular expression to find out the image name, and thus whether it's selected or not. (This gives us the initial 'ignore' status) We throw up a confirmation box if ignore is being set and then we start the AJAX call.

The AJAX call uses an APEX standard function called html_Get. This handles the request, the parameters you need to pass, and any browser variations. In our example we don't have any values being returned to the browser, but it would be easy to handle this by altering the 'get' command.

var gReturn = ajax.get();
// then do something with this value 

The final stage is to alter the radio image, and its hover-over text. Again we use a regular expression to alter the image name.

Next we need the back-end code that updates the table.

Adding an Application Process

You possibly noticed the AJAX function above is calling an Application Process called 'ToggleIgnore'.

So I created this Application Process using the name 'ToggleIgnore' and used the following anonymous block of PL/SQL code.

DECLARE
  l_person_key varchar2(20);
  l_checkbox varchar2(1);
BEGIN
  wwv_flow_api.set_security_group_id;
  l_person_key := apex_application.g_x01;
  l_checkbox := apex_application.g_x02;
  // Update the table
  switchboard_pkg.set_ignore(
    p_personkey => l_person_key,
    p_checked   => l_checkbox
  );
END;

The parameters are passed into p_personkey and p_checked, and in my example I've stored the update code in a packaged procedure. (It simply runs an update, but you shouldn't need me to go through that part).

Hopefully that's enough to help you get started building your own custom AJAX update methods.

Monday, 21 December 2015

TrendNet TV-IP562WI IP Connected Wifi Camera

First Impressions

I wanted a camera for inside the garage, so after a bit of research I picked up a TRENDnet TV-IP562WI. The specs are quite impressive for something less than £70:-
  • 2 way audio
  • Night vision up to 5 metres
  • Receive motion & sound detection video via email or FTP
  • Record to MicroSD card or network storage device (samba server)
  • Free mobile apps
  • Wireless N networking
  • Built in mic + external speaker socket
  • Dynamic DNS (support for NO-IP.com or Dyn.com)
  • Max 20 user accounts.
  • Video Codecs/Resolution 
Video resolution is..
  • H.264: 1280 x 720 up to 30 fps 
  • MPEG4: 1280 x 720 up to 30 fps 
  • JPEG: 1280 x 720 up to 5 fps (or 640 x 480 up to 30 fps)


TrendNet TV-IP562WI - Wifi Camera

It looks pretty good, small enough to easily fit in my hand, and it comes with a solid metal stand that doubles as a mounting bracket. Also in the box was a small 5v PSU, a cat5 cable for initial config, a software CD and assorted fittings for the stand.
 
I ran the software from the CD and had the camera up and running in less than 10 minutes. All you need to do is set the admin password and configure your wireless settings, then once it restarts you continue using a web browser. But that's where my problems started.


Mac Problems

Yeah I know, serves me right for owning a mac,.. but I did manage to solve the problems without resorting to a windows box. My newer Mac (running OSX 10.10) wouldn't display the login box, so it kept failing authentication. I tried using Firefox instead and updating Java, but looking online it appears it has problems with El Capitan.

When I switched over to my kid's Mac Mini, which was running 10.9, it worked perfectly. I was then able to continue config, renaming the device and setting it to run on port 82 rather than the standard 80. (This I did so that I could set up port forwarding on my router and see it on the web).

Mounting the Camera

My wifi signal is a bit weak in the garage, but mounting it high up on the wall got around any potential problems. The mount itself has a sturdy metal ring base with 3 mounting holes and a gimbal jointed adapter to a standard 1/4" thread. This gives you pretty much unlimited mounting options, but it's not overly robust.

I used a couple of ½ inch screws to fasten it to one of the wooden rafters, so it's out of reach, and unlikely to be tinkered with.


View from the installed camera using night vision mode

Updating the Firmware

Initially I added the App software to my iPhone and iPad and it worked without a problem. The app is pretty straight forward, search for devices and then enter the login details. Simple as that really,.. but it didn't work on my wife's iPhone. (I suspected I need to update the firmware.)

You update the firmware by downloading the image from TrendNet's (slow) website and use the camera's mini website to apply it. After a few failed attempts in Safari I switched to Firefox, but it still didn't complete properly. Instead of automatically restarting after the file uploaded I had to use the reset to factory defaults before it reported the new firmware number. After that it worked properly on my Mac using El Capitan.

Solving that iPhone Problem

I tried my wife's iPhone again, but it still didn't work. It was an identical phone to mine, running the same version of iOS. It had to be something else.

I compared the network settings of both devices and the only difference was one was using a web proxy. I disabled this and the problem went away.

Thursday, 3 December 2015

Copy & Paste Select Options from a Web Page

Introduction

Something that's always bugged me when I've looked after web based systems is struggling to grab a list of options from drop-down boxes on a web page. Sometimes you just want to be able to select them all, press control+v and paste them into an email.

Until now, the only way I found to get the list of options was to view the web page source, locate the <option> tags from the <select> and then manually hack the code about until you are left with a simple list.

... it's not exactly easy.

I've put up with this method for years for the odd quick request, but today I needed to get lists from 42 select boxes, and I didnt want to spend all day doing it.

There had to be a better way!!!

The Solution

I use Firefox for most of my web development and queries, mainly because of firebug, and I found this great add-on called SelectBoxTools which among other things allows you to copy options into a comma separated list.

Right-click from within your select box, and when the menu appears go down to SelectBoxTools > Copy as comma-separated text.

SelectBoxTools options.

Then when pasted into a text editor you'll get the visible name and it's value as a list. eg:-

 PK_ID,Pk Id
 OS,Os
 AVAILABILITY,Availability
 SUPPORT_DATE,Support Date
 RISK_LEVEL,Risk Level
 RATIONALE,Rationale
 NOTES,Notes


If you want to use the SelectBoxTools > Selection option, you'll need to select one of the picklist options on your web form first or you'll get the error:-

  TypeError:this.focused.options[this.focused.selectedIndex] is undefined

 Simple, effective and fast.

Monday, 12 October 2015

Running MAME on a Raspberry Pi


Wocka-wocka....MAME on a Raspberry Pi

Introduction

The other weekend we stopped off for a pub lunch and my boys spotted a coin operated video game table. They'd been given a coin to play after their lunch and the food just couldn't arrive quick enough. But it wasn't that long before they got their first tastes of Pac Man, Galaxian and Centipede.... games of my youth were surprisingly just as amazing to my own kids.

The MAME table we found in the pub.
Right there I hatched an idea to look into running MAME on our Raspberry Pi and maybe even built a gaming table that was something like it. I've seen joystick/control kits on ebay, and read various blogs where impressive cabinets have been built around the diminutive computer, so I figured there'd be plenty of help out there. (I wasn't wrong)


I did some research this weekend; there's a few software projects providing more or less the same thing, with PiPlay (formerly PiMAME) being one of the common choices. I decided to look for something that needed the least amount of effort and I found a project called Raspicade which supports USB controllers or direct wired connections to the GPIO.

Installing Raspicade

Download the card image from the Files page (I chose raspicade-samba-gamepads-keyboard-pikeyd_ready-B-B B-GPIOready-ES-20150513.img.gz), unzip the file and then use DD to write the contents to an empty MicroSD card.

On my Mac the command would be something like..


sudo dd if=raspicade.img of=/dev/disk4

(nb. I shortened the img file name, and ensure you find out what disk number the card is mounted on - use "diskutil list" if you need to find out.)

I did try using diskutility to do this, but it can only handle a single partition. The image has two partitions, one dos and the other linux.

it's a 2.5Gb image, it took a long time!.. but after that it was a case of inserting the flash card into the Pi and booting. After the usual linux boot messages you are asked some questions about sound, input type and which front-end to use. Then you get an amazing looking start-up screen and a simple, easy to interface.

Update - 13/10/15
Before you go much further if you are using a card bigger than 4Gb it's a good idea to resize the linux partition to fill your card. I used gparted to do this. It has a nice easy to use GUI and only takes a few minutes.


My first issue was how do I install ROM files?... even though Samba and ftp should be included, it had failed to correctly set my wireless network up (which isn't surprising). I had to resort to shutting it down and mounting the file on a Linux system (I used my Raspbian install) and copy the ROMs over via USB.


The ROM files are NOT freely available, I guess the content owners (from 30 years ago) haven't released the copyright because there's been a lot of reboot type stuff available on android/ios. So you have no choice but to find a torrent.


I downloaded the MAME 0.37b11 mame4all ROMs and installed them in the following folder..

/home/pi/mame4all-pi/roms/

I also copied the artwork and samples to the following directory:-

/home/pi/mame4all-pi/

Interestingly there's a folder in roms for SCUMMVM too,.. something to look into, as the boys have been known to play Day of The Tentacle from time to time. But I couldn't see an option in the menus in which to load them.

After replacing the card into the Pi we powered on and found all of our old arcade games ready to play. Most seemed to work (the odd one refused to start) but it was apparent that we had no sound. There was no obvious setting (other than the questions on boot-up) so I searched the forums.

Fixing the Sound

Initially I checked to see if the sound was being routed to the 3.5mm jack socket, but nothing there either. There had to be a setting fix because it works fine in Raspbian.

So you can press escape to exit to the console, and the Raspicade forum suggests two things that might fix the sound:-

1) Set the output source on the sound mixer to 2 (HDMI)..
  

sudo amixer cset numid=3 2

2) Then enable HDMI sound..

sudo nano /boot/config.txt

Find and uncomment the line..

#hdmi_drive=2

After I'd saved and then rebooted, the splash screen burst into life with glorious sound, and moments later our selected game responded with glorious 8 bit retro tones.

Big Smiles All Round :o)

Wednesday, 30 September 2015

Background Use of Mobile Network on iPhone

What's with WIFI on the iPhone?

After reviewing my Apache logs to resolve a Davical problem on my wife's phone I discovered something very interesting. I put together a PHP script to enable the caldav messages to be monitored more easily (I based it on my Minecraft log script), and it gave me log messages like this...

86.152.117.242 - rick [29/Sep/2015:00:16:00 +0100] "PROPFIND /davical/caldav.php/private/ HTTP/1.1" 207 2180 "-" "iOS/8.3 (12F70) dataaccessd/1.0"
86.152.117.242 - rick [29/Sep/2015:00:16:01 +0100] "PROPFIND /davical/caldav.php/pingucal%3A8800/.well-known/carddav HTTP/1.1" 403 670 "-" "iOS/8.3 (12F70) dataaccessd/1.0"
86.152.117.242 - rick [29/Sep/2015:00:16:01 +0100] "PROPFIND /davical/caldav.php/pingucal%3A8080/.well-known/carddav HTTP/1.1" 403 670 "-" "iOS/8.3 (12F70) dataaccessd/1.0" 


My phone was in fact connected to my home Wifi with an IP address of 192.168.1.157, but it was clearly using the Mobile network connection to talk to the Davical service because the log showed my phone was sending requests from 86.152.117.242.

It won't fix!!

I tried lots of different things, resetting network settings, disabling various wifi options, and even turning off mobile networking. But when I viewed the Apache log it was still (as if by magic) logging connections from the mobile network IP address.

I was using iOS8.3 and couldn't find any listed problems with it, so I took the plunge and upgraded to iOS9. Twenty minutes and 1 gigabyte download later I was upgraded, but the problem remained. I gave up and went to bed with Mobile networks clearly OFF.

I viewed the logs the next day - every hour the phone accessed the Davical service from 86.152.117.242

86.152.117.242 - rick [30/Sep/2015:06:32:09 +0100] "PROPFIND /davical/caldav.php/pingucal%3A8008/principals/ HTTP/1.1" 403 664 "-" "iOS/9.0.1 (13A404) dataaccessd/1.0"
86.152.117.242 - rick [30/Sep/2015:06:32:09 +0100] "PROPFIND /davical/caldav.php/pingucal%3A8800/principals/ HTTP/1.1" 403 664 "-" "iOS/9.0.1 (13A404) dataaccessd/1.0"
86.152.117.242 - rick [30/Sep/2015:06:32:09 +0100] "PROPFIND /davical/caldav.php/pingucal%3A8080/principals/ HTTP/1.1" 403 664 "-" "iOS/9.0.1 (13A404) dataaccessd/1.0" 


Now that's surprising!!.. just what does that OFF button do then?


It's all about the Cache

While grabbing log entries for this blog tonight I discovered something quite baffling... it now appears to be using Wifi correctly. My phone continued to connect via 4G from work (using another IP address)
86.152.117.242 - rick [30/Sep/2015:14:13:24 +0100] "PROPFIND /davical/caldav.php/pingucal/ HTTP/1.1" 207 2525 "-" "iOS/8.3 (12F69) dataaccessd/1.0"
213.205.199.17 - rick [30/Sep/2015:14:17:03 +0100] "OPTIONS /davical/caldav.php/paulcal/ HTTP/1.1" 200 613 "-" "iOS/9.0.1 (13A404) dataaccessd/1.0" 


Here half way through the afternoon we can also see an iOS/8.3 device, which is my iPad. It doesn't have a cellular networking radio, so it doesn't make sense that it should have that IP. Clearly there's some sort of caching going on with the Apache logging. My phone is showing as the iOS/9.0.1 device.

Once my iPhone connected to the Wifi it started showing correctly..

192.168.1.157 - rick [30/Sep/2015:19:04:32 +0100] "OPTIONS /davical/caldav.php/lehrlecal/ HTTP/1.1" 200 613 "-" "iOS/9.0.1 (13A404) dataaccessd/1.0"
192.168.1.157 - rick [30/Sep/2015:19:04:32 +0100] "PROPFIND /davical/caldav.php/lehrlecal/ HTTP/1.1" 207 2666 "-" "iOS/9.0.1 (13A404) dataaccessd/1.0"


So I can't trust the IP addresses in the Apache2 log. If you reset the Mobile Data statistics you can confirm for yourself that it isn't actually using Mobile data IF it's turned off. My faith in Apple has almost been restored,.. I'm sorry I ever doubted you!
BUT, with Mobile data turned on, the background process is showing it's being used instead of the Wifi connection. There might be a good reason for this, but as is often the case these days, as a consumer I have little or no control over it without turning the phone off or disabling Mobile data every evening.

Now I'm thinking I need to jailbreak my phone again!

Thursday, 20 August 2015

Weaving a Paper Sombrero

Introduction

I'm leaving my usual topic of computers and technology for a change, and doing something crafty. I've made a fair but of stuff out of cardboard in the past, but today I'll be using paper.

We're hosting a Mexican dinner party soon and I thought it'd be fun to try and make a sombrero. 

Obviously the first thing you do is google it, but the search results returned mostly pictures of cakes, so I set about trying to build my own hat from scratch. After a few false starts I decided to use long paper strips and construct it in the style of a woven palm hat.

Making a Start

The boring bit is making a ton of paper strips using wall lining paper. It's relatively stiff paper, course finished and slightly off-white coloured. (Ideal really)

Making paper strips - you'll need lots of these!
  1. Cut off a long strip of lining paper, around 1.5 metres (5ft) long. Fold it along it's length and cut down with a knife to give you two narrower strips.

  2. Repeat this until you end up with strips about 4 cm (1.5 inches) wide, fold these along their length but don't cut them this time. (We're aiming for double thickness 2 cm strips to give the hat better strength.)

  3. Take eight strips and weave them together to give a square starting point, as per the next photo, and fix them with a glue stick. (Glue sticks are quick bonding, not too messy, and can be unpicked if things go wrong.)
Starting weave showing eight strips woven togther.

Weaving the Crown

OK, that was the easy bit, now go and find yourself a tapered pot (I used a flower pot) to use as a former to weave the crown around.
  1. Bend the strips down and start weaving and gluing into a diamond pattern.
Weave down in a diamond formation.
  1. Now it gets a little tricky, add extra strips to fill in the gaps as go, but don't add too many, or your crown will grow too wide. Pull the sides in to each other as you weave.
Add extra strips and weave sides together.
  1. Keep going until your crown is about 20 cm (7-8 inches) high and slightly wider than your head.

Finishing the Crown

Now we'll fix a paper band inside the hat to give it strength and set the width.

  1. Take a strip of paper long enough to go just over twice round your head and glue it together to form a strong oval (head shaped) band.



  1. Once dried, glue this to the inside of the crown with PVA or contact adhesive. (I used the latter because it's fast drying and very strong)

Starting on the Brim

Once dried, turn the hat over and start weaving the brim. Again this can be a little tricky to get started because the strips don't tend to be very evenly spaced. But at least this time you can weave on a flat surface.

Ready to start the brim.
  1. Start with easy diamond shaped weaves again, adding extra strips where missing.
Weaving the brim.
  1. Build it out until you reach your desired width. I went for 18 cm (7 inches) from the base of the crown. (You'll find that even with four layers of paper the brim is very floppy, but don't worry the next stage with fix that.)
Keep adding extra strips as you weave.

Finishing the Brim

Everyone knows that sombreros curve up at the edge, but that's very hard to make. Instead I opted for a simple square edge, and it really transformed the brim from a floppy sheet into a rigid hat.
    Brim edge marked and strips folded up.
  1. Mark the brim's circumference with a pencil, fold up the strips and cut off any excess length. (You may need to unpick some of the weaving if you went too wide.)
  1. Take one of your left over weaving strips, ensure it's long enough to go round the hats circumference, and temporarily join it together to make an edge band. (You'll want to fine tune the length later.)

  2. Weave the strip around the brim's edge using clips until the glue dries. Work your way from the middle of the band to the join.

  3. Glue the join as you finish the last bit of weaving and then repeat from step 12 to add a second band.
Weave edge bands to add rigidity.
  • Your hat should look something like this..
The hat almost finished.
  1. Finally double back the remainder of the strips, trim and glue over the top edge band and you're done.
The completed sombrero.

What Next?

It's a good idea to give the whole thing a coat of diluted PVA glue. (50:50 should do it) This will make the hat a little more durable and prepare it for any pattern you want to add.

I hope you found this useful, and if anyone does have a go at building one, good luck. Oh and please send pix, and I'd love to see the results.

Friday, 7 August 2015

Web Scanning for Devices on your network using Nmap and Perl

Introduction

I wanted a way of keeping tabs on how much the kid's computer was being used during the day while we were out. I can look on the router to see what devices are connected to the network, but this only works while I'm at home.

As I run a web server at my home, I decided to put together a quick webpage that scans for devices and shows the result on a smartphone.




Scanning the Network

After a few web searches I found the best tool to use on my Linux server was nmap, so I installed it and started playing around scanning my network. Results were initially pretty patchy, either the scan took too long (in excess of a minute) or finished quickly but didn't pick everything up.

The results depend on what sort of scan you want to do, it will scan ports and even attempt to detect the device's operating system.

In it's simplest form just enter:-

  nmap 192.168.1.*

Which will scan for range of addresses using a wildcard, or you could just do the following for a smaller range (192.168.1.1 - 192.168.1.20):-

  nmap 192.168.1.1-20

You can perform a fast scan by using the F flag:-

  nmap -F 192.168.1.*

Or include a range of ports (say 80 to 200):-

  nmap -p 80-200 192.168.1.*

Anyway, it does loads of good stuff, and also generated a lot of errors like this..


  RTTVAR has grown to over 2.3 seconds, decreasing to 2.0

I couldn't find much help about this warning message, but it seems to be related to the scan taking longer than expected. Be patient.

Eventually, I settled on the following command which gave a rapid reply and a reasonable degree of accuracy. (The T5 flag sets the timing template so it times-out sooner.)

  nmap -F -T5 192.168.1.*


Building it into a Webpage

My main aim was to show the data in a webpage which could be viewed from anywhere. I chose to use Perl and the CGI library to do this, although I could have used PHP instead. (I'm just more familiar with Perl)

Here's the code I came up with, it also uses the Nmap_Parser library to make handling the nmap data easy.

#!/usr/bin/perl
use strict;
use Nmap::Parser;
use CGI ":all";

my $q = CGI->new();
print $q->header();
print $q->start_html(-title => 'Scan Network',
                     -head  => meta({-name    => 'viewport',
                                     -content => 'width=device-width, initial-scale=1'}));

my %known = ('Unknown-f0-b4-79-19-a8-ff.home'             => "Rick's Laptop",
                         'android-16f4d57ae478ea5.home'   => "Saul's Phone",
                         'Unknown-84-38-35-41-0d-56.home' => "Ruth's Laptop");

print "<h1>Devices on Network</h1>";
print "<FORM><INPUT TYPE='button' onClick='history.go(0)' VALUE='Refresh'></FORM></h1>";

my $np = new Nmap::Parser;
$np->parsescan('/usr/bin/nmap','-F -T5',"192.168.1.*");

for my $host ($np->all_hosts()){
   if ($host->hostname){
       my $hostname = $host->hostname;
       if (exists($known{$hostname})){ $hostname = $known{$hostname}; }     
       print "<p><span style='display:inline-block;width:7em'>".$host->addr."</span>: ".$hostname."</p>";
   }
}
print $q->end_html();

You may also notice I added a way of remapping 'Unknown' devices to something more helpful. It goes without saying that you should add your own devices to the '%known' hash array, if you find them being displayed with unhelpful names.

I also set the meta tag in the html head so that it behaves better on mobile devices.

Performance

In use it takes between 5 and 10 seconds to perform a scan. Sometimes devices aren't detected on the first scan (especially mobile devices), so refresh the page a few times to get a more accurate idea.

It picks up most devices including my Bluray player and Humax PVR, but for some reason it wouldn't detect the Nintendo Wii earlier. (not sure why)

Anyway, hope some of you find it useful.