Skip to content
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
55 changes: 33 additions & 22 deletions examples/dashboard/src/DeviceActionsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,28 @@ function findClosest(arr: number[], target: number): number {
);
}

function getDefaultActionValue(action: DeviceTypeAction): number {
if (action.d !== undefined) {
return action.d;
// Map keys in display order: numeric keys (e.g. rates) ascending, label keys (e.g. "raw", "on") as listed
function getMapKeys(map: Record<string, unknown>): string[] {
const keys = Object.keys(map);
const allNumeric = keys.every((key) => key.trim() !== '' && Number.isFinite(Number(key)));
return allNumeric ? keys.sort((a, b) => Number(a) - Number(b)) : keys;
}

// The map key selected by default: the one matching d, else the first
function getDefaultMapKey(action: DeviceTypeAction): string {
const mapKeys = action.map ? getMapKeys(action.map) : [];
if (action.d !== undefined && mapKeys.includes(String(action.d))) {
return String(action.d);
}
return mapKeys[0] ?? '';
}

function getDefaultActionValue(action: DeviceTypeAction): number | string {
if (action.map) {
const firstMapKey = Object.keys(action.map).sort((a, b) => parseFloat(a) - parseFloat(b))[0];
return firstMapKey !== undefined ? parseFloat(firstMapKey) : 0;
return getDefaultMapKey(action);
}
if (action.d !== undefined) {
return action.d;
}
if (action.r && action.r.length > 1) {
return (action.r[1] + action.r[0]) / 2;
Expand All @@ -33,8 +48,9 @@ type DeviceActionsTableProps = {
deviceKey: string;
};

// A number for a value action, the map key for an action with a map
interface InputValues {
[key: string]: number;
[key: string]: number | string;
}

const DeviceActionsForm: React.FC<DeviceActionsTableProps> = ({ deviceKey }: DeviceActionsTableProps) => {
Expand Down Expand Up @@ -104,7 +120,7 @@ const DeviceActionsForm: React.FC<DeviceActionsTableProps> = ({ deviceKey }: Dev
}
}
if (bestKey) {
initialValues['_conf.rate'] = parseFloat(bestKey);
initialValues['_conf.rate'] = bestKey;
}
} else {
// For generic rate dropdown: find closest option
Expand All @@ -120,22 +136,22 @@ const DeviceActionsForm: React.FC<DeviceActionsTableProps> = ({ deviceKey }: Dev
}, 1000);
}, [deviceKey]);

const handleInputChange = (name: string, value: number) => {
const handleInputChange = (name: string, value: number | string) => {
setInputValues((prevValues: any) => ({
...prevValues,
[name]: value,
}));
};

const handleSendAction = async (action: DeviceTypeAction, value: number) => {
// Send action to device
if (!deviceManager || !Number.isFinite(value)) {
const handleSendAction = async (action: DeviceTypeAction, value: number | string) => {
// Send action to device - a map action sends its key, any other action a number
if (!deviceManager || (typeof value === 'number' ? !Number.isFinite(value) : value === '')) {
return;
}
// For _conf.rate actions, use setSampleRate for coordinated polling config
if (action.n === '_conf.rate' && action.map) {
setActionStatus('Setting sample rate...');
const result = await deviceManager.setSampleRate(deviceKey, value);
const result = await deviceManager.setSampleRate(deviceKey, Number(value));
if (result.ok) {
setActionStatus(`Rate: ${result.actualRateHz} Hz, poll: ${result.intervalUs} µs, buf: ${result.numSamples}`);
} else {
Expand Down Expand Up @@ -191,27 +207,22 @@ const DeviceActionsForm: React.FC<DeviceActionsTableProps> = ({ deviceKey }: Dev
</tr>
);
} else if (action.map) {
const mapKeys = Object.keys(action.map).sort((a, b) => parseFloat(a) - parseFloat(b));
const mapKeys = getMapKeys(action.map);
// Label: "Rate Hz" for _conf.rate, the description for other _conf.* settings (their names
// aren't readable), otherwise the action name - the description is shown on hover
const actionLabel = action.n === '_conf.rate' ? 'Rate Hz' :
(action.n.startsWith('_conf.') ? (action.desc ?? action.n) : action.n);
const actionValue = inputValues[action.n] ?? getDefaultActionValue(action);
const actionValue = String(inputValues[action.n] ?? getDefaultMapKey(action));
return (
<tr key={action.n}>
<td title={action.desc}>{actionLabel}</td>
<td>
<select
value={actionValue}
onChange={(e) =>
handleInputChange(
action.n,
parseFloat(e.target.value)
)
}
onChange={(e) => handleInputChange(action.n, e.target.value)}
>
{mapKeys.map((key) => (
<option key={key} value={parseFloat(key)}>
<option key={key} value={key}>
{key}
</option>
))}
Expand All @@ -232,7 +243,7 @@ const DeviceActionsForm: React.FC<DeviceActionsTableProps> = ({ deviceKey }: Dev
</tr>
);
} else {
const actionValue = inputValues[action.n] ?? getDefaultActionValue(action);
const actionValue = Number(inputValues[action.n] ?? getDefaultActionValue(action));
return (
<tr key={action.n}>
<td title={action.desc}>{action.n}</td>
Expand Down
2 changes: 1 addition & 1 deletion examples/dashboard/src/SystemTypeMarty/RICStateInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ export class RICStateInfo implements RaftDeviceMgrIF {
}

// Marty ROSSerial devices are read-only in this view
async sendAction(_deviceKey: string, _action: DeviceTypeAction, _data: number[]): Promise<boolean> {
async sendAction(_deviceKey: string, _action: DeviceTypeAction, _data: Array<number | string>): Promise<boolean> {
// Not supported for ROSSerial-derived devices
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@robdobsn/raftjs",
"version": "2.9.0",
"version": "2.9.1",
"description": "Javascript/TS library for Raft library",
"main": "dist/web/main.js",
"types": "dist/web/main.d.ts",
Expand Down
16 changes: 11 additions & 5 deletions src/RaftDeviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1457,12 +1457,14 @@ export class DeviceManager implements RaftDeviceMgrIF{
}
}

public async sendAction(deviceKey: string, action: DeviceTypeAction, data: number[]): Promise<boolean> {
// For an action with a map, data is one value: the map key (a string such as "raw", or a number for a
// numeric key such as "104")
public async sendAction(deviceKey: string, action: DeviceTypeAction, data: Array<number | string>): Promise<boolean> {
console.log(`DeviceManager sendAction ${deviceKey} action ${action.n} data ${data} map ${JSON.stringify(action.map)} keys ${action.map ? Object.keys(action.map) : 'none'}`);

// For _conf.* actions, delegate to setSampleRate() which coordinates polling params
if (action.n.startsWith('_conf.') && action.map && data.length === 1) {
const result = await this.setSampleRate(deviceKey, data[0]);
// For _conf.rate, delegate to setSampleRate() which coordinates polling params
if (action.n === '_conf.rate' && action.map && data.length === 1) {
const result = await this.setSampleRate(deviceKey, Number(data[0]));
return result.ok;
}

Expand Down Expand Up @@ -1495,7 +1497,11 @@ export class DeviceManager implements RaftDeviceMgrIF{
return false;
}
} else {
const encoded = encodeActionWrite(action, data);
if (data.some((val) => typeof val !== 'number')) {
console.warn(`DeviceManager sendAction: action ${action.n} has no map so needs numbers, got ${data}`);
return false;
}
const encoded = encodeActionWrite(action, data as number[]);
if (encoded === null) {
console.warn(`DeviceManager sendAction: can't encode ${data} for action ${action.n}`);
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/RaftDeviceMgrIF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ export default interface RaftDeviceMgrIF {
removeDeviceRemovedCallback(callback: (deviceKey: string, state: DeviceState) => void): void;

// Send action to device
sendAction(deviceKey: string, action: DeviceTypeAction, data: number[]): Promise<boolean>;
// For an action with a map, data is one value: the map key
sendAction(deviceKey: string, action: DeviceTypeAction, data: Array<number | string>): Promise<boolean>;
sendCompoundAction(deviceKey: string, action: DeviceTypeAction, data: number[][]): Promise<boolean>;

// Raw I2C write + read (returns read bytes, or null if unsupported/failed)
Expand Down
Loading