-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7.1_offer_control.py
106 lines (81 loc) · 4.07 KB
/
7.1_offer_control.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2016 Iotic Labs Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://github.com/Iotic-Labs/py-application-examples/blob/master/LICENSE
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# INFO-----------------------------------------------------------------------------------------------------------------
# 7.1_offer_control.py 12/02/2016 Iotic Labs.
# Creates a new Thing (or connects an existing one).
# Prints information about that Thing (Thing object, lid, guid).
# Adds Thing metadata and tags.
# Advertises a Control.
# Adds Control metadata and tags
# Makes Thing private
# Adds Control callback and prints data received.
# PYTHON2 COMPATIBILITY -----------------------------------------------------------------------------------------------
from __future__ import unicode_literals, print_function # pylint: disable=unused-import
# LOGGING -------------------------------------------------------------------------------------------------------------
# Logging set to only CRITICAL messages by default. To see more, use logging.INFO, or to see loads, logging.DEBUG
import logging
logging.basicConfig(format='%(asctime)s,%(msecs)03d %(levelname)s [%(name)s] {%(threadName)s} %(message)s',
level=logging.CRITICAL)
# IMPORTS -------------------------------------------------------------------------------------------------------------
import time
# IOTIC AGENT IMPORTS -------------------------------------------------------------------------------------------------
from IoticAgent import IOT
# ---------------------------------------------------------------------------------------------------------------------
def create_thing(client):
print("Creating my Thing")
# Create your Thing in this script
# Note:
# Calling 'create_thing' will create a new Thing, unless the Thing local ID (lid) is already in use.
# If it is, it will connect your script to that Thing.
my_thing = client.create_thing('My_Controllable_Thing') # GIVE IT A NAME
# Let's have a look at it
# print some information about your Thing
print("About my Thing")
print("My Thing object:", my_thing)
print("My Thing local ID (lid):", my_thing.lid)
print("My Thing globally unique ID (guid):", my_thing.guid)
return my_thing
def create_control(my_thing):
print("Creating a Control")
# create a Control
my_control = my_thing.create_control('My_Controllable_Point', ctrl_callback)
# Let's have a look at it
# Print some information about the Control you created
print("About my Control")
print("My Control object:", my_control)
print("My Control local ID (pid):", my_control.pid)
print("My Thing local ID that advertises this Control (lid):", my_control.lid)
print("My Control globally unique ID (guid):", my_control.guid) # guid or gpid
print("Check this is a feed or control:", my_control.foc)
return my_control
def ctrl_callback(data):
print("Callback receiving data:", data)
# MAIN -------------------------------------------------------------------------------------------------------------
def main():
with IOT.Client('Control_Offer.ini') as client:
my_thing = create_thing(client)
create_control(my_thing)
while True:
try:
print("Main running, press ctrl+c to quit.")
time.sleep(10)
except KeyboardInterrupt:
break
# RUN --------------------------------------------------------------------------------------------------
if __name__ == '__main__':
main()
# END --------------------------------------------------------------------------------------------------