obix协议在java中的配置和使用详解
作者:失控的婴儿车 发布时间:2023-11-25 20:59:42
前言
本文主要给大家介绍的是关于obix协议在java中的配置和使用,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
什么是 oBIX?
简单来讲,obix是一种 XML 通讯协议,使用Http Request/Post方式进行数据通讯。所有数据通过可读字符进行传送,一个oBIX对象可以有唯一的一个URL识别。
oBIX的实现原理
首先数据储存在Niagara的服务平台上,我们需要做的是从Niagara获取数据,并且储存在InfluxDB中。下面是实现的流程方法。
加粗 Ctrl + B
斜体 Ctrl + I
引用 Ctrl + Q
插入链接 Ctrl + L
插入代码 Ctrl + K
插入图片 Ctrl + G
提升标题 Ctrl + H
有序列表 Ctrl + O
无序列表 Ctrl + U
横线 Ctrl + R
撤销 Ctrl + Z
重做 Ctrl + Y
我们都需要定义哪些类以及变量?
类/接口 名 | 用途 |
---|---|
Calculator | |
DiscoverEngine | 搜索工具 |
FactorInfo | 定义所采集元素的信息 |
FactorNameDecoderInterface | 元素名称解码接口 |
FactorNameDecoderObixUrlImpl | |
NewValueInterface | |
NewValueInterfaceImpl | |
ObixClientMgr | |
ObixClient | |
ObixFetcher | 循环抓取obix传输的数据 |
1、遍历各个点
2、先遍历各个设备,将相同的typeid的设备存入同一个hashmap中
3、开始执行主程序,先从数据库中查询出项目名称
4、开始搜索!
public class ObixFetcher implements JobInterface{
//这个是接口的抽象方法
public void cycleOnce() {
//从数据库中取出项目信息
List<Project> ps = dao.selectByExample(new ProjectExample());
//遍历项目信息,如果项目信息的关键信息不为null
for(Project p : ps){
if(p.getObixBaseAddress() != null && p.getObixUsername() != null
&& p.getObixPassword() != null){
//开启探索工具 (应该还是一个内部类),将关键项目信息传入探索工具,
DiscoverEngine de = new DiscoverEngine(p.getObixBaseAddress(),
p.getObixUsername(), p.getObixPassword());
//从build数据库中将数据取出,存入bulidNameToId(同样还是构造方法)
//从device数据库中将数据取出,存入deviceNumberToId(同样还是构造方法)
de.setNewValueInterface(new NewValueInterfaceImpl(p.getId(), deviceService, deviceDao, deviceTypeDao, buildDao));
//return回来一个FactorInfo
de.setFactorNameDecoderInterface(new FactorNameDecoderObixUrlImpl());
de.run();
}
}
}
}
以下是上文 DiscoverEngine de的构造方法
public class DiscoverEngine {
public DiscoverEngine(String baseUrl, String username, String password){
this.baseUrl = baseUrl;
obixClient = new ObixClient(baseUrl, username, password);
}
}
以下是上文obixClient = new ObixClient(baseUrl, username, password)
的构造方法
public class ObixClient {
public ObixClient(String baseUrl, String userName, String password){
this.baseUrl = baseUrl;
this.userName = userName;
this.password = password;
init();
}
//uri中存放着路由地址,然后传给session,session会在后面用到
private void init() {
Uri uri = new Uri(baseUrl);
session = new ObixSession (uri, userName, password);
}
}
this就是说这个类的当前这个对象,也就是构造方法产生的对象。
以下信息好像并没有用到
public class NewValueInterfaceImpl implements NewValueInterface{
HashMap<String, Integer> buildNameToId = new HashMap<String, Integer>();
HashMap<String, Integer> deviceNumberToId = new HashMap<String, Integer>();
public NewValueInterfaceImpl(Integer projectId, IDeviceManagementService deviceService, DeviceMapper deviceDao, DeviceTypeMapper deviceTypeDao,BuildMapper buildDao) {
this.deviceDao = deviceDao;
this.deviceTypeDao = deviceTypeDao;
this.buildDao = buildDao;
this.projectId = projectId;
this.deviceService = deviceService;
//遍历数据库中的建筑,存入map
List<Build> bs = buildDao.selectByExample(new BuildExample());
for(Build b : bs){
buildNameToId.put(b.getName(), b.getId());
}
//遍历数据库中的设备,存入map
List<Device> ds = deviceDao.selectByExample(new DeviceExample());
for(Device d : ds){
deviceNumberToId.put(d.getNumber(), d.getId());
}
}
}
以上信息好像并没有用到
接着跑了下面两个接口
还没搞懂什么意思以下
public class DiscoverEngine {
//此处一直用内部类来实现,上面定义了一个匿名内部类,此处给匿名内部类赋值
public void setNewValueInterface(NewValueInterface inft){
newValueInterface = inft;
}
//同上
public void setFactorNameDecoderInterface(FactorNameDecoderInterface inft){
factorNameDecoderInterface = inft;
}
}
以上
然后开始无情的 run 起来这个搜索工具
public class DiscoverEngine {
//首先传进来url地址
public void run(){
readUrl(baseUrl);
}
public void readUrl(String url){
// System.out.println("processing url " + url);
//此处用到session方法
Obj obj = obixClient.read(url);
if(obj == null){
return;
}
//新建一个Obj,用于储存 out 所对应的 value
Obj outObj = obj.get("out");//此处的out是储存的值(理解成标签的ID)
if(outObj != null){
//如果****那么就新建一个fi 将项目信息分项保存
if(this.factorNameDecoderInterface != null){
FactorInfo fi = factorNameDecoderInterface.decode(obj.getHref().get());
if(newValueInterface != null && fi != null){
//如果信息不为空,我们就要准备存读出来的数了
newValueInterface.onNewValue(fi, outObj.toString());
}
}
}
else{
for(Obj o : obj.list()){
readUrl(url + o.getHref());
}
}
}
}
下面用到了session
public class ObixClient {
public Obj read(String url){
try {
//根据我们传进去的url中读出一个obj,这个obj如果有value,就返回一个数,否则就返回地址 href="http://115.28.2.201:28088/obix/config/Drivers/NiagaraNetwork/Himalayas_PC/points/Himalayas_301/points/JF/"/>
Obj obj = session.read(new Uri(url));
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
将URL地址中的信息分项保存
public class FactorNameDecoderObixUrlImpl implements FactorNameDecoderInterface{
@Override
public FactorInfo decode(String url) {
String[] tokens = url.split(":")[2].split("\\/");
//新建一个 对象 将url中解析出的信息分享保存到这个对象中
FactorInfo fi = new FactorInfo();
fi.setDeviceName(tokens[tokens.length - 2]);
fi.setDeviceTypeName(tokens[tokens.length - 3]);
fi.setFactorName(tokens[tokens.length - 1]);
fi.setBuildName("");
fi.setFloorName("");
fi.setProjectName("");
return fi;
}
}
public class NewValueInterfaceImpl implements NewValueInterface{
static ConcurrentHashMap<String, Long> dataInfluxTime = new ConcurrentHashMap<String, Long>();
DeviceMapper deviceDao;
IDeviceManagementService deviceService;
DeviceTypeMapper deviceTypeDao;
BuildMapper buildDao;
Integer projectId;
HashMap<String, Integer> buildNameToId = new HashMap<String, Integer>();
HashMap<String, Integer> deviceNumberToId = new HashMap<String, Integer>();
public NewValueInterfaceImpl(Integer projectId, IDeviceManagementService deviceService, DeviceMapper deviceDao, DeviceTypeMapper deviceTypeDao,BuildMapper buildDao) {
this.deviceDao = deviceDao;
this.deviceTypeDao = deviceTypeDao;
this.buildDao = buildDao;
this.projectId = projectId;
this.deviceService = deviceService;
List<Build> bs = buildDao.selectByExample(new BuildExample());
for(Build b : bs){
buildNameToId.put(b.getName(), b.getId());
}
List<Device> ds = deviceDao.selectByExample(new DeviceExample());
for(Device d : ds){
deviceNumberToId.put(d.getNumber(), d.getId());
}
}
@Override
public void onNewValue(FactorInfo fi, String value) {
//先将URL中的设备名称信息取出来,然后再取出对应的ID
String number = fi.getDeviceName();
Integer deviceId = deviceNumberToId.get(number);
if(deviceId == null){
number = fi.getDeviceName();
Device record = new Device();
record.setName(number);
record.setNumber(number);
record.setProjectId(projectId);
record.setTypeId(fi.getDeviceTypeId());
deviceService.insert(record );
deviceId = record.getId();
System.out.println("found new device id=" + deviceId + ", name=" + number);
deviceNumberToId.put(number, deviceId);
}
Double val = null;
//然后将ID存入device中
Device updateRecord = new Device();
updateRecord.setId(deviceId);
//将取出的值也存入device
try{
Double d = Double.parseDouble(value);
updateRecord.setCurrentValue(d.intValue());
val = d;
}
catch(Exception e){
if(value.equalsIgnoreCase("true")){
updateRecord.setCurrentValue(1);
val = 1.0;
}
else if(value.equalsIgnoreCase("false")){
updateRecord.setCurrentValue(0);
val = 0.0;
}
}
if(updateRecord.getCurrentValue() != null){
deviceDao.updateByPrimaryKeySelective(updateRecord );
}
//将所得所得数据存入influxDB
try{
String timeKey = projectId+"_"+deviceId+"_"+fi.getFactorName();
Long t = dataInfluxTime.get(timeKey);
if(t == null) t = new Long(0);
Long now = System.currentTimeMillis();
if(now - t > 10 * 60 * 1000){
InfluxDBUtil.insert(projectId, deviceId, convert10Minutes(now), fi.getFactorName(), val);
dataInfluxTime.put(timeKey, now);
}
}
catch(Exception e){
e.printStackTrace();
}
}
来源:https://segmentfault.com/a/1190000010890756


猜你喜欢
- 一丶先引入上传下载的lib二丶上传的的servletpackage com.test.action;import java.io.File;
- 本文实例总结了Android实现计时与倒计时的常用方法。分享给大家供大家参考,具体如下:方法一Timer与TimerTask(Java实现)
- 一.NET Remoting 介绍简介.NET Remoting与MSMQ不同,它不支持离线可得,另外只适合.NET平台的程序进行通信。它提
- 简单的理解,MyBatis逆向工程,就是通过相应插件,自动生成MyBatis数据库连接的一些文件。mybatis需要编写sql语句,myba
- 一、平衡二叉树的定义平衡二叉树是一种二叉排序树,其中每一个节点的左子树和右子树的高度差至多等于1 。它是一种高度平衡的二叉排序树。意思是说,
- 前言 用过微信的都知道,微信对话列表滑动删除效果是很不错的,这个效果我们也可以有。思路其实很简单,弄个ListView,然后里面的
- 一、时区的基本概念GMT(Greenwich Mean Time),即格林威治标准时,是东西经零度的地方。人们将地球人为的分为24等份,每一
- 前言这个也是Java实验课程的一个作业,和Java实现简单的图形界面计算器一起做的,因为以前没有做过GUI编程,所以做的非常简陋,还有很多B
- 本篇文章基于redisson-3.17.6版本源码进行分析一、主从redis架构中分布式锁存在的问题1、线程A从主redis中请求一个分布式
- 一、基本概念(重要)Integer 是 int 的包装类,int 则是 java 的一种基本数据类型;Integer 变量必须实例化后才能使
- 目录前言一、技术介绍1.Minio是什么?二、使用步骤1.引入maven库2.封装Minio3.配置文件4.单元测试总结前言使用Spring
- 概述还没玩过Spring Boot,现在越来越多的公司在用了,不得不学习了。本篇是Spring Boot的开篇,简单介绍一下如何创建一个Sp
- 本篇主要总结下Spring容器在初始化实例前后,提供的一些回调方法和可扩展点。利用这些方法和扩展点,可以实现在Spring初始化实例前后做一
- 具体代码如下所示;<!-- 第一种打包方式 (maven-jar-plugin), 将依赖包和配置文件放到jar包外 -->&l
- using System;using System.Collections.Generic;using System.Linq;using
- 本文主要介绍了C# 泛型List排序的实现,分享给大家,具体如下:代码using System;using System.Collectio
- 对单表进行增删改查是项目中不可避免的需求,Mybatis的通用Mapper插件使这些操作变得简单添加maven依赖在对应工程的pom.xml
- 某天突然发现idea非常重要的快捷键ctrl+shift+f无效了,网上搜了很多都说是qq快捷键冲突,但是找了下qq快捷键却没有解决,现在给
- “Hello World!”的程序写过不少,不过都是在黑色背景的控制台上显示白色的文字。这次决定写点特别的,让“Hello World!”变
- 本文实例为大家分享了java实现鲜花销售系统的具体代码,供大家参考,具体内容如下一、练习目标1.体会数组的作用2.找到分层开发的感觉3.收获