Importing python library - the wrong way?

I thought it would be an easy task to get my weather station data into ignition. I learned that my weather station has an API available, so that is the path I took. It suggests using their premade helper functions (https://ambientweather.docs.apiary.io/#introduction/authentication) so I thought the best choice would be to use python (https://github.com/avryhof/ambient_api).

I noted that the newest release of the helper functions don’t work with Python 2.7, so I downloaded the source files for a previous version that did support 2.7. I copied them into the /usr/local/bin/ignition/user-lib/pylib folder, and then tested it out in ignition’s script console. I did it this way because I figured issuing the install command “pip install ambient_api” would write to the computer’s local installation, not ignition’s installation. Each time I tried to import the library, I got a dependency error, so I found the library files for that dependency and copied them into the pylib folder. I did this about 6 times and decided this can’t be the right way to go about this.

I’m new to the concept of APIs and python libraries and I’m not sure I’m doing any of this right. Any help in this area would be appreciated.

  1. Am I on the right path for getting weather station data into ignition?
  2. Is there a better way to import python libraries?
  1. No idea
  2. Use jython’s pip. See the instructions at these links:

http://forum.inductiveautomation.com/search?q=jython%20ensurepip

Note that if any dependency uses C extensions to python, you won’t be able to make it work.

1 Like

You can also skip the SDK approach entirely, and just build your own using system.net.httpClient(). You can even wrap that up in your own mini-SDK (it doesn't look like the Ambient Weather REST API is very complex) to make it a bit easier to use. I went into the (very broad strokes) here:

2 Likes

I like the idea! I'm not knowledgeable enough to figure this out yet!

The last commit in the Python registry you linked is titled ‘drop Python 2 support’, so you might not have much luck.
But, really, it looks like the API is pretty simple to wrap. Try this out:

class AmbientWeatherClient(object):
	def __init__(self, applicationKey, apiKey):
		self.baseUrl = "https://api.ambientweather.net/v1/"
		self.authorization = {
			"applicationKey": applicationKey,
			"apiKey": apiKey
		}	
		self.client = system.net.httpClient()
		
	def call(self, endpoint, **kwargs):
		kwargs.update(self.authorization)
		response = self.client.get(self.baseUrl + endpoint, params=kwargs)
		return response.json

	def get_devices(self):
		# https://api.ambientweather.net/v1/devices?applicationKey=&apiKey=
		return self.call("devices")

	def get_data(self, macAddress, endDate=None, limit=None):
		# https://api.ambientweather.net/v1/devices/macAddress?apiKey=&applicationKey=&endDate=&limit=288
		kwargs = {"macAddress": macAddress}
		if endDate is not None:
			kwargs["endDate"] = endDate
		if limit is not None:
			kwargs["limit"] = limit
		return self.call("devices", **kwargs)
		
client = AmbientWeatherClient("yourApplicationKey", "yourApiKey")

print client.get_devices()

print client.get_data("yourDeviceMacAddress")
1 Like

:laughing:
I appreciate your help! I’ve learned a lot from this exercise. Now I need to learn how to get the JSON string into ignition tags.

In theory, you can directly write it to a Document type tag, and then there’s a jsonGet expression function you could use in derived tags to extract relevant pieces of info.

1 Like