Example #1
0
 def __DispatchThread(self, parameter):
     debug = 0
     # get db-instance
     database = db_sqlite.cdb_sqlite(self.__cfgfilename, logger=self._logging)
     database.connect()
     message = ht3_dispatch.cht3_dispatch(
         self.__port, ht3_cworker.__gdata, database, debug, self.__filehandle, logger=self._logging
     )
     while self.__threadrun:
         message.dispatcher()
     # close db at the end of thread
     database.close()
    #zs#test#   print("Folder already available:{0}".format(abslogpath))
    pass
    
logfilepathname=os.path.join(abslogpath, os.path.basename(filepathname))
try:
    logobj=ht_utils.clog()
    logging=logobj.create_logfile(logfilepathname, loggertag="create_db")
except:
    errorstr="create_databases();Error;Can't create logfile:{0}".format(logfilepathname)
    print(errorstr)
    raise

####################################
## set configuration-filename and create sqlite-db
configurationfilename='./etc/config/HT3_db_cfg.xml'
sqlitedb=db_sqlite.cdb_sqlite(configurationfilename, logger=logging)
sqlitedb.connect()
infostr="------------------------------------------------"
logging.info(infostr)
print(infostr)
infostr="Create: sqlite -database"
logging.info(infostr)
print(infostr)
sqlitedb.createdb_sqlite()
#check created sqlite-database for read/write access
databasename=sqlitedb.db_sqlite_filename()
if os.access(databasename,os.W_OK and os.R_OK):
    infostr="sqlite-database:'{0}' created and access possible".format(databasename)
else:
    infostr="sqlite-database:'{0}' not available".format(databasename)
print(infostr)
Example #3
0
    def __DispatchThread(self, parameter):
        """
            Dispatch-Thread: open databases if required and storing the results in db.
             automatic draw for rrdtool-draw ist called every 60 sec. if db is enabled.
             automatic erase of old data is done in sqlite-db if it is enabled.
        """
        debug = 0
        rrdtooldb = None
        nextTimeStep = time.time()
        nextTimeautocreate = time.time()
        sqlite_autoerase = False

        # get db-instance
        database = db_sqlite.cdb_sqlite(self.__cfgfilename, logger=self._logging)
        database.connect()

        # set flag for erasing sqlite-db if enabled
        if database.is_sql_db_enabled() and ht3_cworker._gdata.Sqlite_autoerase_seconds() > 0:
            sqlite_autoerase = True

        if ht3_cworker._gdata.is_db_rrdtool_enabled():
            try:
                rrdtooldb = db_rrdtool.cdb_rrdtool(self.__cfgfilename, logger=self._logging)
                rrdtooldb.createdb_rrdtool()
                # setup the first 'nextTimeStep' to 3 times stepseconds waiting for valid data
                nextTimeStep = time.time() + int(ht3_cworker._gdata.db_rrdtool_stepseconds()) * 3
            except:
                errorstr = "ht3_cworker();Error; could not init rrdtool-db"
                self._logging.critical(errorstr)
                self._logging.info("ht3_cworker.run(); End   ----------------------")
                quit()
            # setup first timestep-value for autocreating draw
            if ht3_cworker._gdata.IsAutocreate_draw() > 0:
                nextTimeautocreate = time.time() + 240

        rawdata = ht_discode.cht_discode(self.__port, ht3_cworker._gdata, debug, self.__filehandle, logger=self._logging)

        while self.__threadrun:
            # get the decoded values and store them to db if enabled.
            (nickname, value) = rawdata.discoder()
            if value != None:
                if database.is_sql_db_enabled():
                    database.insert(str(ht3_cworker._gdata.getlongname(nickname)), value)
                    database.commit()

                if ht3_cworker._gdata.is_db_rrdtool_enabled() and rrdtooldb != None:
                    # write data to rrdtool-db after 'stepseconds' seconds
                    if time.time() >= nextTimeStep:
                        # setup next timestep
                        nextTimeStep = time.time() + int(ht3_cworker._gdata.db_rrdtool_stepseconds())
                        # update rrdtool database
                        for syspartshortname in rrdtooldb.syspartnames():
                            if syspartshortname.upper() == 'DT':
                                continue
                            syspartname = rrdtooldb.syspartnames()[syspartshortname]
                            itemvalue_array = ht3_cworker._gdata.getall_sorted_items_with_values(syspartshortname)
                            error = rrdtooldb.update(syspartname, itemvalue_array, time.time())
                            if error:
                                self._logging.critical("rrdtooldb.update();Error;syspartname:{0}".format(syspartname))

                if ht3_cworker._gdata.is_db_rrdtool_enabled() and ht3_cworker._gdata.IsAutocreate_draw() > 0:
                    if time.time() >= nextTimeautocreate:
                        # setup next timestep for autocreating draw
                        nextTimeautocreate = time.time() + 60 * ht3_cworker._gdata.IsAutocreate_draw()
                        # create draw calling script
                        (db_path, dbfilename) = ht3_cworker._gdata.db_rrdtool_filepathname()
                        (html_path, filename) = ht3_cworker._gdata.db_rrdtool_filepathname('.')
                        self.__Autocreate_draw(db_path, html_path, ht3_cworker._gdata.heatercircuits_amount())

                if sqlite_autoerase:
                    self.__Autoerasing_sqlitedb(database)

        #close db at the end of thread
        database.close()
