Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

porting to v2 #6

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
9ab73b1
Add files via upload
jasmine8988 Apr 10, 2023
2b4768b
Update server.py
jasmine8988 Apr 10, 2023
e145710
Update and rename app0330.py to app.py
jasmine8988 Apr 10, 2023
ba17e43
Update server.py
jasmine8988 Apr 10, 2023
a2fe8a6
Update app.py
jasmine8988 Apr 12, 2023
51a8e0f
Update requirements.txt
jasmine8988 Apr 14, 2023
14b45a9
Update api.py
jasmine8988 Apr 18, 2023
0a21efd
Update config.py
jasmine8988 Apr 20, 2023
eac7f6f
Update server.py
jasmine8988 Apr 20, 2023
de1c6fc
Create CHANGELOG.md
jasmine8988 Apr 21, 2023
1ed5a1d
Update CHANGELOG.md
jasmine8988 Apr 21, 2023
6685398
Add files via upload
jasmine8988 May 3, 2023
01a0ebc
Add files via upload
jasmine8988 May 3, 2023
3062640
Add files via upload
jasmine8988 May 3, 2023
c04c0b9
Update css.html
jasmine8988 May 3, 2023
6b1279b
Update script.html
jasmine8988 May 3, 2023
2c592d3
Add files via upload
jasmine8988 May 3, 2023
96316f8
Add files via upload
jasmine8988 May 3, 2023
c4202f0
Update CHANGELOG.md
jasmine8988 May 3, 2023
ccca985
Delete c3.css
jasmine8988 May 3, 2023
c8648ba
Delete c3.min.css
jasmine8988 May 3, 2023
a77500a
Delete d3.js
jasmine8988 May 3, 2023
6710f33
Delete c3.min.js
jasmine8988 May 3, 2023
d9ce9bf
Delete c3.js
jasmine8988 May 3, 2023
5f5a444
Delete d3.min.js
jasmine8988 May 3, 2023
6eef292
Delete test.html
jasmine8988 May 3, 2023
8b67017
Update css.html
jasmine8988 May 3, 2023
bed5122
Update script.html
jasmine8988 May 3, 2023
85f4428
Update config.py
jasmine8988 May 4, 2023
78855e5
Add files via upload
jasmine8988 May 10, 2023
1905f6b
Delete app.py
jasmine8988 May 10, 2023
3e7830d
Update sa.py
jasmine8988 May 10, 2023
6ba89ef
delete legacy code
jasmine8988 May 11, 2023
3697a7e
Update da/sa.py
jasmine8988 May 11, 2023
daa2fa4
config.py
jasmine8988 May 28, 2023
17fa97f
Merge branch 'testliao' of https://github.com/IoTtalk/FarmDashboard i…
jasmine8988 May 28, 2023
77f31f2
CHANGELOG.md
jasmine8988 May 28, 2023
d015e3c
config.py
jasmine8988 May 28, 2023
c411403
update server.py
jasmine8988 May 31, 2023
93fcec8
update da/sa.py
jasmine8988 May 31, 2023
c60a712
Update README.md
jasmine8988 Oct 30, 2023
35a00bd
Update README.md
jasmine8988 Oct 30, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 2023-04-20

### Added

