Skip to content

Commit

Permalink
Merge pull request #296 from Z-Bolt/2.7.3-dev
Browse files Browse the repository at this point in the history
2.7.3 dev into master
  • Loading branch information
JeffB42 authored Apr 25, 2021
2 parents d102685 + 02adb46 commit b073bbe
Show file tree
Hide file tree
Showing 34 changed files with 353 additions and 191 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ JESSIE_GO_TAGS := gtk_3_14

# Build information
#GIT_COMMIT = $(shell git rev-parse HEAD | cut -c1-7)
VERSION := 2.7.2
VERSION := 2.7.3
BUILD_DATE ?= $(shell date --utc +%Y%m%d-%H:%M:%S)
#BRANCH = $(shell git rev-parse --abbrev-ref HEAD)

Expand Down
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,15 @@ There are two ways to install OctoScreen: the recommended and supported way is t

For example, to install on a new RaspberryPi with OctoPi:
```sh
wget https://github.com/Z-Bolt/OctoScreen/releases/download/v2.7.2/octoscreen_2.7.2_armhf.deb
sudo dpkg -i octoscreen_2.7.2_armhf.deb
wget https://github.com/Z-Bolt/OctoScreen/releases/download/v2.7.3/octoscreen_2.7.3_armhf.deb
sudo dpkg -i octoscreen_2.7.3_armhf.deb
```

Or to update an existing version of OctoScreen:
```sh
wget https://github.com/Z-Bolt/OctoScreen/releases/download/v2.7.2/octoscreen_2.7.2_armhf.deb
wget https://github.com/Z-Bolt/OctoScreen/releases/download/v2.7.3/octoscreen_2.7.3_armhf.deb
sudo dpkg -r octoscreen
sudo dpkg -i octoscreen_2.7.2_armhf.deb
sudo dpkg -i octoscreen_2.7.3_armhf.deb
sudo reboot now
```

Expand Down Expand Up @@ -169,6 +169,11 @@ The controls are limit to static controls without `inputs`.



------------
## Wiki
For troubleshooting and general information about this project, be sure to check out the Wiki page, located at https://github.com/Z-Bolt/OctoScreen/wiki



------------
<!--
Expand Down
1 change: 1 addition & 0 deletions interfaces/IPanel.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

