Commit ac4cf899 by 余嘉宇

first commit

parents
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Other
data/*
test/*
.idea
go.mod
go.sum
main
static
---")
resInfo := fmt.Sprintf("%v", infoList[1])
resInfo = strings.Replace(resInfo, "---
#### 3
测试
#### 软件架构
---
#### 2
测试2222
#### 软件架构
---
# SmartHomeNodeGO
#### 介绍
用go编写的可用于智能路由器Web端管理系统。
集成了 HTTP服务 和 TCP 连接服务。
可给智能家居设备提供服务。
可理解为边缘计算。
#### 特点
1. 无需安装,直接运行,所有服务会自动关联。
2. 包含了前端,后端,连接端,定时任务,文件存储。
3. 使用拓展硬盘或U盘,告别容量拮据且读写缓慢的的flash。
4. 尽量少的使用第三方包,确保能运行在大多数MIPS架构CPU的路由器上。
#### 软件架构
1. 后端使用GO开发,前端使用VueJs开发。
2. 后端框架使用gin,前端框架使用 vue-admin + element-ui。
3. 没有使用数据库,以json文件存储设备数据,简单使用了go-cache缓存。
#### 安装教程
* 如要修改本代码,请安装 GoLang 1.14+ 环境。
* 项目使用 go mod 管理包,使用以下命令:
``` bash
# 初始化 go mod 文件
go mod init smartHomeNode/v1
# 安装依赖
go mod tidy
# 基础运行(默认端口 8080 ):
go run main.go
# 指定端口:
go run main.go -p 8081
# 系统初始化:
go run main.go -c cc
```
#### 交叉编译
``` bash
# 路由器 cpu RT-5350:
GOOS=linux GOARCH=mipsle GOMIPS=softfloat CGO_ENABLED=0 go build main.go
# 树莓派 B+:
GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=0 go build main.go
# 树莓派 3B:
GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=0 go build main.go
# Linux 64位:
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build main.go
*说明:
GOOS:目标平台的操作系统(darwin、freebsd、linux、windows)
GOARCH:目标平台的体系架构(386、amd64、arm、mipsle)
交叉编译不支持 CGO 所以要禁用它(CGO_ENABLED=0)
```
#### 使用教程
``` bash
1. 基础运行(默认端口 8080 ):
./main
2. 指定端口:
./main -p 8081
3. 初始化运行:
./main -c cc
* 可能需要添加权限,linux 系统下请使用:
chmod +x main
```
#### 使用说明
1. 请确保路由本机时间准确。
2. 确保防火墙允许连接服务开启的端口。
3. Web服务默认 8080 端口,TCP 连接使用 50001 端口。
4. 如需使用 80端口 请确保路由器原本后台服务已经改到别的端口。
5. *ESP8266 MINI D1 控制代码在项目 arduino/ESP8266-MINI-D1.ino
#### 项目地址
SERVER: \
github: https://github.com/crazyjy/SmartHomeNodeGO \
gitee: https://gitee.com/yjygit/SmartHomeNodeGO
H5: \
github: https://github.com/crazyjy/SmartHomeNodeH5 \
gitee: https://gitee.com/yjygit/SmartHomeNodeH5
#### 参与贡献
1. 【飞鱼智能】- 淘宝店,欢迎选购。\
https://shop223999585.taobao.com/?spm=2013.1.1000126.d21.25751acaxy0uZy
---
package base
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"time"
)
type Base struct {
}
var FileNameList []string
/**
* 初始化存储文件
*/
func (ctx *Base)InitFile(path string) {
if !ctx.CheckFile(path) {
ctx.CreateFile(path)
ctx.WriteFile(path, "[]")
}
}
/**
* 检查文件夹是否存在
*/
func (ctx *Base)CheckDir(path string) bool {
s,err:=os.Stat(path)
if err!=nil{
return false
}
return s.IsDir()
}
/**
* 检查文件是否存在
*/
func (ctx *Base)CheckFile(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
/**
* 新建文件夹
*/
func (ctx *Base)CreateDir(path string) bool {
err := os.MkdirAll(path, os.ModePerm)
if err != nil {
fmt.Println("create dir err", err)
return false
}
return true
}
/**
* 新建文件
*/
func (ctx *Base)CreateFile(path string) bool {
file, err := os.Create(path)
defer file.Close()
if err != nil {
fmt.Println("create file err", err)
return false
}
return true
}
/**
* 读取文件内容
*/
func (ctx *Base)ReadFile(path string) string {
file, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer file.Close()
info, err := ioutil.ReadAll(file)
if err != nil {
log.Fatal(err)
}
return string(info)
}
/**
* 写入文件内容(快速)
*/
func (ctx *Base)WriteFile(path string, info string) bool {
err := ioutil.WriteFile(path, []byte(info), 0666)
if err != nil {
fmt.Println("write file err", err)
return false
}
return true
}
/**
* 删除文件
*/
func (ctx *Base)DelFile(path string) bool {
if ctx.CheckFile(path) {
err := os.Remove(path)
if err != nil {
fmt.Println("delete file err", err)
return false
}
return true
}
return false
}
/**
* 获取文件夹下文件列表
*/
func (ctx *Base)GetFileList(path string) []os.FileInfo {
files, _ := ioutil.ReadDir(path)
return files
}
/**
* 初始化文件夹
*/
func (ctx *Base)InitDir(path string) {
if !ctx.CheckDir(path) {
ctx.CreateDir(path)
}
}
/**
* 获取当前时间 ["23","05","40"]
*/
func (ctx *Base)NowTime() [3]string {
date := [3]string{"00","00","00"}
cstZone := time.FixedZone("CST", 8*3600) // 东八
date[0] = fmt.Sprintf("%v", time.Now().In(cstZone).Hour())
date[1] = fmt.Sprintf("%v", time.Now().In(cstZone).Minute())
date[2] = fmt.Sprintf("%v", time.Now().In(cstZone).Second())
if len(date[0]) == 1 {
date[0] = "0"+date[0]
}
if len(date[1]) == 1 {
date[1] = "0"+date[1]
}
if len(date[2]) == 1 {
date[2] = "0"+date[2]
}
return date
}
/**
* 删除文件夹
*/
func (ctx *Base)DelDir(path string) bool {
err := os.RemoveAll(path)
if err != nil {
fmt.Println("delete file err", err)
return false
}
return true
}
/**
* 追加内容到文件中
*/
func (ctx *Base)AppendToFile(path string, content string) {
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()
if _, err := f.WriteString(content + "\n"); err != nil {
log.Println(err)
}
}
/**
* 获取参数
*/
func (ctx *Base)Args() [2]string {
// 定义几个变量,用于接收命令行的参数值
var read string
var path string
// &user 就是接收命令行中输入 -u 后面的参数值,其他同理
flag.StringVar(&read, "r", "./", "[-r ./files ] readme create path default './'")
flag.StringVar(&path, "p", "./README.md", "[-p ./README.md ] file path default './README.md'")
// 解析命令行参数写入注册的flag里
flag.Parse()
return [2]string{read + "/", path}
}
/**
* 获取目录所有文件
*/
func GetTreeDir(fpath string) {
// 获取fileinfo
if finfo, err := os.Stat(fpath); err == nil {
// 判断是不是目录 如果不是目录而是文件 打印文件path并跳出递归
if !finfo.IsDir() {
//fmt.Println(fpath)
appName := fmt.Sprintf("%v", fpath)
FileNameList = append(FileNameList, appName)
} else {
// 是目录的情况 打印目录path
//fmt.Println(fpath)
f, _ := os.Open(fpath) // 通过目录path open一个file
defer f.Close()
names, _ := f.Readdirnames(0) // 通过file的Readdirnames 拿到当前目录下的所有filename
for _, name := range names {
newpath := path.Join(fpath, name) // 遍历names 拼接新的fpath
GetTreeDir(newpath) // 递归
}
}
}
}
\ No newline at end of file
:<<!
/**********
# SmartHomeNodeGO
#### 介绍
用go编写的可用于智能路由器Web端管理系统。
集成了 HTTP服务 和 TCP 连接服务。
可给智能家居设备提供服务。
可理解为边缘计算。
#### 特点
1. 无需安装,直接运行,所有服务会自动关联。
2. 包含了前端,后端,连接端,定时任务,文件存储。
3. 使用拓展硬盘或U盘,告别容量拮据且读写缓慢的的flash。
4. 尽量少的使用第三方包,确保能运行在大多数MIPS架构CPU的路由器上。
#### 软件架构
1. 后端使用GO开发,前端使用VueJs开发。
2. 后端框架使用gin,前端框架使用 vue-admin + element-ui。
3. 没有使用数据库,以json文件存储设备数据,简单使用了go-cache缓存。
#### 安装教程
* 如要修改本代码,请安装 GoLang 1.14+ 环境。
* 项目使用 go mod 管理包,使用以下命令:
``` bash
# 初始化 go mod 文件
go mod init smartHomeNode/v1
# 安装依赖
go mod tidy
# 基础运行(默认端口 8080 ):
go run main.go
# 指定端口:
go run main.go -p 8081
# 系统初始化:
go run main.go -c cc
```
#### 交叉编译
``` bash
# 路由器 cpu RT-5350:
GOOS=linux GOARCH=mipsle GOMIPS=softfloat CGO_ENABLED=0 go build main.go
# 树莓派 B+:
GOOS=linux GOARCH=arm GOARM=6 CGO_ENABLED=0 go build main.go
# 树莓派 3B:
GOOS=linux GOARCH=arm GOARM=7 CGO_ENABLED=0 go build main.go
# Linux 64位:
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build main.go
*说明:
GOOS:目标平台的操作系统(darwin、freebsd、linux、windows)
GOARCH:目标平台的体系架构(386、amd64、arm、mipsle)
交叉编译不支持 CGO 所以要禁用它(CGO_ENABLED=0)
```
#### 使用教程
``` bash
1. 基础运行(默认端口 8080 ):
./main
2. 指定端口:
./main -p 8081
3. 初始化运行:
./main -c cc
* 可能需要添加权限,linux 系统下请使用:
chmod +x main
```
#### 使用说明
1. 请确保路由本机时间准确。
2. 确保防火墙允许连接服务开启的端口。
3. Web服务默认 8080 端口,TCP 连接使用 50001 端口。
4. 如需使用 80端口 请确保路由器原本后台服务已经改到别的端口。
5. *ESP8266 MINI D1 控制代码在项目 arduino/ESP8266-MINI-D1.ino
#### 项目地址
SERVER: \
github: https://github.com/crazyjy/SmartHomeNodeGO \
gitee: https://gitee.com/yjygit/SmartHomeNodeGO
H5: \
github: https://github.com/crazyjy/SmartHomeNodeH5 \
gitee: https://gitee.com/yjygit/SmartHomeNodeH5
#### 参与贡献
1. 【飞鱼智能】- 淘宝店,欢迎选购。\
https://shop223999585.taobao.com/?spm=2013.1.1000126.d21.25751acaxy0uZy
*****/
!
#!/bin/bash
init_centos7(){
setenforce 0
systemctl stop firewalld && systemctl disable firewalld
yum install -y wget curl bash-completion install ntp ntpdate net-tools telnet bridge-utils nmap nmap-ncat bind-utils tree lsof vim git sysstat redhat-lsb-core
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install -y bash-completion-extras ack nload iftop
}
init_deb(){
apt install redhat-lsb-core vim git ack-grep
}
docker_centos(){
setenforce 0
systemctl stop firewalld && systemctl disable firewalld
yum install -y yum-utils
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
yum makecache
yum -y install docker-ce
systemctl enable --now docker
}
docker_deb(){
apt-get update
apt-get -y install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
apt-get -y update
apt-get -y install docker-ce docker-ce-cli containerd.io
}
# install_docker-compose(){
# }
swarm_init(){
docker swarm init --autolock=false --cert-expiry 86400h --task-history-limit=3
}
distributor_os=$(lsb_release -si)
case $distributor_os in
CentOS)
init_centos7
docker_centos
;;
debian|ubuntu)
init_deb
docker_deb
;;
*)
exit 1;
;;
esac
:<<!
/**********
#### 2
测试2222
#### 软件架构
*****/
!
:<<!
/**********
#### 3
测试
#### 软件架构
*****/
!
package main
import (
"fmt"
"readHead/v1/base"
"regexp"
"strings"
)
var b base.Base
func main() {
args := b.Args()
b.WriteFile(args[1], "---")
reg := regexp.MustCompile(`\/\*\*\*\*\*\*\*\*\*\*[\s\S]*\*\*\*\*\*/`)
if reg == nil {
fmt.Println("regexp err")
return
}
base.GetTreeDir(args[0])
if len(base.FileNameList) > 0 {
for _, fileListName:= range base.FileNameList {
fmt.Println(fileListName)
fileInfo := b.ReadFile(fileListName)
//根据规则提取关键信息
result := reg.FindAllStringSubmatch(fileInfo, -1)
if len(result) == 1 {
resultInfo := fmt.Sprintf("%v", result[0])
infoList := strings.Split(resultInfo, "/**********")
resInfo := fmt.Sprintf("%v", infoList[1])
resInfo = strings.Replace(resInfo, "*****/]", "---", -1)
b.AppendToFile(args[1], resInfo)
}
}
} else {
fmt.Println("not found dir!!!")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment