Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update ds-card-reader to use EdgeX-3.0 #144

Merged
merged 7 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/trivyimagescan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@7b7aa264d83dc58691451798b4d117d53d21edfe
with:
image-ref: 'automated-checkout/ds-card-reader:dev'
image-ref: 'automated-vending/ds-card-reader:dev'
format: 'template'
template: '@/contrib/sarif.tpl'
output: 'trivy-results-ds-card-reader.sarif'
Expand Down
20 changes: 11 additions & 9 deletions ds-card-reader/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Copyright © 2022 Intel Corporation. All rights reserved.
# Copyright © 2023 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause

FROM golang:1.18-alpine3.16 AS builder
FROM golang:1.20-alpine3.17 AS builder

LABEL license='SPDX-License-Identifier: BSD-3-Clause' \
copyright='Copyright (c) 2022: Intel'
copyright='Copyright (c) 2023: Intel'


# add git for go modules
RUN apk update && apk add --no-cache make git gcc libc-dev zeromq-dev linux-headers
# hadolint ignore=DL3018
RUN apk update && apk add --no-cache make git gcc libc-dev linux-headers
vli11 marked this conversation as resolved.
Show resolved Hide resolved

ENV GO111MODULE=on
WORKDIR /usr/local/bin/
Expand All @@ -28,16 +29,17 @@ COPY . .
RUN make gobuild

# Next image - Copy built Go binary into new workspace
FROM alpine
FROM alpine:3.17

LABEL license='SPDX-License-Identifier: BSD-3-Clause' \
copyright='Copyright (c) 2022: Intel'
copyright='Copyright (c) 2023: Intel'

RUN apk add --update --no-cache zeromq dumb-init
# hadolint ignore=DL3018
RUN apk add --update --no-cache dumb-init

COPY --from=builder /usr/local/bin/ds-card-reader/res/profiles/ds-card-reader.yaml /res/profiles/ds-card-reader.yaml
COPY --from=builder /usr/local/bin/ds-card-reader/res/devices/device-list.toml /res/devices/device-list.toml
COPY --from=builder /usr/local/bin/ds-card-reader/res/configuration.toml /res/configuration.toml
COPY --from=builder /usr/local/bin/ds-card-reader/res/devices/device-list.yaml /res/devices/device-list.yaml
COPY --from=builder /usr/local/bin/ds-card-reader/res/configuration.yaml /res/configuration.yaml
COPY --from=builder /usr/local/bin/ds-card-reader/main /ds-card-reader

CMD [ "/ds-card-reader", "-cp=consul.http://edgex-core-consul:8500", "-r"]
10 changes: 5 additions & 5 deletions ds-card-reader/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Copyright © 2022 Intel Corporation. All rights reserved.
# Copyright © 2023 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause

.PHONY: build gobuild run gorun stop test lint

MICROSERVICE=automated-checkout/ds-card-reader
MICROSERVICE=automated-vending/ds-card-reader

ARCH=$(shell uname -m)

Expand All @@ -19,7 +19,7 @@ docker:
.

gobuild: tidy
CGO_ENABLED=1 GOOS=linux go build -ldflags='-s -w' -a -installsuffix cgo main.go
CGO_ENABLED=1 GOOS=linux go build -ldflags='-s -w' -a main.go

run:
docker run \
Expand All @@ -34,7 +34,7 @@ stop:
docker rm -f $(MICROSERVICE):dev

test:
go test -tags no_zmq -test.v \
go test -test.v \
-cover \
./...

Expand All @@ -46,7 +46,7 @@ testHTML:
go tool cover -html=test_coverage.out

testPhysical:
sudo -E go test -tags no_zmq -test.v \
sudo -E go test -test.v \
-cover \
-tags=physical \
./...
Expand Down
6 changes: 3 additions & 3 deletions ds-card-reader/device/device.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Copyright © 2022 Intel Corporation. All rights reserved.
// Copyright © 2023 Intel Corporation. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause

package device

import (
"fmt"

dsModels "github.com/edgexfoundry/device-sdk-go/v2/pkg/models"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
dsModels "github.com/edgexfoundry/device-sdk-go/v3/pkg/models"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
)

