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

Runtime status #139

Open
wants to merge 12 commits into
base: dev
Choose a base branch
from
16 changes: 11 additions & 5 deletions main/networking/Runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import * as protos from '../../protos-main';

import RendererBridge from '../RendererBridge';
import { updateConsole } from '../../renderer/actions/ConsoleActions';
import { infoPerMessage } from '../../renderer/actions/InfoActions';
import { updatePeripherals } from '../../renderer/actions/PeripheralActions';
import { runtimeDisconnect, infoPerMessage, updateCodeStatus } from '../../renderer/actions/InfoActions';
import { updateBattery, updatePeripherals, updateRuntimeVersion } from '../../renderer/actions/PeripheralActions';
import { Logger, defaults } from '../../renderer/utils/utils';
import { Peripheral } from '../../renderer/types';
import { setLatencyValue } from '../../renderer/actions/EditorActions';
Expand All @@ -28,9 +28,10 @@ enum MsgType {
START_POS = 1,
LOG = 2,
DEVICE_DATA = 3,
// 4 reserved for some Shepherd msg type
// 5 reserved for some Shepherd msg type
INPUTS = 5,
TIME_STAMPS = 6
TIME_STAMPS = 6,
RUNTIME_STATUS = 7
}

interface TCPPacket {
Expand Down Expand Up @@ -200,7 +201,12 @@ class RuntimeConnection {
// TODO: we can probably do an average of n timestamps so the display doesn't change too frequently
RendererBridge.reduxDispatch(setLatencyValue(oneWayLatency));
break;

case MsgType.RUNTIME_STATUS:
decoded = protos.RuntimeStatus.decode(packet.payload);
//Not sure if we need to update runtimeStatus state bcs heartbeat keeps track of that already
RendererBridge.reduxDispatch(updateCodeStatus(decoded.mode));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Runtime Status sends us the current run mode it is in. Calls the updateCodeStatus action to change the mode

RendererBridge.reduxDispatch(updateRuntimeVersion(decoded.version));
RendererBridge.reduxDispatch(updateBattery(decoded.battery));
case MsgType.DEVICE_DATA:
try {
RendererBridge.reduxDispatch(infoPerMessage());
Expand Down
10 changes: 10 additions & 0 deletions renderer/actions/PeripheralActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ export const peripheralRename: PeripheralActions['peripheralRename'] = (uid: num
id: uid,
name: newName,
});

export const updateRuntimeVersion: PeripheralActions['updateRuntimeVersion'] = (version: string) => ({
type: consts.PeripheralActionsTypes.UPDATE_RUNTIME_VERSION,
version: version,
})

export const updateBattery: PeripheralActions['updateBattery'] = (battery: number) => ({
type: consts.PeripheralActionsTypes.UPDATE_BATTERY,
battery: battery,
})
2 changes: 2 additions & 0 deletions renderer/consts/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export enum ConsoleActionsTypes {
export enum PeripheralActionsTypes {
UPDATE_PERIPHERALS = 'UPDATE_PERIPHERALS',
PERIPHERAL_RENAME = 'PERIPHERAL_RENAME',
UPDATE_RUNTIME_VERSION = 'UPDATE_RUNTIME_VERSION',
UPDATE_BATTERY = 'UPDATE_BATTERY',
}

export enum EditorActionsTypes {
Expand Down
22 changes: 15 additions & 7 deletions renderer/reducers/peripherals.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as consts from '../consts';
import { UpdatePeripheralsAction, PeripheralRenameAction, Peripheral, PeripheralList } from '../types';
import { UpdatePeripheralsAction, PeripheralRenameAction, Peripheral, PeripheralList, UpdateRuntimeVersionAction, UpdateBatteryAction } from '../types';

type Actions = UpdatePeripheralsAction | PeripheralRenameAction;
type Actions = UpdatePeripheralsAction | PeripheralRenameAction | UpdateRuntimeVersionAction | UpdateBatteryAction;

interface PeripheralState {
peripheralList: PeripheralList;
Expand All @@ -19,9 +19,7 @@ const initialPeripheralState: PeripheralState = {

// Taken from runtime_util.c in Runtime repo
const IS_UNSAFE = 0;
const V_BATT = 5;

// TODO: Handle runtimeVersion since no longer sent
export const peripherals = (state: PeripheralState = initialPeripheralState, action: Actions) => {
const nextState = Object.assign({}, state);
const nextPeripherals = nextState.peripheralList;
Expand All @@ -32,12 +30,10 @@ export const peripherals = (state: PeripheralState = initialPeripheralState, act
(action.peripherals ?? []).forEach((peripheral: Peripheral) => {
if (peripheral.name === consts.PeripheralTypes.BatteryBuzzer) {
const batteryParams = peripheral.params;
//since batterysafety is not included in runtimestatus, have to extract the value from params
if (batteryParams[IS_UNSAFE] && batteryParams[IS_UNSAFE].bval) {
nextState.batterySafety = batteryParams[IS_UNSAFE].bval!;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However batterysafety is not included in the runtime status thus extracted manually.

}
if (batteryParams[V_BATT] && batteryParams[V_BATT].fval) {
nextState.batteryLevel = batteryParams[V_BATT].fval!;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got removed because the batteryLevel is already changed from calling changeBatteryLevel in runtime.ts

}
} else {
const key = `${peripheral.type}_${peripheral.uid}`;
keys.push(key);
Expand All @@ -47,6 +43,8 @@ export const peripherals = (state: PeripheralState = initialPeripheralState, act
}
nextPeripherals[key] = { ...peripheral, uid: key };
}
//batteryLevel gets updated from runtimestatus proto
nextState.batteryLevel = state.batteryLevel;
});

Object.keys(nextPeripherals).forEach((uid: string) => {
Expand All @@ -62,6 +60,16 @@ export const peripherals = (state: PeripheralState = initialPeripheralState, act
// nextPeripherals[action.id].name = action.name;
return nextState;
}
case consts.PeripheralActionsTypes.UPDATE_RUNTIME_VERSION:
return {
...state,
runtimeVersion: action.version
}
case consts.PeripheralActionsTypes.UPDATE_BATTERY:
return {
...state,
batteryLevel: action.battery
}
default: {
return state;
}
Expand Down
2 changes: 1 addition & 1 deletion renderer/types/actions/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { AlertActions, AddAsyncAlertAction, RemoveAsyncAlertAction } from './alert-actions';
export { ConsoleActions, ToggleConsoleAction, UpdateConsoleAction, ClearConsoleAction, ToggleScrollAction } from './console-actions';
export { PeripheralActions, UpdatePeripheralsAction, PeripheralRenameAction } from './peripheral-actions';
export { PeripheralActions, UpdatePeripheralsAction, PeripheralRenameAction, UpdateRuntimeVersionAction, UpdateBatteryAction } from './peripheral-actions';
export {
EditorActions,
EditorUpdateAction,
Expand Down
14 changes: 14 additions & 0 deletions renderer/types/actions/peripheral-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,22 @@ export interface PeripheralRenameAction {
name: string;
}

export interface UpdateRuntimeVersionAction {
type: consts.PeripheralActionsTypes.UPDATE_RUNTIME_VERSION;
version: string;
}

export interface UpdateBatteryAction {
type:consts.PeripheralActionsTypes.UPDATE_BATTERY;
battery: number;
}

export interface PeripheralActions {
updatePeripherals: (sensors: Peripheral[]) => UpdatePeripheralsAction;

peripheralRename: (uid: number, newName: string) => PeripheralRenameAction;

updateRuntimeVersion: (version: string) => UpdateRuntimeVersionAction;

updateBattery: (battery: number) => UpdateBatteryAction;
}