Exemplo n.º 1
0
 def test_report(self):
     now = int(time.time() * 1000)  # current time in msec
     report = {
         "from": t0,
         "to": t1,
         "aggregationMethods": ["average", "min", "max"],
         # "dimensions": ["dim1", "dim2"],
         # "offset": <results_index>,
         # "limit": <number>,
         # "countOnly": <true/false>,
         # "outputType": "json",
         # "deviceIds":["<device1>", "<device2>"],
         # "gatewayIds":["<gateway1>", "<gateway2>"],
         # "componentIds":["<component1>", "<component2>"],
         # "sort": [
         # {"sortField1": "<sortOrder>"},
         # {"sortField2": "<sortOrder>"}
         # ],
         "filters": {
             "componentType": ["temperature.v1.0", "pressure.v1.0"]
         }
     }
     print t0, t1, data
     iotkitclient.prettyprint(report)
     js = acct.data_report(report)
     # print js
     iotkitclient.prettyprint(js)
     self.assertTrue(js)
Exemplo n.º 2
0
 def test_list_alerts(self):
     iot = self.login()
     acct = iotkitclient.Account(iot)
     acct.get_account(newaccount)
     js = acct.list_alerts()
     iotkitclient.prettyprint(js)
     self.assertEqual(0, len(js))
Exemplo n.º 3
0
#      without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import iotkitclient
import config
import time

# Connect to IoT Analytics site and authenticate
print "Connecting to %s ..." % config.hostname
iot = iotkitclient.Request(host=config.hostname, proxies=config.proxies)
iot.login(config.username, config.password)
print "Connected. User ID: %s ..." % iot.user_id

# Get server information/status
print "IoT Analytics Server Info:"
iotkitclient.prettyprint(iot.get_version())

# Display authentication token info
print "Token info:"
iotkitclient.prettyprint(iot.get_user_tokeninfo())
Exemplo n.º 4
0
    }
    device.create_device(device_info)
    act_code = acct.renew_activation_code()
    device.activate_new_device(act_code)
    device.save_config("device.json", True)

device.load_config("device.json")
print "Using Device: %s ..." % device_id

# Set up actuator component - if it doesn't exist
cname = "motor"
ctype = "powerswitch.v1.0"
comp = device.component()
if comp.get_component(cname):
    print "Found component:", comp.id
else:
    comp.add_component(cname, ctype)

# Set up actuation message
msg["commands"][0]["componentId"] = str(comp.id)
msg["commands"][0]["parameters"][0]["value"] = "1"
print "*** Sending actuation message:"
iotkitclient.prettyprint(msg)
acct.send_control_msg(msg)

# List actuation history for this device
print "*** Listing actuation history:"
start_time = 0
js = acct.list_control_msgs(device_id, start_time)
iotkitclient.prettyprint(js)
Exemplo n.º 5
0
acct.get_account(config.account_name)
print "*** Using Account: %s (%s)" % (acct.id, config.account_name)
invite = acct.invites()

# Delete any pending invites
invite_list = invite.get_account_invites()
for user_email in invite_list:
    print "  *** Deleting old invites for", user_email
    invite.delete_invites(user_email)

# Display list of pending invites
invite_list = acct.invites().get_account_invites()
print "*** Pending invites for Account: %s" % config.account_name
for email in invite_list:
    print "   %s" % email

# Connect and login to IoT cloud as invitee
iot2 = iotkitclient.Request(host=config.hostname)
iot2.login(config.invitee_email, config.invitee_password)
acct2 = iot2.account()
invite2 = acct2.invites()

# find invite and accept
invite_list = invite2.get_user_invites(config.invitee_email)
for item in invite_list:
    invite_id = item["_id"]
    print "*** Accepting invite:", invite_id
    iotkitclient.prettyprint(invite2.accept_invite(invite_id))
    print "*** Deleting invites for:", config.invitee_email
    iotkitclient.prettyprint(invite.delete_invites(config.invitee_email))
Exemplo n.º 6
0
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import iotkitclient
import config

# Connect to IoT Analytics site and authenticate
iot = iotkitclient.Request(host=config.hostname, proxies=config.proxies)
iot.login(config.username, config.password)

# Create IoT Analytics account instance
acct = iot.account()
print acct.client.base_url
try:
    # check if account exists
    acct.get_account(config.account_name)
    print "Using existing account \"%s\" (%s)." % (config.account_name, acct.id)
