Skip to content

Commit

Permalink
[smsmodem] Add encoding parameter
Browse files Browse the repository at this point in the history
For non latin character in SMS

Signed-off-by: Gwendal Roulleau <[email protected]>
  • Loading branch information
dalgwen committed Jun 20, 2022
1 parent c1455ee commit 89c5a03
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 27 deletions.
8 changes: 8 additions & 0 deletions bundles/org.openhab.binding.smsmodem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ The *smsconversation* thing is just a shortcut to address/receive messages with
|-------|--------------------------|
| recipient | The msisdn of the phone you want to discuss with.|
| deliveryReport | If enabled, ask the network for a delivery report (default false)|
| encoding | The encoding to use when sending the message (either Enc7, Enc8, EncUcs2, EncCustom, default is Enc7). EncUcs2 is good for non latin character, but SMS character size limit is then reduced|


```
Thing smsmodem:smsconversation:aconversationname [ recipient="XXXXXXXXXXX", deliveryReport="true" ]
Expand Down Expand Up @@ -90,6 +92,12 @@ Once this action instance is retrieved, you can invoke the 'send' method on it:
smsAction.sendSMS("1234567890", "Hello world!")
```

Or with a special encoding:

```
smsAction.sendSMS("1234567890", "Hello world!", "EncUcs2")
```

## Full Example

### Send SMS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ public class SMSConversationConfiguration {

public String recipient = "";
public boolean deliveryReport = false;
public String encoding = "Enc7";
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.openhab.core.thing.binding.ThingHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smslib.message.AbstractMessage.Encoding;

