Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

# -*- coding: utf-8 -*- 

"""The Raspberry tutorial 

 

""" 

 

__license__ = """ 

This file is part of Janitoo. 

 

Janitoo 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 3 of the License, or 

(at your option) any later version. 

 

Janitoo 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 Janitoo. If not, see <http://www.gnu.org/licenses/>. 

 

""" 

__author__ = 'Sébastien GALLET aka bibi21000' 

__email__ = 'bibi21000@gmail.com' 

__copyright__ = "Copyright © 2013-2014-2015-2016 Sébastien GALLET aka bibi21000" 

 

import logging 

logger = logging.getLogger(__name__) 

import os, sys 

import threading 

import datetime 

 

from janitoo.thread import JNTBusThread, BaseThread 

from janitoo.options import get_option_autostart 

from janitoo.utils import HADD 

from janitoo.node import JNTNode 

from janitoo.value import JNTValue 

from janitoo.component import JNTComponent 

from janitoo.bus import JNTBus 

 

from janitoo_raspberry_dht.dht import DHTComponent 

from janitoo_raspberry_gpio.gpio import GpioBus, LedComponent as GPIOLed 

from janitoo_raspberry_1wire.bus_1wire import OnewireBus 

from janitoo_raspberry_1wire.components import DS18B20 

from janitoo_hostsensor_raspberry.component import HardwareCpu 

 

OID = 'tutorial2' 

 

def make_ambiance(**kwargs): 

return AmbianceComponent(**kwargs) 

 

def make_temperature(**kwargs): 

return TemperatureComponent(**kwargs) 

 

def make_cpu(**kwargs): 

return CpuComponent(**kwargs) 

 

class TutorialBus(JNTBus): 

"""A bus to manage Tutorial 

""" 

 

def __init__(self, **kwargs): 

""" 

""" 

JNTBus.__init__(self, **kwargs) 

self.buses = {} 

self.buses['gpiobus'] = GpioBus(masters=[self], **kwargs) 

self.buses['1wire'] = OnewireBus(masters=[self], **kwargs) 

uuid="{:s}_temperature".format(OID) 

self.values[uuid] = self.value_factory['sensor_temperature'](options=self.options, uuid=uuid, 

node_uuid=self.uuid, 

get_data_cb=self.get_temperature_cb, 

help='The average temperature of tutorial. Can be use as a good quality source for a thermostat.', 

label='Temp', 

) 

poll_value = self.values[uuid].create_poll_value(default=300) 

self.values[poll_value.uuid] = poll_value 

 

def start(self, mqttc, trigger_thread_reload_cb=None): 

"""Start the bus 

""" 

for bus in self.buses: 

self.buses[bus].start(mqttc, trigger_thread_reload_cb=None) 

JNTBus.start(self, mqttc, trigger_thread_reload_cb) 

 

def stop(self): 

"""Stop the bus 

""" 

for bus in self.buses: 

self.buses[bus].stop() 

JNTBus.stop(self) 

 

def loop(self, stopevent): 

"""Retrieve data 

Don't do long task in loop. Use a separated thread to not perturbate the nodeman 

 

""" 

for bus in self.buses: 

self.buses[bus].loop(stopevent) 

 

def get_temperature_cb(self, node_uuid=None, index=None): 

"""Callback for average temperature""" 

nums = 0 

total = 0 

for value in [('temperature', 'temperature'), ('ambiance', 'temperature'), ('cpu', 'temperature')]: 

data = self.nodeman.find_value(*value).data 

if data is not None: 

nums += 1 

total += data 

if nums > 0: 

return 1.0 * total / nums 

return None 

 

class AmbianceComponent(DHTComponent): 

""" A component for ambiance """ 

 

def __init__(self, bus=None, addr=None, **kwargs): 

""" 

""" 

oid = kwargs.pop('oid', '%s.ambiance'%OID) 

name = kwargs.pop('name', "Ambiance sensor") 

DHTComponent.__init__(self, oid=oid, bus=bus, addr=addr, name=name, 

**kwargs) 

logger.debug("[%s] - __init__ node uuid:%s", self.__class__.__name__, self.uuid) 

 

class TemperatureComponent(DS18B20): 

""" A water temperature component """ 

 

def __init__(self, bus=None, addr=None, **kwargs): 

""" 

""" 

oid = kwargs.pop('oid', '%s.temperature'%OID) 

name = kwargs.pop('name', "Temperature") 

DS18B20.__init__(self, oid=oid, bus=bus, addr=addr, name=name, 

**kwargs) 

logger.debug("[%s] - __init__ node uuid:%s", self.__class__.__name__, self.uuid) 

 

class CpuComponent(HardwareCpu): 

""" A water temperature component """ 

 

def __init__(self, bus=None, addr=None, **kwargs): 

""" 

""" 

oid = kwargs.pop('oid', '%s.cpu'%OID) 

name = kwargs.pop('name', "CPU") 

HardwareCpu.__init__(self, oid=oid, bus=bus, addr=addr, name=name, 

**kwargs) 

logger.debug("[%s] - __init__ node uuid:%s", self.__class__.__name__, self.uuid)