发布于 2018-01-18 06:58:12 | 204 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Java设计模式,程序狗速度看过来!

Java程序设计语言

java 是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于1995年5月推出的Java程序设计语言和Java平台(即JavaEE(j2ee), JavaME(j2me), JavaSE(j2se))的总称。


这篇文章主要给大家介绍了关于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、遍历各个点

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();
    }
  }

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对PHPERZ的支持。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务