服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|

服务器之家 - 编程语言 - JAVA教程 - obix协议在java中的配置和使用详解

obix协议在java中的配置和使用详解

2020-12-22 15:36失控的婴儿车 JAVA教程

这篇文章主要给大家介绍了关于obix协议在java中的配置和使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。

前言

本文主要给大家介绍的是关于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、遍历各个点

obix协议在java中的配置和使用详解

2、先遍历各个设备,将相同的typeid的设备存入同一个hashmap中

obix协议在java中的配置和使用详解

3、开始执行主程序,先从数据库中查询出项目名称

obix协议在java中的配置和使用详解

4、开始搜索!

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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的构造方法

?
1
2
3
4
5
6
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)的构造方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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就是说这个类的当前这个对象,也就是构造方法产生的对象。

以下信息好像并没有用到

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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());
    }
  }
}

obix协议在java中的配置和使用详解

以上信息好像并没有用到

接着跑了下面两个接口

还没搞懂什么意思以下

?
1
2
3
4
5
6
7
8
9
10
public class discoverengine {
  //此处一直用内部类来实现,上面定义了一个匿名内部类,此处给匿名内部类赋值
  public void setnewvalueinterface(newvalueinterface inft){
    newvalueinterface = inft;
  }
  //同上
  public void setfactornamedecoderinterface(factornamedecoderinterface inft){
    factornamedecoderinterface = inft;
  }
}

以上

然后开始无情的 run 起来这个搜索工具

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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地址中的信息分项保存

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
  }
 
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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

延伸 · 阅读

精彩推荐