All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.envision.energy.demo.SimpleEventV2SubDemo Maven / Gradle / Ivy

There is a newer version: 3.0.3
Show newest version
package com.envision.energy.demo;

import com.envision.energy.eos.exception.SubscribeException;
import com.envision.energy.eos.sdk.*;
import com.envision.eos.event.api.bo.Event;
import com.google.common.collect.Lists;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.util.Arrays;
import java.util.List;

/**
 * @author yuanyuan.xia
 * copy from SimpleEventSubDemo
 */
public class SimpleEventV2SubDemo {
    private static final int SUB_BY_CUS_ARGS_LENGTH = 4;
    private static final int SUB_BY_SITES_AND_CODES_ARGS_LENGTH = 5;
    private static Logger logger = LogManager.getLogger(SimpleAssetSubDemo.class);
    private static EOSClient client;
    private static String appKey;
    private static String host;
    private static String appSecret;
    private static String customerId;
    private static List sites;
    private static List codes;

    public static void startSimpleDemo() {
        try {
            client = new EOSClient(appKey, appSecret, host);
            IEventV2Service service = client.getEventV2Service();
            try {
                service.subscribe(new IEventV2Handler() {
                    @Override
                    public void eventRead(com.envision.event.bean.Event event) {
                        System.out.println(event);
                    }
                }, Lists.newArrayList(customerId));

            } catch (SubscribeException e) {
                logger.error("subscribe err ", e);
            }
        } catch (Exception e) {
            logger.error("init client err ", e);
        }
    }

    public static void subcribeBySitesAndCodes() throws Exception {
        client = new EOSClient(appKey, appSecret, host);
        IEventV2Service service = client.getEventV2Service();
        try {
            service.subscribe(new IEventV2Handler() {
                @Override
                public void eventRead(com.envision.event.bean.Event event) {
                    System.out.println(event);
                }
            }, sites, codes);
        } catch (SubscribeException e) {
            logger.error("subscribe err ", e);
        }
    }

    public static void unsubscribe() {
        try {
            client = new EOSClient(appKey, appSecret, host);
            IEventV2Service service = client.getEventV2Service();
            try {
                service.unsubscribe();
            } catch (SubscribeException e) {
                logger.error("subscribe err ", e);
            }
        } catch (Exception e) {
            logger.error("init client err ", e);
        }
    }


    public static void main(String[] args) throws Exception {
        if (args == null) {
            System.out.println("args: (appKey host appSecret customerId) or (appKey host appSecret site1,site2 code1,code2), split by space");
            return;
        }

        if (args.length == SUB_BY_CUS_ARGS_LENGTH) {
            appKey = args[0];
            host = args[1];
            appSecret = args[2];
            customerId = args[3];

            startSimpleDemo();
        } else if (args.length == SUB_BY_SITES_AND_CODES_ARGS_LENGTH) {
            appKey = args[0];
            host = args[1];
            appSecret = args[2];

            sites = Arrays.asList(args[3].split(","));
            codes = Arrays.asList(args[4].split(","));

            subcribeBySitesAndCodes();
        } else {
            unsubscribe();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy