Skip to content

Commit 6e1a00a

Browse files
committed
Update local dev
1 parent 4e6c800 commit 6e1a00a

1 file changed

Lines changed: 24 additions & 91 deletions

File tree

docs/tutorials/control/air-quality-fleet.md

Lines changed: 24 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,6 @@ You can access the data from your browser's local storage with the following cod
283283

284284
Currently, Viam applications only provide access to single machines but in future you will be able to access entire locations or organizations.
285285

286-
{{< table >}}
287-
{{% tablestep number=1 %}}
288-
289286
Create another file inside the <file>aqi-dashboard</file> folder and name it <file>main.ts</file>.
290287
Paste the following code into <file>main.ts</file>:
291288

@@ -298,7 +295,7 @@ import Cookies from "js-cookie";
298295

299296
let apiKeyId = "";
300297
let apiKeySecret = "";
301-
let hostname = "";
298+
let host = "";
302299
let machineId = "";
303300

304301
async function main() {
@@ -319,103 +316,39 @@ async function main() {
319316
// <Insert getLastFewAv function definition here in later steps>
320317

321318
document.addEventListener("DOMContentLoaded", async () => {
322-
machineId = window.location.pathname.split("/")[2];
319+
// Extract the machine identifier from the URL
320+
let machineCookieKey = window.location.pathname.split("/")[2];
323321
({
324-
id: apiKeyId,
325-
key: apiKeySecret,
326-
hostname: hostname,
327-
} = JSON.parse(Cookies.get(machineId)!));
328-
322+
apiKey: { id: apiKeyId, key: apiKeySecret },
323+
machineId: machineId,
324+
hostname: host,
325+
} = JSON.parse(Cookies.get(machineCookieKey)!));
329326
main().catch((error) => {
330327
console.error("encountered an error:", error);
331328
});
329+
console.log(apiKeyId, apiKeySecret, host, machineId);
332330
});
333331
```
334332

335-
{{% /tablestep %}}
336-
{{% tablestep number=2 %}}
337-
338-
For developing your application on localhost, **add the same information to your browser's local storage**.
339-
340-
Navigate to [Camera Viewer](https://air-quality_naomi.viamapplications.com/) and log in, then select your development machine.
341-
342-
Open Developer Tools, go to the console and paste the following JavaScript to obtain the cookies you need:
333+
### Local development
343334

344-
```js {class="line-numbers linkable-line-numbers" data-line=""}
345-
function generateCookieSetterScript() {
346-
// Get all cookies from current page
347-
const currentCookies = document.cookie.split(";");
348-
let cookieSetterCode = "// Cookie setter script for localhost\n";
349-
cookieSetterCode +=
350-
"// Copy and paste this entire script into your browser console when on localhost\n\n";
335+
For developing your application on localhost:
351336

352-
// Process each cookie
353-
let cookieCount = 0;
354-
currentCookies.forEach((cookie) => {
355-
if (cookie.trim()) {
356-
// Extract name and value from the cookie
357-
const [name, value] = cookie.trim().split("=");
337+
1. Run the following command to serve the application you are building:
358338

359-
// Add code to set this cookie
360-
cookieSetterCode += `document.cookie = "${name}=${value}; path=/";\n`;
361-
cookieCount++;
362-
}
363-
});
364-
365-
// Add summary comment
366-
cookieSetterCode += `\nconsole.log("Set ${cookieCount} cookies on localhost");\n`;
367-
368-
// Display the generated code
369-
console.log(cookieSetterCode);
370-
371-
// Create a textarea element to make copying easier
372-
const textarea = document.createElement("textarea");
373-
textarea.value = cookieSetterCode;
374-
textarea.style.position = "fixed";
375-
textarea.style.top = "0";
376-
textarea.style.left = "0";
377-
textarea.style.width = "100%";
378-
textarea.style.height = "250px";
379-
textarea.style.zIndex = "9999";
380-
document.body.appendChild(textarea);
381-
textarea.focus();
382-
textarea.select();
383-
}
384-
385-
// Execute the function
386-
generateCookieSetterScript();
387-
```
388-
389-
{{% /tablestep %}}
390-
{{% tablestep number=3 %}}
391-
392-
Copy the resulting script. It will look like this:
393-
394-
```js {class="line-numbers linkable-line-numbers" data-line=""}
395-
// Cookie setter script for localhost
396-
// Copy and paste this entire script into your browser console when on localhost
397-
398-
document.cookie = "<SECRET COOKIE INFO>; path=/";
399-
document.cookie = "machinesWhoseCredentialsAreStored=<MACHINE ID>; path=/";
400-
401-
console.log("Set 2 cookies on localhost");
402-
```
403-
404-
{{% /tablestep %}}
405-
{{% tablestep number=4 %}}
406-
407-
Run the following command to serve the application you are building:
408-
409-
```sh {class="command-line" data-prompt="$" data-output="2-10"}
410-
npm run start
411-
```
339+
```sh {class="command-line" data-prompt="$" data-output="2-10"}
340+
npm run start
341+
```
412342

413-
Open the application in your browser at `http://127.0.0.1:8000/`.
343+
Open the application in your browser at `http://localhost:8000/`.
414344

