Skip to content

Commit

Permalink
[signal] return value for action and user agent configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
dalgwen committed Apr 26, 2024
1 parent 54cd029 commit 8ede55b
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 13 deletions.
2 changes: 2 additions & 0 deletions bundles/org.openhab.binding.signal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ When using the linked bridge thing, you can use the special recipient "self" to

If you want notification when sending message to your own account, use the send action with your full number.

Each "send" action returns a map with one key : "RESULT", which has either "OK" or "KO" as a value.

## Full Example

### Thing configuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.binding.signal.internal.protocol.RegistrationType;
import org.openhab.binding.signal.internal.protocol.SignalService;

/**
* The {@link SignalBridgeConfiguration} class contains fields mapping bridge configuration parameters.
Expand All @@ -28,6 +29,7 @@ public class SignalBridgeConfiguration {
public String captcha = "";
public RegistrationType verificationCodeMethod = RegistrationType.PhoneCall;
public String verificationCode = "";
public String userAgent = SignalService.DEFAULT_USER_AGENT;

// linked bridge configuration
public String deviceName = "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@
*/
package org.openhab.binding.signal.internal.actions;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.signal.internal.handler.SignalBridgeHandler;
import org.openhab.binding.signal.internal.protocol.DeliveryReport;
import org.openhab.core.automation.annotation.ActionInput;
import org.openhab.core.automation.annotation.RuleAction;
import org.openhab.core.thing.binding.ThingActions;
Expand Down Expand Up @@ -47,41 +51,70 @@ public void setThingHandler(@Nullable ThingHandler handler) {
}

@RuleAction(label = "Send Message With Signal", description = "Send a message with Signal")
public void sendSignal(
public Map<String, Object> sendSignal(
@ActionInput(name = "recipient", label = "recipient", description = "Recipient of the message") @Nullable String recipient,
@ActionInput(name = "message", label = "message", description = "Message to send") @Nullable String message) {
Map<String, Object> resultMap = new HashMap<>();
if (recipient != null && !recipient.isEmpty() && message != null) {
handler.send(recipient, message);
DeliveryReport report = handler.send(recipient, message);
switch (report.deliveryStatus) {
case DELIVERED:
case READ:
case SENT:
resultMap.put("RESULT", "OK");
break;
case FAILED:
case UNKNOWN:
resultMap.put("RESULT", "KO");
break;
}
} else {
resultMap.put("RESULT", "KO");
logger.error("Signal cannot send a message with no recipient or text");
}
return resultMap;
}

public static void sendSignal(@Nullable ThingActions actions, @Nullable String recipient,
public static Map<String, Object> sendSignal(@Nullable ThingActions actions, @Nullable String recipient,
@Nullable String message) {
if (actions instanceof SignalActions) {
((SignalActions) actions).sendSignal(recipient, message);
return ((SignalActions) actions).sendSignal(recipient, message);
} else {
throw new IllegalArgumentException("Instance is not an SignalActions class.");
}
}

@RuleAction(label = "Send Image With Signal", description = "Send an Image with Signal")
public void sendSignalImage(
public Map<String, Object> sendSignalImage(
@ActionInput(name = "recipient", label = "recipient", description = "Recipient of the message") @Nullable String recipient,
@ActionInput(name = "image", label = "image", description = "Image to send") @Nullable String image,
@ActionInput(name = "text", label = "text", description = "Text to send") @Nullable String text) {
Map<String, Object> resultMap = new HashMap<>();

if (recipient != null && !recipient.isEmpty() && image != null) {
handler.sendImage(recipient, image, text);
DeliveryReport report = handler.sendImage(recipient, image, text);
switch (report.deliveryStatus) {
case DELIVERED:
case READ:
case SENT:
resultMap.put("RESULT", "OK");
break;
case FAILED:
case UNKNOWN:
resultMap.put("RESULT", "KO");
break;
}
} else {
logger.error("Signal cannot send a photo with no recipient or text");
resultMap.put("RESULT", "KO");
}
return resultMap;
}

public static void sendSignalImage(@Nullable ThingActions actions, @Nullable String recipient,
public static Map<String, Object> sendSignalImage(@Nullable ThingActions actions, @Nullable String recipient,
@Nullable String image, @Nullable String text) {
if (actions instanceof SignalActions) {
((SignalActions) actions).sendSignalImage(recipient, image, text);
return ((SignalActions) actions).sendSignalImage(recipient, image, text);
} else {
throw new IllegalArgumentException("Instance is not an SignalActions class.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public void initialize() {
: ProvisionType.MAIN;
try {
this.signalService = new SignalService(this, this, config.phoneNumber, config.captcha,
config.verificationCode, config.verificationCodeMethod, config.deviceName, provisionType);
config.verificationCode, config.verificationCodeMethod, config.deviceName, provisionType,
config.userAgent);
} catch (IOException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
"Error during initialization : " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class SignalService {

private static final String SIGNAL_DIRECTORY = "signal";
private static final int MAX_BACKOFF_COUNTER = 9;
private static final String USER_AGENT = "Signal-Android/6.46.0 signal-cli";
public static final String DEFAULT_USER_AGENT = "Signal-Android/6.46.0 signal-cli";
public static final String NOTE_TO_SELF = "SELF";
private static final ServiceEnvironment SERVICE_ENVIRONMENT = ServiceEnvironment.LIVE;

Expand Down Expand Up @@ -111,8 +111,8 @@ public class SignalService {

public SignalService(MessageListener messageListener, StateListener connectionStateListener, String phoneNumber,
@Nullable String captcha, @Nullable String verificationCode,
@Nullable RegistrationType verificationCodeMethod, String deviceName, ProvisionType provisionType)
throws IOException {
@Nullable RegistrationType verificationCodeMethod, String deviceName, ProvisionType provisionType,
@Nullable String userAgent) throws IOException {
this.messageListener = messageListener;
this.connectionStateListener = connectionStateListener;
this.phoneNumber = phoneNumber;
Expand All @@ -123,12 +123,13 @@ public SignalService(MessageListener messageListener, StateListener connectionSt
: verificationCodeMethod;
this.deviceName = deviceName;
this.provisionType = provisionType;
String resovedUserAgent = userAgent != null && !userAgent.isBlank() ? userAgent : DEFAULT_USER_AGENT;

initializeLock.lock(); // class wide lock. init this just once :
try {
if (signalAccountsFiles == null) {
signalAccountsFiles = new SignalAccountFiles(new File(OpenHAB.getUserDataFolder(), SIGNAL_DIRECTORY),
SERVICE_ENVIRONMENT, USER_AGENT, SETTINGS);
SERVICE_ENVIRONMENT, resovedUserAgent, SETTINGS);
}
} finally {
initializeLock.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@
<description>Received by SMS or vocal message upon ongoing registration.</description>
<default></default>
</parameter>
<parameter name="userAgent" type="text" required="false">
<label>User Agent</label>
<description>Overwrite the user agent sent to Signal Server</description>
<advanced>true</advanced>
</parameter>
</config-description>
</bridge-type>

Expand Down

0 comments on commit 8ede55b

Please sign in to comment.