Exemplo n.º 1
0
def all(zcli):
    """
.. _zdm-cmd-device-get-all:

List devices
------------

To list all the devices, use this command to see a table with a device for each rows
with the device uid, name and the uid of the fleet and workspace containing them.
To see all the devices use the command: ::

    zdm device all

    """

    table = []
    devs = zcli.zdm.devices.list()
    if env.human:
        for d in devs:
            table.append([
                d.id, d.name, d.fleet_id if d.fleet_id else "<none>",
                d.workspace_id, d.workspace_name
            ])
        log_table(
            table,
            headers=["ID", "Name", "FleeId", "WorkspaceID", "WorkspaceName"])
    else:
        log_json([dev.toJson for dev in devs])
Exemplo n.º 2
0
def get(zcli, id):
    """
.. _zdm-cmd-device-get-device:

Get device
----------

To get a single device information, use this command to see the device name and the uid
of the fleet and the workspace that contain the device. ::

    zdm device get uid

where :samp:`uid` is the device uid.

    """

    device = zcli.zdm.devices.get(id)
    if env.human:
        log_table(
            [[
                device.id, device.name, device.fleet_id, device.workspace_id,
                device.workspace_name
            ]],
            headers=["ID", "Name", "FleetID", "WorkspaceID", "WorkspaceName"])
    else:
        dev = device.toJson
        dev.update({"workspace_id": device.workspace_id})
        log_json(dev)
Exemplo n.º 3
0
def create(zcli, fleet_id, name):
    """
.. _zdm-cmd-device-create:

Device creation
---------------

To connect a device to the ZDM, create a new device on ZDM to obtain a new device uid.
The creation command is: ::

    zdm device create name

where :samp:`name` is the name that of the device

Creating a device with this command, will associate it to the default fleet inside the default workspace.
To associate the device to another fleet with the optional argument:

:option:`--fleet-id uid`

To associate the device to another fleet, see the :ref:`update command <zdm-cmd-device-update>`
    """
    dev = zcli.zdm.devices.create(name, fleet_id)
    if env.human:
        log_table([[dev.id, dev.name, dev.fleet_id]],
                  headers=["ID", "Name", "fleet_id"])
    else:
        log_json(dev.toJson)
Exemplo n.º 4
0
def check(zcli, device_id):
    """
    .. _zdm-cmd-fota-check:

    Check FOTA status
    -----------------

    To check the status of a FOTA update, to know if the device finished the task or if an error occurred, type the
    following command: ::

        zdm fota check device_uid

    where :samp:`device_uid` is the uid of the device to check.

        """
    fota_exp = zcli.zdm.fota.status_expected(device_id)
    fota_cur = zcli.zdm.fota.status_current(device_id)

    schedule_at = fota_exp.version if fota_exp else "<none>"

    if fota_exp is None and fota_cur is not None:
        # the job has been scheduled (exp is None)  and the device has sent the response (fota_cur not None)
        status = "done"
        result = fota_cur.value if fota_cur is not None else "<no result>"
        result_at = fota_cur.version if fota_cur is not None else "<no result>"
    elif fota_exp is None and fota_cur is None:
        # the job has not been scheduled nor a response has been received
        status = "<none>"
        result = "<none>"
        result_at = "<none>"
    elif fota_exp is not None and fota_cur is not None:
        if fota_cur.version > fota_exp.version:
            # fota has been scheduled and the device has sent a response
            status = "done"
            result = fota_cur.value if fota_cur is not None else "<no result>"
            result_at = fota_cur.version if fota_cur is not None else "<no result>"
        else:
            status = "pending"
            result = "<none>"
            result_at = "<none>"
    elif fota_exp is not None and fota_cur is None:
        # the job has been scheduled bu the device has not sent a response
        status = "pending"
        result = "<none>"
        result_at = "<none>"
    else:
        status = "<unknown>"
        result = "<none>"
        result_at = "<none>"

    log_table([[
        status,
        schedule_at,
        result,
        result_at,
    ]],
              headers=["Status", "ScheduleAt", "Result", "ResultAt"])
Exemplo n.º 5
0
def list_fleets(zcli, workspace_id):
    """
    List fleets of a workspace
    """
    resp = zcli.zdm.fleets.list(workspace_id)

    if env.human:
        log_table([[fleet.id, fleet.name, fleet.workspace_id]],
                  headers=["ID", "Name", "WorkspaceID"])
    else:
        log_json(fleet.toJson)
