SDK Script dio-monitor.are

dio-monitor.are
/* DESC: This script monitors the DIO ports and sends a SMS to the specified phone number.
 * Copyright (C) 2012 NetModule AG, Switzerland
 */
 
void usage()
{
    print("dio-monitor.are <din> <tel>\n");
    print("  din:  digital input [in1|in2]\n");
    print("  tel:  telephone number to send the SMS to\n");
    exit(1);
}
 
void sendsms(tel, msg)
{
    id = nb_sms_send(tel, msg);
    if (id) {
        printf("Successfully spooled SMS (ID %s) to '%s'.\n", id, tel);
    } else {
        print("Unable to send SMS to '%s'.\n", tel);
    }
}
 
if (argc != 3) {
    usage();
}
 
din = argv[1];
tel = argv[2];
 
in1 = nb_dio_get("in1");
in2 = nb_dio_get("in2");
 
while (1) {
    if (left(din,4) == "in1") {
        cin1 = nb_dio_get("in1");
        if (cin1 != in1) {
            msg = sprintf("Digital input state of '%s' has changed from %d to %d\n", din, in1, cin1);
            sendsms(tel, msg);
            in1 = cin1;
        }
    }
    if (left(din,4) == "in2") {
        cin2 = nb_dio_get("in2");
        if (cin2 != in2) {
            msg = sprintf("Digital input state of '%s' has changed from %d to %d\n", din, in2, cin2);
            sendsms(tel, msg);
            in2 = cin2;
        }
    }
    sleep(3);
}
 
exit(0);