-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
71 lines (51 loc) · 3.1 KB
/
__init__.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
from string import Template
from pi_display_webthing.app import App
from pi_display_webthing.display_webthing import run_server
PACKAGENAME = 'pi_display_webthing'
ENTRY_POINT = "display"
DESCRIPTION = "A web connected LCD display module"
UNIT_TEMPLATE = Template('''
[Unit]
Description=$packagename
After=syslog.target network.target
[Service]
Type=simple
ExecStart=$entrypoint --command listen --hostname $hostname --verbose $verbose --port $port --name $name --i2c_address $i2c_address --i2c_expander $i2c_expander --num_lines $num_lines --num_chars $num_chars
SyslogIdentifier=$packagename
StandardOutput=syslog
StandardError=syslog
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
''')
class DhtApp(App):
def do_add_argument(self, parser):
parser.add_argument('--i2c_expander', metavar='i2c_expander', required=False, type=str, help='the name of the port I²C port expander. Supported: PCF8574, MCP23008, MCP23017')
parser.add_argument('--i2c_address', metavar='i2c_address', required=False, type=str, help='the I²C address of the LCD Module as hex string')
parser.add_argument('--name', metavar='name', required=False, type=str, default="", help='the name of the display')
parser.add_argument('--num_lines', metavar='num_lines', required=False, type=int, default=2, help='the number of lines')
parser.add_argument('--num_chars', metavar='num_chars', required=False, type=int, default=16, help='the numberof chars per line')
def do_additional_listen_example_params(self):
return "--name NAS --i2c_expander PCF8574 --i2c_address 0x27 --num_lines 2 --num_chars 16"
def do_process_command(self, command:str, hostname: str, port: int, verbose: bool, args) -> bool:
if command == 'listen' and (args.i2c_expander is not None) and (args.i2c_address is not None):
print("running " + self.packagename + " on " + hostname + ":" + str(port) + " (LCD " + str(args.num_lines) + "/" + str(args.num_chars) + ")")
run_server(hostname, port, args.name, args.i2c_expander, self.to_hex(args.i2c_address), int(args.num_lines), int(args.num_chars), self.description)
return True
elif args.command == 'register' and (args.i2c_expander is not None) and (args.i2c_address is not None):
print("register " + self.packagename + " on " + hostname + ":" + str(port) + " (LCD " + str(args.num_lines) + "/" + str(args.num_chars) + ") and starting it")
unit = UNIT_TEMPLATE.substitute(packagename=self.packagename, entrypoint=self.entrypoint, hostname=hostname, port=port, verbose=verbose, name=args.name, i2c_expander=args.i2c_expander, i2c_address=args.i2c_address, num_lines=args.num_lines, num_chars=args.num_chars)
self.unit.register(hostname, port, unit)
return True
else:
return False
def to_hex(self, hexString: str) -> int:
if hexString.startswith("0x"):
return int(hexString, 16)
else:
return int(hexString)
def main():
DhtApp(PACKAGENAME, ENTRY_POINT, DESCRIPTION).handle_command()
if __name__ == '__main__':
main()