// CardReader is an abstraction that allows for operations to be performed on
Expand Down
6 changes: 3 additions & 3 deletions ds-card-reader/device/device_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Intel Corporation. All rights reserved.
// Copyright © 2023 Intel Corporation. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause

package device
Expand All @@ -7,8 +7,8 @@ import (
"reflect"
"testing"

dsModels "github.com/edgexfoundry/device-sdk-go/v2/pkg/models"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
dsModels "github.com/edgexfoundry/device-sdk-go/v3/pkg/models"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
require "github.com/stretchr/testify/require"
)

Expand Down
8 changes: 4 additions & 4 deletions ds-card-reader/device/physical.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Intel Corporation. All rights reserved.
// Copyright © 2023 Intel Corporation. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause

package device
Expand All @@ -8,9 +8,9 @@ import (
"fmt"
"time"

dsModels "github.com/edgexfoundry/device-sdk-go/v2/pkg/models"
logger "github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
edgexcommon "github.com/edgexfoundry/go-mod-core-contracts/v2/common"
dsModels "github.com/edgexfoundry/device-sdk-go/v3/pkg/models"
logger "github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
edgexcommon "github.com/edgexfoundry/go-mod-core-contracts/v3/common"
evdev "github.com/gvalkov/golang-evdev"
)

Expand Down
5 changes: 2 additions & 3 deletions ds-card-reader/device/physical_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build all || physical
// +build all physical

// Copyright © 2022 Intel Corporation. All rights reserved.
// Copyright © 2023 Intel Corporation. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause

package device
Expand All @@ -14,8 +14,7 @@ import (
"strings"
"testing"

dsModels "github.com/edgexfoundry/device-sdk-go/v2/pkg/models"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
evdev "github.com/gvalkov/golang-evdev"
assert "github.com/stretchr/testify/assert"
require "github.com/stretchr/testify/require"
Expand Down
8 changes: 4 additions & 4 deletions ds-card-reader/device/virtual.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Intel Corporation. All rights reserved.
// Copyright © 2023 Intel Corporation. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause

package device
Expand All @@ -7,9 +7,9 @@ import (
"fmt"
"time"

dsModels "github.com/edgexfoundry/device-sdk-go/v2/pkg/models"
logger "github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/common"
dsModels "github.com/edgexfoundry/device-sdk-go/v3/pkg/models"
logger "github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
)

// CardReaderVirtual allows for the emulation of a physical card reader device
Expand Down
16 changes: 8 additions & 8 deletions ds-card-reader/device/virtual_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build all || !physical
// +build all !physical

// Copyright © 2022 Intel Corporation. All rights reserved.
// Copyright © 2023 Intel Corporation. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause

package device
Expand All @@ -12,18 +12,18 @@ import (

"ds-card-reader/common"

dsModels "github.com/edgexfoundry/device-sdk-go/v2/pkg/models"
logger "github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
dsModels "github.com/edgexfoundry/device-sdk-go/v3/pkg/models"
logger "github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
assert "github.com/stretchr/testify/assert"
require "github.com/stretchr/testify/require"
)

const (
virtualDeviceSearchPath = ""
virtualDeviceName = "ds-card-reader"
virtualVID = uint16(0x0000)
virtualPID = uint16(0x0000)
expectedCardNumberVirtual = "0123456789"
virtualDeviceSearchPath = ""
virtualDeviceName = "ds-card-reader"
virtualVID = uint16(0x0000)
virtualPID = uint16(0x0000)
expectedCardNumberVirtual = "0123456789"
)

// TestVirtualCardReader validates that the InitializeCardReader
Expand Down
59 changes: 40 additions & 19 deletions ds-card-reader/driver/driver.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,55 @@
// Copyright © 2022 Intel Corporation. All rights reserved.
// Copyright © 2023 Intel Corporation. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause

package driver

import (
"errors"
"fmt"
"sync"

common "ds-card-reader/common"
device "ds-card-reader/device"

dsModels "github.com/edgexfoundry/device-sdk-go/v2/pkg/models"
"github.com/edgexfoundry/device-sdk-go/v2/pkg/service"
"github.com/edgexfoundry/go-mod-core-contracts/v2/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v2/models"
"github.com/edgexfoundry/device-sdk-go/v3/pkg/interfaces"
dsModels "github.com/edgexfoundry/device-sdk-go/v3/pkg/models"
"github.com/edgexfoundry/go-mod-core-contracts/v3/clients/logger"
"github.com/edgexfoundry/go-mod-core-contracts/v3/models"
)

// CardReaderDriver represents the EdgeX driver that interfaces with the
// underlying device
type CardReaderDriver struct {
LoggingClient logger.LoggingClient
asyncCh chan<- *dsModels.AsyncValues
CardReader device.CardReader
Config *device.ServiceConfig

svc *service.DeviceService
sdk interfaces.DeviceServiceSDK
}

var once sync.Once
var cardReaderDriver *CardReaderDriver

func NewCardReaderDriver() interfaces.ProtocolDriver {
once.Do(func() {
cardReaderDriver = new(CardReaderDriver)
})
return cardReaderDriver
}
lenny-goodell marked this conversation as resolved.
Show resolved Hide resolved

// Initialize initializes the card reader device within EdgeX. This is the
// main entrypoint of this application
func (drv *CardReaderDriver) Initialize(lc logger.LoggingClient, asyncCh chan<- *dsModels.AsyncValues, deviceCh chan<- []dsModels.DiscoveredDevice) (err error) {
func (drv *CardReaderDriver) Initialize(sdk interfaces.DeviceServiceSDK) (err error) {
// propagate the logging client to the driver so it can use it too
drv.LoggingClient = lc
drv.sdk = sdk
drv.LoggingClient = sdk.LoggingClient()
drv.asyncCh = sdk.AsyncValuesChannel()

// Only setting if nil allows for unit testing with VirtualBoard enabled
if drv.Config == nil {
drv.svc = service.RunningService()
if drv.svc == nil {
return errors.New("custom card reader driver service is nil")
}

drv.Config = &device.ServiceConfig{}

err := drv.svc.LoadCustomConfig(drv.Config, "DriverConfig")
err := sdk.LoadCustomConfig(drv.Config, "DriverConfig")
if err != nil {
return fmt.Errorf("custom card reader configuration failed to load: %v", err)
}
Expand All @@ -52,8 +60,8 @@ func (drv *CardReaderDriver) Initialize(lc logger.LoggingClient, asyncCh chan<-
// initialize the card reader device so that it can be controlled by our
// EdgeX driver, and so that it can store configuration values
drv.CardReader, err = device.InitializeCardReader(
lc,
asyncCh,
drv.LoggingClient,
drv.asyncCh,
drv.Config.DriverConfig.DeviceSearchPath,
drv.Config.DriverConfig.DeviceName,
drv.Config.DriverConfig.VID,
Expand Down Expand Up @@ -144,12 +152,25 @@ func (drv *CardReaderDriver) RemoveDevice(deviceName string, protocols map[strin
return nil
}

// DisconnectDevice allows EdgeX to emulate disconnection
func (drv *CardReaderDriver) DisconnectDevice(deviceName string, protocols map[string]models.ProtocolProperties) error {
return nil
}

// Stop allows EdgeX to emulate stopping the device
func (drv *CardReaderDriver) Stop(force bool) error {
return nil
}

// DisconnectDevice allows EdgeX to emulate disconnection
func (drv *CardReaderDriver) DisconnectDevice(deviceName string, protocols map[string]models.ProtocolProperties) error {
func (drv *CardReaderDriver) Start() error {
return nil
}

func (drv *CardReaderDriver) Discover() error {
return fmt.Errorf("driver's Discover function isn't implemented")
}

func (drv *CardReaderDriver) ValidateDevice(device models.Device) error {
drv.LoggingClient.Debug("Driver's ValidateDevice function isn't implemented")
lenny-goodell marked this conversation as resolved.
Show resolved Hide resolved
return nil
}
Loading