Example #4
0
    def run(self):
        self._logging.info("ht3_cworker.run(); Start ----------------------")
        self._logging.info("ht3_cworker.run();  Loglevel      :{0}".format(logging.getLevelName(self._loglevel)))

        if (self.__inputfile != None) and (len(self.__inputfile) > 0):
            # open input-file in readonly-binary mode for analysing binary HT3-data
            try:
                self.__filehandle = open(self.__inputfile, "rb")
                self.__gui_titel_input = "FILE"
            except:
                errorstr = "ht3_cworker();Error; could not open file:{0}".format(self.__inputfile)
                self._logging.critical(errorstr)
                quit()
        else:
            # open socket for client and connect to server,
            #   socket-object is written to 'self.__port'
            if ht3_cworker.__gdata.IsDataIf_socket():
                try:
                    client_cfg_file = os.path.normcase(os.path.abspath(ht3_cworker.__gdata.client_cfg_file()))
                    if not os.path.exists(client_cfg_file):
                        errorstr = "ht3_cworker();Error;couldn't find file:{0}".format(client_cfg_file)
                        self._logging.critical(errorstr)
                        raise EnvironmentError(errorstr)
                except:
                    errorstr = "ht3_cworker();Error;couldn't find file:{0}".format(client_cfg_file)
                    self._logging.critical(errorstr)
                    raise EnvironmentError(errorstr)

                try:
                    self.__port = ht_proxy_client(client_cfg_file, loglevel=self._loglevel)
                    self.__gui_titel_input = "SOCKET"
                except:
                    errorstr = "ht3_cworker();Error;couldn't open requested socket; cfg-file:{0}".format(
                        client_cfg_file
                    )
                    self._logging.critical(errorstr)
                    raise
            else:
                # open serial port for reading HT3-data
                try:
                    self.__port = serial.Serial(self.__serialdevice, self.__baudrate)
                    self.__port.setInterCharTimeout(0.1)  # VTIME; set to 0.1*1sec
                    self.__gui_titel_input = "ASYNC"
                except:
                    errorstr = "ht3_cworker();Error;couldn't open requested device:{0}".format(self.__serialdevice)
                    self._logging.critical(errorstr)
                    raise EnvironmentError(errorstr)

        self._logging.info("ht3_cworker.run();  Datainput-Mode:{0}".format(self.__gui_titel_input))
        if ht3_cworker.__gdata.IsDataIf_async():
            self._logging.info("ht3_cworker.run();   Baudrate     :{0}".format(self.__baudrate))
            self._logging.info("ht3_cworker.run();   Configuration:{0}".format(ht3_cworker.__gdata.AsyncConfig()))

        try:
            db = db_sqlite.cdb_sqlite(self.__cfgfilename, logger=self._logging)
            # create database-sqlite if required
            if db.is_sql_db_enabled():
                if not db.is_sqlite_db_available():
                    ###                if self.__gui_active:
                    ###                    # disply information, creating database if not available
                    ###                    import tkinter.messagebox
                    ###                    response=tkinter.messagebox.askyesno("SQlite-db not available","Creating ?:yes/no")
                    ###                    if response:
                    ###                        print("creating the database")
                    ###                    else:
                    ###                        print("terminating, no further run")
                    ###                        quit()
                    ###                else:
                    infostr = "ht3_cworker();Wait a bit, database: sqlite will be created"
                else:
                    infostr = "ht3_cworker();sqlite-database already available"

                print(infostr)
                self._logging.info(infostr)

                db.connect()
                db.createdb_sqlite()
                db.close()

            # start thread for dispatching
            _thread.start_new_thread(self.__DispatchThread, (0,))

            if self.__gui_active:
                # start GUI endless until 'Ende' is pressed
                GUI = gui_worker.gui_cworker(
                    ht3_cworker.__gdata, self.__hexdump_window, self.__gui_titel_input, logger=self._logging
                )
                self.__threadrun = GUI.run()

        except (sqlite3.OperationalError, ValueError) as e:
            errorstr = "ht3_cworker();Error; {0}".format(e)
            self._logging.critical(errorstr)
            self._logging.info("ht3_cworker.run(); End   ----------------------")
            quit()
