-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathadd-charger.sh
executable file
·176 lines (149 loc) · 5.22 KB
/
add-charger.sh
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
#!/usr/bin/env bash
# Configuration
DIRECTUS_API_URL="http://localhost:8055"
CHARGEPOINT_ID="cp001"
CP_PASSWORD="DEADBEEFDEADBEEF"
DIRECTUS_EMAIL="[email protected]"
DIRECTUS_PASSWORD="CitrineOS!"
#TODO put github images
PROJECT_LOGO_IMAGE_URL="https://public-citrineos-logo.s3.amazonaws.com/Citrine-Directus-Project-Logo.png"
PUBLIC_BACKGROUND_IMAGE_URL="https://public-citrineos-logo.s3.amazonaws.com/Citrine-Directus-Public-Background.png"
# Function to get the Directus token
get_directus_token() {
local login_url="${DIRECTUS_API_URL}/auth/login"
local json_body=$(printf '{"email": "%s", "password": "%s"}' "$DIRECTUS_EMAIL" "$DIRECTUS_PASSWORD")
local response=$(curl -s -X POST "$login_url" -H "Content-Type: application/json" -d "$json_body")
# Extract token from the response
local token=$(jq -r '.data.access_token' <<< "$response")
echo "$token"
}
# Function to upload an image via URL to Directus
upload_image() {
local token=$1
local image_url=$2
local title=$3
local response=$(curl -s -X POST "${DIRECTUS_API_URL}/files/import" \
-H "Authorization: Bearer ${token}" \
-H "Content-Type: application/json" \
-d "{
\"url\": \"${image_url}\",
\"data\": {
\"title\": \"${title}\"
}
}"| tee /dev/tty && echo)
local file_id=$(jq -r '.data.id' <<< "$response")
echo "$file_id"
}
# Function to set the project image
set_project_image() {
local token=$1
local project_logo=$2
local project_background=$3
curl -s -X PATCH "${DIRECTUS_API_URL}/settings" \
-H "Authorization: Bearer ${token}" \
-H "Content-Type: application/json" \
-d "{
\"project_logo\": \"${project_logo}\",
\"public_background\": \"${project_background}\"
}" | tee /dev/tty && echo
}
# Function to add a new location
add_location() {
local token=$1
local response=$(curl -s -X POST "${DIRECTUS_API_URL}/items/Locations" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" \
-d '{
"id": "2",
"name": "New EVerst",
"coordinates": {
"type": "Point",
"coordinates": [-74.0620872, 41.041548]
}
}' | tee /dev/tty && echo)
local location_id=$(jq -r '.data.id' <<< "$response")
echo "$location_id"
}
# Function to add a charging station
add_charging_station() {
local token="$1"
local location_id="$2"
local chargepointId="$3"
curl -s --request POST \
--url "${DIRECTUS_API_URL}/items/ChargingStations" \
--header "Authorization: Bearer $token" \
--header "Content-Type: application/json" \
--data '{
"id": "'"$chargepointId"'",
"locationId": "'"$location_id"'"
}' | tee /dev/tty && echo
}
# Function to update SP1 password
add_cp001_password() {
local response
local success=false
local attempt=1
local passwordString=$1
until $success; do
echo "Attempt $attempt: Updating SP1 password..."
response=$(curl -s -o /dev/null -w "%{http_code}" --location --request PUT "http://localhost:8080/data/monitoring/variableAttribute?stationId=${CHARGEPOINT_ID}&setOnCharger=true" \
--header "Content-Type: application/json" \
--data-raw '{
"component": {
"name": "SecurityCtrlr"
},
"variable": {
"name": "BasicAuthPassword"
},
"variableAttribute": [
{
"value": "'"$passwordString"'"
}
],
"variableCharacteristics": {
"dataType": "passwordString",
"supportsMonitoring": false
}
}' | tee /dev/tty)
if [[ $response -ge 200 && $response -lt 300 ]]; then
echo "Password update successful."
success=true
else
echo "Password update failed with HTTP status: $response. Retrying in 2 second..."
sleep 2
((attempt++))
fi
done
}
# Main script execution
TOKEN=$(get_directus_token)
echo "Received Token: $TOKEN"
if [ -z "$TOKEN" ]; then
echo "Failed to retrieve access token."
exit 1
fi
# Upload image and set as project logo
echo "Uploading project images..."
FILE_ID_LOGO=$(upload_image "$TOKEN" "$PROJECT_LOGO_IMAGE_URL" "Citrine Logo")
if [ -z "$FILE_ID_LOGO" ]; then
echo "Failed to upload project image."
exit 1
fi
FILE_ID_BACKGROUND=$(upload_image "$TOKEN" "$PUBLIC_BACKGROUND_IMAGE_URL" "Citrine Background")
if [ -z "$FILE_ID_BACKGROUND" ]; then
echo "Failed to upload project image."
exit 1
fi
echo "Setting project image..."
set_project_image "$TOKEN" "$FILE_ID_LOGO" "$FILE_ID_BACKGROUND"
echo "Adding a new location..."
LOCATION_ID=$(add_location "$TOKEN")
if [ -z "$LOCATION_ID" ]; then
echo "Failed to add new location."
exit 1
fi
echo "Location ID: $LOCATION_ID"
echo "Adding new station..."
add_charging_station "$TOKEN" "$LOCATION_ID" "$CHARGEPOINT_ID"
echo "Add cp001 password to citrine..."
add_cp001_password "$CP_PASSWORD"