Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
ObjectServiceFactory serviceFactory = new ObjectServiceFactory();
Service service = serviceFactory.create(FewsPiService.class);
XFireProxyFactory proxyFactory = new XFireProxyFactory();
 //port Number must be equal to the number configured in the Explorer.xml
int portNumber = 8100;
//localhost can be replaced by the ip address of the machine on which the FEWS system is running.
FewsPiService serviceProxy = (FewsPiService) proxyFactory.create(service, "http://localhost:" + portNumber + "/FewsPiService")

Setting up method calls

Here follow some code examples of how to query the PI Service proxy from the client application. The configuration used to make these examples can be found in the attached Example Configuration.

Code Block

   /**
     * Test the retrieval of system time.
     * <p/>
     * With this test the system time of a running Fews Region (on port 8191) can be retrieved. If the
     * system time of the region is changed then the returned value here will match the change.
     */
    public void testSystemTime() {
        String clientId = "not required";
        Date date = serviceProxy.getSystemTime(clientId);


        System.out.println("Current System time : " + dateFormat.format(date));
    }

    /**
     * Return the warm state times from the data base for the configured moduleState id=RSNWELEV_DETO3I
     * in the service configuration file = TestConfig.
     */
    public void testGetWarmStateTimes() {
        //name of service config file
        String clientId = "TestConfig";
        //id of moduleState element
        String moduleId = "RSNWELEV_DETO3I";

        Date[] stateTimes = serviceProxy.getAvailableStateTimes(clientId, moduleId);

        System.out.println("State times for Module " + moduleId);
        for (Date stateTime : stateTimes) {
            System.out.println("time: " + dateFormat.format(stateTime));
        }
    }

    /**
     * Return the cold state ids from the data base for the configured moduleState id=RSNWELEV_DETO3I
     * in the service configuration file = TestConfig.
     */
    public void testGetColdStateIds() {
        //name of service config file
        String clientId = "TestConfig";
        //id of moduleState element
        String moduleId = "RSNWELEV_DETO3I";

        String[] stateIds = serviceProxy.getColdStateIds(clientId, moduleId);

        System.out.println("Cold state ids for model " + moduleId);
        for (String id : stateIds) {
            System.out.println("cold state: " + id);
        }
    }

    /**
     * Return the warm state file from the data base for the configured moduleState id=SNOW17_LSMO3U
     * in the service configuration file = TestConfig. Write file to location d:/temp/<moduleStateId>.zip
     */
    public void testGetModuleStateBinary() throws IOException {
        //name of service config file
        String clientId = "TestConfig";
        //id of moduleState element
        String moduleId = "SNOW17_LSMO3U";

        Date[] stateTimes = serviceProxy.getAvailableStateTimes(clientId, moduleId);
        byte[] binary = serviceProxy.getModuleStateBinary(clientId, moduleId, stateTimes[0], null, -1);

        ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(binary));
        try {
            ZipUtils.unzipFiles(zipInputStream, new File("d:/temp/" + moduleId + ".zip"));
        } finally {
            zipInputStream.close();
        }
    }

    /**
     * Returns the content of a client configuration file from the config directory PiClientConfigFiles.
     *
     * Only requirement is that a configuration file (any type) with the clientId as name and moduleId as extension
     * must exist in this config directory.
     */
    public void testGetClientConfigurationFile(){
        //Name of client file
        String clientId = "MyClientConfigFile";
        //extension of client file
        String moduleId = "txt";

        String clientText = serviceProxy.getClientConfigFile(clientId, moduleId);

        System.out.println("Client file content: " + clientText);

    }

    /**
     * Retrieve the content of the locations xml. Write this to d:/temp/locations.xml
     * @throws IOException
     */
    public void testGetLocations() throws IOException {

        String locations = serviceProxy.getLocations("id not required");

        System.out.println("Content of locations xml file: " + locations);
        FileUtils.writeText("d:/temp/locations.xml", IOUtils.UTF8_CHARSET, locations);

    }

    /**
     * Return the member indices for the ensemble id = "ESP" from the timeseries table.
     *
     * This only works when there are ensemble members in the TimeSeries table.
     */
    public void testGetEnsembleMemberIndices(){

        String ensembleId = "ESP";
        int[] indices = serviceProxy.getEnsembleMemberIndices("id not required", ensembleId);

        System.out.println("Ensemble id " + ensembleId + " contains " + indices.length + " members.");
        System.out.println("indices are: ");
        for (int indice : indices) {
            System.out.println(indice);
        }

    }

    /**
     * Get the logentries for an Import TaskId.
     *
     * Check the LogEntries table to find a matching taskId.
     * 
     * @throws IOException
     */
    public void testGetLogInformation() throws IOException {

        String taskId = "NWSNWMC00:0000026"; //=import
        String logInfo = serviceProxy.getLogMessages("id not required", taskId);

        System.out.println("Content of log: " + logInfo);
        FileUtils.writeText("d:/temp/log.xml", IOUtils.UTF8_CHARSET, logInfo);

    }

    /**
     * Return the timeseries defined in the service configuration file under timeSeries element with id 'Reservoir'.
     *
     * Filter using parameter ids QIN and RQIN and location ids DETO3, GPRO3 and FOSO3
     *
     * Check timeseries table to look for existing times for these timeseries.
     *
     * @throws IOException
     */
    public void testGetTimeSeries() throws IOException {

        Date systemTime = serviceProxy.getSystemTime("id not required");
        Calendar instance = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
        instance.set(2009, 4, 1);
        Date start = instance.getTime();
        instance.set(2009, 4, 11);
        Date end = instance.getTime();
        String[] params = new String[]{"QIN", "RQIN"};
        String[] locs = new String[]{"DETO3","GPRO3","FOSO3"};
        String headers = serviceProxy.getTimeSeriesHeaders("TestConfig", "Reservoir", null, start, systemTime, end, params, locs, null, -1);

        System.out.println("Content of tineseries headers: " + headers);

        String timeseriesfile = serviceProxy.getTimeSeries("TestConfig", "Reservoir", null, start, systemTime, end, params, locs, null, -1);

        FileUtils.writeText("d:/temp/timeseries.xml", IOUtils.UTF8_CHARSET, timeseriesfile);

    }

    /**
     * Upload a pi_diag log file to the region.
     *
     * Make sure to create an input file in corresponding directory.
     *
     * @throws IOException
     */
    public void testInsertLogMessages() throws IOException {

        String logText = FileUtils.readText("d:/temp/inputlog.xml");
        serviceProxy.putLogMessage("used as description", logText);
    }

    /**
     * Upload a pi_timeseries file to the Region. The timeseries in this file must be linked to timeseries configured
     * in the PiServiceConfig file.
     *
     * Make sure to create an input file in the corresponding directory.
     *
     * @throws IOException
     */
    public void testInsertTimeSeries() throws IOException {

        String timeseriesText = FileUtils.readText("d:/temp/inputtimeseries.xml");
        System.out.println("Input timeseries: " + timeseriesText);
        serviceProxy.putTimeSeries("TestConfig", null, "Reservoir", timeseriesText, null, -1);
    }

    /**
     * Schedule a task run and wait for it to finish.
     * 
     */
    public void testRunningTask(){
        String taskId = serviceProxy.createTask("id not required");
        Date date = serviceProxy.getSystemTime("");
        System.out.println("Starting task with id " + taskId + " time=" + new Date(System.currentTimeMillis()));
        String taskRunId = serviceProxy.runTask("id not required", taskId, "Santiam_Forecast", date, date, date, null, null, "Test user", "PiWebservice taskrun");

        boolean result = serviceProxy.waitForTask("id not required", taskRunId, 120000);
        System.out.println("Task with id " + taskId + (result ? " finished successfully" : " failed"));
        System.out.println("time=" + new Date(System.currentTimeMillis()));
    }

Appendix

FewsPiService API

...