-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunserver.py
More file actions
executable file
·113 lines (106 loc) · 3.23 KB
/
Copy pathrunserver.py
File metadata and controls
executable file
·113 lines (106 loc) · 3.23 KB
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import os
import hub
import sys
import getopt
import thread
from hub import app
from hub.logger import Log
from hub.webapi import WebAPI
from hub.models import Printer, Node
from hub.dealer import PrinterCollector, NodeCollector
from hub.database import init_db
debug = False
port = None
host = None
threaded = False
print_enabled = False
config = None
def set_args(args):
global hub
global config
global debug
global host
global threaded
global port
global print_enabled
try:
opts, args = getopt.getopt(args, "hdtvp:a:w:c:",
["apikey=","pass=","help",
"port=","debug","host=",
"threaded","verbose","weburl=",
"config="])
except getopt.GetoptError, err:
# Print debug info
print str(err)
sys.exit(2)
for opt, arg in opts:
if opt in ["-h", "--help"]:
print "Usage: Server.py"\
+ "\n -c/--config <arguments.config file>"\
+ "\n load config file for arguments. "\
+ "\n each argument on it's own line"\
+ "\n with a space between option and argument"\
+ "\n -w/--weburl <web url>"\
+ "\n -a/--apikey <apikey>"\
+ "\n -p/--port <port>"\
+ "\n --pass <password>"\
+ "\n --host <ip address>"\
+ "\n -d/--debug"\
+ "\n -h/--help"\
+ "\n -t/--threaded"\
+ "\n -v/--verbose"
sys.exit(0)
elif opt in ["-c", "--config"]:
config = arg
elif opt in ["-w", "--weburl"]:
hub.WEB_API_URL = arg
elif opt in ["-a", "--apikey"]:
hub.WEB_API_KEY = arg
elif opt in ["--pass"]:
hub.SND_PASSWD = arg
elif opt in ["-p", "--port"]:
port = int(arg)
elif opt in ["-d", "--debug"]:
debug = True
elif opt in ["--host"]:
host = arg
elif opt in ["-t", "--threaded"]:
threaded = True
elif opt in ["-v", "--verbose"]:
print_enabled = True
def load_config(config):
path = os.path.abspath(config)
l = []
with open(path, "r") as f:
for line in f:
if line[0] == "#":
continue
l.extend(line.split())
set_args(l)
return 0
set_args(sys.argv[1:])
if config != None:
load_config(config)
hub.log = Log(print_enabled=print_enabled)
hub.log.log("Starting up HUB webserver")
hub.Webapi = WebAPI(hub.WEB_API_URL, hub.WEB_API_KEY, hub.log)
hub.Webapi.sign_in()
init_db()
updates = {"nodes": []}
node_updates = updates.get("nodes")
for printer in Printer.get_all():
id = printer.id
t = PrinterCollector(id, hub.Webapi, hub.log)
t.start()
hub.printer_listeners.add_thread(id, t)
for node in Node.get_all():
id = node.id
if node.printer_id == None:
node_updates.append(id)
t = NodeCollector(id, hub.Webapi, hub.log)
t.start()
hub.node_listeners.add_thread(id, t)
hub.Webapi.update_nodes(updates)
app.run(host=host, debug=debug, port=port,threaded=threaded)