Repository: edgexfoundry/device-modbus-go
Pull request #514 (2023-10-24 14:24:54)

feat!: Remove snap packaging
Removing snap packaging in accordance with the TSC vote to discontinue release of snaps as of v3.1.

## PR Checklist
Please check if your PR fulfills the following requirements:

- [x] I am not introducing a breaking change (if you are, flag in conventional commit message with `BREAKING CHANGE:` describing the break)
- [x] I am not introducing a new dependency (add notes below if you are)
- [ ] I have added unit tests for the new feature or bug fix (if not, why?)
- [ ] I have fully tested (add details below) this the new feature or bug fix (if not, why?)
- [ ] I have opened a PR for the related docs change (if not, why?)
  <link to docs PR>

## Testing Instructions
<!-- How can the reviewers test your change? -->

## New Dependency Instructions (If applicable)
<!-- Please follow [vetting instructions](https://wiki.edgexfoundry.org/display/FA/Vetting+Process+for+3rd+Party+Dependencies) and place results here -->


Patch:
diff --git a/.github/dependabot.yml b/.github/dependabot.yml
index 4f21246..f34c249 100644
--- a/.github/dependabot.yml
+++ b/.github/dependabot.yml
@@ -17,7 +17,3 @@ updates:
         # Included when device-sdk-go is updated
       - dependency-name: "github.com/edgexfoundry/go-mod-bootstrap/v3"
         # Included when device-sdk-go is updated          
-  - package-ecosystem: "gomod"
-    directory: "/snap/local/helper-go"
-    schedule:
-      interval: "daily"  
diff --git a/.github/workflows/snap.yaml b/.github/workflows/snap.yaml
deleted file mode 100644
index f719c49..0000000
--- a/.github/workflows/snap.yaml
+++ /dev/null
@@ -1,30 +0,0 @@
-name: Snap Testing
-
-on:
-  push:
-    branches: [ main ]
-  pull_request:
-    branches: [ main ]
-  # allow manual trigger
-  workflow_dispatch:
-
-jobs:
-  build:
-    runs-on: ubuntu-latest
-    steps:
-      - name: Build and upload snap
-        id: build
-        uses: canonical/edgex-snap-testing/build@v3
-    outputs:
-      snap: ${{steps.build.outputs.snap}}
-
-  test:
-    needs: build
-    runs-on: ubuntu-latest
-    steps:
-      - name: Download and test snap
-        uses: canonical/edgex-snap-testing/test@v3
-        with:
-          name: device-modbus
-          snap: ${{needs.build.outputs.snap}}
-
diff --git a/.gitignore b/.gitignore
index 3705aae..d9702ee 100644
--- a/.gitignore
+++ b/.gitignore
@@ -19,11 +19,4 @@ cmd/device-modbus
 glide.lock
 VERSION
 
-# snap files
-*.snap
-*.assert
-prime/
-stage/
-parts/
-squashfs-root/
 coverage.out
diff --git a/Jenkinsfile b/Jenkinsfile
index eed737f..eb16417 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -15,6 +15,5 @@
 //
 
 edgeXBuildGoApp (
-    project: 'device-modbus-go',
-    buildSnap: false
+    project: 'device-modbus-go'
 )
diff --git a/README.md b/README.md
index 550768a..4b8a446 100644
--- a/README.md
+++ b/README.md
@@ -12,7 +12,7 @@
 Modbus Micro Service - device service for connecting Modbus devices to EdgeX.
 ## Build with NATS Messaging
 Currently, the NATS Messaging capability (NATS MessageBus) is opt-in at build time.
-This means that the published Docker image and Snaps do not include the NATS messaging capability.
+This means that the published Docker images do not include the NATS messaging capability.
 
 The following make commands will build the local binary or local Docker image with NATS messaging
 capability included.
@@ -43,12 +43,8 @@ Start up a Modbus TCP simulator.
 ```
 ## Packaging
 
-This component is packaged as docker image and snap.
-
 For docker, please refer to the [Dockerfile](Dockerfile) and [Docker Compose Builder] scripts.
 
-For the snap, refer to the [snap](snap) directory.
-
 ## Modbus RTU Usage
 Users can refer to [the document](https://docs.edgexfoundry.org/2.0/examples/Ch-ExamplesAddingModbusDevice/#set-up-the-modbus-rtu-device) to set up the Modbus RTU device.
 
diff --git a/internal/driver/constant.go b/internal/driver/constant.go
index 295ce7c..7f1a957 100644
--- a/internal/driver/constant.go
+++ b/internal/driver/constant.go
@@ -25,6 +25,7 @@ const (
 	FLOAT64 = "FLOAT64"
 
 	DISCRETES_INPUT   = "DISCRETES_INPUT"
+	DISCRETE_INPUTS   = "DISCRETE_INPUTS" // since DISCRETES_INPUT looks like a typo and it come from Modbus spec, so we also support DISCRETE_INPUTS as primary table
 	COILS             = "COILS"
 	INPUT_REGISTERS   = "INPUT_REGISTERS"
 	HOLDING_REGISTERS = "HOLDING_REGISTERS"
@@ -43,6 +44,7 @@ const (
 
 var PrimaryTableBitCountMap = map[string]uint16{
 	DISCRETES_INPUT:   1,
+	DISCRETE_INPUTS:   1,
 	COILS:             1,
 	INPUT_REGISTERS:   16,
 	HOLDING_REGISTERS: 16,
diff --git a/internal/driver/modbusclient.go b/internal/driver/modbusclient.go
index 837429b..a76853a 100644
--- a/internal/driver/modbusclient.go
+++ b/internal/driver/modbusclient.go
@@ -64,7 +64,7 @@ func (c *ModbusClient) GetValue(commandInfo interface{}) ([]byte, error) {
 	var err error
 
 	switch modbusCommandInfo.PrimaryTable {
-	case DISCRETES_INPUT:
+	case DISCRETES_INPUT, DISCRETE_INPUTS:
 		response, err = c.client.ReadDiscreteInputs(modbusCommandInfo.StartingAddress, modbusCommandInfo.Length)
 	case COILS:
 		response, err = c.client.ReadCoils(modbusCommandInfo.StartingAddress, modbusCommandInfo.Length)
@@ -94,7 +94,7 @@ func (c *ModbusClient) SetValue(commandInfo interface{}, value []byte) error {
 	var err error
 
 	switch modbusCommandInfo.PrimaryTable {
-	case DISCRETES_INPUT:
+	case DISCRETES_INPUT, DISCRETE_INPUTS:
 		err = fmt.Errorf("Error: DISCRETES_INPUT is Read-Only..!!")
 
 	case COILS:
diff --git a/snap/README.md b/snap/README.md
deleted file mode 100644
index 67fb6cc..0000000
--- a/snap/README.md
+++ /dev/null
@@ -1,38 +0,0 @@
-# EdgeX Modbus Device Service Snap
-[![edgex-device-modbus](https://snapcraft.io/edgex-device-modbus/badge.svg)](https://snapcraft.io/edgex-device-modbus)
-
-This directory contains the snap packaging of the EdgeX Modbus device service.
-
-The snap is built automatically and published on the Snap Store as [edgex-device-modbus].
-
-For usage instructions, please refer to Device Modbus section in [Getting Started using Snaps][docs].
-
-## Build from source
-Execute the following command from the top-level directory of this repo:
-```
-snapcraft
-```
-
-This will create a snap package file with `.snap` extension. It can be installed locally by setting the `--dangerous` flag:
-```bash
-sudo snap install --dangerous <snap-file>
-```
-
-The [snapcraft overview](https://snapcraft.io/docs/snapcraft-overview) provides additional details.
-
-### Obtain a Secret Store token
-The `edgex-secretstore-token` snap slot makes it possible to automatically receive a token from a locally installed platform snap.
-
-If the snap is built and installed locally, the interface will not auto-connect. You can check the status of the connections by running the `snap connections edgex-device-modbus` command.
-
-To manually connect and obtain a token:
-```bash
-sudo snap connect edgexfoundry:edgex-secretstore-token edgex-device-modbus:edgex-secretstore-token
-```
-
-Please refer [here][secret-store-token] for further information.
-
-
-[edgex-device-modbus]: https://snapcraft.io/edgex-device-modbus
-[docs]: https://docs.edgexfoundry.org/2.2/getting-started/Ch-GettingStartedSnapUsers/#device-modbus
-[secret-store-token]: https://docs.edgexfoundry.org/2.2/getting-started/Ch-GettingStartedSnapUsers/#secret-store-token
diff --git a/snap/hooks/configure b/snap/hooks/configure
deleted file mode 100644
index 074e47e..0000000
--- a/snap/hooks/configure
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/bash -e
-
-exec $SNAP/bin/helper-go configure
diff --git a/snap/hooks/install b/snap/hooks/install
deleted file mode 100644
index 7676123..0000000
--- a/snap/hooks/install
+++ /dev/null
@@ -1,3 +0,0 @@
-#!/bin/bash -e
-
-exec $SNAP/bin/helper-go install
\ No newline at end of file
diff --git a/snap/local/.gitkeep b/snap/local/.gitkeep
deleted file mode 100644
index e69de29..0000000
diff --git a/snap/local/bin/source-env-file.sh b/snap/local/bin/source-env-file.sh
deleted file mode 100755
index 81914a5..0000000
--- a/snap/local/bin/source-env-file.sh
+++ /dev/null
@@ -1,21 +0,0 @@
-#!/bin/bash -e
-
-# convert cmdline to string array
-ARGV=($@)
-
-# grab binary path
-BINPATH="${ARGV[0]}"
-
-# binary name == service name/key
-SERVICE=$(basename "$BINPATH")
-ENV_FILE="$SNAP_DATA/config/$SERVICE/overrides.env"
-TAG="edgex-$SERVICE."$(basename "$0")
-
-if [ -f "$ENV_FILE" ]; then
-    logger --tag=$TAG "sourcing $ENV_FILE"
-    set -o allexport
-    source "$ENV_FILE" set
-    set +o allexport
-fi
-
-exec "$@"
diff --git a/snap/local/helper-go/Makefile b/snap/local/helper-go/Makefile
deleted file mode 100644
index 5706aa0..0000000
--- a/snap/local/helper-go/Makefile
+++ /dev/null
@@ -1,10 +0,0 @@
-
-build:
-	go build -ldflags="-s -w" -o helper-go
-
-tidy:
-	go mod tidy
-
-clean:
-	rm -f helper-go
-
diff --git a/snap/local/helper-go/configure.go b/snap/local/helper-go/configure.go
deleted file mode 100644
index b84e999..0000000
--- a/snap/local/helper-go/configure.go
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (C) 2021 Canonical Ltd
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
- *  in compliance with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License
- * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
- * or implied. See the License for the specific language governing permissions and limitations under
- * the License.
- *
- * SPDX-License-Identifier: Apache-2.0'
- */
-
-package main
-
-import (
-	"github.com/canonical/edgex-snap-hooks/v3/log"
-	"github.com/canonical/edgex-snap-hooks/v3/options"
-)
-
-func configure() {
-	log.SetComponentName("configure")
-
-	if err := options.ProcessConfig(app); err != nil {
-		log.Fatalf("Error processing config options: %v", err)
-	}
-
-	if err := options.ProcessAutostart(app); err != nil {
-		log.Fatalf("Error processing autostart options: %v", err)
-	}
-}
diff --git a/snap/local/helper-go/go.mod b/snap/local/helper-go/go.mod
deleted file mode 100644
index 0feca00..0000000
--- a/snap/local/helper-go/go.mod
+++ /dev/null
@@ -1,5 +0,0 @@
-module github.com/edgexfoundry/device-modbus-go/snap/local/helper-go
-
-require github.com/canonical/edgex-snap-hooks/v3 v3.0.0
-
-go 1.20
diff --git a/snap/local/helper-go/go.sum b/snap/local/helper-go/go.sum
deleted file mode 100644
index 483308a..0000000
--- a/snap/local/helper-go/go.sum
+++ /dev/null
@@ -1,6 +0,0 @@
-github.com/canonical/edgex-snap-hooks/v3 v3.0.0 h1:G2mX0c+QuzOoo3AJMmFFVwnxYBNASiaEaUgJiJ5+/48=
-github.com/canonical/edgex-snap-hooks/v3 v3.0.0/go.mod h1:qGZwprCZGZk2pA9BrleUtSrGrfHIaIz1356p8aqzuN4=
-github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
-github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
-github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
-gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
diff --git a/snap/local/helper-go/install.go b/snap/local/helper-go/install.go
deleted file mode 100644
index 661fc05..0000000
--- a/snap/local/helper-go/install.go
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Copyright (C) 2022 Canonical Ltd
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
- *  in compliance with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License
- * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
- * or implied. See the License for the specific language governing permissions and limitations under
- * the License.
- *
- * SPDX-License-Identifier: Apache-2.0'
- */
-
-package main
-
-import (
-	hooks "github.com/canonical/edgex-snap-hooks/v3"
-	"github.com/canonical/edgex-snap-hooks/v3/env"
-	"github.com/canonical/edgex-snap-hooks/v3/log"
-	"github.com/canonical/edgex-snap-hooks/v3/snapctl"
-)
-
-// installConfig copies all config files from $SNAP to $SNAP_DATA
-func installConfig() error {
-	path := "/config/" + app + "/res"
-
-	err := hooks.CopyDir(
-		env.Snap+path,
-		env.SnapData+path)
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-func install() {
-	log.SetComponentName("install")
-
-	// Install default config files only if no config provider is connected
-	isConnected, err := snapctl.IsConnected(app + "-config").Run()
-	if err != nil {
-		log.Fatalf("Error checking interface connection: %s", err)
-	}
-	if !isConnected {
-		if err := installConfig(); err != nil {
-			log.Fatalf("Error installing config files: %s", err)
-		}
-	}
-}
diff --git a/snap/local/helper-go/main.go b/snap/local/helper-go/main.go
deleted file mode 100644
index baeb4f9..0000000
--- a/snap/local/helper-go/main.go
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (C) 2022 Canonical Ltd
- *
- *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
- *  in compliance with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software distributed under the License
- * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
- * or implied. See the License for the specific language governing permissions and limitations under
- * the License.
- * SPDX-License-Identifier: Apache-2.0'
- */
-
-package main
-
-import (
-	"os"
-)
-
-const app = "device-modbus"
-
-func main() {
-	subCommand := os.Args[1]
-	switch subCommand {
-	case "install":
-		install()
-	case "configure":
-		configure()
-	default:
-		panic("Unknown subcommand: " + subCommand)
-	}
-}
diff --git a/snap/snapcraft.yaml b/snap/snapcraft.yaml
deleted file mode 100644
index 215db45..0000000
--- a/snap/snapcraft.yaml
+++ /dev/null
@@ -1,94 +0,0 @@
-name: edgex-device-modbus
-base: core22
-license: Apache-2.0
-summary: EdgeX Modbus Device Service
-description: Refer to https://snapcraft.io/edgex-device-modbus
-adopt-info: device-modbus
-
-architectures:
-  - build-on: amd64
-  - build-on: arm64
-
-grade: stable
-confinement: strict
-
-# 1: edinburgh, geneva, hanoi
-# 2: ireland, jakarta, kamakura, levski
-# 3: minnesota
-epoch: 3
-
-slots:
-  edgex-secretstore-token:
-    interface: content
-    source:
-      write: [$SNAP_DATA/device-modbus]
-plugs:
-  device-modbus-config:
-    interface: content
-    target: $SNAP_DATA/config/device-modbus
-
-apps:
-  device-modbus:
-    command: bin/device-modbus --configDir $SNAP_DATA/config/device-modbus/res --configProvider --registry
-    command-chain:
-      - bin/source-env-file.sh
-    environment:
-      DEVICE_PROFILESDIR: "$SNAP_DATA/config/device-modbus/res/profiles"
-      DEVICE_DEVICESDIR: "$SNAP_DATA/config/device-modbus/res/devices"
-      SECRETSTORE_TOKENFILE: $SNAP_DATA/device-modbus/secrets-token.json
-    daemon: simple
-    install-mode: disable
-    plugs: [network, network-bind]
-
-parts:
-  helper-go:
-    source: snap/local/helper-go
-    plugin: make
-    build-snaps:
-      - go/1.20/stable
-    override-build: |
-      cd $CRAFT_PART_SRC
-      make build
-      install -DT ./helper-go $CRAFT_PART_INSTALL/bin/helper-go
-
-  device-modbus:
-    source: .
-    plugin: make
-    build-packages: 
-      - git
-    build-snaps:
-      - go/1.20/stable
-    override-build: |
-      cd $CRAFT_PART_SRC
-
-      if git describe ; then
-        VERSION=$(git describe --tags --abbrev=0 | sed 's/v//')
-      else
-        VERSION="0.0.0"
-      fi
-      
-      # set the version of the snap
-      craftctl set version=$VERSION
-      
-      # write version to file for the build
-      echo $VERSION > VERSION
-
-      make build
-
-      install -DT "./cmd/device-modbus" "$CRAFT_PART_INSTALL/bin/device-modbus"
-
-      RES=$CRAFT_PART_INSTALL/config/device-modbus/res/
-      mkdir -p $RES
-      cp -r cmd/res/* $RES
-      
-      DOC=$CRAFT_PART_INSTALL/usr/share/doc/device-modbus
-      mkdir -p $DOC
-      cp Attribution.txt $DOC/Attribution.txt
-      cp LICENSE $DOC/LICENSE
-
-  local-bin:
-    plugin: dump
-    source: snap/local/bin
-    organize:
-      source-env-file.sh: bin/source-env-file.sh
-


Test patch:
diff --git a/internal/driver/deviceclient_test.go b/internal/driver/deviceclient_test.go
index 00bef75..8a2c09f 100644
--- a/internal/driver/deviceclient_test.go
+++ b/internal/driver/deviceclient_test.go
@@ -209,17 +209,33 @@ func TestTransformDataBytesToResult_BOOL(t *testing.T) {
 			STARTING_ADDRESS: 10,
 		},
 	}
-	commandInfo, err := createCommandInfo(&req)
-	require.NoError(t, err)
-	dataBytes := []byte{1} // => 00000001
-	expected := true
 
-	commandValue, err := TransformDataBytesToResult(&req, dataBytes, commandInfo)
-	require.NoError(t, err)
-	result, err := commandValue.BoolValue()
-	require.NoError(t, err)
-
-	assert.Equal(t, expected, result)
+	tests := []struct {
+		name         string
+		primaryTable string
+		data         []byte
+		expected     bool
+	}{
+		{"transform true value from DISCRETES_INPUT", DISCRETES_INPUT, []byte{1}, true},
+		{"transform false value from DISCRETES_INPUT", DISCRETES_INPUT, []byte{0}, false},
+		{"transform true value from DISCRETE_INPUTS", DISCRETE_INPUTS, []byte{1}, true},
+		{"transform false value from DISCRETE_INPUTS", DISCRETE_INPUTS, []byte{0}, false},
+		{"transform true value from COILS", COILS, []byte{1}, true},
+		{"transform false value from COILS", COILS, []byte{0}, false},
+	}
+	for _, testCase := range tests {
+		t.Run(testCase.name, func(t *testing.T) {
+			req.Attributes[PRIMARY_TABLE] = testCase.primaryTable
+			commandInfo, err := createCommandInfo(&req)
+			require.NoError(t, err)
+			commandValue, err := TransformDataBytesToResult(&req, testCase.data, commandInfo)
+			require.NoError(t, err)
+			result, err := commandValue.BoolValue()
+			require.NoError(t, err)
+
+			assert.Equal(t, testCase.expected, result)
+		})
+	}
 }
 
 func TestTransformDataBytesToResult_RawType_INT16_ValueType_FLOAT32(t *testing.T) {