Exemplo n.º 6
0
def check(zcli, name, device_id):
    """

.. _zdm-cmd-job-check:

Check a job status
------------------

To check the status of a scheduled job, type the command: ::

    zdm job check job uid

where :samp:`job` is the job name and :samp:`uid` is the device uid to check

    """
    status_exp = zcli.zdm.jobs.status_expected(name, device_id)
    status_cur = zcli.zdm.jobs.status_current(name, device_id)
    schedule_at = status_exp.version if status_exp else "<unknown>"

    if status_exp is None and status_cur is not None:
        # the job has been scheduled (exp is None)  and the device has sent the response (status_cur not None)
        status = "done"
        result = status_cur.value if status_cur is not None else "<no result>"
        result_at = status_cur.version if status_cur is not None else "<no result>"

    elif status_exp is None and status_cur is None:
        # the job has not been scheduled nor a device has not sent a response
        status = "<none>"
        result = "<none>"
        result_at = "<none>"
    elif status_exp is not None and status_cur is not None:
        if status_cur.version > status_exp.version:
            # job has been scheduled and the device has sent a response
            status = "done"
            result = status_cur.value if status_cur is not None else "<no result>"
            result_at = status_cur.version if status_cur is not None else "<no result>"
        else:
            status = "pending"
            result = "<none>"
            result_at = "<none>"
    elif status_exp is not None and status_cur is None:
        # the job has been scheduled bu the device has not sent a response
        status = "pending"
        result = "<none>"
        result_at = "<none>"
    else:
        status = "<unknown>"
        result = "<none>"
        result_at = "<none>"

    log_table([[name, status, schedule_at, result, result_at, ]],
              headers=["Name", "Status", "ScheduleAt", "Result", "ResultAt"])
Exemplo n.º 7
0
def all(zcli, workspace_id, status):
    table = []

    alerts = zcli.zdm.alerts.list(workspace_id, status)
    if env.human:
        for a in alerts:
            table.append(
                [a.name, a.threshold, a.status, a.last_time_scheduled])
        log_table(table,
                  headers=["Name", "Threshold", "Status", "LastSchedule"])

    else:
        log_json([a.toJson for a in alerts])
Exemplo n.º 8
0
def all(zcli, workspace_id, status):
    dstream = zcli.zdm.streams.list(workspace_id, "condition", status)
    if env.human:
        table = []
        for stream in dstream:
            table.append([
                stream.name, stream.status, stream.period, stream.subtype,
                stream.last_time_scheduled
            ])
        log_table(
            table,
            headers=["Name", "Status", "Period (s)", "To", "Last Schedule"])
    else:
        log_json([s.toJson for s in dstream])
Exemplo n.º 9
0
def get(zcli, export_id):
    """
.. _zdm-cmd-workspace-data-export-get:

Get export
----------

To get an existing export information use the command: ::

    zdm workspace data export get export_id

where :samp:`export_id` is the uid of the export

    """
    exp = zcli.zdm.exports.get(export_id)

    log_table([[exp.id, exp.Name, exp.Type, exp.Status, exp.Url]],
              headers=["ID", "Name", "Type", "Status", "URL"])
Exemplo n.º 10
0
def all(zcli, workspace_id, tag, device_id, start, end):
    """
.. _zdm-cmd-workspace-data-get:

Get data
--------

To get all the data of a workspace associated to a tag use the command: ::

    zdm workspace data all uid tag

where :samp:`uid` is the uid of the workspace, and  :samp:`tag` is the tag of the data to download.

To filter result use the options:

* :option:`--device-id`
* :option:`--start`
* :option:`--end`

    """

    tags = zcli.zdm.data.get(workspace_id,
                             tag,
                             device_id=device_id,
                             start=start,
                             end=end)
    if env.human:
        if len(tags) > 0:
            table = []
            for tag in tags:
                table.append([
                    tag.Tag, tag.Payload, tag.DeviceId, tag.DeviceName,
                    tag.TimestampDevice, tag.TimestampCloud
                ])
            log_table(table,
                      headers=[
                          "Tag", "Payload", "DeviceId", "DeviceName",
                          "TimestampDevice", "TimestampCloud"
                      ])
        else:
            info("No data present for to tag [{}].".format(tag))
    else:
        log_json([tag.toJson for tag in tags])
