Create a Function to Automate the Process

  1. To create a function, click Functions > Workers.
  2. Click New Worker.
  3. Add the Function name as update-cpu-state and description as This function is to update the computer status based on the disk space.
  4. Select the Python 3 template.
  5. Enter the following code. In the provided code, add your thing’s Client ID and Secret ID from the Interface tab inside the Thing. Also, complete the PATH with your own values.
    import time
    import requests
    import json
    API_HOST = ''
    CLIENT_ID = "Add_your_clientID_from_Interface_tab_inside_the_Thing"
    CLIENT_SECRET = "Add_your_client_Secret_from_Interface_tab_inside_the_Thing"
    def get_access_token():
        payload = f'grant_type=client_credentials&' \
                  f'client_id={CLIENT_ID}&' \
                  f'client_secret={CLIENT_SECRET}&' \
                  f' thing.update'
        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        response = requests.request("POST", API_HOST + "/oauth2/token",
                                    headers=headers, data=payload)
        return response.json()['access_token']
    def revoke_token(token):
        payload = f'token={token}&' \
                  f'client_id={CLIENT_ID}&' \
        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        response = requests.request("POST", API_HOST + "/oauth2/revoke",
                                    headers=headers, data=payload)
        return response
    def handle(req):
        body = req.body.decode("utf-8")
        body = json.loads(body)
        cpu = body['cpu']
        if cpu >= 85:
            state = "Critical usage"
        elif 50 < cpu < 85:
            state = "Normal usage"
            state = "Low usage"
        PATH = "/spaces/Enter_your_space_id/collections/Enter_your_collection_name/things/Enter_your_thing_uid/properties/statecpu"
        headers = {"Authorization": "Bearer " + get_access_token()}
        response = requests.request("PUT", API_HOST + PATH, headers=headers,json={"statecpu": state})
        return {
        "body": response.json(),
        "status_code": response.status_code

    Figure 1.
    Note: You can edit the code as necessary.

    Figure 2.
  6. Add an Event trigger as follows:

    When using this method, the function will be invoked whenever there is a change on the Property cpu on the Thing you have created.

  7. Deploy your function by clicking Save.
    The function is created. This could take a few minutes. The status of your Function will be updated to a Building status and, eventually, it will become Running. At this point, your Function is ready to be called.
  8. Test the function created in the Test tab by sending the input parameters to the function and receiving the solution in the test response field.
    1. In Test Request Input field enter, enter {"cpu": 57.9}.
    2. Click Run.
      The Test Response field shows Normal usage.

      Figure 3.