Skip to content
This repository was archived by the owner on Dec 23, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 10 additions & 38 deletions src/adafruit_circuitplayground/express.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from . import constants as CONSTANTS
from collections import namedtuple
from applicationinsights import TelemetryClient
from .telemetry import telemetry_py

Acceleration = namedtuple('acceleration', ['x', 'y', 'z'])

Expand Down Expand Up @@ -43,16 +44,6 @@ def __init__(self):
'touch': [False]*7,
'shake': False,
}
self.telemetry_state = {
"DETECT_TAPS": False,
"TAPPED": False,
"RED_LED": False,
"ADJUST_THRESHOLD": False,
"PLAY_FILE": False,
"PLAY_TONE": False,
"START_TONE": False,
"STOP_TONE": False,
}
self.__debug_mode = False
self.__abs_path_to_code_file = ''
self.pixels = Pixel(self.__state, self.__debug_mode)
Expand All @@ -71,9 +62,7 @@ def button_b(self):

@property
def detect_taps(self):
if(utils.telemetry_available() and not self.telemetry_state["DETECT_TAPS"]):
utils.send_telemetry("DETECT_TAPS")
self.telemetry_state["DETECT_TAPS"] = True
telemetry_py.send_telemetry("DETECT_TAPS")
return self.__state['detect_taps']

@detect_taps.setter
Expand All @@ -86,23 +75,17 @@ def detect_taps(self, value):
def tapped(self):
""" Not Implemented!
"""
if(utils.telemetry_available() and not self.telemetry_state["TAPPED"]):
utils.send_telemetry("TAPPED")
self.telemetry_state["TAPPED"] = True
telemetry_py.send_telemetry("TAPPED")
raise NotImplementedError(CONSTANTS.NOT_IMPLEMENTED_ERROR)

@property
def red_led(self):
if(utils.telemetry_available() and not self.telemetry_state["RED_LED"]):
utils.send_telemetry("RED_LED")
self.telemetry_state["RED_LED"] = True
telemetry_py.send_telemetry("RED_LED")
return self.__state['red_led']

@red_led.setter
def red_led(self, value):
if(utils.telemetry_available() and not self.telemetry_state["RED_LED"]):
utils.send_telemetry("RED_LED")
self.telemetry_state["RED_LED"] = True
telemetry_py.send_telemetry("RED_LED")
self.__state['red_led'] = bool(value)
self.__show()

Expand Down Expand Up @@ -156,20 +139,15 @@ def adjust_touch_threshold(self, adjustement):
"""Not implemented!
The Pacifica Simulator doesn't use capacitive touch threshold.
"""
if(utils.telemetry_available() and not self.telemetry_state["ADJUST_THRESHOLD"]):
utils.send_telemetry("ADJUST_THRESHOLD")
self.telemetry_state["ADJUST_THRESHOLD"] = True

telemetry_py.send_telemetry("ADJUST_THRESHOLD")
raise NotImplementedError(
CONSTANTS.NOT_IMPLEMENTED_ERROR)

def shake(self, shake_threshold=30):
return self.__state['shake']

def play_file(self, file_name):
if(utils.telemetry_available() and not self.telemetry_state["PLAY_FILE"]):
utils.send_telemetry("PLAY_FILE")
self.telemetry_state["PLAY_FILE"] = True
telemetry_py.send_telemetry("PLAY_FILE")
file_name = utils.remove_leading_slashes(file_name)
abs_path_parent_dir = os.path.abspath(
os.path.join(self.__abs_path_to_code_file, os.pardir))
Expand All @@ -192,27 +170,21 @@ def play_file(self, file_name):
def play_tone(self, frequency, duration):
""" Not Implemented!
"""
if(utils.telemetry_available() and not self.telemetry_state["PLAY_TONE"]):
utils.send_telemetry("PLAY_TONE")
self.telemetry_state["PLAY_TONE"] = True
telemetry_py.send_telemetry("PLAY_TONE")
raise NotImplementedError(
CONSTANTS.NOT_IMPLEMENTED_ERROR)

def start_tone(self, frequency):
""" Not Implemented!
"""
if(utils.telemetry_available() and not self.telemetry_state["START_TONE"]):
utils.send_telemetry("START_TONE")
self.telemetry_state["START_TONE"] = True
telemetry_py.send_telemetry("START_TONE")
raise NotImplementedError(
CONSTANTS.NOT_IMPLEMENTED_ERROR)

def stop_tone(self):
""" Not Implemented!
"""
if(utils.telemetry_available() and not self.telemetry_state["STOP_TONE"]):
utils.send_telemetry("STOP_TONE")
self.telemetry_state["STOP_TONE"] = True
telemetry_py.send_telemetry("STOP_TONE")
raise NotImplementedError(
CONSTANTS.NOT_IMPLEMENTED_ERROR)

Expand Down
9 changes: 3 additions & 6 deletions src/adafruit_circuitplayground/pixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from . import utils
from applicationinsights import TelemetryClient
from . import constants as CONSTANTS
from .telemetry import telemetry_py


