Esempio n. 1
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)
Esempio n. 2
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])
Esempio 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)
Esempio n. 4
0
def credentials(zcli, device_id, endpoint_mode, credentials, output):
    """
.. _zdm-cmd-device-credentials:

Devices credentials
-------------------

To create a set of device credentials an existent device uid is needed.

The credentials generation command is: ::

    zdm device credentials device_id --credentials ctype --endpoint_mode etype --output dest-folder

where :samp:`device_id` is the device uid, :samp:`ctype` is the credentials type (chosen from :samp:`device_token` or :samp:`cloud_token`), :samp:`etype` is the endpoint security mode (choosen from :samp:`secure` or :samp:`insecure`), and :samp:`dest-folder` is the path where to save the credential file (named :samp:`zdevice.json`).

All options are not mandatory and if not given a default value is assigned:

    * :samp:`device_token` for :samp:`ctype`
    * :samp:`secure` for :samp:`etype`
    * current directory for :samp:`dest-folder`


    """
    res = zcli.zdm.devices.credentials(device_id, credentials, endpoint_mode)
    if env.human:
        outfile = fs.path(output, "zdevice.json")
        fs.set_json(res, outfile)
        info("Credentials file for", device_id, "saved at", outfile)
    else:
        log_json(res)
Esempio 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)
Esempio n. 6
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])
Esempio n. 7
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])
Esempio n. 8
0
def create(zcli, name, type, workspace_id, start, end, tag, fleet):
    """
.. _zdm-cmd-workspace-data-export-create:

Export creation
----------------

To create a new export use the command: ::

    zdm workspace data export create name type workspace_id

where :samp:`name` is the name of the new export
:samp:`type` is the type ox export (json or csv)
:samp:`workspace_id` is the uid of the workspace to receive data from
:samp:`start` is the starting date for data (RFC3339)
:samp:`end` is the ending date for data (RFC3339)

It's also possible to add filters on data using the following options:

:option:`--tag` To specify tags to filter data (can be specify more than one)
:option:`--fleet` To specify fleets to filter data (can be specify more than one)

    """

    tags = []
    fleets = []

    for t in tag:
        tags.append(t)

    for f in fleet:
        fleets.append(f)

    configurations = {
        "workspace_id": workspace_id,
        "start": start,
        "end": end,
        "tags": tags,
        "fleets": fleets
    }

    notifications = {}

    exp = zcli.zdm.exports.create(name, type, configurations, notifications)
    if env.human:
        info("Export [{}] created successfully.".format(exp.id))
    else:
        log_json(exp.toJson)
Esempio n. 9
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])
Esempio n. 10
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)
Esempio n. 11
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)
Esempio n. 12
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)
Esempio n. 13
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)
Esempio n. 14
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)
Esempio n. 15
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])
Esempio n. 16
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])
Esempio 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])
Esempio n. 18
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])
Esempio n. 19
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)