Example #5
0
    def __DispatchThread(self, parameter):
        """
            Dispatch-Thread: open databases if required and storing the results in db.
             automatic draw for rrdtool-draw ist called every 60 sec. if db is enabled.
             automatic erase of old data is done in sqlite-db if it is enabled.
        """
        debug = 0
        rrdtooldb = None
        nextTimeStep = time.time()
        nextTimeautocreate = time.time()
        sqlite_autoerase = False

        # get db-instance
        database = db_sqlite.cdb_sqlite(self.__cfgfilename, logger=self._logging)
        database.connect()

        # set flag for erasing sqlite-db if enabled
        if database.is_sql_db_enabled() and ht3_cworker._gdata.Sqlite_autoerase_seconds() > 0:
            sqlite_autoerase = True

        if ht3_cworker._gdata.is_db_rrdtool_enabled():
            try:
                rrdtooldb = db_rrdtool.cdb_rrdtool(self.__cfgfilename, logger=self._logging)
                rrdtooldb.createdb_rrdtool()
                # setup the first 'nextTimeStep' to 3 times stepseconds waiting for valid data
                nextTimeStep = time.time() + int(ht3_cworker._gdata.db_rrdtool_stepseconds()) * 3
            except:
                errorstr = "ht3_cworker();Error; could not init rrdtool-db"
                self._logging.critical(errorstr)
                self._logging.info("ht3_cworker.run(); End   ----------------------")
                quit()
            # setup first timestep-value for autocreating draw
            if ht3_cworker._gdata.IsAutocreate_draw() > 0:
                nextTimeautocreate = time.time() + 240

        rawdata = ht_discode.cht_discode(self.__port, ht3_cworker._gdata, debug, self.__filehandle, logger=self._logging)

        while self.__threadrun:
            # get the decoded values and store them to db if enabled.
            (nickname, value) = rawdata.discoder()
            if value != None:
                if database.is_sql_db_enabled():
                    database.insert(str(ht3_cworker._gdata.getlongname(nickname)), value)
                    database.commit()

                if ht3_cworker._gdata.is_db_rrdtool_enabled() and rrdtooldb != None:
                    # write data to rrdtool-db after 'stepseconds' seconds
                    if time.time() >= nextTimeStep:
                        # setup next timestep
                        nextTimeStep = time.time() + int(ht3_cworker._gdata.db_rrdtool_stepseconds())
                        # update rrdtool database
                        for syspartshortname in rrdtooldb.syspartnames():
                            if syspartshortname.upper() == 'DT':
                                continue
                            syspartname = rrdtooldb.syspartnames()[syspartshortname]
                            itemvalue_array = ht3_cworker._gdata.getall_sorted_items_with_values(syspartshortname)
                            error = rrdtooldb.update(syspartname, itemvalue_array, time.time())
                            if error:
                                self._logging.critical("rrdtooldb.update();Error;syspartname:{0}".format(syspartname))

                if ht3_cworker._gdata.is_db_rrdtool_enabled() and ht3_cworker._gdata.IsAutocreate_draw() > 0:
                    if time.time() >= nextTimeautocreate:
                        # setup next timestep for autocreating draw
                        nextTimeautocreate = time.time() + 60 * ht3_cworker._gdata.IsAutocreate_draw()
                        # create draw calling script
                        (db_path, dbfilename) = ht3_cworker._gdata.db_rrdtool_filepathname()
                        (html_path, filename) = ht3_cworker._gdata.db_rrdtool_filepathname('.')
                        rrdtooldb.create_draw(db_path, html_path,
                                              ht3_cworker._gdata.heatercircuits_amount(),
                                              ht3_cworker._gdata.controller_type_nr(),
                                              ht3_cworker._gdata.GetAllMixerFlags(),
                                              int(ht3_cworker._gdata.IsTempSensor_Hydrlic_Switch()),
                                              int(ht3_cworker._gdata.IsSolarAvailable()),
                                              int(ht3_cworker._gdata.IsSecondCollectorValue_SO()))
                if sqlite_autoerase:
                    self.__Autoerasing_sqlitedb(database)

        #close db at the end of thread
        database.close()
