forked from user1321/RemoteSettings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteSettings.py
347 lines (287 loc) · 11.4 KB
/
RemoteSettings.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
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import json
import configparser
import binascii
from itertools import chain
import os
import sys
import fileinput
import socket
import re
import subprocess
from subprocess import call
from tempfile import mkstemp
from shutil import move
from os import fdopen, remove
from time import sleep
import threading
WFBCSettingsFile = "/boot/wifibroadcast-1.txt"
OSDSettingsFile = "/boot/osdconfig.txt"
JoystickSettingsFile = "/boot/joyconfig.txt"
IPAndroidClient = ""
IsPhoneThredRunning = 0
ConfigResp = bytearray(b'ConfigResp')
RequestAllSettings = bytearray(b'RequestAllSettings')
RequestChangeSettings = bytearray(b'RequestChangeSettings')
RequestChangeOSD = bytearray(b'RequestChangeOSD')
RequestChangeJoystick = bytearray(b'RequestChangeJoystick')
RequestChangeTxPower = bytearray(b'RequestChangeTxPower')
wbc_settings_blacklist = [""]
def replace_OSD_config(LookFor, NewLine):
os.system('mount -o remount,rw /boot')
#example: #define COPTER true
#use: replace_OSD_config(COPTER, COPTER true)
ret = -1
print("replace_OSD_config in: Look for: " + LookFor + "NewLine: " + NewLine)
for line in fileinput.input([OSDSettingsFile], inplace=True):
if line.strip().startswith('#define ' + LookFor):
line = '#define ' + NewLine + '\n'
ret = 1
sys.stdout.write(line)
os.system('mount -o remount,ro /boot')
return ret
def replace_TxPower_config(Param, Value):
if 'txpowerA' in Param:
subprocess.Popen(["txpower_atheros", Value])
if 'txpowerR' in Param:
subprocess.Popen(["txpower_ralink", Value])
return 1
def replace_Joystick_config(LookFor, NewLine):
os.system('mount -o remount,rw /boot')
#example: #define UPDATE_NTH_TIME 10
#use: replace_OSD_config(UPDATE_NTH_TIME, UPDATE_NTH_TIME 3)
ret = -1
print("replace_OSD_config in: Look for: " + LookFor + "NewLine: " + NewLine)
for line in fileinput.input([JoystickSettingsFile], inplace=True):
if line.strip().startswith('#define ' + LookFor):
line = '#define ' + NewLine + '\n'
ret = 1
sys.stdout.write(line)
os.system('mount -o remount,ro /boot')
return ret
def replace_WFBC_config(file_path, LookFor, NewLine):
#Create temp file
os.system('mount -o remount,rw /boot')
NewLine += "\n"
fh, abs_path = mkstemp()
with fdopen(fh,'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
if line.startswith( LookFor ) == True:
new_file.write(NewLine)
else:
new_file.write(line)
#Remove original file
remove(file_path)
#Move new file
move(abs_path, file_path)
os.system('mount -o remount,ro /boot')
def read_osd_settings(response_header):
d = {}
with open (OSDSettingsFile, 'rt') as file: # Open file lorem.txt for reading of text data.
for line in file: # Store each line in a string variable "line"
if 'define COPTER true' in line:
d['Copter'] = "true"
print("copter = true")
if 'define COPTER false' in line:
d['Copter'] = "false"
print("Copter = false")
if 'define IMPERIAL true' in line:
d['Imperial'] = "true"
print("Imperial = true")
if 'define IMPERIAL false' in line:
d['Imperial'] = "false"
print("Imperial = false")
response_header['settings'].update(d)
return response_header
def read_txpower_settings(response_header):
txp = {}
processA = subprocess.Popen(["sed", "s/.*txpower=\\([0-9]*\\).*/\\1/", "/etc/modprobe.d/ath9k_hw.conf"], stdout=subprocess.PIPE)
ret_txpowerA = processA.communicate()[0]
txpowerA = re.sub("[^0-9]+", "", ret_txpowerA.decode('utf-8'))
processR = subprocess.Popen(["sed", "s/.*txpower=\\([0-9]*\\).*/\\1/", "/etc/modprobe.d/rt2800usb.conf"], stdout=subprocess.PIPE)
ret_txpowerR = processR.communicate()[0]
txpowerR = re.sub("[^0-9]+", "", ret_txpowerR.decode('utf-8'))
txp['txpowerR'] = txpowerR
txp['txpowerA'] = txpowerA
print("txpowerR: ")
print(txp['txpowerR'])
print("txpowerA: ")
print(txp['txpowerA'])
response_header['settings'].update(txp)
return response_header
def read_joystick_settings(response_header):
d = {}
with open (JoystickSettingsFile, 'rt') as file: # Open file lorem.txt for reading of text data.
for line in file: # Store each line in a string variable "line"
if '#define UPDATE_NTH_TIME' in line:
splitResult = line.split("UPDATE_NTH_TIME")
filter = re.sub("\D", "", splitResult[1])
d['UPDATE_NTH_TIME'] = filter
print("Result: ")
print(d['UPDATE_NTH_TIME'])
response_header['settings'].update(d)
return response_header
def read_wbc_settings(response_header):
virtual_section = 'root'
settings = {}
config = configparser.ConfigParser()
config.optionxform = str
with open(WFBCSettingsFile, 'r') as lines:
lines = chain(('[' + virtual_section + ']',), lines)
config.read_file(lines)
for key in config[virtual_section]:
if key not in wbc_settings_blacklist:
settings[key] = config.get(virtual_section, key)
response_header['settings'] = settings
return response_header
def SendData(IP, MessageBuf):
global IPAndroidClient
UDP_PORT = 5115
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sock.sendto( MessageBuf, (IPAndroidClient, UDP_PORT))
def SendDataToWFBC(MessageBuf):
global IPAndroidClient
UDP_PORT = 9090
sockToAir = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
sockToAir.sendto( bytes(MessageBuf,'utf-8'), ('127.0.0.1', UDP_PORT))
complete_response = {}
read_wbc_settings(complete_response)
read_txpower_settings(complete_response)
read_osd_settings(complete_response)
read_joystick_settings(complete_response)
def SendAllSettingToPhone():
for te in complete_response['settings']:
SendBuff = bytearray()
#Header
SendBuff[0:0] = ConfigResp
#Form Payload. Header from 0 to 19 byte, 20+ data
ValuePayload = te.encode('utf-8')
SendBuff.extend(te.encode('utf-8') )
SendBuff.extend(str.encode("=",'utf-8') )
data = complete_response['settings'][te]
ValueDataPayload = data.encode('utf-8')
SendBuff.extend(ValueDataPayload)
global IPAndroidClient
SendData(IPAndroidClient,SendBuff)
print("v :", SendBuff)
SendBuff.clear()
sleep(0.02)
def ConfirmSave(VarName):
SendBuffSave = bytearray()
SendBuffSave[0:0] = ConfigResp
PayLoad = VarName.encode('utf-8')
SendBuffSave.extend(PayLoad)
SendBuffSave.extend(str.encode("=SavedGround",'utf-8') )
global IPAndroidClient
SendData(IPAndroidClient,SendBuffSave)
print("confirm save Ground RPi to Phone: ", SendBuffSave)
SendBuffSave.clear()
def ForwardMessageToRPiAir(VarName):
print("ForwardMessageToRPiAir ")
print(VarName)
SendDataToWFBC(VarName)
def WaitForRPiAirACK():
print("ack")
def PhoneConnected():
print("Phone connected. Say Hello to Air unit.")
for i in range(60):
SendDataToWFBC("PhoneConnected")
sleep(1)
UDP_IP = ""
UDP_PORT = 1011
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
IPAndroidClient = addr[0]
print("ip: ", IPAndroidClient)
if data == RequestAllSettings:
SendAllSettingToPhone()
if IsPhoneThredRunning == 0:
IsPhoneThredRunning = 1
tHandler = threading.Thread(target=PhoneConnected)
tHandler.start()
if RequestChangeSettings in data:
DataStr = data.decode('utf-8')
parts = DataStr.split(RequestChangeSettings.decode('utf-8') )
VariableNamAndData = parts[1]
VariableNamAndDataArr = VariableNamAndData.split("=")
VariableNam = VariableNamAndDataArr[0]
VariableNameReport = VariableNamAndDataArr[0]
Data = VariableNamAndDataArr[1]
VariableNam = VariableNam + "="
#add sync conde
ForwardMessageToRPiAir(DataStr)
replace_WFBC_config(WFBCSettingsFile,VariableNam,VariableNamAndData)
ConfirmSave(VariableNameReport)
complete_response = {}
read_wbc_settings(complete_response)
read_txpower_settings(complete_response)
read_osd_settings(complete_response)
read_joystick_settings(complete_response)
#change OSD settings
if RequestChangeOSD in data:
DataStr = data.decode('utf-8')
parts = DataStr.split(RequestChangeOSD.decode('utf-8') )
VariableNamAndData = parts[1]
print("VariableNamAndData: " + VariableNamAndData)
VariableNamAndDataArr = VariableNamAndData.split("=")
VariableNam = VariableNamAndDataArr[0]
VariableNameReport = VariableNamAndDataArr[0]
Data = VariableNamAndDataArr[1]
#add sync conde
ForwardMessageToRPiAir(DataStr)
ret = replace_OSD_config(VariableNam.upper(), VariableNam.upper() + " " + Data)
if ret == 1:
ConfirmSave(VariableNameReport)
#reload values from disk to memory
complete_response = {}
read_wbc_settings(complete_response)
read_txpower_settings(complete_response)
read_osd_settings(complete_response)
read_joystick_settings(complete_response)
#change Joystick settings
if RequestChangeJoystick in data:
DataStr = data.decode('utf-8')
parts = DataStr.split(RequestChangeJoystick.decode('utf-8') )
VariableNamAndData = parts[1]
print("VariableNamAndData: " + VariableNamAndData)
VariableNamAndDataArr = VariableNamAndData.split("=")
VariableNam = VariableNamAndDataArr[0]
VariableNameReport = VariableNamAndDataArr[0]
Data = VariableNamAndDataArr[1]
#add sync conde
ForwardMessageToRPiAir(DataStr)
ret = replace_Joystick_config(VariableNam.upper(), VariableNam.upper() + " " + Data)
if ret == 1:
ConfirmSave(VariableNameReport)
#reload values from disk to memory
complete_response = {}
read_wbc_settings(complete_response)
read_txpower_settings(complete_response)
read_osd_settings(complete_response)
read_joystick_settings(complete_response)
#change TX Power settings
if RequestChangeTxPower in data:
DataStr = data.decode('utf-8')
parts = DataStr.split(RequestChangeTxPower.decode('utf-8') )
VariableNamAndData = parts[1]
print("VariableNamAndData: " + VariableNamAndData)
VariableNamAndDataArr = VariableNamAndData.split("=")
VariableNam = VariableNamAndDataArr[0]
VariableNameReport = VariableNamAndDataArr[0]
Data = VariableNamAndDataArr[1]
#add sync conde
ForwardMessageToRPiAir(DataStr)
ret = replace_TxPower_config(VariableNam, Data)
if ret == 1:
ConfirmSave(VariableNameReport)
#reload values from disk to memory
complete_response = {}
read_wbc_settings(complete_response)
read_txpower_settings(complete_response)
read_osd_settings(complete_response)
read_joystick_settings(complete_response)
print("received message:", data)
sys.exit()
#OLD