当前位置:网站首页>How to use opcua protocol on appinventor?
How to use opcua protocol on appinventor?
2022-04-22 22:43:00 【Taoist incense】
Here is an inexplicable need , So it's not easy to find tutorials online , So I stepped on some pits .
Because I'm not familiar with java, It's also stumbling .
opcua Library selection
First opcua I found two :
1.https://github.com/eclipse/milo
https://github.com/eclipse/milo
The new is the good principle , Used milo, There are many online tutorials , But there was a pit .
because milo It's a newer version of java Development , The features of the new version are not applicable to the old version !
and appinventor It is a project with a very early development time , Supported by java The version is 7/8.
In the step of importing and compiling appinventor Development _ Taoist incense's blog -CSDN Blog Reference documents :https://saitwalshreyash19.medium.com/writing-extensions-for-app-inventor-2-and-kodular-7d20092bff16https://saitwalshreyash19.medium.com/writing-extensions-for-app-inventor-2-and-kodular-7d20092bff16https://islet.blog.csdn.net/article/details/124301948?spm=1001.2014.3001.5502 But it keeps reporting mistakes !
I found out later because milo Used lambda characteristic , As a result, the library is not supported .
So in the end I chose OPCFoundation This , Can be published in tag Download directly from .jar file , It's relatively simple .
Library import problem
But even so , There will still be problems , Because the library also relies on sl4j This .
Due to initial contact java, I don't know why this library is used , But you can find some solutions on the Internet , Go directly to download :

Pay attention to the version I use , Even if it is opcua You can't use too new versions , If you choose wrong, there will be compilation errors .
Then pay attention to java Both libraries are required when importing from a file :
@SimpleObject(external = true) // External plug-ins
// You can't put it in this position ! Must be in SimpleObject below
@UsesLibraries(libraries = "opc-ua-stack-1.4.1-224.jar,slf4j-api-1.7.0.jar")
Development process
there import Not very professional , Add everything you may need :
// opcua
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.InvalidParameterSpecException;
import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.util.Locale;
import com.google.appinventor.components.annotations.UsesLibraries;
import org.opcfoundation.ua.application.Client;
import org.opcfoundation.ua.application.SessionChannel;
import org.opcfoundation.ua.builtintypes.LocalizedText;
import org.opcfoundation.ua.builtintypes.NodeId;
import org.opcfoundation.ua.cert.CertificateCheck;
import org.opcfoundation.ua.cert.DefaultCertificateValidator;
import org.opcfoundation.ua.cert.DefaultCertificateValidatorListener;
import org.opcfoundation.ua.cert.PkiDirectoryCertificateStore;
import org.opcfoundation.ua.cert.ValidationResult;
import org.opcfoundation.ua.core.ApplicationDescription;
import org.opcfoundation.ua.core.Attributes;
import org.opcfoundation.ua.core.BrowseDescription;
import org.opcfoundation.ua.core.BrowseDirection;
import org.opcfoundation.ua.core.BrowseResponse;
import org.opcfoundation.ua.core.BrowseResultMask;
import org.opcfoundation.ua.core.Identifiers;
import org.opcfoundation.ua.core.NodeClass;
import org.opcfoundation.ua.core.ReadResponse;
import org.opcfoundation.ua.core.ReadValueId;
import org.opcfoundation.ua.core.TimestampsToReturn;
import org.opcfoundation.ua.common.ServiceResultException;
import org.opcfoundation.ua.transport.security.Cert;
import org.opcfoundation.ua.transport.security.KeyPair;
import org.opcfoundation.ua.transport.security.PrivKey;
import org.opcfoundation.ua.utils.CertificateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.opcfoundation.ua.utils.EndpointUtil.selectByMessageSecurityMode;
import static org.opcfoundation.ua.utils.EndpointUtil.selectByProtocol;
import static org.opcfoundation.ua.utils.EndpointUtil.sortBySecurityLevel;
import java.net.InetAddress;
import org.opcfoundation.ua.application.Client;
import org.opcfoundation.ua.core.Attributes;
import org.opcfoundation.ua.core.EndpointDescription;
import org.opcfoundation.ua.core.Identifiers;
import org.opcfoundation.ua.core.MessageSecurityMode;
import org.opcfoundation.ua.core.ReadRequest;
import org.opcfoundation.ua.core.ReadResponse;
import org.opcfoundation.ua.core.ReadValueId;
import org.opcfoundation.ua.core.TimestampsToReturn;
import org.opcfoundation.ua.transport.ServiceChannel;
import org.opcfoundation.ua.transport.security.KeyPair;
//write
import org.opcfoundation.ua.builtintypes.DataValue;
import org.opcfoundation.ua.builtintypes.Variant;
import org.opcfoundation.ua.core.WriteResponse;
import org.opcfoundation.ua.core.WriteValue;
import org.opcfoundation.ua.core.WriteRequest;
import org.opcfoundation.ua.core.RequestHeader;
//thread
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
The steps to connect to the server :
// Create a task class , similar Runnable
public static class CallerTask implements Callable<String>{
@Override
public String call() throws Exception{
String retStr = "";
Client myClient = Client.createClientApplication(null);
try {
String url = "opc.tcp://192.168.1.8:4840";
SessionChannel mySession = myClient.createSessionChannel(url);
mySession.activate("username", "passwd.");
mySession.close();
mySession.closeAsync();
} catch (Exception e1) {
retStr = "Error";
}
return retStr;
}
}
Threads are used here because the main program cannot use network services .
Read operations :
ReadResponse res4 = mySession.Read(null, 500.0, TimestampsToReturn.Source,
new ReadValueId(new NodeId(1, "XXXXX"), Attributes.Value, null, null));
retStr = String.valueOf(res4.getResults()[0].getValue());//getStatusCode
Write operations :
NodeId nodeId = new NodeId(1, "XXX");
//Bad_TypeMismatch double Type must have a decimal point
DataValue dataValue = new DataValue(new Variant(999.0));
WriteValue writeValue[] = new WriteValue[1];
writeValue[0] = new WriteValue(nodeId, Attributes.Value, null, dataValue); // The third parameter is zero null You can use ordinary writing
RequestHeader requestHeader = new RequestHeader(nodeId, null, null, null, null, null, null);
WriteRequest writeRequest = new WriteRequest(requestHeader, writeValue);
WriteResponse response = mySession.Write(writeRequest);
retStr = String.valueOf(response.getResults()[0]);//
Finally, add the call of thread return value :
public String readNode() throws Exception{
String retStr = "";
// Android 4.0 After that... Cannot be requested in the main thread HTTP request
// Create asynchronous tasks
FutureTask<String> futureTask = new FutureTask<>(new ReadCallerTask());
// Start thread
new Thread(futureTask).start();
try{
// Wait for the task to finish , And return the result
String result = futureTask.get();
return result;
} catch (ExecutionException e){
e.printStackTrace();
}
return retStr;
}
版权声明
本文为[Taoist incense]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222204196433.html
边栏推荐
- 二分法应用:875. 爱吃香蕉的珂珂
- GBase 8s V8.8 SQL 指南:教程-6.1.2(2)
- Database resource load management (Part 2)
- vickers威格士比例阀的特点
- 多线程进阶(八)----线程池
- 【玩转Lighthouse】搭建即收即毁的临时邮箱系统
- 处理用逗号分隔的字符串 并按字典升序排序输出
- MySql--库的操作
- 【Paper】2019_Distributed fixed-time consensus-based formation tracking for multiple nonholonomic whee
- GBase 8s V8.8 SQL 指南:教程-6.2.1(2)
猜你喜欢