Example #6
0
    def run(self):
        """
            Main loop doing the work.
        """
        self._logging.info("ht3_cworker.run(); Start ----------------------")
        self._logging.info("ht3_cworker.run();  Loglevel      :{0}".format(logging.getLevelName(self._loglevel)))

        if ((self.__inputfile != None) and (len(self.__inputfile) > 0)):
            # open input-file in readonly-binary mode for analysing binary HT3-data
            try:
                self.__filehandle = open(self.__inputfile, "rb")
                self.__gui_titel_input = "FILE"
            except:
                errorstr = 'ht3_cworker();Error; could not open file:{0}'.format(self.__inputfile)
                self._logging.critical(errorstr)
                quit()
        else:
            # open socket for client and connect to server,
            #   socket-object is written to 'self.__port'
            if(ht3_cworker._gdata.IsDataIf_socket()):
                try:
                    client_cfg_file = os.path.normcase(os.path.abspath(ht3_cworker._gdata.client_cfg_file()))
                    if not os.path.exists(client_cfg_file):
                        errorstr = "ht3_cworker();Error;couldn't find file:{0}".format(client_cfg_file)
                        self._logging.critical(errorstr)
                        raise EnvironmentError(errorstr)
                except:
                    errorstr = "ht3_cworker();Error;couldn't find file:{0}".format(client_cfg_file)
                    self._logging.critical(errorstr)
                    raise EnvironmentError(errorstr)
                try:
                    self.__port = ht_proxy_client(client_cfg_file, loglevel=self._loglevel)
                    self.__gui_titel_input = "SOCKET"
                except:
                    errorstr = "ht3_cworker();Error;couldn't open requested socket; cfg-file:{0}".format(client_cfg_file)
                    self._logging.critical(errorstr)
                    raise
            else:
                #open serial port for reading HT3-data
                try:
                    self.__port = serial.Serial(self.__serialdevice, self.__baudrate)
                    self.__port.setInterCharTimeout(0.1)  # VTIME; set to 0.1*1sec
                    self.__gui_titel_input = "ASYNC"
                except:
                    errorstr = "ht3_cworker();Error;couldn't open requested device:{0}".format(self.__serialdevice)
                    self._logging.critical(errorstr)
                    raise EnvironmentError(errorstr)

        self._logging.info("ht3_cworker.run();  Datainput-Mode:{0}".format(self.__gui_titel_input))
        if ht3_cworker._gdata.IsDataIf_async():
            self._logging.info("ht3_cworker.run();   Baudrate     :{0}".format(self.__baudrate))
            self._logging.info("ht3_cworker.run();   Configuration:{0}".format(ht3_cworker._gdata.AsyncConfig()))

        try:
            db=db_sqlite.cdb_sqlite(self.__cfgfilename, logger=self._logging)
            # create database-sqlite if required
            if db.is_sql_db_enabled():
                if not db.is_sqlite_db_available():
                    infostr = "ht3_cworker();Wait a bit, database: sqlite will be created"
                else:
                    infostr = "ht3_cworker();sqlite-database already available"
                print(infostr)
                self._logging.info(infostr)

                db.connect()
                db.createdb_sqlite()
                db.close()

            # start thread for dispatching
            _thread.start_new_thread(self.__DispatchThread, (0,))

            if self.__gui_active:
                # start GUI endless until 'Ende' is pressed
                GUI = gui_worker.gui_cworker(ht3_cworker._gdata, self.__hexdump_window, self.__gui_titel_input, logger=self._logging)
                self.__threadrun=GUI.run()

        except (sqlite3.OperationalError, ValueError) as e:
            errorstr = "ht3_cworker();Error; {0}".format(e)
            self._logging.critical(errorstr)
            self._logging.info("ht3_cworker.run(); End   ----------------------")
            quit()
