mirror of
https://gitee.com/dgiiot/dgiot.git
synced 2024-12-02 12:18:30 +08:00
delete sin
This commit is contained in:
parent
d4bf9f0ea6
commit
3635267c2e
6
apps/dgiot_sinmahe/.gitignore
vendored
6
apps/dgiot_sinmahe/.gitignore
vendored
@ -1,6 +0,0 @@
|
||||
.eunit
|
||||
deps
|
||||
*.o
|
||||
*.beam
|
||||
*.plt
|
||||
erl_crash.dump
|
@ -1,201 +0,0 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
@ -1,28 +0,0 @@
|
||||
PROJECT = dgiot_sinmahe
|
||||
PROJECT_DESCRIPTION = dgiot_sinmahe
|
||||
|
||||
CUR_BRANCH := $(shell git branch | grep -e "^*" | cut -d' ' -f 2)
|
||||
BRANCH := $(if $(filter $(CUR_BRANCH), master develop), $(CUR_BRANCH), develop)
|
||||
|
||||
BUILD_DEPS = emqx cuttlefish ekaf jsx brod supervisor3
|
||||
dep_emqx = git-emqx https://github.com/emqx/emqx $(BRANCH)
|
||||
dep_cuttlefish = git-emqx https://github.com/emqx/cuttlefish v2.2.1
|
||||
|
||||
ERLC_OPTS += +debug_info
|
||||
|
||||
ERLC_OPTS += +'{parse_transform, lager_transform}'
|
||||
|
||||
NO_AUTOPATCH = cuttlefish
|
||||
|
||||
COVER = true
|
||||
|
||||
ERLC_OPTS += +'{parse_transform, lager_transform}'
|
||||
|
||||
$(shell [ -f erlang.mk ] || curl -s -o erlang.mk https://raw.githubusercontent.com/emqx/erlmk/master/erlang.mk)
|
||||
|
||||
include erlang.mk
|
||||
|
||||
app:: rebar.config
|
||||
|
||||
app.config::
|
||||
./deps/cuttlefish/cuttlefish -l info -e etc/ -c etc/dgiot_sinmahe.conf -i priv/dgiot_sinmahe.schema -d data
|
@ -1,4 +0,0 @@
|
||||
## dgiot_sinmahe
|
||||
|
||||
dgiot_sinmahe
|
||||
|
1
apps/dgiot_sinmahe/erlang.mk
vendored
1
apps/dgiot_sinmahe/erlang.mk
vendored
@ -1 +0,0 @@
|
||||
include ../../erlang.mk
|
@ -1,17 +0,0 @@
|
||||
##--------------------------------------------------------------------
|
||||
## iot_dgiot_sinmahe Plugin
|
||||
##--------------------------------------------------------------------
|
||||
## dgiot iot app id
|
||||
iot_dgiot_sinmahe.iotserver = http://sinmahe.iotn2n.com:5080/iotapi
|
||||
|
||||
## dgiot iot app id
|
||||
iot_dgiot_sinmahe.appid = 1bhUhR0LhK
|
||||
|
||||
## dgiot iot app secret
|
||||
iot_dgiot_sinmahe.appsecret = STEyOTg1NzkxNTg3NzE3Nzk3NzU0
|
||||
|
||||
## dgiot iot port
|
||||
iot_dgiot_sinmahe.port = 4081
|
||||
|
||||
|
||||
|
@ -1,18 +0,0 @@
|
||||
%%--------------------------------------------------------------------
|
||||
%% Copyright (c) 2020-2021 DGIOT Technologies Co., Ltd. All Rights Reserved.
|
||||
%%
|
||||
%% Licensed under the Apache License, Version 2.0 (the "License");
|
||||
%% you may not use this file except in compliance with the License.
|
||||
%% You may obtain a copy of the License at
|
||||
%%
|
||||
%% http://www.apache.org/licenses/LICENSE-2.0
|
||||
%%
|
||||
%% Unless required by applicable law or agreed to in writing, software
|
||||
%% distributed under the License is distributed on an "AS IS" BASIS,
|
||||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
%% See the License for the specific language governing permissions and
|
||||
%% limitations under the License.
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
-define(DGIOT_SINMAHE_DTU, dgiot_sinmahe_dtu_ets).
|
||||
|
@ -1,14 +0,0 @@
|
||||
[
|
||||
{
|
||||
"name": "mqtt_online",
|
||||
"help": "mqtt客户端在线数据统计",
|
||||
"type": "gauge",
|
||||
"labels": []
|
||||
},
|
||||
{
|
||||
"name": "mqtt_recv",
|
||||
"help": "mqtt客户端收包数据统计",
|
||||
"type": "gauge",
|
||||
"labels": []
|
||||
}
|
||||
]
|
@ -1,20 +0,0 @@
|
||||
{mapping, "iot_dgiot_sinmahe.iotserver", "dgiot_sinmahe.iotserver", [
|
||||
{default, ""},
|
||||
{datatype, string}
|
||||
]}.
|
||||
|
||||
{mapping, "iot_dgiot_sinmahe.appid", "dgiot_sinmahe.appid", [
|
||||
{default, ""},
|
||||
{datatype, string}
|
||||
]}.
|
||||
|
||||
{mapping, "iot_dgiot_sinmahe.appsecret", "dgiot_sinmahe.appsecret", [
|
||||
{default, ""},
|
||||
{datatype, string}
|
||||
]}.
|
||||
|
||||
{mapping, "iot_dgiot_sinmahe.port", "dgiot_sinmahe.port", [
|
||||
{datatype, integer},
|
||||
{default, 4081}
|
||||
]}.
|
||||
|
@ -1,197 +0,0 @@
|
||||
# coding=utf8
|
||||
import os,sys
|
||||
import requests
|
||||
import json
|
||||
import base64
|
||||
from dateutil.parser import parse
|
||||
import datetime
|
||||
import hashlib
|
||||
import time
|
||||
exec_count = 0
|
||||
from threading import Timer
|
||||
|
||||
|
||||
groupSession = ''
|
||||
globalEnv = {}
|
||||
s = requests.session()
|
||||
host = '47.110.83.36'
|
||||
|
||||
# 打印时间函数
|
||||
def printTime(inc):
|
||||
global exec_count
|
||||
if (exec_count < 1):
|
||||
t = Timer(inc, printTime, (inc,))
|
||||
t.start()
|
||||
else:
|
||||
os._exit(0)
|
||||
exec_count = exec_count + 1
|
||||
|
||||
def login():
|
||||
url = 'http://{}/iotapi/login'.format(host)
|
||||
headers = {'accept': 'application/json', "Content-Type": "text/plain"}
|
||||
body = {"username": 'sinmahedev', "password": '123456'}
|
||||
r = s.post(url, headers=headers, data=json.dumps(body))
|
||||
global globalEnv
|
||||
globalEnv = json.loads(r.content) # 解码JSON对象
|
||||
global groupSession
|
||||
groupSession = r.json()['sessionToken']
|
||||
s.headers.update({"sessionToken": groupSession, 'Content-Type': 'application/json'})
|
||||
|
||||
def get_deviceid(productid,devaddr):
|
||||
data = 'Device' + productid + devaddr
|
||||
print(data)
|
||||
md5 = hashlib.md5(data.encode(encoding='UTF-8')).hexdigest()
|
||||
return md5[0:10]
|
||||
|
||||
def time_data1(time_sj): #传入单个时间比如'2019-8-01 00:00:00',类型为str
|
||||
data_sj = time.strptime(time_sj,"%Y-%m-%d %H:%M:%S") #定义格式
|
||||
time_int = int(time.mktime(data_sj))
|
||||
return time_int #返回传入时间的时间戳,类型为int
|
||||
|
||||
def time_data2(time_sj): #传入参数
|
||||
data_sj = time.localtime(time_sj)
|
||||
time_str = time.strftime("%Y-%m-%d %H:%M:%S",data_sj) #时间戳转换正常时间
|
||||
return time_str #返回日期,格式为str
|
||||
|
||||
def post(args,session,env):
|
||||
body = json.loads(base64.b64decode(args).decode("utf-8"))
|
||||
#print(body)
|
||||
url = 'http://{}:6020/rest/sql'.format(host)
|
||||
# curl -H 'Authorization: Basic <TOKEN>' -d '<SQL>' <ip>:<PORT>/rest/sql
|
||||
# TOKEN为{username}:{password}经过Base64编码之后的字符串,例如root:taosdata编码后为cm9vdDp0YW9zZGF0YQ==
|
||||
headers = {'Authorization': 'Basic cm9vdDp0YW9zZGF0YQ=='}
|
||||
deviceid = get_deviceid(body['productid'],body['devaddr'])
|
||||
timeint = body['timeint']+1
|
||||
# print(timeint)
|
||||
delta = datetime.timedelta(days=1)
|
||||
date1 = datetime.date.today()
|
||||
rows = []
|
||||
|
||||
flag=True
|
||||
for i in range(0,timeint):
|
||||
strdate = date1.strftime('%Y-%m-%d')
|
||||
|
||||
date2 = date1+delta
|
||||
strdate1 = date1.strftime('"%Y-%m-%d %H:%M:%S.000"')
|
||||
|
||||
strdate2 = date2.strftime('"%Y-%m-%d %H:%M:%S.000"')
|
||||
data = "select createdat,servicetime, IdleTime, uptime, downtime from _" + body['productid'] + "._" + deviceid + " where createdat > " + strdate1 + " and createdat <" + strdate2 + " ;"
|
||||
#print(data)
|
||||
date1 = date1 - delta
|
||||
r = requests.post(url, headers=headers,data=data)
|
||||
t = r.content # 读取返回的内容
|
||||
t = json.loads(t) # 解码JSON对象
|
||||
#print( json.JSONEncoder().encode(t))
|
||||
if t['data']==[]:
|
||||
item={ "日期": strdate, "服务时间": "0h" , "空闲时间": "0h" ,"上行时间": "0h", "下行时间":"0h"}
|
||||
print(item)
|
||||
rows.append(item)
|
||||
continue
|
||||
|
||||
#print(json.JSONEncoder().encode(t))
|
||||
k=t['rows']
|
||||
servicetime=0
|
||||
idletime=0
|
||||
uptime=0
|
||||
downtime=0
|
||||
if flag==True:
|
||||
flag=False
|
||||
for j in range(0,t['rows']):
|
||||
try:
|
||||
if j>=0 and j<k-1:
|
||||
if t['data'][j+1][1]>=t['data'][j][1]:
|
||||
servicetime+=(t['data'][j+1][1]-t['data'][j][1])/60
|
||||
servicetime = round(servicetime, 3)
|
||||
if t['data'][j+1][2]>=t['data'][j][2]:
|
||||
idletime+=(t['data'][j+1][2]-t['data'][j][2])/60
|
||||
idletime = round(idletime, 3)
|
||||
if t['data'][j+1][3]>=t['data'][j][3]:
|
||||
uptime+=(t['data'][j+1][3]-t['data'][j][3])/60
|
||||
uptime = round(uptime, 3)
|
||||
if t['data'][j+1][4]>=t['data'][j][4]:
|
||||
downtime+=(t['data'][j+1][4]-t['data'][j][4])/60
|
||||
downtime = round(downtime, 3)
|
||||
else:
|
||||
pass
|
||||
except:
|
||||
pass
|
||||
item={ "日期": strdate, "服务时间": str(servicetime)+"h" , "空闲时间": str(idletime)+"h" ,"上行时间": str(uptime)+"h", "下行时间":str(downtime)+"h"}
|
||||
print(item)
|
||||
rows.append(item)
|
||||
pre = t['data'][0]
|
||||
# print(pre)
|
||||
else:
|
||||
for j in range(0,t['rows']):
|
||||
try:
|
||||
if j>=0 and j<k-1:
|
||||
if t['data'][j+1][1]>=t['data'][j][1]:
|
||||
servicetime+=(t['data'][j+1][1]-t['data'][j][1])/60
|
||||
servicetime = round(servicetime, 3)
|
||||
if t['data'][j+1][2]>=t['data'][j][2]:
|
||||
idletime+=(t['data'][j+1][2]-t['data'][j][2])/60
|
||||
idletime = round(idletime, 3)
|
||||
if t['data'][j+1][3]>=t['data'][j][3]:
|
||||
uptime+=(t['data'][j+1][3]-t['data'][j][3])/60
|
||||
uptime = round(uptime, 3)
|
||||
if t['data'][j+1][4]>=t['data'][j][4]:
|
||||
downtime+=(t['data'][j+1][4]-t['data'][j][4])/60
|
||||
downtime = round(downtime, 3)
|
||||
else:
|
||||
if t['data'][j][1] <= pre[1]:
|
||||
servicetime += (pre[1]-t['data'][j][1])/60
|
||||
servicetime = round(servicetime, 3)
|
||||
if t['data'][j][2] <= pre[2]:
|
||||
idletime += (pre[2]-t['data'][j][2])/60
|
||||
idletime = round(idletime, 3)
|
||||
if t['data'][j][3] <=pre[3]:
|
||||
uptime += (pre[3]-t['data'][j][3])/60
|
||||
uptime = round(uptime, 3)
|
||||
if t['data'][j][4]<=pre[4]:
|
||||
downtime += (pre[4]-t['data'][j][4])/60
|
||||
downtime = round(downtime, 3)
|
||||
except:
|
||||
pass
|
||||
item={ "日期": strdate, "服务时间": str(servicetime)+"h", "空闲时间": str(idletime)+"h","上行时间": str(uptime)+"h","下行时间":str(downtime)+"h"}
|
||||
print(item)
|
||||
rows.append(item)
|
||||
pre = t['data'][0]
|
||||
# print(pre)
|
||||
|
||||
deviceData = {
|
||||
"columns": ["日期", "小时数"],
|
||||
"rows": rows
|
||||
}
|
||||
|
||||
data = json.JSONEncoder().encode(deviceData)
|
||||
enbody = base64.b64encode(data.encode('utf-8'))
|
||||
print(data)
|
||||
printTime(5)
|
||||
return enbody
|
||||
|
||||
|
||||
|
||||
|
||||
def main():
|
||||
global groupSession
|
||||
global globalEnv
|
||||
login()
|
||||
body = {
|
||||
"productid": "389a16cda1",
|
||||
"devaddr": "BasicInformation_20083411",
|
||||
"timeint": 30
|
||||
#"devaddr": "BasicInformation_20100629"
|
||||
}
|
||||
|
||||
data = json.JSONEncoder().encode(body)
|
||||
argvs = base64.b64encode(data.encode('utf-8'))
|
||||
print(argvs)
|
||||
data = json.JSONEncoder().encode(globalEnv)
|
||||
env = base64.b64encode(data.encode('utf-8'))
|
||||
print(env)
|
||||
return post(argvs, groupSession, env)
|
||||
|
||||
def exit():
|
||||
os._exit(0)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
@ -1,525 +0,0 @@
|
||||
{
|
||||
"FaultType": {
|
||||
"0": "正常",
|
||||
"1": "变频故障",
|
||||
"2": "系统故障",
|
||||
"3": "参数故障"
|
||||
},
|
||||
"FaultLevel": {
|
||||
"0": "正常",
|
||||
"1": "最高级故障",
|
||||
"2": "二级故障",
|
||||
"3": "三级故障",
|
||||
"4": "四级故障",
|
||||
"5": "五级故障",
|
||||
"6": "六级故障"
|
||||
},
|
||||
"FaultCode": {
|
||||
"Err1": [
|
||||
{
|
||||
"id": 1,
|
||||
"code": 0,
|
||||
"desc": "失速保护",
|
||||
"cause": [
|
||||
"负载过重",
|
||||
"加减速时间过短",
|
||||
"F1-10 空载电流设置过小"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err2": [
|
||||
{
|
||||
"id": 2,
|
||||
"code": 0,
|
||||
"desc": "加速过电流",
|
||||
"cause": [
|
||||
"主回路输出接地或短路",
|
||||
"电机是否进行了参数调谐",
|
||||
"负载太大"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err3": [
|
||||
{
|
||||
"id": 3,
|
||||
"code": 0,
|
||||
"desc": "减速过电流",
|
||||
"cause": [
|
||||
"主回路输出接地或短路",
|
||||
"电机是否进行了参数调谐",
|
||||
"负载太大",
|
||||
"减速曲线太陡"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err4": [
|
||||
{
|
||||
"id": 4,
|
||||
"code": 0,
|
||||
"desc": "减速过电流",
|
||||
"cause": [
|
||||
"主回路输出接地或短路",
|
||||
"电机是否进行了参数调谐",
|
||||
"负载太大",
|
||||
"减速曲线太陡"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err5": [
|
||||
{
|
||||
"id": 5,
|
||||
"code": 0,
|
||||
"desc": "加速过电压",
|
||||
"cause": [
|
||||
"输入电压过高;",
|
||||
"起重机倒拉严重",
|
||||
"制动电阻选择偏大,或制动单元异常",
|
||||
"加速曲线太陡"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err6": [
|
||||
{
|
||||
"id": 6,
|
||||
"code": 0,
|
||||
"desc": "减速过电压",
|
||||
"cause": [
|
||||
"输入电压过高;",
|
||||
"起重机倒拉严重",
|
||||
"制动电阻选择偏大,或制动单元异常",
|
||||
"减速曲线太陡"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err7": [
|
||||
{
|
||||
"id": 7,
|
||||
"code": 0,
|
||||
"desc": "恒速过电压",
|
||||
"cause": [
|
||||
"输入电压过高;",
|
||||
"制动电阻选择偏大,或制动单元异常"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err8": [
|
||||
{
|
||||
"id": 8,
|
||||
"code": 0,
|
||||
"desc": "控制电源故障",
|
||||
"cause": [
|
||||
"输入电压过高",
|
||||
"驱动控制板异常"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err9": [
|
||||
{
|
||||
"id": 9,
|
||||
"code": 9,
|
||||
"desc": "欠电压故障",
|
||||
"cause": [
|
||||
"输入电源瞬间停电",
|
||||
"输入电压过过低",
|
||||
"驱动控制板异常"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err10": [
|
||||
{
|
||||
"id": 10,
|
||||
"code": 0,
|
||||
"desc": "施工升降机一化控制器过载",
|
||||
"cause": [
|
||||
"抱闸回路异常",
|
||||
"负载过大"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err11": [
|
||||
{
|
||||
"id": 11,
|
||||
"code": 0,
|
||||
"desc": "电机过载",
|
||||
"cause": [
|
||||
"FC-02 设定不当",
|
||||
"抱闸回路异常",
|
||||
"负载过大"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err12": [
|
||||
{
|
||||
"id": 12,
|
||||
"code": 0,
|
||||
"desc": "输入侧缺相",
|
||||
"cause": [
|
||||
"输入电源不对称",
|
||||
"驱动控制板异常"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err13": [
|
||||
{
|
||||
"id": 13,
|
||||
"code": 0,
|
||||
"desc": "输出侧缺相",
|
||||
"cause": [
|
||||
"主回路输出接线松动",
|
||||
"电机损坏"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err14": [
|
||||
{
|
||||
"id": 14,
|
||||
"code": 0,
|
||||
"desc": "模块过热",
|
||||
"cause": [
|
||||
"环境温度过高",
|
||||
"风扇损坏",
|
||||
"风道堵塞"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err15": [
|
||||
{
|
||||
"id": 15,
|
||||
"code": 0,
|
||||
"desc": "外部设备故障",
|
||||
"cause": [
|
||||
"在非键盘运行状态下按下了STOP 键",
|
||||
"多功能 DI 端子输入了外部故障信号"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err16": [
|
||||
{
|
||||
"id": 16,
|
||||
"code": 0,
|
||||
"desc": "通讯故障",
|
||||
"cause": [
|
||||
"RS485 通讯接线是否正确",
|
||||
"FD 组参数设置是否正确"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err17": [
|
||||
{
|
||||
"id": 17,
|
||||
"code": 0,
|
||||
"desc": "接触器故障",
|
||||
"cause": [
|
||||
"母线电压异常",
|
||||
"驱动控制板异常"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err18": [
|
||||
{
|
||||
"id": 18,
|
||||
"code": 0,
|
||||
"desc": "接触器故障",
|
||||
"cause": [
|
||||
"母线电压异常",
|
||||
"驱动控制板异常"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err19": [
|
||||
{
|
||||
"id": 19,
|
||||
"code": 0,
|
||||
"desc": "电机调谐故障",
|
||||
"cause": [
|
||||
"电机参数设定不对",
|
||||
"参数调谐超时"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err20": [
|
||||
{
|
||||
"id": 20,
|
||||
"code": 0,
|
||||
"desc": "旋转编码器故障",
|
||||
"cause": [
|
||||
"旋转编码器型号是否匹配",
|
||||
"旋转编码器连线错误"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err23": [
|
||||
{
|
||||
"id": 23,
|
||||
"code": 0,
|
||||
"desc": "对地短路故障",
|
||||
"cause": [
|
||||
"输出对地短路"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err25": [
|
||||
{
|
||||
"id": 25,
|
||||
"code": 0,
|
||||
"desc": "输出断线保护",
|
||||
"cause": [
|
||||
"变频器输出侧三相全部缺失"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err26": [
|
||||
{
|
||||
"id": 26,
|
||||
"code": 0,
|
||||
"desc": "控制板与主回路通讯故障",
|
||||
"cause": [
|
||||
"系统上电时控制板与主回路通讯异常"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err31": [
|
||||
{
|
||||
"id": 31,
|
||||
"code": 0,
|
||||
"desc": "抱闸释放故障",
|
||||
"cause": [
|
||||
"外部抱闸电路是否收到抱闸释放信号"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err32": [
|
||||
{
|
||||
"id": 32,
|
||||
"code": 0,
|
||||
"desc": "抱闸反馈异常",
|
||||
"cause": [
|
||||
"抱闸反馈信号是否有输入",
|
||||
"控制板选择抱闸反馈信号的点是否正常"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err33": [
|
||||
{
|
||||
"id": 33,
|
||||
"code": 0,
|
||||
"desc": "运行指令故障",
|
||||
"cause": [
|
||||
"外部运行命令输入端子同一时间正反转同时有效"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err35": [
|
||||
{
|
||||
"id": 35,
|
||||
"code": 0,
|
||||
"desc": "操纵杆未归零",
|
||||
"cause": [
|
||||
"上电时,命令或者速度信号有闭合情况"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err37": [
|
||||
{
|
||||
"id": 37,
|
||||
"code": 0,
|
||||
"desc": "速度方向异常",
|
||||
"cause": [
|
||||
"目标给定频率和 DSP 反馈频率的方向相反"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err38": [
|
||||
{
|
||||
"id": 38,
|
||||
"code": 0,
|
||||
"desc": "速度反馈异常",
|
||||
"cause": [
|
||||
"目标给定频率和 DSP 反馈频率差异过大"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err39": [
|
||||
{
|
||||
"id": 39,
|
||||
"code": 0,
|
||||
"desc": "远程控制受限",
|
||||
"cause": [
|
||||
"远程控制模块异常或远程受控"
|
||||
]
|
||||
}
|
||||
],
|
||||
"Err40": [
|
||||
{
|
||||
"id": 40,
|
||||
"code": 1,
|
||||
"cause": [
|
||||
"编码器线数设置值不在设定范围内"
|
||||
],
|
||||
"solution": "检查编码器线数范围是否在 20~5000 之 间"
|
||||
},
|
||||
{
|
||||
"id": 40,
|
||||
"code": 2,
|
||||
"cause": [
|
||||
"编码器方向与运行方向相反"
|
||||
],
|
||||
"solution": "将实际接线的 A,B相交换或者修改 FC-14"
|
||||
},
|
||||
{
|
||||
"id": 40,
|
||||
"code": 3,
|
||||
"cause": [
|
||||
"运行 1s 时间内,检测不到 A,B 相输入"
|
||||
],
|
||||
"solution": "检查编码器连线是否正确,供电是否正常"
|
||||
},
|
||||
{
|
||||
"id": 40,
|
||||
"code": 4,
|
||||
"cause": [
|
||||
"在实际运行过程中,连续两个 Z 脉冲之间编码器计数值不为编码器线数的 2 倍"
|
||||
],
|
||||
"solution": "请确定编码器线数值是否在设定范围内,吊笼运行速度是否太高"
|
||||
},
|
||||
{
|
||||
"id": 40,
|
||||
"code": 5,
|
||||
"cause": [
|
||||
"编码器 A 或 B 缺相"
|
||||
],
|
||||
"solution": "请确定 A,B 相信号连接正确"
|
||||
},
|
||||
{
|
||||
"id": 40,
|
||||
"code": 6,
|
||||
"cause": [
|
||||
"当编码器旋转3圈后仍然检测不到Z信号"
|
||||
],
|
||||
"solution": "请确定Z信号连接正常"
|
||||
},
|
||||
{
|
||||
"id": 40,
|
||||
"code": 7,
|
||||
"cause": [
|
||||
"编码器 Z 信号检测无效"
|
||||
],
|
||||
"solution": "请检查编码器链接是否正常"
|
||||
},
|
||||
{
|
||||
"id": 40,
|
||||
"code": 8,
|
||||
"cause": [
|
||||
"编码器跳齿"
|
||||
],
|
||||
"solution": "请检查编码器线路"
|
||||
}
|
||||
],
|
||||
"Err41": [
|
||||
{
|
||||
"id": 41,
|
||||
"code": 1,
|
||||
"cause": [
|
||||
"当前脉冲数超出已自学习脉冲范围"
|
||||
],
|
||||
"solution": "重新上电,如果依然出现,重新自学习"
|
||||
},
|
||||
{
|
||||
"id": 41,
|
||||
"code": 2,
|
||||
"cause": [
|
||||
"上限位脉冲异常"
|
||||
],
|
||||
"solution": "重新自学习"
|
||||
},
|
||||
{
|
||||
"id": 41,
|
||||
"code": 3,
|
||||
"cause": [
|
||||
"下限位脉冲异常"
|
||||
],
|
||||
"solution": "重新自学习"
|
||||
},
|
||||
{
|
||||
"id": 41,
|
||||
"code": 4,
|
||||
"cause": [
|
||||
"上减速限位脉冲异常"
|
||||
],
|
||||
"solution": "重新自学习"
|
||||
},
|
||||
{
|
||||
"id": 41,
|
||||
"code": 5,
|
||||
"cause": [
|
||||
"下减速限位脉冲异常"
|
||||
],
|
||||
"solution": "重新自学习"
|
||||
},
|
||||
{
|
||||
"id": 41,
|
||||
"code": 6,
|
||||
"cause": [
|
||||
"上限位与上减速距离太近"
|
||||
],
|
||||
"solution": "检查上限位与上减速安装位置,如安装正常,请检查编码器安装"
|
||||
},
|
||||
{
|
||||
"id": 41,
|
||||
"code": 7,
|
||||
"cause": [
|
||||
"下限位与下减速距离太近"
|
||||
],
|
||||
"solution": "检查下限位与下减速安装位置,检查下限位与下减速安装"
|
||||
}
|
||||
],
|
||||
"Err50": [
|
||||
{
|
||||
"id": 50,
|
||||
"code": 0,
|
||||
"desc": "上限位故障",
|
||||
"cause": []
|
||||
}
|
||||
],
|
||||
"Err51": [
|
||||
{
|
||||
"id": 51,
|
||||
"code": 0,
|
||||
"desc": "下限位故障",
|
||||
"cause": []
|
||||
}
|
||||
],
|
||||
"Err52": [
|
||||
{
|
||||
"id": 52,
|
||||
"code": 0,
|
||||
"desc": "门限位开关异常",
|
||||
"cause": []
|
||||
}
|
||||
],
|
||||
"Err53": [
|
||||
{
|
||||
"id": 53,
|
||||
"code": 0,
|
||||
"desc": "参数设置错误",
|
||||
"cause": []
|
||||
}
|
||||
],
|
||||
"Err54": [
|
||||
{
|
||||
"id": 54,
|
||||
"code": 0,
|
||||
"desc": "上减速开关异常",
|
||||
"cause": []
|
||||
}
|
||||
],
|
||||
"Err55": [
|
||||
{
|
||||
"id": 55,
|
||||
"code": 0,
|
||||
"desc": "下减速开关异常",
|
||||
"cause": []
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
{
|
||||
"definitions": {},
|
||||
"paths": {
|
||||
"/python_sinmahe_statistics": {
|
||||
"post": {
|
||||
"description": "新马赫单个设备运行统计时间",
|
||||
"parameters": [
|
||||
{
|
||||
"description": "新马赫运行统计时间",
|
||||
"in": "body",
|
||||
"name": "data",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"properties": {
|
||||
"devaddr": {
|
||||
"description": "外壳编号",
|
||||
"example": "dgiot",
|
||||
"required": true,
|
||||
"type": "string"
|
||||
},
|
||||
"productid": {
|
||||
"description": "",
|
||||
"example": "dgiot",
|
||||
"required": true,
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"type": "object"
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Returns operation status"
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request"
|
||||
},
|
||||
"401": {
|
||||
"description": "Unauthorized"
|
||||
},
|
||||
"403": {
|
||||
"description": "Forbidden"
|
||||
},
|
||||
"500": {
|
||||
"description": "Server Internal error"
|
||||
}
|
||||
},
|
||||
"summary": "新马赫运行统计时间",
|
||||
"tags": [
|
||||
"Python"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
"tags": [
|
||||
{
|
||||
"description": "扩展编程",
|
||||
"name": "EXPROTO"
|
||||
}
|
||||
]
|
||||
}
|
@ -1,199 +0,0 @@
|
||||
{
|
||||
"tags": [
|
||||
{
|
||||
"name": "Sinmahe",
|
||||
"description": "新马赫"
|
||||
}
|
||||
],
|
||||
"definitions": {},
|
||||
"paths": {
|
||||
"/sinmahe_count": {
|
||||
"get": {
|
||||
"description": "获取新马赫设备统计数据",
|
||||
"consumes": [
|
||||
"*"
|
||||
],
|
||||
"parameters": [
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Returns success",
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"deviceInfo": {
|
||||
"description": "统计数据信息",
|
||||
"type": "object",
|
||||
"example": {"total": 10, "online": 10, "running": 10, "suspend": 10}
|
||||
},
|
||||
"devicedata": {
|
||||
"description": "位置信息",
|
||||
"type": "list",
|
||||
"example": [{"Lon": 32.12, "Lat": 120.34, "devaddr": 30001}]
|
||||
},
|
||||
"warnings": {
|
||||
"description": "告警信息",
|
||||
"type": "list",
|
||||
"example": [{"warnType": 6, "devaddr": 30001}]
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"404": {
|
||||
"description": "object not found",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/Error"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request"
|
||||
},
|
||||
"500": {
|
||||
"description": "Server Internal error"
|
||||
}
|
||||
},
|
||||
"summary": "获取新马赫设备统计数据",
|
||||
"tags": [
|
||||
"Sinmahe"
|
||||
]
|
||||
}
|
||||
},
|
||||
"/sinmahe_fault": {
|
||||
"get": {
|
||||
"description": "获取新马赫告警信息",
|
||||
"parameters": [
|
||||
{
|
||||
"name": "FaultType",
|
||||
"in": "query",
|
||||
"type": "number",
|
||||
"default": 1,
|
||||
"description": "告警类型"
|
||||
},
|
||||
{
|
||||
"name": "FaultLevel",
|
||||
"in": "query",
|
||||
"type": "number",
|
||||
"default": 1,
|
||||
"description": "告警等级"
|
||||
},
|
||||
{
|
||||
"name": "FaultCode",
|
||||
"in": "query",
|
||||
"type": "number",
|
||||
"default": 1,
|
||||
"description": "告警代号"
|
||||
},
|
||||
{
|
||||
"name": "FaultSrc",
|
||||
"in": "query",
|
||||
"type": "number",
|
||||
"default": 0,
|
||||
"description": "告警原因"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Returns success"
|
||||
},
|
||||
"404": {
|
||||
"description": "object not found",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/Error"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request"
|
||||
},
|
||||
"500": {
|
||||
"description": "Server Internal error"
|
||||
}
|
||||
},
|
||||
"summary": "获取新马赫告警信息",
|
||||
"tags": [
|
||||
"Sinmahe"
|
||||
]
|
||||
},
|
||||
"post": {
|
||||
"description": "获取新马赫告警规则文件",
|
||||
"consumes": [
|
||||
"*"
|
||||
],
|
||||
"parameters": [
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Returns success"
|
||||
},
|
||||
"404": {
|
||||
"description": "object not found",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/Error"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request"
|
||||
},
|
||||
"500": {
|
||||
"description": "Server Internal error"
|
||||
}
|
||||
},
|
||||
"summary": "获取新马赫告警规则文件",
|
||||
"tags": [
|
||||
"Sinmahe"
|
||||
]
|
||||
}
|
||||
},
|
||||
"/control_device": {
|
||||
"post": {
|
||||
"description": "发送新马赫控制板控制信息",
|
||||
"parameters": [
|
||||
{
|
||||
"in": "body",
|
||||
"name": "data",
|
||||
"required": true,
|
||||
"schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"type": {
|
||||
"description": "命令类型",
|
||||
"type": "string",
|
||||
"example": "ControlParams",
|
||||
"required": true
|
||||
},
|
||||
"devaddr": {
|
||||
"description": "设备地址",
|
||||
"type": "string",
|
||||
"example": "30001"
|
||||
},
|
||||
"payload": {
|
||||
"description": "命令参数",
|
||||
"type": "object",
|
||||
"example": {"PowerOnCtrl":"1","PubFreq":23,"PubCtrl":"1","ParaGet":"0","FOTA":"0"}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Returns success"
|
||||
},
|
||||
"404": {
|
||||
"description": "object not found",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/Error"
|
||||
}
|
||||
},
|
||||
"400": {
|
||||
"description": "Bad Request"
|
||||
},
|
||||
"500": {
|
||||
"description": "Server Internal error"
|
||||
}
|
||||
},
|
||||
"summary": "发送新马赫控制板控制信息",
|
||||
"tags": ["Sinmahe"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
{deps, [
|
||||
|
||||
]}.
|
@ -1,143 +0,0 @@
|
||||
%%--------------------------------------------------------------------
|
||||
%% Copyright (c) 2020-2021 DGIOT Technologies Co., Ltd. All Rights Reserved.
|
||||
%%
|
||||
%% Licensed under the Apache License, Version 2.0 (the "License");
|
||||
%% you may not use this file except in compliance with the License.
|
||||
%% You may obtain a copy of the License at
|
||||
%%
|
||||
%% http://www.apache.org/licenses/LICENSE-2.0
|
||||
%%
|
||||
%% Unless required by applicable law or agreed to in writing, software
|
||||
%% distributed under the License is distributed on an "AS IS" BASIS,
|
||||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
%% See the License for the specific language governing permissions and
|
||||
%% limitations under the License.
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
-module(sinmahe_mqttc).
|
||||
-author("kenneth").
|
||||
-include_lib("dgiot/include/logger.hrl").
|
||||
-record(state, {addr, client, key, secret, count}).
|
||||
%% API
|
||||
-export([start_link/3, init/1, handle_cast/2, handle_call/3, handle_info/2, terminate/2, code_change/3]).
|
||||
-export([start/1]).
|
||||
|
||||
start(Tid) ->
|
||||
Count = 100,
|
||||
Sup = list_to_atom(lists:concat([Tid, mqttc])),
|
||||
NewCount =
|
||||
case Count of
|
||||
1 ->
|
||||
2;
|
||||
_ ->
|
||||
Count
|
||||
end,
|
||||
lists:map(fun(Id) ->
|
||||
{ok, _} = supervisor:start_child(Sup, [Tid, Id, NewCount])
|
||||
end, lists:seq(1, NewCount)).
|
||||
|
||||
%%推送协议:MQTT
|
||||
%%➢ 连接地址:服务器 IP
|
||||
%%➢ 连接端口:1883
|
||||
%%➢ 连接方式:TCP
|
||||
%%➢ 认证参数
|
||||
%%用户名:api_key(企业编码)
|
||||
%%密码:api_secret(企业秘钥)
|
||||
%%clientID:api_key:api_secret: + 三位随机数字。 如:api_key:api_secret125
|
||||
%%注:服务器 IP 由平台运营商提供,企业编码、企业秘钥可在 ZETA 网管平台->权限管理->企业信
|
||||
%%息中获得
|
||||
|
||||
start_link(_Name, Id, _Count) ->
|
||||
_Options = [
|
||||
{clientid, <<"ClientId">>},
|
||||
{host, binary_to_list(<<"Host">>)},
|
||||
{port, <<"Port">>},
|
||||
{username, binary_to_list(<<"Key">>)},
|
||||
{password, binary_to_list(<<"Secret">>)},
|
||||
{proto_ver, v3},
|
||||
{keepalive, 60},
|
||||
{clean_start, true}
|
||||
],
|
||||
|
||||
%% ?LOG(info,"Options ~p ",[Options]),
|
||||
case dgiot_data:lookup({Id, mqttc}) of
|
||||
{ok, Pid} ->
|
||||
case is_pid(Pid) andalso is_process_alive(Pid) of
|
||||
true ->
|
||||
{ok, Pid};
|
||||
false ->
|
||||
%% dgiot_mqtt_client:start_link(?MODULE, [Id, Count], Options)
|
||||
pass
|
||||
end;
|
||||
{error, not_find} ->
|
||||
%% dgiot_mqtt_client:start_link(?MODULE, [Id, Count], Options)
|
||||
pass
|
||||
end.
|
||||
|
||||
init([Id, Key, Secret, Count]) ->
|
||||
dgiot_data:insert({Id, mqttc}, self()),
|
||||
%% ?LOG(info,"Id ~p Count ~p ", [Id, Count]),
|
||||
{ok, #state{addr = Id, key = Key, secret = Secret, count = Count}}.
|
||||
|
||||
handle_call(_Request, _From, State) ->
|
||||
{reply, ok, State}.
|
||||
|
||||
handle_cast(_Request, State) ->
|
||||
{noreply, State}.
|
||||
|
||||
handle_info({connect, Client}, #state{addr = Id, count = Count} = State) ->
|
||||
erlang:send_after(2000, self(), {subscribe, Client}),
|
||||
?LOG(info, "Id ~p Count ~p ", [Id, Count]),
|
||||
dgiot_metrics:inc(dgiot_sinmahe, <<"mqtt_online">>, 1),
|
||||
update(Count),
|
||||
{noreply, State#state{client = Client}};
|
||||
|
||||
handle_info({subscribe, Client}, #state{count = Count, key = Key, addr = 1} = State) ->
|
||||
Topic = <<Key/binary, "/v1/ms/#">>,
|
||||
?LOG(info, "Topic ~p Count ~p ", [Topic, Count]),
|
||||
emqtt:subscribe(Client, {Topic, 1}),
|
||||
{noreply, State};
|
||||
|
||||
handle_info({subscribe, Client}, #state{count = Count, key = Key, addr = 2} = State) ->
|
||||
Topic = <<Key/binary, "/v1/ap/#">>,
|
||||
?LOG(info, "Topic ~p Count ~p ", [Topic, Count]),
|
||||
emqtt:subscribe(Client, {Topic, 1}),
|
||||
{noreply, State};
|
||||
|
||||
handle_info(disconnect, State) ->
|
||||
dgiot_metrics:dec(dgiot_sinmahe, <<"mqtt_online">>, 1),
|
||||
?LOG(info, "disconnect ~p ", [State]),
|
||||
{noreply, State#state{client = disconnect}};
|
||||
|
||||
handle_info({publish, #{payload := _Payload} = _Msg}, State) ->
|
||||
dgiot_metrics:inc(dgiot_sinmahe, <<"mqtt_recv">>, 1),
|
||||
{noreply, State};
|
||||
|
||||
handle_info(_Info, State) ->
|
||||
%% ?LOG(info,"ecapturer ~p~n", [_Info]),
|
||||
{noreply, State}.
|
||||
|
||||
terminate(_Reason, #state{count = Count, addr = Id} = _State) ->
|
||||
%% ?LOG(info,"_Reason ~p~n", [_Reason]),
|
||||
update(Count),
|
||||
dgiot_data:delete({Id, mqttc}),
|
||||
ok.
|
||||
|
||||
code_change(_OldVsn, State, _Extra) ->
|
||||
{ok, State}.
|
||||
|
||||
update(Count) ->
|
||||
OnlineCount = lists:foldl(fun(Id, Acc) ->
|
||||
case dgiot_data:lookup({Id, mqttc}) of
|
||||
{ok, Pid} ->
|
||||
case is_pid(Pid) andalso is_process_alive(Pid) of
|
||||
true ->
|
||||
Acc + 1;
|
||||
false ->
|
||||
Acc
|
||||
end;
|
||||
_ ->
|
||||
Acc
|
||||
end
|
||||
end, 0, lists:seq(1, Count)),
|
||||
dgiot_data:insert({<<"mqtt_online">>, dgiot_metrics}, OnlineCount).
|
@ -1,46 +0,0 @@
|
||||
%--------------------------------------------------------------------
|
||||
%% Copyright (c) 2016-2017 John liu <34489690@qq.com>.
|
||||
%%
|
||||
%% Licensed under the Apache License, Version 2.0 (the "License");
|
||||
%% you may not use this file except in compliance with the License.
|
||||
%% You may obtain a copy of the License at
|
||||
%%
|
||||
%% http://www.apache.org/licenses/LICENSE-2.0
|
||||
%%
|
||||
%% Unless required by applicable law or agreed to in writing, software
|
||||
%% distributed under the License is distributed on an "AS IS" BASIS,
|
||||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
%% See the License for the specific language governing permissions and
|
||||
%% limitations under the License.
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
%% @doc shuwa_zeta supervisor
|
||||
-module(sinmahe_mqttc_sup).
|
||||
|
||||
-behaviour(supervisor).
|
||||
|
||||
%% API
|
||||
-export([start_link/1]).
|
||||
|
||||
%% Supervisor callbacks
|
||||
-export([init/1]).
|
||||
|
||||
%% Helper macro for declaring children of supervisor
|
||||
-define(CHILD(I, Type, Args), {I, {I, start_link, Args}, transient, 5000, Type, [I]}).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% API functions
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
start_link(Name) ->
|
||||
supervisor:start_link({local, list_to_atom(lists:concat([Name, mqttc]))}, ?MODULE, []).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% Supervisor callbacks
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
init([]) ->
|
||||
{ok, { {simple_one_for_one, 5, 10}, [
|
||||
?CHILD(sinmahe_mqttc, worker, [])
|
||||
]}}.
|
||||
|
@ -1,7 +0,0 @@
|
||||
{application,dgiot_sinmahe,
|
||||
[{description,"dgiot_sinmahe"},
|
||||
{vsn,"1.6.4"},
|
||||
{modules,[]},
|
||||
{registered,[dgiot_sinmahe_sup]},
|
||||
{applications,[kernel,stdlib,dgiot]},
|
||||
{mod,{dgiot_sinmahe_app,[]}}]}.
|
@ -1,25 +0,0 @@
|
||||
%%-*- mode: erlang -*-
|
||||
%% .app.src.script
|
||||
|
||||
RemoveLeadingV =
|
||||
fun(Tag) ->
|
||||
case re:run(Tag, "v\[0-9\]+\.\[0-9\]+\.*") of
|
||||
nomatch ->
|
||||
Tag;
|
||||
{match, _} ->
|
||||
%% if it is a version number prefixed by 'v' then remove the 'v'
|
||||
"v" ++ Vsn = Tag,
|
||||
Vsn
|
||||
end
|
||||
end,
|
||||
|
||||
case os:getenv("EMQX_DEPS_DEFAULT_VSN") of
|
||||
false -> CONFIG; % env var not defined
|
||||
[] -> CONFIG; % env var set to empty string
|
||||
Tag ->
|
||||
[begin
|
||||
AppConf0 = lists:keystore(vsn, 1, AppConf, {vsn, RemoveLeadingV(Tag)}),
|
||||
{application, App, AppConf0}
|
||||
end || Conf = {application, App, AppConf} <- CONFIG]
|
||||
end.
|
||||
|
@ -1,348 +0,0 @@
|
||||
%%--------------------------------------------------------------------
|
||||
%% Copyright (c) 2020-2021 DGIOT Technologies Co., Ltd. All Rights Reserved.
|
||||
%%
|
||||
%% Licensed under the Apache License, Version 2.0 (the "License");
|
||||
%% you may not use this file except in compliance with the License.
|
||||
%% You may obtain a copy of the License at
|
||||
%%
|
||||
%% http://www.apache.org/licenses/LICENSE-2.0
|
||||
%%
|
||||
%% Unless required by applicable law or agreed to in writing, software
|
||||
%% distributed under the License is distributed on an "AS IS" BASIS,
|
||||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
%% See the License for the specific language governing permissions and
|
||||
%% limitations under the License.
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
-module(dgiot_sinmahe).
|
||||
-include_lib("dgiot/include/logger.hrl").
|
||||
-author("jonhl").
|
||||
|
||||
-export([
|
||||
get_sinmahe_json/0,
|
||||
create_subdev/3,
|
||||
test_fault/0,
|
||||
send_fault/4,
|
||||
send/4,
|
||||
get_fault/1,
|
||||
post_fault/0,
|
||||
update_runstate/3,
|
||||
update_powerstate/3,
|
||||
get_device_count/1,
|
||||
post_control_device/2
|
||||
]).
|
||||
|
||||
send(DeviceId, Name, FaultRule, #{
|
||||
<<"FaultType">> := FaultType,
|
||||
<<"FaultLevel">> := FaultLevel,
|
||||
<<"FaultCode">> := FaultCode} = FaultData) ->
|
||||
maps:fold(fun(UserId, Rule, Acc) ->
|
||||
IsFaultType = lists:member(dgiot_utils:to_binary(FaultType), maps:get(<<"FaultType">>, Rule)),
|
||||
IsFaultLevel = lists:member(dgiot_utils:to_binary(FaultLevel), maps:get(<<"FaultLevel">>, Rule)),
|
||||
IsFaultCode = lists:member(dgiot_utils:to_int(FaultCode), maps:get(<<"FaultCode">>, Rule)),
|
||||
case (IsFaultType and IsFaultLevel and IsFaultCode) of
|
||||
true ->
|
||||
send_fault(DeviceId, Name, UserId, #{
|
||||
<<"FaultType">> => maps:get(<<"FaultType">>, FaultData, 1),
|
||||
<<"FaultLevel">> => maps:get(<<"FaultLevel">>, FaultData, 1),
|
||||
<<"FaultCode">> => maps:get(<<"FaultCode">>, FaultData, 1),
|
||||
<<"FaultSrc">> => maps:get(<<"FaultSrc">>, FaultData, 0)
|
||||
}),
|
||||
Acc + 1;
|
||||
false -> Acc
|
||||
end
|
||||
end, 0, FaultRule).
|
||||
|
||||
test_fault() ->
|
||||
UserId = <<"QOGSAQMoX4">>,
|
||||
DeviceId = <<"1e82f1c59b">>,
|
||||
Name = <<"数蛙002"/utf8>>,
|
||||
FaultData = #{
|
||||
<<"FaultType">> => 1,
|
||||
<<"FaultLevel">> => 1,
|
||||
<<"FaultCode">> => 5,
|
||||
<<"FaultSrc">> => 0
|
||||
},
|
||||
send_fault(DeviceId, Name, UserId, FaultData).
|
||||
|
||||
get_fault(FaultData) ->
|
||||
FaultData#{
|
||||
<<"FaultType">> => get_faulttype(maps:get(<<"FaultType">>, FaultData)),
|
||||
<<"FaultLevel">> => get_faultlevel(maps:get(<<"FaultLevel">>, FaultData)),
|
||||
<<"FaultCode">> => get_faultcode(maps:get(<<"FaultCode">>, FaultData), maps:get(<<"FaultSrc">>, FaultData))
|
||||
}.
|
||||
|
||||
post_fault() ->
|
||||
Rules = dgiot_data:get({sinmahe_json, fault}),
|
||||
FaultType =
|
||||
maps:fold(fun(K, V, Acc) ->
|
||||
Acc ++ [#{<<"id">> => K, <<"text">> => V}]
|
||||
end, [], maps:get(<<"FaultType">>, Rules)),
|
||||
FaultLevel =
|
||||
maps:fold(fun(K, V, Acc) ->
|
||||
Acc ++ [#{<<"id">> => K, <<"text">> => V}]
|
||||
end, [], maps:get(<<"FaultLevel">>, Rules)),
|
||||
FaultCode =
|
||||
maps:fold(fun(_K, V, Acc) ->
|
||||
Acc ++ V
|
||||
end, [], maps:get(<<"FaultCode">>, Rules)),
|
||||
#{
|
||||
<<"FaultType">> => FaultType,
|
||||
<<"FaultLevel">> => FaultLevel,
|
||||
<<"FaultCode">> => FaultCode
|
||||
}.
|
||||
|
||||
%%
|
||||
%%#{
|
||||
%%<<"FaultType">> => 1,
|
||||
%%<<"FaultLevel">> => 1,
|
||||
%%<<"FaultCode">> => 5,
|
||||
%%<<"FaultSrc">> => 0
|
||||
%%}
|
||||
send_fault(DeviceId, Name, UserId, FaultData) ->
|
||||
?LOG(info, "DeviceId ~p ,Name ~p", [DeviceId, Name]),
|
||||
FaultType = get_faulttype(maps:get(<<"FaultType">>, FaultData)),
|
||||
FaultLevel = get_faultlevel(maps:get(<<"FaultLevel">>, FaultData)),
|
||||
Data = get_faultcode(maps:get(<<"FaultCode">>, FaultData), maps:get(<<"FaultSrc">>, FaultData)),
|
||||
Payload = #{
|
||||
<<"description">> => <<"设备:"/utf8, Name/binary, " ", FaultType/binary>>,
|
||||
<<"title">> => FaultLevel,
|
||||
<<"ticker">> => DeviceId,
|
||||
<<"text">> => Data
|
||||
},
|
||||
dgiot_umeng:send(UserId, Payload).
|
||||
|
||||
get_faulttype(FaultType) ->
|
||||
case dgiot_data:get({sinmahe_json, fault}) of
|
||||
#{<<"FaultType">> := Type} ->
|
||||
maps:get(dgiot_utils:to_binary(FaultType), Type);
|
||||
_ -> <<"正常"/utf8>>
|
||||
end.
|
||||
|
||||
get_faultlevel(FaultLevel) ->
|
||||
case dgiot_data:get({sinmahe_json, fault}) of
|
||||
#{<<"FaultLevel">> := Level} ->
|
||||
maps:get(dgiot_utils:to_binary(FaultLevel), Level);
|
||||
_ -> <<"正常"/utf8>>
|
||||
end.
|
||||
|
||||
get_faultcode(FaultCode, FaultSrc) ->
|
||||
case dgiot_data:get({sinmahe_json, fault}) of
|
||||
#{<<"FaultCode">> := Codes} ->
|
||||
BinFaultCode = dgiot_utils:to_binary(FaultCode),
|
||||
Error = <<"Err", BinFaultCode/binary>>,
|
||||
lists:filter(fun(#{<<"code">> := Code}) ->
|
||||
case Code of
|
||||
FaultSrc -> true;
|
||||
_ -> false
|
||||
end
|
||||
end, maps:get(Error, Codes));
|
||||
_ -> <<"正常"/utf8>>
|
||||
end.
|
||||
|
||||
get_sinmahe_json() ->
|
||||
{file, Here} = code:is_loaded(?MODULE),
|
||||
Dir = filename:dirname(filename:dirname(Here)),
|
||||
Root = dgiot_httpc:url_join([Dir, "/priv/"]),
|
||||
TplPath = Root ++ "sinmahe.json",
|
||||
case file:read_file(TplPath) of
|
||||
{ok, Bin} ->
|
||||
case catch jsx:decode(Bin, [{labels, binary}, return_maps]) of
|
||||
{'EXIT', Reason} ->
|
||||
?LOG(info, "~p", [Reason]);
|
||||
Datas ->
|
||||
dgiot_data:insert({sinmahe_json, fault}, Datas)
|
||||
end;
|
||||
{error, Reason2} ->
|
||||
?LOG(info, "~p", [Reason2])
|
||||
end,
|
||||
case dgiot_parse:query_object(<<"Product">>, #{<<"where">> => #{<<"devType">> => <<"sinmahe_PeriodicInformation">>}}) of
|
||||
{ok, #{<<"results">> := [#{<<"objectId">> := PeriodProductId} | _]}} ->
|
||||
dgiot_data:insert({simahe, <<"PeriodicInformation">>}, PeriodProductId);
|
||||
_ ->
|
||||
?LOG(info, "Product not exist")
|
||||
end,
|
||||
case dgiot_parse:query_object(<<"Product">>, #{<<"where">> => #{<<"devType">> => <<"sinmahe_FaultInformation">>}}) of
|
||||
{ok, #{<<"results">> := [#{<<"objectId">> := FaultProductId} | _]}} ->
|
||||
dgiot_data:insert({simahe, <<"FaultInformation">>}, FaultProductId);
|
||||
_ ->
|
||||
?LOG(info, "Product not exist")
|
||||
end,
|
||||
case dgiot_parse:query_object(<<"Product">>, #{<<"where">> => #{<<"devType">> => <<"sinmahe_BasicInformation">>}}) of
|
||||
{ok, #{<<"results">> := [#{<<"objectId">> := BasicProductId} | _]}} ->
|
||||
dgiot_data:insert({simahe, <<"BasicInformation">>}, BasicProductId);
|
||||
_ ->
|
||||
?LOG(info, "Product not exist"),
|
||||
<<"">>
|
||||
end.
|
||||
|
||||
create_subdev(ProductId, DevAddr, Type) ->
|
||||
SubProductId = dgiot_data:get({simahe, Type}),
|
||||
SubDevaddr = <<Type/binary, "_", DevAddr/binary>>,
|
||||
case dgiot_parse:get_object(<<"Device">>, dgiot_parse:get_deviceid(SubProductId, SubDevaddr)) of
|
||||
{ok, #{<<"objectId">> := _ObjectId}} -> pass;
|
||||
_ ->
|
||||
Device = #{
|
||||
<<"ACL">> => #{
|
||||
<<"*">> => #{
|
||||
<<"read">> => true
|
||||
}
|
||||
},
|
||||
<<"devaddr">> => SubDevaddr,
|
||||
<<"product">> => #{<<"__type">> => <<"Pointer">>,
|
||||
<<"className">> => <<"Product">>,
|
||||
<<"objectId">> => SubProductId},
|
||||
<<"parentId">> => #{<<"__type">> => <<"Pointer">>,
|
||||
<<"className">> => <<"Device">>,
|
||||
<<"objectId">> => dgiot_parse:get_deviceid(ProductId, DevAddr)},
|
||||
<<"route">> => #{DevAddr => SubDevaddr}
|
||||
},
|
||||
dgiot_parse:create_object(<<"Device">>, Device)
|
||||
end.
|
||||
|
||||
update_runstate(ProductId, DevAddr, Data) ->
|
||||
case maps:get(<<"RunState">>, Data, undefine) of
|
||||
undefine -> pass;
|
||||
RunState ->
|
||||
case dgiot_data:get({sinmahe_runstate, DevAddr}) of
|
||||
RunState -> pass;
|
||||
_ -> case dgiot_parse:get_object(<<"Device">>, dgiot_parse:get_deviceid(ProductId, DevAddr)) of
|
||||
{ok, #{<<"objectId">> := ObjectId, <<"basedata">> := BaseData}} ->
|
||||
dgiot_parse:update_object(<<"Device">>, ObjectId, #{<<"isEnable">> => false,
|
||||
<<"basedata">> => BaseData#{<<"RunState">> => RunState}});
|
||||
_ ->
|
||||
pass
|
||||
end
|
||||
end
|
||||
end.
|
||||
|
||||
update_powerstate(ProductId, DevAddr, Data) ->
|
||||
case maps:get(<<"PowerState">>, Data, undefine) of
|
||||
undefine -> pass;
|
||||
RunState ->
|
||||
case dgiot_data:get({sinmahe_runstate, DevAddr}) of
|
||||
RunState -> pass;
|
||||
_ -> case dgiot_parse:get_object(<<"Device">>, dgiot_parse:get_deviceid(ProductId, DevAddr)) of
|
||||
{ok, #{<<"objectId">> := ObjectId, <<"basedata">> := BaseData}} ->
|
||||
dgiot_parse:update_object(<<"Device">>, ObjectId, #{<<"isEnable">> => false,
|
||||
<<"basedata">> => BaseData#{<<"PowerState">> => RunState}});
|
||||
_ ->
|
||||
pass
|
||||
end
|
||||
end
|
||||
end.
|
||||
|
||||
|
||||
% dgiot_sinmahe_oper:get_device_count().
|
||||
get_device_count(SessionToken) ->
|
||||
ProductId = dgiot_data:get({simahe, <<"PeriodicInformation">>}),
|
||||
Filter = #{
|
||||
<<"keys">> => [<<"count(*)">>, <<"devaddr">>, <<"basedata">>, <<"status">>],
|
||||
<<"where">> => #{<<"product">> => ProductId}
|
||||
},
|
||||
case dgiot_parse:query_object(<<"Device">>, Filter, [{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
|
||||
{ok, #{<<"count">> := Count, <<"results">> := Results}} ->
|
||||
{Loc2, Warn2} =
|
||||
lists:foldl(fun(#{<<"objectId">> := DeviceId, <<"devaddr">> := Addr, <<"basedata">> := Basedata}, {Loc, Warn}) ->
|
||||
Factory =
|
||||
case maps:find(<<"factory">>, Basedata) of
|
||||
{ok, Factory1} -> Factory1;
|
||||
error -> <<"新马赫"/utf8>>
|
||||
end,
|
||||
Loc1 =
|
||||
case maps:find(<<"basicdata">>, Basedata) of
|
||||
{ok, BasicData} ->
|
||||
case maps:is_key(<<"GPS">>, BasicData) of
|
||||
true ->
|
||||
#{<<"GPS">> := #{<<"Lon">> := Lon, <<"Lat">> := Lat}} = BasicData,
|
||||
[#{<<"Lon">> => Lon, <<"Lat">> => Lat,
|
||||
<<"factory">> => Factory, <<"addr">> => Addr, <<"did">> => DeviceId} | Loc];
|
||||
false ->
|
||||
[#{<<"factory">> => Factory, <<"addr">> => Addr, <<"did">> => DeviceId} | Loc]
|
||||
end;
|
||||
error ->
|
||||
[#{<<"factory">> => Factory, <<"addr">> => Addr, <<"did">> => DeviceId} | Loc]
|
||||
end,
|
||||
Warn1 =
|
||||
case maps:find(<<"faultdata">>, Basedata) of
|
||||
{ok, #{<<"FaultLevel">> := Level}} ->
|
||||
[
|
||||
#{
|
||||
<<"FaultLevel">> => dgiot_utils:to_int(Level),
|
||||
<<"factory">> => Factory,
|
||||
<<"addr">> => Addr, <<"did">> => DeviceId} | Warn];
|
||||
_ ->
|
||||
[
|
||||
#{
|
||||
<<"FaultLevel">> => 0,
|
||||
<<"factory">> => Factory,
|
||||
<<"addr">> => Addr, <<"did">> => DeviceId} | Warn]
|
||||
end,
|
||||
{Loc1, Warn1}
|
||||
end, {[], []}, Results),
|
||||
Query = #{
|
||||
<<"keys">> => [<<"last_row(RunState,createdat)">>],
|
||||
<<"limit">> => 1,
|
||||
<<"group">> => <<"devaddr">>,
|
||||
<<"where">> => #{
|
||||
<<"createdat">> => #{
|
||||
<<"$gt">> => <<"now - 30s">>
|
||||
}
|
||||
}
|
||||
},
|
||||
{NewOnline, NewRunning, NewSuspend} =
|
||||
case dgiot_tdengine:get_product(ProductId, Query) of
|
||||
{ok, #{<<"results">> := Result}} when length(Result) > 0 ->
|
||||
lists:foldl(fun(#{<<"runstate">> := State, <<"devaddr">> := DevAddr, <<"createdat">> := Ts}, {Online, Running, Suspend}) ->
|
||||
case DevAddr of
|
||||
<<"_PeriodicInformation">> ->
|
||||
{Online, Running, Suspend};
|
||||
_ ->
|
||||
case (dgiot_datetime:now_secs() - dgiot_tdengine:to_unixtime(Ts) < 500) of
|
||||
true ->
|
||||
case State of
|
||||
0 ->
|
||||
{Online + 1, Running, Suspend + 1};
|
||||
1 ->
|
||||
{Online + 1, Running + 1, Suspend};
|
||||
2 ->
|
||||
{Online + 1, Running + 1, Suspend};
|
||||
_ ->
|
||||
{Online, Running, Suspend}
|
||||
end;
|
||||
false ->
|
||||
{Online, Running, Suspend}
|
||||
end
|
||||
end
|
||||
end, {0, 0, 0}, Result);
|
||||
_Result ->
|
||||
{0, 0, 0}
|
||||
end,
|
||||
{200, #{
|
||||
<<"deviceInfo">> =>
|
||||
#{
|
||||
<<"productid">> => ProductId,
|
||||
<<"total">> => Count,
|
||||
<<"online">> => NewOnline,
|
||||
<<"running">> => NewRunning,
|
||||
<<"suspend">> => NewSuspend},
|
||||
<<"devicedata">> => Loc2,
|
||||
<<"warnings">> => Warn2}
|
||||
};
|
||||
_ ->
|
||||
{error, <<"devaddr not exist">>}
|
||||
end.
|
||||
|
||||
|
||||
% dgiot_sinmahe_oper:post_control_device(Args).
|
||||
post_control_device(#{<<"type">> := Type, <<"devaddr">> := DevAddr, <<"payload">> := Payload}, SessionToken) ->
|
||||
case dgiot_parse:query_object(<<"Device">>, #{<<"where">> => #{<<"devaddr">> => DevAddr}},
|
||||
[{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
|
||||
{ok, #{<<"results">> := Result}} when length(Result) > 0 ->
|
||||
Topic = <<Type/binary, "/", DevAddr/binary>>,
|
||||
dgiot_mqtt:publish(DevAddr, Topic, jsx:encode(Payload)),
|
||||
{ok, #{<<"results">> => Result}};
|
||||
Error ->
|
||||
Error
|
||||
end.
|
||||
|
@ -1,37 +0,0 @@
|
||||
%%--------------------------------------------------------------------
|
||||
%% Copyright (c) 2020-2021 DGIOT Technologies Co., Ltd. All Rights Reserved.
|
||||
%%
|
||||
%% Licensed under the Apache License, Version 2.0 (the "License");
|
||||
%% you may not use this file except in compliance with the License.
|
||||
%% You may obtain a copy of the License at
|
||||
%%
|
||||
%% http://www.apache.org/licenses/LICENSE-2.0
|
||||
%%
|
||||
%% Unless required by applicable law or agreed to in writing, software
|
||||
%% distributed under the License is distributed on an "AS IS" BASIS,
|
||||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
%% See the License for the specific language governing permissions and
|
||||
%% limitations under the License.
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
-module(dgiot_sinmahe_app).
|
||||
-include_lib("dgiot/include/logger.hrl").
|
||||
-include("dgiot_sinmahe.hrl").
|
||||
-behaviour(application).
|
||||
-emqx_plugin(?MODULE).
|
||||
|
||||
%% Application callbacks
|
||||
-export([start/2, stop/1]).
|
||||
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% Application callbacks
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
start(_StartType, _StartArgs) ->
|
||||
dgiot_data:init(?DGIOT_SINMAHE_DTU),
|
||||
dgiot_metrics:start(dgiot_sinmahe),
|
||||
dgiot_sinmahe_sup:start_link().
|
||||
|
||||
stop(_State) ->
|
||||
ok.
|
@ -1,279 +0,0 @@
|
||||
%%--------------------------------------------------------------------
|
||||
%% Copyright (c) 2020-2021 DGIOT Technologies Co., Ltd. All Rights Reserved.
|
||||
%%
|
||||
%% Licensed under the Apache License, Version 2.0 (the "License");
|
||||
%% you may not use this file except in compliance with the License.
|
||||
%% You may obtain a copy of the License at
|
||||
%%
|
||||
%% http://www.apache.org/licenses/LICENSE-2.0
|
||||
%%
|
||||
%% Unless required by applicable law or agreed to in writing, software
|
||||
%% distributed under the License is distributed on an "AS IS" BASIS,
|
||||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
%% See the License for the specific language governing permissions and
|
||||
%% limitations under the License.
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
-module(dgiot_sinmahe_channel).
|
||||
-include_lib("dgiot/include/logger.hrl").
|
||||
-behavior(dgiot_channelx).
|
||||
-author("johnliu").
|
||||
-include("dgiot_sinmahe.hrl").
|
||||
-define(TYPE, <<"SINMAHE">>).
|
||||
-define(MAX_BUFF_SIZE, 1024).
|
||||
-record(state, {id, mod, product, env = #{}, buff_size = 1024000, header = <<>>}).
|
||||
%% API
|
||||
-export([start/2]).
|
||||
|
||||
%% Channel callback
|
||||
-export([init/3, handle_init/1, handle_event/3, handle_message/2, stop/3]).
|
||||
|
||||
%% 注册通道类型
|
||||
-channel(?TYPE).
|
||||
-channel_type(#{type => 1,
|
||||
title => #{zh => <<"SINMAHE采集通道"/utf8>>},
|
||||
description => #{zh => <<"SINMAHE采集通道"/utf8>>}}).
|
||||
%% 注册通道参数
|
||||
-params(#{
|
||||
<<"ico">> => #{
|
||||
order => 102,
|
||||
type => string,
|
||||
required => false,
|
||||
default => <<"http://dgiot-1253666439.cos.ap-shanghai-fsi.myqcloud.com/dgiot_tech/zh/product/dgiot/channel/SINMAHE%20%E5%9B%BE%E6%A0%87.jpg">>,
|
||||
title => #{
|
||||
en => <<"channel ICO">>,
|
||||
zh => <<"通道ICO"/utf8>>
|
||||
},
|
||||
description => #{
|
||||
en => <<"channel ICO">>,
|
||||
zh => <<"通道ICO"/utf8>>
|
||||
}
|
||||
}
|
||||
}).
|
||||
|
||||
|
||||
start(ChannelId, ChannelArgs) ->
|
||||
Size = maps:get(<<"Size">>, ChannelArgs, 5),
|
||||
MaxOverFlow = maps:get(<<"MaxOverFlow">>, ChannelArgs, 10),
|
||||
dgiot_channelx:add(?TYPE, ChannelId, ?MODULE, ChannelArgs#{<<"Size">> => Size,
|
||||
<<"MaxOverFlow">> => MaxOverFlow}).
|
||||
|
||||
%% 通道初始化
|
||||
init(?TYPE, ChannelId, Args) ->
|
||||
#{<<"product">> := Products} = Args,
|
||||
[{ProductId, Acl} | _] =
|
||||
lists:map(fun({ProdcutId1, #{<<"ACL">> := Acl1}}) ->
|
||||
{ProdcutId1, Acl1}
|
||||
end, Products),
|
||||
dgiot_sinmahe:get_sinmahe_json(),
|
||||
State = #state{id = ChannelId, product = Products, env = #{<<"ACL">> => Acl, <<"productid">> => ProductId}},
|
||||
{ok, State, []}.
|
||||
|
||||
handle_init(State) ->
|
||||
{ok, State}.
|
||||
|
||||
handle_event(EventType, Event, _State) ->
|
||||
?LOG(info, "channel ~p, ~p", [EventType, Event]),
|
||||
ok.
|
||||
|
||||
% SELECT clientid, payload, topic FROM "BasicInformation"
|
||||
% SELECT clientid, payload, topic FROM "PeriodicInformation"
|
||||
% SELECT clientid, payload, topic FROM "FaultInformation"
|
||||
% SELECT clientid, disconnected_at FROM "$events/client_disconnected" WHERE username = 'sinmahe'
|
||||
% SELECT clientid, connected_at FROM "$events/client_connected" WHERE username = 'sinmahe'
|
||||
handle_message({rule, #{clientid := DevAddr, connected_at := _ConnectedAt}, #{peername := PeerName} = _Context}, State) ->
|
||||
ProductId = dgiot_data:get({simahe, <<"PeriodicInformation">>}),
|
||||
dgiot_mqtt:publish(DevAddr, <<"connected/", DevAddr/binary>>, jsx:encode(#{DevAddr => <<"connected">>})),
|
||||
dgiot_parse:update_object(<<"Device">>, dgiot_parse:get_deviceid(ProductId, DevAddr), #{<<"isEnable">> => true,
|
||||
<<"status">> => <<"ONLINE">>,
|
||||
<<"ip">> => PeerName}),
|
||||
dgiot_data:insert({simahe, DevAddr}, 1),
|
||||
dgiot_sinmahe:create_subdev(ProductId, DevAddr, <<"FaultInformation">>),
|
||||
dgiot_sinmahe:create_subdev(ProductId, DevAddr, <<"BasicInformation">>),
|
||||
{ok, State};
|
||||
|
||||
handle_message({rule, #{clientid := DevAddr, disconnected_at := _DisconnectedAt}, _Context}, State) ->
|
||||
ProductId = dgiot_data:get({simahe, <<"PeriodicInformation">>}),
|
||||
dgiot_mqtt:publish(DevAddr, <<"disconnected/", DevAddr/binary>>, jsx:encode(#{DevAddr => <<"disconnected">>})),
|
||||
dgiot_parse:update_object(<<"Device">>, dgiot_parse:get_deviceid(ProductId, DevAddr), #{<<"status">> => <<"OFFLINE">>}),
|
||||
dgiot_data:insert({simahe, DevAddr}, 0),
|
||||
{ok, State};
|
||||
|
||||
handle_message({rule, #{clientid := DevAddr, payload := Payload, topic := <<"BasicInformation">>}, _Msg},
|
||||
#state{id = ChannelId, env = #{<<"productid">> := ProductId}} = State) ->
|
||||
dgiot_mqtt:publish(DevAddr, <<"BasicInformation/", DevAddr/binary>>, Payload),
|
||||
dgiot_bridge:send_log(ChannelId, "topic:~p Payload:~p ~n", [<<DevAddr/binary, "/", "BasicInformation">>, Payload]),
|
||||
case jsx:is_json(Payload) of
|
||||
true ->
|
||||
Map = jsx:decode(Payload, [{labels, binary}, return_maps]),
|
||||
Data = format_basic(Map),
|
||||
BasicProductId = dgiot_data:get({simahe, <<"BasicInformation">>}),
|
||||
ProductId = dgiot_data:get({simahe, <<"PeriodicInformation">>}),
|
||||
Now =
|
||||
case maps:get(<<"UpdateTime">>, Data, now) of
|
||||
now ->
|
||||
dgiot_datetime:now_secs();
|
||||
<<"2011", _/binary>> ->
|
||||
dgiot_datetime:now_secs();
|
||||
<<Y:4/binary, M:2/binary, D:2/binary, H:2/binary>> ->
|
||||
dgiot_datetime:to_unixtime(dgiot_datetime:to_localtime(<<Y/binary, "-", M/binary, "-", D/binary, " ", H/binary, ":00:00">>))
|
||||
end,
|
||||
NewData = maps:without([<<"GPS">>, <<"UpdateTime">>], Data),
|
||||
%% todo 修改为取最后一次周期数据的时间
|
||||
Query = #{<<"keys">> => <<"last_row(*)">>, <<"limit">> => 1},
|
||||
case dgiot_tdengine:get_device(ProductId, <<"PeriodicInformation_", DevAddr/binary>>, Query) of
|
||||
{ok, Data} ->
|
||||
?LOG(info, "Data ~p", [Data]);
|
||||
_ -> Now
|
||||
end,
|
||||
dgiot_tdengine_adapter:save(BasicProductId, <<"BasicInformation_", DevAddr/binary>>, NewData#{
|
||||
<<"createdat">> => dgiot_datetime:format(Now, <<"YY-MM-DDTHH:NN:SS.000Z">>),
|
||||
<<"UpdateTime">> => Now}),
|
||||
case dgiot_parse:get_object(<<"Device">>, dgiot_parse:get_deviceid(ProductId, DevAddr)) of
|
||||
{ok, #{<<"objectId">> := ObjectId, <<"basedata">> := BaseData}} ->
|
||||
Data3 =
|
||||
case maps:find(<<"basicdata">>, BaseData) of
|
||||
{ok, Data1} ->
|
||||
Data2 =
|
||||
case maps:find(<<"GPS">>, Data) of
|
||||
{ok, #{<<"Lat">> := Lat, <<"Lon">> := Lon}} ->
|
||||
Address = dgiot_gps:get_baidu_addr(Lon, Lat),
|
||||
maps:merge(Data1, Address);
|
||||
error -> Data1
|
||||
end,
|
||||
maps:merge(Data1, Data2);
|
||||
error ->
|
||||
case maps:find(<<"GPS">>, Data) of
|
||||
{ok, #{<<"Lat">> := Lat, <<"Lon">> := Lon}} ->
|
||||
Address = dgiot_gps:get_baidu_addr(Lon, Lat),
|
||||
maps:merge(Data, Address);
|
||||
error -> Data
|
||||
end
|
||||
end,
|
||||
dgiot_parse:update_object(<<"Device">>, ObjectId, #{<<"isEnable">> => true,
|
||||
<<"basedata">> => BaseData#{<<"basicdata">> => maps:merge(Data3, Data)}});
|
||||
_ ->
|
||||
?LOG(info, "Device not exist")
|
||||
end;
|
||||
false ->
|
||||
pass
|
||||
end,
|
||||
{ok, State};
|
||||
|
||||
handle_message({rule, #{clientid := DevAddr, payload := Payload, topic := <<"PeriodicInformation">>}, _Msg},
|
||||
#state{id = ChannelId} = State) ->
|
||||
ProductId = dgiot_data:get({simahe, <<"PeriodicInformation">>}),
|
||||
dgiot_mqtt:publish(DevAddr, <<"PeriodicInformation/", DevAddr/binary>>, Payload),
|
||||
dgiot_bridge:send_log(ChannelId, "topic:~p Payload:~p ~n", [<<DevAddr/binary, "/", "PeriodicInformation">>, Payload]),
|
||||
case dgiot_data:get({simahe, DevAddr}) of
|
||||
{ok, 1} -> pass;
|
||||
_ ->
|
||||
dgiot_data:insert({simahe, DevAddr}, 1),
|
||||
dgiot_parse:update_object(<<"Device">>, dgiot_parse:get_deviceid(ProductId, DevAddr), #{<<"isEnable">> => true, <<"status">> => <<"ONLINE">>})
|
||||
end,
|
||||
case jsx:is_json(Payload) of
|
||||
true ->
|
||||
Data1 = format_periodic(jsx:decode(Payload, [{labels, binary}, return_maps])),
|
||||
List = maps:to_list(Data1),
|
||||
Result =
|
||||
if
|
||||
length(List) > 40 ->
|
||||
{ok, Data1};
|
||||
true ->
|
||||
case dgiot_data:get({periodicInformation, DevAddr}) of
|
||||
not_find ->
|
||||
dgiot_data:insert({periodicInformation, DevAddr}, Data1),
|
||||
{error, #{}};
|
||||
Data2 ->
|
||||
dgiot_data:delete({periodicInformation, DevAddr}),
|
||||
{ok, maps:merge(Data1, Data2)}
|
||||
end
|
||||
end,
|
||||
case Result of
|
||||
{ok, Data} ->
|
||||
dgiot_bridge:send_log(ChannelId, "topic:~p Data:~p ~n", [<<DevAddr/binary, "/", "PeriodicInformation">>, Data]),
|
||||
case length(maps:to_list(Data)) < 40 of
|
||||
true -> pass;
|
||||
_ ->
|
||||
dgiot_sinmahe:update_runstate(ProductId, DevAddr, Data),
|
||||
dgiot_sinmahe:update_powerstate(ProductId, DevAddr, Data),
|
||||
dgiot_tdengine_adapter:save(ProductId, DevAddr, Data)
|
||||
end;
|
||||
_ -> pass
|
||||
end;
|
||||
false ->
|
||||
pass
|
||||
end,
|
||||
{ok, State};
|
||||
|
||||
handle_message({rule, #{clientid := DevAddr, payload := Payload, topic := <<"FaultInformation">>}, _Msg}, #state{id = ChannelId} = State) ->
|
||||
dgiot_bridge:send_log(ChannelId, "topic:~p Payload:~p ~n", [<<DevAddr/binary, "/", "FaultInformation">>, Payload]),
|
||||
dgiot_mqtt:publish(DevAddr, <<"FaultInformation/", DevAddr/binary>>, Payload),
|
||||
case jsx:is_json(Payload) of
|
||||
true ->
|
||||
Data = jsx:decode(Payload, [{labels, binary}, return_maps]),
|
||||
FaultProductId1 = dgiot_data:get({simahe, <<"FaultInformation">>}),
|
||||
ProductId = dgiot_data:get({simahe, <<"PeriodicInformation">>}),
|
||||
dgiot_tdengine_adapter:save(FaultProductId1, <<"FaultInformation_", DevAddr/binary>>, Data),
|
||||
Data1 = Data#{<<"occurtime">> => dgiot_datetime:now_secs()},
|
||||
case dgiot_parse:get_object(<<"Device">>, dgiot_parse:get_deviceid(ProductId, DevAddr)) of
|
||||
{ok, #{<<"objectId">> := ObjectId, <<"name">> := Name, <<"basedata">> := BaseData}} ->
|
||||
FaultData =
|
||||
case maps:find(<<"faultdata">>, BaseData) of
|
||||
{ok, Data2} -> maps:merge(Data2, Data1);
|
||||
error -> Data1
|
||||
end,
|
||||
dgiot_parse:update_object(<<"Device">>, ObjectId, #{<<"isEnable">> => false,
|
||||
<<"basedata">> => BaseData#{<<"faultdata">> => FaultData}}),
|
||||
case maps:find(<<"faultrule">>, BaseData) of
|
||||
{ok, FaultRule} -> dgiot_sinmahe:send(ObjectId, Name, FaultRule, Data);
|
||||
error -> Data1
|
||||
end;
|
||||
_ ->
|
||||
?LOG(info, "Device not exist")
|
||||
end;
|
||||
false ->
|
||||
pass
|
||||
end,
|
||||
{ok, State};
|
||||
|
||||
handle_message(Message, #state{id = ChannelId, product = ProductId} = State) ->
|
||||
?LOG(info, "Channel ~p, Product ~p, handle_message ~p", [ChannelId, ProductId, Message]),
|
||||
{ok, State}.
|
||||
|
||||
|
||||
stop(ChannelType, ChannelId, _State) ->
|
||||
?LOG(info, "channel stop ~p,~p", [ChannelType, ChannelId]),
|
||||
ok.
|
||||
|
||||
|
||||
format_basic(#{<<"WeightFactor">> := WeightFactor} = Message) when is_integer(WeightFactor) ->
|
||||
format_basic(Message#{<<"WeightFactor">> => WeightFactor / 1});
|
||||
format_basic(#{<<"RatedLoad">> := RatedLoad} = Message) when is_integer(RatedLoad) ->
|
||||
format_basic(Message#{<<"RatedLoad">> => RatedLoad / 1});
|
||||
format_basic(#{<<"RatedFreq">> := RatedFreq} = Message) when is_integer(RatedFreq) ->
|
||||
format_basic(Message#{<<"RatedFreq">> => RatedFreq / 1});
|
||||
format_basic(#{<<"RatedPower">> := RatedPower} = Message) when is_integer(RatedPower) ->
|
||||
format_basic(Message#{<<"RatedPower">> => RatedPower / 1});
|
||||
format_basic(#{<<"Lat">> := Lat, <<"Lon">> := Lon} = Message) ->
|
||||
[LonDeg, LatDeg] = dgiot_gps:get_deng_gps(Lon, Lat),
|
||||
Message#{<<"GPS">> => #{<<"Lon">> => LonDeg, <<"Lat">> => LatDeg},
|
||||
<<"Lat">> := LatDeg, <<"Lon">> := LonDeg};
|
||||
|
||||
format_basic(Message) ->
|
||||
Message.
|
||||
|
||||
format_periodic(#{<<"DeadLoad">> := DeadLoad} = Data) when is_integer(DeadLoad) ->
|
||||
format_periodic(Data#{<<"DeadLoad">> := DeadLoad / 1});
|
||||
format_periodic(#{<<"NetWeight">> := NetWeight} = Data) when is_integer(NetWeight) ->
|
||||
format_periodic(Data#{<<"NetWeight">> := NetWeight / 1});
|
||||
format_periodic(#{<<"Iout">> := Iout} = Data) when is_integer(Iout) ->
|
||||
format_periodic(Data#{<<"Iout">> := Iout / 1});
|
||||
format_periodic(#{<<"Vout">> := Vout} = Data) when is_integer(Vout) ->
|
||||
format_periodic(Data#{<<"Vout">> := Vout / 1});
|
||||
format_periodic(#{<<"BusVoltage">> := BusVoltage} = Data) when is_integer(BusVoltage) ->
|
||||
format_periodic(Data#{<<"BusVoltage">> := BusVoltage / 1});
|
||||
format_periodic(#{<<"RunFreq">> := RunFreq} = Data) when is_integer(RunFreq) ->
|
||||
format_periodic(Data#{<<"RunFreq">> := RunFreq / 1});
|
||||
format_periodic(Data) ->
|
||||
Data.
|
@ -1,46 +0,0 @@
|
||||
%%--------------------------------------------------------------------
|
||||
%% Copyright (c) 2020-2021 DGIOT Technologies Co., Ltd. All Rights Reserved.
|
||||
%%
|
||||
%% Licensed under the Apache License, Version 2.0 (the "License");
|
||||
%% you may not use this file except in compliance with the License.
|
||||
%% You may obtain a copy of the License at
|
||||
%%
|
||||
%% http://www.apache.org/licenses/LICENSE-2.0
|
||||
%%
|
||||
%% Unless required by applicable law or agreed to in writing, software
|
||||
%% distributed under the License is distributed on an "AS IS" BASIS,
|
||||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
%% See the License for the specific language governing permissions and
|
||||
%% limitations under the License.
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
-module(dgiot_sinmahe_sup).
|
||||
-include_lib("dgiot/include/logger.hrl").
|
||||
-include("dgiot_sinmahe.hrl").
|
||||
-behaviour(supervisor).
|
||||
|
||||
%% API
|
||||
-export([start_link/0]).
|
||||
|
||||
%% Supervisor callbacks
|
||||
-export([init/1]).
|
||||
|
||||
%% Helper macro for declaring children of supervisor
|
||||
-define(CHILD(I, Type, Args), {I, {I, start_link, Args}, permanent, 5000, Type, [I]}).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% API functions
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
start_link() ->
|
||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||
|
||||
%%--------------------------------------------------------------------
|
||||
%% Supervisor callbacks
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
init([]) ->
|
||||
{ok, {{one_for_one, 5, 10}, [
|
||||
?CHILD(sinmahe_mqttc_sup, supervisor, [])
|
||||
]}}.
|
||||
|
@ -1,98 +0,0 @@
|
||||
%%--------------------------------------------------------------------
|
||||
%% Copyright (c) 2020-2021 DGIOT Technologies Co., Ltd. All Rights Reserved.
|
||||
%%
|
||||
%% Licensed under the Apache License, Version 2.0 (the "License");
|
||||
%% you may not use this file except in compliance with the License.
|
||||
%% You may obtain a copy of the License at
|
||||
%%
|
||||
%% http://www.apache.org/licenses/LICENSE-2.0
|
||||
%%
|
||||
%% Unless required by applicable law or agreed to in writing, software
|
||||
%% distributed under the License is distributed on an "AS IS" BASIS,
|
||||
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
%% See the License for the specific language governing permissions and
|
||||
%% limitations under the License.
|
||||
%%--------------------------------------------------------------------
|
||||
|
||||
-module(dgiot_sinmahe_handler).
|
||||
-include_lib("dgiot/include/logger.hrl").
|
||||
-author("dgiot").
|
||||
-behavior(dgiot_rest).
|
||||
-dgiot_rest(all).
|
||||
|
||||
%% API
|
||||
-export([swagger_sinmahe/0, swagger_python/0]).
|
||||
-export([handle/4]).
|
||||
|
||||
%% API描述
|
||||
%% 支持二种方式导入
|
||||
%% 示例:
|
||||
%% 1. Metadata为map表示的JSON,
|
||||
%% dgiot_http_server:bind(<<"/pump">>, ?MODULE, [], Metadata)
|
||||
%% 2. 从模块的priv/swagger/下导入
|
||||
%% dgiot_http_server:bind(<<"/swagger_sinmahe.json">>, ?MODULE, [], priv)
|
||||
swagger_sinmahe() ->
|
||||
[
|
||||
dgiot_http_server:bind(<<"/swagger_sinmahe.json">>, ?MODULE, [], priv)
|
||||
].
|
||||
|
||||
swagger_python() ->
|
||||
[
|
||||
dgiot_http_server:bind(<<"/swagger_python.json">>, ?MODULE, [], priv)
|
||||
].
|
||||
|
||||
%%%===================================================================
|
||||
%%% 请求处理
|
||||
%%% 如果登录, Context 内有 <<"user">>, version
|
||||
%%%===================================================================
|
||||
|
||||
-spec handle(OperationID :: atom(), Args :: map(), Context :: map(), Req :: dgiot_req:req()) ->
|
||||
{Status :: dgiot_req:http_status(), Body :: map()} |
|
||||
{Status :: dgiot_req:http_status(), Headers :: map(), Body :: map()} |
|
||||
{Status :: dgiot_req:http_status(), Headers :: map(), Body :: map(), Req :: dgiot_req:req()}.
|
||||
|
||||
handle(OperationID, Args, Context, Req) ->
|
||||
Headers = #{},
|
||||
case catch do_request(OperationID, Args, Context, Req) of
|
||||
{ErrType, Reason} when ErrType == 'EXIT'; ErrType == error ->
|
||||
?LOG(info, "do request: ~p, ~p, ~p~n", [OperationID, Args, Reason]),
|
||||
Err = case is_binary(Reason) of
|
||||
true -> Reason;
|
||||
false ->
|
||||
dgiot_utils:format("~p", [Reason])
|
||||
end,
|
||||
{500, Headers, #{<<"error">> => Err}};
|
||||
ok ->
|
||||
%% lager:debug("do request: ~p, ~p ->ok ~n", [OperationID, Args]),
|
||||
{200, Headers, #{}, Req};
|
||||
{ok, Res} ->
|
||||
%% ?LOG(info,"do request: ~p, ~p ->~p~n", [OperationID, Args, Res]),
|
||||
{200, Headers, Res, Req};
|
||||
{Status, Res} ->
|
||||
%% ?LOG(info,"do request: ~p, ~p ->~p~n", [OperationID, Args, Res]),
|
||||
{Status, Headers, Res, Req};
|
||||
{Status, NewHeaders, Res} ->
|
||||
%% ?LOG(info,"do request: ~p, ~p ->~p~n", [OperationID, Args, Res]),
|
||||
{Status, maps:merge(Headers, NewHeaders), Res, Req}
|
||||
end.
|
||||
|
||||
|
||||
%%%===================================================================
|
||||
%%% 内部函数 Version:API版本
|
||||
%%%===================================================================
|
||||
|
||||
do_request(get_sinmahe_count, _Args, #{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
|
||||
dgiot_sinmahe:get_device_count(SessionToken);
|
||||
|
||||
do_request(post_control_device, Args, #{<<"sessionToken">> := SessionToken} = _Context, _Req) ->
|
||||
dgiot_sinmahe:post_control_device(Args, SessionToken);
|
||||
|
||||
do_request(get_sinmahe_fault, Args, #{<<"sessionToken">> := _SessionToken} = _Context, _Req) ->
|
||||
{ok, dgiot_sinmahe:get_fault(Args)};
|
||||
|
||||
do_request(post_sinmahe_fault, _Args, #{<<"sessionToken">> := _SessionToken} = _Context, _Req) ->
|
||||
{ok, dgiot_sinmahe:post_fault()};
|
||||
|
||||
%% 服务器不支持的API接口
|
||||
do_request(_OperationId, _Args, _Context, _Req) ->
|
||||
{error, <<"Not Allowed.">>}.
|
@ -19,4 +19,3 @@
|
||||
{dgiot_topo, {{enable_plugin_dgiot_topo}}}.
|
||||
{dgiot_opc, {{enable_plugin_dgiot_opc}}}.
|
||||
{dgiot_niisten, {{enable_plugin_dgiot_niisten}}}.
|
||||
{dgiot_sinmahe, {{enable_plugin_dgiot_sinmahe}}}.
|
||||
|
@ -197,7 +197,6 @@ overlay_vars_rel(RelType) ->
|
||||
, {enable_plugin_dgiot_topo, true}
|
||||
, {enable_plugin_dgiot_opc, true}
|
||||
, {enable_plugin_dgiot_niisten, true}
|
||||
, {enable_plugin_dgiot_sinmahe, true}
|
||||
, {vm_args_file, VmArgs}
|
||||
].
|
||||
|
||||
@ -319,7 +318,6 @@ relx_plugin_apps_per_rel(cloud) ->
|
||||
, dgiot_topo
|
||||
, dgiot_opc
|
||||
, dgiot_niisten
|
||||
, dgiot_sinmahe
|
||||
];
|
||||
relx_plugin_apps_per_rel(edge) ->
|
||||
[].
|
||||
|
Loading…
Reference in New Issue
Block a user