-
Notifications
You must be signed in to change notification settings - Fork 35
/
userSettingsHandler.py
179 lines (154 loc) · 6.72 KB
/
userSettingsHandler.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
172
173
174
175
176
177
178
179
###
#
# WEIO Web Of Things Platform
# Copyright (C) 2013 Nodesign.net, Uros PETREVSKI, Drasko DRASKOVIC
# All rights reserved
#
# ## ## ######## #### #######
# ## ## ## ## ## ## ##
# ## ## ## ## ## ## ##
# ## ## ## ###### ## ## ##
# ## ## ## ## ## ## ##
# ## ## ## ## ## ## ##
# ### ### ######## #### #######
#
# Web Of Things Platform
#
# This file is part of WEIO and is published under BSD license.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
# must display the following acknowledgement:
# This product includes software developed by the WeIO project.
# 4. Neither the name of the WeIO nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY WEIO PROJECT AUTHORS AND CONTRIBUTORS ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL WEIO PROJECT AUTHORS AND CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# Authors :
# Uros PETREVSKI <uros@nodesign.net>
# Drasko DRASKOVIC <drasko.draskovic@gmail.com>
#
###
from os.path import isfile, join
from sockjs.tornado import SockJSRouter, SockJSConnection
import functools
import json
# IMPORT BASIC CONFIGURATION FILE
from weioLib import weioConfig
import subprocess
import platform
clients = set()
class WeioSettingsHandler(SockJSConnection):
def __init__(self, *args, **kwargs):
SockJSConnection.__init__(self, *args, **kwargs)
self.errObject = []
self.errReason = ""
self.callbacks = {
'updateSettings' : self.updateUserData,
'updataNetwork' : self.updateNetworkData
}
def updateUserData(self, rq):
data = {}
self.user = rq['data']['user']
self.password = rq['data']['password']
self.login_required = rq['data']['login_required']
self.play_composition_on_server_boot = rq['data']['play_composition_on_server_boot']
config = weioConfig.getConfiguration()
config["user"] = self.user
config["play_composition_on_server_boot"] = self.play_composition_on_server_boot
config["login_required"] = self.login_required
# Check if new password is sent
if self.password:
config["password"] = self.password
# ATTENTION, DON'T MESS WITH THIS STUFF ON YOUR LOCAL COMPUTER
# First protection is mips detection, second is your own OS
# who hopefully needs sudo to change passwd on the local machine
if (platform.machine() == 'mips'):
# Change root password
command = "sh scripts/change_root_pswd.sh " + self.password
try:
subprocess.call(command, shell=True)
firstTimeSwitch = "NO"
config['first_time_run']=firstTimeSwitch
data['data'] = "msg_success"
except:
output = "ERR_CMD PASSWD"
data['data'] = "msg_fail"
print output
else:
# On PC
firstTimeSwitch = "NO"
config['first_time_run']=firstTimeSwitch
data['data'] = "msg_success"
# Save new user data in config file
weioConfig.saveConfiguration(config);
data['requested'] = "updateSettings"
self.send(json.dumps(data))
def updateNetworkData(self, rq):
data = {}
self.dns_name = rq['data']['dns_name']
self.auto_to_ap = rq['data']['auto_to_ap']
self.timezone = rq['data']['timezone']
config = weioConfig.getConfiguration()
config['dns_name'] = self.dns_name + ".local"
config['auto_to_ap'] = self.auto_to_ap
# Check timezone
if self.timezone:
config["timezone"] = self.timezone
if (platform.machine() == 'mips'):
# Change avahi name
command = "sh scripts/change_boardname.sh " + self.dns_name
if self.timezone:
commandConfig = "uci set system.@system[0].timezone=" + self.timezone # Set timezone on openwrt config (required for system reboot)
commandCommitConfig = "uci commit system.@system[0].timezone"
try:
subprocess.call(command, shell=True)
subprocess.call(commandConfig, shell=True)
subprocess.call(commandCommitConfig, shell=True)
firstTimeSwitch = "NO"
config['first_time_run']=firstTimeSwitch
data['data'] = "msg_success"
except:
output = "ERR_CMD BRDNAME"
data['data'] = "msg_fail"
print output
else:
# On PC
firstTimeSwitch = "NO"
config['first_time_run']=firstTimeSwitch
data['data'] = "msg_success"
# Save new user data in config file
weioConfig.saveConfiguration(config);
data['requested'] = "updataNetwork"
self.send(json.dumps(data))
def on_message(self, data):
"""Parsing JSON data that is comming from browser into python object"""
req = json.loads(data)
self.serve(req)
def serve(self, rq):
request = rq['request']
if request in self.callbacks :
self.callbacks[request](rq)
else :
print "unrecognised request"
def on_close(self):
global clients
# Remove client from the clients list and broadcast leave message
clients.remove(self)