I use the Ubiquiti Unifi network platform at home, and here is how I integrate it into Ignition. I primarily use it for presence detection and to analyze my network The script as is, in my usage, generates about 6000 tags, so I presume you will want to implement some changes for it to work better with Maker.
endpoint = "https://yourcontrolleriporhostname:8443"
username = 'yourusername'
password = 'yourpassword'
class unifi():
client = system.net.httpClient(bypass_cert_validation = True,cookie_policy = "ACCEPT_ALL")
logged_in = False
endpoint = None
username = None
password = None
LOG = system.util.getLogger("unifi")
def __init__(self,endpoint,username=None,password=None):
self.endpoint = endpoint
if username:
self.username = username
if password:
self.password = password
def login(self,username=None,password=None):
self.LOG.info("logging in")
if username:
self.username = username
if password:
self.password = password
data = {"username":self.username,"password":self.password,"remember":True}
response = self.client.post(endpoint+"/api/login",data = data)
self.logged_in = True
def getHealth(self):
if not self.logged_in:
self.login()
self.LOG.info(self.logged_in)
if self.logged_in:
response = self.client.get(self.endpoint+"/api/s/default/stat/health")
if response.json['meta']['rc'] == u'ok':
return response.json['data']
elif response.json['meta']['rc'] == u'error' and response.json['meta']['msg'] == u'api.err.LoginRequired':
self.logged_in = False
def getSta(self):
if not self.logged_in:
self.login()
if self.logged_in:
response = self.client.get(self.endpoint+"/api/s/default/stat/sta")
if response.json['meta']['rc'] == u'ok':
return response.json['data']
elif response.json['meta']['rc'] == u'error' and response.json['meta']['msg'] == u'api.err.LoginRequired':
self.logged_in = False
def get(self,uri):
if not self.logged_in:
self.login()
if self.logged_in:
response = self.client.get(self.endpoint+"/api/s/default/"+uri)
if response.json['meta']['rc'] == u'ok':
return response.json['data']
elif response.json['meta']['rc'] == u'error' and response.json['meta']['msg'] == u'api.err.LoginRequired':
self.logged_in = False
client = unifi(endpoint,username,password)
def processResult(_r = None,tagProvider = "unifi",prefix = ""):
writePaths = []
writeValues = []
tags = []
typeMap = {str:"String",bool:"Boolean",unicode:"String",float:"Float4"}
for folder in _r:
fname = folder["name"] if "name" in folder else folder["mac"] if "mac" in folder else folder["subsystem"] if "subsystem" in folder else folder["bssid"] if "bssid" in folder else None
for item in folder:
if isinstance(folder[item],tuple(typeMap.keys())):
tagPath = '[{}]{}{}/{}'.format(tagProvider,prefix,fname,item)
if system.tag.exists(tagPath):
writePaths.append(tagPath)
writeValues.append(folder[item])
else:
tag = {
"name":item,
"dataType":typeMap[type(folder[item])],
"value":folder[item]
}
system.tag.configure('[{}]{}{}'.format(tagProvider,prefix,fname),[tag])
if len(writePaths):
system.tag.writeBlocking(writePaths,writeValues)
To call and generate your tags, I use this on a tag that triggers periodically.
d1 = system.date.now()
_r = unifi.client.get("stat/health")
unifi.processResult(_r,"api","unifi/health/")
_r = unifi.client.get("stat/sysinfo")
unifi.processResult(_r,"api","unifi/sysinfo/")
_r = unifi.client.get("stat/sta")
unifi.processResult(_r,"api","unifi/sta/")
_r = unifi.client.get("stat/device")
unifi.processResult(_r,"api","unifi/device/")
d2 = system.date.now()
system.tag.writeBlocking(["[.]last duration"],[d2.time-d1.time])