Skip to content
Draft
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
37 changes: 30 additions & 7 deletions backend/src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
// For example updating the EEPROM

import crypto from "node:crypto"
// import * as z from "zod"
import * as z from "zod"

import { read, write } from "../../lib/eeprom.js"
import { procedure, request } from "../../lib/mqtt.js"
import { setHardwareVersion } from "../../lib/hardware.js"
import { hardware_versions, setHardwareVersion } from "../../lib/hardware.js"

await procedure("factory/init", async () => {
const eeprom = await read()
let eeprom = await read()

if (eeprom?.custom_data?.eeprom_version !== 0) {
return {
eeprom = {
product_uuid: crypto.randomUUID(),
product_id: "0x0000", // TODO
product_ver: "0x0000", //TODO
Expand All @@ -31,14 +31,37 @@ await procedure("factory/init", async () => {
}
}

return eeprom
return { eeprom, hardware_versions }
})

const Schema = z.object({
hardware_version: z.enum(["v3.0", "v2.6"]),
serial_number: z.string(),
})

await procedure("factory/update", async (data) => {
const { hardware_version } = data.custom_data
const { hardware_version, serial_number } = Schema.parse(data)

if (hardware_version === "v3.0") {
await write({
product_uuid: crypto.randomUUID(),
product_id: "0x0000", // TODO
product_ver: "0x0000", //TODO
vendor: "FairScope",
product: "PlanktoScope HAT v3",
current_supply: 0,
dt_blob: "planktoscope-hat-v3",
custom_data: {
serial_number,
hardware_version: "v3.0",
eeprom_version: 0,
led_operating_time: 0,
},
})
}

await Promise.all([
hardware_version && write(data),
hardware_version === "v3.0" && write(data),
setHardwareVersion(hardware_version),
])

Expand Down
5 changes: 4 additions & 1 deletion backend/src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ app.post("/api/reset", async (req, res) => {

app.get("/", async (req, res) => {
const software_config = await readSoftwareConfig()
if (!software_config) {
return res.redirect(302, "/factory")
}

if (software_config?.user_setup !== true) {
if (software_config.user_setup !== true) {
return res.redirect(302, "/ps/node-red-v2/dashboard/setup")
}

Expand Down
33 changes: 15 additions & 18 deletions frontend/src/pages/factory.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ import { useSubmission, action } from "@solidjs/router"
import { request } from "../../../lib/mqtt.js"

export default function Factory() {
const [EEPROM, { refetch }] = createResource(
"factory/init",
async (topic) => {
return request(topic)
},
)
const [data, { refetch }] = createResource("factory/init", async (topic) => {
return request(topic)
})

const updateFactoryAction = action(async (data) => {
const {
Expand Down Expand Up @@ -58,23 +55,23 @@ export default function Factory() {
<input
name="product_uuid"
readonly={disabled}
value={EEPROM()?.product_uuid}
value={data()?.eeprom?.product_uuid ?? ""}
/>
</label>
<label>
product_id
<input
name="product_id"
readonly={disabled}
value={EEPROM()?.product_id}
value={data()?.eeprom?.product_id ?? ""}
/>
</label>
<label>
product_ver
<input
name="product_ver"
readonly={disabled}
value={EEPROM()?.product_ver}
value={data()?.eeprom?.product_ver ?? ""}
/>
</label>
</div>
Expand All @@ -84,7 +81,7 @@ export default function Factory() {
<input
name="product"
readonly={disabled}
value={EEPROM()?.product}
value={data()?.eeprom?.product ?? ""}
/>
</label>
{/* <label>
Expand All @@ -93,23 +90,23 @@ export default function Factory() {
name="current_supply"
type="number"
readonly={disabled}
value={EEPROM?.current_supply}
value={data()?.eeprom?.current_supply}
/>
</label>*/}
<label>
dt_blob
<input
name="dt_blob"
readonly={disabled}
value={EEPROM()?.dt_blob}
value={data()?.eeprom?.dt_blob ?? ""}
/>
</label>
<label>
vendor
<input
name="vendor"
readonly={disabled}
value={EEPROM()?.vendor}
value={data()?.eeprom?.vendor ?? ""}
/>
</label>
</div>
Expand All @@ -128,15 +125,15 @@ export default function Factory() {
serial_number
<input
name="serial_number"
value={EEPROM()?.custom_data?.serial_number}
value={data()?.eeprom?.custom_data?.serial_number ?? ""}
/>
</label>
<label>
led_operating_time
<input
name="led_operating_time"
type="number"
value={EEPROM()?.custom_data?.led_operating_time}
value={data()?.eeprom?.custom_data?.led_operating_time ?? ""}
/>
</label>
</fieldset>
Expand All @@ -146,7 +143,7 @@ export default function Factory() {
hardware_version
<input
name="hardware_version"
value={EEPROM()?.custom_data?.hardware_version}
value={data()?.eeprom?.custom_data?.hardware_version ?? ""}
/>
</label>
<label>
Expand All @@ -155,13 +152,13 @@ export default function Factory() {
name="eeprom_version"
type="number"
readonly={disabled}
value={EEPROM()?.custom_data?.eeprom_version}
value={data()?.eeprom?.custom_data?.eeprom_version ?? ""}
/>
</label>
</fieldset>

<button
disabled={EEPROM.loading || submission.pending}
disabled={data.loading || submission.pending}
aria-busy={submission.pending}
type="submit"
>
Expand Down
2 changes: 1 addition & 1 deletion lib/hardware.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { read as readEEPROM } from "./eeprom.js"

const execFile = promisify(child_process.execFile)

const hardware_versions = ["v3.0", "v2.6"].map((v) => {
export const hardware_versions = ["v3.0", "v2.6"].map((v) => {
return { label: `PlanktoScope ${v}`, value: v }
})

Expand Down
14 changes: 2 additions & 12 deletions lib/setup.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as z from "zod"

import { publish } from "./mqtt.js"
import { setHardwareVersion, detectHardwareVersion } from "./hardware.js"
import {
getWifiRegulatoryDomains,
setWifiRegulatoryDomain,
Expand All @@ -10,7 +9,6 @@ import {
import { getTimezones, getTimezone, setTimezone } from "./timezone.js"

import { updateSoftwareConfig } from "./file-config.js"
import { promiseDashboardOnline } from "./nodered.js"

export async function readSetup() {
const [regions, region, timezones, timezone] = await Promise.all([
Expand All @@ -35,18 +33,10 @@ const Schema = z.object({

export async function writeSetup(data) {
const { region, timezone } = Schema.parse(data)
const hardware_version = await detectHardwareVersion()

await Promise.all([
setWifiRegulatoryDomain(region),
setTimezone(timezone),
setHardwareVersion(hardware_version),
])
await Promise.all([setWifiRegulatoryDomain(region), setTimezone(timezone)])

await Promise.all([
updateSoftwareConfig({ user_setup: true }),
promiseDashboardOnline(),
])
await updateSoftwareConfig({ user_setup: true })

await publish("setup/ready", {})
}
Loading