brewpanel/brewpanel.c

Mon, 15 Apr 2024 17:04:57 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Mon, 15 Apr 2024 17:04:57 +0200
changeset 678
cc49115e769e
parent 638
186f0c2d3e76
permissions
-rw-r--r--

Better websocket broadcast messages. Added GLOBAL JSON command to the server. Better logic to trigger websocket and mqtt data updates for the fermenter units. Websocket receive added fermenter mode, stage, setpoints, switches. Added more css styles for the fermenter screen. Added the fermenter screen php and javascript.

/*****************************************************************************
 * Copyright (C) 2015-2024
 *   
 * Michiel Broek <mbroek at mbse dot eu>
 *
 * This file is part of the mbsePi-apps
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * mbsePi-apps is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ThermFerm; see the file COPYING.  If not, write to the Free
 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 *****************************************************************************/

#include "brewpanel.h"
#include "sdlgui.h"
#include "dlgBrew.h"

#ifdef HAVE_SDL2_SDL_H

int			debug = FALSE;			/* Console debugging		*/
static pid_t		pgrp, mypid;
int			my_shutdown = FALSE;		/* Shutdown requested		*/
char			*Paneltype = NULL;		/* Panel to emulate		*/
int			PAN_x;				/* Screen X size		*/
int			PAN_y;				/* Screen Y size		*/

int			LCD_fcol;			/* LCD foreground color		*/
int			LCD_bcol;			/* LCD background color		*/

SDL_Surface		*S_screen = NULL;		/* Main screen			*/
SDL_Window		*S_window;			/* Main window			*/

int server(void);

void help(void)
{
    fprintf(stdout, "Usage: brewpanel [-d] [-h] [-p <paneltype>]\n");
    fprintf(stdout, "  -d --debug                    Debug on\n");
    fprintf(stdout, "  -h --help                     Display this help\n");
    fprintf(stdout, "  -p --paneltype <ferm|brew>    Select panel type\n");
}



void die(int onsig)
{
    switch (onsig) {
	case SIGHUP:    syslog(LOG_NOTICE, "Got SIGHUP, shutting down");
			break;
	case SIGINT:    syslog(LOG_NOTICE, "Keyboard interrupt, shutting down");
			break;
	case SIGTERM:   syslog(LOG_NOTICE, "Got SIGTERM, shutting down");
			break;
	case SIGSEGV:   syslog(LOG_NOTICE, "Got SIGSEGV, shutting down");
			my_shutdown = TRUE;
			exit(SIGSEGV);
			break;
	default:        syslog(LOG_NOTICE, "die() on signal %d", onsig);
    }

    my_shutdown = TRUE;
}



int main(int argc, char *argv[])
{
    int		i, c, rc = 0;
    pid_t	frk;

    Paneltype = calloc(128, sizeof(char));

    while (1) {
	int option_index = 0;
	static struct option long_options[] = {
	    {"debug", 0, 0, 'c'},
	    {"help", 0, 0, 'h'},
	    {"paneltype", 1, 0, 'p'},
	    {0, 0, 0, 0}
	};

	c = getopt_long(argc, argv, "dhp:", long_options, &option_index);
	if (c == -1)
	    break;

	switch (c) {
	    case 'd':   debug = TRUE;
			break;
	    case 'h':   help();
			return 1;
	    case 'p':   snprintf(Paneltype, 127, "%s", optarg);
			break;
	}
    }

    openlog("brewpanel", LOG_PID|LOG_CONS|LOG_NOWAIT, LOG_USER);
    syslog(LOG_NOTICE, "mbsePi-apps brewpanel v%s starting", VERSION);

    /*
     *  Catch all the signals we can, and ignore the rest. Note that SIGKILL can't be ignored
     *  but that's live. This daemon should only be stopped by SIGTERM.
     *  Don't catch SIGCHLD.
     */
    for (i = 0; i < NSIG; i++) {
	if ((i != SIGCHLD) && (i != SIGKILL) && (i != SIGSTOP))
	    signal(i, (void (*))die);
    }

    if (debug) {
	/*
	 * Run in foreground when debugging
	 */
    	rc = server();
    } else {
	/*
	 * Fork the daemon
	 */
	if ((pgrp = setpgid(0, 0)) == -1) {
	    syslog(LOG_NOTICE, "setpgpid failed");
	}

	frk = fork();
	switch (frk) {
	    case -1:
		    	syslog(LOG_NOTICE, "Daemon fork failed: %s", strerror(errno));
			exit(1);
	    case 0:	/*
			 * Run the daemon
			 */
			fclose(stdin);
			if (open("/dev/null", O_RDONLY) != 0) {
			    syslog(LOG_NOTICE, "Reopen of stdin to /dev/null failed");
			    _exit(2);
			}
			fclose(stdout);
			if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 1) {
			    syslog(LOG_NOTICE, "Reopen of stdout to /dev/null failed");
			    _exit(2);
			}
			fclose(stderr);
			if (open("/dev/null", O_WRONLY | O_APPEND | O_CREAT,0600) != 2) {
			    syslog(LOG_NOTICE, "Reopen of stderr to /dev/null failed");
			    _exit(2);
			}
			mypid = getpid();
			rc = server();
			break;
			/* Not reached */
	    default:
			/*
			 * Here we detach this process and let the child
			 * run the deamon process.
			 */
			syslog(LOG_NOTICE, "Starting daemon with pid %d", frk);
			exit(0);
	}
    }

    syslog(LOG_NOTICE, "Finished, rc=%d", rc);
    return rc;
}


int server(void)
{
    char        title[81];

    /* 
     * Initialize defaults, Video and Audio
     */
    if ((SDL_Init(SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO) == -1)) { 
	syslog(LOG_NOTICE, "[main] could not initialize SDL: %s", SDL_GetError());
	exit(-1);
    }
    if ((TTF_Init() < 0)) {
        syslog(LOG_NOTICE, "[main] SDL_ttf could not initialize! SDL_ttf Error: %s", TTF_GetError());
        exit(-1);
    }
    SDLGui_Init();

    /*
     * Get available fullscreen modes, so we will get the effective screensize
     */
    SDL_DisplayMode modes = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 };
    if (SDL_GetDisplayMode(0, 0, &modes) != 0) {
        syslog(LOG_NOTICE, "[main] SDL_GetDisplayMode failed: %s", SDL_GetError());
        exit(-1);
    }

    PAN_x = 384;
    PAN_y = 480;

    snprintf(title, 80, "brewpanel v%s", VERSION);
    S_window = SDL_CreateWindow(title, 0, 0, PAN_x, PAN_y, SDL_WINDOW_OPENGL);

    S_screen = SDL_GetWindowSurface(S_window);
    if (S_screen == NULL) {
	syslog(LOG_NOTICE, "Could not create S_screen: %s", SDL_GetError());
	exit(-1);
    }


    syslog(LOG_NOTICE, "Starting mainloop");
    if (strlen(Paneltype) && !strcmp(Paneltype, (char *)"ferm") ) {
	Dialog_BrewDlg(1);
    } else {
    	Dialog_BrewDlg(0);
    }
    syslog(LOG_NOTICE, "Out of mainloop, cleanup");

    SDLGui_UnInit();
    SDL_FreeSurface(S_screen);
    SDL_Quit();

    return 0;
}

#else

int main(int argc, char *argv[])
{
    fprintf(stderr, "brewpanel not compiled because SDL2 is missing.\n");
    return 1;
}

#endif

mercurial