-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathpistar-bmapi
executable file
·167 lines (157 loc) · 5.02 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
#!/bin/bash
#
##############################################################################
# #
# Pi-Star BrandMeister API Tool #
# #
# Version 1.10, 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
# Figure out the DMR ID
DMRNetHost=$(sed -n '/^\[DMR Network\]/,/^\[/p' /etc/mmdvmhost | grep "^Address=" | head -n 1 | awk -F "=" '{print $2}')
if [[ "${DMRNetHost}" == "127.0.0.1" ]]; then
# Using DMRGateway
DMRID=$(sed -n '/^\[DMR Network 1\]/,/^\[/p' /etc/dmrgateway | grep "^Id" | awk -F "=" '{print $2}')
else
# Using MMDVMHost
DMRMMDVMHostID=$(sed -n '/^\[DMR\]/,/^\[/p' /etc/mmdvmhost | grep "^Id" | awk -F "=" '{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 | awk -F "=" '{print $2}')
fi
fi
# Fix edge case where there is some additional chars in the DMR ID Fields (DF9FZ)
DMRID=${DMRID//$'\r'/}
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 ""
exit 0
fi
# Get the stored BM API Key
if [ -f ${APIKeyFile} ]; then
if [[ $(wc -c ${APIKeyFile} | awk '{print $1}') -gt 200 ]]; then
echo -e "Using BM API v2"
APIURL="https://api.brandmeister.network/v2/device/"
APIKey=$(grep -m 1 'apikey=' ${APIKeyFile} | awk -F "=" '/apikey/ {print $2}')
APIVer="2"
else
echo -e "Using BM API v1.0"
echo -e "Please obtain a new BM API Key, v1 keys are no longer supported!"
exit 0
fi
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}${DMRID}/talkgroup"
APIDATA="{ \"slot\": ${TS}, \"group\": ${targetTG} }"
TYPE="POST"
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}${DMRID}/talkgroup/${TS}/${targetTG}"
TYPE="DELETE"
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}${DMRID}/action/dropDynamicGroups/${TS}"
TYPE="GET"
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}${DMRID}/action/dropCallRoute/${TS}"
TYPE="GET"
REQUEST="Drop Active QSO on TimeSlot ${TS} for DMR ID ${DMRID}"
;;
*)
echo "ERROR: Unknown Command Specified"
exit 0
;;
esac
# Do the magic
echo " Request to BrandMesiter API: ${REQUEST}"
RESULT=$(curl -s -X "${TYPE}" "${APIURL}" -A "Pi-Star cli tool for: ${DMRID}" -H "accept: application/json" -H "Authorization: Bearer ${APIKey}" -H "Content-Type: application/json" -d "${APIDATA}" > /dev/null 2>&1 && echo OK || echo ERROR)
echo "Answer from BrandMesiter API: ${RESULT}"
echo ""
exit 0