2017-03-22 22:17:16 +01:00
/ *
* Author : ntoff
* License : AGPLv3
* /
$ ( function ( ) {
function EstopViewModel ( parameters ) {
var self = this ;
2018-03-01 03:04:10 +01:00
2017-03-22 22:17:16 +01:00
self . loginState = parameters [ 0 ] ;
2017-03-22 23:54:26 +01:00
self . printerState = parameters [ 1 ] ;
2017-11-09 15:22:35 +01:00
self . settings = parameters [ 2 ] ;
self . estopCommand = ko . observable ( "M112" ) ;
2018-03-01 03:04:10 +01:00
self . estopReconnect = ko . observable ( false ) ;
2018-03-09 17:25:51 +01:00
self . emergencyCalled = ko . observable ( false ) ;
2017-03-24 07:18:17 +01:00
2018-03-01 03:04:10 +01:00
self . enableEstop = ko . pureComputed ( function ( ) {
2017-03-22 23:54:26 +01:00
return self . printerState . isOperational ( ) && self . loginState . isUser ( ) ;
} ) ;
2017-03-24 07:18:17 +01:00
self . estopState = ko . pureComputed ( function ( ) {
return self . loginState . isUser ( ) > 0 ? "estop_sidebar" : "estop_sidebar_disabled" ;
} ) ;
2018-03-09 17:25:51 +01:00
self . reconnect = ko . pureComputed ( function ( ) {
return self . estopReconnect ( ) && self . emergencyCalled ( ) ;
} )
2017-03-24 07:18:17 +01:00
self . buttonText = ko . pureComputed ( function ( ) {
if ( self . enableEstop ( ) ) {
return gettext ( "EMERGENCY STOP" ) ;
2018-03-09 17:25:51 +01:00
} else if ( self . reconnect ( ) ) {
return gettext ( "Reconnecting..." )
}
else {
2017-03-24 07:18:17 +01:00
return gettext ( "Offline" ) ;
}
} ) ;
2017-11-09 15:22:35 +01:00
self . buttonTitle = ko . pureComputed ( function ( ) {
self . estopCommand ( self . settings . settings . plugins . estop . estopCommand ( ) ) ;
return gettext ( "Sends " + self . estopCommand ( ) + " to the printer IMMEDIATELY" ) ;
} ) ;
self . onBeforeBinding = function ( ) {
2018-03-01 03:04:10 +01:00
self . updateSettingsValues ( ) ;
2017-11-09 15:22:35 +01:00
}
2018-03-01 03:04:10 +01:00
self . onSettingsHidden = function ( ) {
self . updateSettingsValues ( ) ;
}
self . updateSettingsValues = function ( ) { //lazy way of making sure we have the latest version of the settings
self . estopCommand ( self . settings . settings . plugins . estop . estopCommand ( ) ) ;
self . estopReconnect ( self . settings . settings . plugins . estop . estopReconnect ( ) ) ;
}
2018-03-09 17:25:51 +01:00
self . onEventDisconnected = function ( ) {
if ( self . estopReconnect ( ) && self . emergencyCalled ( ) ) {
self . timedReconnect = setTimeout ( function ( ) { //reconnect 3 seconds after detecting the printer is offline
self . emergencyCalled ( false ) ;
OctoPrint . connection . connect ( ) ;
} , 3 * 1000 ) ; //3 seconds
}
}
2018-03-01 03:04:10 +01:00
self . sendEstopCommand = function ( ) {
if ( self . enableEstop ( ) ) {
2018-03-09 17:25:51 +01:00
self . emergencyCalled ( true ) ;
2017-11-09 15:22:35 +01:00
self . estopCommand ( self . settings . settings . plugins . estop . estopCommand ( ) ) ;
OctoPrint . control . sendGcode ( self . estopCommand ( ) ) ;
2018-03-09 17:25:51 +01:00
if ( self . estopReconnect ( ) ) {
OctoPrint . connection . disconnect ( ) ; //normally octoprint would probably disconnect anyway, just calling this here in case the printer is in a blocking loop
2018-03-01 03:04:10 +01:00
}
2018-03-09 17:25:51 +01:00
}
}
2017-03-22 22:17:16 +01:00
}
2018-03-01 03:04:10 +01:00
OCTOPRINT _VIEWMODELS . push ( {
2017-03-22 22:17:16 +01:00
construct : EstopViewModel ,
dependencies : [
2018-03-01 03:04:10 +01:00
"loginStateViewModel" ,
"printerStateViewModel" ,
"settingsViewModel" ] ,
2017-03-24 07:18:17 +01:00
elements : [ "#sidebar_plugin_estop_wrapper" ]
2017-03-22 22:17:16 +01:00
} ) ;
} ) ;