aboutsummaryrefslogtreecommitdiff
path: root/main/settings.cpp
blob: 7f002e24cc6cf06a9f6c85e7e9e1661bcc5cd42d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "esp_mac.h"
#include "esp_log.h"
#include "esp_check.h"
#include "esp_wifi.h"

#include "relay.h"
#include "settings.h"
#include "utils.h"

static const char *TAG = "settings";

char* settings_sta_json(void)
{
    wifi_config_t wifi_cfg;
    ESP_ERROR_CHECK(esp_wifi_get_config(WIFI_IF_STA, &wifi_cfg));

    wifi_mode_t mode;
    esp_wifi_get_mode(&mode);

    char* resp = NULL;

    int rc = asprintf(
            &resp,
            "{"
                "\"enabled\":%s,"
                "\"ssid\":\"%s\","
                "\"ipv4_method\":\"dhcp\","
                "\"ip\":null,"
                "\"gw\":null,"
                "\"mask\":null,"
                "\"dns\":null"
            "}",
            mode == WIFI_MODE_STA ? "true" : "false",
            (const char*)wifi_cfg.sta.ssid
        );

    ESP_RETURN_ON_FALSE(resp && rc, NULL, TAG, "buffer alloc failed");

    return resp;
}

char* settings_relay_json(void)
{
    char* resp = NULL;

    int rc = asprintf(
            &resp,
            "{"
                "\"name\":null,"
                "\"appliance_type\":\"General\","
                "\"ison\":%s,"
                "\"has_timer\":false,"
                "\"default_state\":\"off\","
                "\"btn_type\":\"toggle\","
                "\"btn_reverse\":1,"
                "\"auto_on\":0,"
                "\"auto_off\":0,"
                "\"power\":0,"
                "\"schedule\":false,"
                "\"schedule_rules\":[]"
            "}",
            state ? "true" : "false"
        );

    ESP_RETURN_ON_FALSE(resp && rc, NULL, TAG, "buffer alloc failed");

    return resp;
}

char* settings_json(void)
{

    char* sta_data = settings_sta_json();
    if (!sta_data)
        sta_data = "{}";

    char* relay_data = settings_relay_json();
    if (!relay_data)
        relay_data = "{}";

    char* resp = NULL;

    int rc = asprintf(
            &resp,
            "{"
                "\"device\":{"
                    "\"type\":\"SHSW-%s\","
                    "\"mac\":\""MACFMT"\","
                    "\"hostname\":\"shelly%s-"MACFMT"\""
                "},"
                "\"wifi_ap\":{"
                    "\"enabled\":false,"
                    "\"ssid\":\"shelly%s-"MACFMT"\","
                    "\"key\":\"\""
                "},"
                "\"wifi_sta\":%s,"
                "\"wifi_sta1\":{"
                    "\"enabled\":false,"
                    "\"ssid\":null,"
                    "\"ipv4_method\":\"dhcp\","
                    "\"ip\":null,"
                    "\"gw\":null,"
                    "\"mask\":null,"
                    "\"dns\":null"
                "},"
                "\"ap_roaming\":{"
                    "\"enabled\":false,"
                    "\"threshold\":-70"
                "},"
                "\"mqtt\":{"
                    "\"enable\":false,"
                    "\"server\":\"192.168.33.3:1883\","
                    "\"user\":\"\","
                    "\"id\":\"shelly%s-"MACFMT"\","
                    "\"reconnect_timeout_max\":60,"
                    "\"reconnect_timeout_min\":2,"
                    "\"clean_session\":true,"
                    "\"keep_alive\":60,"
                    "\"max_qos\":0,"
                    "\"retain\":false,"
                    "\"update_period\":30"
                "},"
                "\"coiot\":{"
                    "\"enabled\":true,"
                    "\"update_period\":15,"
                    "\"peer\":\"\""
                "},"
                "\"sntp\":{"
                    "\"server\":\"time.google.com\","
                    "\"enabled\":false"
                "},"
                "\"login\":{"
                    "\"enabled\":false,"
                    "\"unprotected\":false,"
                    "\"username\":\"admin\""
                "},"
                "\"pin_code\":\"123456\","
                "\"name\":\"shelly%s-"MACFMT"\","
                "\"fw\":\"20170427-114337/master@79dbb397\","
                "\"discoverable\":true,"
                "\"build_info\":{"
                    "\"build_id\":\"20191112-140800\","
                    "\"build_timestamp\":\"2019-11-12T14:08:00Z\","
                    "\"build_version\":\"1.0\""
                "},"
                "\"cloud\":{"
                    "\"enabled\":false,"
                    "\"connected\":false"
                "},"
                "\"timezone\":\"Europe/Sofia\","
                "\"lat\":42.1234,"
                "\"lng\":24.5678,"
                "\"tzautodetect\":true,"
                "\"tz_utc_offset\":0,"
                "\"tz_dst\":false,"
                "\"tz_dst_auto\":true,"
                "\"time\":\"16:40\","
                "\"unixtime\":0,"
                "\"led_status_disable\":false,"
                "\"debug_enable\":false,"
                "\"allow_cross_origin\":false,"
                "\"wifirecovery_reboot_enabled\":true,"

                "\"factory_reset_from_switch\":false,"
                "\"mode\":\"relay\","
                "\"longpush_time\":1000,"
                "\"actions\":{"
                    "\"active\":false,"
                    "\"names\":[]"
                "},"
                "\"relays\":["
                    "%s"
                "],"
                "\"ext_sensors\":{},"
                "\"ext_temperature\":{},"
                "\"ext_humidity\":{},"
                "\"ext_switch_enable\":1,"
                "\"ext_switch\":{"
                  "\"0\":{"
                    "\"relay_num\":0"
                  "}"
                "}"
            "}",
            CONFIG_SHELLSP_MODEL, MAC2STR(get_wifi_mac()),
            CONFIG_SHELLSP_MODEL, MAC2STR(get_wifi_mac()),
            CONFIG_SHELLSP_MODEL, MAC2STR(get_wifi_mac()),
            sta_data,
            CONFIG_SHELLSP_MODEL, MAC2STR(get_wifi_mac()),
            CONFIG_SHELLSP_MODEL, MAC2STR(get_wifi_mac()),
            relay_data
        );

    ESP_RETURN_ON_FALSE(resp && rc, NULL, TAG, "buffer alloc failed");

    return resp;
}