├── .idea └── JetIot.iml ├── README.md ├── conf └── initConfig.go ├── config.json ├── docs ├── MQTT物通信格式 │ ├── 物基本通信格式说明.md │ └── 物生命周期.md ├── MqttTopicRules.json └── apis │ ├── 物管理 │ ├── 改变物体组件值.http │ ├── 查询当前账号下的设备以及信息列表.http │ ├── 查询指定组件的值.http │ ├── 注册物体.http │ ├── 绑定设备.http │ └── 解绑设备.http │ └── 账号管理 │ ├── 发送好友申请.http │ ├── 同意好友申请.http │ ├── 查看好友申请.http │ ├── 查询好友列表.http │ ├── 查询所有账号.http │ ├── 注册测试.http │ └── 登录测试.http ├── go.mod ├── go.sum ├── main.go ├── model ├── account │ └── accountSystemModel.go ├── event │ └── mqttEvent.go ├── response │ └── responsesModel.go └── thingModel │ └── thingModel.go ├── server ├── appServer │ ├── appServer.go │ └── login.go └── thingServer │ ├── test.go │ └── thingMqttHandle.go └── util ├── Log └── Log.go ├── errorCode └── errorCode.go ├── mqtt └── mqtt.go ├── mysql └── mysql.go ├── public.go └── redis └── redis.go /.idea/JetIot.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JetIot 捷特物联网接入服务器 2 | > ###为什么叫 Jet 3 | > Jet(捷特)指 游戏《Fallout》系列中的一种药物,在游戏设定中是一种相当强大的兴奋剂。 4 | > 它可以刺激中央神经系统。当使用时,会感到一股力量,但是只会持续几分钟而已。 5 | > 此处寓意是希望以最快的速度搭建物联平台并接入设备和终端。 6 | 7 | ## 一、运行环境: 8 | > 推荐使用docker搭建环境 9 | - mysql 5.8 10 | - redis-server 11 | - EMQ X Broker 12 | ## 二、基本原理 13 | 架构主要分为两个部分: 14 | - **HttpServer**:用来与用户终端进行交互使用gin框架 15 | - **MqttClient**:用来和EMQ进行交互用来获取设备信息使用使用paho.mqtt库构建了一个基于事件触发和函数回调的消息系统 16 | 17 | --- 18 | ~~别的等我下次上班摸鱼再写吧~~ 19 | 20 | 硬件接入方案暂定 [看这里](https://github.com/xsj321/JetIot/blob/master/apis/MQTT%E7%89%A9%E9%80%9A%E4%BF%A1%E6%A0%BC%E5%BC%8F/%E7%89%A9%E5%9F%BA%E6%9C%AC%E9%80%9A%E4%BF%A1%E6%A0%BC%E5%BC%8F%E8%AF%B4%E6%98%8E.md) 21 | -------------------------------------------------------------------------------- /conf/initConfig.go: -------------------------------------------------------------------------------- 1 | package conf 2 | 3 | import ( 4 | "JetIot/util/Log" 5 | "encoding/json" 6 | _ "github.com/sirupsen/logrus" 7 | "io/ioutil" 8 | ) 9 | 10 | type Config_t struct { 11 | RedisServer string `json:"redis_server"` 12 | RedisPort string `json:"redis_port"` 13 | RedisPassword string `json:"redis_password"` 14 | MysqlServer string `json:"mysql_server"` 15 | MysqlPort string `json:"mysql_port"` 16 | MysqlPassword string `json:"mysql_password"` 17 | MqttServer string `json:"mqtt_server"` 18 | MqttPort string `json:"mqtt_port"` 19 | MqttClientID string `json:"mqtt_client_id"` 20 | MqttUserName string `json:"mqtt_user_name"` 21 | MqttPassword string `json:"mqtt_password"` 22 | } 23 | 24 | var ( 25 | Default Config_t 26 | ) 27 | 28 | func InitConfig(path string) { 29 | file, err := ioutil.ReadFile(path) 30 | if err != nil { 31 | Log.E()("读取文件失败", err.Error()) 32 | return 33 | } 34 | Log.I()("文件读取成功") 35 | 36 | Default = Config_t{} 37 | err = json.Unmarshal(file, &Default) 38 | if err != nil { 39 | Log.E()("解析配置文件失败", err.Error()) 40 | } 41 | Log.I()("解析配置文件成功") 42 | } 43 | -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | "redis_server":"", 3 | "redis_port":"6379", 4 | "redis_password":"", 5 | "mysql_server":"", 6 | "mysql_port":"3306", 7 | "mysql_password":"", 8 | "mqtt_server":"", 9 | "mqtt_port":"1883", 10 | "mqtt_client_id":"main_server", 11 | "mqtt_user_name":"admin", 12 | "mqtt_password":"public" 13 | } -------------------------------------------------------------------------------- /docs/MQTT物通信格式/物基本通信格式说明.md: -------------------------------------------------------------------------------- 1 | # _基本通信格式说明_ 2 | > 在大括号内为变量值 如:{设备唯一ID} 指的是设备的唯一ID在这一段字符中 3 | ---- 4 | ## _消息传递_ 5 | ### _主题订阅_: 6 | #### _服务端_ 7 | - 服务端订阅主题: 8 | `thingServer/entity/toserver` 9 | - 服务端发布主题: 10 | `thingServer/entity/{设备唯一ID}/todevice` 11 | 12 | #### _客户端_ 13 | - 客户端订阅主题: 14 | `thingServer/entity/{设备唯一ID}/todevice` 15 | - 客户端发布主题: 16 | `thingServer/entity/toserver` 17 | 18 | --- 19 | 20 | ### _消息格式_: 21 | #### _基础格式_: 22 | > 为了和http客户端兼容采用了和http一样的回复格式: 23 | 24 | 25 | - 回复格式示例: 26 | ```json 27 | { 28 | "code": 0, 29 | "msg": "修改设备组件内容", 30 | "success": true, 31 | "data": { 32 | "cover": { 33 | "status": 0, 34 | "event_id":0, 35 | "id": "{设备唯一ID}", 36 | "component_name": "{组件名称}", 37 | "value": "{组件内容}" 38 | } 39 | } 40 | } 41 | ``` 42 | 在实际使用中我们只专注于对 `data`中的数据处理,所以在之后的描述中本文档只对`data` 43 | 字段中的数据进行描述 44 | #### _★ 服务端 ---Publish---> 设备 指令_: 45 | ##### _◎ 修改组件内容_: 46 | - **topic**:`thing/entity/{设备唯一ID}/todevice` 47 | - **json**: 48 | - **event_id**: 0 49 | ```json 50 | { 51 | "cover": { 52 | "id": "{设备唯一ID}", 53 | "component_name": "{组件名称}", 54 | "value": "{组件内容}" 55 | } 56 | } 57 | ``` 58 | 59 | 60 | 61 | #### _★ 设备 ---Publish---> 服务端 指令_: 62 | > event_id 的作用是去在收到订阅设备消息时调用指定的方法的ID 63 | ##### _◎ 解析服务端组件列表数据变更的信息_: 64 | - **topic**:`thing/entity/toserver` 65 | - **json**: 66 | - **event_id**: 0 67 | ```json 68 | { 69 | "event_id":0, 70 | "cover": { 71 | "id": "{设备唯一ID}", 72 | "component_name": "{组件名称}", 73 | "value": "{组件内容}" 74 | } 75 | } 76 | ``` 77 | -------------------------------------------------------------------------------- /docs/MQTT物通信格式/物生命周期.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xsj321/JetIot/feb05e8b140672aae3d6824fb3003c69041d43cd/docs/MQTT物通信格式/物生命周期.md -------------------------------------------------------------------------------- /docs/MqttTopicRules.json: -------------------------------------------------------------------------------- 1 | { 2 | "thing": { 3 | "function": { 4 | "register": "thing/function/register", 5 | "register_relay": "thing/function/register/register_relay/$id" 6 | }, 7 | "entity": { 8 | "info": "thing/entity/$id", 9 | "components": "thing/entity/$id/&componentName" 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /docs/apis/物管理/改变物体组件值.http: -------------------------------------------------------------------------------- 1 | POST http://127.0.0.1:8080/app/thing/setThingComponentValue 2 | Content-Type: application/json 3 | 4 | { 5 | "id": "33322", 6 | "component_name": "cover", 7 | "value": "cover" 8 | } -------------------------------------------------------------------------------- /docs/apis/物管理/查询当前账号下的设备以及信息列表.http: -------------------------------------------------------------------------------- 1 | POST http://127.0.0.1:8080/app/thing/getDeviceListByAccount 2 | Content-Type: application/json 3 | 4 | { 5 | "account":"xsj321@outlook.com" 6 | } -------------------------------------------------------------------------------- /docs/apis/物管理/查询指定组件的值.http: -------------------------------------------------------------------------------- 1 | POST http://127.0.0.1:8080/app/thing/getThingComponentValue 2 | Content-Type: application/json 3 | 4 | { 5 | "id": "33322", 6 | "component_name": "cover" 7 | } -------------------------------------------------------------------------------- /docs/apis/物管理/注册物体.http: -------------------------------------------------------------------------------- 1 | POST http://127.0.0.1:8080/app/thing/register 2 | Content-Type: application/json 3 | 4 | { 5 | "name": "COVER 1", 6 | "id": "testA", 7 | "components": { 8 | "cover": { 9 | "name": "cover", 10 | "type": "int", 11 | "value": 1 12 | }, 13 | "humidity": { 14 | "name": "humidity", 15 | "type": "int", 16 | "value": 1 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /docs/apis/物管理/绑定设备.http: -------------------------------------------------------------------------------- 1 | POST http://127.0.0.1:8080/app/thing/bindingDevice 2 | Content-Type: application/json 3 | 4 | { 5 | "account":"xsj321@outlook.com", 6 | "device_id":"222333" 7 | } -------------------------------------------------------------------------------- /docs/apis/物管理/解绑设备.http: -------------------------------------------------------------------------------- 1 | POST http://127.0.0.1:8080/app/thing/unbindingDevice 2 | Content-Type: application/json 3 | 4 | { 5 | "account":"xsj321@outlook.com", 6 | "device_id":"222333" 7 | } -------------------------------------------------------------------------------- /docs/apis/账号管理/发送好友申请.http: -------------------------------------------------------------------------------- 1 | POST http://101.37.86.133:8080/account/addFriend 2 | Content-Type: application/json 3 | 4 | { 5 | "source_user":"hs@outlook.com", 6 | "target_user":"xsj321@outlook.com", 7 | "status": "0" 8 | } 9 | 10 | <> 2021-01-27T173954.200.json 11 | <> 2021-01-27T173945.200.json 12 | <> 2021-01-27T173851.200.json 13 | <> 2021-01-27T173844.200.json 14 | <> 2021-01-27T173757.200.json 15 | <> 2021-01-27T173715.200.json 16 | <> 2021-01-27T173413.200.json 17 | <> 2021-01-27T173356.200.json 18 | <> 2021-01-27T172201.200.json 19 | <> 2021-01-27T171911.200.json 20 | <> 2021-01-27T171810.200.json 21 | <> 2021-01-27T170950.200.json 22 | <> 2021-01-27T170734.200.json 23 | <> 2021-01-27T170700.200.json 24 | <> 2021-01-27T170538.200.json 25 | <> 2021-01-27T170453.200.json 26 | <> 2021-01-27T154153.200.json 27 | <> 2021-01-27T154138.200.json 28 | <> 2021-01-27T154034.200.json 29 | <> 2021-01-27T153939.200.json 30 | <> 2021-01-27T153814.200.json 31 | <> 2021-01-27T153748.200.json 32 | <> 2021-01-27T153723.200.json 33 | <> 2021-01-27T152836.200.json 34 | <> 2021-01-27T152533.200.json 35 | <> 2021-01-27T152001.200.json 36 | <> 2021-01-27T151853.200.json 37 | <> 2021-01-27T151526.200.json 38 | <> 2021-01-27T151413.200.json 39 | <> 2021-01-27T151124.200.json 40 | <> 2021-01-27T150704.200.json 41 | <> 2021-01-27T150632.200.json 42 | <> 2021-01-27T150409.200.json 43 | <> 2021-01-27T150323.200.json 44 | <> 2021-01-27T145935.200.json 45 | <> 2021-01-27T145720.200.json 46 | <> 2021-01-27T145039.200.json 47 | <> 2021-01-27T144952.200.json 48 | <> 2021-01-27T144830.200.json 49 | <> 2021-01-27T144455.200.json 50 | <> 2021-01-27T144439.200.json 51 | <> 2021-01-27T143830.200.json 52 | <> 2021-01-27T143718.200.json 53 | <> 2021-01-27T143657.200.json 54 | <> 2021-01-27T141937.200.json 55 | 56 | ### -------------------------------------------------------------------------------- /docs/apis/账号管理/同意好友申请.http: -------------------------------------------------------------------------------- 1 | POST http://101.37.86.133:8080/account/acceptFriend 2 | Content-Type: application/json 3 | 4 | { 5 | "source_user":"hs@outlook.com", 6 | "target_user":"xsj321@outlook.com", 7 | "status": "1" 8 | } 9 | 10 | <> 2021-01-27T173954.200.json 11 | <> 2021-01-27T173945.200.json 12 | <> 2021-01-27T173851.200.json 13 | <> 2021-01-27T173844.200.json 14 | <> 2021-01-27T173757.200.json 15 | <> 2021-01-27T173715.200.json 16 | <> 2021-01-27T173413.200.json 17 | <> 2021-01-27T173356.200.json 18 | <> 2021-01-27T172201.200.json 19 | <> 2021-01-27T171911.200.json 20 | <> 2021-01-27T171810.200.json 21 | <> 2021-01-27T170950.200.json 22 | <> 2021-01-27T170734.200.json 23 | <> 2021-01-27T170700.200.json 24 | <> 2021-01-27T170538.200.json 25 | <> 2021-01-27T170453.200.json 26 | <> 2021-01-27T154153.200.json 27 | <> 2021-01-27T154138.200.json 28 | <> 2021-01-27T154034.200.json 29 | <> 2021-01-27T153939.200.json 30 | <> 2021-01-27T153814.200.json 31 | <> 2021-01-27T153748.200.json 32 | <> 2021-01-27T153723.200.json 33 | <> 2021-01-27T152836.200.json 34 | <> 2021-01-27T152533.200.json 35 | <> 2021-01-27T152001.200.json 36 | <> 2021-01-27T151853.200.json 37 | <> 2021-01-27T151526.200.json 38 | <> 2021-01-27T151413.200.json 39 | <> 2021-01-27T151124.200.json 40 | <> 2021-01-27T150704.200.json 41 | <> 2021-01-27T150632.200.json 42 | <> 2021-01-27T150409.200.json 43 | <> 2021-01-27T150323.200.json 44 | <> 2021-01-27T145935.200.json 45 | <> 2021-01-27T145720.200.json 46 | <> 2021-01-27T145039.200.json 47 | <> 2021-01-27T144952.200.json 48 | <> 2021-01-27T144830.200.json 49 | <> 2021-01-27T144455.200.json 50 | <> 2021-01-27T144439.200.json 51 | <> 2021-01-27T143830.200.json 52 | <> 2021-01-27T143718.200.json 53 | <> 2021-01-27T143657.200.json 54 | <> 2021-01-27T141937.200.json 55 | 56 | ### -------------------------------------------------------------------------------- /docs/apis/账号管理/查看好友申请.http: -------------------------------------------------------------------------------- 1 | POST http://101.37.86.133:8080/account/getFriendRequestList 2 | Content-Type: application/json 3 | 4 | { 5 | "account":"xsj321@outlook.com" 6 | } 7 | 8 | <> 2021-01-27T173954.200.json 9 | <> 2021-01-27T173945.200.json 10 | <> 2021-01-27T173851.200.json 11 | <> 2021-01-27T173844.200.json 12 | <> 2021-01-27T173757.200.json 13 | <> 2021-01-27T173715.200.json 14 | <> 2021-01-27T173413.200.json 15 | <> 2021-01-27T173356.200.json 16 | <> 2021-01-27T172201.200.json 17 | <> 2021-01-27T171911.200.json 18 | <> 2021-01-27T171810.200.json 19 | <> 2021-01-27T170950.200.json 20 | <> 2021-01-27T170734.200.json 21 | <> 2021-01-27T170700.200.json 22 | <> 2021-01-27T170538.200.json 23 | <> 2021-01-27T170453.200.json 24 | <> 2021-01-27T154153.200.json 25 | <> 2021-01-27T154138.200.json 26 | <> 2021-01-27T154034.200.json 27 | <> 2021-01-27T153939.200.json 28 | <> 2021-01-27T153814.200.json 29 | <> 2021-01-27T153748.200.json 30 | <> 2021-01-27T153723.200.json 31 | <> 2021-01-27T152836.200.json 32 | <> 2021-01-27T152533.200.json 33 | <> 2021-01-27T152001.200.json 34 | <> 2021-01-27T151853.200.json 35 | <> 2021-01-27T151526.200.json 36 | <> 2021-01-27T151413.200.json 37 | <> 2021-01-27T151124.200.json 38 | <> 2021-01-27T150704.200.json 39 | <> 2021-01-27T150632.200.json 40 | <> 2021-01-27T150409.200.json 41 | <> 2021-01-27T150323.200.json 42 | <> 2021-01-27T145935.200.json 43 | <> 2021-01-27T145720.200.json 44 | <> 2021-01-27T145039.200.json 45 | <> 2021-01-27T144952.200.json 46 | <> 2021-01-27T144830.200.json 47 | <> 2021-01-27T144455.200.json 48 | <> 2021-01-27T144439.200.json 49 | <> 2021-01-27T143830.200.json 50 | <> 2021-01-27T143718.200.json 51 | <> 2021-01-27T143657.200.json 52 | <> 2021-01-27T141937.200.json 53 | 54 | ### -------------------------------------------------------------------------------- /docs/apis/账号管理/查询好友列表.http: -------------------------------------------------------------------------------- 1 | POST http://101.37.86.133:8080/account/getFriendList 2 | Content-Type: application/json 3 | 4 | { 5 | "account":"hs@outlook.com" 6 | } 7 | 8 | <> 2021-01-27T173954.200.json 9 | <> 2021-01-27T173945.200.json 10 | <> 2021-01-27T173851.200.json 11 | <> 2021-01-27T173844.200.json 12 | <> 2021-01-27T173757.200.json 13 | <> 2021-01-27T173715.200.json 14 | <> 2021-01-27T173413.200.json 15 | <> 2021-01-27T173356.200.json 16 | <> 2021-01-27T172201.200.json 17 | <> 2021-01-27T171911.200.json 18 | <> 2021-01-27T171810.200.json 19 | <> 2021-01-27T170950.200.json 20 | <> 2021-01-27T170734.200.json 21 | <> 2021-01-27T170700.200.json 22 | <> 2021-01-27T170538.200.json 23 | <> 2021-01-27T170453.200.json 24 | <> 2021-01-27T154153.200.json 25 | <> 2021-01-27T154138.200.json 26 | <> 2021-01-27T154034.200.json 27 | <> 2021-01-27T153939.200.json 28 | <> 2021-01-27T153814.200.json 29 | <> 2021-01-27T153748.200.json 30 | <> 2021-01-27T153723.200.json 31 | <> 2021-01-27T152836.200.json 32 | <> 2021-01-27T152533.200.json 33 | <> 2021-01-27T152001.200.json 34 | <> 2021-01-27T151853.200.json 35 | <> 2021-01-27T151526.200.json 36 | <> 2021-01-27T151413.200.json 37 | <> 2021-01-27T151124.200.json 38 | <> 2021-01-27T150704.200.json 39 | <> 2021-01-27T150632.200.json 40 | <> 2021-01-27T150409.200.json 41 | <> 2021-01-27T150323.200.json 42 | <> 2021-01-27T145935.200.json 43 | <> 2021-01-27T145720.200.json 44 | <> 2021-01-27T145039.200.json 45 | <> 2021-01-27T144952.200.json 46 | <> 2021-01-27T144830.200.json 47 | <> 2021-01-27T144455.200.json 48 | <> 2021-01-27T144439.200.json 49 | <> 2021-01-27T143830.200.json 50 | <> 2021-01-27T143718.200.json 51 | <> 2021-01-27T143657.200.json 52 | <> 2021-01-27T141937.200.json 53 | 54 | ### -------------------------------------------------------------------------------- /docs/apis/账号管理/查询所有账号.http: -------------------------------------------------------------------------------- 1 | POST http://101.37.86.133:8080/account/listAccount 2 | Content-Type: application/json 3 | 4 | { 5 | "account":"123w@13.com" 6 | } 7 | 8 | <> 2021-01-27T173954.200.json 9 | <> 2021-01-27T173945.200.json 10 | <> 2021-01-27T173851.200.json 11 | <> 2021-01-27T173844.200.json 12 | <> 2021-01-27T173757.200.json 13 | <> 2021-01-27T173715.200.json 14 | <> 2021-01-27T173413.200.json 15 | <> 2021-01-27T173356.200.json 16 | <> 2021-01-27T172201.200.json 17 | <> 2021-01-27T171911.200.json 18 | <> 2021-01-27T171810.200.json 19 | <> 2021-01-27T170950.200.json 20 | <> 2021-01-27T170734.200.json 21 | <> 2021-01-27T170700.200.json 22 | <> 2021-01-27T170538.200.json 23 | <> 2021-01-27T170453.200.json 24 | <> 2021-01-27T154153.200.json 25 | <> 2021-01-27T154138.200.json 26 | <> 2021-01-27T154034.200.json 27 | <> 2021-01-27T153939.200.json 28 | <> 2021-01-27T153814.200.json 29 | <> 2021-01-27T153748.200.json 30 | <> 2021-01-27T153723.200.json 31 | <> 2021-01-27T152836.200.json 32 | <> 2021-01-27T152533.200.json 33 | <> 2021-01-27T152001.200.json 34 | <> 2021-01-27T151853.200.json 35 | <> 2021-01-27T151526.200.json 36 | <> 2021-01-27T151413.200.json 37 | <> 2021-01-27T151124.200.json 38 | <> 2021-01-27T150704.200.json 39 | <> 2021-01-27T150632.200.json 40 | <> 2021-01-27T150409.200.json 41 | <> 2021-01-27T150323.200.json 42 | <> 2021-01-27T145935.200.json 43 | <> 2021-01-27T145720.200.json 44 | <> 2021-01-27T145039.200.json 45 | <> 2021-01-27T144952.200.json 46 | <> 2021-01-27T144830.200.json 47 | <> 2021-01-27T144455.200.json 48 | <> 2021-01-27T144439.200.json 49 | <> 2021-01-27T143830.200.json 50 | <> 2021-01-27T143718.200.json 51 | <> 2021-01-27T143657.200.json 52 | <> 2021-01-27T141937.200.json 53 | 54 | ### -------------------------------------------------------------------------------- /docs/apis/账号管理/注册测试.http: -------------------------------------------------------------------------------- 1 | POST http://101.37.86.133:8080/account/register 2 | Content-Type: application/json 3 | 4 | { 5 | "account":"hs@outlook.com", 6 | "password":"123", 7 | "name": "hs", 8 | "type": 0 9 | } 10 | 11 | <> 2021-01-27T173954.200.json 12 | <> 2021-01-27T173945.200.json 13 | <> 2021-01-27T173851.200.json 14 | <> 2021-01-27T173844.200.json 15 | <> 2021-01-27T173757.200.json 16 | <> 2021-01-27T173715.200.json 17 | <> 2021-01-27T173413.200.json 18 | <> 2021-01-27T173356.200.json 19 | <> 2021-01-27T172201.200.json 20 | <> 2021-01-27T171911.200.json 21 | <> 2021-01-27T171810.200.json 22 | <> 2021-01-27T170950.200.json 23 | <> 2021-01-27T170734.200.json 24 | <> 2021-01-27T170700.200.json 25 | <> 2021-01-27T170538.200.json 26 | <> 2021-01-27T170453.200.json 27 | <> 2021-01-27T154153.200.json 28 | <> 2021-01-27T154138.200.json 29 | <> 2021-01-27T154034.200.json 30 | <> 2021-01-27T153939.200.json 31 | <> 2021-01-27T153814.200.json 32 | <> 2021-01-27T153748.200.json 33 | <> 2021-01-27T153723.200.json 34 | <> 2021-01-27T152836.200.json 35 | <> 2021-01-27T152533.200.json 36 | <> 2021-01-27T152001.200.json 37 | <> 2021-01-27T151853.200.json 38 | <> 2021-01-27T151526.200.json 39 | <> 2021-01-27T151413.200.json 40 | <> 2021-01-27T151124.200.json 41 | <> 2021-01-27T150704.200.json 42 | <> 2021-01-27T150632.200.json 43 | <> 2021-01-27T150409.200.json 44 | <> 2021-01-27T150323.200.json 45 | <> 2021-01-27T145935.200.json 46 | <> 2021-01-27T145720.200.json 47 | <> 2021-01-27T145039.200.json 48 | <> 2021-01-27T144952.200.json 49 | <> 2021-01-27T144830.200.json 50 | <> 2021-01-27T144455.200.json 51 | <> 2021-01-27T144439.200.json 52 | <> 2021-01-27T143830.200.json 53 | <> 2021-01-27T143718.200.json 54 | <> 2021-01-27T143657.200.json 55 | <> 2021-01-27T141937.200.json 56 | 57 | ### -------------------------------------------------------------------------------- /docs/apis/账号管理/登录测试.http: -------------------------------------------------------------------------------- 1 | POST http://127.0.0.1:8080/app/account/login 2 | Content-Type: application/json 3 | 4 | { 5 | "account":"xsj321@outlook.com", 6 | "password":"xsj08262334910" 7 | } 8 | 9 | <> 2021-01-27T173954.200.json 10 | <> 2021-01-27T173945.200.json 11 | <> 2021-01-27T173851.200.json 12 | <> 2021-01-27T173844.200.json 13 | <> 2021-01-27T173757.200.json 14 | <> 2021-01-27T173715.200.json 15 | <> 2021-01-27T173413.200.json 16 | <> 2021-01-27T173356.200.json 17 | <> 2021-01-27T172201.200.json 18 | <> 2021-01-27T171911.200.json 19 | <> 2021-01-27T171810.200.json 20 | <> 2021-01-27T170950.200.json 21 | <> 2021-01-27T170734.200.json 22 | <> 2021-01-27T170700.200.json 23 | <> 2021-01-27T170538.200.json 24 | <> 2021-01-27T170453.200.json 25 | <> 2021-01-27T154153.200.json 26 | <> 2021-01-27T154138.200.json 27 | <> 2021-01-27T154034.200.json 28 | <> 2021-01-27T153939.200.json 29 | <> 2021-01-27T153814.200.json 30 | <> 2021-01-27T153748.200.json 31 | <> 2021-01-27T153723.200.json 32 | <> 2021-01-27T152836.200.json 33 | <> 2021-01-27T152533.200.json 34 | <> 2021-01-27T152001.200.json 35 | <> 2021-01-27T151853.200.json 36 | <> 2021-01-27T151526.200.json 37 | <> 2021-01-27T151413.200.json 38 | <> 2021-01-27T151124.200.json 39 | <> 2021-01-27T150704.200.json 40 | <> 2021-01-27T150632.200.json 41 | <> 2021-01-27T150409.200.json 42 | <> 2021-01-27T150323.200.json 43 | <> 2021-01-27T145935.200.json 44 | <> 2021-01-27T145720.200.json 45 | <> 2021-01-27T145039.200.json 46 | <> 2021-01-27T144952.200.json 47 | <> 2021-01-27T144830.200.json 48 | <> 2021-01-27T144455.200.json 49 | <> 2021-01-27T144439.200.json 50 | <> 2021-01-27T143830.200.json 51 | <> 2021-01-27T143718.200.json 52 | <> 2021-01-27T143657.200.json 53 | <> 2021-01-27T141937.200.json 54 | 55 | ### -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module JetIot 2 | 3 | go 1.15 4 | 5 | require ( 6 | github.com/eclipse/paho.mqtt.golang v1.3.1 7 | github.com/gin-gonic/gin v1.6.3 8 | github.com/go-redis/redis v6.15.9+incompatible 9 | github.com/jinzhu/gorm v1.9.16 10 | github.com/kr/pretty v0.1.0 // indirect 11 | github.com/onsi/ginkgo v1.14.2 // indirect 12 | github.com/onsi/gomega v1.10.4 // indirect 13 | github.com/sirupsen/logrus v1.7.0 14 | github.com/spf13/pflag v1.0.5 15 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect 16 | ) 17 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= 2 | github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= 3 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 4 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 5 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 6 | github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM= 7 | github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= 8 | github.com/eclipse/paho.mqtt.golang v1.3.1 h1:6F5FYb1hxVSZS+p0ji5xBQamc5ltOolTYRy5R15uVmI= 9 | github.com/eclipse/paho.mqtt.golang v1.3.1/go.mod h1:eTzb4gxwwyWpqBUHGQZ4ABAV7+Jgm1PklsYT/eo8Hcc= 10 | github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= 11 | github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= 12 | github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= 13 | github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= 14 | github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= 15 | github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= 16 | github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= 17 | github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14= 18 | github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= 19 | github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= 20 | github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= 21 | github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= 22 | github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= 23 | github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= 24 | github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= 25 | github.com/go-playground/validator/v10 v10.2.0 h1:KgJ0snyC2R9VXYN2rneOtQcw5aHQB1Vv0sFl1UcHBOY= 26 | github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI= 27 | github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg= 28 | github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA= 29 | github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= 30 | github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= 31 | github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe h1:lXe2qZdvpiX5WZkZR4hgp4KJVfY3nMkvmwbVkpv1rVY= 32 | github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= 33 | github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= 34 | github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= 35 | github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= 36 | github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= 37 | github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= 38 | github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= 39 | github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= 40 | github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= 41 | github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= 42 | github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 43 | github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= 44 | github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= 45 | github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= 46 | github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= 47 | github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= 48 | github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 49 | github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= 50 | github.com/jinzhu/gorm v1.9.16 h1:+IyIjPEABKRpsu/F8OvDPy9fyQlgsg2luMV2ZIH5i5o= 51 | github.com/jinzhu/gorm v1.9.16/go.mod h1:G3LB3wezTOWM2ITLzPxEXgSkOXAntiLHS7UdBefADcs= 52 | github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= 53 | github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= 54 | github.com/jinzhu/now v1.0.1 h1:HjfetcXq097iXP0uoPCdnM4Efp5/9MsM0/M+XOTeR3M= 55 | github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= 56 | github.com/json-iterator/go v1.1.9 h1:9yzud/Ht36ygwatGx56VwCZtlI/2AD15T1X2sjSuGns= 57 | github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= 58 | github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= 59 | github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= 60 | github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= 61 | github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= 62 | github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= 63 | github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y= 64 | github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= 65 | github.com/lib/pq v1.1.1 h1:sJZmqHoEaY7f+NPP8pgLB/WxulyR3fewgCM2qaSlBb4= 66 | github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= 67 | github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= 68 | github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= 69 | github.com/mattn/go-sqlite3 v1.14.0 h1:mLyGNKR8+Vv9CAU7PphKa2hkEqxxhn8i32J6FPj1/QA= 70 | github.com/mattn/go-sqlite3 v1.14.0/go.mod h1:JIl7NbARA7phWnGvh0LKTyg7S9BA+6gx71ShQilpsus= 71 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= 72 | github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= 73 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= 74 | github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= 75 | github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78= 76 | github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= 77 | github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= 78 | github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= 79 | github.com/onsi/ginkgo v1.14.2 h1:8mVmC9kjFFmA8H4pKMUhcblgifdkOIXPvbhN1T36q1M= 80 | github.com/onsi/ginkgo v1.14.2/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= 81 | github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= 82 | github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= 83 | github.com/onsi/gomega v1.10.4 h1:NiTx7EEvBzu9sFOD1zORteLSt3o8gnlvZZwSE9TnY9U= 84 | github.com/onsi/gomega v1.10.4/go.mod h1:g/HbgYopi++010VEqkFgJHKC09uJiW9UkXvMUuKHUCQ= 85 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 86 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 87 | github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM= 88 | github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= 89 | github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= 90 | github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= 91 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 92 | github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= 93 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= 94 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 95 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 96 | github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= 97 | github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= 98 | github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= 99 | github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= 100 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 101 | golang.org/x/crypto v0.0.0-20190325154230-a5d413f7728c/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= 102 | golang.org/x/crypto v0.0.0-20191205180655-e7c4368fe9dd/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 103 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= 104 | golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= 105 | golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 106 | golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= 107 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= 108 | golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= 109 | golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= 110 | golang.org/x/net v0.0.0-20200425230154-ff2c4b7c35a0/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= 111 | golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= 112 | golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb h1:eBmm0M9fYhWpKZLjQUUKka/LtIxf46G4fxeEz5KJr9U= 113 | golang.org/x/net v0.0.0-20201202161906-c7110b5ffcbb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= 114 | golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 115 | golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 116 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 117 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 118 | golang.org/x/sys v0.0.0-20190904154756-749cb33beabd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 119 | golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 120 | golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 121 | golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 122 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 123 | golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 124 | golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 125 | golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f h1:+Nyd8tzPX9R7BWHguqsrbFdRx3WQ/1ib8I44HXV5yTA= 126 | golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 127 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= 128 | golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= 129 | golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= 130 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 131 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 132 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= 133 | golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= 134 | google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= 135 | google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= 136 | google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= 137 | google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= 138 | google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= 139 | google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= 140 | google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= 141 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 142 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= 143 | gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 144 | gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= 145 | gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= 146 | gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= 147 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 148 | gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 149 | gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 150 | gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= 151 | gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 152 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "JetIot/conf" 5 | "JetIot/model/event" 6 | "JetIot/server/appServer" 7 | "JetIot/server/thingServer" 8 | "JetIot/util/Log" 9 | "JetIot/util/mqtt" 10 | "JetIot/util/mysql" 11 | "JetIot/util/redis" 12 | "github.com/gin-gonic/gin" 13 | "github.com/spf13/pflag" 14 | "os" 15 | "os/signal" 16 | ) 17 | 18 | var configPath = pflag.StringP("config", "c", "conf/defaultConfig.json", "the config json file path") 19 | 20 | func initEvn() { 21 | conf.InitConfig(*configPath) 22 | mysql.InitDB() 23 | redis.InitRedis() 24 | mqtt.InitMqttClient() 25 | mqtt.Publish("server/status", "restart") 26 | } 27 | 28 | func runHttpServer() { 29 | engine := gin.Default() 30 | 31 | //app 32 | appGroup := engine.Group("app") 33 | 34 | //物管理 35 | thingGroup := appGroup.Group("thing") 36 | thingGroup.POST("register", appServer.RegisterThing) 37 | thingGroup.POST("setThingComponentValue", appServer.SetThingComponentValue) 38 | thingGroup.POST("getThingComponentValue", appServer.GetThingComponentValue) 39 | thingGroup.POST("getThingAllValue", appServer.GetThingAllValue) 40 | thingGroup.POST("bindingDevice", appServer.BindingDevice) 41 | thingGroup.POST("unbindingDevice", appServer.UnBindingDevice) 42 | thingGroup.POST("getDeviceListByAccount", appServer.GetDeviceInfoListByAccount) 43 | 44 | //账号管理 45 | accountGroup := engine.Group("account") 46 | accountGroup.POST("login", appServer.Login) 47 | accountGroup.POST("register", appServer.Register) 48 | accountGroup.POST("listAccount", appServer.ListAccount) 49 | accountGroup.POST("addFriend", appServer.AddFriend) 50 | accountGroup.POST("acceptFriend", appServer.AcceptFriend) 51 | accountGroup.POST("getFriendList", appServer.GetFriendList) 52 | accountGroup.POST("getFriendRequestList", appServer.GetFriendRequestList) 53 | err := engine.Run() 54 | 55 | if err != nil { 56 | Log.E()(err.Error()) 57 | } 58 | } 59 | 60 | func runMqttServer() { 61 | mqtt.Subscribe("thingServer/function/register", thingServer.RegisterThingByMqttHandle) 62 | mqtt.RegisterEventHandle(event.EVENT_COMPONENT_CHANGE_VALUE, "更新设备组件数据", thingServer.SetThingComponentValueByMqttHandle) 63 | mqtt.RegisterEventHandle(event.EVENT_THING_DEVICE_ONLIONE, "设备上线初始化", thingServer.DeviceOnlineByMqttHandle) 64 | mqtt.RegisterEventHandle(event.EVENT_THING_DEVICE_OFFLIONE, "设备离线遗嘱", thingServer.DeviceOfflineByMqttHandle) 65 | mqtt.EventListenStart() 66 | } 67 | 68 | func main() { 69 | initEvn() 70 | go runHttpServer() 71 | go runMqttServer() 72 | quit := make(chan os.Signal) 73 | signal.Notify(quit, os.Interrupt) 74 | <-quit 75 | } 76 | -------------------------------------------------------------------------------- /model/account/accountSystemModel.go: -------------------------------------------------------------------------------- 1 | package account 2 | 3 | type Account struct { 4 | Account string `json:"account"` 5 | Password string `json:"password"` 6 | Name string `json:"name"` 7 | Type int `json:"type"` 8 | CreateTime string `json:"create_time"` 9 | } 10 | 11 | type BindingDevice struct { 12 | Account string `json:"account"` 13 | DeviceId string `json:"device_id"` 14 | CreateTime string `json:"create_time"` 15 | } 16 | 17 | type AddFriendOV struct { 18 | SourceUser string `json:"source_user"` 19 | TargetUser string `json:"target_user"` 20 | Status string `json:"status"` 21 | } 22 | -------------------------------------------------------------------------------- /model/event/mqttEvent.go: -------------------------------------------------------------------------------- 1 | /** 2 | * @Description: mqtt RPC函数方法ID表 3 | */ 4 | package event 5 | 6 | const ( 7 | EVENT_OTHER = -1 8 | EVENT_COMPONENT_CHANGE_VALUE = 0 9 | EVENT_THING_CHANGE_VALUE = 1 10 | EVENT_THING_DEVICE_ONLIONE = 3 11 | EVENT_THING_DEVICE_OFFLIONE = 4 12 | ) 13 | -------------------------------------------------------------------------------- /model/response/responsesModel.go: -------------------------------------------------------------------------------- 1 | package response 2 | 3 | import ( 4 | "JetIot/util/errorCode" 5 | ) 6 | 7 | type Responses_t struct { 8 | Code int `json:"code"` 9 | Msg string `json:"msg"` 10 | Success bool `json:"success"` 11 | Data interface{} `json:"data"` 12 | EventId int `json:"event_id"` 13 | } 14 | 15 | func GetSuccessResponses(msg string, data ...interface{}) Responses_t { 16 | if len(data) == 0 { 17 | data = append(data, nil) 18 | } 19 | t := Responses_t{ 20 | Msg: msg, 21 | Data: data[0], 22 | Code: errorCode.ERR_NO_ERROR, 23 | Success: true, 24 | } 25 | return t 26 | } 27 | 28 | func GetFailResponses(msg string, code int) Responses_t { 29 | t := Responses_t{ 30 | Msg: msg, 31 | Data: nil, 32 | Code: code, 33 | Success: false, 34 | } 35 | return t 36 | } 37 | 38 | func GetMqttSuccessResponses(msg string, eventId int, data ...interface{}) Responses_t { 39 | if len(data) == 0 { 40 | data = append(data, nil) 41 | } 42 | t := Responses_t{ 43 | Msg: msg, 44 | EventId: eventId, 45 | Data: data[0], 46 | Code: errorCode.ERR_NO_ERROR, 47 | Success: true, 48 | } 49 | return t 50 | } 51 | 52 | func GetMqttFailResponses(msg string, eventId int, code int) Responses_t { 53 | t := Responses_t{ 54 | Msg: msg, 55 | Data: nil, 56 | Code: code, 57 | EventId: eventId, 58 | Success: false, 59 | } 60 | return t 61 | } 62 | -------------------------------------------------------------------------------- /model/thingModel/thingModel.go: -------------------------------------------------------------------------------- 1 | package thingModel 2 | 3 | import "JetIot/util/Log" 4 | 5 | type BaseInfo struct { 6 | EventId int `json:"event_id"` 7 | } 8 | 9 | type Thing struct { 10 | BaseInfo 11 | Id string `json:"id"` 12 | Name string `json:"name"` 13 | Components map[string]*Component `json:"components"` 14 | } 15 | 16 | type Component struct { 17 | BaseInfo 18 | Name string `json:"name"` 19 | Type string `json:"type"` 20 | Value interface{} `json:"value"` 21 | } 22 | 23 | type ThingComponentValueOV struct { 24 | BaseInfo 25 | Id string `json:"id"` //物体id 26 | ComponentName string `json:"component_name"` //组件名称 27 | Value interface{} `json:"value"` //值 28 | } 29 | 30 | type ThingInitOV struct { 31 | BaseInfo 32 | Id string `json:"id"` //物体id 33 | } 34 | 35 | func (c *Component) Do(value interface{}) { 36 | Log.D()(value) 37 | c.Value = value 38 | } 39 | 40 | func (c *Component) Call() interface{} { 41 | return c.Value 42 | } 43 | -------------------------------------------------------------------------------- /server/appServer/appServer.go: -------------------------------------------------------------------------------- 1 | /** 2 | * @Description: 通过HTTP对设备进行注册修改等操作 3 | */ 4 | package appServer 5 | 6 | import ( 7 | "JetIot/model/account" 8 | "JetIot/model/response" 9 | "JetIot/model/thingModel" 10 | "JetIot/util" 11 | "JetIot/util/Log" 12 | "JetIot/util/errorCode" 13 | "JetIot/util/mysql" 14 | "JetIot/util/redis" 15 | "encoding/json" 16 | "github.com/gin-gonic/gin" 17 | "net/http" 18 | ) 19 | 20 | /** 21 | * @Description: 注册物类型与组件 22 | * @param context 23 | */ 24 | func RegisterThing(context *gin.Context) { 25 | thing := thingModel.Thing{} 26 | err := context.ShouldBindJSON(&thing) 27 | if err != nil { 28 | Log.E()("参数解析错误") 29 | context.JSON(http.StatusOK, response.GetFailResponses( 30 | "参数解析错误", 31 | errorCode.ERR_SVR_INTERNAL, 32 | )) 33 | return 34 | } 35 | 36 | marshal, _ := json.Marshal(thing) 37 | 38 | //保存到缓存库 39 | err = redis.Set("thingServer:"+thing.Id, string(marshal)) 40 | if err != nil { 41 | Log.E()("保存到缓存库错误" + err.Error()) 42 | context.JSON(http.StatusOK, response.GetFailResponses( 43 | "保存到缓存库错误", 44 | errorCode.ERR_SVR_INTERNAL, 45 | )) 46 | return 47 | } 48 | context.JSON(http.StatusOK, response.GetSuccessResponses("注册完成")) 49 | } 50 | 51 | /** 52 | * @Description: 设置组件的值 53 | * @param context 54 | */ 55 | func SetThingComponentValue(context *gin.Context) { 56 | ov := thingModel.ThingComponentValueOV{} 57 | err := context.ShouldBindJSON(&ov) 58 | if err != nil { 59 | Log.E()("参数解析错误" + err.Error()) 60 | context.JSON(http.StatusOK, response.GetFailResponses( 61 | "参数解析错误", 62 | errorCode.ERR_SVR_INTERNAL, 63 | )) 64 | return 65 | } 66 | 67 | thing, err := util.LoadThing(ov.Id) 68 | if err != nil { 69 | Log.E()("加载错误" + err.Error()) 70 | context.JSON(http.StatusOK, response.GetFailResponses( 71 | "加载错误", 72 | errorCode.ERR_SVR_INTERNAL, 73 | )) 74 | return 75 | } 76 | 77 | thing.Components[ov.ComponentName].Do(ov.Value) 78 | err = util.Commit(thing) 79 | if err != nil { 80 | Log.E()("更新缓存错误" + err.Error()) 81 | context.JSON(http.StatusOK, response.GetFailResponses( 82 | "更新缓存错误", 83 | errorCode.ERR_SVR_INTERNAL, 84 | )) 85 | return 86 | } 87 | context.JSON(http.StatusOK, response.GetSuccessResponses("修改完成")) 88 | } 89 | 90 | /** 91 | * @Description: 解绑设备 92 | * @param context 93 | */ 94 | func UnBindingDevice(context *gin.Context) { 95 | deviceOV := account.BindingDevice{} 96 | err := context.ShouldBindJSON(&deviceOV) 97 | if err != nil { 98 | Log.E()("参数解析错误" + err.Error()) 99 | context.JSON(http.StatusOK, response.GetFailResponses( 100 | "参数解析错误", 101 | errorCode.ERR_SVR_INTERNAL, 102 | )) 103 | return 104 | } 105 | 106 | err = redis.Del("thing:" + deviceOV.DeviceId) 107 | if err != nil { 108 | Log.E()("删除设备失败", err) 109 | context.JSON(http.StatusOK, response.GetFailResponses( 110 | "删除设备失败", 111 | errorCode.ERR_REDIS_FAILED, 112 | )) 113 | return 114 | } 115 | 116 | err = mysql.Conn.Delete(&deviceOV).Error 117 | if err != nil { 118 | Log.E()("删除设备失败", err) 119 | context.JSON(http.StatusOK, response.GetFailResponses( 120 | "删除设备失败", 121 | errorCode.ERR_MYSQL_FAILED, 122 | )) 123 | return 124 | } 125 | 126 | context.JSON(http.StatusOK, response.GetSuccessResponses( 127 | "设备解绑完成", 128 | errorCode.ERR_NO_ERROR, 129 | )) 130 | } 131 | 132 | /** 133 | * @Description: 绑定设备 134 | * @param context 135 | */ 136 | func BindingDevice(context *gin.Context) { 137 | deviceOV := account.BindingDevice{} 138 | err := context.ShouldBindJSON(&deviceOV) 139 | if err != nil { 140 | Log.E()("参数解析错误" + err.Error()) 141 | context.JSON(http.StatusOK, response.GetFailResponses( 142 | "参数解析错误", 143 | errorCode.ERR_SVR_INTERNAL, 144 | )) 145 | return 146 | } 147 | 148 | //验证设备是否被绑定 149 | isBinding := util.IsDeviceBinding(deviceOV.DeviceId) 150 | 151 | if isBinding { 152 | context.JSON(http.StatusOK, response.GetFailResponses( 153 | "设备已经被绑定", 154 | errorCode.ERR_DEV_BINDED, 155 | )) 156 | 157 | return 158 | } 159 | 160 | deviceOV.CreateTime = mysql.GetFormatNowTime() 161 | err = mysql.Conn.Create(&deviceOV).Error 162 | if err != nil { 163 | Log.E()("设备保存错误" + err.Error()) 164 | context.JSON(http.StatusOK, response.GetFailResponses( 165 | "设备保存错误", 166 | errorCode.ERR_SVR_INTERNAL, 167 | )) 168 | return 169 | } 170 | context.JSON(http.StatusOK, response.GetSuccessResponses( 171 | "设备绑定完成", 172 | errorCode.ERR_NO_ERROR, 173 | )) 174 | 175 | } 176 | 177 | func GetDeviceInfoListByAccount(context *gin.Context) { 178 | ov := account.Account{} 179 | err := context.ShouldBindJSON(&ov) 180 | if err != nil { 181 | Log.E()("参数解析错误" + err.Error()) 182 | context.JSON(http.StatusOK, response.GetFailResponses( 183 | "参数解析错误", 184 | errorCode.ERR_SVR_INTERNAL, 185 | )) 186 | return 187 | } 188 | 189 | var deviceList []account.BindingDevice 190 | err = mysql.Conn.Select("device_id").Table("binding_devices").Where("account = ?", ov.Account).Scan(&deviceList).Error 191 | if err != nil { 192 | Log.E()("查询设备列表失败", err) 193 | context.JSON(http.StatusOK, response.GetFailResponses( 194 | "查询设备列表失败", 195 | errorCode.ERR_MYSQL_FAILED, 196 | )) 197 | return 198 | } 199 | if len(deviceList) == 0 { 200 | Log.D()("设备列表为空", err) 201 | context.JSON(http.StatusOK, response.GetFailResponses( 202 | "设备列表为空", 203 | errorCode.ERR_DEVICE_LIST_EMPTY, 204 | )) 205 | return 206 | } 207 | 208 | var thingList []thingModel.Thing 209 | 210 | for _, s := range deviceList { 211 | thing, err := util.LoadThing(s.DeviceId) 212 | if err != nil { 213 | Log.E()("加载错误" + err.Error()) 214 | context.JSON(http.StatusOK, response.GetFailResponses( 215 | "加载错误", 216 | errorCode.ERR_SVR_INTERNAL, 217 | )) 218 | return 219 | } 220 | thingList = append(thingList, *thing) 221 | } 222 | 223 | context.JSON(http.StatusOK, response.GetSuccessResponses( 224 | "设备列表获取成功", 225 | thingList, 226 | )) 227 | } 228 | 229 | func GetThingComponentValue(context *gin.Context) { 230 | ov := thingModel.ThingComponentValueOV{} 231 | err := context.ShouldBindJSON(&ov) 232 | if err != nil { 233 | Log.E()("参数解析错误" + err.Error()) 234 | context.JSON(http.StatusOK, response.GetFailResponses( 235 | "参数解析错误", 236 | errorCode.ERR_SVR_INTERNAL, 237 | )) 238 | return 239 | } 240 | 241 | thing, err := util.LoadThing(ov.Id) 242 | if err != nil { 243 | Log.E()("加载错误" + err.Error()) 244 | context.JSON(http.StatusOK, response.GetFailResponses( 245 | "加载错误", 246 | errorCode.ERR_SVR_INTERNAL, 247 | )) 248 | return 249 | } 250 | res := thing.Components[ov.ComponentName].Call() 251 | ov.Value = res 252 | context.JSON(http.StatusOK, response.GetSuccessResponses("查询成功", ov)) 253 | } 254 | 255 | func GetThingAllValue(context *gin.Context) { 256 | ov := thingModel.ThingInitOV{} 257 | err := context.ShouldBindJSON(&ov) 258 | if err != nil { 259 | Log.E()("参数解析错误" + err.Error()) 260 | context.JSON(http.StatusOK, response.GetFailResponses( 261 | "参数解析错误", 262 | errorCode.ERR_SVR_INTERNAL, 263 | )) 264 | return 265 | } 266 | 267 | thing, err := util.LoadThing(ov.Id) 268 | if err != nil { 269 | Log.E()("加载错误" + err.Error()) 270 | context.JSON(http.StatusOK, response.GetFailResponses( 271 | "加载错误", 272 | errorCode.ERR_SVR_INTERNAL, 273 | )) 274 | return 275 | } 276 | res, _ := json.Marshal(thing) 277 | //ov.Value = res 278 | context.JSON(http.StatusOK, response.GetSuccessResponses("查询成功", string(res))) 279 | } 280 | -------------------------------------------------------------------------------- /server/appServer/login.go: -------------------------------------------------------------------------------- 1 | package appServer 2 | 3 | import ( 4 | "JetIot/model/account" 5 | "JetIot/model/response" 6 | "JetIot/util/Log" 7 | "JetIot/util/errorCode" 8 | "JetIot/util/mysql" 9 | "JetIot/util/redis" 10 | "github.com/gin-gonic/gin" 11 | "net/http" 12 | "time" 13 | ) 14 | 15 | /** 16 | * @Description: 登录接口 17 | * @param context gin上下文 18 | */ 19 | func Login(context *gin.Context) { 20 | param := account.Account{} 21 | err := context.ShouldBindJSON(¶m) 22 | if err != nil { 23 | Log.E()("参数解析错误") 24 | context.JSON(http.StatusOK, response.GetFailResponses( 25 | "参数解析错误", 26 | errorCode.ERR_SVR_INTERNAL, 27 | )) 28 | return 29 | } 30 | res, err := mysql.Find("accounts", ¶m) 31 | if err != nil { 32 | Log.E()("数据库查询错误", err.Error()) 33 | context.JSON(http.StatusOK, response.GetFailResponses( 34 | "数据库查询错误或账号密码错误", 35 | errorCode.ERR_MYSQL_FAILED, 36 | )) 37 | return 38 | } 39 | resTrue := res.(*account.Account) 40 | if resTrue.Password == param.Password && resTrue.Account == resTrue.Account { 41 | context.JSON(http.StatusOK, response.GetSuccessResponses("login_succeed")) 42 | return 43 | } 44 | context.JSON(http.StatusOK, response.GetFailResponses( 45 | "login_error", 46 | errorCode.ERR_MYSQL_FAILED, 47 | )) 48 | } 49 | 50 | func Register(context *gin.Context) { 51 | param := account.Account{} 52 | param.CreateTime = time.Now().Format("2006-01-02 15:04:05") 53 | err := context.ShouldBindJSON(¶m) 54 | if err != nil { 55 | Log.E()("参数解析错误") 56 | context.JSON(http.StatusOK, response.GetFailResponses( 57 | "参数解析错误", 58 | errorCode.ERR_SVR_INTERNAL, 59 | )) 60 | return 61 | } 62 | 63 | res := account.Account{} 64 | err = mysql.Conn.Table("accounts").Where("account = ?", param.Account).Scan(&res).Error 65 | if err != nil { 66 | Log.I()(err) 67 | } 68 | if res.Account != "" { 69 | Log.D()(res.Account) 70 | context.JSON(http.StatusOK, response.GetFailResponses( 71 | "注册失败,账号已存在", 72 | errorCode.ERR_MYSQL_FAILED, 73 | )) 74 | return 75 | } 76 | err = mysql.Create(param) 77 | if err != nil { 78 | Log.E()(err.Error()) 79 | context.JSON(http.StatusOK, response.GetFailResponses( 80 | "注册失败,系统错误", 81 | errorCode.ERR_SVR_INTERNAL, 82 | )) 83 | return 84 | } 85 | key := redis.FRIEND_ALL 86 | redis.SAdd(key, param.Account) 87 | context.JSON(http.StatusOK, response.GetSuccessResponses("reg_succeed")) 88 | 89 | } 90 | 91 | func ListAccount(context *gin.Context) { 92 | param := account.Account{} 93 | err := context.ShouldBindJSON(¶m) 94 | if err != nil { 95 | Log.E()("参数解析错误") 96 | context.JSON(http.StatusOK, response.GetFailResponses( 97 | "参数解析错误", 98 | errorCode.ERR_SVR_INTERNAL, 99 | )) 100 | return 101 | } 102 | 103 | allAccount := mysql.FindAllAccount() 104 | 105 | for _, a := range allAccount { 106 | Log.D()(a.Account) 107 | } 108 | context.JSON(http.StatusOK, response.GetSuccessResponses("succeed", allAccount)) 109 | 110 | } 111 | 112 | func AddFriend(context *gin.Context) { 113 | param := account.AddFriendOV{} 114 | err := context.ShouldBindJSON(¶m) 115 | if err != nil { 116 | Log.E()("参数解析错误") 117 | context.JSON(http.StatusOK, response.GetFailResponses( 118 | "参数解析错误", 119 | errorCode.ERR_SVR_INTERNAL, 120 | )) 121 | return 122 | } 123 | 124 | err = mysql.Conn.Table("friends_call").Create(param).Error 125 | if err != nil { 126 | Log.E()(err.Error()) 127 | context.JSON(http.StatusOK, response.GetFailResponses( 128 | "添加好友失败,系统错误", 129 | errorCode.ERR_SVR_INTERNAL, 130 | )) 131 | return 132 | } 133 | 134 | context.JSON(http.StatusOK, response.GetSuccessResponses("发送好友申请成功")) 135 | } 136 | 137 | func AcceptFriend(context *gin.Context) { 138 | param := account.AddFriendOV{} 139 | err := context.ShouldBindJSON(¶m) 140 | if err != nil { 141 | Log.E()("参数解析错误") 142 | context.JSON(http.StatusOK, response.GetFailResponses( 143 | "参数解析错误", 144 | errorCode.ERR_SVR_INTERNAL, 145 | )) 146 | return 147 | } 148 | 149 | //err = mysql.Conn.Table("friends_call").Select("param").Scan(¶m).Error 150 | //if err != nil { 151 | // Log.E()(err.Error()) 152 | // context.JSON(http.StatusOK, response.GetFailResponses( 153 | // "确认添加好友失败,系统错误", 154 | // errorCode.ERR_SVR_INTERNAL, 155 | // )) 156 | // return 157 | //} 158 | 159 | err = mysql.Conn.Table("friends_call"). 160 | Where("source_user = ? and target_user = ?", param.SourceUser, param.TargetUser). 161 | Update("status", 1).Error 162 | 163 | if err != nil { 164 | Log.E()(err.Error()) 165 | context.JSON(http.StatusOK, response.GetFailResponses( 166 | "确认添加好友失败,系统错误", 167 | errorCode.ERR_SVR_INTERNAL, 168 | )) 169 | return 170 | } 171 | 172 | //添加相互的好友缓存 173 | redis.SAdd(param.SourceUser+":"+redis.FRIEND_LIST, param.TargetUser) 174 | redis.SAdd(param.TargetUser+":"+redis.FRIEND_LIST, param.SourceUser) 175 | 176 | context.JSON(http.StatusOK, response.GetSuccessResponses("好友添加成功")) 177 | 178 | } 179 | 180 | func GetFriendList(context *gin.Context) { 181 | param := account.Account{} 182 | err := context.ShouldBindJSON(¶m) 183 | if err != nil { 184 | Log.E()("参数解析错误") 185 | context.JSON(http.StatusOK, response.GetFailResponses( 186 | "参数解析错误", 187 | errorCode.ERR_SVR_INTERNAL, 188 | )) 189 | return 190 | } 191 | 192 | nowUser := param.Account 193 | 194 | members, err := redis.SMembers(nowUser + ":" + redis.FRIEND_LIST) 195 | 196 | if err != nil { 197 | Log.E()("查找用户好友列表失败(redis错误)") 198 | context.JSON(http.StatusOK, response.GetFailResponses( 199 | "查找用户好友列表失败(redis错误)", 200 | errorCode.ERR_REDIS_FAILED, 201 | )) 202 | return 203 | } 204 | 205 | for _, a := range members { 206 | Log.D()(a) 207 | } 208 | context.JSON(http.StatusOK, response.GetSuccessResponses("查询好友列表成功", members)) 209 | 210 | } 211 | 212 | func GetFriendRequestList(context *gin.Context) { 213 | param := account.Account{} 214 | err := context.ShouldBindJSON(¶m) 215 | if err != nil { 216 | Log.E()("参数解析错误") 217 | context.JSON(http.StatusOK, response.GetFailResponses( 218 | "参数解析错误", 219 | errorCode.ERR_SVR_INTERNAL, 220 | )) 221 | return 222 | } 223 | 224 | nowUser := param.Account 225 | 226 | requestList := new([]account.AddFriendOV) 227 | err = mysql.Conn.Table("friends_call").Where("target_user = ? and status = 0", nowUser).Scan(requestList).Error 228 | 229 | if err != nil { 230 | Log.E()("查找用户好友申请列表失败(MySql错误)") 231 | context.JSON(http.StatusOK, response.GetFailResponses( 232 | "查找用户好友申请列表失败(MySql错误)", 233 | errorCode.ERR_MYSQL_FAILED, 234 | )) 235 | return 236 | } 237 | 238 | for _, a := range *requestList { 239 | Log.D()(a.TargetUser) 240 | } 241 | context.JSON(http.StatusOK, response.GetSuccessResponses("查询好友申请列表成功", *requestList)) 242 | 243 | } 244 | -------------------------------------------------------------------------------- /server/thingServer/test.go: -------------------------------------------------------------------------------- 1 | package thingServer 2 | 3 | import ( 4 | "JetIot/util/Log" 5 | "JetIot/util/mysql" 6 | "JetIot/util/redis" 7 | "fmt" 8 | "github.com/gin-gonic/gin" 9 | "time" 10 | //"github.com/jinzhu/gorm" 11 | ) 12 | 13 | type Account struct { 14 | Account string `json:"account"` 15 | Password string `json:"password"` 16 | Name string `json:"name"` 17 | Type int `json:"type"` 18 | CreateTime string `json:create_time` 19 | } 20 | 21 | func Test(context *gin.Context) { 22 | set := redis.Set("time2", "gg") 23 | if set != nil { 24 | Log.D()(set) 25 | } 26 | get, _ := redis.Get("time2") 27 | Log.I()(get) 28 | 29 | format := time.Now().Format("2006-01-02 15:04:05") 30 | account := Account{ 31 | Account: "xsjcool@outook.com", 32 | Name: "xsjcool", 33 | Password: "123", 34 | Type: 0, 35 | CreateTime: format, 36 | } 37 | 38 | mysql.Create(&account) 39 | fmt.Println(account) 40 | } 41 | -------------------------------------------------------------------------------- /server/thingServer/thingMqttHandle.go: -------------------------------------------------------------------------------- 1 | package thingServer 2 | 3 | import ( 4 | "JetIot/model/event" 5 | "JetIot/model/response" 6 | "JetIot/model/thingModel" 7 | "JetIot/util" 8 | "JetIot/util/Log" 9 | "JetIot/util/errorCode" 10 | "JetIot/util/mqtt" 11 | "JetIot/util/redis" 12 | "encoding/json" 13 | mq "github.com/eclipse/paho.mqtt.golang" 14 | ) 15 | 16 | /** 17 | * @Description: 注册回调 18 | * @param client 19 | * @param message 20 | */ 21 | func RegisterThingByMqttHandle(client mq.Client, message mq.Message) { 22 | thing := thingModel.Thing{} 23 | err := mqtt.ShouldBindJSON(message, &thing) 24 | replayTopic := "thingServer/function/register/" + thing.Id 25 | 26 | if err != nil { 27 | Log.E()("参数解析错误") 28 | mqtt.Replay(replayTopic, response.GetMqttFailResponses( 29 | "参数解析错误", 30 | event.EVENT_OTHER, 31 | errorCode.ERR_SVR_INTERNAL, 32 | )) 33 | return 34 | } 35 | 36 | marshal, _ := json.Marshal(thing) 37 | 38 | //保存到缓存库 39 | err = redis.Set("thingServer:"+thing.Id, string(marshal)) 40 | if err != nil { 41 | Log.E()("保存到缓存库错误" + err.Error()) 42 | mqtt.Replay(replayTopic, response.GetMqttFailResponses( 43 | "保存到缓存库错误", 44 | event.EVENT_OTHER, 45 | errorCode.ERR_SVR_INTERNAL, 46 | )) 47 | return 48 | } 49 | mqtt.Replay(replayTopic, response.GetMqttSuccessResponses("注册完成", event.EVENT_OTHER)) 50 | } 51 | 52 | /** 53 | * @Description: 设置解析并存储来自设备的值 54 | * @param context 55 | */ 56 | func SetThingComponentValueByMqttHandle(client mq.Client, message mq.Message) { 57 | ov := thingModel.ThingComponentValueOV{} 58 | err := mqtt.ShouldBindJSON(message, &ov) 59 | if err != nil { 60 | Log.E()("参数解析错误" + err.Error()) 61 | return 62 | } 63 | 64 | thing, err := util.LoadThing(ov.Id) 65 | if err != nil { 66 | Log.E()("加载错误" + err.Error()) 67 | return 68 | } 69 | 70 | thing.Components[ov.ComponentName].Do(ov.Value) 71 | err = util.Commit(thing) 72 | if err != nil { 73 | Log.E()("更新缓存错误" + err.Error()) 74 | return 75 | } 76 | } 77 | 78 | /** 79 | * @Description: 设备上线回调 80 | * @param client mqtt客户端 81 | * @param message 消息对象 82 | */ 83 | func DeviceOnlineByMqttHandle(client mq.Client, message mq.Message) { 84 | ov := thingModel.ThingInitOV{} 85 | err := mqtt.ShouldBindJSON(message, &ov) 86 | if err != nil { 87 | Log.E()("参数解析错误" + err.Error()) 88 | return 89 | } 90 | 91 | isReg := util.IsRegisterThing(ov.Id) 92 | if isReg { 93 | Log.D()("设备已经注册") 94 | err := util.SetDeviceOnlineStatus(ov.Id, true) 95 | if err != nil { 96 | mqtt.ReplayToDevice(ov.Id, response.GetMqttFailResponses("上线失败", ov.EventId, errorCode.ERR_SVR_INTERNAL)) 97 | } 98 | mqtt.ReplayToDevice(ov.Id, response.GetMqttSuccessResponses("设备已经注册,并上线", ov.EventId, nil)) 99 | 100 | return 101 | } else { 102 | Log.D()("设备未注册") 103 | mqtt.ReplayToDevice(ov.Id, response.GetMqttFailResponses("设备未注册", ov.EventId, errorCode.ERR_DEVICE_NOT_FIND)) 104 | 105 | return 106 | } 107 | } 108 | 109 | /** 110 | * @Description: 设备下线回调 111 | * @param client mqtt客户端 112 | * @param message 消息对象 113 | */ 114 | func DeviceOfflineByMqttHandle(client mq.Client, message mq.Message) { 115 | Log.D()("遗嘱内容", string(message.Payload())) 116 | ov := thingModel.ThingInitOV{} 117 | err := mqtt.ShouldBindJSON(message, &ov) 118 | if err != nil { 119 | Log.E()("参数解析错误" + err.Error()) 120 | return 121 | } 122 | 123 | if util.IsRegisterThing(ov.Id) { 124 | err := util.SetDeviceOnlineStatus(ov.Id, false) 125 | if err != nil { 126 | Log.E()("设备下线失败,但是已经离线", err.Error(), "ID:", ov.Id) 127 | return 128 | } 129 | } else { 130 | Log.E()("未注册设备") 131 | return 132 | } 133 | 134 | } 135 | -------------------------------------------------------------------------------- /util/Log/Log.go: -------------------------------------------------------------------------------- 1 | package Log 2 | 3 | import ( 4 | "github.com/sirupsen/logrus" 5 | "os" 6 | "runtime" 7 | "strconv" 8 | ) 9 | 10 | var logger = logrus.New() 11 | var Entry *logrus.Entry 12 | 13 | func init() { 14 | logger.SetOutput(os.Stdout) 15 | logger.SetLevel(logrus.DebugLevel) 16 | logger.Formatter = &logrus.TextFormatter{ 17 | TimestampFormat: "2006-01-02 15:04:05", 18 | FullTimestamp: true, 19 | CallerPrettyfier: callerFormatter, 20 | } 21 | logger.SetReportCaller(true) 22 | Entry = logger.WithFields(logrus.Fields{}) 23 | } 24 | 25 | func callerFormatter(frame *runtime.Frame) (function string, file string) { 26 | line := frame.Line 27 | funcName := "[" + frame.Function + "]" + "[" + strconv.Itoa(line) + "]" 28 | return funcName, "" 29 | } 30 | 31 | func D() func(args ...interface{}) { 32 | logger.SetReportCaller(true) 33 | return Entry.Debug 34 | } 35 | 36 | func W() func(args ...interface{}) { 37 | logger.SetReportCaller(true) 38 | return Entry.Warn 39 | } 40 | 41 | func E() func(args ...interface{}) { 42 | logger.SetReportCaller(true) 43 | return Entry.Error 44 | } 45 | 46 | func I() func(args ...interface{}) { 47 | logger.SetReportCaller(false) 48 | return Entry.Info 49 | } 50 | -------------------------------------------------------------------------------- /util/errorCode/errorCode.go: -------------------------------------------------------------------------------- 1 | package errorCode 2 | 3 | const ( 4 | ERR_NO_ERROR = 0 5 | ERR_UNSUPPORT_VER = 1 // 版本不支持 6 | ERR_AUTH_FAILED = 2 // 用户名密码错误 7 | ERR_VA_CODE_MISMATCH = 3 // VA CODE mimatched. 8 | ERR_VA_CODE_TIMEOUT = 4 // 验证码超时 9 | ERR_USER_NON_EXIST = 5 // mysql中用户不存在 10 | ERR_USER_EXISTS = 6 // mysql中用户存在 11 | ERR_MYSQL_FAILED = 7 // mysql查询错误 12 | ERR_REDIS_FAILED = 8 // redis查询错误 13 | ERR_LAST_VA_CODE_STILL_VALID = 9 // 上一个VA_CODE 依然有效,不能产生新的 14 | ERR_ADD_MSG_FAILED = 10 // 邮件或者短信加入队列失败 15 | ERR_APP_NO_NEW_VER = 11 // No other new app version 16 | ERR_ONE_HOUR_SMS_OVERFLOW = 12 // 一小时内发送短信已达上限 17 | ERR_OTHER_APP_ONLINE = 13 // 其他app已经登录该帐号 18 | ERR_ACCOUNT_WARNING = 14 // 帐号不安全,重新设置密码 19 | ERR_ONE_DAY_SMS_OVERFLOW = 15 // 二十四小时内发送短信已达上限 20 | ERR_SHARE_ACCOUNT_CNT_OVER_FLOW = 16 // 授权帐号个数已达上线 21 | ERR_ACCOUNT_HAD_PERMISSION = 17 // 帐号已经有权限 22 | ERR_NEW_APP_MANAGE = 18 // 有新帐号来管理,失去管理权限 23 | ERR_OLD_APP_MANAGE = 19 // 其老帐号正在管理,不能进行管理 24 | ERR_TOKEN_EXPIRED = 20 // token超时,请重新获取token 25 | ERR_APP_NEW_VER = 21 // 当前app有更新版本 26 | ERR_SVR_INTERNAL = 22 // 服务器内部错误 27 | ERR_DATA_ERR = 23 // 来源数据错误 28 | ERR_SIG_ERR = 24 // 签名错误 29 | ERR_USER_REDIS_NON_EXIST = 25 // redis中用户不存在 30 | ERR_THIRD_NAME_BINDED = 26 // 第三方账号已绑定 31 | ERR_OLD_PASSWD = 27 // 旧密码错误 32 | ERR_ACCOUNT_STILL_BOUND_DEVICE = 28 // 账号下仍有绑定设备 33 | ERR_MEMBER_REQ_NON_EXIST = 29 // 好友请求不存在 34 | ERR_ORDER_NO_PRODUCE_FAILED = 30 // 订单号生成失败 35 | ERR_MEMBERSHIP_ALREADY_EXIST = 31 // 好友关系已存在 36 | ERR_HAS_NO_FRIENDS = 32 // 没有好友 37 | ERR_TELBOOK_MATCH_NOTHING = 33 // 没有匹配的电话簿 38 | ERR_ORDER_PAID = 34 // 订单已支付 39 | ERR_ORDER_NOT_PAY = 35 // 订单未支付 40 | ERR_ORDER_CLOSED = 36 // 订单已关闭 41 | ERR_ORDER_PAYERROR = 37 // 订单支付失败,非内部原因 42 | ERR_DEV_BINDED = 38 // 设备已被绑定 43 | ERR_ACCOUNT_LOGOUT = 39 // 账号已被注销 44 | ERR_MAX = 40 45 | ERR_DEVICE_NOT_FIND = 41 // 设备未注册 46 | ERR_DEVICE_LIST_EMPTY = 42 // 设备未注册 47 | ) 48 | -------------------------------------------------------------------------------- /util/mqtt/mqtt.go: -------------------------------------------------------------------------------- 1 | package mqtt 2 | 3 | import ( 4 | "JetIot/conf" 5 | "JetIot/util/Log" 6 | "encoding/json" 7 | mq "github.com/eclipse/paho.mqtt.golang" 8 | ) 9 | 10 | var Client mq.Client 11 | 12 | type handleFunc struct { 13 | funcName string 14 | handle mq.MessageHandler 15 | } 16 | 17 | var EventHandleList map[int]handleFunc 18 | 19 | func InitMqttClient() { 20 | EventHandleList = make(map[int]handleFunc, 0) 21 | opts := mq.NewClientOptions() 22 | opts.AddBroker("tcp://"+conf.Default.MqttServer+":"+conf.Default.MqttPort). 23 | SetClientID(conf.Default.MqttClientID). 24 | SetUsername(conf.Default.MqttUserName). 25 | SetPassword(conf.Default.MqttPassword). 26 | SetWill("server_will", "lose_connect", 2, false) 27 | 28 | Client = mq.NewClient(opts) 29 | if token := Client.Connect(); token.Wait() && token.Error() != nil { 30 | Log.E()("初始化Mqtt客户端失败" + token.Error().Error()) 31 | return 32 | } 33 | Client.Publish("server_will", 2, true, "server_start") 34 | Log.I()("初始化Mqtt客户端完成") 35 | 36 | } 37 | 38 | func Publish(topic string, msg string) { 39 | Log.D()("topic:" + topic + " msg:" + msg) 40 | token := Client.Publish(topic, 1, false, msg) 41 | if token.Error() != nil { 42 | Log.E()("发布消息失败" + token.Error().Error()) 43 | return 44 | } 45 | if !token.Wait() { 46 | Log.E()("发布发送等待") 47 | return 48 | } 49 | } 50 | 51 | func Subscribe(topic string, callback mq.MessageHandler) { 52 | if token := Client.Subscribe(topic, 0, callback); token.Wait() && token.Error() != nil { 53 | Log.E()("订阅失败" + token.Error().Error()) 54 | } 55 | } 56 | 57 | func Unsubscribe(topic string) { 58 | if token := Client.Unsubscribe(topic); token.Wait() && token.Error() != nil { 59 | Log.E()("取消订阅失败" + token.Error().Error()) 60 | } 61 | } 62 | 63 | func Replay(topic string, replay interface{}) { 64 | marshal, err := json.Marshal(replay) 65 | if err != nil { 66 | Log.E()("回复失败" + err.Error()) 67 | return 68 | } 69 | Publish(topic, string(marshal)) 70 | } 71 | 72 | func ReplayToDevice(deviceId string, replay interface{}) { 73 | marshal, err := json.Marshal(replay) 74 | if err != nil { 75 | Log.E()("回复失败" + err.Error()) 76 | return 77 | } 78 | Publish("thingServer/entity/"+deviceId+"/todevice", string(marshal)) 79 | } 80 | 81 | /** 82 | * @Description: 注册订阅事件回调函数 83 | * @param eventId 事件ID 84 | * @param eventName 事件名称 85 | * @param handle 回调函数 86 | */ 87 | func RegisterEventHandle(eventId int, eventName string, handle mq.MessageHandler) { 88 | Log.I()("注册事件:", eventName, " \t--> EVENT_ID:", eventId) 89 | handleFunc := handleFunc{ 90 | funcName: eventName, 91 | handle: handle, 92 | } 93 | EventHandleList[eventId] = handleFunc 94 | } 95 | 96 | func EventListenStart() { 97 | Log.I()("初始化事件系统") 98 | Subscribe("thingServer/entity/toserver", DealEventHandle) 99 | } 100 | 101 | func DealEventHandle(client mq.Client, message mq.Message) { 102 | payload := struct { 103 | EventId int `json:"event_id"` 104 | }{} 105 | err := ShouldBindJSON(message, &payload) 106 | if err != nil { 107 | Log.E()("解析事件ID错误") 108 | } 109 | Log.D()("出现事件:", payload.EventId) 110 | handleFunc, exist := EventHandleList[payload.EventId] 111 | if !exist { 112 | Log.E()("EevntId:", payload.EventId, " 不存在") 113 | return 114 | } 115 | Log.D()("调用方法: ", handleFunc.funcName) 116 | handleFunc.handle(client, message) 117 | } 118 | 119 | /** 120 | * @Description: 解析来着设备传递的Json字符串到对象 121 | * @param message 数据对象 122 | * @param model 解析目标对象的指针 123 | * @return error 124 | */ 125 | func ShouldBindJSON(message mq.Message, model interface{}) error { 126 | payload := message.Payload() 127 | Log.D()(string(payload)) 128 | err := json.Unmarshal(payload, model) 129 | return err 130 | } 131 | -------------------------------------------------------------------------------- /util/mysql/mysql.go: -------------------------------------------------------------------------------- 1 | package mysql 2 | 3 | import ( 4 | "JetIot/conf" 5 | "JetIot/model/account" 6 | "github.com/jinzhu/gorm" 7 | _ "github.com/jinzhu/gorm/dialects/mysql" 8 | "time" 9 | ) 10 | 11 | var Conn *gorm.DB 12 | 13 | func InitDB() { 14 | var err error 15 | mysqlUrl := "root:" + conf.Default.MysqlPassword + "@tcp(" + conf.Default.MysqlServer + ":3306)/cloudDB?charset=utf8mb4&parseTime=true" 16 | Conn, err = gorm.Open("mysql", mysqlUrl) 17 | if err != nil { 18 | panic(err) 19 | } 20 | //DbConn.AutoMigrate(&Post{}, &Comment{}) 21 | } 22 | 23 | func Create(model interface{}) error { 24 | create := Conn.Create(model) 25 | err := create.Error 26 | return err 27 | } 28 | 29 | /** 30 | * @Description: 根据条件查询数据库 31 | * @param table 表名 32 | * @param query 查询用的对象 33 | * @param columns 筛选的字段,可以为空则查找全部 34 | * @return interface{} 返回的空接口中为对象的指针 35 | * @return error 36 | */ 37 | func Find(table string, query interface{}, columns ...string) (interface{}, error) { 38 | var err error 39 | if len(columns) == 0 { 40 | err = Conn.Table(table).Where(query).Scan(query).Error 41 | } else { 42 | err = Conn.Table(table).Select(columns).Where(query).Scan(query).Error 43 | } 44 | return query, err 45 | } 46 | 47 | func FindAllAccount() []account.Account { 48 | //var err error 49 | //if len(columns) == 0 { 50 | // err = Conn.Table(table).Scan("*").Error 51 | //} else { 52 | // err = Conn.Table(table).Select(columns).Scan(columns).Error 53 | //} 54 | //return query, err 55 | 56 | a := make([]account.Account, 0) 57 | Conn.Table("accounts").Select([]string{"account", "name"}).Scan(&a) 58 | return a 59 | 60 | } 61 | 62 | /** 63 | * @Description: 获取数据库格式化时间 64 | * @return string 65 | */ 66 | func GetFormatNowTime() string { 67 | format := time.Now().Format("2006-01-02 15:04:05") 68 | return format 69 | } 70 | -------------------------------------------------------------------------------- /util/public.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import ( 4 | "JetIot/model/account" 5 | "JetIot/model/thingModel" 6 | "JetIot/util/Log" 7 | "JetIot/util/mysql" 8 | "JetIot/util/redis" 9 | "encoding/json" 10 | ) 11 | 12 | func LoadThing(id string) (*thingModel.Thing, error) { 13 | thing := thingModel.Thing{} 14 | get, err := redis.Get("thingServer:" + id) 15 | if err != nil { 16 | Log.E()("查找物体错误" + err.Error()) 17 | return &thing, err 18 | } 19 | 20 | err = json.Unmarshal(get, &thing) 21 | if err != nil { 22 | Log.E()("解析物体错误" + err.Error()) 23 | return &thing, err 24 | } 25 | 26 | return &thing, nil 27 | } 28 | 29 | func Commit(thing *thingModel.Thing) error { 30 | marshal, _ := json.Marshal(*thing) 31 | Log.D()(string(marshal)) 32 | //保存到缓存库 33 | err := redis.Set("thingServer:"+thing.Id, string(marshal)) 34 | if err != nil { 35 | Log.E()("保存到缓存库错误" + err.Error()) 36 | return err 37 | } 38 | return nil 39 | } 40 | 41 | func IsRegisterThing(id string) bool { 42 | _, err := redis.Get("thingServer:" + id) 43 | if err != nil { 44 | return false 45 | } 46 | return true 47 | } 48 | 49 | /** 50 | * @Description: 设置设备在线状态 在线:1 离线:0 51 | * @param id 设备id 52 | * @param isOnline 是否在线 53 | */ 54 | func SetDeviceOnlineStatus(id string, isOnline bool) error { 55 | if isOnline { 56 | err := redis.Set("thingServer:online_status:"+id, 1) 57 | if err != nil { 58 | Log.E()(id, "更新设备状态错误", err) 59 | return err 60 | } 61 | } else { 62 | err := redis.Set("thingServer:online_status:"+id, 0) 63 | if err != nil { 64 | Log.E()(id, "更新设备状态错误", err) 65 | return err 66 | } 67 | } 68 | return nil 69 | } 70 | 71 | /** 72 | * @Description: 查询设备是否绑定 73 | * @param id 设备ID 74 | * @return bool 是否被绑定 75 | */ 76 | func IsDeviceBinding(id string) bool { 77 | device := account.BindingDevice{} 78 | err := mysql.Conn.Select("account").Table("binding_devices").Where("device_id = ?", id).Scan(&device).Error 79 | if err != nil { 80 | Log.I()("未找到数据,设备", id, "未绑定", err) 81 | return false 82 | } else { 83 | Log.I()("设备", id, "已经绑定用户:", device.Account) 84 | return true 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /util/redis/redis.go: -------------------------------------------------------------------------------- 1 | package redis 2 | 3 | import ( 4 | "JetIot/conf" 5 | "github.com/go-redis/redis" 6 | ) 7 | 8 | var redisDb *redis.Client 9 | 10 | const ( 11 | FRIEND_ALL = "all_user_list" 12 | FRIEND_LIST = "friend_list" 13 | GROUP_LIST = "group_list" 14 | ) 15 | 16 | func InitRedis() { 17 | redisDb = redis.NewClient(&redis.Options{ 18 | Addr: conf.Default.RedisServer + ":" + conf.Default.RedisPort, // use default Addr 19 | Password: conf.Default.RedisPassword, // no password set 20 | DB: 0, // use default DB 21 | }) 22 | } 23 | 24 | func Set(key string, value interface{}) error { 25 | err := redisDb.Set(key, value, 0).Err() 26 | return err 27 | } 28 | 29 | func Get(key string) ([]byte, error) { 30 | get := redisDb.Get(key) 31 | return get.Bytes() 32 | } 33 | 34 | func Del(key string) error { 35 | err := redisDb.Del(key).Err() 36 | return err 37 | } 38 | 39 | func SAdd(key string, value interface{}) { 40 | redisDb.SAdd(key, value) 41 | } 42 | 43 | func SMembers(key string) ([]string, error) { 44 | members := redisDb.SMembers(key) 45 | result, err := members.Result() 46 | return result, err 47 | } 48 | --------------------------------------------------------------------------------