OPC UA HistoryRead(Java版)

/**
 * Synchronous HistoryRead service request.
 * Based on opc-ua-java-stack-1.02-source-code-and-sample-applications-337.2-stable
 */
private static void OpcUaHistoryRead() throws UnknownHostException, ServiceResultException, ParseException, InterruptedException{
	UATcpParam param=new UATcpParam();
	param.setUrl(url);
	param.setAuthentication(0);
	//param.setUserName("admin");
	//param.setPassword("admin");
	param.setSecurityMode(MessageSecurityMode.None);
	//param.setSecurityPolicie(SecurityPolicy.BASIC128RSA15);
	String nodeIdStr1="ns=5;s=Random1";
	NodeId nodeId = NodeId.get(OpcTcpClientUtil.getIdType(nodeIdStr1),OpcTcpClientUtil.getNs(nodeIdStr1), OpcTcpClientUtil.getId(nodeIdStr1));
	ReadRawModifiedDetails rrmd = new ReadRawModifiedDetails();
	rrmd.setIsReadModified(false);
	DateTime.setStrFormat("yyyy-MM-dd hh:mm:ssZ");
	DateTime StartTime = DateTime.parseDateTime("2018-12-05 13:56:19");//GMT
	DateTime EndTime = DateTime.parseDateTime("2018-12-05 14:01:19");
	rrmd.setStartTime(StartTime);
	rrmd.setEndTime(EndTime);
	rrmd.setNumValuesPerNode(UnsignedInteger.getFromBits(1000));
	rrmd.setReturnBounds(true);
	HistoryReadValueId[] NodesToReads = new HistoryReadValueId[1];
	HistoryReadValueId hrv = new HistoryReadValueId();
	hrv.setNodeId(nodeId);
	NumericRange parsedIndexRange = new NumericRange(-1);
	hrv.setParsedIndexRange(parsedIndexRange);
	NodesToReads[0] = hrv;
	ExtensionObject eo = ExtensionObject.encode(rrmd, QualifiedName.DEFAULT_BINARY_ENCODING, 
			StackUtils.getDefaultSerializer(), EncoderContext.getDefaultInstance());
	SessionChannel mySession = OpcTcpClientUtil.getSession(param);
	RequestHeader rh = new RequestHeader();
	rh.setRequestHandle(UnsignedInteger.getFromBits(754));
	rh.setTimeoutHint(UnsignedInteger.getFromBits(30000));
	rh.setReturnDiagnostics(UnsignedInteger.getFromBits(0));
	HistoryReadResponse resp = mySession.HistoryRead(rh, eo, TimestampsToReturn.Source, false, NodesToReads);
	HistoryReadResult[] hrr = resp.getResults();
	for (HistoryReadResult historyReadResult : hrr) {
		System.out.println(historyReadResult);
	}
}

你可能感兴趣的:(OPC UA HistoryRead(Java版))