Example #7
0
 print(" used configuration:<{0}>".format(configurationfilename))
 print()    
 print("For this test it is required to have:")
 print(" 1. Hardware connected to the above serial-device.")
 print("    If not, change the name in config-file and start again.")
 print(" 2. Hardware connected to the Heater HT-bus")
 print("As result it is:")
 try:
     port = serial.Serial(serialdevice, baudrate )
 except:
     print()
     print("couldn't open requested device: <{0}>".format(serialdevice))
     print()
     sys.exit(1)
     
 db=db_sqlite.cdb_sqlite(configurationfilename)
 if db.is_sql_db_enabled():
     print("--> Database created if not available")
     print("-- create database")
     db.connect()
     db.createdb_sqlite()
     DB_path_and_name=db.db_sqlite_filename()
     if os.access(DB_path_and_name,os.W_OK and os.R_OK):
         print("Database:'{0}' created and access possible".format(DB_path_and_name))
     else:
         print("Database:'{0}' not available".format(DB_path_and_name))
         quit()
     print("--> Intercepted data written to sqlite database")
 else:
     print("--> Database disabled in config-file, no creation done")
 
    pass

logfilepathname = os.path.join(abslogpath, os.path.basename(filepathname))
try:
    logobj = ht_utils.clog()
    logging = logobj.create_logfile(logfilepathname, loggertag="create_db")
except:
    errorstr = "create_databases();Error;Can't create logfile:{0}".format(
        logfilepathname)
    print(errorstr)
    raise

####################################
## set configuration-filename and create sqlite-db
configurationfilename = './etc/config/HT3_db_cfg.xml'
sqlitedb = db_sqlite.cdb_sqlite(configurationfilename, logger=logging)
sqlitedb.connect()
infostr = "------------------------------------------------"
logging.info(infostr)
print(infostr)
infostr = "Create: sqlite -database"
logging.info(infostr)
print(infostr)
sqlitedb.createdb_sqlite()
#check created sqlite-database for read/write access
databasename = sqlitedb.db_sqlite_filename()
if os.access(databasename, os.W_OK and os.R_OK):
    infostr = "sqlite-database:'{0}' created and access possible".format(
        databasename)
else:
    infostr = "sqlite-database:'{0}' not available".format(databasename)