except Exception, ex:
    acct.create_account(config.account_name)
    print "Creating new account \"%s\" (%s)." % (config.account_name, acct.id)
    iot.reinit(config.username, config.password)

print "Account info:"
iotkitclient.prettyprint(acct.get_info())

print "Account users:"
iotkitclient.prettyprint(acct.list_account_users())

print "Generate a device activation code: %s" % acct.renew_activation_code()
Exemplo n.º 7
0
try:
    device.get_device(device_id)
except Exception, ex:
    raise RuntimeError(str(ex))

device.load_config("device.json")
print "Using Device: %s ..." % device_id

comp = device.component()
if not comp.get_component(config.component_name):
    comp.add_component(config.component_name, config.component_type)

print "Using Component: %s (%s) ..." % (config.component_name, comp.id)

data = [
    (int(time.time() * 1000 - 500), "65.3"),
    (int(time.time() * 1000 - 400), "57.5"),
    (int(time.time() * 1000 - 300), "61.4"),
    (int(time.time() * 1000 - 200), "59.2"),
    (int(time.time() * 1000 - 100), "66.5"),
    (int(time.time() * 1000), "65.3")
]
dataseries = device.package_data_series(data, comp.id)
device.send_data(dataseries)
print "Submitted data: "
iotkitclient.prettyprint(dataseries)

# save latest device-info
device.get_device(device_id)
device.save_config("device.json", True)
Exemplo n.º 8
0
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import iotkitclient
import config

# Connect to IoT Analytics site and authenticate
iot = iotkitclient.Connect(host=config.hostname, proxies=config.proxies)
iot.login(config.username, config.password)

# Create IoT Analytics account instance
acct = iotkitclient.Account(iot)
try:
    # check if account exists
    acct.get_account(config.account_name)
    print "Using existing account \"%s\" (%s)." % (config.account_name,
                                                   acct.id)
except Exception, ex:
    acct.create_account(config.account_name)
    print "Creating new account \"%s\" (%s)." % (config.account_name, acct.id)
    iot.reinit(config.username, config.password)

print "Account info:"
iotkitclient.prettyprint(acct.get_info())

print "Account users:"
iotkitclient.prettyprint(acct.list_account_users())

print "Generate a device activation code: %s" % acct.renew_activation_code()
comp_type_info = {
                    "dimension": sensor_type,
                    "version": str(sensor_version),
                    "type": "sensor",
                    "dataType":"Number",
                    "format": "float",
                    "min": -150,
                    "max": 150,
                    "measureunit": "Degress Celsius",
                    "display": "timeSeries"
                 }

print "*** Adding new component type (%s) to Component Catalog" % comp_type_id
# Create new component type
info = catalog.add_comp_type(comp_type_info)
iotkitclient.prettyprint(info)

# Update new component (sensor version will be incremented)
sensor_version = float(sensor_version) + 0.1
new_comp_type_id = sensor_type + ".v" + str(sensor_version)
print "*** Updating component type (%s) in Component Catalog..." % comp_type_id
print "    ...will create %s" % new_comp_type_id

# Create random min/max sensor values
max = int(random.random()*100)
new_info = {
    "max": max,
    "min": -1 * max
}
print "    min: %d" % new_info["min"]
print "    max: %d" % new_info["max"]
Exemplo n.º 10
0
            }
        ]
    },
    "resetType": "Automatic",
    "population": {
        "ids": [
            "685.1.1.1"
        ],
        "attributes": None
    }
}

rule.add_rule(rule_info)

rules = rule.get_rules()
for r in rules:
    if r["name"] == rule_name:
        id = r["externalId"]
        info = rule.get_rule(id)
        iotkitclient.prettyprint(info)
        print "*** Updating Rule %s" % id
        rule_info["description"] = "This is an updated rule"
        try:
            rule.update_rule(rule_info)
            rule.update_rule_status("Archived")
        except:
            pass
        iotkitclient.prettyprint(rule.get_rule(id))

rule.update_rule_status("Active")
Exemplo n.º 11
0
        "gatewayId": device_id,
        "name": "Device #1"
    }
    # Create device
    print "Creating new device:", device_id
    device.create_device(device_info)
    # Obtain activation code
    act_code = acct.renew_activation_code()
    # Activate device
    device.activate_new_device(act_code)
    # Save device token to file **DO NOT LOSE THIS TOKEN**
    print "Writing device token to device.json..."
    device.save_config("device.json", True)

