-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: wbc6080 <[email protected]>
- Loading branch information
Showing
16 changed files
with
324 additions
and
108 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
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,35 @@ | ||
FROM golang:1.20.10-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:18.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 |
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
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
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
108 changes: 108 additions & 0 deletions
108
mappers/kubeedge-v1.17.0/onvif-mapper/data/dbmethod/mysql/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,108 @@ | ||
/* | ||
Copyright 2024 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 mysql | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
"k8s.io/klog/v2" | ||
|
||
"github.com/kubeedge/mapper-framework/pkg/common" | ||
) | ||
|
||
var ( | ||
DB *sql.DB | ||
) | ||
|
||
type DataBaseConfig struct { | ||
MySQLClientConfig *MySQLClientConfig `json:"mysqlClientConfig"` | ||
} | ||
|
||
type MySQLClientConfig struct { | ||
Addr string `json:"addr,omitempty"` | ||
Database string `json:"database,omitempty"` | ||
UserName string `json:"userName,omitempty"` | ||
} | ||
|
||
func NewDataBaseClient(config json.RawMessage) (*DataBaseConfig, error) { | ||
configdata := new(MySQLClientConfig) | ||
err := json.Unmarshal(config, configdata) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &DataBaseConfig{ | ||
MySQLClientConfig: configdata, | ||
}, nil | ||
} | ||
|
||
func (d *DataBaseConfig) InitDbClient() error { | ||
password := os.Getenv("PASSWORD") | ||
usrName := d.MySQLClientConfig.UserName | ||
addr := d.MySQLClientConfig.Addr | ||
dataBase := d.MySQLClientConfig.Database | ||
dataSourceName := fmt.Sprintf("%s:%s@tcp(%s)/%s", usrName, password, addr, dataBase) | ||
var err error | ||
DB, err = sql.Open("mysql", dataSourceName) | ||
if err != nil { | ||
return fmt.Errorf("connection to %s of mysql faild with err:%v", dataBase, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (d *DataBaseConfig) CloseSession() { | ||
err := DB.Close() | ||
if err != nil { | ||
klog.Errorf("close mysql failed with err:%v", err) | ||
} | ||
} | ||
|
||
func (d *DataBaseConfig) AddData(data *common.DataModel) error { | ||
deviceName := data.DeviceName | ||
propertyName := data.PropertyName | ||
namespace := data.Namespace | ||
tableName := namespace + "/" + deviceName + "/" + propertyName | ||
datatime := time.Unix(data.TimeStamp/1e3, 0).Format("2006-01-02 15:04:05") | ||
|
||
createTable := fmt.Sprintf("CREATE TABLE IF NOT EXISTS `%s` (id INT AUTO_INCREMENT PRIMARY KEY, ts DATETIME NOT NULL,field TEXT)", tableName) | ||
_, err := DB.Exec(createTable) | ||
if err != nil { | ||
return fmt.Errorf("create tabe into mysql failed with err:%v", err) | ||
} | ||
|
||
stmt, err := DB.Prepare(fmt.Sprintf("INSERT INTO `%s` (ts,field) VALUES (?,?)", tableName)) | ||
if err != nil { | ||
return fmt.Errorf("prepare parament failed with err:%v", err) | ||
} | ||
defer func(stmt *sql.Stmt) { | ||
err := stmt.Close() | ||
if err != nil { | ||
klog.Errorf("close mysql's statement failed with err:%v", err) | ||
} | ||
}(stmt) | ||
_, err = stmt.Exec(datatime, data.Value) | ||
if err != nil { | ||
return fmt.Errorf("insert data into msyql failed with err:%v", err) | ||
} | ||
|
||
return nil | ||
} |
73 changes: 73 additions & 0 deletions
73
mappers/kubeedge-v1.17.0/onvif-mapper/data/dbmethod/mysql/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 2024 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 mysql | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"k8s.io/klog/v2" | ||
|
||
"github.com/kubeedge/onvif/driver" | ||
"github.com/kubeedge/mapper-framework/pkg/common" | ||
) | ||
|
||
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.MySQLClientConfig) | ||
if err != nil { | ||
klog.Errorf("new database client error: %v", err) | ||
return | ||
} | ||
err = dbConfig.InitDbClient() | ||
if err != nil { | ||
klog.Errorf("init redis 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) | ||
if err != nil { | ||
klog.Errorf("mysql database add data error: %v", err) | ||
return | ||
} | ||
case <-ctx.Done(): | ||
dbConfig.CloseSession() | ||
return | ||
} | ||
} | ||
}() | ||
} |
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
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
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
Oops, something went wrong.