From 0004cf988bea56eaf5cca11d7428465435f80549 Mon Sep 17 00:00:00 2001 From: Rick Watson Date: Fri, 29 May 2020 20:58:09 +0100 Subject: [PATCH] prefer enums over named consts in UI for better static analysis --- interface/src/ap/APModes.ts | 10 +++----- interface/src/ap/APSettingsForm.tsx | 10 ++++---- interface/src/ap/types.ts | 8 +++++- interface/src/ntp/NTPStatus.ts | 15 +++++------ interface/src/ntp/types.ts | 7 +++++- interface/src/wifi/WiFiSecurityModes.ts | 23 ++++++----------- interface/src/wifi/WiFiStatus.ts | 33 ++++++++++--------------- interface/src/wifi/types.ts | 22 +++++++++++++++-- 8 files changed, 69 insertions(+), 59 deletions(-) diff --git a/interface/src/ap/APModes.ts b/interface/src/ap/APModes.ts index 87c70fe..95c9ebe 100644 --- a/interface/src/ap/APModes.ts +++ b/interface/src/ap/APModes.ts @@ -1,7 +1,5 @@ -import { APSettings } from "./types"; +import { APSettings, ApProvisionMode } from "./types"; -export const AP_MODE_ALWAYS = 0; -export const AP_MODE_DISCONNECTED = 1; -export const AP_NEVER = 2; - -export const isAPEnabled = ({ provision_mode }: APSettings) => provision_mode === AP_MODE_ALWAYS || provision_mode === AP_MODE_DISCONNECTED; +export const isAPEnabled = ({ provision_mode }: APSettings) => { + return provision_mode === ApProvisionMode.AP_MODE_ALWAYS || provision_mode === ApProvisionMode.AP_MODE_DISCONNECTED; +} diff --git a/interface/src/ap/APSettingsForm.tsx b/interface/src/ap/APSettingsForm.tsx index 10695a3..f007de2 100644 --- a/interface/src/ap/APSettingsForm.tsx +++ b/interface/src/ap/APSettingsForm.tsx @@ -6,8 +6,8 @@ import SaveIcon from '@material-ui/icons/Save'; import {PasswordValidator, RestFormProps, FormActions, FormButton} from '../components'; -import { isAPEnabled, AP_MODE_ALWAYS, AP_MODE_DISCONNECTED, AP_NEVER } from './APModes'; -import { APSettings } from './types'; +import { isAPEnabled } from './APModes'; +import { APSettings, ApProvisionMode } from './types'; type APSettingsFormProps = RestFormProps; @@ -24,9 +24,9 @@ class APSettingsForm extends React.Component { variant="outlined" onChange={handleValueChange('provision_mode')} margin="normal"> - Always - When WiFi Disconnected - Never + Always + When WiFi Disconnected + Never { isAPEnabled(data) && diff --git a/interface/src/ap/types.ts b/interface/src/ap/types.ts index 649f139..bcf4620 100644 --- a/interface/src/ap/types.ts +++ b/interface/src/ap/types.ts @@ -1,3 +1,9 @@ +export enum ApProvisionMode { + AP_MODE_ALWAYS = 0, + AP_MODE_DISCONNECTED = 1, + AP_NEVER = 2 +} + export interface APStatus { active: boolean; ip_address: string; @@ -6,7 +12,7 @@ export interface APStatus { } export interface APSettings { - provision_mode: number; + provision_mode: ApProvisionMode; ssid: string; password: string; } diff --git a/interface/src/ntp/NTPStatus.ts b/interface/src/ntp/NTPStatus.ts index 21601f9..744b56d 100644 --- a/interface/src/ntp/NTPStatus.ts +++ b/interface/src/ntp/NTPStatus.ts @@ -1,16 +1,13 @@ import { Theme } from "@material-ui/core"; -import { NTPStatus } from "./types"; +import { NTPStatus, NTPSyncStatus } from "./types"; -export const NTP_INACTIVE = 0; -export const NTP_ACTIVE = 1; - -export const isNtpActive = ({ status }: NTPStatus) => status === NTP_ACTIVE; +export const isNtpActive = ({ status }: NTPStatus) => status === NTPSyncStatus.NTP_ACTIVE; export const ntpStatusHighlight = ({ status }: NTPStatus, theme: Theme) => { switch (status) { - case NTP_INACTIVE: + case NTPSyncStatus.NTP_INACTIVE: return theme.palette.info.main; - case NTP_ACTIVE: + case NTPSyncStatus.NTP_ACTIVE: return theme.palette.success.main; default: return theme.palette.error.main; @@ -19,9 +16,9 @@ export const ntpStatusHighlight = ({ status }: NTPStatus, theme: Theme) => { export const ntpStatus = ({ status }: NTPStatus) => { switch (status) { - case NTP_INACTIVE: + case NTPSyncStatus.NTP_INACTIVE: return "Inactive"; - case NTP_ACTIVE: + case NTPSyncStatus.NTP_ACTIVE: return "Active"; default: return "Unknown"; diff --git a/interface/src/ntp/types.ts b/interface/src/ntp/types.ts index 2b36802..6ee4610 100644 --- a/interface/src/ntp/types.ts +++ b/interface/src/ntp/types.ts @@ -1,5 +1,10 @@ +export enum NTPSyncStatus { + NTP_INACTIVE = 0, + NTP_ACTIVE = 1 +} + export interface NTPStatus { - status: number; + status: NTPSyncStatus; time_utc: string; time_local: string; server: string; diff --git a/interface/src/wifi/WiFiSecurityModes.ts b/interface/src/wifi/WiFiSecurityModes.ts index 06c7bdb..b65c69a 100644 --- a/interface/src/wifi/WiFiSecurityModes.ts +++ b/interface/src/wifi/WiFiSecurityModes.ts @@ -1,26 +1,19 @@ -import { WiFiNetwork } from "./types"; +import { WiFiNetwork, WiFiEncryptionType } from "./types"; -export const WIFI_AUTH_OPEN = 0; -export const WIFI_AUTH_WEP = 1; -export const WIFI_AUTH_WEP_PSK = 2; -export const WIFI_AUTH_WEP2_PSK = 3; -export const WIFI_AUTH_WPA_WPA2_PSK = 4; -export const WIFI_AUTH_WPA2_ENTERPRISE = 5; - -export const isNetworkOpen = ({ encryption_type }: WiFiNetwork) => encryption_type === WIFI_AUTH_OPEN; +export const isNetworkOpen = ({ encryption_type }: WiFiNetwork) => encryption_type === WiFiEncryptionType.WIFI_AUTH_OPEN; export const networkSecurityMode = ({ encryption_type }: WiFiNetwork) => { switch (encryption_type) { - case WIFI_AUTH_WEP: - case WIFI_AUTH_WEP_PSK: + case WiFiEncryptionType.WIFI_AUTH_WEP: + case WiFiEncryptionType.WIFI_AUTH_WEP_PSK: return "WEP"; - case WIFI_AUTH_WEP2_PSK: + case WiFiEncryptionType.WIFI_AUTH_WEP2_PSK: return "WEP2"; - case WIFI_AUTH_WPA_WPA2_PSK: + case WiFiEncryptionType.WIFI_AUTH_WPA_WPA2_PSK: return "WPA/WEP2"; - case WIFI_AUTH_WPA2_ENTERPRISE: + case WiFiEncryptionType.WIFI_AUTH_WPA2_ENTERPRISE: return "WEP2 Enterprise"; - case WIFI_AUTH_OPEN: + case WiFiEncryptionType.WIFI_AUTH_OPEN: return "None"; default: return "Unknown"; diff --git a/interface/src/wifi/WiFiStatus.ts b/interface/src/wifi/WiFiStatus.ts index bbab81f..c4206aa 100644 --- a/interface/src/wifi/WiFiStatus.ts +++ b/interface/src/wifi/WiFiStatus.ts @@ -1,24 +1,17 @@ import { Theme } from '@material-ui/core'; -import { WiFiStatus } from './types'; +import { WiFiStatus, WiFiConnectionStatus } from './types'; -export const WIFI_STATUS_IDLE = 0; -export const WIFI_STATUS_NO_SSID_AVAIL = 1; -export const WIFI_STATUS_CONNECTED = 3; -export const WIFI_STATUS_CONNECT_FAILED = 4; -export const WIFI_STATUS_CONNECTION_LOST = 5; -export const WIFI_STATUS_DISCONNECTED = 6; - -export const isConnected = ({ status }: WiFiStatus) => status === WIFI_STATUS_CONNECTED; +export const isConnected = ({ status }: WiFiStatus) => status === WiFiConnectionStatus.WIFI_STATUS_CONNECTED; export const wifiStatusHighlight = ({ status }: WiFiStatus, theme: Theme) => { switch (status) { - case WIFI_STATUS_IDLE: - case WIFI_STATUS_DISCONNECTED: + case WiFiConnectionStatus.WIFI_STATUS_IDLE: + case WiFiConnectionStatus.WIFI_STATUS_DISCONNECTED: return theme.palette.info.main; - case WIFI_STATUS_CONNECTED: + case WiFiConnectionStatus.WIFI_STATUS_CONNECTED: return theme.palette.success.main; - case WIFI_STATUS_CONNECT_FAILED: - case WIFI_STATUS_CONNECTION_LOST: + case WiFiConnectionStatus.WIFI_STATUS_CONNECT_FAILED: + case WiFiConnectionStatus.WIFI_STATUS_CONNECTION_LOST: return theme.palette.error.main; default: return theme.palette.warning.main; @@ -27,17 +20,17 @@ export const wifiStatusHighlight = ({ status }: WiFiStatus, theme: Theme) => { export const wifiStatus = ({ status }: WiFiStatus) => { switch (status) { - case WIFI_STATUS_IDLE: + case WiFiConnectionStatus.WIFI_STATUS_IDLE: return "Idle"; - case WIFI_STATUS_NO_SSID_AVAIL: + case WiFiConnectionStatus.WIFI_STATUS_NO_SSID_AVAIL: return "No SSID Available"; - case WIFI_STATUS_CONNECTED: + case WiFiConnectionStatus.WIFI_STATUS_CONNECTED: return "Connected"; - case WIFI_STATUS_CONNECT_FAILED: + case WiFiConnectionStatus.WIFI_STATUS_CONNECT_FAILED: return "Connection Failed"; - case WIFI_STATUS_CONNECTION_LOST: + case WiFiConnectionStatus.WIFI_STATUS_CONNECTION_LOST: return "Connection Lost"; - case WIFI_STATUS_DISCONNECTED: + case WiFiConnectionStatus.WIFI_STATUS_DISCONNECTED: return "Disconnected"; default: return "Unknown"; diff --git a/interface/src/wifi/types.ts b/interface/src/wifi/types.ts index 2e5ec9f..5152015 100644 --- a/interface/src/wifi/types.ts +++ b/interface/src/wifi/types.ts @@ -1,5 +1,23 @@ +export enum WiFiConnectionStatus { + WIFI_STATUS_IDLE = 0, + WIFI_STATUS_NO_SSID_AVAIL = 1, + WIFI_STATUS_CONNECTED = 3, + WIFI_STATUS_CONNECT_FAILED = 4, + WIFI_STATUS_CONNECTION_LOST = 5, + WIFI_STATUS_DISCONNECTED = 6 +} + +export enum WiFiEncryptionType { + WIFI_AUTH_OPEN = 0, + WIFI_AUTH_WEP = 1, + WIFI_AUTH_WEP_PSK = 2, + WIFI_AUTH_WEP2_PSK = 3, + WIFI_AUTH_WPA_WPA2_PSK = 4, + WIFI_AUTH_WPA2_ENTERPRISE = 5 +} + export interface WiFiStatus { - status: number; + status: WiFiConnectionStatus; local_ip: string; mac_address: string; rssi: number; @@ -33,5 +51,5 @@ export interface WiFiNetwork { ssid: string; bssid: string; channel: number; - encryption_type: number; + encryption_type: WiFiEncryptionType; }