FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
webthing-python/example/single-thing.py at master · WebThingsIO/webthing-python · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
WebThingsIO
/
webthing-python
Public
Notifications
You must be signed in to change notification settings
Fork
36
Star
175
Code
Issues
2
Pull requests
2
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
webthing-python
/
example
/
single-thing.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
118 lines (100 loc) · 3.28 KB
Breadcrumbs
webthing-python
/
example
/
single-thing.py
Copy path
File metadata and controls
118 lines (100 loc) · 3.28 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
107
108
109
110
111
112
113
114
115
116
117
118
from
__future__
import
division
from
webthing
import
(
Action
,
Event
,
Property
,
SingleThing
,
Thing
,
Value
,
WebThingServer
)
import
logging
import
time
import
uuid
class
OverheatedEvent
(
Event
):
def
__init__
(
self
,
thing
,
data
):
Event
.
__init__
(
self
,
thing
,
'overheated'
,
data
=
data
)
class
FadeAction
(
Action
):
def
__init__
(
self
,
thing
,
input_
):
Action
.
__init__
(
self
,
uuid
.
uuid4
().
hex
,
thing
,
'fade'
,
input_
=
input_
)
def
perform_action
(
self
):
time
.
sleep
(
self
.
input
[
'duration'
]
/
1000
)
self
.
thing
.
set_property
(
'brightness'
,
self
.
input
[
'brightness'
])
self
.
thing
.
add_event
(
OverheatedEvent
(
self
.
thing
,
102
))
def
make_thing
():
thing
=
Thing
(
'urn:dev:ops:my-lamp-1234'
,
'My Lamp'
,
[
'OnOffSwitch'
,
'Light'
],
'A web connected lamp'
)
thing
.
add_property
(
Property
(
thing
,
'on'
,
Value
(
True
),
metadata
=
{
'@type'
:
'OnOffProperty'
,
'title'
:
'On/Off'
,
'type'
:
'boolean'
,
'description'
:
'Whether the lamp is turned on'
,
}))
thing
.
add_property
(
Property
(
thing
,
'brightness'
,
Value
(
50
),
metadata
=
{
'@type'
:
'BrightnessProperty'
,
'title'
:
'Brightness'
,
'type'
:
'integer'
,
'description'
:
'The level of light from 0-100'
,
'minimum'
:
0
,
'maximum'
:
100
,
'unit'
:
'percent'
,
}))
thing
.
add_available_action
(
'fade'
,
{
'title'
:
'Fade'
,
'description'
:
'Fade the lamp to a given level'
,
'input'
: {
'type'
:
'object'
,
'required'
: [
'brightness'
,
'duration'
,
],
'properties'
: {
'brightness'
: {
'type'
:
'integer'
,
'minimum'
:
0
,
'maximum'
:
100
,
'unit'
:
'percent'
,
},
'duration'
: {
'type'
:
'integer'
,
'minimum'
:
1
,
'unit'
:
'milliseconds'
,
},
},
},
},
FadeAction
)
thing
.
add_available_event
(
'overheated'
,
{
'description'
:
'The lamp has exceeded its safe operating temperature'
,
'type'
:
'number'
,
'unit'
:
'degree celsius'
,
})
return
thing
def
run_server
():
thing
=
make_thing
()
# If adding more than one thing, use MultipleThings() with a name.
# In the single thing case, the thing's name will be broadcast.
server
=
WebThingServer
(
SingleThing
(
thing
),
port
=
8888
)
try
:
logging
.
info
(
'starting the server'
)
server
.
start
()
except
KeyboardInterrupt
:
logging
.
info
(
'stopping the server'
)
server
.
stop
()
logging
.
info
(
'done'
)
if
__name__
==
'__main__'
:
logging
.
basicConfig
(
level
=
10
,
format
=
"%(asctime)s %(filename)s:%(lineno)s %(levelname)s %(message)s"
)
run_server
()
Back
|
FazBrowse Home
|
New Git URL