Example #1
0
  /**
   * Create XML content for scan data
   *
   * @param doc XML {@link Document}
   * @param data {@link ScanData}
   * @return XML {@link Element} for the scan data
   */
  public static Node createXMLElement(final Document doc, final ScanData data) {
    final Element result = doc.createElement("data");

    for (String device_name : data.getDevices()) {
      final Element device = doc.createElement("device");
      device.appendChild(createXMLElement(doc, "name", device_name));

      final Element samples = doc.createElement("samples");
      for (ScanSample data_sample : data.getSamples(device_name)) {
        final Element sample = doc.createElement("sample");
        sample.setAttribute("id", Long.toString(data_sample.getSerial()));
        sample.appendChild(createXMLElement(doc, "time", data_sample.getTimestamp()));
        sample.appendChild(
            createXMLElement(doc, "value", ScanSampleFormatter.asString(data_sample)));
        samples.appendChild(sample);
      }

      device.appendChild(samples);
      result.appendChild(device);
    }
    return result;
  }
  /** This 'Runnable' is executed by a demo client thread */
  @Override
  public void run() {
    System.out.println("--- Client starts ---");
    try {
      // Pre-set xpos
      final Device pv = new PVDevice(new DeviceInfo("motor_x", "xpos"));
      pv.start();
      new WaitForDevicesCondition(pv).await();
      pv.write(0.0);

      // Connect to scan server
      final ScanServerInfo server_info = server.getInfo();
      System.out.println("Thread using " + server_info);

      // Submit two scans, holding on to the second one
      final CommandSequence commands = createCommands();
      long id = server.submitScan("My Test 1", commands.getXML(), true);
      // Poll 1st scan until it finishes
      while (true) {
        final ScanInfo info = server.getScanInfo(id);
        System.out.println("Poll: " + info + " @ " + info.getCurrentCommand());
        if (info.getState() == ScanState.Finished) break;
        Thread.sleep(100);
      }

      // Submit second scan
      id = server.submitScan("My Test 2", commands.getXML(), true);

      System.out.println("All Scans on server:");
      List<ScanInfo> infos = server.getScanInfos();
      for (ScanInfo info : infos) System.out.println(info);

      // Poll 2nd scan until it finishes
      while (true) {
        final ScanInfo info = server.getScanInfo(id);
        System.out.println("Poll: " + info);
        if (info.getState() == ScanState.Finished) break;
        Thread.sleep(100);
      }

      // Show finished scans
      System.out.println("All Scans on server:");
      infos = server.getScanInfos();
      for (ScanInfo info : infos) System.out.println(info);

      // Check if scan that just finished shows up as such
      assertEquals(ScanState.Finished, server.getScanInfo(id).getState());

      // Also wait for scan to end by monitoring xpos (not really useful)
      System.out.println("Client waiting for PV to reach final value...");
      new NumericValueCondition(pv, Comparison.EQUALS, 5.0, 0.1, null).await();

      // Submit scan again, and pause it early on
      id = server.submitScan("My Test 3", commands.getXML(), true);
      // Wait for thread to start
      while (true) {
        final ScanInfo info = server.getScanInfo(id);
        System.out.println("Started? " + info);
        if (info.getState() != ScanState.Idle) break;
        Thread.sleep(100);
      }
      // Pause it
      server.pause(id);
      System.out.println("All Scans on server:");
      infos = server.getScanInfos();

      // Only that one scan should be paused
      for (ScanInfo info : infos) {
        System.out.println(info);
        if (info.getId() == id) assertEquals(ScanState.Paused, info.getState());
        else assertTrue(ScanState.Paused != info.getState());
      }

      // Resume 'all' and pause 'all' should again pause the running scan
      server.resume(-1);
      server.pause(-1);
      System.out.println("All Scans on server:");
      infos = server.getScanInfos();
      // Only that one scan should be paused
      for (ScanInfo info : infos) {
        System.out.println(info);
        if (info.getId() == id) assertEquals(ScanState.Paused, info.getState());
        else assertTrue(ScanState.Paused != info.getState());
      }

      // Should stay paused
      for (int i = 0; i < 3; ++i) {
        final ScanInfo info = server.getScanInfo(id);
        System.out.println("Poll: " + info);
        assertEquals(ScanState.Paused, info.getState());
        Thread.sleep(1000);
      }
      System.out.println("Resume, wait to finish");
      server.resume(id);
      // Poll scan until it finishes
      while (true) {
        final ScanInfo info = server.getScanInfo(id);
        System.out.println("Poll: " + info);
        if (info.getState() == ScanState.Finished) break;
        Thread.sleep(100);
      }

      // Fetch data
      System.out.println("Logged data:");
      final ScanData data = server.getScanData(id);
      final List<String> devices = Arrays.asList(data.getDevices());
      System.out.println("Devices: " + devices);
      assertTrue(devices.contains("xpos"));
      assertTrue(devices.contains("ypos"));
      assertTrue(devices.contains("readback"));
      new ScanDataIterator(data).printTable(System.out);

      pv.stop();
    } catch (Exception ex) {
      client_error = ex;
    }
    System.out.println("--- Client ends ---");
  }