los.adrian
Posts: 2
Joined: Tue Sep 22, 2015 8:24 am

Call web service in handleGpioPinDigitalStateChangeEvent?

Tue Sep 22, 2015 8:37 am

Dear Forumers!

I'd like to call a webservice when my PIR sensor is active.
I created the web service call with wsimport:
wsimport -keep -s src http://webservicex.net/geoipservice.asmx?WSDL

I wrote a test application. In this code call the service on the start, and call again when PIR sensor gettig active.
The first calling is right, but when i try to call on the handleGpioPinDigitalStateChangeEvent method of GpioPinListenerDigital object i've got a NullPointerException in pi4j-gpio-event-executor-0.

Code: Select all

package me.losadrian.Pirel;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;

import net.webservicex.GeoIP;
import net.webservicex.GeoIPService;
import net.webservicex.GeoIPServiceSoap;

public class App {
    public static void main(String[] args) {

        GeoIPService service = new GeoIPService();
        GeoIPServiceSoap client = service.getGeoIPServiceSoap();
        GeoIP geoIp = client.getGeoIP("74.125.239.142");
        System.out.println(geoIp.getCountryName());

        final GpioController gpioPIR = GpioFactory.getInstance();
        final GpioPinDigitalInput sensorPir = gpioPIR.provisionDigitalInputPin(RaspiPin.GPIO_07, PinPullResistance.PULL_DOWN);

        final GpioController gpioREL1 = GpioFactory.getInstance();           
        final GpioPinDigitalOutput rel1 = gpioREL1.provisionDigitalOutputPin(RaspiPin.GPIO_08, "REL", PinState.HIGH);         
        final GpioController gpioREL2 = GpioFactory.getInstance();           
        final GpioPinDigitalOutput rel2 = gpioREL2.provisionDigitalOutputPin(RaspiPin.GPIO_09, "REL", PinState.HIGH);         

        rel1.high();
        rel2.high();

        sensorPir.addListener(new GpioPinListenerDigital() {

            public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
                if (event.getState().isHigh()) {
                    System.out.println("Motion!!");
                    GeoIPService service = new GeoIPService();
                    GeoIPServiceSoap client = service.getGeoIPServiceSoap();
                    GeoIP geoIp = client.getGeoIP("74.125.239.142");
                    System.out.println(geoIp.getCountryName());
                    rel1.low();
                    rel2.high();
                }
                if (event.getState().isLow()) {
                    System.out.println("Quiet...");
                    rel1.high();
                    rel2.low();
                }
            }
        });

        try {
            for (;;) {
                Thread.sleep(3000);
            }
        } catch (final Exception e) {
            System.out.println(e.getMessage());
        }

    }
}
The console output is:

Code: Select all

pi@raspberrypi ~ $ sudo java -jar Pirel.jar 
United States
Motion!!
Exception in thread "pi4j-gpio-event-executor-0" java.lang.NullPointerException
	at com.sun.xml.internal.ws.client.ClientContainer$1.getResource(ClientContainer.java:45)
	at com.sun.xml.internal.ws.assembler.MetroConfigLoader.locateResource(MetroConfigLoader.java:220)
	at com.sun.xml.internal.ws.assembler.MetroConfigLoader.locateResource(MetroConfigLoader.java:230)
	at com.sun.xml.internal.ws.assembler.MetroConfigLoader.init(MetroConfigLoader.java:125)
	at com.sun.xml.internal.ws.assembler.MetroConfigLoader.<init>(MetroConfigLoader.java:104)
	at com.sun.xml.internal.ws.assembler.TubelineAssemblyController.getTubeCreators(TubelineAssemblyController.java:78)
	at com.sun.xml.internal.ws.assembler.MetroTubelineAssembler.createClient(MetroTubelineAssembler.java:103)
	at com.sun.xml.internal.ws.client.Stub.createPipeline(Stub.java:328)
	at com.sun.xml.internal.ws.client.Stub.<init>(Stub.java:295)
	at com.sun.xml.internal.ws.client.Stub.<init>(Stub.java:228)
	at com.sun.xml.internal.ws.client.Stub.<init>(Stub.java:243)
	at com.sun.xml.internal.ws.client.sei.SEIStub.<init>(SEIStub.java:84)
	at com.sun.xml.internal.ws.client.WSServiceDelegate.getStubHandler(WSServiceDelegate.java:814)
	at com.sun.xml.internal.ws.client.WSServiceDelegate.createEndpointIFBaseProxy(WSServiceDelegate.java:803)
	at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:436)
	at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:404)
	at com.sun.xml.internal.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:386)
	at javax.xml.ws.Service.getPort(Service.java:119)
	at net.webservicex.GeoIPService.getGeoIPServiceSoap(GeoIPService.java:72)
	at me.losadrian.Pirel.App$1.handleGpioPinDigitalStateChangeEvent(App.java:42)
	at com.pi4j.io.gpio.tasks.impl.GpioEventDispatchTaskImpl.run(GpioEventDispatchTaskImpl.java:66)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:744)
