bool ipc_client::ipc_connect(void) { struct sockaddr_un remote; int len; if (sfd != -1) return true; if ((sfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { supla_log(LOG_ERR, "Socket error %i", errno); return false; } remote.sun_family = AF_UNIX; snprintf(remote.sun_path, sizeof(remote.sun_path), "%s", scfg_string(CFG_IPC_SOCKET_PATH)); len = strnlen(remote.sun_path, 107) + sizeof(remote.sun_family); if (connect(sfd, (struct sockaddr *)&remote, len) == -1) { supla_log(LOG_ERR, "IPC connect error %i", errno); ipc_disconnect(); return false; } if (read() && strcmp(buffer, hello) == 0) return true; ipc_disconnect(); return false; }
void SuplaDeviceClass::onRegisterResult(TSD_SuplaRegisterDeviceResult *register_device_result) { switch(register_device_result->result_code) { case SUPLA_RESULTCODE_BAD_CREDENTIALS: supla_log(LOG_ERR, "Bad credentials!"); break; case SUPLA_RESULTCODE_TEMPORARILY_UNAVAILABLE: supla_log(LOG_NOTICE, "Temporarily unavailable!"); break; case SUPLA_RESULTCODE_LOCATION_CONFLICT: supla_log(LOG_ERR, "Location conflict!"); break; case SUPLA_RESULTCODE_CHANNEL_CONFLICT: supla_log(LOG_ERR, "Channel conflict!"); break; case SUPLA_RESULTCODE_TRUE: server_activity_timeout = register_device_result->activity_timeout; registered = 1; supla_log(LOG_DEBUG, "Registered and ready."); if ( server_activity_timeout != ACTIVITY_TIMEOUT ) { TDCS_SuplaSetActivityTimeout at; at.activity_timeout = ACTIVITY_TIMEOUT; srpc_dcs_async_set_activity_timeout(srpc, &at); } return; case SUPLA_RESULTCODE_DEVICE_DISABLED: supla_log(LOG_NOTICE, "Device is disabled!"); break; case SUPLA_RESULTCODE_LOCATION_DISABLED: supla_log(LOG_NOTICE, "Location is disabled!"); break; case SUPLA_RESULTCODE_DEVICE_LIMITEXCEEDED: supla_log(LOG_NOTICE, "Device limit exceeded!"); break; case SUPLA_RESULTCODE_GUID_ERROR: supla_log(LOG_NOTICE, "Incorrect device GUID!"); break; } Params.cb.svr_disconnect(); delay(5000); }
char gpio_port_init(unsigned char port_number, unsigned char in, char value) { char result = 0; char buffer[GPIO_BUFFER_SIZE]; const char val_str[] = "value"; if ( st_file_exists(gpio_get_file(buffer, port_number, val_str)) == 0 ) { #ifdef __GPIO_SIMULATE snprintf(buffer, GPIO_BUFFER_SIZE, "/supla_virtual_dev/gpio/gpio%d", port_number); if ( st_file_exists(buffer) == 0 ) { mkdir(buffer, 0755); gpio_set_value(port_number, 0); gpio_create_file(port_number, "value"); gpio_create_file(port_number, "direction"); gpio_create_file(port_number, "edge"); } snprintf(buffer, GPIO_BUFFER_SIZE, "%d", port_number); #else snprintf(buffer, GPIO_BUFFER_SIZE, "%d", port_number); gpio_write_file("/sys/class/gpio/export", buffer); #endif } if ( st_file_exists(gpio_get_file(buffer, port_number, val_str)) == 0 ) { supla_log(LOG_ERR, "GPIO %d does not exist.", port_number); return 0; } if ( in == 1 ) { result = gpio_set_params(buffer, port_number, "in", "both"); } else { result = gpio_set_params(buffer, port_number, "out", "none"); }; if ( result == 0 ) supla_log(LOG_ERR, "Can't set GPIO port parameters. Port number %d", port_number); return result; }
void client_loop_channel_value_update(void *_suplaclient, void *sthread, TSC_SuplaChannelValue *channel_value) { double temp; if ( channel_value->Id == 82 || channel_value->Id == 83 || channel_value->Id == 97 || channel_value->Id == 127 ) { memcpy(&temp, channel_value->value.value, sizeof(double)); supla_log(LOG_DEBUG, "Channel #%i: %f st.", channel_value->Id, temp); } else { supla_log(LOG_DEBUG, "Channel #%i: Value: online %i, value[0]: %i, sub_value[0]: %i, EOL: %i", channel_value->Id, channel_value->online, channel_value->value.value[0], channel_value->value.sub_value[0], channel_value->EOL); } }
void s_worker::execute(void *sthread) { if (!db->connect()) return; s_exec = q->get_job(); while (s_exec.id && !sthread_isterminated(sthread)) { if (db->set_fetched(s_exec.id)) q->mark_fetched(); s_worker_action *action = AbstractActionFactory::createByActionType(s_exec.action, this); if (action) { action->execute(); delete action; } else { db->set_result(s_exec.id, ACTION_EXECUTION_RESULT_CANCELLED); supla_log(LOG_ERR, "Action %i is not supported!", s_exec.action); } if (s_exec.action_param != NULL) free(s_exec.action_param); s_exec = q->get_job(); } q->raise_loop_event(); }
void user_init(void) { supla_log(LOG_DEBUG, "Starting"); wifi_status_led_uninstall(); supla_esp_cfg_init(); supla_esp_devconn_init(); supla_esp_gpio_init(); if ( supla_esp_cfg.LocationID == 0 || supla_esp_cfg.LocationPwd[0] == 0 || supla_esp_cfg.Server[0] == 0 || supla_esp_cfg.WIFI_PWD[0] == 0 || supla_esp_cfg.WIFI_SSID[0] == 0 ) { supla_esp_cfgmode_start(); return; } supla_esp_devconn_start(); }
void client_loop(void *user_data, void *sthread) { void *sclient = client_loop_init(sthread); if ( sclient == NULL ) { st_app_terminate = 1; return; } if ( user_data ) *(void**)user_data = sclient; while(sthread_isterminated(sthread) == 0) { supla_log(LOG_INFO, "Connecting..."); if ( 0 == supla_client_connect(sclient) ) { usleep(2000000); } else { while( sthread_isterminated(sthread) == 0 && supla_client_iterate(sclient, 10000000) == 1 ) { } } } if ( user_data ) *(void**)user_data =NULL; supla_client_free(sclient); }
void supla_esp_gpio_check_switch_cfgbtn(void *timer_arg) { char v = GPIO_INPUT_GET(GPIO_ID_PIN((int)timer_arg)); if ( v != switch_cfgbtn_last_state ) { if ( switch_cfgbtn_counter == 0 ) { os_timer_disarm(&supla_gpio_timer5); os_timer_setfn(&supla_gpio_timer5, supla_esp_gpio_reset_cfg_counter, NULL); os_timer_arm (&supla_gpio_timer5, 10000, false); } switch_cfgbtn_counter++; supla_log(LOG_DEBUG, "Switch counter: %i", switch_cfgbtn_counter); if ( switch_cfgbtn_counter >= 10 ) { supla_esg_gpio_cfg_pressed(); } else { supla_esg_gpio_manual_pressed(); } switch_cfgbtn_last_state = v; } switch_cfgbtn_state_check = 0; }
int SuplaDeviceClass::addChannel(int pin1, int pin2, bool hiIsLo, bool bistable) { if ( isInitialized(true) ) return -1; if ( Params.reg_dev.channel_count >= SUPLA_CHANNELMAXCOUNT ) { supla_log(LOG_DEBUG, "Channel limit exceeded"); return -1; } if ( bistable && ( pin1 == 0 || pin2 == 0 ) ) bistable = false; Params.reg_dev.channels[Params.reg_dev.channel_count].Number = Params.reg_dev.channel_count; channel_pin = (SuplaChannelPin*)realloc(channel_pin, sizeof(SuplaChannelPin)*(Params.reg_dev.channel_count+1)); channel_pin[Params.reg_dev.channel_count].pin1 = pin1; channel_pin[Params.reg_dev.channel_count].pin2 = pin2; channel_pin[Params.reg_dev.channel_count].hiIsLo = hiIsLo; channel_pin[Params.reg_dev.channel_count].bistable = bistable; channel_pin[Params.reg_dev.channel_count].time_left = 0; channel_pin[Params.reg_dev.channel_count].vc_time = 0; channel_pin[Params.reg_dev.channel_count].bi_time_left = 0; channel_pin[Params.reg_dev.channel_count].last_val = suplaDigitalRead(Params.reg_dev.channel_count, bistable ? pin2 : pin1); Params.reg_dev.channel_count++; return Params.reg_dev.channel_count-1; }
void devcfg_channel_cfg(const char* section, const char* name, const char* value) { const char *sec_name = "CHANNEL_"; int sec_name_len = strlen(sec_name); if ( strlen(section) <= sec_name_len || strncasecmp(section, sec_name, sec_name_len) != 0 ) return; if ( strlen(value) == 0 ) { supla_log(LOG_ERR, "Empty value in configuration file for key: %s", name); return; } unsigned char number = atoi(§ion[sec_name_len]); if ( strcasecmp(name, "type") == 0 ) { channelio_set_type(number, decode_channel_type(value)); } else if ( strcasecmp(name, "gpio1") == 0 ) { channelio_set_gpio1(number, atoi(value) % 255); } else if ( strcasecmp(name, "gpio2") == 0 ) { channelio_set_gpio2(number, atoi(value) % 255); } else if ( strcasecmp(name, "w1") == 0 && strlen(value) > 0 ) { channelio_set_w1(number, value); } }
void supla_arduino_on_remote_call_received(void *_srpc, unsigned _supla_int_t rr_id, unsigned _supla_int_t call_type, void *_sdc, unsigned char proto_version) { TsrpcReceivedData rd; char result; ((SuplaDeviceClass*)_sdc)->onResponse(); if ( SUPLA_RESULT_TRUE == ( result = srpc_getdata(_srpc, &rd, 0)) ) { switch(rd.call_type) { case SUPLA_SDC_CALL_VERSIONERROR: ((SuplaDeviceClass*)_sdc)->onVersionError(rd.data.sdc_version_error); break; case SUPLA_SD_CALL_REGISTER_DEVICE_RESULT: ((SuplaDeviceClass*)_sdc)->onRegisterResult(rd.data.sd_register_device_result); break; case SUPLA_SD_CALL_CHANNEL_SET_VALUE: ((SuplaDeviceClass*)_sdc)->channelSetValue(rd.data.sd_channel_new_value); break; case SUPLA_SDC_CALL_SET_ACTIVITY_TIMEOUT_RESULT: ((SuplaDeviceClass*)_sdc)->channelSetActivityTimeoutResult(rd.data.sdc_set_activity_timeout_result); break; } srpc_rd_free(&rd); } else if ( result == SUPLA_RESULT_DATA_ERROR ) { supla_log(LOG_DEBUG, "DATA ERROR!"); } }
bool database::get_oauth_user(char *access_token, int *OAuthUserID, int *UserID, int *expires_at) { MYSQL_STMT *stmt; MYSQL_BIND pbind[1]; memset(pbind, 0, sizeof(pbind)); bool result = false; pbind[0].buffer_type = MYSQL_TYPE_STRING; pbind[0].buffer = (char *)access_token; pbind[0].buffer_length = strnlen(access_token, 512); const char sql[] = "SELECT t.user_id, c.parent_id, t.expires_at FROM " "`supla_oauth_access_tokens` AS t, `supla_oauth_clients` AS c WHERE c.id " "= t.client_id AND c.parent_id != 0 AND t.expires_at > " "UNIX_TIMESTAMP(NOW()) AND t.scope = 'restapi' AND token = ? LIMIT 1"; if (stmt_execute((void **)&stmt, sql, pbind, 1, true)) { mysql_stmt_store_result(stmt); if (mysql_stmt_num_rows(stmt) > 0) { MYSQL_BIND rbind[3]; memset(rbind, 0, sizeof(rbind)); int _OAuthUserID, _UserID, _expires_at; rbind[0].buffer_type = MYSQL_TYPE_LONG; rbind[0].buffer = (char *)&_OAuthUserID; rbind[1].buffer_type = MYSQL_TYPE_LONG; rbind[1].buffer = (char *)&_UserID; rbind[2].buffer_type = MYSQL_TYPE_LONG; rbind[2].buffer = (char *)&_expires_at; if (mysql_stmt_bind_result(stmt, rbind)) { supla_log(LOG_ERR, "MySQL - stmt bind error - %s", mysql_stmt_error(stmt)); } else if (mysql_stmt_fetch(stmt) == 0) { if (OAuthUserID != NULL) *OAuthUserID = _OAuthUserID; if (UserID != NULL) *UserID = _UserID; if (expires_at != NULL) { *expires_at = _expires_at; } result = true; } } mysql_stmt_free_result(stmt); mysql_stmt_close(stmt); } return result; }
void client_loop_channel_update(void *_suplaclient, void *sthread, TSC_SuplaChannel *channel) { double temp; if ( channel->Func == 40 ) { memcpy(&temp, channel->value.value, sizeof(double)); supla_log(LOG_DEBUG, "-> Channel #%i: %f st. EOL=%i", channel->Id, temp, channel->EOL); } else { supla_log(LOG_DEBUG, "Channel #%i: %s LocationID=%i, Function=%i, online=%i, value[0]: %i sub_value[0]: %i, EOL=%i", channel->Id, channel->Caption, channel->LocationID, channel->Func, channel->online, channel->value.value[0], channel->value.sub_value[0], channel->EOL); } }
bool SuplaDeviceClass::isInitialized(bool msg) { if ( srpc != NULL ) { if ( msg ) supla_log(LOG_DEBUG, "SuplaDevice is already initialized"); return true; } return false; }
unsigned char scfg_load(int argc, char* argv[], char default_file[]) { int a; char *cfg_path = NULL; for(a=0;a<argc;a++) { if ( strcmp("-c", argv[a]) == 0 && a<argc-1 ) { cfg_path = argv[a+1]; a++; } else if ( strcmp("-p", argv[a]) == 0 && a<argc-1 && strlen(argv[a+1]) < 1024 ) { pidfile_path = strdup(argv[a+1]); a++; } else if ( strcmp("-d", argv[a]) == 0 ) { run_as_daemon = 1; } else if ( strcmp("-D", argv[a]) == 0 ) { debug_mode = 1; } else if ( strcmp("-h", argv[a]) == 0 ) { scfg_print_help(argv[0]); return 0; } } if ( cfg_path == NULL ) { cfg_path = default_file; } if ( st_file_exists(cfg_path) == 0 ) { supla_log(LOG_ERR, "Configuration file not found: %s", cfg_path == NULL ? "" : cfg_path); return 0; } if ( ini_parse(cfg_path, scfg_ini_handler, NULL) < 0 ) { supla_log(LOG_ERR, "Config read error"); return 0; } return 1; }
void supla_client_on_version_error(TSuplaClientData *scd, TSDC_SuplaVersionError *version_error) { supla_log(LOG_ERR, "Protocol version error. Server doesn't support this client. S:%d-%d/C:%d", version_error->server_version_min, version_error->server_version, SUPLA_PROTO_VERSION); if ( scd->cfg.cb_on_versionerror ) scd->cfg.cb_on_versionerror(scd, scd->cfg.user_data, SUPLA_PROTO_VERSION, version_error->server_version_min, version_error->server_version); supla_client_disconnect(scd); }
void database::get_client_locations(int ClientID, supla_client_locations *locs) { MYSQL_STMT *stmt; const char sql[] = "SELECT `id`, `caption` FROM `supla_v_client_location` WHERE " "`client_id` = ?"; MYSQL_BIND pbind[1]; memset(pbind, 0, sizeof(pbind)); pbind[0].buffer_type = MYSQL_TYPE_LONG; pbind[0].buffer = (char *)&ClientID; if (stmt_execute((void **)&stmt, sql, pbind, 1, true)) { my_bool is_null[2]; MYSQL_BIND rbind[2]; memset(rbind, 0, sizeof(rbind)); int id; unsigned long size; char caption[401]; // utf8 rbind[0].buffer_type = MYSQL_TYPE_LONG; rbind[0].buffer = (char *)&id; rbind[0].is_null = &is_null[0]; rbind[1].buffer_type = MYSQL_TYPE_STRING; rbind[1].buffer = caption; rbind[1].buffer_length = 401; rbind[1].length = &size; rbind[1].is_null = &is_null[1]; if (mysql_stmt_bind_result(stmt, rbind)) { supla_log(LOG_ERR, "MySQL - stmt bind error - %s", mysql_stmt_error(stmt)); } else { mysql_stmt_store_result(stmt); if (mysql_stmt_num_rows(stmt) > 0) { while (!mysql_stmt_fetch(stmt)) { caption[size] = 0; locs->add_location(id, caption); } } } mysql_stmt_close(stmt); } }
void gpio_create_file(unsigned char port_number, char *file) { char buffer[GPIO_BUFFER_SIZE]; int fd; fd = open(gpio_get_file(buffer, port_number, file), O_RDWR|O_CREAT, 0755); if ( fd != -1 ) { close(fd); } else { supla_log(LOG_DEBUG, "Can't create %s", buffer); } }
char gpio_set_value(unsigned char port_number, char value) { supla_log(LOG_DEBUG, "gpio_set_value port: %i, value: %i", port_number, value); char v[2]; char buffer[GPIO_BUFFER_SIZE]; if ( port_number == 0 ) return 0; v[0] = value == 49 || value == 1 ? 49 : 48; v[1] = 0; return gpio_write_file(gpio_get_file(buffer, port_number, "value"), v); }
void supla_client_on_register_result(TSuplaClientData *scd, TSC_SuplaRegisterClientResult *register_client_result) { if ( register_client_result->result_code == SUPLA_RESULTCODE_TRUE ) { supla_client_set_registered(scd, 1); scd->server_activity_timeout = register_client_result->activity_timeout; scd->client_id = register_client_result->ClientID; supla_log(LOG_DEBUG, "Registered."); if ( scd->cfg.cb_on_registered ) scd->cfg.cb_on_registered(scd, scd->cfg.user_data, register_client_result); } else { switch(register_client_result->result_code) { case SUPLA_RESULTCODE_BAD_CREDENTIALS: supla_log(LOG_ERR, "Bad credentials!"); break; case SUPLA_RESULTCODE_TEMPORARILY_UNAVAILABLE: supla_log(LOG_NOTICE, "Temporarily unavailable!"); break; case SUPLA_RESULTCODE_ACCESSID_DISABLED: supla_log(LOG_NOTICE, "Access Identifier is disabled!"); break; case SUPLA_RESULTCODE_CLIENT_DISABLED: supla_log(LOG_NOTICE, "Client is disabled!"); break; case SUPLA_RESULTCODE_CLIENT_LIMITEXCEEDED: supla_log(LOG_NOTICE, "Client limit exceeded!"); break; case SUPLA_RESULTCODE_GUID_ERROR: supla_log(LOG_NOTICE, "Incorrect client GUID!"); break; } if ( scd->cfg.cb_on_registererror ) scd->cfg.cb_on_registererror(scd, scd->cfg.user_data, register_client_result->result_code); supla_client_disconnect(scd); } }
void sproto_log_summary(void *spd_ptr) { if ( spd_ptr == NULL ) { supla_log(LOG_DEBUG, "SPROTO - Not initialized!"); return; } TSuplaProtoData *spd = (TSuplaProtoData *)spd_ptr; supla_log(LOG_DEBUG, "BUFFER IN"); supla_log(LOG_DEBUG, " size: %i", spd->in.size); supla_log(LOG_DEBUG, " data_size: %i", spd->in.data_size); supla_log(LOG_DEBUG, " begin_tag: %i", spd->in.begin_tag); supla_log(LOG_DEBUG, "BUFFER OUT"); supla_log(LOG_DEBUG, " size: %i", spd->out.size); supla_log(LOG_DEBUG, " data_size: %i", spd->out.data_size); }
void supla_client_on_remote_call_received(void *_srpc, unsigned int rr_id, unsigned int call_type, void *_scd, unsigned char proto_version) { TsrpcReceivedData rd; char result; TSuplaClientData *scd = (TSuplaClientData*)_scd; if ( SUPLA_RESULT_TRUE == ( result = srpc_getdata(_srpc, &rd, 0)) ) { switch(rd.call_type) { case SUPLA_SDC_CALL_VERSIONERROR: supla_client_on_version_error(scd, rd.data.sdc_version_error); break; case SUPLA_SC_CALL_REGISTER_CLIENT_RESULT: supla_client_on_register_result(scd, rd.data.sc_register_client_result); break; case SUPLA_SC_CALL_LOCATION_UPDATE: supla_client_location_update(scd, rd.data.sc_location, 1); break; case SUPLA_SC_CALL_LOCATIONPACK_UPDATE: supla_client_locationpack_update(scd, rd.data.sc_location_pack); break; case SUPLA_SC_CALL_CHANNEL_UPDATE: supla_client_channel_update(scd, rd.data.sc_channel, 1); break; case SUPLA_SC_CALL_CHANNELPACK_UPDATE: supla_client_channelpack_update(scd, rd.data.sc_channel_pack); break; case SUPLA_SC_CALL_CHANNEL_VALUE_UPDATE: supla_client_channel_value_update(scd, rd.data.sc_channel_value); break; case SUPLA_SC_CALL_EVENT: supla_client_on_event(scd, rd.data.sc_event); break; } srpc_rd_free(&rd); } else if ( result == SUPLA_RESULT_DATA_ERROR ) { supla_log(LOG_DEBUG, "DATA ERROR!"); } }
bool database::get_authkey_hash(int ID, char *buffer, unsigned int buffer_size, bool *is_null, const char *sql) { MYSQL_STMT *stmt = NULL; MYSQL_BIND pbind[1]; memset(pbind, 0, sizeof(pbind)); pbind[0].buffer_type = MYSQL_TYPE_LONG; pbind[0].buffer = (char *)&ID; bool result = false; if (stmt_execute((void **)&stmt, sql, pbind, 1, true)) { my_bool _is_null = false; unsigned long size = 0; MYSQL_BIND rbind[1]; memset(rbind, 0, sizeof(rbind)); rbind[0].buffer_type = MYSQL_TYPE_STRING; rbind[0].buffer = buffer; rbind[0].buffer_length = buffer_size; rbind[0].length = &size; rbind[0].is_null = &_is_null; if (mysql_stmt_bind_result(stmt, rbind)) { supla_log(LOG_ERR, "MySQL - stmt bind error - %s", mysql_stmt_error(stmt)); } else { mysql_stmt_store_result(stmt); if (mysql_stmt_num_rows(stmt) > 0 && !mysql_stmt_fetch(stmt) && buffer_size > size) { buffer[size] = 0; *is_null = _is_null > 0; result = true; } } mysql_stmt_close(stmt); } return result; }
void ICACHE_FLASH_ATTR supla_esp_gpio_state_ipreceived(void) { if ( supla_last_state == STATE_IPRECEIVED ) return; supla_last_state = STATE_IPRECEIVED; supla_log(LOG_DEBUG, "IP Received"); #if defined(LED_RED_PORT) && defined(LED_GREEN_PORT) && defined(LED_BLUE_PORT) supla_esp_gpio_set_led(0, 0, 1); #elif defined(LED_GREEN_PORT) && defined(LED_BLUE_PORT) supla_esp_gpio_led_blinking(LED_GREEN | LED_BLUE, 500); #elif defined(LED_RED_PORT) supla_esp_gpio_led_blinking(LED_RED, 500); #endif }
void sproto_buffer_dump(void *spd_ptr, unsigned char in) { _supla_int_t a; char *buffer; _supla_int_t size; TSuplaProtoData *spd = (TSuplaProtoData *)spd_ptr; if ( in != 0 ) { buffer = spd->in.buffer; size = spd->in.data_size; } else { buffer = spd->out.buffer; size = spd->out.data_size; } for(a=0;a<size;a++) supla_log(LOG_DEBUG, "%c [%i]", buffer[a], buffer[a]); }
void ICACHE_FLASH_ATTR supla_esp_gpio_state_connected(void) { if ( supla_last_state == STATE_CONNECTED ) return; supla_last_state = STATE_CONNECTED; supla_log(LOG_DEBUG, "Server connected"); #if defined(LED_RED_PORT) && defined(LED_GREEN_PORT) && defined(LED_BLUE_PORT) supla_esp_gpio_set_led(0, 1, 0); #else supla_esp_gpio_set_led(0, 0, 0); #endif #if defined(INPUT_PORT1) || defined(INPUT_PORT2) os_timer_setfn(&supla_gpio_timer2, supla_esp_gpio_enable_inputs, NULL); os_timer_arm (&supla_gpio_timer2, 1000, 0); #endif }
void supla_esg_gpio_relay_switch(char port) { char hi = supla_esp_gpio_is_hi(port) == 1 ? 0 : 1; supla_log(LOG_DEBUG, "supla_esg_gpio_relay_switch %i, %i", port, hi); #ifdef RELAY_STATE_RESTORE if ( RELAY1_PORT == port ) supla_esp_state.Relay1 = hi; #ifdef RELAY2_PORT else if ( RELAY2_PORT == port ) supla_esp_state.Relay2 = hi; #endif supla_esp_save_state(SAVE_STATE_DELAY); #endif if ( RELAY1_PORT == port ) { if ( supla_esp_gpio_relay1_hi(hi) == 1 ) { supla_esp_channel_value_changed(0, hi); } } else { #ifdef RELAY2_PORT if ( supla_esp_gpio_relay2_hi(hi) == 1 ) { supla_esp_channel_value_changed(1, hi); } #endif } }
void ICACHE_FLASH_ATTR supla_esp_gpio_state_disconnected(void) { if ( supla_last_state == STATE_DISCONNECTED ) return; supla_last_state = STATE_DISCONNECTED; supla_log(LOG_DEBUG, "Disconnected"); #if defined(INPUT_PORT1) || defined(INPUT_PORT2) supla_esp_gpio_init_led(); #endif #if defined(LED_RED_PORT) && defined(LED_GREEN_PORT) && defined(LED_BLUE_PORT) supla_esp_gpio_set_led(1, 0, 0); #elif defined(LED_GREEN_PORT) && defined(LED_BLUE_PORT) supla_esp_gpio_led_blinking(LED_GREEN, 500); #elif defined(LED_RED_PORT) supla_esp_gpio_led_blinking(LED_RED, 2000); #endif }
void SuplaDeviceClass::iterate(void) { if ( !isInitialized(false) ) return; if ( !Params.cb.svr_connected() ) { supla_log(LOG_DEBUG, "Not connected"); registered = 0; last_response = 0; ping_flag = false; if ( !Params.cb.svr_connect(Params.server, 2015) ) { supla_log(LOG_DEBUG, "Connection fail. Server: %s", Params.server); Params.cb.svr_disconnect(); delay(2000); return; } } unsigned long _millis = millis(); if ( registered == 0 ) { registered = -1; srpc_ds_async_registerdevice_b(srpc, &Params.reg_dev); supla_log(LOG_DEBUG, "Register in progress"); } else if ( registered == 1 ) { // PING if ( (_millis-last_response)/1000 >= (server_activity_timeout+10) ) { supla_log(LOG_DEBUG, "TIMEOUT"); Params.cb.svr_disconnect(); } else if ( ping_flag == false && (_millis-last_response)/1000 >= (server_activity_timeout-5) ) { ping_flag = true; srpc_dcs_async_ping_server(srpc); } } if ( last_iterate_time != 0 ) { unsigned long td = abs(_millis - last_iterate_time); for(int a=0; a<Params.reg_dev.channel_count; a++) { if ( channel_pin[a].bi_time_left != 0 ) { if ( td >= channel_pin[a].bi_time_left ) { suplaDigitalWrite(Params.reg_dev.channels[a].Number, channel_pin[a].pin1, channel_pin[a].hiIsLo ? HIGH : LOW); channel_pin[a].bi_time_left = 0; } else if ( channel_pin[a].bi_time_left > 0 ) { channel_pin[a].bi_time_left-=td; } } if ( channel_pin[a].time_left != 0 ) { if ( td >= channel_pin[a].time_left ) { channel_pin[a].time_left = 0; if ( Params.reg_dev.channels[a].Type == SUPLA_CHANNELTYPE_SENSORNO ) channel_pin[a].last_val = -1; else if ( Params.reg_dev.channels[a].Type == SUPLA_CHANNELTYPE_RELAY ) channelSetValue(a, 0, 0); } else if ( channel_pin[a].time_left > 0 ) { channel_pin[a].time_left-=td; } } if ( Params.reg_dev.channels[a].Type == SUPLA_CHANNELTYPE_RELAY && channel_pin[a].bistable ) if ( td >= channel_pin[a].vc_time ) { channel_pin[a].vc_time-=td; } else { uint8_t val = suplaDigitalRead(Params.reg_dev.channels[a].Number, channel_pin[a].pin2); if ( val != channel_pin[a].last_val ) { channel_pin[a].last_val = val; channel_pin[a].vc_time = 200; channelValueChanged(Params.reg_dev.channels[a].Number, val == HIGH ? 1 : 0); } } if ( Params.reg_dev.channels[a].Type == SUPLA_CHANNELTYPE_SENSORNO ) { uint8_t val = suplaDigitalRead(Params.reg_dev.channels[a].Number, channel_pin[a].pin1); if ( val != channel_pin[a].last_val ) { channel_pin[a].last_val = val; if ( channel_pin[a].time_left <= 0 ) { channel_pin[a].time_left = 500; channelValueChanged(Params.reg_dev.channels[a].Number, val == HIGH ? 1 : 0); } } } if ( Params.reg_dev.channels[a].Type == SUPLA_CHANNELTYPE_THERMOMETERDS18B20 && Params.cb.get_temperature != NULL ) { if ( channel_pin[a].time_left <= 0 ) { channel_pin[a].time_left = 10000; double val = Params.cb.get_temperature(a, channel_pin[a].last_val_dbl1); if ( val != channel_pin[a].last_val_dbl1 ) { channel_pin[a].last_val_dbl1 = val; channelDoubleValueChanged(a, val); } } } if ( ( Params.reg_dev.channels[a].Type == SUPLA_CHANNELTYPE_DHT11 || Params.reg_dev.channels[a].Type == SUPLA_CHANNELTYPE_DHT22 || Params.reg_dev.channels[a].Type == SUPLA_CHANNELTYPE_AM2302 ) && Params.cb.get_temperature_and_humidity != NULL ) { if ( channel_pin[a].time_left <= 0 ) { channel_pin[a].time_left = 10000; double t = channel_pin[a].last_val_dbl1; double h = channel_pin[a].last_val_dbl2; Params.cb.get_temperature_and_humidity(a, &t, &h); if ( t != channel_pin[a].last_val_dbl1 || h != channel_pin[a].last_val_dbl2 ) { channel_pin[a].last_val_dbl1 = t; channel_pin[a].last_val_dbl2 = h; channelSetTempAndHumidityValue(a, t, h); srpc_ds_async_channel_value_changed(srpc, a, Params.reg_dev.channels[a].value); } } } if ( Params.reg_dev.channels[a].Type == SUPLA_CHANNELTYPE_DISTANCESENSOR && Params.cb.get_distance != NULL ) { if ( channel_pin[a].time_left <= 0 ) { if ( channel_pin[a].time_left <= 0 ) { channel_pin[a].time_left = 1000; double val = Params.cb.get_distance(a, channel_pin[a].last_val_dbl1); if ( val != channel_pin[a].last_val_dbl1 ) { channel_pin[a].last_val_dbl1 = val; channelDoubleValueChanged(a, val); } } } } } } last_iterate_time = millis(); if( srpc_iterate(srpc) == SUPLA_RESULT_FALSE ) { supla_log(LOG_DEBUG, "Iterate fail"); Params.cb.svr_disconnect(); delay(5000); } }
void SuplaDeviceClass::onVersionError(TSDC_SuplaVersionError *version_error) { supla_log(LOG_ERR, "Protocol version error"); Params.cb.svr_disconnect(); delay(5000); }