Re: Prioritäten bei Threads
Verfasst: Donnerstag 17. Januar 2019, 09:05
Ok, ich denke da muss ich mir hilfe suchen - und die Software neu machen/machen lassen.
Danke für eure Geduld
Danke für eure Geduld
Seit 2002 Diskussionen rund um die Programmiersprache Python
https://www.python-forum.de/
Code: Alles auswählen
onionGpio.OnionGpio(blau).setOutputDirection(0)
onionGpio.OnionGpio(gruen).setOutputDirection(0)
onionGpio.OnionGpio(rot).setOutputDirection(0)
time.sleep(2)
onionGpio.OnionGpio(blau).setOutputDirection(0)
onionGpio.OnionGpio(gruen).setOutputDirection(1)
onionGpio.OnionGpio(rot).setOutputDirection(1)
time.sleep(0.15)
Code: Alles auswählen
def setup_logger(logger_name, log_file):
l = logging.getLogger(logger_name)
l.setLevel(logging.INFO)
fileHandler = logging.handlers.RotatingFileHandler(log_file, maxBytes=100, backupCount=10)
formatter = logging.Formatter('%(asctime)s;%(message)s;test')
fileHandler.setFormatter(formatter)
l.addHandler(fileHandler)
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from OmegaExpansion import onionI2C
import time
import logging
import logging.handlers
import importlib
import multiprocessing
from multiprocessing import Process, Queue
import threading
import os
import datetime
import configparser
def setup_logger(logger_name, log_file):
l = logging.getLogger(logger_name)
l.setLevel(logging.INFO)
fileHandler = logging.handlers.RotatingFileHandler(log_file, maxBytes=10000000, backupCount=10) # 10MB x 10
formatter = logging.Formatter('%(message)s')
fileHandler.setFormatter(formatter)
l.addHandler(fileHandler)
def read_i2c(read_i2c_queue):
i2c = onionI2C.OnionI2C()
i2cdata = [0xFF]
i2c.write(0x20, i2cdata)
act_channel = 6
channel_ppulse = [False,False,False,False,False,False]
channel_ppulse_hm = [False,False,False,False,False,False]
last_pulsetime = [0,0,0,0,0,0]
act_pulsetime = [0,0,0,0,0,0]
while True:
os.nice(-20) # fast process
act_channel += 1
if act_channel > 5:
act_channel = 0
i2cdata = i2c.readBytes(0x20, 0xFF, 1)
i2cdata = (i2cdata[0] & 0xFF) # Convert the data
channel_ppulse[act_channel] = not (i2cdata & (2 ** act_channel)) and not channel_ppulse_hm[act_channel]
channel_ppulse_hm[act_channel] = not (i2cdata & (2 ** act_channel))
if channel_ppulse[act_channel]:
read_i2c_queue.put(act_channel+1)
read_i2c_queue.put(datetime.datetime.now())
read_i2c_queue.put(time.time())
time.sleep(0.001)
def actual_logging(setpoint, timeset, softwareversion, id, last_pulstime, act_pulstime, channeltype, unit_actual, name1, name2, name3, name4, name5, geo_lat, geo_long):
act_channel = 1
act_pausetime = 0
last_pausetime = 0
setup_logger('ch_a', '/mnt/sd0/logs/ch_a.log')
ch_a = logging.getLogger('ch_a')
while True:
act_pausetime = time.time() - act_pulstime[act_channel]
last_pausetime = act_pulstime[act_channel] - last_pulstime[act_channel]
if act_pausetime < last_pausetime and not last_pausetime == 0:
act_value = float(timeset[act_channel]) / last_pausetime * float(setpoint[act_channel])
elif act_pausetime > last_pausetime and not last_pausetime == 0:
act_value = float(timeset[act_channel]) / act_pausetime * float(setpoint[act_channel])
else:
act_value = 0
if act_value != 0:
timestamp = str(datetime.datetime.now())
ch_a.info(timestamp.replace('.',',')+';'+str(softwareversion)+';'+str(id)+';'+str(act_channel)+';s0da;'+str(act_value)+';'+str(channeltype[act_channel])+';'+str(unit_actual[act_channel])+';'+str(name1[act_channel])+';'+str(name2[act_channel])+';'+str(name3[act_channel])+';'+str(name4[act_channel])+';'+str(name5[act_channel])+';'+str(geo_lat[act_channel])+';'+str(geo_long[act_channel]))
ch_out = configparser.ConfigParser()
ch_out.read('/opt/config/ch' + str(act_channel) + '_out.ini')
ch_out['ch' + str(act_channel)]['value'] = str(act_value)
with open('/opt/config/ch' + str(act_channel) + '_out.ini.new', 'w') as configfile_w:
ch_out.write(configfile_w)
os.system('mv -f /opt/config/ch' + str(act_channel) + '_out.ini.new /opt/config/ch' + str(act_channel) + '_out.ini')
time.sleep(0.4)
act_channel += 1
if act_channel == 7:
act_channel = 1
def main():
print '***** Startup *****'
config_channel = 1
setpoint = [0,0,0,0,0,0,0]
timeset = [0,0,0,0,0,0,0]
softwareversion = ''
id = ''
channelnumber = [0,0,0,0,0,0,0]
channeltype = [0,0,0,0,0,0,0]
unit_actual = [0,0,0,0,0,0,0]
unit_counter = [0,0,0,0,0,0,0]
name1 = [0,0,0,0,0,0,0]
name2 = [0,0,0,0,0,0,0]
name3 = [0,0,0,0,0,0,0]
name4 = [0,0,0,0,0,0,0]
name5 = [0,0,0,0,0,0,0]
geo_lat = [0,0,0,0,0,0,0]
geo_long = [0,0,0,0,0,0,0]
last_pulstime = [0,0,0,0,0,0,0]
act_pulstime = [0,0,0,0,0,0,0]
# CONFIG
setup = configparser.ConfigParser()
setup.read('/opt/config/config_out.ini')
id = setup.get('system', 'id')
softwareversion = setup.get('software', 'softwareversion')
while config_channel <= 6:
setup = configparser.ConfigParser()
setup.read('/opt/config/ch' + str(config_channel) + '_setup.ini')
setpoint[config_channel] = setup.get('ch' + str(config_channel), 'setpoint')
timeset[config_channel] = setup.get('ch' + str(config_channel), 'timeset')
channeltype[config_channel] = setup.get('ch' + str(config_channel), 'type')
unit_actual[config_channel] = setup.get('ch' + str(config_channel), 'unit_act')
unit_counter[config_channel] = setup.get('ch' + str(config_channel), 'unit_count')
name1[config_channel] = setup.get('ch' + str(config_channel), 'name1')
name2[config_channel] = setup.get('ch' + str(config_channel), 'name2')
name3[config_channel] = setup.get('ch' + str(config_channel), 'name3')
name4[config_channel] = setup.get('ch' + str(config_channel), 'name4')
name5[config_channel] = setup.get('ch' + str(config_channel), 'name5')
geo_lat[config_channel] = setup.get('ch' + str(config_channel), 'geo_lat')
geo_long[config_channel] = setup.get('ch' + str(config_channel), 'geo_long')
config_channel += 1
# LOGGING
setup_logger('ch_c', '/mnt/sd0/logs/ch_c.log')
ch_c = logging.getLogger('ch_c')
read_i2c_queue = Queue()
read_process = multiprocessing.Process(target=read_i2c, args=(read_i2c_queue, ))
read_process.start()
# FILEBEAT
os.system('kill -9 `pgrep filebeat`')
time.sleep(1)
os.system('nice -n 19 /mnt/sd0/filebeat/filebeat -c /mnt/sd0/filebeat/filebeat.use.yml &')
# ACTUAL LOGGING
thread_actual = threading.Thread(target=actual_logging, args=(setpoint, timeset, softwareversion, id, last_pulstime, act_pulstime, channeltype, unit_actual, name1, name2, name3, name4, name5, geo_lat, geo_long))
thread_actual.start()
# COUNTER LOGGING
while True:
if not read_i2c_queue.empty():
channel = read_i2c_queue.get()
datetimestamp = read_i2c_queue.get()
timestamp = read_i2c_queue.get()
ch_c.info(str(datetimestamp)+';'+str(softwareversion)+';'+str(id)+';'+str(channel)+';s0dc;'+str(setpoint[channel])+';'+str(channeltype[channel])+';'+str(unit_counter[channel])+';'+str(name1[channel])+';'+str(name2[channel])+';'+str(name3[channel])+';'+str(name4[channel])+';'+str(name5[channel])+';'+str(geo_lat[channel])+';'+str(geo_long[channel]))
last_pulstime[channel] = act_pulstime[channel]
act_pulstime[channel] = timestamp
time.sleep(0.01)
if __name__ == "__main__":
main()
Code: Alles auswählen
def read_i2c(read_i2c_queue):
os.nice(-20) # fast process
i2c = onionI2C.OnionI2C()
i2c.write(0x20, [0xFF])
previous_pulse = [False] * 6
while True:
i2cdata = i2c.readByte(0x20, 0xFF)
timestamp = time.time()
for channel in range(6):
pulse = i2cdata & (2 ** channel)
if not pulse and previous_pulse[channel]:
read_i2c_queue.put((channel+1, timestamp))
previous_pulse[channel] = pulse
time.sleep(0.006)
Code: Alles auswählen
ch_out = configparser.ConfigParser()
ch_out.read('/opt/config/ch' + str(act_channel) + '_out.ini')
ch_out['ch' + str(act_channel)]['value'] = str(act_value)
with open('/opt/config/ch' + str(act_channel) + '_out.ini.new', 'w') as configfile_w:
ch_out.write(configfile_w)
os.system('mv -f /opt/config/ch' + str(act_channel) + '_out.ini.new /opt/config/ch' + str(act_channel) + '_out.ini')
Code: Alles auswählen
channel = 'ch{}'.format(act_channel)
config_filename = '/opt/config/{}_out.ini'.format(channel)
config_filename_new = config_filename + '.new'
ch_out = configparser.ConfigParser()
ch_out.read(config_filename)
ch_out[channel]['value'] = str(act_value)
with open(config_filename_new, 'w') as configfile_w:
ch_out.write(configfile_w)
os.rename(config_filename_new, config_filename)
Code: Alles auswählen
while True:
channel, timestamp = read_i2c_queue.get()
ch_c.info(';'.join(map(str, [
datetime.datetime.fromtimestamp(timestamp),
softwareversion, id,
channel, 's0dc', setpoint[channel],
channeltype[channel], unit_counter[channel],
name1[channel], name2[channel], name3[channel],
name4[channel], name5[channel],
geo_lat[channel], geo_long[channel]
]))