brewco/rdsession.c

Sat, 26 Dec 2015 21:45:44 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 26 Dec 2015 21:45:44 +0100
changeset 473
fdd30e935079
parent 472
55bcbf92ecab
child 474
fe1c3e3e90dc
permissions
-rw-r--r--

The brew state machine is complete, works but is not bugfree

/*****************************************************************************
 * Copyright (C) 2015
 *   
 * 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 "brewco.h"
#include "rdsession.h"
#include "util.h"
#include "xutil.h"

extern int	debug;
extern char	*etcpath;


const char      BREWSTEP[16][10] = { 	"NA", "INIT", "WATEROK", "PRIME", "WAITSTART",
					"PREMASH", "MASHING", "REMOVE", "PREBOIL", "BOIL",
					"BOILDONE", "COOLING", "HOPSTAND", "WHIRLPOOL", "CLEANUP",
					"DONE" };
const char	MASHSTEP[8][11] = {	"MASH-IN", "PHYTASE", "GLUCANASE", "PROTEASE", "B-AMYLASE", "A-AMYLASE1", "A-AMYLASE2", "MASH-OUT" };

#define MY_ENCODING "utf-8"


int do_wrsession(brew_session *brew);
int do_wrsession(brew_session *brew)
{
    int			rc = 0;
    FILE		*fp;
    char		*mypath = NULL;
    xmlTextWriterPtr	writer;
    xmlBufferPtr	buf;

    /* 
     * Create a new XML buffer, to which the XML document will be written
     */
    if ((buf = xmlBufferCreate()) == NULL) {
	syslog(LOG_NOTICE, "wrsession: error creating the xml buffer");
	return 1;
    }

    /* 
     * Create a new XmlWriter for memory, with no compression.
     */
    if ((writer = xmlNewTextWriterMemory(buf, 0)) == NULL) {
	syslog(LOG_NOTICE, "wrsession: error creating the xml writer");
	return 1;
    }

    /*
     * Use indentation instead of one long line
     */
    if ((rc = xmlTextWriterSetIndent(writer, 2)) < 0) {
	syslog(LOG_NOTICE, "wrsession: error setting Indent");
	return 1;
    }

    /* 
     * Start the document with the xml default for the version,
     * encoding ISO 8859-1 and the default for the standalone
     * declaration. 
     */
    if ((rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL)) < 0) {
	syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterStartDocument");
	return 1;
    }

    /* 
     * Start an element named "BREWCO". Since thist is the first
     * element, this will be the root element of the document.
     */
    if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "BREWCO")) < 0) {
	syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterStartElement");
	return 1;
    }
    if ((rc = xmlTextWriterStartElement(writer, BAD_CAST "BREW")) < 0) { 
	syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterStartElement");
	return 1;
    }

    /* 
     * Add an attribute with name "VERSION" and value "1" to BRWCO.
     */
    if ((rc = xmlTextWriterWriteElement(writer, BAD_CAST "VERSION", BAD_CAST "1")) < 0) {
	syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
	return 1;
    }
    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID_RECIPE", "%s", brew->uuid_recipe)) < 0) {
	syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement");
	return 1;
    }
    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "UUID_UNIT", "%s", brew->uuid_unit)) < 0) {
        syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement");
        return 1;
    }
    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "NAME", "%s", brew->name)) < 0) {
	syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteFormatElement");
	return 1;
    }
    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BREWSTEP", "%s", BREWSTEP[brew->brewstep])) < 0) {
	syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
	return 1;
    }
    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "MASHSTEP", "%s", MASHSTEP[brew->mashstep])) < 0) {
        syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
        return 1;
    }
    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "TIMEOUT", "%d", brew->timeout)) < 0) {
        syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
        return 1;
    }
    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "BOILTIMER", "%d", brew->boiltimer)) < 0) {
        syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
        return 1;
    }
    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "STARTTIME", "%d", (int)brew->starttime)) < 0) {
        syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
        return 1;
    }
    if ((rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST "ENDTIME", "%d", (int)brew->endtime)) < 0) {
        syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterWriteElement");
        return 1;
    }

    /* 
     * Close the element named BREW.
     */
    if ((rc = xmlTextWriterEndElement(writer)) < 0) {
	syslog(LOG_NOTICE, "wrrecipes: error at xmlTextWriterEndElement");
	return 1;
    }

    /*
     * All done, close any open elements
     */
    if ((rc = xmlTextWriterEndDocument(writer)) < 0) {
	syslog(LOG_NOTICE, "wrsession: error at xmlTextWriterEndDocument");
	return 1;
    }
    xmlFreeTextWriter(writer);

    /*
     * Now write the XML configuration
     */
    mypath = xstrcpy(etcpath);
    mypath = xstrcat(mypath, (char *)"brewing.xml");

    if (debug)
	fprintf(stdout, "Writing %s\n", mypath);

    if ((fp = fopen(mypath, "w")) == NULL) {
	syslog(LOG_NOTICE, "could not rewrite %s", mypath);
	free(mypath);
	return 1;
    }
    free(mypath);

    fprintf(fp, "%s", (const char *) buf->content);
    fclose(fp);
    xmlBufferFree(buf);

    return 0;
}