- 讓使用者自定要使用V1版本還是V2版本 (#eac7f6f)
- 新增常數`IOTTALK_VERSION` (#0a21efd)

## 2023-04-18

### Changed

- tablename的命名方式,改成只刪除-O,不刪後面的數字 (#14b45a9)

## 2023-04-12

### Changed

- IoTtalk-v2可使用的dashboard(dynamic) (#a2fe8a6)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這句是什麼意思?CHANGELOG 裡儘量放明確一點的文字,最好是能一看就知道意思。

然後如果是過程中的變動,不是最後結果的就不用放了,
比如這句 dynamic 跟下面的 static,這是在做一樣的事,最後的結果是 dynamic 的話,那 static 就不需要放了。


## 2023-04-10

### Added

- IoTtalk-v2可使用的dashboard(static)(#9ab73b1)

### Changed

- rename `app0330.py` to `app.py` (#e145710)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CHANGELOG 的對象是最後要使用系統的人,像這種開發訊息就不需要放在 CHANGELOG 了,
放在 commit log 中就可以


## Rule
每一個軟體的版本必須:
* 標明日期(要用上面說過的規範)
* 標明分類(採用英文)。規範如下:
* 'Added' 添加的新功能
* 'Changed' 功能變更
* 'Deprecated' 不建議使用,未來會刪掉
* 'Removed' 之前不建議使用的功能,這次真的刪掉了
* 'Fixed' 修正的 bug
* 'Security' 修正了安全相關的 bug
6 changes: 4 additions & 2 deletions app/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,8 @@ def api_sensor():
if sensor_record > 0:
return 'The sensor name "{}" or df_name "{}" already exists'.format(name, df_name), 404

db.inject_new_model(re.sub(r'-O[\d]*$', '', df_name))
#db.inject_new_model(re.sub(r'-O[\d]*$', '', df_name))
jasmine8988 marked this conversation as resolved.
Show resolved Hide resolved
db.inject_new_model(re.sub(r'-O', '', df_name))

new_sensor = db.models.sensor(df_name=df_name,
name=request.json.get('name'),
Expand Down Expand Up @@ -563,7 +564,8 @@ def api_sensor():
if sensor_record > 0:
return 'The sensor name "{}" or df_name "{}" already exists'.format(name, df_name), 404

db.inject_new_model(re.sub(r'-O[\d]*$', '', df_name))
#db.inject_new_model(re.sub(r'-O[\d]*$', '', df_name))
db.inject_new_model(re.sub(r'-O', '', df_name))

(g.session
.query(db.models.sensor)
Expand Down
2 changes: 1 addition & 1 deletion app/templates/base/css.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
jasmine8988 marked this conversation as resolved.
Show resolved Hide resolved
<![endif]-->
5 changes: 5 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@

# IoTtalk server's 'IP' or 'DomainName' only, without any protocol 'http://' or 'https://'.
# ex: '8.8.8.8' or 'google.com',
# if you want to use IoTtalk V1 :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CSM_HOST 的註解應該跟 IOTTALK_VERSION 分開,因為不管 v1還是 v2 都需要 CSM_HOST。

IOTTALK_VERSION 的說明,應該是指出使用者連接的 CSM 所使用的 IoTtalk version,然後說明接受的值為 1 或 2,default 為 1 這樣

CSM_HOST = '127.0.0.1:9999'
IOTTALK_VERSION = 1
# if you want to use IoTtalk V2 :
#CSM_HOST = 'https://iottalk2.tw/csm'
#IOTTALK_VERSION = 2

# For the demo page without login
# The data format is '<Field Name>':'<token>', examples are shown as follows.
Expand Down
103 changes: 103 additions & 0 deletions da/sa.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import time
import logging
import re
jasmine8988 marked this conversation as resolved.
Show resolved Hide resolved
from datetime import datetime
from db import db
from iottalkpy.dai import module_to_sa
from config import CSM_HOST as host
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const 的參數就不要用 alias name 了,大寫才會知道是 const,不然會誤會成一般變數


log = logging.getLogger("\033[1;35m[DA]: \033[0m")


def on_register(dan):
print('register successfully')


class SaClass:
def __init__(self, host, field, field_id, profile={}, odf_list=[],
alert_range={}, device_addr=None, **kwargs):
self.host = host
self.field = field
self.field_id = field_id
self.profile = profile
self.odf_list = odf_list
self.alert_range = alert_range
self.device_addr = device_addr
self.device_name = field.name + '_DataServer'
self.api_url = host # get from config CSM_HOST
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

這句怪怪的,host 這樣不就衝突了嗎?到底是要 CSM_HOST 的 host 還是 init 參數中的 host,然後如果確定變數是用在 api_url 的話,那第19行就不需要存成 host了,init 參數的 host 也可以改成 api_URL

self.device_model = 'DataServer'
self.push_interval = 10
self.interval = {'Dummy_Sensor': 3, }
jasmine8988 marked this conversation as resolved.
Show resolved Hide resolved
self.kwargs = kwargs

for k, v in kwargs.items():
setattr(self, k, v)

for odf in odf_list:
jasmine8988 marked this conversation as resolved.
Show resolved Hide resolved
odf = re.sub(r'-', r'_', odf)
setattr(self, odf, lambda data, odf=odf: self.run(data, odf))

def run(self, data: list, odf):
jasmine8988 marked this conversation as resolved.
Show resolved Hide resolved
try:
session = db.get_session()
if data:
value = float(data[0])
odf_class = getattr(db.models, re.sub(r"[-_]O", "", odf))
new_record = odf_class(timestamp=datetime.now(),
field=self.field_id,
value=value)
session.add(new_record)
session.commit()

except KeyboardInterrupt:
log.info(self.field, ': exit')
except Exception as e:
log.error('[ERROR]:', str(e))
if str(e).find('mac_addr not found:') != -1:
jasmine8988 marked this conversation as resolved.
Show resolved Hide resolved
log.error('Reg_addr is not found. Try to re-register...')
else:
log.error('Connection failed due to unknow reasons.')
time.sleep(1)
jasmine8988 marked this conversation as resolved.
Show resolved Hide resolved
finally:
session.close()


def main():
db.connect()
processes = []

session = db.get_session()

for field in (session.query(db.models.field).all()):
profile = {'d_name': field.name + '_DataServer',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不需要 profile了吧,這是 v1 的用法,應該可以直接建立各個變數塞進 SaClass 中

'dm_name': 'DataServer',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dm_named_name 你這樣指定一次,可是沒有用到,又在 SaClass 中指定一次,
統一在這邊指定塞進 SaClass 中吧

'odf_list': [],
'is_sim': False}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_sim 應該用不到?

alert_range = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alert_range 的東西先拿掉好了,目前用不到

query_df = (session.query(db.models.field_sensor)
.select_from(db.models.field_sensor)
.join(db.models.sensor)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只取 field_sensor 的資料的話,有需要 join sensor 嗎?

.filter(db.models.field_sensor.field == field.id)
.all())
for df in query_df:
profile['odf_list'].append(df.df_name)
alert_range[df.df_name] = {'min': df.alert_min,
'max': df.alert_max}
sa = SaClass(host, field, field.id,
profile=profile,
odf_list=profile['odf_list'],
aler_range=alert_range)

process = module_to_sa(sa)
process.daemon = True
process.start()
processes.append(process)

session.close()

for process in processes:
process.join()


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ pymysql~=0.10.1
tzlocal~=2.1

MarkupSafe==2.0.1

iottalk-py==2.3.1
5 changes: 4 additions & 1 deletion server.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def _start_server():


def _start_da():
from da.DAI import main as da
if config.IOTTALK_VERSION == 1:
from da.DAI import main as da
elif config.IOTTALK_VERSION == 2:
from da.app import main as da
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是 da.app.main 還是 da.sa.main?

da()


Expand Down