Ejemplo n.º 1
0
def EnCallback(a):

    (context, queues) = a
    qRadio = queues[ESP3Radio.typeId]

    # polling timestamp
    utime = int(time.mktime(time.gmtime()))
    stime = int(time.mktime(time.localtime()))

    # slave 0x00 register 0x03 (HR)
    register = 0x03
    slave_id = 0x00

    builder = PayloadBuilder(endian=config.endian)

    endata = {}

    # endata persistency (load)
    if os.path.isfile(config.enstore):
	try:
	    enfile = open(config.enstore,'rb')
	    endata = pickle.load(enfile)
	    enfile.close()
	except OSError:
	    logger.warning("Cannot open %s for reading." % config.enstore) 

    while qRadio.qsize():
	qr = qRadio.get()
	t = ESP3Telegram(None, qr.data)
	try:
	    device = config.getDevice(address = t.sender())
	    eep = device.values()[0]['eep']
	    t = ESP3Telegram(eep, qr.data)
	    if t.tmp():
		endata[t.sender()] = [t.tmp(), stime]
	    if t.hum():
		endata[t.sender()] = [t.hum(), stime]
	except (AttributeError, KeyError):
	    pass

    # update RRDs
    rrdupdate(endata)

    # endata validation
    for sensor in endata:
	last = endata[sensor][1]
	if ((stime - last) > config.enhold) or endata[sensor][0] == '':
	    endata[sensor][0] = config.enfail
	    logger.warning("Cannot receive anymore EnOcean sensor %s." % sensor)

    # endata persistency (save)
    try:
        enfile = open(config.enstore,'wb')
        pickle.dump(endata, enfile)
        enfile.close()
    except OSError:
        logger.warning("Cannot open %s for writing." % config.enstore) 

    # reset holding registers with enfail value
    devices = config.getDevices(bus = 'enocean')
    for sensor in devices:
	try:
	    id = sensor.keys()[0]
	    device = config.getDevice(id)
	    mbaddr = int(device.values()[0]['register'])
	    builder.reset()
	    builder.add_32bit_float(config.owfail)
	    context[slave_id].setValues(register, mbaddr, builder.values())
	except AttributeError:
	    pass

    # update holding registers with endata
    for address in endata:
	try:
	    device = config.getDevice(address = address)
	    mbaddr = int(device.values()[0]['register'])
	    data = endata[address][0]
	    builder.reset()
	    builder.add_32bit_float(data)
	    context[slave_id].setValues(register, mbaddr, builder.values())
	except AttributeError:
	    pass
Ejemplo n.º 2
0
def EnCallback(a):

    (context, queues) = a
    qRadio = queues[ESP3Radio.typeId]

    # polling timestamp
    utime = int(time.mktime(time.gmtime()))
    stime = int(time.mktime(time.localtime()))

    # slave 0x00 register 0x03 (HR)
    register = 0x03
    slave_id = 0x00

    builder = PayloadBuilder(endian=config.endian)

    endata = {}

    # endata persistency (load)
    if os.path.isfile(config.enstore):
        try:
            enfile = open(config.enstore, 'rb')
            endata = pickle.load(enfile)
            enfile.close()
        except OSError:
            logger.warning("Cannot open %s for reading." % config.enstore)

    while qRadio.qsize():
        qr = qRadio.get()
        t = ESP3Telegram(None, qr.data)
        try:
            device = config.getDevice(address=t.sender())
            eep = device.values()[0]['eep']
            t = ESP3Telegram(eep, qr.data)
            if t.tmp():
                endata[t.sender()] = [t.tmp(), stime]
            if t.hum():
                endata[t.sender()] = [t.hum(), stime]
        except (AttributeError, KeyError):
            pass

    # update RRDs
    rrdupdate(endata)

    # endata validation
    for sensor in endata:
        last = endata[sensor][1]
        if ((stime - last) > config.enhold) or endata[sensor][0] == '':
            endata[sensor][0] = config.enfail
            logger.warning("Cannot receive anymore EnOcean sensor %s." %
                           sensor)

    # endata persistency (save)
    try:
        enfile = open(config.enstore, 'wb')
        pickle.dump(endata, enfile)
        enfile.close()
    except OSError:
        logger.warning("Cannot open %s for writing." % config.enstore)

    # reset holding registers with enfail value
    devices = config.getDevices(bus='enocean')
    for sensor in devices:
        try:
            id = sensor.keys()[0]
            device = config.getDevice(id)
            mbaddr = int(device.values()[0]['register'])
            builder.reset()
            builder.add_32bit_float(config.owfail)
            context[slave_id].setValues(register, mbaddr, builder.values())
        except AttributeError:
            pass

    # update holding registers with endata
    for address in endata:
        try:
            device = config.getDevice(address=address)
            mbaddr = int(device.values()[0]['register'])
            data = endata[address][0]
            builder.reset()
            builder.add_32bit_float(data)
            context[slave_id].setValues(register, mbaddr, builder.values())
        except AttributeError:
            pass
