Ejemplo n.º 1
0
    def setUpClass(self):
        self.zapi = zdmapi(base_url=env.zdm)
        self.d = self.zapi.devices.create("Test")
        key = self.zapi.keys.create(self.d.id, "testkey")
        jwt = key.as_jwt(exp_delta_in_days=90)
        log.info("Created device {}, password {}".format(self.d.id, jwt))

        self.device = zdm.ZDMClient()
        self.device.connect()
Ejemplo n.º 2
0
    def test_expected_status(self):
        """"
        Test the expected status.
        Simulate the case in whicj the cloud schedule a job but the device is not online.
        when the device connect the fist time. it requests the status and execute the jobs
        """
        job = "myJob"

        self.zapi.jobs.schedule(job, {"value": 45}, [self.d.id], on_time="")

        status = self.zapi.jobs.status_expected(job, self.d.id)
        self.assertEqual("@" + job, status.key)

        def test_job(zdmclient, args):
            global received
            received = True
            v = args["value"]
            print("Executing job. Received args: {}".format(args))
            return {"value": v + 1}

        my_jobs = {
            job: test_job,
        }
        self.device = zdm.ZDMClient(device_id=self.d.id,
                                    jobs=my_jobs,
                                    endpoint=ENDPOINT)
        self.device.set_password(self.jwt)
        self.device.connect()

        time.sleep(6)
        self.assertEqual(True, received)
        status = self.zapi.jobs.status_current(job, self.d.id)
        self.assertEqual("@" + job, status.key)

        status = self.zapi.jobs.status_expected(job, self.d.id)
        self.assertEqual(None, status)
Ejemplo n.º 3
0
basic.py

Show the basic example of a ZdmClient that sends a stream of messages to the ZDM.
Each message is published into a random tag with a random value.

"""
import random
import time

import zdm


def pub_temp_hum():
    # this function publish into the tag weather two values: the temperature and the humidity
    tag = 'weather'
    temp = random.randint(19, 38)
    hum = random.randint(50, 70)
    payload = {'temp': temp, 'hum': hum}
    device.publish(payload, tag)
    print('Published: ', payload)


# connection to the ZDM
device = zdm.ZDMClient()
device.connect()

# infinite loop
while True:
    pub_temp_hum()
    time.sleep(5)
Ejemplo n.º 4
0
import time
import zdm

logger = zdm.ZdmLogger().get_logger()

condition_tag = "battery"


# this function asks to ZDM for open conditions, then close it
def on_open_conditions(zdmclient, conditions):
    for c in conditions:
        print("Closing: ", c)
        c.close(payload={"callback": "condition closed by callback"})


device = zdm.ZDMClient(condition_tags=[condition_tag],
                       on_open_conditions=on_open_conditions)
device.connect()

# Create four conditions on the same tag.
# Note. The condition_tag must be passed in the conditions_tags parameter of the constructor
infoLevel = device.new_condition(condition_tag)
warningLevel = device.new_condition(condition_tag)
criticalLevel = device.new_condition(condition_tag)
fatalLevel = device.new_condition(condition_tag)

# initially the battery level is 100%
battery_lvl_curr = 100
battery_lvl_prv = 100

# indicate if the battery is in the recharge state
recharge = False
Ejemplo n.º 5
0

# This job adds two numbers (num1, num2) and return the result.
def job_adder(zdmclient, arg):
    print("Executing Job adder ...")
    if "num1" in arg and "num2" in arg:
        res = arg['num1'] + arg["num2"]
        return {"res": res}
    else:
        return {
            "err":
            "Bad arguments. Arguments 'num1' and 'num2' must be provided."
        }


# define the list of jobs exposed by the device.
# A job is a function that receives two parameters (the device instance itself, and the arguments in a dictionary)
# and returns the result as a dictionary.
my_jobs = {
    'jobRandom': job_random,
    'jobAdder': job_adder,
}

# create a ZDM Device instance and pass to it the the jobs dictionary
device = zdm.ZDMClient(jobs_dict=my_jobs)
device.connect()

while True:
    print("waiting for jobs...")
    time.sleep(3)
Ejemplo n.º 6
0
################################################################################
# Zerynth Device Manager
#
# Created by Zerynth Team 2020 CC
# Authors: E.Neri, D.Neri
###############################################################################
"""
timestamp.py

Show a simple example of how to received the timestamp from the ZDM.

"""
import time

import zdm


def time_callback(zdmclient, arg):
    print("Timestamp received: {}".format(arg))


device = zdm.ZDMClient(on_timestamp=time_callback)
device.connect()

while True:
    # Request the timestamp
    device.request_timestamp()
    print("Time requested ...")
    time.sleep(2)