<jupyter_start><jupyter_text>imports<jupyter_code>import requests
import json
import sys
import io
import logging<jupyter_output><empty_output><jupyter_text>settings<jupyter_code>st_url = "http://smartaqnet-dev.teco.edu:8080/FROST-Server/v1.0"<jupyter_output><empty_output><jupyter_text>logging<jupyter_code>log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s - %(levelname)s(%(name)s): %(message)s")

fh = logging.FileHandler('./log')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
log.addHandler(fh)

sh = logging.StreamHandler(sys.stderr)
sh.setLevel(logging.ERROR)
sh.setFormatter(formatter)
log.addHandler(sh)<jupyter_output><empty_output><jupyter_text>geo conversion function<jupyter_code>def geo_conversion(inputstring):
    direction = {
        "N": 1,
        "S": -1,
        "E": 1,
        "W": -1
    }
    
    new = inputstring.replace(" ", "")
    new = new.replace(u"°", " ")
    new = new.replace("'", " ")
    new = new.replace("\"", " ")
    new = new.split()
    new_dir = new.pop()
    
    return (float(new[0])+float(new[1])/60.0+float(new[2])/3600.0) * direction[new_dir]

# test
geo_conversion("""48°21'28.6"N""")<jupyter_output><empty_output><jupyter_text>post entity function<jupyter_code>def PostEntity(st_url, where, entity):
        """TODO: add docstring"""
        try:
            entityid = entity["@<EMAIL>"]
        except KeyError:
            entityid = "new ID"
            
        p = requests.post(st_url + where, json.dumps(entity))
        if (p.status_code  == 201):
            log.debug("entity created: " + st_url + where + "('" + entityid + ")'")
            return True
        else:
            body = b"";
            for chunk in p.iter_content(chunk_size=128):
                body += chunk
                
            body = body.decode("utf8")
            body = body.replace("\n", " ").replace("\r", "")
            
            if (p.status_code  == 500 and entityid != "new ID"):
                log.warning("entity exists already: " + st_url + where + "('" + entityid + "')" + " - body: " + body)
                return False
            
            log.error("cannot create entity: " + st_url + where + "('" + entityid + "')" + \
                      " - exit code: " + str(p.status_code) + " - body: " + body)
            raise TypeError("invalid entity")<jupyter_output><empty_output><jupyter_text>define data<jupyter_code>exceldata = [
    {
        "name": "<NAME>",
        "acronym": "FH",
        "lat": """48°21'28.6"N""",
        "lon": """10°54'25.1"E""",
        "type": "Urban Background",
        "url": "helmholtz-muenchen.de",
        "device": "container",
        "srn": "FH"
    },
    
    {
        "name": "<NAME>",
        "acronym": "KP",
        "lat": """48°21'52.5"N""",
        "lon": """10°53'42.1"E""",
        "type": "Traffic Site (City Zone)",
        "url": "lfu.bayern.de",
        "device": "container",
        "srn": "701"
    },
    
    
    {
        "name": "<NAME>",
        "acronym": "BP",
        "lat": """48°22'36.4"N""",
        "lon": """10°53'18.2"E""",
        "type": "Urban Background",
        "url": "lfu.bayern.de",
        "device": "container",
        "srn": "706"
    },
    
    {
        "name": "<NAME>",
        "acronym": "KS",
        "lat": """48°22'13.0"N""",
        "lon": """10°53'46.7"E""",
        "type": "Traffic Site (Hot Spot)",
        "url": "lfu.bayern.de",
        "device": "container",
        "srn": "1401"
    },
    
    {
        "name": "<NAME>",
        "acronym": "LfU",
        "lat": """48°19'33.7"N""",
        "lon": """10°54'11.1"E""",
        "type": "Regional Background",
        "url": "lfu.bayern.de",
        "device": "container",
        "srn": "1501"
    },
    
    {
        "name": "Schertlinstraße",
        "acronym": "SST",
        "lat": """48°21'07.2"N""",
        "lon": """10°54'09.9"E""",
        "type": "Traffic Site (Crossing)",
        "url": "helmholtz-muenchen.de",
        "device": "container",
        "srn": "SST"
    },
    
    {
        "name": "Klostergarten",
        "acronym": "KLG",
        "lat": """48°22'27.3"N""",
        "lon": """10°53'59.3"E""",
        "type": "Urban Background",
        "url": "helmholtz-muenchen.de",
        "device": "container",
        "srn": "KLG"
    },
    
    {
        "name": "Lindenau",
        "acronym": "LDH",
        "lat": """48°19'20.6"N""",
        "lon": """10°57'49.6"E""",
        "type": "Rural Background",
        "url": "helmholtz-muenchen.de",
        "device": "container",
        "srn": "LDH"
    }
]<jupyter_output><empty_output><jupyter_text>create things<jupyter_code>things = list()

for elem in exceldata:
    thing = dict()
    thing["name"] = "Messcontainer"
    thing["description"] = elem["name"] + " - " + elem["acronym"]
    thing["properties"] = {
        "SiteType": elem["type"]
    }
    thing["@iot.id"] = "saqn:t:" + elem["url"] + ":" + elem["device"] + ":2018:" + elem["srn"]
    elem["thingid"] = thing["@iot.id"]
    
    things.append(thing)<jupyter_output><empty_output><jupyter_text>create locations<jupyter_code>locations = list()
for elem in exceldata:
    location = dict()
    location["name"] = "Messcontainer"
    location["description"] = "Koordinaten des Messcontainers"
    location["encodingType"] = "application/vnd.geo+json"
    location["location"] = {
        "type": "Point",
        "coordinates": [
            geo_conversion(elem["lon"]),
            geo_conversion(elem["lat"])
        ]
    }
    
    location["@iot.id"] = "saqn:l:" + elem["url"]
    
    if "Lindenau" in elem["name"]:
        location["@iot.id"] += ":Friedberg:" + elem["name"]
    else:
        location["@iot.id"] += ":Augsburg:" + elem["name"]
        
    elem["locationid"] = location["@iot.id"]
    
    locations.append(location)<jupyter_output><empty_output><jupyter_text>add locations to things<jupyter_code>comb = zip(things, locations)
things = list()
for elem in comb:
    elem[0]["Locations"] = [elem[1]]
    things.append(elem[0])<jupyter_output><empty_output><jupyter_text>send everything<jupyter_code>for elem in things:
    PostEntity(st_url, "/Things", elem)<jupyter_output><empty_output><jupyter_text>write data<jupyter_code>with io.open("exceldata.dat", "w", encoding="utf8") as file:
    json.dump(exceldata, file, ensure_ascii=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20062353858144974, 'fraction_numerical': 0.02681215900233827, 'mean_word_length': 2.5447513812154696, 'pattern_counts': {'":': 75, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '480400', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2429, 'n_tokens_neox': 2299, 'n_words': 368, 'path': 'LeafletMap/src/CreateContainerThings.ipynb', 'repo_name': 'SmartAQnet/FROST-Examples', 'size': '6450'}