/
rs232server
executable file
·116 lines (104 loc) · 3.98 KB
/
rs232server
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
#!/usr/bin/env python3
# Copyright (C) 2011-2020 Brendan Le Foll <brendan@fridu.net>
#
# 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, either version 2 of the License, or
# (at your option) any later version.
#
# 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, see <http://www.gnu.org/licenses/>.
import argparse
import configparser
import dbus
import importlib
import logging
import os
import sys
from dbus.mainloop.glib import DBusGMainLoop
from gi.repository import GLib
import rs232modules.Shared
from rs232modules.BaseService import invalidtty
LOG_FILENAME = '/tmp/' + rs232modules.Shared.APP_NAME + '.log'
LOG_FORMAT = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
RS232SERVER_BUS_NAME = 'uk.co.madeo.' + rs232modules.Shared.APP_NAME
DESCRIPTION = "Listen over dbus for commands to be sent over RS232"
def configureLogging(verbose, dev, logger):
if dev:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
formatter = logging.Formatter(LOG_FORMAT)
try:
fh = logging.FileHandler(LOG_FILENAME)
except:
print('Check you have write permissions to', LOG_FILENAME)
exit(1)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
ch = logging.StreamHandler()
if verbose:
ch.setLevel(logging.DEBUG)
else:
ch.setLevel(logging.ERROR)
ch.setFormatter(formatter)
logger.addHandler(ch)
def main():
# parse CLI arguments
parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument('--verbose', '-v', action='store_true', dest='verbose', default=False,
help='enables more verbose output')
parser.add_argument('--dev', '-d', action='store_true', dest='dev', default=False,
help='development mode disables some commands that could be dangerous. Read documentation before using')
args = parser.parse_args()
# set up logging
logger = logging.getLogger(rs232modules.Shared.APP_NAME)
configureLogging(args.verbose, args.dev, logger)
# read configuration file according to priorities
parser = configparser.ConfigParser()
filenames = [rs232modules.Shared.SYS_CONF, os.path.expanduser(rs232modules.Shared.USER_CONF), rs232modules.Shared.DEV_CONF]
readfiles = parser.read(filenames)
logger.debug('config files read: ' + str(readfiles)[1:-1])
if not readfiles:
logger.error('failed to read any configuration files. Looked in ' + str(filenames)[1:-1])
exit(1)
# start dbus mainloop
DBusGMainLoop(set_as_default=True)
try:
bus_name = dbus.service.BusName(RS232SERVER_BUS_NAME, bus=dbus.SystemBus())
except:
logger.info(sys.exc_info()[0])
logger.error('fatal dbus error: have you set up a dbus config?')
exit(1)
for section in parser.sections():
try:
tty = parser.get(section, 'tty')
logger.debug("service is %s", str(section))
x = getattr(importlib.import_module("rs232modules." + section), section)
service = x(tty, bus_name)
try:
service.set_model(parser.get(section, 'model'))
except Exception as e:
logger.debug('No model found in config file %s', str(e))
#if not issubclass(service, BaseService):
# raise Exception("Class is not subclass of BaseService")
except configparser.NoOptionError as e:
logger.error('%s in %s', str(e), str(readfiles)[1:-1])
exit(1)
except invalidtty as e:
logger.error('%s', str(e))
exit(1)
except:
logger.debug(sys.exc_info()[0])
logger.error('Check that %s is a valid service name', section)
exit(1)
loop = GLib.MainLoop()
loop.run()
if __name__ == "__main__":
sys.exit(main())