def setConfigInfo(data): result = {'result': 'ERROR: setConfigInfo()'} bc = beerChipPersistant( beerChip(beermonConfig['i2cBus'], beermonConfig['i2cAddr'])) if ('controlProbe' in data): prbInfo = data['controlProbe'] if (('dataSetId' in prbInfo) and ('probeId' in prbInfo)): try: dbConn = beerDB() dbConn.connect(beermonConfig['dbLocation']) sql = "SELECT id, probe_chan, control_probe FROM Probes " sql += "WHERE proj_id = ? AND id = ?" row = dbConn.fetchOne( sql, (int(prbInfo['dataSetId']), int(prbInfo['probeId']))) if (row): if (row[2] == 1): # If configured for control if (bc.setControlProbeChan(int(row[1]))): result['result'] = 'OK' else: result[ 'result'] = 'ERROR: Error writing chan %d to control chip' % ( int(row[1])) else: result[ 'result'] = 'ERROR: Probe not configured for control' else: result['result'] = 'ERROR: Dataset/Probe not found.' except: result = {'result': 'ERROR: trying setConfigInfo()'} finally: if (dbConn != None): dbConn.close() if ('activate' in data): if (data['activate']): bc.switchIn() else: bc.switchOut() result = {'result': 'OK'} if ('setpoint' in data): try: if (('id' in data['setpoint']) and ('setting' in data['setpoint'])): if (int(data['setpoint']['id']) == 0): temp = float(data['setpoint']['setting']) bc.setSetpoint(temp) except: print('ERROR: Setting setpoint') return result
def _writeConfigToDb(self): dbConn = None try: dbConn = beerDB() dbConn.connect(beermonConfig['dbLocation']) cfgStr = json.dumps(self.cfg) sql = "UPDATE Scalar SET value = '%s' " % (cfgStr) sql += "WHERE type = 'beerchip_config'" cnt = dbConn.execute(sql) if (cnt == 0): # Entry does not exist sql = "INSERT INTO Scalar (type, value) " sql += "VALUES( 'beerchip_config', '%s' ) " % (cfgStr) dbConn.execute(sql) self.dirty = False finally: if (dbConn != None): dbConn.close()
def _readConfigFromDb(self): dbConn = None try: dbConn = beerDB() dbConn.connect(beermonConfig['dbLocation']) sql = "SELECT value FROM Scalar " sql += "WHERE type = 'beerchip_config'" cfgRow = dbConn.fetchOne(sql, ()) cfg = None if (cfgRow != None): try: cfg = json.loads(cfgRow[0]) except: cfg = None except: cfg = None finally: if (dbConn != None): dbConn.close() return cfg
import sys import time from datetime import datetime, timedelta from beerChipDB import beerChipSQLiteDB as beerDB def usage(): print('') print('Usage:') print(' python queryTemperature.py "DatasetName" ') print('') if __name__ == '__main__': dbConn = beerDB() dbConn.connect('/opt/beermon/data/beermonSQLite3DB.db') if (len(sys.argv) != 2): print('ERROR: Usage') usage() print('Datasets:') for proj in dbConn.getAvailableProj(): print('\t%s' % proj) dbConn.close() sys.exit(1) projName = sys.argv[1] if (dbConn.setProject(projName)): header = ['timestamp']
def getTempStat(data): # import pdb; pdb.set_trace() dbConn = None result = {} try: dbConn = beerDB() dbConn.connect(beermonConfig['dbLocation']) bc = beerChip(beermonConfig['i2cBus'], beermonConfig['i2cAddr']) result['uptime'] = bc.getUptime() result['state'] = {} result['state']['controlState'] = bc.getState() result['state']['setpoint'] = bc.getSetpoint() cntlChan = bc.getControlProbeChan() result['datasetId'] = data['datasetId'] result['probes'] = [] sql = "SELECT id, probe_name, probe_chan, type, min_range, max_range, control_probe FROM Probes " sql += "WHERE proj_id = ?" for row in dbConn.querySafe(sql, (int(data['datasetId']), )): probeData = { 'id': row[0], 'name': row[1], 'min_range': row[4], 'max_range': row[5], 'cntl_able': row[6] } if (row[2] == cntlChan): probeData['cntl'] = True else: probeData['cntl'] = False probeWhere = [] tupleWhere = (data['datasetId'], ) probeWhere.append("probe_id = %d" % row[0]) startts = datetime.now() if ('startTime' in data): # startts = datetime.fromisoformat( data['startTime'] ) startts = datetime.strptime(data['startTime'], "%Y-%m-%dT%H:%M:%S.%fZ") probeWhere.append("temp_time <= '%s'" % (startts.isoformat())) if ('endTime' in data): # ts = datetime.fromisoformat( data['startTime'] ) ts = datetime.strptime(data['endTime'], "%Y-%m-%dT%H:%M:%S.%fZ") probeWhere.append("temp_time > '%s'" % (ts.isoformat())) if ('statInterval' in data): ts = startts - timedelta(seconds=int(data['statInterval'])) probeWhere.append("temp_time > '%s'" % (ts.isoformat())) """ probeSql = "SELECT " probeSql += "COUNT(*) as cnt, " probeSql += "AVG(temp) as avg, " probeSql += "SUM(temp * temp) as Ex2, " probeSql += "MIN(temp) as min, " probeSql += "MAX(temp) as max " probeSql += "FROM Temperature " probeSql += "WHERE (proj_id = ?) " if( len(probeWhere) > 0 ): probeSql += 'AND (' + ') AND ('.join(probeWhere) + ')' for prbRow in dbConn.querySafe( probeSql, tupleWhere ): if( prbRow[0] > 1 ): probeData['avgTemp'] = prbRow[1] probeData['sdevTemp'] = math.sqrt((prbRow[2] / prbRow[0]) - (probeData['avgTemp'] * probeData['avgTemp'])) # probeData['sdevTemp'] = result['uptime'] % 20 probeData['minTemp'] = prbRow[3] probeData['maxTemp'] = prbRow[4] probeData['currentTemp'] = bc.getTemperature( row[2], row[3] ) result['probes'].append( probeData ) """ probeData['currentTemp'] = bc.getTemperature(row[2], row[3]) result['probes'].append(probeData) result['result'] = 'OK' except: result['result'] = 'ERROR: GetTempStat()' return result
def getConfigInfo(data): activityMap = ['stale', 'collecting'] dbConn = None result = {} try: dbConn = beerDB() dbConn.connect(beermonConfig['dbLocation']) result['version'] = '%d.%d.%d' % (beermonConfig['majVersion'], beermonConfig['minVersion'], beermonConfig['buildVersion']) bc = beerChipPersistant( beerChip(beermonConfig['i2cBus'], beermonConfig['i2cAddr'])) cntlChan = bc.getControlProbeChan() result['uptime'] = bc.getUptime() result['state'] = {} result['state']['controlState'] = bc.getState() dataset = [] sql = "SELECT id, proj_name, activity FROM Project " sql += "ORDER BY activity desc" for row in dbConn.query(sql): if (row[2] and (row[2] < len(activityMap))): act = activityMap[row[2]] else: act = 'err' dat = {'id': row[0], 'name': row[1], 'activity': act, 'probes': []} setpoints = [] hasSp = False probeSql = "SELECT id, probe_name, probe_chan, type, min_range, max_range, control_probe FROM Probes " probeSql += "WHERE proj_id = %d" % (row[0]) for prb in dbConn.query(probeSql): probe = { 'id': prb[0], 'name': prb[1], 'chan': prb[2], 'type': prb[3], 'min_range': prb[4], 'max_range': prb[5], 'cntl_able': prb[6] } if (probe['chan'] == cntlChan): probe['cntl'] = True else: probe['cntl'] = False if (probe['cntl_able'] == 1): hasSp = True dat['probes'].append(probe) if (hasSp): setpoints.append({ 'chan': 0, 'value': bc.getSetpoint(0), 'min_range': 20.0, 'max_range': 90.0 }) dat['setpoints'] = setpoints dataset.append(dat) result['dataset'] = dataset result['result'] = 'OK' except: result['result'] = 'Error' if (dbConn != None): dbConn.close() return result