print "Device Info:"
iotkitclient.prettyprint(device.get_device())

print "Update Device Info:"
device_info = {
    "name": "Just changed my name"
}
device.update_device(device_info, device_id)
iotkitclient.prettyprint(device.get_device())

# Add/find a component
comp = iotkitclient.Component(device)
if comp.get_component(config.component_name):
    print "Found component:", comp.id
else:
    comp.add_component(config.component_name, config.component_type)
    print "Created component:", comp.id
Exemplo n.º 12
0
    acct.get_account(config.account_name)
except Exception, err:    
    raise RuntimeError("Unable to find account %s" % str(err))
    
device_id = iot.user_id + "_01"
device = iotkitclient.Device(acct, device_id)
comp = iotkitclient.Component(device)
comp.get_component(config.component_name)

t0 = 0
t1 = None
devices = [device_id]
components = [comp.id]
data = acct.search_data(t0, t1, devices, components, csv=False)
print "\nSimple query..."
iotkitclient.prettyprint(data)

data = acct.search_data(t0, t1, devices, components, csv=True)
print "\nSimple query (CSV format)..."
iotkitclient.prettyprint(data)

print "\nAdvanced query..."
# filters = {
    # "gatewayIds" : ["<gid1>", "<gid2>"],
    # "deviceIds" : ["<did1>", "<did2>"],
    # "componentIds" : ["<cid1>", "<cid2>"],
    # "from" :  <start_datetime>,
    # "to" :  <end_datetime>,
    # "returnedMeasureAttributes" : ["att_1", "att_2"],
    # "showMeasureLocation" :  <true/false>,
        # "aggregations":<include/exclude/only>,
Exemplo n.º 13
0
                    "gatewayId": device_id,
                    "name": "Device #1"
                  }
    # Create device
    print "Creating new device:", device_id
    device.create_device(device_info)
    # Obtain activation code
    act_code = acct.renew_activation_code()
    # Activate device
    device.activate_new_device(act_code)
    # Save device token to file **DO NOT LOSE THIS TOKEN**
    print "Writing device token to device.json..."
    device.save_config("device.json", True)
    
print "Device Info:"
iotkitclient.prettyprint(device.get_device())

print "Update Device Info:"
device_info = {
                "name": "Just changed my name"
              }
device.update_device(device_info, device_id)
iotkitclient.prettyprint(device.get_device())


    

    
    
    
Exemplo n.º 14
0
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Get list of alerts and print details for each one

import iotkitclient
import config
import time
import json
import utils

iot = iotkitclient.Request(host=config.hostname)
iot.login(config.username, config.password)
print "*** Connected. User ID: %s ..." % iot.user_id

acct = iot.account()
acct.get_account(config.account_name)
print "*** Using Account: %s (%s)" % (acct.id, config.account_name)

alert = acct.alert()
alerts = alert.get_alerts()
print "*** Alerts (%d):" % len(alerts)
for a in alerts:
    id = a["alertId"]
    triggered = int(a["triggered"]) / 1000
    status = a["status"]
    print "--- Alert", id, status, utils.unix_to_realtime(int(triggered))
    iotkitclient.prettyprint(alert.get_alert(id))
    # alert.reset_alert(id)
Exemplo n.º 15
0
comp_type_info_dust = {
                    "dimension": sensor_type_dust,
                    "version": str(sensor_version_dust),
                    "type": "sensor",
                    "dataType":"Number",
                    "format": "float",
                    "min": 0,
                    "max": 8000,
                    "measureunit": "0.01 cf",
                    "display": "timeSeries"
                 }

print "*** Adding new component type (%s) to Component Catalog" % comp_type_id_dust
# Create new accel component type
info_dust = catalog.add_comp_type(comp_type_info_dust)
iotkitclient.prettyprint(info_dust)
##
### Add sound Component
##sensor_version_sound = "1.4"
##sensor_type_sound = "sound"
##for type in catalog.get_comp_types():
##    print type["id"]
##    if (type["dimension"] == sensor_type_sound and
##       type["version"] is not None):
##        if type["version"] >= sensor_version_sound:
##            sensor_version_sound = type["version"]
### increment sensor version
##sensor_version_sound = float(sensor_version_sound) + 0.1
##
### Define new component type properties
##comp_type_id_sound = sensor_type_sound + ".v" + str(sensor_version_sound)