-
Notifications
You must be signed in to change notification settings - Fork 5
/
HMpvinverter.py
171 lines (126 loc) · 4.75 KB
/
HMpvinverter.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python
# import normal packages
import logging
import sys
import os
if sys.version_info.major == 2:
import gobject
else:
from gi.repository import GLib as gobject
import configparser # for config/ini file
try:
import thread # for daemon = True / Python 2.x
except:
import _thread as thread # for daemon = True / Python 3.x
import dbus
#sys.path.insert(1, os.path.join(os.path.dirname(__file__), '/opt/victronenergy/dbus-systemcalc-py/ext/velib_python'))
#from vedbus import VeDbusService
#from settingsdevice import SettingsDevice
#from dbusmonitor import DbusMonitor
from multiprocessing import Process
from Inverter import HmInverter
from MicroPlus import MicroPlus
EXTINFO = 15
#class SystemBus(dbus.bus.BusConnection):
# def __new__(cls):
# return dbus.bus.BusConnection.__new__(cls, dbus.bus.BusConnection.TYPE_SYSTEM)
#class SessionBus(dbus.bus.BusConnection):
# def __new__(cls):
# return dbus.bus.BusConnection.__new__(cls, dbus.bus.BusConnection.TYPE_SESSION)
#def dbusconnection():
# return SessionBus() if 'DBUS_SESSION_BUS_ADDRESS' in os.environ else SystemBus()
def getConfig():
config = configparser.ConfigParser()
config.read("%s/config.ini" % (os.path.dirname(os.path.realpath(__file__))))
return config;
class clsProcess:
serial = 0
target = None
process = None
class mainControl:
def __init__(self):
self.config = self._getConfig()
self.procs = []
if self.config.has_option('DEFAULT', 'InverterCount') == True:
InverterCount = int(self.config["DEFAULT"]["InverterCount"])
else:
InverterCount = 1
for i in range(1, InverterCount+1):
proc = clsProcess()
proc.serial = i
proc.target = self._startInverter
proc.process = Process(target=proc.target, args=(proc.serial,))
self.procs.append(proc)
proc = clsProcess()
proc.serial = 0
proc.target = self._startVebus
proc.process = Process(target=proc.target, args=(proc.serial,))
self.procs.append(proc)
gobject.timeout_add_seconds(1, self._start)
def _startInverter(self,serial):
try:
newDevice = HmInverter(serial)
mainloop = gobject.MainLoop()
mainloop.run()
except Exception as e:
pass
logging.critical('Error at %s', 'main', exc_info=e)
def _startVebus(self,serial):
try:
newDevice = MicroPlus()
mainloop = gobject.MainLoop()
mainloop.run()
except Exception as e:
pass
logging.critical('Error at %s', 'main', exc_info=e)
def _start(self):
for proc in self.procs:
proc.process.start()
gobject.timeout_add_seconds(10, self._loop)
return False
def _loop(self):
for proc in self.procs:
if proc.process.is_alive() == False:
logging.warning("Process stoped, restart process %s"% (proc.serial))
proc.process = Process(target=proc.target, args=(proc.serial,))
proc.process.start()
return True
def _getConfig(self):
config = configparser.ConfigParser()
config.read("%s/config.ini" % (os.path.dirname(os.path.realpath(__file__))))
return config
################################################################################
# #
# Main #
# #
################################################################################
def main():
thread.daemon = True # allow the program to quit
try:
logging.addLevelName(EXTINFO, 'EXTINFO')
config = getConfig()
if config.has_option('DEFAULT', 'Logging') == True:
logging_level = config["DEFAULT"]["Logging"]
else:
logging_level = logging.INFO
#configure logging
logging.basicConfig( format='%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s',
datefmt='%Y-%m-%d %H:%M:%S',
level=logging_level,
handlers=[
logging.FileHandler("%s/current.log" % (os.path.dirname(os.path.realpath(__file__)))),
logging.StreamHandler()
])
from dbus.mainloop.glib import DBusGMainLoop
# Have a mainloop, so we can send/receive asynchronous calls to and from dbus
DBusGMainLoop(set_as_default=True)
logging.info('Connected to dbus, and switching over to gobject.MainLoop() (= event based)')
mainloop = gobject.MainLoop()
#start our main-service
control = mainControl()
mainloop.run()
except Exception as e:
pass
logging.critical('Error at %s', 'main', exc_info=e)
if __name__ == "__main__":
main()