Quiet...
Does someone have any idea how to change this code?

Thank you in advance for any help you can provide.

Adrian
Last edited by los.adrian on Wed Sep 23, 2015 7:26 am, edited 1 time in total.

User avatar
xranby
Posts: 540
Joined: Sat Mar 03, 2012 10:02 pm
Contact: Website

Re: Call web service in handleGpioPinDigitalStateChangeEvent

Tue Sep 22, 2015 3:33 pm

It looks like the sequence:

Code: Select all

GeoIPService service = new GeoIPService();
service.getGeoIPServiceSoap();
fail if you run it twice inside the same application.

Does the code work once if you only do this once inside the event handler?
Xerxes Rånby @xranby I once had two, then I gave one away. Now both are in use every day!
twitter.com/xranby

los.adrian
Posts: 2
Joined: Tue Sep 22, 2015 8:24 am

Re: Call web service in handleGpioPinDigitalStateChangeEvent

Wed Sep 23, 2015 7:47 am

Dear Xerxes!

Thanks your reply.
The code not works if that was only in the handler but i've found some solution.
- The service call works if I create static GeoIPService and GeoIPServiceSoap class variable.
- The service call works if I create final GeoIPServiceSoap local variable before the handler in the main.

Code: Select all

package me.losadrian.Pirel;
 
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalInput;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinPullResistance;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.io.gpio.event.GpioPinDigitalStateChangeEvent;
import com.pi4j.io.gpio.event.GpioPinListenerDigital;
 
import net.webservicex.GeoIP;
import net.webservicex.GeoIPService;
import net.webservicex.GeoIPServiceSoap;
 
public class App {
// Works here
//  static GeoIPService service = new GeoIPService();
//  static GeoIPServiceSoap client = service.getGeoIPServiceSoap();
     
    public static void main(String[] args) {
        // Works here too
        GeoIPService service = new GeoIPService();
        final GeoIPServiceSoap client = service.getGeoIPServiceSoap();
         
        final GpioController gpioPIR = GpioFactory.getInstance();
        final GpioPinDigitalInput sensorPir = gpioPIR.provisionDigitalInputPin(RaspiPin.GPIO_07, PinPullResistance.PULL_DOWN);
 
        final GpioController gpioREL1 = GpioFactory.getInstance();           
        final GpioPinDigitalOutput rel1 = gpioREL1.provisionDigitalOutputPin(RaspiPin.GPIO_08, "REL", PinState.HIGH);         
        final GpioController gpioREL2 = GpioFactory.getInstance();           
        final GpioPinDigitalOutput rel2 = gpioREL2.provisionDigitalOutputPin(RaspiPin.GPIO_09, "REL", PinState.HIGH);         
 
        rel1.high();
        rel2.high();
         
        sensorPir.addListener(new GpioPinListenerDigital() {
 
            public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
                if (event.getState().isHigh()) {
                    System.out.println("Motion!!");
// Not works here
//                  GeoIPService service = new GeoIPService();
//                  GeoIPServiceSoap client = service.getGeoIPServiceSoap();
                    GeoIP geoIp = client.getGeoIP("212.58.246.92");
                    System.out.println(geoIp.getCountryName());
                    rel1.low();
                    rel2.high();
                }
                if (event.getState().isLow()) {
                    System.out.println("Quiet...");
                    rel1.high();
                    rel2.low();
                }
            }
        });
 
        try {
            for (;;) {
                Thread.sleep(3000);
            }
        } catch (final Exception e) {
            System.out.println(e.getMessage());
        }
         
    }
}
Adrian

Return to “Java”