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: Add device up/down detection #1672

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

FelixTing
Copy link
Member

@FelixTing FelixTing commented Jan 7, 2025

fix: #1665

PR Checklist

Please check if your PR fulfills the following requirements:

  • I am not introducing a breaking change (if you are, flag in conventional commit message with BREAKING CHANGE: describing the break)
  • 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?)
    feat: Add device up/down detection edgex-docs#1407

Testing Instructions

Run Edgex stack using Edgex compose-builder

make run no-secty modbus-sim

Run device-modbus with this version of the device sdk and the following configuration

Writable:
  LogLevel: INFO

Service:
  Host: localhost
  Port: 59901
  StartupMsg: device modbus started

MessageBus:
  Optional:
    ClientId: device-modbus

Device:
  ProfilesDir: ""
  DevicesDir: ""
  AllowedFails: 1
  DeviceDownTimeout: 10

Upload device profile and onboard a device

# profile sample: https://github.com/edgexfoundry/device-modbus-go/blob/main/cmd/res/profiles/modbus.test.device.profile.yml 
curl http://localhost:59881/api/v3/deviceprofile/uploadfile -F "file=@modbus.test.device.profile.yml"
curl http://localhost:59881/api/v3/device -H "Content-Type:application/json" -X POST \
  -d '[
        {
            "apiVersion" : "v3",
            "device": {
               "name" :"Modbus-TCP-Temperature-Sensor",
               "description":"This device is a product for monitoring the temperature via the ethernet",
               "labels":[ 
                  "Temperature",
                  "Modbus TCP"
               ],
               "serviceName": "device-modbus",
               "profileName": "Test-Device-Modbus-Profile",
               "protocols":{
                  "modbus-tcp":{
                     "Address" : "localhost",
                     "Port" : "1502",
                     "UnitID" : "1",
                     "Timeout" : "1",
                     "IdleTimeout" : "1"
                  }
               },
               "autoEvents":[ 
                  { 
                     "Interval":"5s",
                     "onChange":false,
                     "SourceName":"Temperature"
                  }
               ],
               "adminState":"UNLOCKED",
               "operatingState":"UP"
            }
        }
    ]'

Stop the modbus simulator and the following will be in device-modbus's log

level=INFO ts=2025-01-07T20:23:55.013483+08:00 app=device-modbus source=driver.go:137 msg="Read command failed. Cmd:Temperature err:dial tcp [::1]:1502: connect: connection refused \n"
level=INFO ts=2025-01-07T20:23:55.013616+08:00 app=device-modbus source=opstate.go:85 msg="Marking device Modbus-TCP-Temperature-Sensor non-operational"
level=WARN ts=2025-01-07T20:23:55.036664+08:00 app=device-modbus source=opstate.go:88 msg="Will retry device Modbus-TCP-Temperature-Sensor in 10 seconds"
level=ERROR ts=2025-01-07T20:23:55.036749+08:00 app=device-modbus source=executor.go:61 msg="AutoEvent - error occurs when reading resource Temperature: error reading Regex DeviceResource(s) Temperature for Modbus-TCP-Temperature-Sensor -> dial tcp [::1]:1502: connect: connection refused"

level=INFO ts=2025-01-07T20:24:05.037492+08:00 app=device-modbus source=opstate.go:31 msg="Checking operational state for device: Modbus-TCP-Temperature-Sensor"
level=INFO ts=2025-01-07T20:24:05.040394+08:00 app=device-modbus source=driver.go:137 msg="Read command failed. Cmd:SwitchA err:dial tcp [::1]:1502: connect: connection refused \n"
level=ERROR ts=2025-01-07T20:24:05.040509+08:00 app=device-modbus source=opstate.go:60 msg="Device Modbus-TCP-Temperature-Sensor unresponsive: retrying in 10 seconds."

Restart the modbus simulator and the following will be in device-modbus's log

level=INFO ts=2025-01-07T20:24:25.04497+08:00 app=device-modbus source=opstate.go:31 msg="Checking operational state for device: Modbus-TCP-Temperature-Sensor"
2025/01/07 20:24:25 modbus: sending 00 08 00 00 00 06 01 01 00 00 00 01
2025/01/07 20:24:25 modbus: received 00 08 00 00 00 04 01 01 01 00
level=INFO ts=2025-01-07T20:24:25.08558+08:00 app=device-modbus source=modbusclient.go:91 msg="Modbus client GetValue's results [0]"
level=INFO ts=2025-01-07T20:24:25.086155+08:00 app=device-modbus source=driver.go:167 msg="Read command finished. Cmd:SwitchA, DeviceResource: SwitchA, Bool: false \n"
level=INFO ts=2025-01-07T20:24:25.086872+08:00 app=device-modbus source=opstate.go:56 msg="Device Modbus-TCP-Temperature-Sensor responsive: setting operational state to up."

Or if the device's operating state has already returned to UP before the check, the following will be in device-modbus's log

level=INFO ts=2025-01-07T20:29:09.03874+08:00 app=device-modbus source=opstate.go:31 msg="Checking operational state for device: Modbus-TCP-Temperature-Sensor"
level=INFO ts=2025-01-07T20:29:09.03894+08:00 app=device-modbus source=opstate.go:40 msg="Device Modbus-TCP-Temperature-Sensor is already operational. Exiting retry loop."

@FelixTing FelixTing force-pushed the issue-1665 branch 8 times, most recently from c8f10ab to c143ca8 Compare January 8, 2025 02:06
@FelixTing FelixTing marked this pull request as draft January 8, 2025 08:00
@FelixTing FelixTing force-pushed the issue-1665 branch 3 times, most recently from 742b5a3 to 6a9267d Compare January 8, 2025 10:59
@FelixTing FelixTing marked this pull request as ready for review January 8, 2025 11:01
@FelixTing FelixTing requested a review from cloudxxx8 January 8, 2025 11:01
Signed-off-by: FelixTing <felix@iotechsys.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add device up/down detection
1 participant