前言
本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/108971807
fly-iot飞凡物联专栏:
https://blog.csdn.net/freewebsys/category_12219758.html
未经博主允许不得转载。
博主CSDN地址是:https://blog.csdn.net/freewebsys
博主掘金地址是:https://juejin.cn/user/585379920479288
博主知乎地址是:https://www.zhihu.com/people/freewebsystem
1,关于ActorCloud
github项目地址:
https://github.com/actorcloud/ActorCloud
gitbook 文档地址:https://wivwiv.gitbooks.io/emq-actorcloud-doc/content/introduction/introduction.html
ActorCloud 使用手册 ActorCloud 是一个面向低功耗IoT网络,为企业提供一站式平台服务的物联网平台。ActorCloud 在安全可靠的基础上,为设备提供多种协议接入的通信能力,设备数据与消息流管理功能。
平台提供基础设备管理功能以连接、管理海量设备,实现设备的消息通信与数据采集持久化;集成规则引擎与数据可视化管理,灵活开放多种权限级别的管理、控制 API,通过 API 可快速开发上层应用,实现多端接入、设备远程控制。
IoT Hub:为终端上云建立可靠双向连接通道,进行认证鉴权、协议解析与消息路由; 设备管理:终端注册开通与生命周期管理,提供状态、故障、流量的不间断监控; 数据引擎:对获取的终端消息高速持久化、实时解析、规则事务处理与可视化展示; 应用使能:提供终端SDK、APP SDK,开放丰富的 REST API 接口,集成消息推送接口。
2,关于Actorcloud后端项目
后端项目使用的是python代码进行编写的,使用的web框架是flask,数据库操作使用 sqlalchemy ,数据库使用的是 postgresql。
项目的结构是:
.
├── actor_libs
│ ├── auth
│ ├── base_test.py
│ ├── cache
│ ├── database
│ ├── decorators.py
│ ├── emqx
│ ├── errors.py
│ ├── http_tools
│ ├── __init__.py
│ ├── logs.py
│ ├── manage
│ ├── schemas
│ ├── send_mails.py
│ ├── tasks
│ ├── types
│ └── utils.py
├── app
│ ├── base.py
│ ├── __init__.py
│ ├── models.py
│ ├── schemas.py
│ └── services
├── config
│ ├── base
│ ├── certs
│ ├── config.yml
│ └── __init__.py
├── docker-entrypoint.sh
├── Dockerfile
├── instance
│ └── certs
├── manage.py
├── migrations
├── Pipfile
├── Pipfile.lock
├── README.md
├── requirements.txt
├── run.py
└── static
├── download
├── images
└── upload
在配置文件中:
config/config.yml
actorcloud:
language: zh # system language: en, zh
timezone: UTC # system timezone
log_level: error # log level: debug, info, error
log_path: /opt/tmp/logs # log store path
actorcloud_api: /api/v1 # api version
backend_node: 0.0.0.0:7000 # backend server ip:port
async_tasks_node: 0.0.0.0:7001 # async task node
codec_node: 0.0.0.0:7002 # codec node
secret_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # flask secret key 32 < len < 64
admin_email: [email protected] # super administrator login username
admin_password: public # super administrator login password
site_name: ActorCloud # the site name
site_domain: http://localhost # the site site domain
email_title: ActorCloud registration invitation # invitation registration email title
default_devices_limit: -1 # number of devices that tenant can manage. no limit is -1
postgres:
postgres_host: 127.0.0.1
postgres_port: 5432
postgres_user: actorcloud
postgres_password: public
postgres_database: actorcloud
emqx:
emqx_api_version: /api/v3
emqx_app_id: actorcloud # emqx application AppID
emqx_app_secret: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # emqx application AppSecret
emqx_lb_ip: 127.0.0.1 # cluster LB ip. if there is no cluster, it is emqx node ip
emqx_lb_port: 8080 # emqx http lb api port, the default is 8080
emqx_nodes:
# emqx node1 internal ip
- username: ubuntu
ip: 192.168.1.2
port: 22
# emqx node2 internal ip
- username: ubuntu
ip: 192.168.1.3
port: 22
mail:
mail_server: '' # smtp server
mail_port: '' # smtp server port
mail_use_ssl: false # whether to use ssl
mail_use_tls: true # whether to use tls
mail_username: '' # email username
mail_password: '' # email password
mail_default_sender: '' # default sender
stream:
stream_ip: 127.0.0.1 # pulsar ip
stream_port: 8888 # pulsar port
使用的是postgres 数据库,并且使用 db = SQLAlchemy 做了一个简单的ORM,封装成对象操作,和封装了分页操作:
最重要的模块就在 app/services 目录下面:
.
├── alerts
│ ├── __init__.py
│ ├── models.py
│ ├── resources.yml
│ ├── schemas.py
│ └── views
├── applications
│ ├── __init__.py
│ ├── models.py
│ ├── resources.yml
│ ├── schemas.py
│ └── views
├── base
│ ├── __init__.py
│ ├── models.py
│ ├── resources.yml
│ ├── schemas.py
│ └── views
├── device_data
│ ├── __init__.py
│ ├── models.py
│ ├── README.md
│ ├── resources.yml
│ ├── schemas.py
│ └── views
├── devices
│ ├── __init__.py
│ ├── models.py
│ ├── resources.yml
│ ├── schemas.py
│ └── views
├── __init__.py
├── products
│ ├── __init__.py
│ ├── models.py
│ ├── resources.yml
│ ├── schemas.py
│ └── views
├── publish
│ ├── __init__.py
│ ├── models.py
│ ├── README.md
│ ├── resources.yml
│ ├── schemas.py
│ └── views
├── rules
│ ├── __init__.py
│ ├── models.py
│ ├── resources.yml
│ ├── schemas.py
│ └── views
└── tasks_scheduler
├── async_tasks
├── __init__.py
├── README.md
└── timer_tasks
比如在产品服务中:
@bp.route('/products')
@auth.login_required
def list_products():
code_list = ['cloudProtocol', 'productType']
records = Product.query.pagination(code_list=code_list)
# Count the number of devices, applications,
# data points, and data streams of the product
records_item = records['items']
records['items'] = records_item_count(records_item)
return jsonify(records)
使用bp.route 注解设置路由地址,然后使用 @auth.login_required 进行登陆拦截。
最后使用 Product.query.pagination 进行分页查询。
而且需要依赖数据库使用 postgresql 数据库。目前没有找到建表的方法。
可以先创建一个基础镜像,这样在使用真正构建的时候用这个基础镜像。
把安装依赖的包都加入。
基础镜像的方法:DockerfileBase
# 构建后端代码:
# docker build --tag actorcloud-server:v3.0.0 .
# FROM python:3-alpine 使用最新的slim 版本。
# docker build -f ./DockerfileBase -t python:3.6-slim-server-base .
FROM python:3.6-slim
COPY docker-entrypoint.sh /usr/bin/
# 设置debian的镜像源
RUN echo "deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" > /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib" >> /etc/apt/sources.list && \
echo "deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list && \
echo "deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib" >> /etc/apt/sources.list
# 设置python3的镜像源
RUN mkdir /root/.pip/ && echo "[global]" > /root/.pip/pip.conf && \
echo "index-url = https://mirrors.aliyun.com/pypi/simple/" >> /root/.pip/pip.conf && \
echo "[install]" >> /root/.pip/pip.conf && \
echo "trusted-host=mirrors.aliyun.com" >> /root/.pip/pip.conf
# 安装需要的lib库
RUN apt update && apt-get install -y locales libpq-dev python3-dev gcc g++ make && \
apt clean
#ENV LC_ALL="zh_CN.UTF-8" LANG="zh_CN.UTF-8"
#ENV LC_ALL="en_US.utf-8" LANG="en_US.utf-8"
ENV LC_ALL="C.UTF-8" LANG="C.UTF-8"
ADD requirements.txt /root/requirements.txt
RUN cd /root && pip3 install -r requirements.txt && rm -rf /root/.cache/
执行基础镜像:
docker build -f ./DockerfileBase -t python:3.6-slim-server-base .
把当前的代码加入 data 然后再启动:
docker run -itd --name py3 -p 7000:7000 -v `pwd`:/data python:3.6-slim-server-base
$ python3 run.py backend
* Running on http://0.0.0.0:7000/ (Press CTRL+C to quit)
端口是7000,
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "users" does not exist
LINE 2: FROM users LEFT OUTER JOIN tenants ON tenants."tenantID" = u...
^
数据库表不存在
解决:No module named ‘werkzeug._compat’
降级 flask 版本到 0.12.5 版本即可。
同时解决:
Traceback (most recent call last):
File "run.py", line 67, in <module>
actorcloud_run()
File "/usr/local/lib/python3.6/site-packages/click/core.py", line 829, in __call__
return self.main(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/click/core.py", line 760, in main
_verify_python3_env()
File "/usr/local/lib/python3.6/site-packages/click/_unicodefun.py", line 130, in _verify_python3_env
" mitigation steps.{}".format(extra)
RuntimeError: Click will abort further execution because Python 3 was configured to use ASCII as encoding for the environment. Consult https://click.palletsprojects.com/python3/ for mitigation steps.
This system supports the C.UTF-8 locale which is recommended. You might be able to resolve your issue by exporting the following environment variables:
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
# 安装 locales 再设置环境变量:
apt install -y locales
export LC_ALL=C.UTF-8
export LANG=C.UTF-8
3,总结
终于把Actorcloud的后端环境也弄好了,使用的是python3+flask + sqlalchemy 还有其他组件开发的应用。
后端终于可以启动了。然后就是研究咋做把数据初始化了。
本文的原文连接是:
https://blog.csdn.net/freewebsys/article/details/108971807