-
Notifications
You must be signed in to change notification settings - Fork 0
/
fadecandyserver
executable file
·263 lines (219 loc) · 5.48 KB
/
fadecandyserver
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/bin/sh /etc/rc.common
#
# Startup/shutdown script for fadecandyserver handling USB
# communications with one or more Fadecandy Controller boards.
#
USE_PROCD=1
START=95
STOP=01
cfg=fadecandyserver
start_service() {
mkdir -p /tmp/etc/fcserver/
echo "Starting FadeCandy Server"
enabled=$(uci -q get fadecandyserver.fcserver.enabled)
if [ "$enabled" == "" ]; then
enabled=1
fi
if [ $enabled -eq 1 ]; then
#configfile=$(uci -q get fadecandyserver.fcserver.config)
configfile="/etc/fcserver.config"
if [ "$configfile" == "" ]; then
configfile="/tmp/etc/fcserver/config.json"
build_config
else
if [ -f $configfile ]; then
echo "Warning: Deprecated - Using legacy config file $configfile"
else
echo "Legacy Config file $configfile does not exist - exiting.."
return 0
fi
fi
procd_open_instance $cfg
procd_set_param command /usr/bin/fcserver $configfile
procd_set_param respawn
procd_set_param file $configfile
procd_set_param stdout 1
procd_set_param stderr 1
procd_close_instance
else
echo "FadeCandy Server is disabled: Exiting"
return 0
fi
}
stop_service() {
# When procd terminates fadecandyserver, it might not exit fast enough.
# So wait a little bit
sleep 1
}
build_config() {
header="{\n"
footer="}"
get_config() {
local opt=$1
local ucistr="uci -q get fadecandyserver"
retval=$($ucistr"."$opt)
}
# listen
get_config "fcserver.listen_ip"
if [ "$retval" == "" ]; then
listen_ip="0.0.0.0"
else
listen_ip=$retval
fi
get_config "fcserver.listen_port"
if [ "$retval" == "" ]; then
listen_port="7890"
else
listen_port=$retval
fi
listen="\"listen\": [\"$listen_ip\", $listen_port],"
# relay
get_config "fcserver.relay_ip"
relay_ip=$retval
get_config "fcserver.relay_port"
relay_port=$retval
if [ "$relay_ip" == "" ] || [ "$relay_port" == "" ]; then
relay=""
else
relay="\"relay\": [\"$relay_ip\", $relay_port],\n\t"
fi
# verbose
get_config "fcserver.verbose"
if [ "$retval" == "false" ]; then
verbosity="false"
else
verbosity="true"
fi
verbose="\"verbose\": $verbosity,"
color="\"color\": {"
# gamma
get_config "color.gamma"
if [ "$retval" == "" ]; then
gammastr="2.5"
else
gammastr=$retval
fi
gamma="\"gamma\": $gammastr,"
#whitepoint
get_config "color.whitepoint"
if [ "$retval" == "" ]; then
whitepointstr="[ 1.0, 1.0, 1.0 ],"
else
whitepointstr="[ $retval ],"
fi
whitepoint="\"whitepoint\": $whitepointstr"
# linearSlope
get_config "color.linearSlope"
if [ "$retval" == "" ]; then
linearSlopestr="1.0"
else
linearSlopestr=$retval
fi
linearSlope="\"linearSlope\": $linearSlopestr,"
# linearCutoff
get_config "color.linearCutoff"
if [ "$retval" == "" ]; then
linearCutoffstr="0.0"
else
linearCutoffstr=$retval
fi
linearCutoff="\"linearCutoff\": $linearCutoffstr"
# Construct server json
server="$header\t$listen\n\t$relay$verbose\n\t$color\n\t\t$gamma\
\n\t\t$whitepoint\n\t\t$linearSlope\n\t\t$linearCutoff\n\t},\n"
# Devices
dheader="\t\"devices\": [\n"
dfooter="\t]\n"
# Parse for devices
count=0
d=0
dmap=""
while [ "$(uci -q get fadecandyserver.@device[$count])" == "device" ]; do
count=$((count+1))
done
while [ "$(uci -q get fadecandyserver.@device[$d])" == "device" ]; do
# Type
type=$(uci -q get fadecandyserver.@device[$d].type)
if [ "$type" == "" ]; then
type="fadecandy"
fi
type="\t\t{\n\t\t\t\"type\": \"$type\",\n"
# Name
name=$(uci -q get fadecandyserver.@device[$d].name)
if [ "$name" != "" ]; then
name="\t\t\t\"name\": \"$name\",\n"
fi
# Serial Number
serial=$(uci -q get fadecandyserver.@device[$d].serial)
if [ "$serial" != "" ]; then
serial="\t\t\t\"serial\": \"$serial\",\n"
fi
# Device specific settings
settings=$(uci -q get fadecandyserver.@device[$d].setting)
i=1
devset=""
scount=1
while [ "$(echo $settings | awk -v j=$scount -F "' '" '{print $j}')" != "" ]; do
scount=$((scount+1))
done
while [ $i -le $scount ];do
setting=$(echo $settings | awk -v i=$i -F "' '" '{print $i}' | tr -d "'")
if [ "$setting" != "" ]; then
nam=$(echo $setting | awk -F ", " '{print $1}')
parm=$(echo $setting | awk -F ", " '{print $2}')
devset=$devset"\t\t\t\"$nam\": $parm,\n"
fi
i=$((i+1))
done
# Maps
mapconf="\t\t\t\"map\": [\n"
devmap=$(uci -q get fadecandyserver.@device[$d].map)
if [ "$devmap" == "" ]; then
devmap="*, 0, 0, 0, 512, \"rgb\""
fi
mcount=1
while [ "$(echo $devmap | awk -v j=$mcount -F "' '" '{print $j}')" != "" ]; do
mcount=$((mcount+1))
done
i=1
while [ $i -le $mcount ];do
mapobj=$(echo $devmap | awk -v i=$i -F "' '" '{print $i}' | tr -d "'")
if [ "$mapobj" != "" ]; then
f=2
mapjson=""
while true; do
field=$(echo $mapobj | awk -v j=$f -F ", " '{print $j}')
if [ "$field" != "" ]; then
if [ "$mapjson" == "" ]; then
mapjson=$mapjson$field
else
mapjson=$mapjson", "$field
fi
f=$((f+1))
else
break
fi
done
mapjson="[ $mapjson ]"
if [ $i -eq $(($mcount-1)) ]; then
mapconf="$mapconf\t\t\t\t$mapjson\n"
else
mapconf="$mapconf\t\t\t\t$mapjson,\n"
fi
fi
i=$((i+1))
done
# increment counter and add footer for each device
d=$((d+1))
mfooter="\t\t},\n"
if [ "$d" == "$count" ]; then
mfooter="\t\t}\n"
else
mfooter="\t\t},\n"
fi
thismap="$type$name$serial$devset$mapconf\t\t\t]\n$mfooter"
dmap=$dmap$thismap
done
devices=$dheader$dmap$dfooter
echo -e $server$devices$footer > /tmp/etc/fcserver/config.json
}