int wrsession(brew_session *brew)
{
    int		rc;

    rc = do_wrsession(brew);
    syslog(LOG_NOTICE, "Rewritten brewsession, rc=%d", rc);
    return rc;
}



/*
 * Returns:
 *  0 - All is well, session loaded.
 *  1 - Something went wrong
 * -1 - No brew session available
 */
int rdsession(brew_session *brew) 
{
    int		i, ival;
    char	*mypath;
    xmlDocPtr	doc;
    xmlNodePtr	cur, s1cur;
    xmlChar	*key;

    /*
     * Search config file
     */
    mypath = xstrcpy(etcpath);
    mypath = xstrcat(mypath, (char *)"brewing.xml");

    /*
     * See if we have a brewing state file.
     */
    if (file_exist(mypath, W_OK)) {
	syslog(LOG_NOTICE, "rdsession: %s not found, good.", mypath);
	free(mypath);
	return -1;
    }

    if ((doc = xmlParseFile(mypath)) == NULL) {
	syslog(LOG_NOTICE, "rdsession: %s not found, should not happen.", mypath);
	free(mypath);
	return -1;
    }
    syslog(LOG_NOTICE, "rdsession: using %s", mypath);

    if ((cur = xmlDocGetRootElement(doc)) == NULL) {
	syslog(LOG_NOTICE, "XML file %s empty.", mypath);
	xmlFreeDoc(doc);
	return 1;
    }
    if (xmlStrcmp(cur->name, (const xmlChar*)"BREWCO")) {
	syslog(LOG_NOTICE, "XML file %s is not a valid configuration file.", mypath);
	xmlFreeDoc(doc);
	return 1;
    }

    /*
     * Parse session
     */
    cur = cur->xmlChildrenNode;
    while (cur != NULL) {

	if ((! xmlStrcmp(cur->name, (const xmlChar*)"BREW"))) {
	    s1cur = cur->xmlChildrenNode;
	    while (s1cur != NULL) {

		if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"UUID_RECIPE"))) {
		    brew->uuid_recipe = (char *)xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
		}
		if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"UUID_UNIT"))) {
		    brew->uuid_unit = (char *)xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
		}
		if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"NAME"))) {
		    brew->name = (char *)xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
		}
		if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"BREWSTEP"))) {
		    key = xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
		    for (i = 0; i < 15; i++) {
			if (! xmlStrcmp(key, (const xmlChar *)BREWSTEP[i])) {
			    brew->brewstep = i;
			    break;
			}
		    }
		    xmlFree(key);
		}
		if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"MASHSTEP"))) {
		    key = xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
		    for (i = 0; i < 8; i++) {
			if (! xmlStrcmp(key, (const xmlChar *)MASHSTEP[i])) {
			    brew->mashstep = i;
			    break;
			}
		    }
		    xmlFree(key);
		}
		if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"TIMEOUT"))) {
		    key = xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
		    if (sscanf((const char *)key, "%d", &ival) == 1)
			brew->timeout = ival;
		    xmlFree(key);
		}
		if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"BOILTIMER"))) {
		    key = xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
		    if (sscanf((const char *)key, "%d", &ival) == 1)
			brew->boiltimer = ival;
		    xmlFree(key);
		}
		if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"STARTTIME"))) {
		    key = xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
		    if (sscanf((const char *)key, "%d", &ival) == 1)
			brew->starttime = (time_t)ival;
		    xmlFree(key);
		}
		if ((!xmlStrcmp(s1cur->name, (const xmlChar *)"ENDTIME"))) {
		    key = xmlNodeListGetString(doc, s1cur->xmlChildrenNode, 1);
		    if (sscanf((const char *)key, "%d", &ival) == 1)
			brew->endtime = (time_t)ival;
		    xmlFree(key);
		}
		s1cur = s1cur->next;
	    }
	}
	cur = cur->next;
    }
    xmlFreeDoc(doc);

    free(mypath);
    mypath = NULL;

    return 0;
}


mercurial