Do not use internal and actual values anymore (Fixes #42)

This commit is contained in:
uwap 2018-06-28 21:25:06 +02:00
parent b40f4a774e
commit c0117fa7d6
13 changed files with 421 additions and 393 deletions

View file

@ -2,24 +2,20 @@
import type { Color } from "config/colors";
import type { Icon } from "config/icon";
export type TopicType = (msg: Buffer) => any;
export type TopicType = (msg: Buffer) => string;
export type StateCommand = {
name: string,
type: TopicType
}
export type Topic = {
state: string,
command: string,
defaultValue: Actual,
values: Map<Internal, Actual>,
type?: TopicType
state?: StateCommand,
command?: StateCommand,
defaultValue: string
};
export type Topics = Map<string, Topic>;
export type TopicDependentOption<T> = (
internal: Internal, actual: Actual, state: State
) => T;
export type StateDependentOption<T> = (
internals: Map<string, Internal>, actuals: Map<string, Actual>, state: State
) => T;
export interface UIControl {
+type: string,
+text: string,
@ -27,7 +23,7 @@ export interface UIControl {
}
export interface Enableable {
enableCondition?: TopicDependentOption<boolean>
enableCondition?: (s: State) => boolean
}
export type UIToggle = $ReadOnly<{|
@ -35,10 +31,10 @@ export type UIToggle = $ReadOnly<{|
text: string,
topic: string,
icon?: Icon,
enableCondition?: TopicDependentOption<boolean>,
on?: Actual,
off?: Actual,
toggled?: TopicDependentOption<boolean>
enableCondition?: (s: State) => boolean,
on?: string,
off?: string,
toggled?: (v: string, s: State) => boolean
|}>;
export type UIDropDown = $ReadOnly<{|
@ -46,8 +42,8 @@ export type UIDropDown = $ReadOnly<{|
text: string,
topic: string,
icon?: Icon,
enableCondition?: TopicDependentOption<boolean>,
options: Map<string, any>,
enableCondition?: (s: State) => boolean,
options: Map<string, string>,
renderValue?: (value: string) => string
|}>;
@ -56,7 +52,7 @@ export type UISlider = $ReadOnly<{|
text: string,
topic: string,
icon?: Icon,
enableCondition?: TopicDependentOption<boolean>,
enableCondition?: (s: State) => boolean,
min?: number,
max?: number,
step?: number,
@ -72,7 +68,7 @@ export type UILink = $ReadOnly<{|
type: "link",
text: string,
link: string,
enableCondition?: StateDependentOption<boolean>,
enableCondition?: (s: State) => boolean,
// TODO: check if both the following options are implemented
icon?: Icon
@ -107,11 +103,7 @@ export type Control = {
name: string,
position: [number, number],
icon: Icon,
iconColor?: (
internals: Map<string, Internal>,
actuals: Map<string, Actual>,
state: State
) => Color,
iconColor?: (state: State) => Color,
ui: Array<ControlUI>
};
export type Controls = Map<string, Control>;

View file

@ -1,11 +1,9 @@
// @flow
import * as React from "react";
import { getInternals, getActuals } from "utils/state";
export opaque type RawIcon: string = string;
export type Icon = (Map<string, Internal>, Map<string, Actual>, State) =>
RawIcon;
export type Icon = (State) => RawIcon;
export const raw_mdi = (name: string): RawIcon => {
return `mdi ${name.split(" ").map((icon) => "mdi-".concat(icon)).join(" ")}`;
@ -13,47 +11,35 @@ export const raw_mdi = (name: string): RawIcon => {
export const mdi = (icon: string) => () => raw_mdi(icon);
export const mdi_battery = (topic: string) =>
(state: Map<string, Internal>) => {
const rawval = state[topic];
const val = parseInt(rawval);
if (isNaN(val)) {
return raw_mdi("battery-unknown");
} else if (val > 95) {
return raw_mdi("battery");
} else if (val > 85) {
return raw_mdi("battery-90");
} else if (val > 75) {
return raw_mdi("battery-80");
} else if (val > 65) {
return raw_mdi("battery-70");
} else if (val > 55) {
return raw_mdi("battery-60");
} else if (val > 45) {
return raw_mdi("battery-50");
} else if (val > 35) {
return raw_mdi("battery-40");
} else if (val > 25) {
return raw_mdi("battery-30");
} else if (val > 15) {
return raw_mdi("battery-20");
} else {
return raw_mdi("battery-10");
}
};
export const toRawIcon = (icon: Icon, state: State): RawIcon => {
const internals: Map<string, Internal> = getInternals(state);
const actuals: Map<string, Actual> = getActuals(state);
return icon(internals, actuals, state);
export const mdi_battery = (topic: string) => (state: State) => {
const rawval = state[topic];
const val = parseInt(rawval);
if (isNaN(val)) {
return raw_mdi("battery-unknown");
} else if (val > 95) {
return raw_mdi("battery");
} else if (val > 85) {
return raw_mdi("battery-90");
} else if (val > 75) {
return raw_mdi("battery-80");
} else if (val > 65) {
return raw_mdi("battery-70");
} else if (val > 55) {
return raw_mdi("battery-60");
} else if (val > 45) {
return raw_mdi("battery-50");
} else if (val > 35) {
return raw_mdi("battery-40");
} else if (val > 25) {
return raw_mdi("battery-30");
} else if (val > 15) {
return raw_mdi("battery-20");
} else {
return raw_mdi("battery-10");
}
};
export const renderRawIcon =
export const renderIcon =
(icon: RawIcon, extraClass?: string): React.Node => {
return <i className={`${extraClass || ""} ${icon}`}></i>;
};
export const renderIcon =
(icon: Icon, state: State, extraClass?: string): React.Node => {
return renderRawIcon(toRawIcon(icon, state), extraClass);
};

View file

@ -1,8 +1,21 @@
// @flow
import type { TopicType } from "config/flowtypes";
import at from "lodash/at";
export const string: TopicType = (msg: Buffer) => msg.toString();
export const string: TopicType = (msg) => msg.toString();
export const json = (path: string, innerType?: TopicType): TopicType => {
const parseAgain = innerType == null ? (x) => x : innerType;
return (msg) => parseAgain(JSON.parse(msg.toString())[path]);
const parseAgain = innerType == null ? (x) => x.toString() : innerType;
return (msg) => parseAgain(Buffer.from(
at(JSON.parse(msg.toString()), path)[0].toString()));
};
export type TypeOptionParam = { otherwise?: string, [string]: string };
export const option = (values: TypeOptionParam): TopicType => {
// TODO: error
const defaultValue = values.otherwise != null ? values.otherwise : "";
const mapVal = (x) => (values[x] != null ? values[x] : defaultValue);
return (x) => mapVal(x.toString());
};
export const jsonArray = (msg: Buffer) => JSON.parse(msg.toString()).join(", ");