Skip to content

Commit

Permalink
Add ONVIF focus lens, iris attenuation, and wiper support (#100)
Browse files Browse the repository at this point in the history
* Added focus functionality
Removed lib directory
Updated copyrights

* Added ONVIF focus functionality
Removed ONVIF lib directory
Updated copyrights for ONVIF classes

* Added multi-step ONVIF operations - wiper oneshot, iris increment
Removed dead code

* Removed Cohu-specific ONVIF tokens, now fetches automatically
All ONVIF operations now use the callback
Added device reset functionality

* Removed ONVIF authenticate flag (will always authenticate if there's a username)

* Get ONVIF entry points programmatically

* Fixed ONVIF document namespace awareness
  • Loading branch information
ethan-beauc authored Jan 4, 2024
1 parent d3e2086 commit 4e0d481
Show file tree
Hide file tree
Showing 10 changed files with 462 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package us.mn.state.dot.tms.server.comm.onvifptz.lib;
package us.mn.state.dot.tms.server.comm.onvifptz;

import java.io.*;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

import org.w3c.dom.Node;
import org.w3c.dom.Document;

import org.xml.sax.InputSource;

/**
* Class for XML document utilities
Expand All @@ -46,4 +52,22 @@ public static String getString(Node n) {
}
return null;
}

/** Gets Document from string */
public static Document getDocument(String s) {
Document doc = null;
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(s));
doc = db.parse(is);
} catch (Exception e) {
e.printStackTrace();
return null;
}

return doc;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package us.mn.state.dot.tms.server.comm.onvifptz.lib;
package us.mn.state.dot.tms.server.comm.onvifptz;

import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.NodeList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

Expand All @@ -29,7 +30,6 @@ public DeviceService(String deviceServiceAddress, String u, String p) {
namespace = "http://www.onvif.org/ver10/device/wsdl";
username = u;
password = p;
authenticate = false;
}

public static DeviceService getDeviceService(String deviceServiceAddress, String u, String p) {
Expand Down Expand Up @@ -57,6 +57,39 @@ public String getServices() {
return sendRequestDocument(doc);
}

/** Gets a service address by its namespace */
public String getServiceAddr(String namespace) {
String servicesRes = getServices();
Document servicesDoc = DOMUtils.getDocument(servicesRes);

NodeList services = servicesDoc.getElementsByTagNameNS("*", "Service");
for (int i = 0; i < services.getLength(); i++) {
Element service = (Element) services.item(i);
Element ns = (Element) service.getElementsByTagNameNS("*", "Namespace").item(0);

if (ns.getTextContent().equalsIgnoreCase(namespace)) {
Element addr = (Element) service.getElementsByTagNameNS("*", "XAddr").item(0);
return addr.getTextContent();
}
}
return null;
}

/** Get the PTZ binding address */
public String getPTZBinding() {
return getServiceAddr("http://www.onvif.org/ver20/ptz/wsdl");
}

/** Get the media binding address */
public String getMediaBinding() {
return getServiceAddr("http://www.onvif.org/ver10/media/wsdl");
}

/** Get the imaging binding address */
public String getImagingBinding() {
return getServiceAddr("http://www.onvif.org/ver20/imaging/wsdl");
}

/** Document builder function for GetScopes */
public Document getScopesDocument() {
Document doc = getBaseDocument();
Expand All @@ -70,7 +103,6 @@ public Document getScopesDocument() {

/** Get the scope parameters of the device */
public String getScopes() {
authenticate = true;
Document doc = getScopesDocument();
return sendRequestDocument(doc);
}
Expand Down Expand Up @@ -106,4 +138,20 @@ public Document getAuxiliaryCommandDocument(String command, String state) {

return doc;
}

public Document getSystemRebootDocument() {
Document doc = getBaseDocument();
Element body = (Element) doc.getElementsByTagName("SOAP-ENV:Body").item(0);

Element systemRebootElem = doc.createElement("wsdl:SystemReboot");
body.appendChild(systemRebootElem);

return doc;
}

/** Reboots the device */
public String systemReboot() {
Document doc = getSystemRebootDocument();
return sendRequestDocument(doc);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package us.mn.state.dot.tms.server.comm.onvifptz.lib;
package us.mn.state.dot.tms.server.comm.onvifptz;

import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
Expand All @@ -29,7 +29,6 @@ public ImagingService(String imagingServiceAddress, String u, String p) {
namespace = "http://www.onvif.org/ver20/imaging/wsdl";
username = u;
password = p;
authenticate = true;
}

public static ImagingService getImagingService(String imagingServiceAddress, String u, String p) {
Expand Down Expand Up @@ -176,6 +175,33 @@ public String setIris(String vToken, String value) {
return sendRequestDocument(doc);
}

/**
* Gets the iris attenuation
*
* @param vToken reference token to the relevant video source
*
* @return float value of iris
*/
public float getIris(String vToken) {
String docString = getImagingSettings(vToken);
Document doc = DOMUtils.getDocument(docString);
if (doc == null) return 0;

Element iris = (Element) doc.getElementsByTagName("tt:Iris").item(0);
return Float.parseFloat(iris.getTextContent());
}

/**
* Increments the iris attenuation by retrieving and setting it (absolute)
*
* @param vToken reference token to the relevant video source
* @param value the requested iris attenuation; "auto", "manual", or a float
*/
public String incrementIris(String vToken, String value) {
float newIris = getIris(vToken) + Float.parseFloat(value);
return setIris(vToken, String.valueOf(newIris));
}

/** Document builder function for GetMoveOptions */
public Document getMoveOptionsDocument(String vToken) {
Document doc = getBaseDocument();
Expand All @@ -201,11 +227,6 @@ public String getMoveOptions(String vToken) {
return sendRequestDocument(doc);
}

/** Document builder function for Move request; uses default mode (relative). */
public Document getMoveDocument(String vToken, float distance) {
return getMoveDocument(vToken, distance, "");
}

/** Document builder function for Move request; takes move mode as a parameter. */
public Document getMoveDocument(String vToken, float distance, String mode) {
Document doc = getBaseDocument();
Expand Down Expand Up @@ -257,12 +278,23 @@ public Document getMoveDocument(String vToken, float distance, String mode) {
*
* @param vToken reference token to the relevant video source
* @param distance the requested move distance
* @param mode mode to send to device ("continuous", "absolute", "relative")
*/
public String moveFocus(String vToken, float distance) {
Document doc = getMoveDocument(vToken, distance);
public String moveFocus(String vToken, float distance, String mode) {
Document doc = getMoveDocument(vToken, distance, mode);
return sendRequestDocument(doc);
}

/**
* Moves the focus lens
*
* @param vToken reference token to the relevant video source
* @param distance the requested move distance
*/
public String moveFocus(String vToken, float distance) {
return moveFocus(vToken, distance, "continuous");
}

/** Document builder function for GetStatus */
public Document getStatusDocument(String vToken) {
Document doc = getBaseDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
package us.mn.state.dot.tms.server.comm.onvifptz.lib;
package us.mn.state.dot.tms.server.comm.onvifptz;

import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
Expand All @@ -29,7 +29,6 @@ public MediaService(String mediaServiceAddress, String u, String p) {
namespace = "http://www.onvif.org/ver10/media/wsdl";
username = u;
password = p;
authenticate = true;
}

public static MediaService getMediaService(String mediaServiceAddress, String u, String p) {
Expand Down
Loading

0 comments on commit 4e0d481

Please sign in to comment.