前言
本文主要給大家介紹的是關于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、開始搜索!
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()); } } } |
以上信息好像并沒有用到
接著跑了下面兩個接口
還沒搞懂什么意思以下
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