Skip to content

Commit e233224

Browse files
committed
init proj
1 parent fd24f1b commit e233224

File tree

15 files changed

+2487
-1
lines changed

15 files changed

+2487
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea/*

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
SRC = $(wildcard *.go)
2+
GITVISION = `git describe`"-"`git rev-parse --short HEAD`
3+
all:simluatedev
4+
simluatedev:$(SRC)
5+
go build -o azuretask -x -ldflags " -w -s -X main.BuildVersion=$(GITVISION)" $^
6+
arm:
7+
8+
GOARM=7 GOARCH=arm64 GOOS=linux go build -x -ldflags "-w -s -X main.BuildVersion=$(GITVISION)" -o simluatedev $^
9+
10+
11+
clean:
12+
rm -rvf simluatedev
13+
14+

README.md

Lines changed: 1490 additions & 1 deletion
Large diffs are not rendered by default.

blog.go

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"os"
7+
"runtime"
8+
"time"
9+
)
10+
11+
var debuglevel = 4
12+
13+
const (
14+
//FatalLevel fatalerror
15+
FatalLevel = 0
16+
//ErrorLevel error happen
17+
ErrorLevel = 1
18+
//WarnLevel just warn something wrong
19+
WarnLevel = 2
20+
//InfoLevel info what happen
21+
InfoLevel = 3
22+
//DebugLevel for debug
23+
DebugLevel = 4
24+
)
25+
26+
var (
27+
defaultLogLevel = DebugLevel
28+
errLevels = []string{"FATAL", "ERROR", "WARN", "INFO", "DEBUG"}
29+
)
30+
31+
var blog = log.New(os.Stdout, "", 0)
32+
var logFlag = "azuretask"
33+
34+
func output(level string, logFlag string, format string, v ...interface{}) {
35+
pc, file, line, _ := runtime.Caller(2)
36+
short := file
37+
38+
for i := len(file) - 1; i > 0; i-- {
39+
if file[i] == '/' {
40+
short = file[i+1:]
41+
break
42+
}
43+
}
44+
f := runtime.FuncForPC(pc)
45+
fn := f.Name()
46+
47+
for i := len(fn) - 1; i > 0; i-- {
48+
if fn[i] == '.' {
49+
fn = fn[i+1:]
50+
break
51+
}
52+
}
53+
54+
if format == "" {
55+
blog.Printf("%v|%v|%v|%v|%v()|%v|%v", now(), level, logFlag, short, fn, line, fmt.Sprintln(v...))
56+
} else {
57+
blog.Printf("%v|%v|%v|%v|%v()|%v|%v", now(), level, logFlag, short, fn, line, fmt.Sprintf(format, v...))
58+
}
59+
60+
}
61+
func now() string {
62+
t := time.Now()
63+
return fmt.Sprintf("%v-%02d-%v %02d:%02d:%02d",
64+
t.Year(), t.Month(), t.Day(),
65+
t.Hour(), t.Minute(), t.Second())
66+
}
67+
68+
//Debug for debug lowest level
69+
func Debug(v ...interface{}) {
70+
if debuglevel >= DebugLevel {
71+
output("DEBUG", logFlag, "", v...)
72+
}
73+
}
74+
75+
//Debugln same as Debug
76+
func Debugln(v ...interface{}) {
77+
if debuglevel >= DebugLevel {
78+
output("DEBUG", logFlag, "", v...)
79+
}
80+
}
81+
82+
//Debugf same as Debug
83+
func Debugf(format string, v ...interface{}) {
84+
if debuglevel >= DebugLevel {
85+
output("DEBUG", logFlag, format, v...)
86+
}
87+
}
88+
89+
//Info info level
90+
func Info(v ...interface{}) {
91+
if debuglevel >= InfoLevel {
92+
output("INFO", logFlag, "", v...)
93+
}
94+
}
95+
96+
//Infof info level
97+
func Infof(format string, v ...interface{}) {
98+
if debuglevel >= InfoLevel {
99+
output("INFO", logFlag, format, v...)
100+
}
101+
}
102+
103+
//Warning warning level
104+
func Warning(v ...interface{}) {
105+
if debuglevel >= WarnLevel {
106+
output("WARN", logFlag, "", v...)
107+
}
108+
}
109+
110+
//Warningln warning level
111+
func Warningln(v ...interface{}) {
112+
if debuglevel >= WarnLevel {
113+
output("WARN", logFlag, "", v...)
114+
}
115+
}
116+
117+
//Warningf warning level
118+
func Warningf(format string, v ...interface{}) {
119+
if debuglevel >= WarnLevel {
120+
output("WARN", logFlag, format, v...)
121+
}
122+
}
123+
124+
//Error error level
125+
func Error(v ...interface{}) {
126+
if debuglevel >= ErrorLevel {
127+
output("ERROR", logFlag, "", v...)
128+
}
129+
}
130+
131+
//Errorln error level
132+
func Errorln(v ...interface{}) {
133+
if debuglevel >= ErrorLevel {
134+
output("ERROR", logFlag, "", v...)
135+
}
136+
}
137+
138+
//Errorf error level
139+
func Errorf(format string, v ...interface{}) {
140+
if debuglevel >= ErrorLevel {
141+
output("ERROR", logFlag, format, v...)
142+
}
143+
}
144+
145+
//Fatal fatal error happen
146+
func Fatal(v ...interface{}) {
147+
if debuglevel >= FatalLevel {
148+
output("FATAL", logFlag, "", v...)
149+
}
150+
}
151+
152+
//Fatalf same as fatal
153+
func Fatalf(format string, v ...interface{}) {
154+
if debuglevel >= FatalLevel {
155+
output("FATAL", logFlag, format, v...)
156+
}
157+
}
158+
159+
//Logger init log func
160+
func Logger(filename string, level string) *os.File {
161+
var f *os.File
162+
if filename != "" {
163+
var err error
164+
f, err = os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
165+
if err != nil {
166+
log.Fatal(err)
167+
}
168+
blog = log.New(f, "", 0)
169+
}
170+
171+
for k, v := range errLevels {
172+
if v == level {
173+
debuglevel = k
174+
Info("debuglevel:", v)
175+
}
176+
}
177+
178+
return f
179+
}
180+
181+
func setDebugLevel(level string) {
182+
for k, v := range errLevels {
183+
if v == level {
184+
debuglevel = k
185+
Fatal("debuglevel:", v)
186+
}
187+
}
188+
}

common.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"crypto/tls"
6+
"io/ioutil"
7+
"net"
8+
"net/http"
9+
"time"
10+
)
11+
12+
func getHttpClient() (client *http.Client) {
13+
transport := http.Transport{
14+
Dial: dialTimeout,
15+
DisableKeepAlives: true,
16+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
17+
}
18+
client = &http.Client{
19+
Transport: &transport,
20+
}
21+
return
22+
}
23+
24+
func dialTimeout(network, addr string) (net.Conn, error) {
25+
c, err := net.DialTimeout(network, addr, time.Second*50) //设置建立连接超时
26+
if err != nil {
27+
return nil, err
28+
}
29+
err = c.SetDeadline(time.Now().Add(10 * time.Second))
30+
if err != nil {
31+
return nil, err
32+
} //设置发送接收数据超时
33+
return c, nil
34+
}
35+
36+
func HttpRequest(method, url string, body []byte, header map[string]string) (resp *http.Response, err error) {
37+
newReq, _ := http.NewRequest(method, url, bytes.NewReader(body))
38+
39+
for k, v := range header {
40+
newReq.Header.Set(k, v)
41+
}
42+
43+
client := getHttpClient()
44+
resp, err = client.Do(newReq)
45+
if err != nil {
46+
Error("new http req err.", err.Error())
47+
return
48+
}
49+
return
50+
}
51+
52+
func ParseResponseString(response *http.Response) (string, error) {
53+
//var result map[string]interface{}
54+
body, err := ioutil.ReadAll(response.Body) // response.Body 是一个数据流
55+
return string(body), err // 将 io数据流转换为string类型返回!
56+
}

config.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
"time"
8+
9+
"toml"
10+
)
11+
12+
const serviceName = "simluatedev"
13+
14+
var BuildVersion = "default"
15+
16+
var Config struct {
17+
DeviceProvisioning struct {
18+
PolicyName string
19+
Method string
20+
IdScope string
21+
OrderId string
22+
DevEndPoint string
23+
RegBaseUrl string
24+
RegStatusBaseUrl string
25+
}
26+
27+
CommonKeys struct {
28+
PrimaryKey string
29+
SecondaryKey string
30+
RegistrationId string
31+
}
32+
33+
DataUploadConf struct {
34+
IdentityId string
35+
Pid string
36+
MsgType string
37+
FirmwareVersion string
38+
TimeVal time.Duration
39+
}
40+
41+
DirectMethodConf struct {
42+
MethodName string
43+
SuccessStatusCode int
44+
MethodJsonPath string
45+
}
46+
}
47+
48+
func LoadCfg() {
49+
showVersion := flag.Bool("v", false, "show version")
50+
configFilePath := flag.String("c", "", "configure file path")
51+
configFileTest := flag.Bool("t", false, "configure file test")
52+
flag.Parse()
53+
if *showVersion {
54+
fmt.Println(BuildVersion)
55+
os.Exit(0)
56+
}
57+
Info("cfg path :", *configFilePath)
58+
if *configFilePath == "" {
59+
*configFilePath = "config.toml"
60+
}
61+
62+
var err error
63+
if _, err = toml.DecodeFile(*configFilePath, &Config); err != nil {
64+
Fatal("toml fail to parse file :", err)
65+
os.Exit(-1)
66+
}
67+
68+
Infof("%+v", Config)
69+
70+
if *configFileTest {
71+
fmt.Printf("configuration file %s test is successful\n", *configFilePath)
72+
os.Exit(0)
73+
}
74+
}

config.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# DPS provisioning with symmetric key
2+
[DeviceProvisioning]
3+
Method = "symmetric_key"
4+
IdScope = "0cn000656B6"
5+
OrderId = "ce190381-3dwef-4d0jc-94h4e-ba7gdfb6c4a161d"
6+
PolicyName = "registration"
7+
DevEndPoint = "global.azure-devices-provisioning.cn"
8+
RegBaseUrl = "%s/%s/registrations/%s/register?api-version=2021-06-01"
9+
RegStatusBaseUrl = "%s/%s/registrations/%s?api-version=2021-06-01"
10+
11+
[CommonKeys]
12+
PrimaryKey = "3k/K7JPeXrG+abUKsdglDkJbBqaB1D1PO74G/AqY4daC2hXMvdAl1nW2sfYfr7UGNvba2HRNhlUi9egqUbj6Hgbc1dg=="
13+
SecondaryKey = "SU3hAUJtnchjaz/DsdfHhyChbIUKdQq4nEFBPwEz5bg9sgm8byA6BzTso0sfGaKeLasJEBnRe1KnInYrooCgfkGKxg=="
14+
RegistrationId = "20220623device07" #设备mac
15+
16+
[DataUploadConf]
17+
IdentityId = "0a527086c5gdfdb1d8b7a76cdfg5137020f801"
18+
Pid = "0000000000000000000000001ecdgc0000"
19+
MsgType = "dataUpload"
20+
FirmwareVersion = "1.0"
21+
TimeVal = 300 # 周期上报数据 默认300秒(1分钟)
22+
23+
[DirectMethodConf]
24+
MethodName = "TestMethod" # 调用方法名称
25+
SuccessStatusCode = 200 # 调用方法名称返回code
26+
MethodJsonPath = "method.json" # # 调用方法返回参数配置文件

0 commit comments

Comments
 (0)