forked from PiSupply/iot-lora-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatePacketfwd.php
executable file
·189 lines (135 loc) · 5.42 KB
/
updatePacketfwd.php
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
<?php
/*
*IoT LoRa Gateway Controller
*Copyright (C) 2018-2019 Nebra LTD. T/a Pi Supply
*This program is free software: you can redistribute it and/or modify
*it under the terms of the GNU General Public License as published by
*the Free Software Foundation, either version 3 of the License, or
*(at your option) any later version.
*
*This program is distributed in the hope that it will be useful,
*but WITHOUT ANY WARRANTY; without even the implied warranty of
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
*GNU General Public License for more details.
*
*You should have received a copy of the GNU General Public License
*along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
include('inc/header.php');
$configHandler = fopen($configLocation, 'r');
$currentConfig = fread($configHandler, filesize($configLocation));
fclose($configHandler);
$jsonDecoded = json_decode($currentConfig,true);
//var_dump($_POST); //For Dev Only
//TheGatewayID Is based off of the mac and starts with PIS
$macAddress = substr(trim(shell_exec("cat /proc/cpuinfo | grep ^Serial")), -10);
$gatewayId = "504953".$macAddress;
$jsonDecoded['gateway_conf']['gateway_ID'] = $gatewayId;
if (php_sapi_name() != "cli") {
if($_POST['semtech']) {
$jsonDecoded['gateway_conf']['servers'][0]['serv_type'] = "semtech";
$jsonDecoded['gateway_conf']['ref_latitude'] = floatval($_POST['latitude']);
//Longitude
$jsonDecoded['gateway_conf']['ref_longitude'] = floatval($_POST['longitude']);
//Altitude
$jsonDecoded['gateway_conf']['ref_altitude'] = intval($_POST['altitude']);
//Ports
$jsonDecoded['gateway_conf']['servers'][0]['serv_port_up'] = 1700;
$jsonDecoded['gateway_conf']['servers'][0]['serv_port_down'] = 1700;
//Description
$jsonDecoded['gateway_conf']['description'] = $_POST['description'];
$jsonDecoded['gateway_conf']['servers'][0]['server_address'] = $_POST['serverAdd'];
$regionSelected = $_POST['regionPlan'];
$globalConfTemp = "/opt/iotloragateway/global_confs/".$regionSelected."-global_conf.json";
$freqPlan = fopen($globalConfTemp, 'r');
var_dump($freqPlan);
$freqPlanRead= fread($freqPlan, filesize($globalConfTemp));
$freqHandler = fopen($globalConfigLocation, 'w');
//var_dump($freqHandler);
fwrite($freqHandler, $freqPlanRead);
fclose($freqHandler);
//echo("Semtech on");
}
else {
$jsonDecoded['gateway_conf']['servers'][0]['serv_type'] = "ttn";
}
//These values will be updated only if not run by the CLI
//This gateway ID Is the TTN Name, only if the value is not null
if($_POST['gatewayId']) {
$jsonDecoded['gateway_conf']['servers'][0]['serv_gw_id'] = $_POST['gatewayId'];
}
//TTN key
if($_POST['ttnKey']) {
$jsonDecoded['gateway_conf']['servers'][0]['serv_gw_key'] = $_POST['ttnKey'];
}
//Contact Email
if($_POST['email']) {
$jsonDecoded['gateway_conf']['contact_email'] = $_POST['email'];
}
if($_POST['gps']) {
$jsonDecoded['gateway_conf']['gps'] = true;
$jsonDecoded['gateway_conf']['fake_gps'] = false;
$jsonDecoded['gateway_conf']['gps_tty_path'] = "/dev/ttyAMA0";
}
else {
$jsonDecoded['gateway_conf']['gps'] = false;
$jsonDecoded['gateway_conf']['fake_gps'] = true;
}
}
#Only do the following if it's a TTN Server
if($jsonDecoded['gateway_conf']['servers'][0]['serv_type'] == "ttn") {
$ttnApiUrl = "https://account.thethingsnetwork.org/api/v2/gateways/".$jsonDecoded['gateway_conf']['servers'][0]['serv_gw_id'];
$ttnApiData = json_decode(file_get_contents($ttnApiUrl),true);
$frequencyPlan = file_get_contents($ttnApiData['frequency_plan_url']);
//We need to set the following
//TTN Server Address
$serverAddress = explode(":",$ttnApiData['router']['address'])[0];
//
if(strstr($serverAddress, "thethings.network")
) {
$serverAddress = "bridge.".$serverAddress;
}
else {
$serverAddress = $serverAddress;
}
$jsonDecoded['gateway_conf']['servers'][0]['server_address'] = $serverAddress;
//description
$jsonDecoded['gateway_conf']['description'] = $ttnApiData['attributes']['description'];
//currently GPS Is false and FakeGPS Is true
//Serv type and enabled
#$jsonDecoded['gateway_conf']['servers'][0]['serv_type'] = "ttn";
$jsonDecoded['gateway_conf']['servers'][0]['serv_enabled'] = true;
//Latitude
$jsonDecoded['gateway_conf']['ref_latitude'] = $ttnApiData['location']['lat'];
//Longitude
$jsonDecoded['gateway_conf']['ref_longitude'] = $ttnApiData['location']['lng'];
//Altitude
$jsonDecoded['gateway_conf']['ref_altitude'] = $ttnApiData['altitude'];
//Frequency Plan Updater
$freqHandler = fopen($globalConfigLocation, 'w');
$freqPlanJson = json_decode($frequencyPlan,true);
//Implement workaround for As920 and as923
if($freqPlanJson['SX1301_conf']['lbt_cfg']) {
$freqPlanJson['SX1301_conf']['lbt_cfg']['enable'] = false;
}
$jsonFreqEncoded = json_encode($freqPlanJson, JSON_PRETTY_PRINT);
fwrite($freqHandler, $jsonFreqEncoded);
fclose($freqHandler);
}
$jsonEncoded = json_encode($jsonDecoded, JSON_PRETTY_PRINT);
$configHandler = fopen($configLocation, 'w');
fwrite($configHandler, $jsonEncoded);
fclose($configHandler);
if (php_sapi_name() != "cli") {
echo('
<div class="row align-items-center">
<div class="text-center">
<h1>Packet Forwarder Configuration Tool</h1>
<h4>Configuration has been written, please reboot the packet forwarder from the commands tab.</h4>
</div>
</div>
'
);
}
include('inc/footer.php');
?>