type IPanel interface {
Name() string
Grid() *gtk.Grid
PreShow()
Show()
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func main() {
settings, _ := gtk.SettingsGetDefault()
settings.SetProperty("gtk-application-prefer-dark-theme", true)

utils.DumpSystemInformation()
utils.DumpEnvironmentVariables()

setCursor()
Expand Down
35 changes: 35 additions & 0 deletions octoprintApis/SystemExecuteCommandRequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package octoprintApis

import (
// "encoding/json"
"fmt"

"github.com/Z-Bolt/OctoScreen/logger"
"github.com/Z-Bolt/OctoScreen/octoprintApis/dataModels"
)


var ExecuteErrors = StatusMapping {
404: "The command could not be found for source and action",
500: "The command didn’t define a command to execute, the command returned a non-zero return code and ignore was not true or some other internal server error occurred",
}

// SystemExecuteCommandRequest retrieves all configured system commands.
type SystemExecuteCommandRequest struct {
// Source for which to list commands.
Source dataModels.CommandSource `json:"source"`

// Action is the identifier of the command, action from its definition.
Action string `json:"action"`
}

// Do sends an API request and returns an error if any.
func (this *SystemExecuteCommandRequest) Do(client *Client) error {
uri := fmt.Sprintf("%s/%s/%s", SystemCommandsApiUri, this.Source, this.Action)
_, err := client.doJsonRequest("POST", uri, nil, ExecuteErrors, true)
if err != nil {
logger.LogError("SystemExecuteCommandRequest.Do()", "client.doJsonRequest(POST)", err)
}

return err
}
2 changes: 1 addition & 1 deletion octoprintApis/ZOffsetRequest.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (this *ZOffsetRequest) Do(client *Client) (*dataModels.ZOffsetResponse, err
return nil, err
}

// bytes, err := client.doJsonRequest("POST", URIZBoltRequest, params, ConnectionErrors)
// bytes, err := client.doJsonRequest("POST", UriZBoltRequest, params, ConnectionErrors)
bytes, err := client.doJsonRequest("GET", PluginZBoltApiUri, params, ConnectionErrors, true)
if err != nil {
logger.LogError("ZOffsetRequest.Do()", "client.doJsonRequest()", err)
Expand Down
83 changes: 60 additions & 23 deletions octoprintApis/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,14 @@ func (this *Client) doJsonRequest(

bytes, err := this.doRequest(method, target, "application/json", body, statusMapping, isRequired)
if err != nil {
if isRequired {
// Some APIs return an error and the error should be logged.
logger.LogError("Client.doJsonRequest()", "this.doRequest()", err)
} else {
// On the other hand, calls to some APIs are optional, and the result should be logged
// as info and leave it up to the caller to determine whether it's an error or not.
logger.Infof("Client.doJsonRequest() - this.doRequest() returned %q", err)
}

logOptionalError("Client.doJsonRequest()", "this.doRequest()", err, isRequired)
logger.TraceLeave("Client.doJsonRequest()")
return nil, err
}

// Use the following only for debugging.
if logger.LogLevel() == "debug" {
logger.Debug("Client.doJsonRequest() - converting bytes to JSON")
json := string(bytes)
logger.Debugf("JSON response: %s", json)
}
Expand Down Expand Up @@ -124,59 +117,103 @@ func (this *Client) doRequest(
logger.LogError("Client.doRequest()", "this.httpClient.Do()", err)
logger.TraceLeave("Client.doRequest()")
return nil, err
} else {
logger.Debug("Client.doRequest() - httpClient.Do() passed")
}

bytes, err := this.handleResponse(response, statusMapping)
bytes, err := this.handleResponse(response, statusMapping, isRequired)
if err != nil {
if isRequired {
// Some APIs return an error and the error should be logged.
logger.LogError("Client.doRequest()", "this.handleResponse()", err)
} else {
// On the other hand, calls to some APIs are optional, and the result should be logged
// as info and leave it up to the caller to determine whether it's an error or not.
logger.Infof("Client.doRequest() - this.handleResponse() returned %q", err)
}
logger.TraceLeave("Client.doRequest()")
return nil, err
logOptionalError("Client.doRequest()", "this.handleResponse()", err, isRequired)
bytes = nil
} else {
logger.Debug("Client.doRequest() - handleResponse() passed")
}

logger.TraceLeave("Client.doRequest()")
return bytes, err
}


func (this *Client) handleResponse(
httpResponse *http.Response,
statusMapping StatusMapping,
isRequired bool,
) ([]byte, error) {
logger.TraceEnter("Client.handleResponse()")

defer httpResponse.Body.Close()

if statusMapping != nil {
if err := statusMapping.Error(httpResponse.StatusCode); err != nil {
logger.LogError("Client.handleResponse()", "statusMapping.Error()", err)
logger.TraceLeave("Client.handleResponse()")
return nil, err
}
}

if httpResponse.StatusCode == 401 {
logger.Error("Client.handleResponse() - StatusCode is 401")
logger.TraceLeave("Client.handleResponse()")
return nil, ErrUnauthorized
}

if httpResponse.StatusCode == 204 {
logger.Error("Client.handleResponse() - StatusCode is 204")
logger.TraceLeave("Client.handleResponse()")
return nil, nil
}

body, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
logger.LogError("Client.handleResponse()", "ioutil.ReadAll()", err)
logger.TraceLeave("Client.handleResponse()")
return nil, err
}

if httpResponse.StatusCode >= 200 && httpResponse.StatusCode <= 209 {
return body, nil
logger.Debugf("Client.handleResponse() - status code %d was within range", httpResponse.StatusCode)
} else {
errMsg := fmt.Sprintf("Unexpected status code: %d", httpResponse.StatusCode)
if httpResponse.StatusCode == 404 {
logOptionalMessage(errMsg, isRequired)
} else {
logger.Error(errMsg)
}

err = fmt.Errorf(errMsg)
body = nil
}

return nil, fmt.Errorf("unexpected status code: %d", httpResponse.StatusCode)
logger.TraceLeave("Client.handleResponse()")
return body, err
}

func logOptionalError(
currentFunctionName string,
functionCalledName string,
err error,
isRequired bool,
) {
if isRequired {
// Some APIs return an error and the error should be logged.
logger.LogError(currentFunctionName, functionCalledName, err)
} else {
// On the other hand, calls to some APIs are optional, and the result should be logged
// as info and leave it up to the caller to determine whether it's an error or not.
msg := fmt.Sprintf("%s - %s returned %q", currentFunctionName, functionCalledName, err)
logger.Info(msg)
}
}

func logOptionalMessage(
msg string,
isRequired bool,
) {
if isRequired {
logger.Error(msg)
} else {
logger.Info(msg)
}
}

func joinUrl(base, uri string) string {
u, _ := url.Parse(uri)
Expand Down
45 changes: 12 additions & 33 deletions octoprintApis/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package octoprintApis

import (
"encoding/json"
"fmt"
// "fmt"

"github.com/Z-Bolt/OctoScreen/logger"
"github.com/Z-Bolt/OctoScreen/octoprintApis/dataModels"
Expand All @@ -11,13 +11,6 @@ import (

const SystemCommandsApiUri = "/api/system/commands"


var ExecuteErrors = StatusMapping {
404: "The command could not be found for source and action",
500: "The command didn’t define a command to execute, the command returned a non-zero return code and ignore was not true or some other internal server error occurred",
}


// SystemCommandsRequest retrieves all configured system commands.
type SystemCommandsRequest struct{}

Expand All @@ -35,39 +28,25 @@ func (cmd *SystemCommandsRequest) Do(c *Client) (*dataModels.SystemCommandsRespo

for i := range response.Core {
commandDefinition := response.Core[i]
err = json.Unmarshal(commandDefinition.RawConfirm, &commandDefinition.Confirm)
if err != nil {
logger.LogError("SystemCommandsRequest.Do()", "json.Unmarshal(Core)", err)
commandDefinition.Confirm = ""
return nil, err
}
convertRawConfirm(commandDefinition)
}

for i := range response.Custom {
commandDefinition := response.Custom[i]
err = json.Unmarshal(commandDefinition.RawConfirm, &commandDefinition.Confirm)
if err != nil {
logger.LogError("SystemCommandsRequest.Do()", "json.Unmarshal(Custom)", err)
commandDefinition.Confirm = ""
return nil, err
}
convertRawConfirm(commandDefinition)
}

return response, err
}

// SystemExecuteCommandRequest retrieves all configured system commands.
type SystemExecuteCommandRequest struct {
// Source for which to list commands.
Source dataModels.CommandSource `json:"source"`

// Action is the identifier of the command, action from its definition.
Action string `json:"action"`
}
func convertRawConfirm(commandDefinition *dataModels.CommandDefinition) {
if commandDefinition == nil || commandDefinition.RawConfirm == nil || len(commandDefinition.RawConfirm) < 1 {
return
}

// Do sends an API request and returns an error if any.
func (cmd *SystemExecuteCommandRequest) Do(c *Client) error {
uri := fmt.Sprintf("%s/%s/%s", SystemCommandsApiUri, cmd.Source, cmd.Action)
_, err := c.doJsonRequest("POST", uri, nil, ExecuteErrors, true)
return err
err := json.Unmarshal(commandDefinition.RawConfirm, &commandDefinition.Confirm)
if err != nil {
logger.LogError("SystemCommandsRequest.convertRawConfirm()", "json.Unmarshal(Custom)", err)
commandDefinition.Confirm = ""
}
}
7 changes: 4 additions & 3 deletions ui/BedLevelPanel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"fmt"

"github.com/gotk3/gotk3/gtk"
"github.com/Z-Bolt/OctoScreen/interfaces"

// "github.com/Z-Bolt/OctoScreen/interfaces"
"github.com/Z-Bolt/OctoScreen/logger"
"github.com/Z-Bolt/OctoScreen/octoprintApis"
// "github.com/Z-Bolt/OctoScreen/octoprintApis/dataModels"
"github.com/Z-Bolt/OctoScreen/utils"
)


var bedLevelPanelInstance *bedLevelPanel

type bedLevelPanel struct {
Expand All @@ -23,11 +25,10 @@ type bedLevelPanel struct {

func BedLevelPanel(
ui *UI,
parentPanel interfaces.IPanel,
) *bedLevelPanel {
if bedLevelPanelInstance == nil {
instance := &bedLevelPanel {
CommonPanel: NewCommonPanel(ui, parentPanel),
CommonPanel: NewCommonPanel("BedLevelPanel", ui),
}
instance.initialize()
bedLevelPanelInstance = instance
Expand Down
Loading

0 comments on commit b073bbe

Please sign in to comment.