Skip to content

Commit

Permalink
* Improved keeping the connection open.
Browse files Browse the repository at this point in the history
* Output pins now can be correctly set according to their scale!
* Print-Fan not updating fixed?
  • Loading branch information
Clon1998 committed Aug 30, 2021
1 parent a90d420 commit 90fd75c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 30 deletions.
39 changes: 23 additions & 16 deletions lib/WebSocket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ enum WebSocketState { disconnected, connecting, connected, error }

class WebSocketWrapper {
final _logger = getLogger('WebSocketWrapper');
final int defaultMaxRetries;
final Duration _defaultTimeout;

int tries = 0;

String url;
IOWebSocketChannel? _channel;

String? apiKey;

WebSocketWrapper(this.url, this._defaultTimeout,
{this.defaultMaxRetries = 3, this.apiKey}) {
this.initCommunication(defaultMaxRetries);
IOWebSocketChannel? _channel;

final Duration _defaultTimeout;

Map<String, dynamic> _headers = {};

WebSocketWrapper(this.url, this._defaultTimeout, {this.apiKey}) {
if (apiKey != null) _headers['X-Api-Key'] = apiKey;
this.initCommunication();
}

BehaviorSubject<WebSocketState> stateStream =
Expand All @@ -29,7 +36,7 @@ class WebSocketWrapper {
WebSocketState get state => stateStream.value;

set state(WebSocketState newState) {
_logger.i("WebSocket: $state ➝ $newState");
_logger.i("$state ➝ $newState");
stateStream.add(newState);
}

Expand All @@ -53,30 +60,29 @@ class WebSocketWrapper {
/// ----------------------------------------------------------
/// Initialization the WebSockets connection with the server
/// ----------------------------------------------------------
initCommunication([int? tries]) {
_tryConnect(tries ?? defaultMaxRetries);
initCommunication() {
_tryConnect();
}

_tryConnect(int maxRetries) {
_logger.i("Trying to connect to $url with APIkey: `$apiKey`");
_tryConnect() {
_logger.i("Trying to connect to $url with APIkey: `${apiKey??'NO-APIKEY'}`");
state = WebSocketState.connecting;
reset();

Map<String, dynamic> headers = {};
if (apiKey != null) headers['X-Api-Key'] = apiKey;
WebSocket.connect(url.toString(), headers: headers)
WebSocket.connect(url.toString(), headers: _headers)
.timeout(_defaultTimeout)
.then((socket) {
socket.pingInterval = _defaultTimeout;
_channel = IOWebSocketChannel(socket);
tries = 0;

///
/// Start listening to notifications / messages
///
_channel!.stream.listen(
_onWSMessage,
onError: _onWSError,
onDone: () => _onWSClosesNormal(maxRetries),
onDone: () => _onWSClosesNormal(),
);
// Send a req msg to be sure we are connected!

Expand Down Expand Up @@ -185,12 +191,13 @@ class WebSocketWrapper {
return false;
}

_onWSClosesNormal(int maxRetries) {
_onWSClosesNormal() {
var t = state;
if (t != WebSocketState.error) {
t = WebSocketState.disconnected;
}
if (!stateStream.isClosed) state = t;
initCommunication();
_logger.i("WS-Stream close normal!");
}
}
18 changes: 18 additions & 0 deletions lib/dto/config/ConfigFile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,22 @@ class ConfigExtruder {
}
}

class ConfigOutput {
final String name;
late double scale;
late bool pwm;
ConfigOutput.parse(this.name, Map<String, dynamic> json) {
scale = json['scale']?? 1;
pwm = json['pwm']??false;
}
}

//TODO Decide regarding null values or not!
class ConfigFile {
ConfigPrinter? configPrinter;
ConfigHeaterBed? configHeaterBed;
Map<String, ConfigExtruder> extruders = HashMap();
Map<String, ConfigOutput> outputs = HashMap();

ConfigFile();

Expand All @@ -80,6 +91,13 @@ class ConfigFile {
}
extruders[key] = ConfigExtruder.parse(key, jsonChild);
}

if (key.startsWith('output')) {
List<String> split = key.split(" ");
String name = split.length > 1 ? split.skip(1).join(" ") : split[0];
Map<String, dynamic> jsonChild = Map.of(rawConfig[key]);
outputs[name] = ConfigOutput.parse(name, jsonChild);
}
});

//ToDo parse the config for e.g. EXTRUDERS (Temp settings), ...
Expand Down
1 change: 1 addition & 0 deletions lib/dto/machine/Printer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ class TemperatureSensor {

class OutputPin {
String name;
// This value is between 0-1
double value = 0.0;

OutputPin(this.name);
Expand Down
4 changes: 2 additions & 2 deletions lib/service/PrinterService.dart
Original file line number Diff line number Diff line change
Expand Up @@ -476,9 +476,9 @@ class PrinterService {
});
}

outputPin(String pinName, double perc) {
outputPin(String pinName, double value) {
_webSocket.sendObject("printer.gcode.script", null,
params: {'script': "SET_PIN PIN=$pinName VALUE=${perc.toInt()}"});
params: {'script': "SET_PIN PIN=$pinName VALUE=${value.toStringAsFixed(2)}"});
}

gCodeMacro(String macro) {
Expand Down
8 changes: 5 additions & 3 deletions lib/ui/dialog/editForm/editForm_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ class EditFormDialogViewArguments {
final num? min;
final num? max;
final num? current;
final int? fraction;

EditFormDialogViewArguments({this.min, this.max, this.current});
EditFormDialogViewArguments({this.min, this.max, this.current, this.fraction});
}

class EditFormDialogView extends StatelessWidget {
Expand Down Expand Up @@ -70,7 +71,8 @@ class NumField extends ViewModelWidget<EditFormViewModel> {
EditFormDialogViewArguments passedData = request.data;
num currentValue = passedData.current ?? 0;
num lowerBorder = passedData.min ?? 0;
num upperBorder = passedData.max ?? 300; //ToDo set to printers Max temp
num upperBorder = passedData.max ?? 100;
int frac = passedData.fraction ?? 0;

return FormBuilderTextField(
autofocus: true,
Expand All @@ -81,7 +83,7 @@ class NumField extends ViewModelWidget<EditFormViewModel> {
FormBuilderValidators.required(context)
]),
valueTransformer: (String? text) => text == null ? 0 : num.tryParse(text),
initialValue: currentValue.toStringAsFixed(0),
initialValue: currentValue.toStringAsFixed(frac.toInt()),
name: 'newValue',
style: Theme.of(context).inputDecorationTheme.counterStyle,
keyboardType:
Expand Down
12 changes: 8 additions & 4 deletions lib/ui/views/overview/tabs/control_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -370,11 +370,12 @@ class PinsCard extends ViewModelWidget<ControlTabViewModel> {
List<Widget> rows = [];

for (var pin in model.printer.outputPins) {
var configForOutput = model.configForOutput(pin.name);
var row = _PinTile(
name: model.beautifyName(pin.name),
value: pin.value,
value: pin.value * (configForOutput?.scale ?? 1),
width: width,
onTap: () => model.onEditPin(pin),
onTap: () => model.onEditPin(pin, configForOutput),
);
rows.add(row);
}
Expand All @@ -384,6 +385,8 @@ class PinsCard extends ViewModelWidget<ControlTabViewModel> {

class _PinTile extends StatelessWidget {
final String name;

/// Expect a value between 0-Scale!
final double value;
final double width;
final VoidCallback? onTap;
Expand All @@ -397,8 +400,9 @@ class _PinTile extends StatelessWidget {
}) : super(key: key);

String get pinValue {
double perc = value * 100;
if (value > 0) return '${perc.toStringAsFixed(0)} %';
// double perc = value * 100;
if (value == value.round()) return value.toStringAsFixed(0);
if (value > 0) return value.toStringAsFixed(2);
return 'Off';
}

Expand Down
16 changes: 12 additions & 4 deletions lib/ui/views/overview/tabs/control_tab_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:mobileraker/app/AppSetup.locator.dart';
import 'package:mobileraker/dto/config/ConfigFile.dart';
import 'package:mobileraker/dto/machine/Printer.dart';
import 'package:mobileraker/dto/machine/PrinterSetting.dart';
import 'package:mobileraker/dto/server/Klipper.dart';
Expand Down Expand Up @@ -74,19 +75,26 @@ class ControlTabViewModel extends MultipleStreamViewModel {

bool get hasPrinter => dataReady(_PrinterStreamKey);

onEditPin(OutputPin pin) {
ConfigOutput? configForOutput(String name) {
return printer.configFile.outputs[name];
}

onEditPin(OutputPin pin, ConfigOutput? configOutput) {
int fractionToShow = (configOutput == null || !configOutput.pwm) ? 0 : 2;
_dialogService
.showCustomDialog(
variant: DialogType.editForm,
title: "Edit ${pin.name} %",
title: "Edit ${beautifyName(pin.name)} value!",
mainButtonTitle: "Confirm",
secondaryButtonTitle: "Cancel",
data: EditFormDialogViewArguments(
max: 100, current: pin.value * 100.round()))
max: configOutput?.scale.toInt() ?? 1,
current: pin.value * (configOutput?.scale ?? 1),
fraction: fractionToShow))
.then((value) {
if (value != null && value.confirmed && value.data != null) {
num v = value.data;
_printerService?.outputPin(pin.name, v.toDouble() / 10);
_printerService?.outputPin(pin.name, v.toDouble());
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
version: 1.1.0+2

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down

0 comments on commit 90fd75c

Please sign in to comment.