Skip to content

Commit

Permalink
replaced apache client by jetty client, reduced size of library,
Browse files Browse the repository at this point in the history
re-enabled dispose
  • Loading branch information
ZzetT committed Mar 23, 2019
1 parent 8cf2d03 commit 2b097c7
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 24 deletions.
2 changes: 1 addition & 1 deletion addons/binding/org.openhab.binding.telegram/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="lib" path="lib/telegrambots-4.1-jar-with-dependencies.jar"/>
<classpathentry kind="lib" path="lib/telegrambots-4.1.2-jar-with-dependencies.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Automatic-Module-Name: org.openhab.binding.telegram;x-internal:=true
Bundle-ActivationPolicy: lazy
Bundle-ClassPath: .,
lib/telegrambots-4.1-jar-with-dependencies.jar
lib/telegrambots-4.1.2-jar-with-dependencies.jar
Bundle-ManifestVersion: 2
Bundle-Name: Telegram Binding
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Expand All @@ -11,6 +11,12 @@ Bundle-Vendor: openHAB
Bundle-Version: 2.4.0.qualifier
Import-Package:
org.eclipse.jdt.annotation;resolution:=optional,
org.eclipse.jetty.client,
org.eclipse.jetty.client.api,
org.eclipse.jetty.client.util,
org.eclipse.jetty.http,
org.eclipse.jetty.util,
org.eclipse.jetty.util.ssl,
org.eclipse.smarthome.automation.annotation;resolution:=optional,
org.eclipse.smarthome.config.core,
org.eclipse.smarthome.core.cache,
Expand All @@ -21,6 +27,7 @@ Import-Package:
org.eclipse.smarthome.core.thing.type,
org.eclipse.smarthome.core.types,
org.eclipse.smarthome.core.util,
org.eclipse.smarthome.io.net.http,
org.openhab.binding.telegram.bot,
org.slf4j
Export-Package:
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/**
* Copyright (c) 2010-2018 by the respective copyright holders.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.openhab.binding.telegram.bot;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -33,7 +41,7 @@
/**
* Provides the actions for the Telegram API.
*
* @author Alexander Krasnogolowy - initial contributor
* @author Alexander Krasnogolowy - Initial contribution
*
*/
@ThingActionsScope(name = "telegram")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Map;

import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.openhab.binding.telegram.internal.TelegramHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -31,7 +32,7 @@
* @author Jens Runge - Initial contribution
*/
public class TelegramBot extends TelegramLongPollingBot {
private final String botToken, botUsenName;
private final String botToken, botUserName;
private final List<Long> chatIds;
private final TelegramHandler telegramHandler;
private static final Logger logger = LoggerFactory.getLogger(TelegramBot.class);
Expand All @@ -43,24 +44,24 @@ public class TelegramBot extends TelegramLongPollingBot {
// answer and need the id of the original message
private final Map<String, Integer> replyIdToMessageId = new HashMap<>();

public TelegramBot(String botToken, String botUsenName, List<Long> chatIds, TelegramHandler telegramHandler) {
public TelegramBot(HttpClient httpClient, String botToken, String botUsenName, List<Long> chatIds,
TelegramHandler telegramHandler) {
super(httpClient);
this.botToken = botToken;
this.botUsenName = botUsenName;
this.botUserName = botUsenName;
this.telegramHandler = telegramHandler;
this.chatIds = chatIds;
}

@Override
public String getBotUsername() {
// TODO Auto-generated method stub
return botUsenName;
return botUserName;
}

@Override
public void onUpdateReceived(Update update) {

if (update.hasMessage() && update.getMessage().hasText()
/* && update.getMessage().getChatId().intValue() == chatIds */) {
if (update.hasMessage() && update.getMessage().hasText()) {
Message message = update.getMessage();
if (!chatIds.contains(message.getChatId())) {
return; // this is very important regarding security to avoid commands from an unknown chat
Expand Down Expand Up @@ -110,7 +111,6 @@ public List<Long> getChatIds() {

@Override
public String getBotToken() {
// TODO Auto-generated method stub
return botToken;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.smarthome.core.library.types.StringType;
import org.eclipse.smarthome.core.thing.ChannelUID;
import org.eclipse.smarthome.core.thing.Thing;
Expand Down Expand Up @@ -47,8 +48,11 @@ public class TelegramHandler extends BaseThingHandler {
@Nullable
private BotSession session;

public TelegramHandler(Thing thing) {
private final HttpClient httpClient;

public TelegramHandler(Thing thing, HttpClient httpClient) {
super(thing);
this.httpClient = httpClient;
}

@Override
Expand Down Expand Up @@ -98,7 +102,7 @@ public void initialize() {
ApiContextInitializer.init();
TelegramBotsApi botsApi = new TelegramBotsApi();
try {
bot = new TelegramBot(botToken, botName, chatIds, this);
bot = new TelegramBot(httpClient, botToken, botName, chatIds, this);
session = botsApi.registerBot(bot);
updateStatus(ThingStatus.ONLINE);
} catch (TelegramApiException e) {
Expand All @@ -108,15 +112,12 @@ public void initialize() {

@Override
public void dispose() {
// if (session != null) {
// BotSession s = session;
// //TODO: this code is currently not usable, because stopping the botsession takes a very long time (see
// https://github.com/rubenlagus/TelegramBots/issues/498), but the current thread context is not allowed to
// wait.
// if (s.isRunning()) {
// s.stop();
// }
// }
if (session != null) {
BotSession s = session;
if (s.isRunning()) {
s.stop();
}
}
}

public void updateChannel(String channelName, String stateString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
import org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory;
import org.eclipse.smarthome.io.net.http.HttpClientFactory;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
* The {@link TelegramHandlerFactory} is responsible for creating things and thing
Expand All @@ -32,6 +35,8 @@
@Component(configurationPid = "binding.telegram", service = ThingHandlerFactory.class)
public class TelegramHandlerFactory extends BaseThingHandlerFactory {

private @NonNullByDefault({}) HttpClient httpClient;

private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(TELEGRAM_THING);

@Override
Expand All @@ -44,9 +49,17 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();

if (TELEGRAM_THING.equals(thingTypeUID)) {
return new TelegramHandler(thing);
return new TelegramHandler(thing, httpClient);
}

return null;
}

@Reference
protected void setHttpClientFactory(HttpClientFactory httpClientFactory) {
this.httpClient = httpClientFactory.getCommonHttpClient();
}

protected void unsetHttpClientFactory(HttpClientFactory httpClientFactory) {
this.httpClient = null;
}
}

0 comments on commit 2b097c7

Please sign in to comment.