forked from datenschuft/SMA-EM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sma-daemon.py
executable file
·156 lines (148 loc) · 4.68 KB
/
sma-daemon.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
#!/usr/bin/env python3
# coding=utf-8
"""
*
* by Wenger Florian 2018-01-30
*
* this software is released under GNU General Public License, version 2.
* This program 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; version 2 of the License.
* This program 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 this program;
* if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* 2020-01-04 datenschuft changes to tun with speedwiredecoder
*/
"""
import sys, time,os
from daemon3x import daemon3x
from configparser import ConfigParser
#import smaem
import socket
import struct
from speedwiredecoder import *
#read configuration
parser = ConfigParser()
#alternate config locations
parser.read(['/etc/smaemd/config','config'])
try:
smaemserials=parser.get('SMA-EM', 'serials')
except:
print('Cannot find base config entry SMA-EM serials')
sys.exit(1)
serials=smaemserials.split(' ')
#smavalues=parser.get('SMA-EM', 'values')
#values=smavalues.split(' ')
pidfile=parser.get('DAEMON', 'pidfile')
ipbind=parser.get('DAEMON', 'ipbind')
MCAST_GRP = parser.get('DAEMON', 'mcastgrp')
MCAST_PORT = int(parser.get('DAEMON', 'mcastport'))
features=parser.get('SMA-EM', 'features')
features=features.split(' ')
statusdir=''
try:
statusdir=parser.get('DAEMON','statusdir')
except:
statusdir="/run/shm/"
if os.path.isdir(statusdir):
statusfile=statusdir+"em-status"
else:
statusfile = "em-status"
import importlib
#Check features and load
featurelist = {}
featurecounter=0
for feature in features:
#print ('import ' + feature + '.py')
featureitem={}
featureitem = {'name': feature}
try:
featureitem['feature'] = importlib.import_module('features.' + feature)
except (ImportError, FileNotFoundError, TypeError):
print('feature '+feature+ ' not found')
sys.exit()
try:
featureitem['config']=dict(parser.items('FEATURE-'+feature))
#print (featureitem['config'])
except:
print('feature '+feature+ ' not configured')
sys.exit()
try:
#run config action, if any
featureitem['feature'].config(featureitem['config'])
except:
pass
featurelist[featurecounter]=featureitem
featurecounter += 1
#set defaults
if MCAST_GRP == "":
MCAST_GRP = '239.12.255.254'
if MCAST_PORT == 0:
MCAST_PORT = 9522
class MyDaemon(daemon3x):
def run(self):
# prepare listen to socket-Multicast
socketconnected = False
while not socketconnected:
#try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', MCAST_PORT))
try:
mreq = struct.pack("4s4s", socket.inet_aton(MCAST_GRP), socket.inet_aton(ipbind))
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
file = open(statusfile, "w")
file.write('multicastgroup connected')
file.close()
socketconnected = True
except BaseException:
print('could not connect to mulicast group... rest a bit and retry')
file = open(statusfile, "w")
file.write('could not connect to mulicast group... rest a bit and retry')
file.close()
time.sleep(5)
emparts = {}
while True:
#getting sma values
try:
#emparts=smaem.readem(sock)
emparts=decode_speedwire(sock.recv(608))
for serial in serials:
# process only known sma serials
if serial==format(emparts['serial']):
# running all enabled features
for featurenr in featurelist:
#print('>>> starting '+featurelist[featurenr]['name'])
featurelist[featurenr]['feature'].run(emparts,featurelist[featurenr]['config'])
except Exception as e:
print("Daemon: Exception occured")
print(e)
pass
#Daemon - Coding
if __name__ == "__main__":
daemon = MyDaemon(pidfile)
if len(sys.argv) == 2:
if 'start' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
for featurenr in featurelist:
print('>>> stopping '+featurelist[featurenr]['name'])
featurelist[featurenr]['feature'].stopping({},featurelist[featurenr]['config'])
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()
elif 'run' == sys.argv[1]:
daemon.run()
else:
print ("Unknown command")
sys.exit(2)
sys.exit(0)
else:
print ("usage: %s start|stop|restart|run" % sys.argv[0])
print (pidfile)
sys.exit(2)