-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add onvif mapper based mapper-framework
Signed-off-by: wbc6080 <[email protected]>
- Loading branch information
Showing
29 changed files
with
2,651 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,20 @@ | ||
FROM golang:1.20.10-alpine3.18 AS builder | ||
|
||
WORKDIR /build | ||
|
||
ENV GO111MODULE=on \ | ||
GOPROXY=https://goproxy.cn,direct | ||
|
||
COPY . . | ||
|
||
RUN CGO_ENABLED=0 GOOS=linux go build -tags Nostream -o main cmd/main.go # If you want to enable stream data processing, you need to change the tag to stream. | ||
|
||
|
||
FROM ubuntu:18.04 | ||
|
||
RUN mkdir -p kubeedge | ||
|
||
COPY --from=builder /build/main kubeedge/ | ||
COPY ./config.yaml kubeedge/ | ||
|
||
WORKDIR kubeedge |
This file contains 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,34 @@ | ||
SHELL := /bin/bash | ||
|
||
curr_dir := $(patsubst %/,%,$(dir $(abspath $(lastword $(MAKEFILE_LIST))))) | ||
rest_args := $(wordlist 2, $(words $(MAKECMDGOALS)), $(MAKECMDGOALS)) | ||
$(eval $(rest_args):;@:) | ||
|
||
help: | ||
# | ||
# Usage: | ||
# make generate : generate a mapper based on a template. | ||
# make mapper {mapper-name} <action> <parameter>: execute mapper building process. | ||
# | ||
# Actions: | ||
# - mod, m : download code dependencies. | ||
# - lint, l : verify code via go fmt and `golangci-lint`. | ||
# - build, b : compile code. | ||
# - package, p : package docker image. | ||
# - clean, c : clean output binary. | ||
# | ||
# Parameters: | ||
# ARM : true or undefined | ||
# ARM64 : true or undefined | ||
# | ||
# Example: | ||
# - make mapper modbus ARM64=true : execute `build` "modbus" mapper for ARM64. | ||
# - make mapper modbus test : execute `test` "modbus" mapper. | ||
@echo | ||
|
||
make_rules := $(shell ls $(curr_dir)/hack/make-rules | sed 's/.sh//g') | ||
$(make_rules): | ||
@$(curr_dir)/hack/make-rules/$@.sh $(rest_args) | ||
|
||
.DEFAULT_GOAL := help | ||
.PHONY: $(make_rules) build test package |
This file contains 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,44 @@ | ||
This mapper is for the ONVIF IP camera. For resource-limited, it's only be tested with the HIKVISION camera. | ||
|
||
Supported functions: | ||
- Save frame. You can define it in device-instance.yaml and save the rtsp stream as video frame files. | ||
- Save video. You can define it in device-instance.yaml and save the rtsp stream as video files. | ||
|
||
steps: | ||
|
||
- Install the dependences: | ||
``` | ||
sudo apt-get update && | ||
sudo apt-get install -y 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 | ||
``` | ||
and install ffmpeg with commond: | ||
``` | ||
sudo 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 && | ||
sudo make install | ||
``` | ||
This may take about 5 minutes to download and build all dependencies | ||
- Compile mapper project: | ||
There are currently two ways to compile and run the mapper project, namely building the mapper image or locally compile. | ||
1. Build onvif mapper image | ||
``` | ||
docker build -t [YOUR MAPPER IMAGE NAME] . | ||
``` | ||
After successfully building the onvif mapper image, you can deploy the mapper in the cluster through deployment or other methods. | ||
A sample configuration file for mapper deployment is provided in the **resource** directory. | ||
2. locally compile | ||
If you want to debug locally first, you can also compile and run the mapper code directly: | ||
``` | ||
go run -tags stream cmd/main.go --v <log level,like 3> --config-file <path to config yaml> | ||
``` | ||
- Build and submit the device yaml file: | ||
Build the device-instance and device-model configuration files according to the characteristics of the user | ||
edge onvif device, and execute the following commands to submit to the kubeedge cluster: | ||
``` | ||
kubectl apply -f <path to device model or device instance yaml> | ||
``` | ||
An example device-model and device-instance configuration file for onvif device is provided in the resource directory. |
This file contains 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,62 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"os" | ||
|
||
"k8s.io/klog/v2" | ||
|
||
"github.com/kubeedge/mapper-framework/pkg/common" | ||
"github.com/kubeedge/mapper-framework/pkg/config" | ||
"github.com/kubeedge/mapper-framework/pkg/grpcclient" | ||
"github.com/kubeedge/mapper-framework/pkg/grpcserver" | ||
"github.com/kubeedge/mapper-framework/pkg/httpserver" | ||
"github.com/kubeedge/onvif/device" | ||
) | ||
|
||
func main() { | ||
var err error | ||
var c *config.Config | ||
|
||
klog.InitFlags(nil) | ||
defer klog.Flush() | ||
|
||
if c, err = config.Parse(); err != nil { | ||
klog.Fatal(err) | ||
os.Exit(1) | ||
} | ||
klog.Infof("config: %+v", c) | ||
|
||
klog.Infoln("Mapper will register to edgecore") | ||
deviceList, deviceModelList, err := grpcclient.RegisterMapper(true) | ||
if err != nil { | ||
klog.Fatal(err) | ||
} | ||
klog.Infoln("Mapper register finished") | ||
|
||
panel := device.NewDevPanel() | ||
err = panel.DevInit(deviceList, deviceModelList) | ||
if err != nil && !errors.Is(err, device.ErrEmptyData) { | ||
klog.Fatal(err) | ||
} | ||
klog.Infoln("devInit finished") | ||
go panel.DevStart() | ||
|
||
// start http server | ||
httpServer := httpserver.NewRestServer(panel, c.Common.HTTPPort) | ||
go httpServer.StartServer() | ||
|
||
// start grpc server | ||
grpcServer := grpcserver.NewServer( | ||
grpcserver.Config{ | ||
SockPath: c.GrpcServer.SocketPath, | ||
Protocol: common.ProtocolCustomized, | ||
}, | ||
panel, | ||
) | ||
defer grpcServer.Stop() | ||
if err = grpcServer.Start(); err != nil { | ||
klog.Fatal(err) | ||
} | ||
|
||
} |
This file contains 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,9 @@ | ||
grpc_server: | ||
socket_path: /etc/kubeedge/onvif.sock | ||
common: | ||
name: Onvif-mapper | ||
version: v1.13.0 | ||
api_version: v1.0.0 | ||
protocol: onvif # TODO add your protocol name | ||
address: 127.0.0.1 | ||
edgecore_sock: /etc/kubeedge/dmi.sock |
76 changes: 76 additions & 0 deletions
76
mappers/kubeedge-v1.17.0/onvif-mapper/data/dbmethod/influxdb2/client.go
This file contains 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,76 @@ | ||
package influxdb2 | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"os" | ||
"time" | ||
|
||
"k8s.io/klog/v2" | ||
|
||
influxdb2 "github.com/influxdata/influxdb-client-go/v2" | ||
"github.com/kubeedge/mapper-framework/pkg/common" | ||
) | ||
|
||
type DataBaseConfig struct { | ||
Influxdb2ClientConfig *Influxdb2ClientConfig `json:"influxdb2ClientConfig,omitempty"` | ||
Influxdb2DataConfig *Influxdb2DataConfig `json:"influxdb2DataConfig,omitempty"` | ||
} | ||
|
||
type Influxdb2ClientConfig struct { | ||
Url string `json:"url,omitempty"` | ||
Org string `json:"org,omitempty"` | ||
Bucket string `json:"bucket,omitempty"` | ||
} | ||
|
||
type Influxdb2DataConfig struct { | ||
Measurement string `json:"measurement,omitempty"` | ||
Tag map[string]string `json:"tag,omitempty"` | ||
FieldKey string `json:"fieldKey,omitempty"` | ||
} | ||
|
||
func NewDataBaseClient(clientConfig json.RawMessage, dataConfig json.RawMessage) (*DataBaseConfig, error) { | ||
// parse influx database config data | ||
influxdb2ClientConfig := new(Influxdb2ClientConfig) | ||
influxdb2DataConfig := new(Influxdb2DataConfig) | ||
err := json.Unmarshal(clientConfig, influxdb2ClientConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = json.Unmarshal(dataConfig, influxdb2DataConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &DataBaseConfig{ | ||
Influxdb2ClientConfig: influxdb2ClientConfig, | ||
Influxdb2DataConfig: influxdb2DataConfig, | ||
}, nil | ||
} | ||
|
||
func (d *DataBaseConfig) InitDbClient() influxdb2.Client { | ||
var usrtoken string | ||
usrtoken = os.Getenv("TOKEN") | ||
client := influxdb2.NewClient(d.Influxdb2ClientConfig.Url, usrtoken) | ||
|
||
return client | ||
} | ||
|
||
func (d *DataBaseConfig) CloseSession(client influxdb2.Client) { | ||
client.Close() | ||
} | ||
|
||
func (d *DataBaseConfig) AddData(data *common.DataModel, client influxdb2.Client) error { | ||
// write device data to influx database | ||
writeAPI := client.WriteAPIBlocking(d.Influxdb2ClientConfig.Org, d.Influxdb2ClientConfig.Bucket) | ||
p := influxdb2.NewPoint(d.Influxdb2DataConfig.Measurement, | ||
d.Influxdb2DataConfig.Tag, | ||
map[string]interface{}{d.Influxdb2DataConfig.FieldKey: data.Value}, | ||
time.Now()) | ||
// write point immediately | ||
err := writeAPI.WritePoint(context.Background(), p) | ||
if err != nil { | ||
klog.V(4).Info("Exit AddData") | ||
return err | ||
} | ||
return nil | ||
} |
73 changes: 73 additions & 0 deletions
73
mappers/kubeedge-v1.17.0/onvif-mapper/data/dbmethod/influxdb2/handler.go
This file contains 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,73 @@ | ||
/* | ||
Copyright 2023 The KubeEdge Authors. | ||
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. | ||
*/ | ||
|
||
package influxdb2 | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"k8s.io/klog/v2" | ||
|
||
"github.com/kubeedge/mapper-framework/pkg/common" | ||
"github.com/kubeedge/onvif/driver" | ||
) | ||
|
||
func DataHandler(ctx context.Context, twin *common.Twin, client *driver.CustomizedClient, visitorConfig *driver.VisitorConfig, dataModel *common.DataModel) { | ||
dbConfig, err := NewDataBaseClient(twin.Property.PushMethod.DBMethod.DBConfig.Influxdb2ClientConfig, twin.Property.PushMethod.DBMethod.DBConfig.Influxdb2DataConfig) | ||
if err != nil { | ||
klog.Errorf("new database client error: %v", err) | ||
return | ||
} | ||
dbClient := dbConfig.InitDbClient() | ||
if err != nil { | ||
klog.Errorf("init database client err: %v", err) | ||
return | ||
} | ||
reportCycle := time.Duration(twin.Property.ReportCycle) | ||
if reportCycle == 0 { | ||
reportCycle = common.DefaultReportCycle | ||
} | ||
ticker := time.NewTicker(reportCycle) | ||
go func() { | ||
for { | ||
select { | ||
case <-ticker.C: | ||
deviceData, err := client.GetDeviceData(visitorConfig) | ||
if err != nil { | ||
klog.Errorf("publish error: %v", err) | ||
continue | ||
} | ||
sData, err := common.ConvertToString(deviceData) | ||
if err != nil { | ||
klog.Errorf("Failed to convert publish method data : %v", err) | ||
continue | ||
} | ||
dataModel.SetValue(sData) | ||
dataModel.SetTimeStamp() | ||
|
||
err = dbConfig.AddData(dataModel, dbClient) | ||
if err != nil { | ||
klog.Errorf("influx database add data error: %v", err) | ||
return | ||
} | ||
case <-ctx.Done(): | ||
dbConfig.CloseSession(dbClient) | ||
return | ||
} | ||
} | ||
}() | ||
} |
Oops, something went wrong.