Ejemplo n.º 3
0
def OwCallback(a):

    context = a

    # polling timestamp
    utime = int(time.mktime(time.gmtime()))
    stime = int(time.mktime(time.localtime()))

    # slave 0x00 register 0x03 (HR)
    register = 0x03
    slave_id = 0x00

    builder = PayloadBuilder(endian=config.endian)

    owdata = {}

    # owdata persistency (load)
    if os.path.isfile(config.owstore):
	try:
	    owfile = open(config.owstore,'rb')
	    owdata = pickle.load(owfile)
	    owfile.close()
	except OSError:
	    logger.warning("Cannot open %s for reading." % config.owstore) 

    # init ownet configuration
    ownet.init('%s:%d' % (config.owserver, config.owport))

    # try to open ownet server
    try:
	ow = ownet.Sensor(config.owpath)
        owread = True
    except (socket.gaierror, socket.error):
        logger.warning("Cannot connect owserver %s:%d." % (config.owserver, config.owport)) 
        owread = False

    # if connected, read known sensors and update owdata
    if owread:
        try:
            for sensor in ow.sensors():
		if (hasattr(sensor, 'id') & hasattr(sensor, 'temperature')):
		    owdata[sensor.id] = [ sensor.temperature, stime ]
		if (hasattr(sensor, 'id') & hasattr(sensor, 'humidity')):
		    owdata[sensor.id] = [ sensor.humidity, stime ]
	except (socket.gaierror, socket.error, OverflowError):
	    pass

    # deinit ownet
    ownet.finish()

    # update RRDs
    if owread:
	rrdupdate(owdata)

    # owdata validation
    if '' in owdata: del owdata['']
    for sensor in owdata:
	last = owdata[sensor][1]
	if ((stime - last) > config.owhold) or owdata[sensor][0] == '':
	    owdata[sensor][0] = config.owfail
	    logger.warning("Cannot read 1 wire sensor %s from owserver %s:%d." % (sensor, config.owserver, config.owport))
	if not owread:
	    logger.warning("Cannot read 1 wire sensor %s from owserver %s:%d." % (sensor, config.owserver, config.owport))

    # owdata persistency (save)
    if owread:
	try:
	    owfile = open(config.owstore,'wb')
	    pickle.dump(owdata, owfile)
	    owfile.close()
	except OSError:
	    logger.warning("Cannot open %s for writing." % config.owstore) 

    # reset holding registers with owfail value
    devices = config.getDevices(bus = 'onewire', types = ['Temperature', 'Humidity'])
    for sensor in devices:
	try:
	    id = sensor.keys()[0]
	    device = config.getDevice(id)
	    mbaddr = int(device.values()[0]['register'])
	    builder.reset()
	    builder.add_32bit_float(config.owfail)
	    context[slave_id].setValues(register, mbaddr, builder.values())
	except AttributeError:
	    pass

    # update holding registers with owdata
    for address in owdata:
	try:
	    device = config.getDevice(address = address)
	    mbaddr = int(device.values()[0]['register'])
	    data = owdata[address][0]
	    builder.reset()
	    builder.add_32bit_float(data)
	    context[slave_id].setValues(register, mbaddr, builder.values())
	except AttributeError:
	    pass
Ejemplo n.º 4
0
def OwCallback(a):

    context = a

    # polling timestamp
    utime = int(time.mktime(time.gmtime()))
    stime = int(time.mktime(time.localtime()))

    # slave 0x00 register 0x03 (HR)
    register = 0x03
    slave_id = 0x00

    builder = PayloadBuilder(endian=config.endian)

    owdata = {}

    # owdata persistency (load)
    if os.path.isfile(config.owstore):
        try:
            owfile = open(config.owstore, 'rb')
            owdata = pickle.load(owfile)
            owfile.close()
        except OSError:
            logger.warning("Cannot open %s for reading." % config.owstore)

    # init ownet configuration
    ownet.init('%s:%d' % (config.owserver, config.owport))

    # try to open ownet server
    try:
        ow = ownet.Sensor(config.owpath)
        owread = True
    except (socket.gaierror, socket.error):
        logger.warning("Cannot connect owserver %s:%d." %
                       (config.owserver, config.owport))
        owread = False

    # if connected, read known sensors and update owdata
    if owread:
        try:
            for sensor in ow.sensors():
                if (hasattr(sensor, 'id') & hasattr(sensor, 'temperature')):
                    owdata[sensor.id] = [sensor.temperature, stime]
                if (hasattr(sensor, 'id') & hasattr(sensor, 'humidity')):
                    owdata[sensor.id] = [sensor.humidity, stime]
        except (socket.gaierror, socket.error, OverflowError):
            pass

    # deinit ownet
    ownet.finish()

    # update RRDs
    if owread:
        rrdupdate(owdata)

    # owdata validation
    if '' in owdata: del owdata['']
    for sensor in owdata:
        last = owdata[sensor][1]
        if ((stime - last) > config.owhold) or owdata[sensor][0] == '':
            owdata[sensor][0] = config.owfail
            logger.warning(
                "Cannot read 1 wire sensor %s from owserver %s:%d." %
                (sensor, config.owserver, config.owport))
        if not owread:
            logger.warning(
                "Cannot read 1 wire sensor %s from owserver %s:%d." %
                (sensor, config.owserver, config.owport))

    # owdata persistency (save)
    if owread:
        try:
            owfile = open(config.owstore, 'wb')
            pickle.dump(owdata, owfile)
            owfile.close()
        except OSError:
            logger.warning("Cannot open %s for writing." % config.owstore)

    # reset holding registers with owfail value
    devices = config.getDevices(bus='onewire',
                                types=['Temperature', 'Humidity'])
    for sensor in devices:
        try:
            id = sensor.keys()[0]
            device = config.getDevice(id)
            mbaddr = int(device.values()[0]['register'])
            builder.reset()
            builder.add_32bit_float(config.owfail)
            context[slave_id].setValues(register, mbaddr, builder.values())
        except AttributeError:
            pass

    # update holding registers with owdata
    for address in owdata:
        try:
            device = config.getDevice(address=address)
            mbaddr = int(device.values()[0]['register'])
            data = owdata[address][0]
            builder.reset()
            builder.add_32bit_float(data)
            context[slave_id].setValues(register, mbaddr, builder.values())
        except AttributeError:
            pass