Merge pull request #4 from MilosKozak/2186

More SMS commands
This commit is contained in:
Fabrizio Casellato 2019-11-22 14:42:30 +01:00 committed by GitHub
commit aab9d695fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 7 deletions

View file

@ -46,9 +46,6 @@ import org.slf4j.LoggerFactory
import java.text.Normalizer
import java.util.*
/**
* Created by mike on 05.08.2016.
*/
object SmsCommunicatorPlugin : PluginBase(PluginDescription()
.mainType(PluginType.GENERAL)
.fragmentClass(SmsCommunicatorFragment::class.java.name)
@ -64,6 +61,23 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription()
var lastRemoteBolusTime: Long = 0
var messages = ArrayList<Sms>()
val commands = mapOf(
"BG" to "BG",
"LOOP" to "LOOP STOP/DISABLE/START/ENABLE/RESUME/STATUS\nLOOP SUSPEND 20",
"TREATMENTS" to "TREATMENTS REFRESH",
"NSCLIENT" to "NSCLIENT RESTART",
"PUMP" to "PUMP",
"BASAL" to "BASAL STOP/CANCEL\nBASAL 0.3\nBASAL 0.3 20\nBASAL 30%\nBASAL 30% 20\n",
"BOLUS" to "BOLUS 1.2\nBOLUS 1.2 MEAL",
"EXTENDED" to "EXTENDED STOP/CANCEL\nEXTENDED 2 120",
"CAL" to "CAL 5.6",
"PROFILE" to "PROFILE STATUS/LIST\nPROFILE 1\nPROFILE 2 30",
"TARGET" to "TARGET MEAL/ACTIVITY/HYPO/STOP",
"SMS" to "SMS DISABLE/STOP",
"CARBS" to "CARBS 12\nCARBS 12 23:05\nCARBS 12 11:05PM",
"HELP" to "HELP\nHELP command"
)
init {
processSettings(null)
}
@ -135,10 +149,11 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription()
}
fun isCommand(command: String, number: String): Boolean {
when (command.toUpperCase(Locale.getDefault())) {
"BG", "LOOP", "TREATMENTS", "NSCLIENT", "PUMP", "BASAL", "BOLUS", "EXTENDED", "CAL", "PROFILE", "TARGET", "SMS", "CARBS" -> return true
var found = false
commands.forEach { (k, _) ->
if (k == command) found = true
}
return messageToConfirm?.requester?.phoneNumber == number
return found || messageToConfirm?.requester?.phoneNumber == number
}
fun isAllowedNumber(number: String): Boolean {
@ -226,6 +241,9 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription()
if (!remoteCommandsAllowed) sendSMS(Sms(receivedSms.phoneNumber, R.string.smscommunicator_remotecommandnotallowed))
else if (splitted.size == 2) processSMS(splitted, receivedSms)
else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat))
"HELP" ->
if (splitted.size == 1 || splitted.size == 2) processHELP(splitted, receivedSms)
else sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat))
else ->
if (messageToConfirm?.requester?.phoneNumber == receivedSms.phoneNumber) {
messageToConfirm?.action(splitted[0])
@ -365,6 +383,17 @@ object SmsCommunicatorPlugin : PluginBase(PluginDescription()
sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat))
}
private fun processHELP(splitted: Array<String>, receivedSms: Sms) {
if (splitted.size == 1) {
sendSMS(Sms(receivedSms.phoneNumber, commands.keys.toString().replace("[", "").replace("]", "")))
receivedSms.processed = true
} else if (isCommand(splitted[1].toUpperCase(Locale.getDefault()), receivedSms.phoneNumber)) {
sendSMS(Sms(receivedSms.phoneNumber, commands[splitted[1].toUpperCase(Locale.getDefault())]))
receivedSms.processed = true
} else
sendSMS(Sms(receivedSms.phoneNumber, R.string.wrongformat))
}
private fun processPUMP(receivedSms: Sms) {
ConfigBuilderPlugin.getPlugin().commandQueue.readStatus("SMS", object : Callback() {
override fun run() {

View file

@ -348,6 +348,20 @@ public class SmsCommunicatorPluginTest {
Assert.assertEquals("PUMP", smsCommunicatorPlugin.getMessages().get(0).text);
Assert.assertEquals("Virtual Pump", smsCommunicatorPlugin.getMessages().get(1).text);
//HELP
smsCommunicatorPlugin.setMessages(new ArrayList<>());
sms = new Sms("1234", "HELP");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("HELP", smsCommunicatorPlugin.getMessages().get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.getMessages().get(1).text.contains("PUMP"));
//HELP PUMP
smsCommunicatorPlugin.setMessages(new ArrayList<>());
sms = new Sms("1234", "HELP PUMP");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("HELP PUMP", smsCommunicatorPlugin.getMessages().get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.getMessages().get(1).text.contains("PUMP"));
//SMS : wrong format
smsCommunicatorPlugin.setMessages(new ArrayList<>());
sms = new Sms("1234", "SMS");
@ -796,7 +810,7 @@ public class SmsCommunicatorPluginTest {
sms = new Sms("1234", "CARBS 1");
smsCommunicatorPlugin.processSms(sms);
Assert.assertEquals("CARBS 1", smsCommunicatorPlugin.getMessages().get(0).text);
Assert.assertTrue(smsCommunicatorPlugin.getMessages().get(1).text.contains("To enter 1g at 01:16AM reply with code"));
Assert.assertTrue(smsCommunicatorPlugin.getMessages().get(1).text.contains("To enter 1g at"));
String passCode = smsCommunicatorPlugin.getMessageToConfirm().confirmCode;
smsCommunicatorPlugin.processSms(new Sms("1234", passCode));
Assert.assertEquals(passCode, smsCommunicatorPlugin.getMessages().get(2).text);