Skip to content

Commit

Permalink
Merge pull request #5 from graugans/pcic
Browse files Browse the repository at this point in the history
Implementation of a basic PCIC receiver
  • Loading branch information
graugans authored Jan 26, 2024
2 parents 63e8634 + 23c34d9 commit 905677d
Show file tree
Hide file tree
Showing 15 changed files with 1,513 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.bz2 filter=lfs diff=lfs merge=lfs -text
21 changes: 15 additions & 6 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,28 @@ jobs:
runs-on: ubuntu-latest
needs: lint
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
- name: ⬇️ Checkout
uses: actions/checkout@v3
with:
go-version: '1.20'
- run: go test -v ./...
lfs: 'true'
- name: 👷 Prepare the Go environment
uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: 🧪 Run the unit tests
run: go test -v -failfast -coverprofile cover.out -timeout=1m ./...
- name: 🚀 Upload the coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21'

- name: Install dependencies
run: go get -d ./...
Expand All @@ -63,7 +72,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
go-version: '1.21'
- uses: go-semantic-release/action@v1
with:
hooks: goreleaser
Expand Down
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"PCIC"
]
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@


# Go client for the ifm OVP8xx series of devices

A GO module and cli to access the ifm OVP8xx series of devices.

[![GitHub go.mod Go version of a Go module](https://img.shields.io/github/go-mod/go-version/gomods/athens.svg)](https://github.com/gomods/athens)
[![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
![example workflow](https://github.com/graugans/go-ovp8xx/actions/workflows/go.yml/badge.svg)
[![codecov](https://codecov.io/gh/graugans/go-ovp8xx/graph/badge.svg?token=BU6UPYCUPI)](https://codecov.io/gh/graugans/go-ovp8xx)

## Project status

Expand Down Expand Up @@ -70,3 +73,12 @@ The result depends on your specific device:
}
}
```

## Testing

Please ensure you have the Git Large File Storage extension installed. Some of the tests require blobs which are handled as Large File Storage files. In case the files are not populated as expected this may help:

```sh
git lfs fetch --all
git lfs pull
```
15 changes: 14 additions & 1 deletion cmd/ovp8xx/cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
type helperConfig struct {
pretty bool
host string
port uint16
pointers []string
}

Expand Down Expand Up @@ -39,18 +40,30 @@ func (c *helperConfig) jsonPointers() []string {
return c.pointers
}

func (c *helperConfig) remotePort() uint16 {
return c.port
}

func NewHelper(cmd *cobra.Command) (helperConfig, error) {
var conf = helperConfig{}
var err error
conf.pretty, err = cmd.Flags().GetBool("pretty")
if err != nil {
return conf, err
// In case no pretty flag is set, we default to false
conf.pretty = false
}
conf.host, err = rootCmd.PersistentFlags().GetString("ip")
if err != nil {
return conf, err
}
// Pointers can be empty
conf.pointers, _ = cmd.Flags().GetStringSlice("pointer")

// Port can be empty
conf.port, err = cmd.Flags().GetUint16("port")
if err != nil {
conf.port = 50010
}

return conf, nil
}
82 changes: 82 additions & 0 deletions cmd/ovp8xx/cmd/pcic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright © 2024 Christian Ege <[email protected]>
*/
package cmd

import (
"fmt"

"github.com/graugans/go-ovp8xx/pkg/pcic"
"github.com/spf13/cobra"
)

// PCICReceiver represents a receiver for PCIC data.
type PCICReceiver struct {
frame pcic.Frame // The PCIC frame.
notificationMsg pcic.NotificationMessage // The notification message.
errorMsg pcic.ErrorMessage // The error message.
framecount int64 // The count of frames received.
}

// Result is a method of the PCICReceiver struct that sets the received frame and increments the framecount.
// It takes a pcic.Frame as a parameter.
func (r *PCICReceiver) Result(frame pcic.Frame) {
r.frame = frame
fmt.Printf("Framecount: %d\n", r.framecount)
r.framecount++
}

// Error handles the error message received from the PCIC.
// It sets the errorMsg field of the PCICReceiver struct and prints the error message.
func (r *PCICReceiver) Error(msg pcic.ErrorMessage) {
r.errorMsg = msg
fmt.Printf("Error: %v\n", msg)
}

// Notification is a method of the PCICReceiver type that handles incoming notification messages.
// It updates the notificationMsg field of the receiver and prints the message to the console.
func (r *PCICReceiver) Notification(msg pcic.NotificationMessage) {
r.notificationMsg = msg
fmt.Printf("Notification: %v\n", msg)
}

// pcicCommand is a function that handles the execution of the "pcic" command.
// It initializes a PCICReceiver, creates a helper, and establishes a connection to the PCIC client.
// It then continuously processes incoming data using the PCIC client and the testHandler.
// If an error occurs during any of these steps, it is returned.
// Returns nil if the function completes successfully.
func pcicCommand(cmd *cobra.Command, args []string) error {
var testHandler *PCICReceiver = &PCICReceiver{}
var err error
helper, err := NewHelper(cmd)
if err != nil {
return err
}

pcic, err := pcic.NewPCICClient(
pcic.WithTCPClient(helper.hostname(), helper.remotePort()),
)
if err != nil {
return err
}
for {
err = pcic.ProcessIncomming(testHandler)
if err != nil {
// An error occured, we break the loop
break
}
}
return err
}

// pcicCmd represents the pcic command
var pcicCmd = &cobra.Command{
Use: "pcic",
Short: "Create a PCIC connection to the device",
RunE: pcicCommand,
}

func init() {
rootCmd.AddCommand(pcicCmd)
pcicCmd.Flags().Uint16("port", 50010, "The port to connect to")
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
module github.com/graugans/go-ovp8xx

go 1.20
go 1.21

require (
alexejk.io/go-xmlrpc v0.4.0
github.com/spf13/cobra v1.7.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/stretchr/testify v1.8.4 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
alexejk.io/go-xmlrpc v0.4.0 h1:HvaeCuACuF2NBJruG90AJKc5JHRGj9vKxu2ltJntQR4=
alexejk.io/go-xmlrpc v0.4.0/go.mod h1:M7f2OzqvZIWrN1LftR4uwW/bLpxrFoQYjWfm4gQB4JA=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit 905677d

Please sign in to comment.