55import java .io .File ;
66import java .io .IOException ;
77import java .io .Writer ;
8- import java .nio .file .Files ;
98import java .nio .file .Path ;
109import java .nio .file .Paths ;
1110import java .security .cert .X509Certificate ;
2827import org .eclipse .milo .opcua .sdk .client .nodes .UaNode ;
2928import org .eclipse .milo .opcua .sdk .client .nodes .UaVariableNode ;
3029import org .eclipse .milo .opcua .sdk .client .nodes .UaVariableTypeNode ;
31- import org .eclipse .milo .opcua .stack .core .Identifiers ;
3230import org .eclipse .milo .opcua .stack .core .NodeIds ;
3331import org .eclipse .milo .opcua .stack .core .OpcUaDataType ;
3432import org .eclipse .milo .opcua .stack .core .UaException ;
35- import org .eclipse .milo .opcua .stack .core .security .FileBasedTrustListManager ;
3633import org .eclipse .milo .opcua .stack .core .security .SecurityPolicy ;
37- import org .eclipse .milo .opcua .stack .core .security . TrustListManager ;
34+ import org .eclipse .milo .opcua .stack .core .types . builtin . ByteString ;
3835import org .eclipse .milo .opcua .stack .core .types .builtin .DataValue ;
36+ import org .eclipse .milo .opcua .stack .core .types .builtin .DateTime ;
3937import org .eclipse .milo .opcua .stack .core .types .builtin .LocalizedText ;
4038import org .eclipse .milo .opcua .stack .core .types .builtin .NodeId ;
4139import org .eclipse .milo .opcua .stack .core .types .builtin .StatusCode ;
4240import org .eclipse .milo .opcua .stack .core .types .builtin .Variant ;
41+ import org .eclipse .milo .opcua .stack .core .types .builtin .unsigned .UByte ;
4342import org .eclipse .milo .opcua .stack .core .types .builtin .unsigned .UInteger ;
43+ import org .eclipse .milo .opcua .stack .core .types .builtin .unsigned .ULong ;
4444import org .eclipse .milo .opcua .stack .core .types .builtin .unsigned .UShort ;
4545import org .eclipse .milo .opcua .stack .core .types .enumerated .MessageSecurityMode ;
4646import org .eclipse .milo .opcua .stack .core .types .enumerated .TimestampsToReturn ;
4747import org .eclipse .milo .opcua .stack .core .types .structured .ApplicationDescription ;
4848import org .eclipse .milo .opcua .stack .core .types .structured .EndpointDescription ;
49- import org .eclipse .milo .opcua .stack .core .types .structured .WriteValue ;
5049import org .iottree .core .Config ;
5150import org .iottree .core .UACh ;
5251import org .iottree .core .UANode ;
@@ -804,7 +803,7 @@ public void writeBindBeSelectTreeSub(Writer w, String pnode_id) throws Exception
804803 {
805804 NodeId pnid = null ;
806805 if (Convert .isNullOrEmpty (pnode_id ))
807- pnid = Identifiers .RootFolder ;
806+ pnid = NodeIds .RootFolder ;
808807 else
809808 pnid = NodeId .parse (pnode_id );
810809 if (pnid == null )
@@ -963,7 +962,7 @@ public void writeUaNodeTreeJson(Writer w, boolean b_var, boolean force_refresh)
963962
964963 public static UaNode findUaNodeByPath (OpcUaClient client , String [] path ) throws UaException
965964 {
966- NodeId nid = Identifiers .RootFolder ;
965+ NodeId nid = NodeIds .RootFolder ;
967966
968967 UaNode pn = client .getAddressSpace ().getNode (nid );
969968 if (pn == null )
@@ -995,7 +994,7 @@ public static void writeUaNodeTreeJson(Writer w, OpcUaClient client, String node
995994 {
996995 NodeId nid = null ;
997996 if (Convert .isNullOrEmpty (nodeid ))
998- nid = Identifiers .RootFolder ;
997+ nid = NodeIds .RootFolder ;
999998 else
1000999 nid = NodeId .parse (nodeid );
10011000 if (nid == null )
@@ -1019,7 +1018,7 @@ public static void writeUaNodeTreeJson(Writer w, OpcUaClient client, UaNode n, b
10191018
10201019 boolean bvar = n instanceof UaVariableNode ;
10211020
1022- System .out .println (nid .toParseableString ()) ;
1021+ // System.out.println(nid.toParseableString()) ;
10231022 w .write ("{\" id\" :\" " + nid .toParseableString () + "\" " );
10241023 w .write (",\" nc\" :" + n .getNodeClass ().getValue ());
10251024 if (bvar )
@@ -1381,8 +1380,7 @@ private void readDataInLoop()
13811380 UShort us = nodeid .getNamespaceIndex ();
13821381 Object id = nodeid .getIdentifier ();
13831382 //DataValue v = uaClient.readValue(maxAge, timestampsToReturn, nodeId)
1384- DataValue v = uaClient .readValue (0.0 , TimestampsToReturn .Both , nodeid );//.get();
1385-
1383+ DataValue v = uaClient .readValue (0.0 , TimestampsToReturn .Both , nodeid );//.get()
13861384 updateTagVal (ch , tag2n .getKey (), v );
13871385 }
13881386 }
@@ -1395,6 +1393,8 @@ private void readDataInLoop()
13951393 lastReadData = System .currentTimeMillis ();
13961394 }
13971395 }
1396+
1397+ private transient HashMap <String ,DataValue > lastTagP2GoodDV = new HashMap <>() ;
13981398
13991399 private void updateTagVal (UACh ch , String tagpath , DataValue v ) throws Exception
14001400 {
@@ -1415,22 +1415,22 @@ private void updateTagVal(UACh ch, String tagpath, DataValue v) throws Exception
14151415 // itemval.getValue().
14161416 if (sc .isGood ())
14171417 {
1418+ lastTagP2GoodDV .put (tagpath ,v ) ;
14181419 long chgdt = v .getServerTime ().getJavaTime ();
14191420 // itemval.
14201421 // UAVal uav =
14211422 // UAVal.createByStrVal(tag.getValTp(),itemval.getLastValueStr(),chgdt,chgdt);
14221423 // tag.RT_setUAVal(uav);
1423- // if(objv instanceof DateTime)
1424- // {
1425- //
1426- // }
1424+ if (objv instanceof DateTime )
1425+ {
1426+
1427+ }
14271428 String strv = objv .toString () ;
14281429 OpcUaDataType dtp = vvt .getDataType ().get () ;
14291430 if ("DateTime" .equals (dtp .name ()))
14301431 {
1431- return ;
1432- //DateTime dt = (DateTime) vvt.getValue();
1433- //dt.getJavaDate() ;
1432+ DateTime dt = (DateTime ) vvt .getValue ();
1433+ strv = Convert .toFullYMDHMS (dt .getJavaDate ()) ;
14341434 }
14351435
14361436 tag .RT_setValRawStr (strv , true , chgdt );
@@ -1495,6 +1495,46 @@ synchronized void disconnect() // throws IOException
14951495 uaClient = null ;
14961496 }
14971497 }
1498+
1499+ private static Object transStr2Obj (OpcUaDataType dt ,String strv )
1500+ {
1501+ switch (dt )
1502+ {
1503+ case Boolean ://(1, Boolean.class),
1504+ return "true" .equalsIgnoreCase (strv ) || "1" .equals (strv ) ;
1505+ case SByte ://(2, Byte.class),
1506+ return Byte .parseByte (strv ) ;
1507+ case Byte ://(3, UByte.class),
1508+ return UByte .valueOf (strv ) ;
1509+ case Int16 ://(4, Short.class),
1510+ return Short .parseShort (strv ) ;
1511+ case UInt16 ://(5, UShort.class),
1512+ return UShort .valueOf (strv ) ;
1513+ case Int32 ://(6, Integer.class),
1514+ return Integer .parseInt (strv ) ;
1515+ case UInt32 ://(7, UInteger.class),
1516+ return UInteger .valueOf (strv ) ;
1517+ case Int64 ://(8, Long.class),
1518+ return Long .parseLong (strv ) ;
1519+ case UInt64 ://(9, ULong.class),
1520+ return ULong .valueOf (strv ) ;
1521+ case Float ://(10, Float.class),
1522+ return Float .parseFloat (strv ) ;
1523+ case Double ://(11, Double.class),
1524+ return Double .parseDouble (strv ) ;
1525+ case String ://(12, String.class),
1526+ return strv ;
1527+ case DateTime ://(13, ),
1528+ return new DateTime (Convert .toCalendar (strv ).toInstant ()) ;
1529+ case Guid ://(14, UUID.class),
1530+ return UUID .fromString (strv ) ;
1531+ case ByteString ://(15, ByteString.class),
1532+ byte [] bs = Convert .hexStr2ByteArray (strv ) ;
1533+ return ByteString .of (bs ) ;
1534+ default :
1535+ return null ;
1536+ }
1537+ }
14981538
14991539 public void RT_writeValByBind (String tagpath , String strv )
15001540 {
@@ -1506,16 +1546,35 @@ public void RT_writeValByBind(String tagpath, String strv)
15061546 NodeId nid = tag2nodeid .get (tagpath );
15071547 if (nid == null )
15081548 return ;
1509-
1510- Variant v = new Variant (Integer .parseInt (strv ));
1511- DataValue dataValue = new DataValue (v , null , null );
1512- WriteValue wv = new WriteValue (nid ,null ,"" ,dataValue );
1549+ DataValue lastdv = lastTagP2GoodDV .get (tagpath ) ;
1550+ if (lastdv ==null )
1551+ return ;
1552+
1553+ OpcUaDataType dt = lastdv .getValue ().getDataType ().get () ;
1554+ if (dt ==null )
1555+ return ;
1556+ Object objv = transStr2Obj (dt ,strv ) ;
1557+ if (objv ==null )
1558+ return ;
1559+
1560+ Variant v = new Variant (objv );
1561+ DataValue dataValue = new DataValue (v ,StatusCode .GOOD ,null ); //timestamp may not allowed
1562+ //WriteValue wv = new WriteValue(nid,null,"",dataValue);
15131563 try
15141564 {
15151565 //StatusCode statusCode = uaClient.write(Arrays.asList(wv)).getResults()[0] ;
1516- StatusCode statusCode = uaClient .writeValues (Arrays .asList (nid ), Arrays .asList (dataValue )).get (0 );
1517- boolean r = statusCode .isGood ();
1518- System .out .println ("w result=" + r );
1566+ List <StatusCode > scs = uaClient .writeValues (Arrays .asList (nid ), Arrays .asList (dataValue )) ;
1567+ if (scs !=null && scs .size ()>0 )
1568+ {
1569+ StatusCode statusCode = scs .get (0 );
1570+ boolean r = statusCode .isGood ();
1571+ if (log .isDebugEnabled ())
1572+ {
1573+ log .debug ("w result=" + r +" status value=" +statusCode .getValue ()+" wstr=" +strv +" objv=" +objv +" objvt=" +objv .getClass ().getCanonicalName ());
1574+ if (!r )
1575+ log .debug (" status code=" +statusCode .toString ());
1576+ }
1577+ }
15191578 }
15201579 catch ( Exception e )
15211580 {
0 commit comments