Browse code

added class methods for configuring sensor; added exception catching for json formatting code

Gandolf authored on 06/20/2021 00:40:59
Showing 1 changed files
... ...
@@ -7,7 +7,7 @@
7 7
 # Description: Creates a rrdtool database for use by the power agent to
8 8
 # store the data from the power and temperature sensors.  The agent uses
9 9
 # the data in the database to generate graphic charts for display in the
10
-# weather station web page.
10
+# node power web page.
11 11
 #
12 12
 # Copyright 2021 Jeff Owrey
13 13
 #    This program is free software: you can redistribute it and/or modify
Browse code

Initial release

Gandolf authored on 05/29/2021 20:13:39
Showing 1 changed files
1 1
new file mode 100755
... ...
@@ -0,0 +1,93 @@
1
+#!/usr/bin/python -u
2
+## The -u option above turns off block buffering of python output. This assures
3
+## that each error message gets individually printed to the log file.
4
+#
5
+# Module: createPowerRrd.py
6
+#
7
+# Description: Creates a rrdtool database for use by the power agent to
8
+# store the data from the power and temperature sensors.  The agent uses
9
+# the data in the database to generate graphic charts for display in the
10
+# weather station web page.
11
+#
12
+# Copyright 2021 Jeff Owrey
13
+#    This program is free software: you can redistribute it and/or modify
14
+#    it under the terms of the GNU General Public License as published by
15
+#    the Free Software Foundation, either version 3 of the License, or
16
+#    (at your option) any later version.
17
+#
18
+#    This program is distributed in the hope that it will be useful,
19
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
+#    GNU General Public License for more details.
22
+#
23
+#    You should have received a copy of the GNU General Public License
24
+#    along with this program.  If not, see http://www.gnu.org/license.
25
+#
26
+# Revision History
27
+#   * v10 released 01 Jun 2021 by J L Owrey
28
+#
29
+#2345678901234567890123456789012345678901234567890123456789012345678901234567890
30
+
31
+import os
32
+import time
33
+import subprocess
34
+
35
+    ### DEFINE DATABASE FILE LOCATION ###
36
+
37
+_USER = os.environ['USER']
38
+# the file that stores the data
39
+_RRD_FILE = "/home/%s/database/powerData.rrd" % _USER
40
+
41
+   ### DEFINE DATABASE SIZE AND GRANULARITY
42
+
43
+_RRD_SIZE_IN_DAYS = 740 # days
44
+_1YR_RRA_STEPS_PER_DAY = 1440
45
+_DATABASE_UPDATE_INTERVAL = 30
46
+
47
+def createRrdFile():
48
+    """Create the rrd file if it does not exist.
49
+       Parameters: none
50
+       Returns: True, if successful
51
+    """
52
+
53
+    if os.path.exists(_RRD_FILE):
54
+        print "power database already exists"
55
+        return True
56
+
57
+     ## Calculate database size
58
+ 
59
+    heartBeat = 2 * _DATABASE_UPDATE_INTERVAL
60
+    rra1yrNumPDP =  int(round(86400 / (_1YR_RRA_STEPS_PER_DAY * \
61
+                    _DATABASE_UPDATE_INTERVAL)))
62
+    rrd24hrNumRows = int(round(86400 / _DATABASE_UPDATE_INTERVAL))
63
+    rrd1yearNumRows = _1YR_RRA_STEPS_PER_DAY * _RRD_SIZE_IN_DAYS
64
+       
65
+    strFmt = ("rrdtool create %s --start now-1day --step %s "
66
+              "DS:CUR:GAUGE:%s:U:U DS:VOLT:GAUGE:%s:U:U "
67
+              "DS:PWR:GAUGE:%s:U:U DS:BTMP:GAUGE:%s:U:U "
68
+              "DS:ATMP:GAUGE:%s:U:U "
69
+              "RRA:LAST:0.5:1:%s RRA:LAST:0.5:%s:%s")
70
+
71
+    strCmd = strFmt % (_RRD_FILE, _DATABASE_UPDATE_INTERVAL,           \
72
+                heartBeat, heartBeat, heartBeat, heartBeat, heartBeat, \
73
+                rrd24hrNumRows, rra1yrNumPDP, rrd1yearNumRows)
74
+
75
+    print "creating power database...\n\n%s\n" % strCmd
76
+
77
+    # Spawn a sub-shell and run the command
78
+    try:
79
+        subprocess.check_output(strCmd, stderr=subprocess.STDOUT, \
80
+                                shell=True)
81
+    except subprocess.CalledProcessError, exError:
82
+        print "rrdtool create failed: %s" % (exError.output)
83
+        return False
84
+    return True
85
+##end def
86
+
87
+def main():
88
+    createRrdFile()
89
+## end def
90
+
91
+if __name__ == '__main__':
92
+    main()
93
+