forked from AndyTaylorTweet/Pi-Star_Binaries_sbin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpistar-bmapi
executable file
·184 lines (173 loc) · 5.56 KB
/
pistar-bmapi
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
#!/bin/bash
#
##############################################################################
# #
# Pi-Star BrandMeister API Tool #
# #
# Version 1.03, Code, Design and Development by Andy Taylor (MW0MWZ). #
# #
# Make it simple to link / unlink reflectors from the CLI on Pi-Star. #
# Added the option to manage static/dynamic TalkGroups and to be able #
# to drop the current QSO. #
# #
##############################################################################
#
if [ "$(id -u)" != "0" ]; then
echo -e "You need to be root to run this command...\n"
exit 1
fi
# Setup some variables
APIKeyFile=/etc/bmapi.key
APIURL="https://api.brandmeister.network/v1.0/repeater/"
# Figure out the DMR ID
DMRNetHost=$(sed -n -e '/\[DMR Network\]/,/\[System Fusion Network\]/p' /etc/mmdvmhost | grep "Address=" | awk -F "=" '/Address=/ {print $2}')
if [[ "${DMRNetHost}" == "127.0.0.1" ]]; then
# Using DMRGateway
DMRID=$(sed -n -e '/DMR Network 1/,/DMR Network 2/p' /etc/dmrgateway | grep "Id" | awk -F "=" '/Id=/ {print $2}')
else
# Using MMDVMHost
DMRMMDVMHostID=$(sed -n -e '/\[DMR\]/,/\[System Fusion\]/p' /etc/mmdvmhost | grep "Id" | awk -F "=" '/Id=/ {print $2}')
if [[ ${DMRMMDVMHostID} ]]; then
# ID Specified in the DMR section
DMRID=${DMRMMDVMHostID}
else
# No ID in the DMR Section
DMRID=$(grep -m 1 'Id=' /etc/mmdvmhost | tac | tail -1 | awk -F "=" '/Id/ {print $2}')
fi
fi
if [ -z "$1" ]
then
echo "NOTE: In the below example commands, please replace {TG Number} with"
echo "the number of the Talkgroup you wish to add/remove. For example 12345."
echo ""
echo "Similarly the {TimeSlot} should be replaced with your target time"
echo "slot (0, 1, 2), for hotspots it is \"0\" (you can also omit the TimeSlot"
echo "value), for duplex repeaters it will be either \"1\" for TS1 or \"2\" for TS2."
echo ""
echo "Reflector numbers are from 4000 to 5000"
echo ""
echo "To add a static TG: pistar-bmapi addtg {TG Number} {TimeSlot Number}"
echo "To remove a static TG: pistar-bmapi deltg {TG Number} {TimeSlot Number}"
echo "To drop all Dynamic TGs: pistar-bmapi dropdyn {TimeSlot Number}"
echo "To drop the current QSO: pistar-bmapi dropqso {TimeSlot Number}"
echo "To set the current position: pistar-bmapi setpos {Lattitude} {Longitude} {Altitude}"
echo ""
exit 0
fi
# Get the stored BM API Key
if [ -f ${APIKeyFile} ]; then
APIKey=$(grep -m 1 'apikey=' ${APIKeyFile} | awk -F "=" '/apikey/ {print $2}')
APIKeyB64=$(echo -n "${APIKey}:" | base64 | sed 's/=$*//g' | sed ':a;N;$!ba;s/\n/ /g' | sed 's/ //g')
else
echo "Unable to find your BrandMesiter API Key, no API commands available."
exit 0
fi
case ${1} in
apikey)
echo "Your BrandMeister API Key is: ${APIKey}"
exit 0
;;
dmrid)
echo "Your DMR ID is: ${DMRID}"
exit 0
;;
addtg)
# Check that the variable for TalkGroup has been passed
if [ -z "$2" ]
then
echo "No TG Specfified"
exit 0
else
targetTG=$2
fi
# Check that the variable to TimeSlot has been passed
if [ -z "$3" ]
then
TS="0"
else
TS=$3
fi
APIURL="${APIURL}talkgroup/?action=ADD&id=${DMRID}"
APIDATA="talkgroup=${targetTG}×lot=${TS}"
REQUEST="Add TG ${targetTG} to TimeSlot ${TS} for DMR ID ${DMRID}"
;;
deltg)
# Check that the variable for TalkGroup has been passed
if [ -z "$2" ]
then
echo "No TG Specfified"
exit 0
else
targetTG=$2
fi
# Check that the variable to TimeSlot has been passed
if [ -z "$3" ]
then
TS="0"
else
TS=$3
fi
APIURL="${APIURL}talkgroup/?action=DEL&id=${DMRID}"
APIDATA="talkgroup=${targetTG}×lot=${TS}"
REQUEST="Delete TG ${targetTG} from TimeSlot ${TS} for DMR ID ${DMRID}"
;;
dropdyn)
# Check that the variable to TimeSlot has been passed
if [ -z "$2" ]
then
TS="0"
else
TS=$2
fi
APIURL="${APIURL}setRepeaterTarantool.php?action=dropDynamicGroups&slot=${TS}&q=${DMRID}"
REQUEST="Drop all Dynamic TG on TimeSlot ${TS} for DMR ID ${DMRID}"
;;
dropqso)
# Check that the variable to TimeSlot has been passed
if [ -z "$2" ]
then
TS="0"
else
TS=$2
fi
APIURL="${APIURL}setRepeaterDbus.php?action=dropCallRoute&slot=${TS}&q=${DMRID}"
REQUEST="Drop Active QSO on TimeSlot ${TS} for DMR ID ${DMRID}"
;;
setpos)
if [ -z "$2" ]
then
echo "ERROR: Latitude not specified"
exit 0
else
LAT=$2
fi
if [ -z "$3" ]
then
echo "ERROR: Longitude not specified"
exit 0
else
LON=$3
fi
if [ -z "$4" ]
then
AGL="0"
else
AGL=$4
fi
APIURL="${APIURL}?action=EDIT&id=${DMRID}"
APIDATA="lat=${LAT}&lng=${LON}&agl=${AGL}"
REQUEST="Set the repeater location to Lattitude: ${LAT} Longitude: ${LON} and Altitude: ${AGL}"
;;
*)
echo "ERROR: Unknown Command Specified"
exit 0
;;
esac
# Do the magic
echo " Request to BrandMesiter API: ${REQUEST}"
#RESULT=$(curl -s --user "${APIKey}:" --data "${APIDATA}" "${APIURL}" | \
RESULT=$(wget -q -O - --user-agent="Pi-Star cli tool for: ${DMRID}" --header="Content-Type: application/x-www-form-urlencoded" --header="Authorization: Basic ${APIKeyB64}:" --post-data="${APIDATA}" "${APIURL}" | \
python3 -c "import sys, json; print(json.load(sys.stdin)['message'])")
echo "Answer from BrandMesiter API: ${RESULT}"
echo ""
exit 0