Exemplo n.º 11
0
def get(zcli, id):
    """
.. _zdm-cmd-fleet-get-fleet:

Get fleet
---------

To get a single fleet information, use this command to see its name, the uid of the workspace that contains it and the list of devices inside::

    zdm fleet get uid

where :samp:`uid` is the fleet uid

    """
    fleet = zcli.zdm.fleets.get(id)
    if env.human:
        log_table([[fleet.id, fleet.name, fleet.workspace_id]],
                  headers=["ID", "Name", "WorkspaceID"])
    else:
        log_json(fleet.toJson)
Exemplo n.º 12
0
def create(zcli, name, workspaceid):
    """
.. _zdm-cmd-fleet-create:

Fleet creation
--------------

To create a new fleet of devices inside a workspace use the command: ::

    zdm fleet create name workspace_uid

where :samp:`name` is the fleet name and :samp:`workspace_id` is the uid of the workspace that will contain the fleet.

    """
    fleet = zcli.zdm.fleets.create(name, workspaceid)
    if env.human:
        log_table([[fleet.id, fleet.name, fleet.workspace_id]],
                  headers=["ID", "Name", "WorkspaceID"])
    else:
        log_json(fleet.toJson)
Exemplo n.º 13
0
def get(zcli, id):
    """
.. _zdm-cmd-workspace-get-workspace:

Get workspace
-------------

To get a single workspace information, use this command: ::

    zdm workspace get uid

where :samp:`uid` is the workspace uid.

    """
    ws = zcli.zdm.workspaces.get(id)
    if env.human:
        data = [ws.id, ws.name, ws.description]
        log_table([data], headers=["ID", "Name", "Description"])
    else:
        log_json(ws.toJson)
Exemplo n.º 14
0
def all(zcli, workspace_id, tag, device_id, threshold, status):
    """
.. _zdm-cmd-workspace-conditions-all:

List conditions
--------------

To get all the conditions of a device use the command: ::

    zdm workspace condition all workspace_id tag

where :samp:`workspace_id` is the uid of the workspace and `tag` is the tag of the conditions
:samp:`device_id` is the uid of the device
:samp:`threshold` is the min duration of the conditions in seconds

It's also possible to filter results using the options:

* :option:`--status` to filter on conditions status [open, closed]
* :option:`--device_id` to filter on devices
* :option:`--threshold` to indicate the minimum duration of the conditions to return

    """

    conditions = zcli.zdm.conditions.list(workspace_id, tag, device_id,
                                          threshold, status)
    if env.human:
        table = []
        for condition in conditions:
            table.append([
                condition.Uuid, condition.Tag, condition.DeviceId,
                condition.Start, condition.Finish, condition.Duration
            ])
        log_table(
            table,
            headers=["ID", "Tag", "Device", "Start", "Finish", "Duration"])
    else:
        cc = []
        for c in conditions:
            cc.append(c.toJson)
        log_json(c)
Exemplo n.º 15
0
def create(zcli, name, description):
    """
.. _zdm-cmd-workspace-create:

Create workspace
------------------

To create a new workspace on the ZDM use the command: ::

    zdm workspace create name

where :samp:`name` is the name of the new workspace

It's possible to insert a description of the workspace adding the option :option:`--description desc`

    """
    wks = zcli.zdm.workspaces.create(name, description)
    if env.human:
        log_table([[wks.id, wks.name, wks.description]],
                  headers=["ID", "Name", "Description"])
    else:
        log_json(wks.toJson)
Exemplo n.º 16
0
def firmwares(zcli, workspace_id):
    """
.. _zdm-cmd-workspace-firmware:

List firmwares
--------------

To have a list of the firmwares uploaded to the ZDM associated to a workspace use the command: ::

    zdm workspace firmwares uid

where :samp:`uid` is the uid of the workspace.

    """
    table = []
    firmwares = zcli.zdm.firmwares.list(workspace_id)
    if env.human:
        for d in firmwares:
            table.append([d.id, d.version, d.metadata, d.workspace_id])
        log_table(table, headers=["ID", "Version", "Metadata", "WorkspaceID"])
    else:
        log_json([frm.toJson for frm in firmwares])
