5
5
import java .io .File ;
6
6
import java .io .IOException ;
7
7
import java .io .Writer ;
8
- import java .nio .file .Files ;
9
8
import java .nio .file .Path ;
10
9
import java .nio .file .Paths ;
11
10
import java .security .cert .X509Certificate ;
28
27
import org .eclipse .milo .opcua .sdk .client .nodes .UaNode ;
29
28
import org .eclipse .milo .opcua .sdk .client .nodes .UaVariableNode ;
30
29
import org .eclipse .milo .opcua .sdk .client .nodes .UaVariableTypeNode ;
31
- import org .eclipse .milo .opcua .stack .core .Identifiers ;
32
30
import org .eclipse .milo .opcua .stack .core .NodeIds ;
33
31
import org .eclipse .milo .opcua .stack .core .OpcUaDataType ;
34
32
import org .eclipse .milo .opcua .stack .core .UaException ;
35
- import org .eclipse .milo .opcua .stack .core .security .FileBasedTrustListManager ;
36
33
import 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 ;
38
35
import org .eclipse .milo .opcua .stack .core .types .builtin .DataValue ;
36
+ import org .eclipse .milo .opcua .stack .core .types .builtin .DateTime ;
39
37
import org .eclipse .milo .opcua .stack .core .types .builtin .LocalizedText ;
40
38
import org .eclipse .milo .opcua .stack .core .types .builtin .NodeId ;
41
39
import org .eclipse .milo .opcua .stack .core .types .builtin .StatusCode ;
42
40
import org .eclipse .milo .opcua .stack .core .types .builtin .Variant ;
41
+ import org .eclipse .milo .opcua .stack .core .types .builtin .unsigned .UByte ;
43
42
import org .eclipse .milo .opcua .stack .core .types .builtin .unsigned .UInteger ;
43
+ import org .eclipse .milo .opcua .stack .core .types .builtin .unsigned .ULong ;
44
44
import org .eclipse .milo .opcua .stack .core .types .builtin .unsigned .UShort ;
45
45
import org .eclipse .milo .opcua .stack .core .types .enumerated .MessageSecurityMode ;
46
46
import org .eclipse .milo .opcua .stack .core .types .enumerated .TimestampsToReturn ;
47
47
import org .eclipse .milo .opcua .stack .core .types .structured .ApplicationDescription ;
48
48
import org .eclipse .milo .opcua .stack .core .types .structured .EndpointDescription ;
49
- import org .eclipse .milo .opcua .stack .core .types .structured .WriteValue ;
50
49
import org .iottree .core .Config ;
51
50
import org .iottree .core .UACh ;
52
51
import org .iottree .core .UANode ;
@@ -804,7 +803,7 @@ public void writeBindBeSelectTreeSub(Writer w, String pnode_id) throws Exception
804
803
{
805
804
NodeId pnid = null ;
806
805
if (Convert .isNullOrEmpty (pnode_id ))
807
- pnid = Identifiers .RootFolder ;
806
+ pnid = NodeIds .RootFolder ;
808
807
else
809
808
pnid = NodeId .parse (pnode_id );
810
809
if (pnid == null )
@@ -963,7 +962,7 @@ public void writeUaNodeTreeJson(Writer w, boolean b_var, boolean force_refresh)
963
962
964
963
public static UaNode findUaNodeByPath (OpcUaClient client , String [] path ) throws UaException
965
964
{
966
- NodeId nid = Identifiers .RootFolder ;
965
+ NodeId nid = NodeIds .RootFolder ;
967
966
968
967
UaNode pn = client .getAddressSpace ().getNode (nid );
969
968
if (pn == null )
@@ -995,7 +994,7 @@ public static void writeUaNodeTreeJson(Writer w, OpcUaClient client, String node
995
994
{
996
995
NodeId nid = null ;
997
996
if (Convert .isNullOrEmpty (nodeid ))
998
- nid = Identifiers .RootFolder ;
997
+ nid = NodeIds .RootFolder ;
999
998
else
1000
999
nid = NodeId .parse (nodeid );
1001
1000
if (nid == null )
@@ -1019,7 +1018,7 @@ public static void writeUaNodeTreeJson(Writer w, OpcUaClient client, UaNode n, b
1019
1018
1020
1019
boolean bvar = n instanceof UaVariableNode ;
1021
1020
1022
- System .out .println (nid .toParseableString ()) ;
1021
+ // System.out.println(nid.toParseableString()) ;
1023
1022
w .write ("{\" id\" :\" " + nid .toParseableString () + "\" " );
1024
1023
w .write (",\" nc\" :" + n .getNodeClass ().getValue ());
1025
1024
if (bvar )
@@ -1381,8 +1380,7 @@ private void readDataInLoop()
1381
1380
UShort us = nodeid .getNamespaceIndex ();
1382
1381
Object id = nodeid .getIdentifier ();
1383
1382
//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()
1386
1384
updateTagVal (ch , tag2n .getKey (), v );
1387
1385
}
1388
1386
}
@@ -1395,6 +1393,8 @@ private void readDataInLoop()
1395
1393
lastReadData = System .currentTimeMillis ();
1396
1394
}
1397
1395
}
1396
+
1397
+ private transient HashMap <String ,DataValue > lastTagP2GoodDV = new HashMap <>() ;
1398
1398
1399
1399
private void updateTagVal (UACh ch , String tagpath , DataValue v ) throws Exception
1400
1400
{
@@ -1415,22 +1415,22 @@ private void updateTagVal(UACh ch, String tagpath, DataValue v) throws Exception
1415
1415
// itemval.getValue().
1416
1416
if (sc .isGood ())
1417
1417
{
1418
+ lastTagP2GoodDV .put (tagpath ,v ) ;
1418
1419
long chgdt = v .getServerTime ().getJavaTime ();
1419
1420
// itemval.
1420
1421
// UAVal uav =
1421
1422
// UAVal.createByStrVal(tag.getValTp(),itemval.getLastValueStr(),chgdt,chgdt);
1422
1423
// tag.RT_setUAVal(uav);
1423
- // if(objv instanceof DateTime)
1424
- // {
1425
- //
1426
- // }
1424
+ if (objv instanceof DateTime )
1425
+ {
1426
+
1427
+ }
1427
1428
String strv = objv .toString () ;
1428
1429
OpcUaDataType dtp = vvt .getDataType ().get () ;
1429
1430
if ("DateTime" .equals (dtp .name ()))
1430
1431
{
1431
- return ;
1432
- //DateTime dt = (DateTime) vvt.getValue();
1433
- //dt.getJavaDate() ;
1432
+ DateTime dt = (DateTime ) vvt .getValue ();
1433
+ strv = Convert .toFullYMDHMS (dt .getJavaDate ()) ;
1434
1434
}
1435
1435
1436
1436
tag .RT_setValRawStr (strv , true , chgdt );
@@ -1495,6 +1495,46 @@ synchronized void disconnect() // throws IOException
1495
1495
uaClient = null ;
1496
1496
}
1497
1497
}
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
+ }
1498
1538
1499
1539
public void RT_writeValByBind (String tagpath , String strv )
1500
1540
{
@@ -1506,16 +1546,35 @@ public void RT_writeValByBind(String tagpath, String strv)
1506
1546
NodeId nid = tag2nodeid .get (tagpath );
1507
1547
if (nid == null )
1508
1548
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);
1513
1563
try
1514
1564
{
1515
1565
//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
+ }
1519
1578
}
1520
1579
catch ( Exception e )
1521
1580
{
0 commit comments