boardcommands.cpp 2.05 KB
Newer Older
1 2 3 4 5

#include "boardcommands.h"
#include "mysensors.h"
#include "utils.h"

6 7
String _sensor_names[__SENSOR_COUNT] = __SENSOR_NAMES;
String _sensor_nicknames[__SENSOR_COUNT] = __SENSOR_NICKNAMES;
8
ReadSensorFP _fp_read_sensor[__SENSOR_COUNT] = __FP_READ_SENSOR;
9 10


11
String execute_board_command(String csv_line)
12 13 14 15
{
    String command = csv_line.substring(0, csv_line.indexOf(CSV_SEP));
    String csv_args = csv_line.substring(csv_line.indexOf(CSV_SEP) + 1);
    
16
    if (command == "read" && csv_args.length() > 0)
17 18 19
    {        
        return read_sensors(csv_args);
    }
20
    else if (command == "setrtc" && csv_args.length() > 0)
21 22 23
    {
        return set_time_from_csv(csv_args);
    }
24
    else if (command == "listall")
25 26 27 28 29 30
    {
        return get_sensor_names();
    }
    else if (command == "help")
    {
        String s = "";
31 32 33
        return s + String("read,<name1>,<name2>,...,<nameN>\n"
                          "setrtc,<year>,<month>,<day>,<hour>,<minute>,<second>\n"
                          "listall\n"
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
                          "help");
    }    
    else
    {
        return String("<invalid_command:'") + csv_line + String("'>");
    }
}


String read_sensors(String csv_line)
{
    int s=0, e=0;  // start,end string indexes
    String result_line="", name;
    while (e != -1)
    {
        e = csv_line.indexOf(CSV_SEP, s);
        name = (e != -1) ? csv_line.substring(s, e) : csv_line.substring(s);
        s = e+1;
        name.trim();
        result_line += call_read_sensor(name);
        if (e != -1) result_line += CSV_SEP;
    }
    return result_line;
}

String call_read_sensor(String name)
{
    for (int i=0; i < __SENSOR_COUNT; i++)
    {
        if (name == _sensor_names[i] || name == _sensor_nicknames[i])
64
            return _fp_read_sensor[i]();
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    }
    return String("<invalid_sensor_name:") + name + String(">");
}


String get_sensor_names()
{
    String s = "";
    for (int i=0; i < __SENSOR_COUNT; i++)
    {
        s += _sensor_names[i];
        s += ":";
        s += _sensor_nicknames[i];
        if (i < __SENSOR_COUNT - 1) s += ',';
    }
    return s;
}