I wasted a lot of time after Openhab updates and when testing experimental addons with deleting and re-adding Things and setting the label and location again on every single Thing. So I took the time and wrote two Python scripts to backup and restore the label and location for every Thing. With those scripts I now only have to delete and re-add the things in PaperUI. After that I can restore the label and location automatically. Maybe I will later also write a script that also automatically deletes and re-adds the Things.
Here are the two scripts:
BackupThingInfo.py
#!/usr/bin/python3
import requests
import json
import csv
from operator import itemgetter
baseUrl = "http://172.17.2.21:8080/rest/things/"
response = requests.get(baseUrl)
thingInfos = response.json()
thingInfos.sort(key=itemgetter("UID"))
with open("ThingsBackup.csv", "w", newline='', encoding="utf8") as csvFile:
csvWriter = csv.writer(csvFile, delimiter=",",quotechar='"',quoting=csv.QUOTE_MINIMAL)
csvWriter.writerow(["ThingUid","Label","Location"])
for thingInfo in thingInfos:
thingUID = thingInfo.get("UID")
thingLabel = thingInfo.get("label")
thingLocation = thingInfo.get("location")
if thingLocation == None:
thingLocation = ""
csvWriter.writerow([thingUID, thingLabel, thingLocation])
RestoreThingInfo.py:
#!/usr/bin/python3
import requests
import json
import csv
from time import sleep
baseUrl = "http://172.17.2.21:8080/rest/things/"
with open("ThingsBackup.csv","r", encoding="utf8") as csvFile:
next(csvFile) # skip headings
csvReader=csv.reader(csvFile,delimiter=",",quotechar='"')
for thingUid,label,location in csvReader:
url = baseUrl + thingUid
payload = {"label": label, "location": location}
headers = {"content-type": "application/json"}
response = requests.put(url, data=json.dumps(payload), headers=headers)
if response.status_code == 200:
sleep(1)
response = requests.get(url)
thingInfo = response.json()
thingLabel = thingInfo.get("label")
thingLocation = thingInfo.get("location")
if thingLabel == label and thingLocation == location:
print("Thing {} updated, label: {} location: {}"
.format(thingUid, thingLabel, thingLocation))
else:
print("Error Thing {} not updated, label: {} location: {}"
.format(thingUid, label, location))
else:
print("Error {} updating Thing {} label: {} location: {}"
.format(response.status_code, thingUid, label, location))