Example #9
0
    def run(self):
        """worker thread for sqlite-db using 'threading.Thread'"""
        import sqlite3
        import db_sqlite
        import db_rrdtool
        debug = 0
        rrdtooldb = None
        nextTimeStep = time.time()
        nextTimeautocreate = time.time()
        sqlite_autoerase = False

        # get db-instance
        database = db_sqlite.cdb_sqlite(self._cfg_file, logger=self._logging)
        database.connect()

        # set flag for erasing sqlite-db if enabled
        if database.is_sql_db_enabled() and self._ht_if.ht_if_data().Sqlite_autoerase_seconds() > 0:
            sqlite_autoerase = True

        if self._ht_if.ht_if_data().is_db_rrdtool_enabled():
            try:
                rrdtooldb = db_rrdtool.cdb_rrdtool(self._cfg_file, self._logging)
                rrdtooldb.createdb_rrdtool()
                # setup the first 'nextTimeStep' to 3 times stepseconds waiting for valid data
                nextTimeStep = time.time() + int(self._ht_if.ht_if_data().db_rrdtool_stepseconds()) * 3
            except:
                errorstr = "cstore2db.run();Error; could not init rrdtool-db"
                self._logging.critical(errorstr)
                self._logging.info("cstore2db.run(); End   ----------------------")
                quit()
            # setup first timestep-value for autocreating draw
            if self._ht_if.ht_if_data().IsAutocreate_draw() > 0:
                nextTimeautocreate = time.time() + 240

        while self.__thread_run:
            try:
                # read values from queue, wait if empty or stop if (None, None)
                (nickname, value) = self._ht_if.decoded_data_4_DBs().get()
                # terminate thread if both values are None, else process them
                if (nickname, value) == (None, None):
                    self.stop()
                    break

                if database.is_sql_db_enabled():
                    database.insert(str(self._ht_if.ht_if_data().getlongname(nickname)), value)
                    database.commit()

                if self._ht_if.ht_if_data().is_db_rrdtool_enabled() and rrdtooldb != None:
                    # write data to rrdtool-db after 'stepseconds' seconds
                    if time.time() >= nextTimeStep:
                        # setup next timestep
                        nextTimeStep = time.time() + int(self._ht_if.ht_if_data().db_rrdtool_stepseconds())
                        # update rrdtool database
                        for syspartshortname in rrdtooldb.syspartnames():
                            if syspartshortname.upper() == 'DT':
                                continue
                            syspartname = rrdtooldb.syspartnames()[syspartshortname]
                            itemvalue_array = self._ht_if.ht_if_data().getall_sorted_items_with_values(syspartshortname)
                            error = rrdtooldb.update(syspartname, itemvalue_array, time.time())
                            if error:
                                self._logging.critical("rrdtooldb.update();Error;syspartname:{0}".format(syspartname))

                if self._ht_if.ht_if_data().is_db_rrdtool_enabled() and self._ht_if.ht_if_data().IsAutocreate_draw() > 0:
                    if time.time() >= nextTimeautocreate:
                        # setup next timestep for autocreating draw
                        nextTimeautocreate = time.time() + 60 * self._ht_if.ht_if_data().IsAutocreate_draw()
                        # create draw calling script
                        (db_path, dbfilename) = self._ht_if.ht_if_data().db_rrdtool_filepathname()
                        (html_path, filename) = self._ht_if.ht_if_data().db_rrdtool_filepathname('.')
                        rrdtooldb.create_draw(db_path, html_path,
                                              self._ht_if.ht_if_data().heatercircuits_amount(),
                                              self._ht_if.ht_if_data().controller_type_nr(),
                                              self._ht_if.ht_if_data().GetAllMixerFlags())

                if sqlite_autoerase:
                    self.__Autoerasing_sqlitedb(database)

                # clear last queue-entry with task_done()
                self._ht_if.decoded_data_4_DBs().task_done()
            except:
                errorstr = "cstore2db.run();Error; on ht_if.decoded_data_4_DBs().get()"
                self._logging.critical(errorstr)
                self.stop()
                raise
        #close db at the end of thread
        database.close()
        errorstr = "cstore2db.run();Error; thread terminated unexpected"
        self._logging.critical(errorstr)