Svg series - 3, powerful path

2.55-在你能够访问的不同机器上,使用show_bytes(文件show_bytes.c)编译并运行示例代码。确定这些机器使用的字节顺序。
![[mmub] mobile phone user behavior modeling based on Hidden Markov Model -- hidden Markov model](/img/8e/e24ddaf4bd68d41695728bf679deaa.png)
[mmub] mobile phone user behavior modeling based on Hidden Markov Model -- hidden Markov model

41.0:GemBox.Spreadsheet|.Document|.Pdf|.Presentation

GBase 8s V8. 8 SQL Guide: Tutorial - 6.2.1 (2)

MySql--表的操作
![[intranet penetration] - vulnstack (I)](/img/d4/b7267b8306b353ea6a8ac22d64f9f6.png)
[intranet penetration] - vulnstack (I)

hawe哈威液压泵站的液压冲击分析

OV代码签名和EV代码签名证书区别

小赛毛游C记——初识C语言(3)
随机推荐
01 knapsack problem
2.60-假设我们将一个w位的字中的字节从0(最低位)到w/8- 1(最高位)编号。写出下面C函数的代码,它会返回一个无符号值,其中参数x的字节i被替换成字节b。
0-1背包问题讲解 & leetcode相关题目总结
Svg series - 3, powerful path
初学单片机点亮第一个外设--LED灯
Centos7安装mysql
On the law of enterprise informatization
2.59-编写一个C表达式,它生成一个字,由x的最低有效字节和y中剩下的字节组成。对于运算数x =0x89ABCDEF和y=0x76543210,就得到0x765432EF.
SecureCRT v7. 0 failed to connect to sshd service. Solution
意大利阿托斯电磁阀的工作性质是什么?
吴恩达-深度学习微课-第四课
0基础UnityURP渲染管线之阴影ShadowCaster-ShadowMask-Map傻傻分不清楚(代码向)
NLP方向 ---- 面试、笔试题集(7):预训练模型 BERT
100 billion level im independent development guide! Global instant messaging full set of codes 4 hours quick (II)
对象大小与指针压缩--JVM
JS solving power deduction daily question (8) -- 396 Rotation function (2022-4-22)
kubeflow创建新用户用户密码
51 MCU proteus simulation key control nixie tube digital display
跨域问题及Umi-proxy代理解决跨域问题
[wechat applet development (cloud wallpaper applet tutorial)]