Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

backport #490

Merged
merged 9 commits into from
May 23, 2024
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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/__tests__/microbit-bluetooth-connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,16 @@ describe('Microbit Bluetooth interface tests', () => {

test('Request device yields device', async () => {
const device = await MicrobitBluetooth.requestDevice(
'vatav',
TypingUtils.emptyFunction,
'vatav',
);
expect(device).toBeDefined();
});

test('Can connect to requested device', async () => {
const device = await MicrobitBluetooth.requestDevice(
'vatav',
TypingUtils.emptyFunction,
'vatav',
);

const con = await MicrobitBluetooth.createMicrobitBluetooth(
Expand Down
3 changes: 3 additions & 0 deletions src/appInsights.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { ApplicationInsights } from '@microsoft/applicationinsights-web';
import CookieManager from './script/CookieManager';

const load = () => {
if (location.hostname !== "ml-machine.org") {
return;
}
if (CookieManager.getComplianceChoices().analytics) {
appInsights.loadAppInsights();
appInsights.trackPageView(); // Manually call trackPageView to establish the current user/session/pageview
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import { DeviceRequestStates } from '../../../script/stores/connectDialogStore';
import Environment from '../../../script/Environment';
import StaticConfiguration from '../../../StaticConfiguration';
import Logger from '../../../script/utils/Logger';

// callbacks
export let deviceState: DeviceRequestStates;
Expand All @@ -38,36 +39,47 @@

let timeouted = writable<boolean>(false);

const connectButtonClicked = () => {
const connectUsingPatternName = () => {
if (!isInputPatternValid()) {
attemptedToPairWithInvalidPattern = true;
return;
}

const name = MBSpecs.Utility.patternToName($patternMatrixState);
attemptToConnect(name);
};

const attemptToConnect = (name?: string) => {
timeoutProgress.set(0);
if (isConnecting) {
// Safeguard to prevent trying to connect multiple times at once
return;
}
isConnecting = true;
let name = MBSpecs.Utility.patternToName($patternMatrixState);
const connectionResult = () => {
if (deviceState == DeviceRequestStates.INPUT) {
return Microbits.assignInput(name);
if (name) {
return Microbits.assignInput(name);
}
return Microbits.assignInputNoName();
} else {
return Microbits.assignOutput(name);
if (name) {
return Microbits.assignOutput(name);
}
return Microbits.assignOutputNoName();
}
};

const connectTimeout = setTimeout(() => {
Environment.isInDevelopment && console.log('Connection timed out');
Logger.log('BluetoothConnectDialog', 'Connection timed-out');
timeouted.set(true);
}, StaticConfiguration.connectTimeoutDuration);

void connectionResult()
.then(didSucceed => {
clearTimeout(connectTimeout);
timeouted.set(false);
Environment.isInDevelopment && console.log('Connection result ', didSucceed);
Logger.log('BluetoothConnectDialog', 'Connection result:', didSucceed);
if (didSucceed) {
onBluetoothConnected();
} else {
Expand All @@ -83,7 +95,7 @@
if (event.code !== 'Enter') {
return;
}
void connectButtonClicked();
void connectUsingPatternName();
}

function updateMatrix(matrix: boolean[]): void {
Expand All @@ -102,6 +114,10 @@
// Resets the bluetooth connection prompt for cancelled device requests
$state.requestDeviceWasCancelled = false;
});

const handleSearchWithoutName = () => {
attemptToConnect();
};
</script>

<main>
Expand All @@ -111,6 +127,14 @@

{#if $state.requestDeviceWasCancelled && !isConnecting}
<p class="text-warning mb-1">{$t('popup.connectMB.bluetooth.cancelledConnection')}</p>
<p class="text-warning mb-1">
{$t('popup.connectMB.bluetooth.cancelledConnection.noNameDescription')}
<span
class="underline text-link cursor-pointer select-none"
on:click={handleSearchWithoutName}>
{$t('popup.connectMB.bluetooth.cancelledConnection.noNameLink')}
</span>
</p>
{/if}
{#if attemptedToPairWithInvalidPattern}
<p class="text-warning mb-1">{$t('popup.connectMB.bluetooth.invalidPattern')}</p>
Expand Down Expand Up @@ -138,7 +162,7 @@
<PatternMatrix matrix={$patternMatrixState} onMatrixChange={updateMatrix} />
</div>
</div>
<StandardButton onClick={connectButtonClicked}
<StandardButton onClick={connectUsingPatternName}
>{$t('popup.connectMB.bluetooth.connect')}</StandardButton>
{/if}
<!-- </div> -->
Expand Down
Loading
Loading