class Pixel:
Expand All @@ -31,15 +32,11 @@ def __getitem__(self, index):
if type(index) is not slice:
if not self.__valid_index(index):
raise IndexError(CONSTANTS.INDEX_ERROR)
if(utils.telemetry_available() and not self.telemetry_state):
utils.send_telemetry("PIXELS")
self.telemetry_state = True
telemetry_py.send_telemetry("PIXELS")
return self.__state['pixels'][index]

def __setitem__(self, index, val):
if(utils.telemetry_available() and not self.telemetry_state):
utils.send_telemetry("PIXELS")
self.telemetry_state = True
telemetry_py.send_telemetry("PIXELS")
is_slice = False
if type(index) is slice:
is_slice = True
Expand Down
34 changes: 34 additions & 0 deletions src/adafruit_circuitplayground/telemetry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from . import constants as CONSTANTS
from applicationinsights import TelemetryClient


class Telemetry:
def __init__(self):
# State of the telemetry
self.__enable_telemetry = True
self.telemetry_state = {
"DETECT_TAPS": False,
"TAPPED": False,
"RED_LED": False,
"ADJUST_THRESHOLD": False,
"PLAY_FILE": False,
"PLAY_TONE": False,
"START_TONE": False,
"STOP_TONE": False,
"PIXELS": False
}
self.telemetry_client = TelemetryClient('__AIKEY__')
self.extension_name = '__EXTENSIONNAME__'

def send_telemetry(self, event_name):
if self.__enable_telemetry and self.telemetry_available() and not self.telemetry_state[event_name]:
self.telemetry_client.track_event(
'{}/{}'.format(self.extension_name, CONSTANTS.TELEMETRY_EVENT_NAMES[event_name]))
self.telemetry_client.flush()
self.telemetry_state[event_name] = True

def telemetry_available(self):
return self.telemetry_client.context.instrumentation_key != '__AIKEY__'


telemetry_py = Telemetry()
14 changes: 1 addition & 13 deletions src/adafruit_circuitplayground/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from applicationinsights import TelemetryClient

previous_state = {}
telemetry_client = TelemetryClient('__AIKEY__')
EXTENSION_NAME = '__EXTENSIONNAME__'


def show(state, debug_mode=False):
Expand All @@ -35,14 +33,4 @@ def remove_leading_slashes(string):
def escape_if_OSX(file_name):
if sys.platform.startswith(CONSTANTS.MAC_OS):
file_name = file_name.replace(" ", "%20")
return file_name


def send_telemetry(event_name):
telemetry_client.track_event(
'{}/{}'.format(EXTENSION_NAME, CONSTANTS.TELEMETRY_EVENT_NAMES[event_name]))
telemetry_client.flush()


def telemetry_available():
return telemetry_client.context.instrumentation_key != '__AIKEY__'
return file_name
20 changes: 9 additions & 11 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ export async function activate(context: vscode.ExtensionContext) {

childProcess = cp.spawn(pythonExecutableName, [
utils.getPathToScript(context, "out", "process_user_code.py"),
currentFileAbsPath
currentFileAbsPath,
JSON.stringify({ enable_telemetry: utils.getTelemetryState() })
]);

let dataFromTheProcess = "";
Expand Down Expand Up @@ -446,7 +447,7 @@ export async function activate(context: vscode.ExtensionContext) {
case "print":
console.log(
`Process print statement output = ${
messageToWebview.data
messageToWebview.data
}`
);
utils.logToOutputChannel(
Expand Down Expand Up @@ -648,10 +649,9 @@ export async function activate(context: vscode.ExtensionContext) {
"pacifica.selectSerialPort",
() => {
if (serialMonitor) {
telemetryAI.runWithLatencyMeasure(
() => { serialMonitor.selectSerialPort(null, null); },
TelemetryEventName.COMMAND_SERIAL_MONITOR_CHOOSE_PORT
);
telemetryAI.runWithLatencyMeasure(() => {
serialMonitor.selectSerialPort(null, null);
}, TelemetryEventName.COMMAND_SERIAL_MONITOR_CHOOSE_PORT);
} else {
vscode.window.showErrorMessage(CONSTANTS.ERROR.NO_FOLDER_OPENED);
console.info("Serial monitor is not defined.");
Expand Down Expand Up @@ -693,10 +693,9 @@ export async function activate(context: vscode.ExtensionContext) {
"pacifica.closeSerialMonitor",
(port, showWarning = true) => {
if (serialMonitor) {
telemetryAI.runWithLatencyMeasure(
() => { serialMonitor.closeSerialMonitor(port, showWarning); },
TelemetryEventName.COMMAND_SERIAL_MONITOR_CLOSE
)
telemetryAI.runWithLatencyMeasure(() => {
serialMonitor.closeSerialMonitor(port, showWarning);
}, TelemetryEventName.COMMAND_SERIAL_MONITOR_CLOSE);
} else {
vscode.window.showErrorMessage(CONSTANTS.ERROR.NO_FOLDER_OPENED);
console.info("Serial monitor is not defined.");
Expand Down Expand Up @@ -874,7 +873,6 @@ const handleSensorTelemetry = (sensor: string) => {

const checkForTelemetry = (sensorState: any) => {
if (sensorState["shake"]) {
console.log(`telemtry sending`);
handleSensorTelemetry("shake");
} else if (sensorState["touch"]) {
handleSensorTelemetry("touch");
Expand Down
Loading