mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 08:19:52 +03:00
adding esp8266-react's latest NTP library
This commit is contained in:
@@ -14,7 +14,7 @@ type APSettingsFormProps = RestFormProps<APSettings>;
|
||||
|
||||
class APSettingsForm extends React.Component<APSettingsFormProps> {
|
||||
|
||||
componentWillMount() {
|
||||
componentDidMount() {
|
||||
ValidatorForm.addValidationRule('isIP', isIP);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { Redirect, Route, RouteProps, RouteComponentProps } from "react-router-d
|
||||
import { withSnackbar, WithSnackbarProps } from 'notistack';
|
||||
|
||||
import * as Authentication from './Authentication';
|
||||
import { withAuthenticationContext, AuthenticationContextProps, AuthenticatedContext } from './AuthenticationContext';
|
||||
import { withAuthenticationContext, AuthenticationContextProps, AuthenticatedContext, AuthenticatedContextValue } from './AuthenticationContext';
|
||||
|
||||
type ChildComponent = React.ComponentType<RouteComponentProps<any>> | React.ComponentType<any>;
|
||||
|
||||
@@ -21,7 +21,7 @@ export class AuthenticatedRoute extends React.Component<AuthenticatedRouteProps>
|
||||
const renderComponent: RenderComponent = (props) => {
|
||||
if (authenticationContext.me) {
|
||||
return (
|
||||
<AuthenticatedContext.Provider value={authenticationContext as AuthenticatedContext}>
|
||||
<AuthenticatedContext.Provider value={authenticationContext as AuthenticatedContextValue}>
|
||||
<Component {...props} />
|
||||
</AuthenticatedContext.Provider>
|
||||
);
|
||||
|
||||
@@ -6,20 +6,20 @@ export interface Me {
|
||||
version: string; // proddy added
|
||||
}
|
||||
|
||||
export interface AuthenticationContext {
|
||||
export interface AuthenticationContextValue {
|
||||
refresh: () => void;
|
||||
signIn: (accessToken: string) => void;
|
||||
signOut: () => void;
|
||||
me?: Me;
|
||||
}
|
||||
|
||||
const AuthenticationContextDefaultValue = {} as AuthenticationContext
|
||||
const AuthenticationContextDefaultValue = {} as AuthenticationContextValue
|
||||
export const AuthenticationContext = React.createContext(
|
||||
AuthenticationContextDefaultValue
|
||||
);
|
||||
|
||||
export interface AuthenticationContextProps {
|
||||
authenticationContext: AuthenticationContext;
|
||||
authenticationContext: AuthenticationContextValue;
|
||||
}
|
||||
|
||||
export function withAuthenticationContext<T extends AuthenticationContextProps>(Component: React.ComponentType<T>) {
|
||||
@@ -34,17 +34,17 @@ export function withAuthenticationContext<T extends AuthenticationContextProps>(
|
||||
};
|
||||
}
|
||||
|
||||
export interface AuthenticatedContext extends AuthenticationContext {
|
||||
export interface AuthenticatedContextValue extends AuthenticationContextValue {
|
||||
me: Me;
|
||||
}
|
||||
|
||||
const AuthenticatedContextDefaultValue = {} as AuthenticatedContext
|
||||
const AuthenticatedContextDefaultValue = {} as AuthenticatedContextValue
|
||||
export const AuthenticatedContext = React.createContext(
|
||||
AuthenticatedContextDefaultValue
|
||||
);
|
||||
|
||||
export interface AuthenticatedContextProps {
|
||||
authenticatedContext: AuthenticatedContext;
|
||||
authenticatedContext: AuthenticatedContextValue;
|
||||
}
|
||||
|
||||
export function withAuthenticatedContext<T extends AuthenticatedContextProps>(Component: React.ComponentType<T>) {
|
||||
@@ -57,4 +57,4 @@ export function withAuthenticatedContext<T extends AuthenticatedContextProps>(Co
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -5,14 +5,14 @@ import jwtDecode from 'jwt-decode';
|
||||
import history from '../history'
|
||||
import { VERIFY_AUTHORIZATION_ENDPOINT } from '../api';
|
||||
import { ACCESS_TOKEN, authorizedFetch, getStorage } from './Authentication';
|
||||
import { AuthenticationContext, Me } from './AuthenticationContext';
|
||||
import { AuthenticationContext, AuthenticationContextValue, Me } from './AuthenticationContext';
|
||||
import FullScreenLoading from '../components/FullScreenLoading';
|
||||
import { withFeatures, WithFeaturesProps } from '../features/FeaturesContext';
|
||||
|
||||
export const decodeMeJWT = (accessToken: string): Me => jwtDecode(accessToken) as Me;
|
||||
|
||||
interface AuthenticationWrapperState {
|
||||
context: AuthenticationContext;
|
||||
context: AuthenticationContextValue;
|
||||
initialized: boolean;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
import React from 'react';
|
||||
import { Features } from './types';
|
||||
|
||||
export interface ApplicationContext {
|
||||
features: Features;
|
||||
}
|
||||
|
||||
const ApplicationContextDefaultValue = {} as ApplicationContext
|
||||
export const ApplicationContext = React.createContext(
|
||||
ApplicationContextDefaultValue
|
||||
);
|
||||
|
||||
export function withAuthenticatedContexApplicationContext<T extends ApplicationContext>(Component: React.ComponentType<T>) {
|
||||
return class extends React.Component<Omit<T, keyof ApplicationContext>> {
|
||||
render() {
|
||||
return (
|
||||
<ApplicationContext.Consumer>
|
||||
{authenticatedContext => <Component {...this.props as T} features={authenticatedContext} />}
|
||||
</ApplicationContext.Consumer>
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
import React from 'react';
|
||||
import { Features } from './types';
|
||||
|
||||
export interface FeaturesContext {
|
||||
export interface FeaturesContextValue {
|
||||
features: Features;
|
||||
}
|
||||
|
||||
const FeaturesContextDefaultValue = {} as FeaturesContext
|
||||
const FeaturesContextDefaultValue = {} as FeaturesContextValue
|
||||
export const FeaturesContext = React.createContext(
|
||||
FeaturesContextDefaultValue
|
||||
);
|
||||
|
||||
@@ -9,12 +9,13 @@ import { MenuAppBar } from '../components';
|
||||
import NetworkStatusController from './NetworkStatusController';
|
||||
import NetworkSettingsController from './NetworkSettingsController';
|
||||
import WiFiNetworkScanner from './WiFiNetworkScanner';
|
||||
import { NetworkConnectionContext } from './NetworkConnectionContext';
|
||||
import { NetworkConnectionContext, NetworkConnectionContextValue } from './NetworkConnectionContext';
|
||||
|
||||
import { WiFiNetwork } from './types';
|
||||
|
||||
type NetworkConnectionProps = AuthenticatedContextProps & RouteComponentProps;
|
||||
|
||||
class NetworkConnection extends Component<NetworkConnectionProps, NetworkConnectionContext> {
|
||||
class NetworkConnection extends Component<NetworkConnectionProps, NetworkConnectionContextValue> {
|
||||
|
||||
constructor(props: NetworkConnectionProps) {
|
||||
super(props);
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import React from 'react';
|
||||
import { WiFiNetwork } from './types';
|
||||
|
||||
export interface NetworkConnectionContext {
|
||||
export interface NetworkConnectionContextValue {
|
||||
selectedNetwork?: WiFiNetwork;
|
||||
selectNetwork: (network: WiFiNetwork) => void;
|
||||
deselectNetwork: () => void;
|
||||
}
|
||||
|
||||
const NetworkConnectionContextDefaultValue = {} as NetworkConnectionContext
|
||||
const NetworkConnectionContextDefaultValue = {} as NetworkConnectionContextValue
|
||||
export const NetworkConnectionContext = React.createContext(
|
||||
NetworkConnectionContextDefaultValue
|
||||
);
|
||||
|
||||
@@ -14,7 +14,7 @@ import MenuItem from '@material-ui/core/MenuItem';
|
||||
import { RestFormProps, PasswordValidator, BlockFormControlLabel, FormActions, FormButton } from '../components';
|
||||
import { isIP, isHostname, optional } from '../validators';
|
||||
|
||||
import { NetworkConnectionContext } from './NetworkConnectionContext';
|
||||
import { NetworkConnectionContext, NetworkConnectionContextValue } from './NetworkConnectionContext';
|
||||
import { isNetworkOpen, networkSecurityMode } from './WiFiSecurityModes';
|
||||
import { NetworkSettings } from './types';
|
||||
|
||||
@@ -25,7 +25,7 @@ class NetworkSettingsForm extends React.Component<NetworkStatusFormProps> {
|
||||
static contextType = NetworkConnectionContext;
|
||||
context!: React.ContextType<typeof NetworkConnectionContext>;
|
||||
|
||||
constructor(props: NetworkStatusFormProps, context: NetworkConnectionContext) {
|
||||
constructor(props: NetworkStatusFormProps, context: NetworkConnectionContextValue) {
|
||||
super(props);
|
||||
|
||||
const { selectedNetwork } = context;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import React, { Component, Fragment } from 'react';
|
||||
import moment from 'moment';
|
||||
|
||||
import { WithTheme, withTheme } from '@material-ui/core/styles';
|
||||
import { Avatar, Divider, List, ListItem, ListItemAvatar, ListItemText, Button } from '@material-ui/core';
|
||||
@@ -14,7 +13,7 @@ import RefreshIcon from '@material-ui/icons/Refresh';
|
||||
|
||||
import { RestFormProps, FormButton, HighlightAvatar } from '../components';
|
||||
import { isNtpActive, ntpStatusHighlight, ntpStatus } from './NTPStatus';
|
||||
import { formatIsoDateTime, formatLocalDateTime } from './TimeFormat';
|
||||
import { formatDuration, formatDateTime, formatLocalDateTimeNow, formatLocalDateTime } from './TimeFormat';
|
||||
import { NTPStatus, Time } from './types';
|
||||
import { redirectingAuthorizedFetch, withAuthenticatedContext, AuthenticatedContextProps } from '../authentication';
|
||||
import { TIME_ENDPOINT } from '../api';
|
||||
@@ -43,30 +42,21 @@ class NTPStatusForm extends Component<NTPStatusFormProps, NTPStatusFormState> {
|
||||
}
|
||||
|
||||
openSetTime = () => {
|
||||
this.setState({ localTime: formatLocalDateTime(moment()), settingTime: true, });
|
||||
this.setState({ localTime: formatLocalDateTimeNow(), settingTime: true, });
|
||||
}
|
||||
|
||||
closeSetTime = () => {
|
||||
this.setState({ settingTime: false });
|
||||
}
|
||||
|
||||
createAdjustedTime = (): Time => {
|
||||
const currentLocalTime = moment(this.props.data.time_local);
|
||||
const newLocalTime = moment(this.state.localTime);
|
||||
newLocalTime.subtract(currentLocalTime.utcOffset())
|
||||
newLocalTime.milliseconds(0);
|
||||
newLocalTime.utc();
|
||||
return {
|
||||
time_utc: newLocalTime.format()
|
||||
}
|
||||
}
|
||||
createTime = (): Time => ({ local_time: formatLocalDateTime(this.state.localTime) });
|
||||
|
||||
configureTime = () => {
|
||||
this.setState({ processing: true });
|
||||
redirectingAuthorizedFetch(TIME_ENDPOINT,
|
||||
{
|
||||
method: 'POST',
|
||||
body: JSON.stringify(this.createAdjustedTime()),
|
||||
body: JSON.stringify(this.createTime()),
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
@@ -153,7 +143,7 @@ class NTPStatusForm extends Component<NTPStatusFormProps, NTPStatusFormState> {
|
||||
<AccessTimeIcon />
|
||||
</Avatar>
|
||||
</ListItemAvatar>
|
||||
<ListItemText primary="Local Time" secondary={formatIsoDateTime(data.time_local)} />
|
||||
<ListItemText primary="Local Time" secondary={formatDateTime(data.local_time)} />
|
||||
</ListItem>
|
||||
<Divider variant="inset" component="li" />
|
||||
<ListItem>
|
||||
@@ -162,7 +152,7 @@ class NTPStatusForm extends Component<NTPStatusFormProps, NTPStatusFormState> {
|
||||
<SwapVerticalCircleIcon />
|
||||
</Avatar>
|
||||
</ListItemAvatar>
|
||||
<ListItemText primary="UTC Time" secondary={formatIsoDateTime(data.time_utc)} />
|
||||
<ListItemText primary="UTC Time" secondary={formatDateTime(data.utc_time)} />
|
||||
</ListItem>
|
||||
<Divider variant="inset" component="li" />
|
||||
<ListItem>
|
||||
@@ -171,7 +161,7 @@ class NTPStatusForm extends Component<NTPStatusFormProps, NTPStatusFormState> {
|
||||
<AvTimerIcon />
|
||||
</Avatar>
|
||||
</ListItemAvatar>
|
||||
<ListItemText primary="Uptime" secondary={moment.duration(data.uptime, 'seconds').humanize()} />
|
||||
<ListItemText primary="Uptime" secondary={formatDuration(data.uptime)} />
|
||||
</ListItem>
|
||||
<Divider variant="inset" component="li" />
|
||||
</List>
|
||||
@@ -195,4 +185,4 @@ class NTPStatusForm extends Component<NTPStatusFormProps, NTPStatusFormState> {
|
||||
}
|
||||
}
|
||||
|
||||
export default withAuthenticatedContext(withTheme(NTPStatusForm));
|
||||
export default withAuthenticatedContext(withTheme(NTPStatusForm));
|
||||
@@ -1,5 +1,45 @@
|
||||
import moment, { Moment } from 'moment';
|
||||
import parseMilliseconds from 'parse-ms';
|
||||
|
||||
export const formatIsoDateTime = (isoDateString: string) => moment.parseZone(isoDateString).format('ll @ HH:mm:ss');
|
||||
const LOCALE_FORMAT = new Intl.DateTimeFormat('default', {
|
||||
day: 'numeric',
|
||||
month: 'short',
|
||||
year: 'numeric',
|
||||
hour: 'numeric',
|
||||
minute: 'numeric',
|
||||
second: 'numeric',
|
||||
hour12: false
|
||||
});
|
||||
|
||||
export const formatLocalDateTime = (moment: Moment) => moment.format('YYYY-MM-DDTHH:mm');
|
||||
export const formatDateTime = (dateTime: string) => {
|
||||
return LOCALE_FORMAT.format(new Date(dateTime.substr(0, 19)));
|
||||
}
|
||||
|
||||
export const formatLocalDateTimeNow = () => {
|
||||
return formatIsoDateTime(new Date()).substr(0, 19);
|
||||
}
|
||||
|
||||
export const formatLocalDateTime = (dateTime: string) => {
|
||||
return formatIsoDateTime(new Date(dateTime)).substr(0, 19);
|
||||
}
|
||||
|
||||
export const formatIsoDateTime = (date: Date) => {
|
||||
return new Date(date.getTime() - date.getTimezoneOffset() * 60000).toISOString().slice(0, -1);
|
||||
}
|
||||
|
||||
export const formatDuration = (duration: number) => {
|
||||
const { days, hours, minutes, seconds } = parseMilliseconds(duration * 1000);
|
||||
var formatted = '';
|
||||
if (days) {
|
||||
formatted += days + ' days ';
|
||||
}
|
||||
if (formatted || hours) {
|
||||
formatted += hours + ' hours ';
|
||||
}
|
||||
if (formatted || minutes) {
|
||||
formatted += minutes + ' minutes ';
|
||||
}
|
||||
if (formatted || seconds) {
|
||||
formatted += seconds + ' seconds';
|
||||
}
|
||||
return formatted;
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
|
||||
export enum NTPSyncStatus {
|
||||
NTP_INACTIVE = 0,
|
||||
NTP_ACTIVE = 1
|
||||
@@ -5,8 +6,8 @@ export enum NTPSyncStatus {
|
||||
|
||||
export interface NTPStatus {
|
||||
status: NTPSyncStatus;
|
||||
time_utc: string;
|
||||
time_local: string;
|
||||
utc_time: string;
|
||||
local_time: string;
|
||||
server: string;
|
||||
uptime: number;
|
||||
}
|
||||
@@ -19,5 +20,5 @@ export interface NTPSettings {
|
||||
}
|
||||
|
||||
export interface Time {
|
||||
time_utc: string;
|
||||
}
|
||||
local_time: string;
|
||||
}
|
||||
@@ -1 +1,3 @@
|
||||
export default (validator: (value: any) => boolean) => (value: any) => !value || validator(value);
|
||||
const OPTIONAL = (validator: (value: any) => boolean) => (value: any) => !value || validator(value);
|
||||
|
||||
export default OPTIONAL;
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
export default (validator1: (value: any) => boolean, validator2: (value: any) => boolean) => {
|
||||
const OR = (validator1: (value: any) => boolean, validator2: (value: any) => boolean) => {
|
||||
return (value: any) => validator1(value) || validator2(value);
|
||||
}
|
||||
};
|
||||
|
||||
export default OR;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user