Merge pull request #136 from rjwats/ui-use-enums

prefer enums over named consts in UI for better static analysis
This commit is contained in:
rjwats 2020-05-29 21:05:29 +01:00 committed by GitHub
commit ddbb84454f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 69 additions and 59 deletions

View File

@ -1,7 +1,5 @@
import { APSettings } from "./types"; import { APSettings, ApProvisionMode } from "./types";
export const AP_MODE_ALWAYS = 0; export const isAPEnabled = ({ provision_mode }: APSettings) => {
export const AP_MODE_DISCONNECTED = 1; return provision_mode === ApProvisionMode.AP_MODE_ALWAYS || provision_mode === ApProvisionMode.AP_MODE_DISCONNECTED;
export const AP_NEVER = 2; }
export const isAPEnabled = ({ provision_mode }: APSettings) => provision_mode === AP_MODE_ALWAYS || provision_mode === AP_MODE_DISCONNECTED;

View File

@ -6,8 +6,8 @@ import SaveIcon from '@material-ui/icons/Save';
import {PasswordValidator, RestFormProps, FormActions, FormButton} from '../components'; import {PasswordValidator, RestFormProps, FormActions, FormButton} from '../components';
import { isAPEnabled, AP_MODE_ALWAYS, AP_MODE_DISCONNECTED, AP_NEVER } from './APModes'; import { isAPEnabled } from './APModes';
import { APSettings } from './types'; import { APSettings, ApProvisionMode } from './types';
type APSettingsFormProps = RestFormProps<APSettings>; type APSettingsFormProps = RestFormProps<APSettings>;
@ -24,9 +24,9 @@ class APSettingsForm extends React.Component<APSettingsFormProps> {
variant="outlined" variant="outlined"
onChange={handleValueChange('provision_mode')} onChange={handleValueChange('provision_mode')}
margin="normal"> margin="normal">
<MenuItem value={AP_MODE_ALWAYS}>Always</MenuItem> <MenuItem value={ApProvisionMode.AP_MODE_ALWAYS}>Always</MenuItem>
<MenuItem value={AP_MODE_DISCONNECTED}>When WiFi Disconnected</MenuItem> <MenuItem value={ApProvisionMode.AP_MODE_DISCONNECTED}>When WiFi Disconnected</MenuItem>
<MenuItem value={AP_NEVER}>Never</MenuItem> <MenuItem value={ApProvisionMode.AP_NEVER}>Never</MenuItem>
</SelectValidator> </SelectValidator>
{ {
isAPEnabled(data) && isAPEnabled(data) &&

View File

@ -1,3 +1,9 @@
export enum ApProvisionMode {
AP_MODE_ALWAYS = 0,
AP_MODE_DISCONNECTED = 1,
AP_NEVER = 2
}
export interface APStatus { export interface APStatus {
active: boolean; active: boolean;
ip_address: string; ip_address: string;
@ -6,7 +12,7 @@ export interface APStatus {
} }
export interface APSettings { export interface APSettings {
provision_mode: number; provision_mode: ApProvisionMode;
ssid: string; ssid: string;
password: string; password: string;
} }

View File

@ -1,16 +1,13 @@
import { Theme } from "@material-ui/core"; import { Theme } from "@material-ui/core";
import { NTPStatus } from "./types"; import { NTPStatus, NTPSyncStatus } from "./types";
export const NTP_INACTIVE = 0; export const isNtpActive = ({ status }: NTPStatus) => status === NTPSyncStatus.NTP_ACTIVE;
export const NTP_ACTIVE = 1;
export const isNtpActive = ({ status }: NTPStatus) => status === NTP_ACTIVE;
export const ntpStatusHighlight = ({ status }: NTPStatus, theme: Theme) => { export const ntpStatusHighlight = ({ status }: NTPStatus, theme: Theme) => {
switch (status) { switch (status) {
case NTP_INACTIVE: case NTPSyncStatus.NTP_INACTIVE:
return theme.palette.info.main; return theme.palette.info.main;
case NTP_ACTIVE: case NTPSyncStatus.NTP_ACTIVE:
return theme.palette.success.main; return theme.palette.success.main;
default: default:
return theme.palette.error.main; return theme.palette.error.main;
@ -19,9 +16,9 @@ export const ntpStatusHighlight = ({ status }: NTPStatus, theme: Theme) => {
export const ntpStatus = ({ status }: NTPStatus) => { export const ntpStatus = ({ status }: NTPStatus) => {
switch (status) { switch (status) {
case NTP_INACTIVE: case NTPSyncStatus.NTP_INACTIVE:
return "Inactive"; return "Inactive";
case NTP_ACTIVE: case NTPSyncStatus.NTP_ACTIVE:
return "Active"; return "Active";
default: default:
return "Unknown"; return "Unknown";

View File

@ -1,5 +1,10 @@
export enum NTPSyncStatus {
NTP_INACTIVE = 0,
NTP_ACTIVE = 1
}
export interface NTPStatus { export interface NTPStatus {
status: number; status: NTPSyncStatus;
time_utc: string; time_utc: string;
time_local: string; time_local: string;
server: string; server: string;

View File

@ -1,26 +1,19 @@
import { WiFiNetwork } from "./types"; import { WiFiNetwork, WiFiEncryptionType } from "./types";
export const WIFI_AUTH_OPEN = 0; export const isNetworkOpen = ({ encryption_type }: WiFiNetwork) => encryption_type === WiFiEncryptionType.WIFI_AUTH_OPEN;
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 networkSecurityMode = ({ encryption_type }: WiFiNetwork) => { export const networkSecurityMode = ({ encryption_type }: WiFiNetwork) => {
switch (encryption_type) { switch (encryption_type) {
case WIFI_AUTH_WEP: case WiFiEncryptionType.WIFI_AUTH_WEP:
case WIFI_AUTH_WEP_PSK: case WiFiEncryptionType.WIFI_AUTH_WEP_PSK:
return "WEP"; return "WEP";
case WIFI_AUTH_WEP2_PSK: case WiFiEncryptionType.WIFI_AUTH_WEP2_PSK:
return "WEP2"; return "WEP2";
case WIFI_AUTH_WPA_WPA2_PSK: case WiFiEncryptionType.WIFI_AUTH_WPA_WPA2_PSK:
return "WPA/WEP2"; return "WPA/WEP2";
case WIFI_AUTH_WPA2_ENTERPRISE: case WiFiEncryptionType.WIFI_AUTH_WPA2_ENTERPRISE:
return "WEP2 Enterprise"; return "WEP2 Enterprise";
case WIFI_AUTH_OPEN: case WiFiEncryptionType.WIFI_AUTH_OPEN:
return "None"; return "None";
default: default:
return "Unknown"; return "Unknown";

View File

@ -1,24 +1,17 @@
import { Theme } from '@material-ui/core'; import { Theme } from '@material-ui/core';
import { WiFiStatus } from './types'; import { WiFiStatus, WiFiConnectionStatus } from './types';
export const WIFI_STATUS_IDLE = 0; export const isConnected = ({ status }: WiFiStatus) => status === WiFiConnectionStatus.WIFI_STATUS_CONNECTED;
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 wifiStatusHighlight = ({ status }: WiFiStatus, theme: Theme) => { export const wifiStatusHighlight = ({ status }: WiFiStatus, theme: Theme) => {
switch (status) { switch (status) {
case WIFI_STATUS_IDLE: case WiFiConnectionStatus.WIFI_STATUS_IDLE:
case WIFI_STATUS_DISCONNECTED: case WiFiConnectionStatus.WIFI_STATUS_DISCONNECTED:
return theme.palette.info.main; return theme.palette.info.main;
case WIFI_STATUS_CONNECTED: case WiFiConnectionStatus.WIFI_STATUS_CONNECTED:
return theme.palette.success.main; return theme.palette.success.main;
case WIFI_STATUS_CONNECT_FAILED: case WiFiConnectionStatus.WIFI_STATUS_CONNECT_FAILED:
case WIFI_STATUS_CONNECTION_LOST: case WiFiConnectionStatus.WIFI_STATUS_CONNECTION_LOST:
return theme.palette.error.main; return theme.palette.error.main;
default: default:
return theme.palette.warning.main; return theme.palette.warning.main;
@ -27,17 +20,17 @@ export const wifiStatusHighlight = ({ status }: WiFiStatus, theme: Theme) => {
export const wifiStatus = ({ status }: WiFiStatus) => { export const wifiStatus = ({ status }: WiFiStatus) => {
switch (status) { switch (status) {
case WIFI_STATUS_IDLE: case WiFiConnectionStatus.WIFI_STATUS_IDLE:
return "Idle"; return "Idle";
case WIFI_STATUS_NO_SSID_AVAIL: case WiFiConnectionStatus.WIFI_STATUS_NO_SSID_AVAIL:
return "No SSID Available"; return "No SSID Available";
case WIFI_STATUS_CONNECTED: case WiFiConnectionStatus.WIFI_STATUS_CONNECTED:
return "Connected"; return "Connected";
case WIFI_STATUS_CONNECT_FAILED: case WiFiConnectionStatus.WIFI_STATUS_CONNECT_FAILED:
return "Connection Failed"; return "Connection Failed";
case WIFI_STATUS_CONNECTION_LOST: case WiFiConnectionStatus.WIFI_STATUS_CONNECTION_LOST:
return "Connection Lost"; return "Connection Lost";
case WIFI_STATUS_DISCONNECTED: case WiFiConnectionStatus.WIFI_STATUS_DISCONNECTED:
return "Disconnected"; return "Disconnected";
default: default:
return "Unknown"; return "Unknown";

View File

@ -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 { export interface WiFiStatus {
status: number; status: WiFiConnectionStatus;
local_ip: string; local_ip: string;
mac_address: string; mac_address: string;
rssi: number; rssi: number;
@ -33,5 +51,5 @@ export interface WiFiNetwork {
ssid: string; ssid: string;
bssid: string; bssid: string;
channel: number; channel: number;
encryption_type: number; encryption_type: WiFiEncryptionType;
} }