/**
* The {@link SMSModemActions} exposes some actions
Expand All @@ -46,22 +47,35 @@ public void setThingHandler(@Nullable ThingHandler handler) {
return handler;
}

@RuleAction(label = "Send Message", description = "Send a message")
@RuleAction(label = "Send Message With Special Encoding", description = "Send a message and specify encoding")
public void sendSMS(
@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) {
@ActionInput(name = "message", label = "message", description = "Message to send") @Nullable String message,
@ActionInput(name = "encoding", label = "encoding", description = "Encoding") @Nullable String encoding) {
if (recipient != null && !recipient.isEmpty() && message != null) {
handler.send(recipient, message, false);
handler.send(recipient, message, false, encoding);
} else {
logger.error("SMSModem cannot send a message with no recipient or text");
}
}

public static void sendSMS(@Nullable ThingActions actions, @Nullable String recipient, @Nullable String message) {
@RuleAction(label = "Send Message", description = "Send a message")
public void sendSMS(
@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) {
sendSMS(recipient, message, Encoding.Enc7.toString());
}

public static void sendSMS(@Nullable ThingActions actions, @Nullable String recipient, @Nullable String message,
@Nullable String encoding) {
if (actions instanceof SMSModemActions) {
((SMSModemActions) actions).sendSMS(recipient, message);
((SMSModemActions) actions).sendSMS(recipient, message, encoding);
} else {
throw new IllegalArgumentException("Instance is not an SMSModemActions class.");
}
}

public static void sendSMS(@Nullable ThingActions actions, @Nullable String recipient, @Nullable String message) {
sendSMS(actions, recipient, message, Encoding.Enc7.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void handleCommand(ChannelUID channelUID, Command command) {
public void send(String text) {
SMSModemBridgeHandler bridgeHandlerFinal = bridgeHandler;
if (bridgeHandlerFinal != null) {
bridgeHandlerFinal.send(getRecipient(), text, config.deliveryReport);
bridgeHandlerFinal.send(getRecipient(), text, config.deliveryReport, config.encoding);
} else {
logger.warn("Only channel 'send' in SMSConversation can receive command");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.smslib.callback.IDeviceInformationListener;
import org.smslib.callback.IInboundOutboundMessageListener;
import org.smslib.callback.IModemStatusListener;
import org.smslib.message.AbstractMessage.Encoding;
import org.smslib.message.DeliveryReportMessage;
import org.smslib.message.InboundMessage;
import org.smslib.message.MsIsdn;
Expand Down Expand Up @@ -265,8 +266,16 @@ public void messageReceived(InboundMessage message) {
* @param text The message content
* @param deliveryReport If we should ask the network for a delivery report
*/
public void send(String recipient, String text, boolean deliveryReport) {
public void send(String recipient, String text, boolean deliveryReport, @Nullable String encoding) {
OutboundMessage out = new OutboundMessage(recipient, text);
try {
if (encoding != null && !encoding.isEmpty()) {
Encoding encoding2 = Encoding.valueOf(encoding);
out.setEncoding(encoding2);
}
} catch (IllegalArgumentException e) {
logger.error("Encoding {} is not supported. Use Enc7, Enc8, EncUcs2, or EncCustom", encoding);
}
out.setRequestDeliveryReport(deliveryReport);
logger.debug("Sending message to {}", recipient);
modem.queue(out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ thing-type.smsmodem.smsmodembridge.description = This bridge represents a modem.

thing-type.config.smsmodem.smsconversation.deliveryReport.label = Delivery Report
thing-type.config.smsmodem.smsconversation.deliveryReport.description = Ask network for delivery report.
thing-type.config.smsmodem.smsconversation.encoding.label = Encoding
thing-type.config.smsmodem.smsconversation.encoding.description = Encoding for the message to send. Default Enc7.
thing-type.config.smsmodem.smsconversation.recipient.label = Recipient Number
thing-type.config.smsmodem.smsconversation.recipient.description = The SMS number of the recipient.
thing-type.config.smsmodem.smsmodembridge.baudOrNetworkPort.label = Baud Or Network Port
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
# binding

binding.smsmodem.name = Extension SMSModem
binding.smsmodem.description = Cette extension gère un modem GSM supportant les messages AT et connecté en série, ou exposé sur le réseau. Elle peut envoyer et recevoir des SMS
binding.smsmodem.description = Cette extension gère un modem GSM supportant les messages AT et connecté en série, ou exposé sur le réseau. Elle peut envoyer et recevoir des SMS

# thing types

thing-type.smsmodem.smsconversation.label = Conversation SMS
thing-type.smsmodem.smsconversation.description = Représente une conversation avec un correspondant.
thing-type.smsmodem.smsconversation.description = Représente une conversation avec un correspondant.
thing-type.smsmodem.smsmodembridge.label = SMSModem
thing-type.smsmodem.smsmodembridge.description = Un modem connecté en série ou par le réseau
thing-type.smsmodem.smsmodembridge.description = Un modem connecté en série ou par le réseau

# thing types config

thing-type.config.smsmodem.smsconversation.deliveryReport.label = Accusé de réception
thing-type.config.smsmodem.smsconversation.deliveryReport.description = Demande au réseau un accusé de réception.
thing-type.config.smsmodem.smsconversation.recipient.label = Numéro Du Correspondant
thing-type.config.smsmodem.smsconversation.recipient.description = Le numéro SMS du correspondant.
thing-type.config.smsmodem.smsmodembridge.baudOrNetworkPort.label = Taux (Baud) Ou Port Réseau
thing-type.config.smsmodem.smsmodembridge.baudOrNetworkPort.description = Taux de transmission, si modem connecté en série, ou port réseau si il est sur une autre machine du réseau.
thing-type.config.smsmodem.smsmodembridge.delayBetweenSend.description = Délai entre deux envois (en millisecondes). Peut être utile pour les modems lents.
thing-type.config.smsmodem.smsmodembridge.pollingInterval.description = Délai entre deux essais de récupération de message (in seconds).
thing-type.config.smsmodem.smsconversation.deliveryReport.label = Accusé de réception
thing-type.config.smsmodem.smsconversation.deliveryReport.description = Demande au réseau un accusé de réception.
thing-type.config.smsmodem.smsconversation.encoding.label = Encodage
thing-type.config.smsmodem.smsconversation.encoding.description = Encodage du message à envoyer. Défaut Enc7.
thing-type.config.smsmodem.smsconversation.recipient.label = Numéro Du Correspondant
thing-type.config.smsmodem.smsconversation.recipient.description = Le numéro SMS du correspondant.
thing-type.config.smsmodem.smsmodembridge.baudOrNetworkPort.label = Taux (Baud) Ou Port Réseau
thing-type.config.smsmodem.smsmodembridge.baudOrNetworkPort.description = Taux de transmission, si modem connecté en série, ou port réseau si il est sur une autre machine du réseau.
thing-type.config.smsmodem.smsmodembridge.delayBetweenSend.description = Délai entre deux envois (en millisecondes). Peut être utile pour les modems lents.
thing-type.config.smsmodem.smsmodembridge.pollingInterval.description = Délai entre deux essais de récupération de message (in seconds).
thing-type.config.smsmodem.smsmodembridge.serialPortOrIP.label = Addresse
thing-type.config.smsmodem.smsmodembridge.serialPortOrIP.description = Port série du modem, ou addresse IP si il est sur le réseau.
thing-type.config.smsmodem.smsmodembridge.serialPortOrIP.description = Port série du modem, ou addresse IP si il est sur le réseau.
thing-type.config.smsmodem.smsmodembridge.simPin.label = Code PIN
thing-type.config.smsmodem.smsmodembridge.simPin.description = Le code PIN (si nécessaire) de la carte SIM.
thing-type.config.smsmodem.smsmodembridge.simPin.description = Le code PIN (si nécessaire) de la carte SIM.

# channel types

channel-type.smsmodem.deliverystatus.label = Accusé De Réception
channel-type.smsmodem.deliverystatus.label = Accusé De Réception
channel-type.smsmodem.deliverystatus.description = Dernier statut de message (soit UNKNOWN, QUEUED, SENT, PENDING, DELIVERED, EXPIRED, ou FAILED)
channel-type.smsmodem.receive.label = Message Reçu
channel-type.smsmodem.receive.description = Dernier message reçu
channel-type.smsmodem.send.label = Message Envoyé
channel-type.smsmodem.send.description = Message à envoyer au correspondant
channel-type.smsmodem.smsmodemreceivetrigger.label = Message Reçu
channel-type.smsmodem.smsmodemreceivetrigger.description = Déclenché quand un message est réceptionné, sous la forme "<msisdn_sender>|<text>"
channel-type.smsmodem.receive.label = Message Reçu
channel-type.smsmodem.receive.description = Dernier message reçu
channel-type.smsmodem.send.label = Message Envoyé
channel-type.smsmodem.send.description = Message à envoyer au correspondant
channel-type.smsmodem.smsmodemreceivetrigger.label = Message Reçu
channel-type.smsmodem.smsmodemreceivetrigger.description = Déclenché quand un message est réceptionné, sous la forme "<msisdn_sender>|<text>"
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
<description>Ask network for delivery report.</description>
<default>false</default>
</parameter>
<parameter name="encoding" type="text" required="false">
<label>Encoding</label>
<options>
<option value="Enc7">Enc7</option>
<option value="Enc8">Enc8</option>
<option value="EncUcs2">EncUcs2</option>
<option value="EncCustom">EncCustom</option>
</options>
<description>Encoding for the message to send. Default Enc7</description>
<default>Enc7</default>
</parameter>
</config-description>
</thing-type>

Expand Down

0 comments on commit 89c5a03

Please sign in to comment.