-
Notifications
You must be signed in to change notification settings - Fork 170
Fix: Add a temperature sensor demo #161
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
Open
aAAaqwq
wants to merge
1
commit into
kubeedge:master
Choose a base branch
from
aAAaqwq:feat-temperature-sensor-demo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+4,249
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| .PHONY: test build-webUI build-modbus build-hardware | ||
|
|
||
| test: deploy-hardware deploy-modbus deploy-webUI | ||
|
|
||
| clean: | ||
| kubectl delete -f ./webUI/resource | ||
| kubectl delete -f ./hardware/resource | ||
| make -C ./modbus clean | ||
|
|
||
| deploy-crds: | ||
| kubectl apply -f ./crds/temperature-model.yaml | ||
| kubectl apply -f ./crds/temperature-instance.yaml | ||
| deploy-webUI: | ||
| kubectl apply -f ./webUI/resource | ||
|
|
||
| deploy-hardware: | ||
| kubectl apply -f ./hardware/resource | ||
|
|
||
| deploy-modbus: | ||
| make -C ./modbus deploy | ||
|
|
||
| build-webUI: | ||
| docker build -t temperature-webui:v1.0 ./webUI | ||
| # kind load docker-image temperature-webui:v1.0 --nodes=kind-control-plane | ||
| build-modbus: | ||
| make -C ./modbus docker-build | ||
|
|
||
| build-hardware: | ||
| docker build -t temperature-sensor:v1.0 ./hardware | ||
| # kind load docker-image temperature-sensor:v1.0 --nodes=kind-worker | ||
|
|
||
| test-webUI: | ||
| go build -o main ./main.go | ||
| docker cp ./main kind-control-plane:./srv/temperature/main | ||
| docker cp ./static kind-control-plane:./srv/temperature/static |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # Temperature Sensor | ||
|
|
||
| ## Description | ||
| This project is used to demonstrate a specific instance of kubeedge. It provides a cloud-edge collaboration example of a temperature sensor. | ||
|
|
||
| ## Structure | ||
|  | ||
|
|
||
| ## Prerequisites | ||
| * Docker | ||
| * KubeEdge v1.21.0+ | ||
| ## Quick Start | ||
| ### 1. Clone the repository to local | ||
| ``` | ||
| git clone https://github.com/kubeedge/examples | ||
| cd ./examples/temperature-sensor-demo | ||
| ``` | ||
| ### 2. Deploy components | ||
|
|
||
| #### [2.1 Deploy hardware](hardware/README.md) ✅(At the edge) | ||
|
|
||
| ``` | ||
| make build-hardware | ||
| make deploy-hardware | ||
| ``` | ||
| #### Other options | ||
| - Through Physical device | ||
|
|
||
| - [Install modbus Slave](https://www.modbustools.com/modbus_slave.html) (only for Windows users) | ||
| ``` | ||
| - Create TCP/RTU device connection, mapper uses TCP protocol by default. | ||
| - Configure Modbus registers: | ||
| - Temperature register: holding register, address 4000, data type: uint16 | ||
| - Working status register: coil register, address 1001, data type: boolean | ||
| ``` | ||
| #### [2.2 Deploy modbus mapper](modbus/README.md) ✅(At the edge) | ||
| ``` | ||
| make build-modbus | ||
| make deploy-modbus | ||
| ``` | ||
| #### [2.3 Deploy webUI](webUI/README.md) ✅(At the cloud) | ||
| ``` | ||
| make build-webUI | ||
| make deploy-webUI | ||
| ``` | ||
| ### 3. Test the demo | ||
| #### 3.1 View the synchronization of the reported field in the twins field: | ||
| ``` | ||
| kubectl get device -o yaml | ||
| ``` | ||
|  | ||
| #### 3.2 View the result through the webUI | ||
|  | ||
|  | ||
|  | ||
|  | ||
|
|
||
|
|
||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| apiVersion: devices.kubeedge.io/v1beta1 | ||
| kind: Device | ||
| metadata: | ||
| name: temperature-instance | ||
| spec: | ||
| deviceModelRef: | ||
| name: temperature-model | ||
| protocol: | ||
| protocolName: modbus | ||
| # Custom protocol configuration | ||
| configData: | ||
| communicateMode: "TCP" # TCP/RTU | ||
| port: "5502" # replace the port with your modbus device port | ||
| slaveID: 1 | ||
| ip: "10.244.1.13" # 1.replace the ip with your modbus device ip | ||
|
|
||
| nodeName: "kind-worker" # 2.replace the nodeName with your edge node name | ||
| properties: | ||
| - name: temperature | ||
| visitors: | ||
| protocolName: modbus | ||
| configData: | ||
| dataType: "float" # Define the output format, consistent with the model type definition Enum::int/float/double/string/boolean/bytes | ||
| register: "HoldingRegister" # Register Type Enum::CoilRegister/DiscreteInputRegister/HoldingRegister/InputRegister | ||
| offset: 0 # Register offset | ||
| limit: 1 # Number of registers to read | ||
| scale: 0.1 # Scaling factor for temperature values | ||
| isSwap: false # Whether to swap bytes | ||
| isRegisterSwap: false # Whether to swap registers | ||
| max: 100.0 # Maximum value for temperature | ||
| min: 1.0 # Minimum value for temperature | ||
| collectCycle: 10000 | ||
| reportCycle: 10000 | ||
| reportToCloud: true | ||
| # Enabling the push function requires deploying related services,like mosquitto broker | ||
| # pushMethod: | ||
| # mqtt: | ||
| # topic: "current temperature" | ||
| # qos: 0 | ||
| # address: "tcp://172.18.0.3:31883" # replace the address with your mqtt broker address | ||
| # retained: false | ||
| - name: temperature-switch | ||
| collectCycle: 10000 | ||
| reportCycle: 10000 | ||
| reportToCloud: true | ||
| desired: | ||
| value: "1" | ||
| visitors: | ||
| protocolName: modbus | ||
| configData: | ||
| dataType: "int" | ||
| register: "CoilRegister" | ||
| offset: 0 | ||
| limit: 1 | ||
| scale: 1 | ||
| isSwap: false | ||
| isRegisterSwap: false | ||
| methods: | ||
| - name: SwitchControl | ||
| description: control the switch of the device | ||
| propertyNames: | ||
| - temperature-switch | ||
| - name: UpdateTemperature | ||
| description: update the temperature of the device | ||
| propertyNames: | ||
| - temperature |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| apiVersion: devices.kubeedge.io/v1beta1 | ||
| kind: DeviceModel | ||
| metadata: | ||
| name: temperature-model | ||
| namespace: default | ||
| spec: | ||
| protocol: modbus | ||
| properties: | ||
| - name: temperature | ||
| description: actual temperature | ||
| type: FLOAT # ENUM: INT,FLOAT,DOUBLE,STRING,BOOLEAN,BYTES | ||
| accessMode: ReadWrite | ||
| minimum: "0" | ||
| maximum: "100.0" | ||
| unit: "Celsius" | ||
| - name: temperature-switch | ||
| description: "the switch of device 0:off,1:on" | ||
| type: INT | ||
| accessMode: ReadWrite |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| FROM golang:1.22.9-alpine3.19 AS builder | ||
|
|
||
| WORKDIR /app | ||
|
|
||
| COPY . . | ||
|
|
||
| RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o main ./main.go | ||
|
|
||
| FROM alpine:3.19 | ||
|
|
||
| COPY --from=builder /app/main . | ||
|
|
||
| EXPOSE 5502 | ||
|
|
||
| CMD ["./main"] | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Modbus Simulator | ||
|
|
||
| ## Description | ||
| This is a modbus simulator built using gomodbus dependencies, currently only supports TCP connections | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ### 1. Build the image | ||
| ``` | ||
| docker build -t temperature-sensor:v1.0 . | ||
| ``` | ||
| ### 2. Upload image to the edge node | ||
|
|
||
| ### 3. Deploy the simulator to the edge | ||
| ``` | ||
| kubectl apply -f ./resource/deploy.yaml | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module hardware | ||
|
|
||
| go 1.22.9 | ||
|
|
||
| require github.com/thinkgos/gomodbus v1.5.2 | ||
|
|
||
| require github.com/goburrow/serial v0.1.0 // indirect |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| github.com/goburrow/serial v0.1.0 h1:v2T1SQa/dlUqQiYIT8+Cu7YolfqAi3K96UmhwYyuSrA= | ||
| github.com/goburrow/serial v0.1.0/go.mod h1:sAiqG0nRVswsm1C97xsttiYCzSLBmUZ/VSlVLZJ8haA= | ||
| github.com/thinkgos/gomodbus v1.5.2 h1:zmFEzXILpCtBYB8FwcAuLnb8Rrr3NcdX8oukmyB9hwE= | ||
| github.com/thinkgos/gomodbus v1.5.2/go.mod h1:HX6B+W3xV/oKinltblD+aqr9LnQuh9mWT4kMul5hYvE= | ||
| github.com/thinkgos/timing v1.1.2/go.mod h1:zS1qzRn2ISTjI1xJc6AHNfWAvCtc7FylwWjt32L8qvo= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "log" | ||
|
|
||
| "github.com/thinkgos/gomodbus" | ||
| ) | ||
|
|
||
| func InitModbusSimulator(address string) { | ||
| srv := modbus.NewTCPServer() | ||
| srv.AddNodes( | ||
| modbus.NewNodeRegister(1, 0, 1, 0, 0, 0,0,0,1), | ||
| ) | ||
| defer srv.Close() | ||
| if err := srv.ListenAndServe(address); err != nil { | ||
| log.Fatalf("Failed to start modbus simulator: %v", err) | ||
| } | ||
| } | ||
| func main() { | ||
| InitModbusSimulator(":5502") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| apiVersion: apps/v1 | ||
| kind: Deployment | ||
| metadata: | ||
| name: temperature-sensor | ||
| spec: | ||
| replicas: 1 | ||
| selector: | ||
| matchLabels: | ||
| app: temperature-sensor | ||
| template: | ||
| metadata: | ||
| labels: | ||
| app: temperature-sensor | ||
| spec: | ||
| nodeName: kind-worker | ||
| containers: | ||
| - name: temperature-sensor | ||
| image: temperature-sensor:v1.0 | ||
| imagePullPolicy: IfNotPresent | ||
| resources: | ||
| limits: | ||
| cpu: 300m | ||
| memory: 500Mi | ||
| requests: | ||
| cpu: 100m | ||
| memory: 100Mi |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| FROM golang:1.22.9-alpine3.19 AS builder | ||
|
|
||
| WORKDIR /build | ||
|
|
||
| ENV GO111MODULE=on \ | ||
| GOPROXY=https://goproxy.cn,direct | ||
|
|
||
| COPY . . | ||
|
|
||
| RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o main ./cmd/main.go | ||
|
|
||
| FROM ubuntu:22.04 | ||
|
|
||
| RUN mkdir -p kubeedge | ||
|
|
||
| COPY --from=builder /build/main kubeedge/ | ||
| COPY ./config.yaml kubeedge/ | ||
|
|
||
| WORKDIR kubeedge | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| FROM golang:1.22.9-bullseye AS builder | ||
|
|
||
| WORKDIR /build | ||
|
|
||
| ENV GO111MODULE=on \ | ||
| GOPROXY=https://goproxy.cn,direct | ||
|
|
||
| COPY . . | ||
|
|
||
| RUN apt-get update && \ | ||
| apt-get install -y bzip2 curl upx-ucl gcc-aarch64-linux-gnu libc6-dev-arm64-cross gcc-arm-linux-gnueabi libc6-dev-armel-cross libva-dev libva-drm2 libx11-dev libvdpau-dev libxext-dev libsdl1.2-dev libxcb1-dev libxau-dev libxdmcp-dev yasm | ||
|
|
||
| RUN curl -sLO https://ffmpeg.org/releases/ffmpeg-4.1.6.tar.bz2 && \ | ||
| tar -jx --strip-components=1 -f ffmpeg-4.1.6.tar.bz2 && \ | ||
| ./configure && make && \ | ||
| make install | ||
|
|
||
| RUN GOOS=linux go build -o main cmd/main.go | ||
|
|
||
| FROM ubuntu:22.04 | ||
|
|
||
| RUN mkdir -p kubeedge | ||
|
|
||
| RUN apt-get update && \ | ||
| apt-get install -y bzip2 curl upx-ucl gcc-aarch64-linux-gnu libc6-dev-arm64-cross gcc-arm-linux-gnueabi libc6-dev-armel-cross libva-dev libva-drm2 libx11-dev libvdpau-dev libxext-dev libsdl1.2-dev libxcb1-dev libxau-dev libxdmcp-dev yasm | ||
|
|
||
| RUN curl -sLO https://ffmpeg.org/releases/ffmpeg-4.1.6.tar.bz2 && \ | ||
| tar -jx --strip-components=1 -f ffmpeg-4.1.6.tar.bz2 && \ | ||
| ./configure && make && \ | ||
| make install | ||
|
|
||
| COPY --from=builder /build/main kubeedge/ | ||
| COPY ./config.yaml kubeedge/ | ||
|
|
||
| WORKDIR kubeedge |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This Dockerfile has a couple of issues that should be addressed:
GOPROXY=https://goproxy.cn,direct, which might not be accessible or desirable for all users. It's better to make this configurable via a build argument.ubuntu:22.04, while the builder usesalpine. Using a large base image like Ubuntu when a smaller one like Alpine is available (and used in the builder stage) unnecessarily increases the final image size. Consider usingalpinefor the final stage as well to keep the image small.