415-
Then, open developer tools, go to the console and paste the copied JavaScript code to set your cookies.
345+
1. Run the following command specifying the address where your app is running on localhost and a machine to test on.
346+
The command will proxy your local app and open a browser window and navigate to `http://localhost:8000/machine/<machineHostname>` for the machine provided with --machine-id.
416347

417-
{{% /tablestep %}}
418-
{{< /table >}}
348+
```sh {class="command-line" data-prompt="$" data-output="3-10"}
349+
viam login
350+
viam module local-app-testing --app-url http://localhost:8000 --machine-id <MACHINE-ID>
351+
```
419352

420353
### Add functionality to your code
421354

@@ -742,7 +675,7 @@ You can find all the code in the [GitHub repo for this tutorial](https://github.
742675
npm start
743676
```
744677

745-
{{<imgproc src="/tutorials/air-quality-fleet/terminal-url.png" resize="800x" declaredimensions=true alt="Terminal window with the command 'npm start' run inside the aqi-dashboard folder. The output says 'start' and then 'esbuild' followed by the esbuild string from the package.json file you configured. Then there's 'Local:' followed by a URL and 'Network:' followed by a different URL." class="imgzoom" style="width:500px">}}
678+
{{<imgproc src="/tutorials/air-quality-fleet/terminal-url.png" resize="800x" declaredimensions=true alt="Terminal window with the command 'npm start' run inside the aqi-dashboard folder. The output says 'start' and then 'esbuild' followed by the esbuild string from the package.json file you configured. Then there's 'Local:' followed by a URL and 'Network:' followed by a different URL." class="imgzoom" style="width:800px">}}
746679

747680
1. The terminal should output a line such as `Local: http://127.0.0.1:8000/`.
748681
Copy the URL the terminal displays and paste it into the address bar in your web browser.
@@ -871,7 +804,7 @@ Repeat to add the New York office: Add a new location called `New York Office`,
871804

872805
## Getting machines ready for third parties
873806

874-
Continuing with our fictitious company, let's assume you want to ship air sensing machines to customers as ready-to-go as possible.
807+
Let's continue with our fictitious company and assume you want to ship air sensing machines out to customers from your factory.
875808
In other words, you want to provision devices.
876809

877810
Before an air sensing machine leaves your factory, you'd complete the following steps:
@@ -885,7 +818,7 @@ Once a customer receives your machine, they will:
885818
1. Plug it in and turn it on.
886819
2. `viam-agent` will start a WiFi network.
887820
3. The customer uses another device to connect to the machine's WiFi network and the user gives the machine the password for their WiFi network.
888-
4. The machine can now connect to the internet and complete setup based on the fragment it knows about.
821+
4. The machine can now connect to the internet and complete setup based on the specified fragment in the configuration template.
889822

890823
### Create the fragment for air sensing machines
891824

0 commit comments

Comments
 (0)