mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-27 21:26:43 +00:00
Support executing shell commands on PC build
This commit is contained in:
parent
4fff3d79c5
commit
603d38ae3c
@ -246,6 +246,12 @@ bool PosixDevice::is_system_pin(uint8_t pin)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Win32Device::run_thread(void (*func)(void*), void* arg)
|
||||||
|
{
|
||||||
|
pthread_t thread;
|
||||||
|
pthread_create(&thread, NULL, (void* (*)(void*))func, arg);
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef TARGET_OS_MAC
|
#ifndef TARGET_OS_MAC
|
||||||
long PosixDevice::get_uptime()
|
long PosixDevice::get_uptime()
|
||||||
{
|
{
|
||||||
|
@ -58,6 +58,8 @@ class PosixDevice : public BaseDevice {
|
|||||||
|
|
||||||
bool is_system_pin(uint8_t pin) override;
|
bool is_system_pin(uint8_t pin) override;
|
||||||
|
|
||||||
|
void run_thread(void (*func)(void*), void* arg);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::string backlight_device;
|
std::string backlight_device;
|
||||||
int backlight_max = 0;
|
int backlight_max = 0;
|
||||||
|
@ -187,6 +187,11 @@ bool Win32Device::is_system_pin(uint8_t pin)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Win32Device::run_thread(void (*func)(void*), void* arg)
|
||||||
|
{
|
||||||
|
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)func, arg, 0, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
long Win32Device::get_uptime()
|
long Win32Device::get_uptime()
|
||||||
{
|
{
|
||||||
return GetTickCount64() / 1000;
|
return GetTickCount64() / 1000;
|
||||||
|
@ -45,6 +45,8 @@ class Win32Device : public BaseDevice {
|
|||||||
|
|
||||||
bool is_system_pin(uint8_t pin) override;
|
bool is_system_pin(uint8_t pin) override;
|
||||||
|
|
||||||
|
void run_thread(void (*func)(void*), void* arg);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string _hostname;
|
std::string _hostname;
|
||||||
std::string _core_version;
|
std::string _core_version;
|
||||||
|
@ -822,6 +822,48 @@ void dispatch_run_script(const char*, const char* payload, uint8_t source)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if HASP_TARGET_PC
|
||||||
|
static void shell_command_thread(char* cmdline)
|
||||||
|
{
|
||||||
|
// run the command
|
||||||
|
FILE* pipe = popen(cmdline, "r");
|
||||||
|
// free the string duplicated previously
|
||||||
|
free(cmdline);
|
||||||
|
if(!pipe) {
|
||||||
|
LOG_ERROR(TAG_MSGR, F("Couldn't execute system command"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// read each line, up to 1023 chars long
|
||||||
|
char command[1024];
|
||||||
|
while(fgets(command, sizeof(command), pipe) != NULL) {
|
||||||
|
// strip newline character
|
||||||
|
char* temp = command;
|
||||||
|
while(*temp) {
|
||||||
|
if(*temp == '\r' || *temp == '\n') {
|
||||||
|
*temp = '\0';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
temp++;
|
||||||
|
}
|
||||||
|
// run the command
|
||||||
|
LOG_INFO(TAG_MSGR, F("Running '%s'"), command);
|
||||||
|
dispatch_text_line(command, TAG_MSGR);
|
||||||
|
}
|
||||||
|
// close the pipe, check return code
|
||||||
|
int status_code = pclose(pipe);
|
||||||
|
if(status_code) {
|
||||||
|
LOG_ERROR(TAG_MSGR, F("Process exited with non-zero return code %d"), status_code);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dispatch_shell_execute(const char*, const char* payload, uint8_t source)
|
||||||
|
{
|
||||||
|
// must duplicate the string for thread's own usage
|
||||||
|
char* command = strdup(payload);
|
||||||
|
haspDevice.run_thread((void (*)(void*))shell_command_thread, (void*)command);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void dispatch_current_page()
|
void dispatch_current_page()
|
||||||
{
|
{
|
||||||
char topic[8];
|
char topic[8];
|
||||||
@ -1495,6 +1537,9 @@ void dispatchSetup()
|
|||||||
dispatch_add_command(PSTR("sensors"), dispatch_send_sensordata);
|
dispatch_add_command(PSTR("sensors"), dispatch_send_sensordata);
|
||||||
dispatch_add_command(PSTR("theme"), dispatch_theme);
|
dispatch_add_command(PSTR("theme"), dispatch_theme);
|
||||||
dispatch_add_command(PSTR("run"), dispatch_run_script);
|
dispatch_add_command(PSTR("run"), dispatch_run_script);
|
||||||
|
#if HASP_TARGET_PC
|
||||||
|
dispatch_add_command(PSTR("shell"), dispatch_shell_execute);
|
||||||
|
#endif
|
||||||
dispatch_add_command(PSTR("service"), dispatch_service);
|
dispatch_add_command(PSTR("service"), dispatch_service);
|
||||||
dispatch_add_command(PSTR("antiburn"), dispatch_antiburn);
|
dispatch_add_command(PSTR("antiburn"), dispatch_antiburn);
|
||||||
dispatch_add_command(PSTR("calibrate"), dispatch_calibrate);
|
dispatch_add_command(PSTR("calibrate"), dispatch_calibrate);
|
||||||
|
@ -108,7 +108,7 @@ static void console_process_line(const char* input)
|
|||||||
}
|
}
|
||||||
#elif HASP_TARGET_PC
|
#elif HASP_TARGET_PC
|
||||||
static bool console_running = true;
|
static bool console_running = true;
|
||||||
static int console_thread(void* arg)
|
static void console_thread(void* arg)
|
||||||
{
|
{
|
||||||
while(console_running) {
|
while(console_running) {
|
||||||
std::string input;
|
std::string input;
|
||||||
@ -145,12 +145,7 @@ void consoleStart()
|
|||||||
}
|
}
|
||||||
#elif HASP_TARGET_PC
|
#elif HASP_TARGET_PC
|
||||||
LOG_TRACE(TAG_MSGR, F(D_SERVICE_STARTING));
|
LOG_TRACE(TAG_MSGR, F(D_SERVICE_STARTING));
|
||||||
#if defined(WINDOWS)
|
haspDevice.run_thread(console_thread, NULL);
|
||||||
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)console_thread, NULL, 0, NULL);
|
|
||||||
#elif defined(POSIX)
|
|
||||||
pthread_t thread;
|
|
||||||
pthread_create(&thread, NULL, (void* (*)(void*))console_thread, NULL);
|
|
||||||
#endif
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,7 +197,7 @@ IRAM_ATTR void consoleLoop()
|
|||||||
case 0:
|
case 0:
|
||||||
case -1:
|
case -1:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default: {
|
default: {
|
||||||
update = true;
|
update = true;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user