forked from amcgee7/picroft_example_skill_gpio
-
Notifications
You must be signed in to change notification settings - Fork 13
/
__init__.py
199 lines (160 loc) · 6.3 KB
/
__init__.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Copyright 2018, Mycroft AI Inc.
#
# 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
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
# -*- coding: utf-8 -*-
"""Skill controlls GPIO on Raspberry Pi.
This allows users to control an LED The LED is Attached to GPIO1
Example:
literal blocks::
Turn Led On
Turn Led Off
Blink Led
Led Status
Responses:
literal blocks::
Led is Off
Led is On
Button Pressed
Button Released
"""
from os.path import dirname, abspath
import sys
import requests
import json
import threading
sys.path.append(abspath(dirname(__file__)))
from adapt.intent import IntentBuilder
try:
from mycroft.skills.core import MycroftSkill
except:
class MycroftSkill:
pass
#import logging.handlers
import GPIO
""" Includes the GPIO interface"""
__author__ = 'amcgee7'
class GPIO_ControlSkill(MycroftSkill):
"""This is the skill for controlling GPIO of the Raspberry Pi
Attributes:
blink_active (bool): Defauts to False and is true while the
led is suppost to be blinking. Will be turned to False on
repeated blink command or when the led is instructed to go
on or off.
"""
def on_led_change(self):
"""used to report the state of the led.
This is attached to the on change event. And will speak the
status of the led.
"""
status = GPIO.get("GPIO1")
self.speak("Led is %s" % status)
def on_button_change(self):
status = GPIO.get("Button")
self.speak("Button is %s" % status)
def __init__(self):
"""This is used to initize the GPIO kill
This will set the default of blink_active and setup the function
for listening to the io change.
"""
self.blink_active = False
GPIO.on("GPIO1",self.on_led_change)
GPIO.on("Button",self.on_button_change)
super(GPIO_ControlSkill, self).__init__(name="GPIO_ControlSkill")
def blink_led(self):
"""This Will Start the Led blink process
This function will start the led blink process and continue
until blink_active is false.
"""
if self.blink_active:
threading.Timer(10, self.blink_led).start()
if self.blink_active:
if GPIO.get("GPIO1")!="On":
GPIO.set("GPIO1","On")
else:
GPIO.set("GPIO1","Off")
def initialize(self):
"""This function will initialize the Skill for Blinking an LED
This creates two intents
* IoCommandIntent - Will fire for any command that controlls the LED
* SystemQueryIntent - Will fire for any system command
The SystemQueryIntent was desinged for debug info while testing
and is not required going forward.
"""
self.load_data_files(dirname(__file__))
command_intent = IntentBuilder("IoCommandIntent").require("command").require("ioobject").optionally("ioparam").build()
system_intent = IntentBuilder("SystemQueryIntent").require("question").require("systemobject").build()
self.register_intent(command_intent, self.handle_command_intent)
self.register_intent(system_intent, self.handle_system_intent)
def handle_system_intent(self, message):
"""This is the handeler for system intent.
This will handle all questions of the system for debug info.
Args:
message(obj):
This is the object containing the message that fired the
intent. This is used to discover what to do within the
intent.
"""
if message.data["systemobject"] == "Name":
self.speak_dialog("name")
self.speak(__name__)
elif message.data["systemobject"] == "GPIO":
self.speak_dialog("check")
if GPIO.is_imported:
self.speak("GPIO is Imported")
else:
self.speak("GPIO is not Imported")
elif message.data["systemobject"] == "Modules":
self.speak_dialog("modules")
for module in sys.modules:
self.speak(module)
elif message.data["systemobject"] == "Path":
self.speak_dialog("path")
for path in sys.path:
self.speak(path)
def handle_command_intent(self, message):
"""This will handle all command intents for controlling GPIO
This handles all commands to controll the LEDS including checking
the status.
Args:
message(obj):
This is the object containing the message that fired the
intent. This is used to discover what to do within the
intent.
"""
if message.data["command"].upper() == "BLINK":
self.speak_dialog("ledblink")
if self.blink_active:
self.blink_active = False
else:
self.blink_active = True
self.blink_led()
elif message.data["command"].upper() == "STATUS":
if message.data["ioobject"].upper() == "LED":
self.on_led_change()
elif message.data["command"].upper() == "TURN":
if message.data["ioobject"].upper() == "LED":
if "ioparam" in message.data:
if message.data["ioparam"].upper() == "ON":
self.blink_active = False
GPIO.set("GPIO1","On")
elif message.data["ioparam"].upper() == "OFF":
self.blink_active = False
GPIO.set("GPIO1","Off")
else:
self.speak_dialog("ipparamrequired")
def stop(self):
"""This function will clean up the Skill"""
self.blink_active = False
def create_skill():
"""This function is to create the skill"""
return GPIO_ControlSkill()