All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.envision.energy.eos.sdk.EventProcessProxy Maven / Gradle / Ivy
package com.envision.energy.eos.sdk;
import com.envision.energy.connective.protobuf.generated.Sdk;
import com.envision.energy.connective.protobuf.generated.Sdk.SubType;
import com.envision.energy.eos.exception.SubscribeException;
import com.envision.eos.event.api.bo.Event;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
class EventProcessProxy {
private Logger logger = LoggerFactory.getLogger(EventProcessProxy.class);
private static final Gson gson = new Gson();
private Transport transport = null;
private Map eventHandlerMap = new HashMap<>();
private Map eventV2HandlerMap = new HashMap<>();
public void setTransport(Transport transport) {
this.transport = transport;
}
public void sendSubMsg() {
if (transport != null) {
for (Map.Entry entry : eventHandlerMap.entrySet()) {
Sdk.SubEventCustomer subEventCustomer = entry.getValue();
if(subEventCustomer != null) {
sendSubMsg(subEventCustomer);
}
}
for (Map.Entry entry : eventV2HandlerMap.entrySet()) {
Sdk.SubEventV2Customer subEventV2Customer = entry.getValue();
if (subEventV2Customer != null) {
sendSubV2Msg(subEventV2Customer);
}
}
}
}
public void sendSubMsg(Sdk.SubEventCustomer subEventCustomer) {
if (transport != null && transport.isStarted() && subEventCustomer != null) {
transport.write(subEventCustomer);
}
}
public void sendSubV2Msg(Sdk.SubEventV2Customer subEventV2Customer) {
if (transport != null && transport.isStarted() && subEventV2Customer != null) {
transport.write(subEventV2Customer);
}
}
private void sendUnsubMsg() {
if (transport != null) {
Sdk.SubEventCustomer.Builder subEventCustomerBuilder = Sdk.SubEventCustomer.newBuilder();
subEventCustomerBuilder.setSubType(SubType.unsub);
Sdk.SubEventCustomer subEventCustomer = subEventCustomerBuilder.build();
sendMsg(SubType.unsub, subEventCustomer);
}
}
private void sendUnsubV2Msg() {
if (transport != null) {
Sdk.SubEventV2Customer.Builder subEventV2CustomerBuilder = Sdk.SubEventV2Customer.newBuilder();
subEventV2CustomerBuilder.setSubType(SubType.unsub);
Sdk.SubEventV2Customer subEventV2Customer = subEventV2CustomerBuilder.build();
sendV2Msg(SubType.unsub, subEventV2Customer);
}
}
private void sendMsg(SubType subType, Sdk.SubEventCustomer subEventCustomer) {
logger.debug("subscribe event: " + subType + ": " + transport.isStarted());
if (transport.isStarted()) {
transport.write(subEventCustomer);
}
}
private void sendV2Msg(SubType subType, Sdk.SubEventV2Customer subEventV2Customer) {
logger.debug("subscribe event v2: " + subType + ": " + transport.isStarted());
if (transport.isStarted()) {
transport.write(subEventV2Customer);
}
}
public void subscribe(IEventHandler handler, Collection customerIDs)
throws NullPointerException, SubscribeException {
Sdk.SubEventCustomer.Builder subEventCustomerBuilder = Sdk.SubEventCustomer.newBuilder();
subEventCustomerBuilder.setSubType(SubType.sub);
subEventCustomerBuilder.addAllCustomerids(customerIDs);
Sdk.SubEventCustomer subEventCustomer = subEventCustomerBuilder.build();
eventHandlerMap.put(handler, subEventCustomer);
sendSubMsg(subEventCustomer);
}
public void subscribeV2(IEventV2Handler handler, Collection customerIDs)
throws NullPointerException, SubscribeException {
Sdk.SubEventV2Customer.Builder subEventV2CustomerBuilder = Sdk.SubEventV2Customer.newBuilder();
subEventV2CustomerBuilder.setSubType(SubType.sub);
subEventV2CustomerBuilder.addAllCustomerids(customerIDs);
Sdk.SubEventV2Customer subEventV2Customer = subEventV2CustomerBuilder.build();
eventV2HandlerMap.put(handler, subEventV2Customer);
sendSubV2Msg(subEventV2Customer);
}
public void subscribe(IEventHandler handler, Collection siteIDs, Collection codes)
throws NullPointerException, SubscribeException {
Sdk.SubEventCustomer.Builder subEventCustomerBuilder = Sdk.SubEventCustomer.newBuilder();
subEventCustomerBuilder.setSubType(SubType.sub);
if (siteIDs != null && !siteIDs.isEmpty()) {
subEventCustomerBuilder.addAllSiteids(siteIDs);
}
if (codes != null && !codes.isEmpty()) {
subEventCustomerBuilder.addAllCodes(codes);
}
Sdk.SubEventCustomer subEventCustomer = subEventCustomerBuilder.build();
eventHandlerMap.put(handler, subEventCustomer);
sendSubMsg(subEventCustomer);
}
public void subscribeV2(IEventV2Handler handler, Collection siteIDs, Collection codes)
throws NullPointerException, SubscribeException {
Sdk.SubEventV2Customer.Builder subEventV2CustomerBuilder = Sdk.SubEventV2Customer.newBuilder();
subEventV2CustomerBuilder.setSubType(SubType.sub);
if (siteIDs != null && !siteIDs.isEmpty()) {
subEventV2CustomerBuilder.addAllSiteids(siteIDs);
}
if (codes != null && !codes.isEmpty()) {
subEventV2CustomerBuilder.addAllCodes(codes);
}
Sdk.SubEventV2Customer subEventV2Customer = subEventV2CustomerBuilder.build();
eventV2HandlerMap.put(handler, subEventV2Customer);
sendSubV2Msg(subEventV2Customer);
}
//向handler分发数据
public void msgToHandler(Sdk.EventStrReq msg) {
Event event = getEvent(msg);
if (event == null) {
return;
}
for (Map.Entry entry : eventHandlerMap.entrySet()) {
Sdk.SubEventCustomer subEventCustomer = entry.getValue();
if (validate(event, subEventCustomer)) {
IEventHandler eventHandler = entry.getKey();
if (eventHandler != null) {
try {
eventHandler.eventRead(event);
} catch (Exception e) {
logger.error("callback event handle error : " + e);
}
}
}
}
}
public void msgV2ToHandler(Sdk.EventV2StrReq msg) {
com.envision.event.bean.Event event = getEventV2(msg);
if (event == null) {
return;
}
for (Map.Entry entry : eventV2HandlerMap.entrySet()) {
Sdk.SubEventV2Customer subEventV2Customer = entry.getValue();
if (validateV2(event, subEventV2Customer)) {
IEventV2Handler eventHandler = entry.getKey();
if (eventHandler != null) {
try {
eventHandler.eventRead(event);
} catch (Exception e) {
logger.error("callback event handle error : " + e);
}
}
}
}
}
private boolean validate(Event event, Sdk.SubEventCustomer subEventCustomer) {
if (event == null || subEventCustomer == null) {
return false;
}
return isEventMatchCustomer(event.getSiteId(), event.getCode(), subEventCustomer.getCustomeridsList(),
subEventCustomer.getSiteidsList(), subEventCustomer.getCodesList());
}
private boolean validateV2(com.envision.event.bean.Event event, Sdk.SubEventV2Customer subEventV2Customer) {
if (event == null || subEventV2Customer == null) {
return false;
}
return isEventMatchCustomer(event.getSiteID(), event.getCode(), subEventV2Customer.getCustomeridsList(),
subEventV2Customer.getSiteidsList(), subEventV2Customer.getCodesList());
}
private boolean isEventMatchCustomer(String eventSiteID, String eventCode, List customerIDList,
List siteIDList, List codeIDList) {
if (customerIDList != null && !customerIDList.isEmpty()) {
return true;
}
if (siteIDList != null && !siteIDList.isEmpty() && siteIDList.contains(eventSiteID)) {
if (codeIDList == null || codeIDList.isEmpty()) {
return true;
}
if (codeIDList != null && !codeIDList.isEmpty() && codeIDList.contains(eventCode)) {
return true;
}
}
return false;
}
private Event getEvent(Sdk.EventStrReq msg) {
Event event = null;
logger.debug("receive event msg : " + msg.getEventJsonStr());
//System.out.println("receive event msg : " + msg.getEventJsonStr());
try {
event = gson.fromJson(msg.getEventJsonStr(), Event.class);
} catch (Exception e) {
logger.warn(msg + " parse event failed. ", e);
return null;
}
return event;
}
private com.envision.event.bean.Event getEventV2(Sdk.EventV2StrReq msg) {
com.envision.event.bean.Event event = null;
logger.debug("receive event v2 msg : " + msg.getEventJsonStr());
try {
event = gson.fromJson(msg.getEventJsonStr(), com.envision.event.bean.Event.class);
} catch (JsonSyntaxException ex) {
logger.warn(msg + " parse event v2 failed. ", ex);
return null;
}
return event;
}
public void unsubscribe() throws SubscribeException {
try {
sendUnsubMsg();
//eventHandler = null;
} catch (Exception e) {
logger.error("exception: " + e);
throw new SubscribeException("unsubscribe failed.");
}
}
public void unsubscribeV2() throws SubscribeException {
try {
sendUnsubV2Msg();
} catch (Exception e) {
logger.error("exception: " + e);
throw new SubscribeException("unsubscribe failed.");
}
}
public void unsubscribe(IEventHandler handler) throws SubscribeException {
if (eventHandlerMap != null) {
//此处退订一部分只会将Handler清楚,即还会发过来,只是过滤的时候把他过滤掉了
eventHandlerMap.remove(handler);
}
if (this.eventHandlerMap == null || this.eventHandlerMap.isEmpty()) {
unsubscribe();
}
}
public void unsubscribeV2(IEventV2Handler handler) throws SubscribeException {
if (eventV2HandlerMap != null) {
//此处退订一部分只会将Handler清楚,即还会发过来,只是过滤的时候把他过滤掉了
eventV2HandlerMap.remove(handler);
}
if (this.eventV2HandlerMap == null || this.eventV2HandlerMap.isEmpty()) {
unsubscribeV2();
}
}
public void shutdown() {
transport = null;
}
}