Exemplo n.º 17
0
def all(zcli):
    """
.. _zdm-cmd-workspace-get-all:

List workspaces
---------------

To see the list of all workspaces, use the command: ::

    zdm workspace all

 The output is a table containing workspaces with ID, name, description

    """
    wks = zcli.zdm.workspaces.list()
    if env.human:
        table = []
        for ws in wks:
            table.append([ws.id, ws.name, ws.description])
        log_table(table, headers=["ID", "Name", "Description"])
    else:
        log_json([wk.toJson for wk in wks])
Exemplo n.º 18
0
def fleets(zcli, workspace_id):
    """
.. _zdm-cmd-workspace-get-all-fleets:

List fleets
------------

Use this command to have the list of fleets inside a workspace: ::

    zdm workspace fleet all uid

where :samp:`uid` is the uid of the workspace.

    """
    fleets = zcli.zdm.fleets.list(workspace_id)
    if env.human:
        table = []
        for fl in fleets:
            table.append([fl.id, fl.name, fl.description, fl.workspace_id])
        log_table(table, headers=["ID", "Name", "Description", "WorkspaceId"])
    else:
        log_json([fl.toJson for fl in fleets])
Exemplo n.º 19
0
def tags(zcli, workspace_id):
    """
.. _zdm-cmd-workspace-data-tags:

List tags
---------

When a device publish data to the ZDM it label them with a string called tag. With the following command it's possible to see all the tags
that devices associated to the workspace used as data label. ::

    zdm workspace data tags uid

where :samp:`uid` is the uid of the workspace

    """
    tags = zcli.zdm.data.list(workspace_id)
    if env.human:
        if len(tags) > 0:
            log_table([[tags]], headers=["Tags"])
        else:
            info("Empty tags for workspace {}.".format(workspace_id))
    else:
        log_json([tag.toJson for tag in tags])
Exemplo n.º 20
0
def do_prepare(zcli, project, device_id, version):
    status = zcli.zdm.status.get_device_vm_info(device_id)
    if status is None:
        fatal(
            "Fota cannot be prepared. Please connect device '{}' to the ZDM at least one time."
            .format(device_id))

    vm_info = status.value

    if "vm_target" not in vm_info:
        fatal(
            "The target of the virutal machine is missing. Please reconnect the device '{}' to the ZDM."
            .format(device_id))
    vm_target = vm_info['vm_target']
    if "vm_uid" not in vm_info:
        fatal(
            "The ID of the virtual machine is missing. Please reconnect the device '{}' to the ZDM."
            .format(device_id))
    vm_uid = vm_info['vm_uid']
    if "vm_version" in vm_info:
        vm_version = vm_info['vm_version']

    vm_hash_feature = ""
    target_device = ""

    vms = _ztc_vm_list()
    if vms['total'] > 0:
        for vm in vms['list']:
            if vm['uid'] == vm_uid:
                if "ota" not in vm['features']:
                    fatal(
                        "The vm '{}' of device '{}' doesn't support FOTA. Please use a VM with feature OTA enabled."
                        .format(vm_uid, device_id))
                else:
                    vm_hash_feature = vm['hash_features']
                    target_device = vm['dev_type']
    else:
        fatal("No virtual machine found. Please create a virtual machine")

    device = zcli.zdm.devices.get(device_id)
    workspace_id = device.workspace_id
    info("workspace id '{}'".format(workspace_id))
    vbo_file = fs.path(env.tmp, 'test_temp_fw.vbo')

    try:
        _ztc_compile(project, vm_target, vbo_file)
        fw_json1 = _ztc_link(vbo_file, vm_uid, '0')
        fw_json2 = _ztc_link(vbo_file, vm_uid, '1')
        fw_bin1 = fw_json1['bcbin']
        fw_bin2 = fw_json2['bcbin']
    except Exception as e:
        fatal(e)

    metadata = {
        "vm_version": vm_version,
        "vm_feature": vm_hash_feature,
        "dev_type": target_device
    }
    res = zcli.zdm.firmwares.upload(workspace_id, version, [fw_bin1, fw_bin2],
                                    metadata)
    if env.human:
        log_table([[res.id, res.version, res.metadata]],
                  headers=["ID", "Version", "Metadata"])
    else:
        raw = res.toJson
        del raw['firmware']
        log_json(raw)