-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
1260 lines (1190 loc) · 41.4 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from datetime import datetime
from flask import Flask, g, request
from flask_sqlalchemy import SQLAlchemy
from flask_apscheduler import APScheduler
from zoneinfo import ZoneInfo
class Config:
SCHEDULER_API_ENABLED = True
scheduler = APScheduler()
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)
app.config.from_object(Config())
scheduler.init_app(app)
# データベースのテーブルを作成
class wetness_value(db.Model):
__tablename__ = 'wetness_value'
id = db.Column(db.Integer, unique=True, primary_key=True)
timestamp = db.Column(db.String, nullable=False)
value = db.Column(db.Integer, nullable=False)
address = db.Column(db.Integer, nullable=False)
class temperature_value(db.Model):
__tablename__ = 'temperature_value'
id = db.Column(db.Integer, unique=True, primary_key=True)
timestamp = db.Column(db.String, nullable=False)
value = db.Column(db.Integer, nullable=False)
address = db.Column(db.Integer, nullable=False)
class humidity_value(db.Model):
__tablename__ = 'humidity_value'
id = db.Column(db.Integer, unique=True, primary_key=True)
timestamp = db.Column(db.String, nullable=False)
value = db.Column(db.Integer, nullable=False)
address = db.Column(db.Integer, nullable=False)
class addresses(db.Model):
__tablename__ = 'addresses'
address = db.Column(db.Integer, unique=True, primary_key=True)
name = db.Column(db.String, unique=True, nullable=False)
class water_supply(db.Model):
__tablename__ = 'water_supply'
id = db.Column(db.Integer, unique=True, primary_key=True)
timestamp = db.Column(db.String, nullable=False)
type = db.Column(db.Integer, nullable=False)
address = db.Column(db.Integer, nullable=False)
class watering_regular(db.Model):
__tablename__ = 'watering_regular'
id = db.Column(db.Integer, unique=True, primary_key=True)
timestamp = db.Column(db.String, nullable=False)
time_hour = db.Column(db.Integer, nullable=False)
time_minutes = db.Column(db.Integer, nullable=False)
weekday = db.Column(db.String, nullable=False)
address = db.Column(db.Integer, nullable=False)
class instructions(db.Model):
__tablename__ = 'instructions'
id = db.Column(db.Integer, unique=True, primary_key=True)
timestamp = db.Column(db.String, nullable=False)
address = db.Column(db.Integer, nullable=False)
instruction = db.Column(db.Integer, nullable=False)
# テーブルを作成
with app.app_context():
db.create_all()
weekdays_set = ["mon", "tue", "wed", "thu", "fri", "sat", "sun", "all"]
# グラフを表示する
@app.route('/', methods=['GET'])
def index():
# /?address=1
# addressが指定されている場合は、そのaddressのデータを取得
# 指定されていない場合は、全addressのデータを取得
address = request.args.get('address')
name = request.args.get('name')
# # ,区切りでaddressを取得
# if designated_addresses:
# designated_addresses = designated_addresses.split(',')
# else:
# designated_addresses = []
# print(designated_addresses)
# name = request.args.get('name')
print(f"addresses : {address}")
print(f"name : {name}")
if not address and name: # nameのみ
print(f"nameだけだお")
# name -> address
try:
address = addresses.query.filter_by(name=name).first().address
except:
print(f"{name}そんなものはない")
wetnesse_array = []
supply_array = []
temperature_array = []
humidity_array = []
# 指定されたaddressのデータを取得
if address:
wetness_data = wetness_value.query.filter_by(address=address).all()
supply_data = water_supply.query.filter_by(address=address, type=1).all()
temperature_data = temperature_value.query.filter_by(address=address).all()
humidity_data = humidity_value.query.filter_by(address=address).all()
for v in wetness_data:
wetnesse_array.append({"value":v.value, "timestamp":v.timestamp})
for v in supply_data:
supply_array.append({"type":v.type, "timestamp":v.timestamp})
for v in temperature_data:
temperature_array.append({"value":v.value, "timestamp":v.timestamp})
for v in humidity_data:
humidity_array.append({"value":v.value, "timestamp":v.timestamp})
else:
return f"""
<html>
<head>
</head>
<body>
<h1>Watering</h1>
<p>address or nameを指定してください</p>
<p>addressが優先されます</p>
<form action="/" method="get">
<p>address : <input type="text" name="address" placeholder="例) 1"></p>
<p>name : <input type="text" name="name" placeholder="例)office"></p>
<p><input type="submit" value="submit"></p>
</form>
<h2>Wetness Value</h2>
<div id="wetness_value"><!-- ここにグラフを表示する --></div>
<h2>Temperature Value</h2>
<div id="temperature_value"><!-- ここにグラフを表示する --></div>
<h2>Humidity Value</h2>
<div id="humidity_value"><!-- ここにグラフを表示する --></div>
</body>
"""
# print("--------------------")
# print(f"wetness_values : {wetnesse_array}")
# print("--------------------")
# print(f"supply_values : {supply_array}")
# print("--------------------")
# print(f"temperature_values : {temperature_array}")
# print("--------------------")
# print(f"humidity_values : {humidity_array}")
# print("--------------------")
# https://cdn.plot.ly/plotly-latest.min.js を使用してグラフを表示
# wetness_value, temperature_value, humidity_valueのデータを取得
# それぞれのaddressごとにデータを取得
return f"""
<html>
<head>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
<h1>Watering</h1>
<p>address or nameを指定してください</p>
<p>addressが優先されます</p>
<form action="/" method="get">
<p>address : <input type="text" name="address" placeholder="例) 1" value="{address}" ></p>
<p>name : <input type="text" name="name" placeholder="例)office" value="{name}" ></p>
<p><input type="submit" value="submit"></p>
</form>
<h2>Wetness Value</h2>
<div id="wetness_value"><!-- ここにグラフを表示する --></div>
<h2>Temperature Value</h2>
<div id="temperature_value"><!-- ここにグラフを表示する --></div>
<h2>Humidity Value</h2>
<div id="humidity_value"><!-- ここにグラフを表示する --></div>
<script>
var wetness_value = [{{
x: [{','.join([f'"{v["timestamp"]}"' for v in wetnesse_array])}],
y: [{','.join([str(v["value"]) for v in wetnesse_array])}],
mode: 'lines+markers',
type: 'scatter',
}}];
var supply_value = [{{
x: [{','.join([f'"{v["timestamp"]}"' for v in supply_array])}],
y: [{','.join([str(v["type"]) for v in supply_array])}],
mode: 'markers',
type: 'scatter',
}}];
var temperature_value = [{{
x: [{','.join([f'"{v["timestamp"]}"' for v in temperature_array])}],
y: [{','.join([str(v["value"]) for v in temperature_array])}],
mode: 'lines+markers',
type: 'scatter',
}}];
var humidity_value = [{{
x: [{','.join([f'"{v["timestamp"]}"' for v in humidity_array])}],
y: [{','.join([str(v["value"]) for v in humidity_array])}],
mode: 'lines+markers',
type: 'scatter',
}}];
var layout = {{
hovermode:'closest',
autosize: false,
title: 'Wetness Value',
xaxis: {{
title: 'timestamp'
}},
yaxis: {{
title: 'value'
}}
}};
var wetness_value_data = wetness_value.concat(supply_value);
Plotly.newPlot('wetness_value', wetness_value_data, layout);
Plotly.newPlot('temperature_value', temperature_value, layout);
Plotly.newPlot('humidity_value', humidity_value, layout);
</script>
</body>
</html>
"""
# timestampを取得 YYYY/MM/DD HH:MM:SS
def get_timestamp():
timestamp = datetime.now(ZoneInfo("Asia/Tokyo"))
return timestamp.strftime('%Y/%m/%d %H:%M:%S')
def address_exists(address):
if addresses.query.filter_by(address=address).first():
return True
else:
return False
# 一番小さい空きアドレスを取得
def get_minimum_free_address():
addresses_data = get_all_address()
minimum_free_address = 1
for data in addresses_data['data']:
if minimum_free_address == data["address"]:
minimum_free_address += 1
else:
break
return minimum_free_address
@app.route('/api/', methods=['GET'])
def func():
return 'hogehoge'
# 全addressの取得
@app.route('/api/addresses', methods=['GET'])
def get_all_address():
try:
addresse = addresses.query.all()
return {
"status": True,
"message": "successfully get all address",
"num_results": f"{len(addresse)}",
"data": [
{
"address": v.address,
"name": v.name
}
for v in addresse
]
}
except:
return {
"status": False,
"message": "failed get all address"
}
# addressの取得
# 使用予定なし
@app.route('/api/addresses/<int:address>', methods=['GET'])
def get_address(address):
try:
try:
address = addresses.query.filter_by(address=address).first()
except:
return {
"status": False,
"message": "address does not exist",
"address": address
}
return {
"status": True,
"message": "successfully get address",
"address": address.address,
"name": address.name
}
except:
return {
"status": False,
"message": "failed get address",
"address": address
}
@app.route('/api/addresses/<string:name>', methods=['GET'])
def get_address_name(name):
try:
try:
address = addresses.query.filter_by(name=name).first()
except:
return {
"status": False,
"message": "name does not exist",
"name": name
}
return {
"status": True,
"message": "successfully get address",
"address": address.address,
"name": address.name
}
except:
return {
"status": False,
"message": "failed get address",
"name": name
}
# 新規address配布
@app.route('/api/addresses', methods=['POST'])
def address_distribution():
name = request.json["name"] #POSTメソッド のnameを取得
address = get_minimum_free_address()
try:
cre = addresses(name = name, address = address)
db.session.add(cre)
db.session.commit()
return {
"status": True,
"message": "successfully distributed address",
"address": address,
"name": name
}
except:
return {
"status": False,
"message": "failed distributed address"
}
# 特定のaddressのnameを変更
# 使用予定なし discordbotで使うかもしれない
@app.route('/api/addresses/<int:address>', methods=['PUT'])
def change_address_name(address):
name = request.json["name"] #POSTメソッド のnameを取得
try:
# 変更前のnameを取得
before_name = addresses.query.filter_by(address=address).first().name
if not address_exists(address): # address存在チェック
return {
"status": False,
"message": "address does not exist",
"address": address,
"name": before_name
}
except:
return {
"status": False,
"message": "database connection error",
"address": address,
"name": before_name
}
try:
addresses.query.filter_by(address=address).update({"name": name}) # addressのnameを変更処理
db.session.commit()
return {
"status": True,
"message": "successfully changed address name",
"address": address,
"name": name
}
except:
return {
"status": False,
"message": "failed changed address name",
"address": address,
"name": before_name
}
# addressの削除
# 使用予定なし <- 要検討
# アドレスが足りなくなることがないので、削除する必要ない
# 使う場合は、addressを削除した場合、そのaddressに関連するデータ
# (instructions,wetness_value,temperature_value,humidity_value,water_supply,watering_regular)
# も削除する必要がある
@app.route('/api/addresses/<int:address>', methods=['DELETE'])
def address_delete(address):
addresses.query.filter_by(address=address).delete()
db.session.commit()
instructions.query.filter_by(address=address).delete()
db.session.commit()
watering_regular.query.filter_by(address=address).delete()
db.session.commit()
temperature_value.query.filter_by(address=address).delete()
db.session.commit()
humidity_value.query.filter_by(address=address).delete()
db.session.commit()
wetness_value.query.filter_by(address=address).delete()
db.session.commit()
water_supply.query.filter_by(address=address).delete()
db.session.commit()
return get_all_address()
# 全水やり指示の取得
@app.route('/api/instructions', methods=['GET'])
def get_all_instructions():
try:
instruction = instructions.query.all()
return {
"status": True,
"message": "successfully get all instructions",
"num_results": f"{len(instruction)}",
"data": [
{
"id": v.id,
"timestamp": v.timestamp,
"address": v.address,
"instruction": v.instruction
}
for v in instruction
]
}
except:
return {
"status": False,
"message": "failed get all instructions"
}
# 特定のaddressの水やり指示の取得
@app.route('/api/instructions/<int:address>', methods=['GET'])
def get_instructions(address):
try:
instruction = instructions.query.filter_by(address=address).all()
return {
"status": True,
"message": "successfully get instructions",
"num_results": f"{len(instruction)}",
"data": [
{
"id": v.id,
"timestamp": v.timestamp,
"address": v.address,
"instruction": v.instruction
}
for v in instruction
]
}
except:
return {
"status": False,
"message": "failed get instructions"
}
# 特定のaddressの最新の水やり指示の取得
# return 要検討!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@app.route('/api/instructions/<int:address>/latest', methods=['GET'])
def get_latest_instructions(address):
try:
instruction = instructions.query.filter_by(address=address).all()
if instruction: # データ存在チェック
return {
"status": True,
"message": "successfully get latest instructions",
"data": {
"id": instruction[-1].id,
"timestamp": instruction[-1].timestamp,
"address": instruction[-1].address,
"instruction": instruction[-1].instruction
}
}
else:
return {
"status": False,
"message": "no data available"
}
except:
return {
"status": False,
"message": "failed get latest instructions"
}
# 給水指示
@app.route('/api/instructions/<int:address>', methods=['POST'])
def set_instructions(address):
instruction = request.json["instruction"] #POSTメソッド のinstructionを取得
if not address_exists(address): # addressが存在チェック
return {
"status": False,
"message": "address does not exist",
"address": address
}
try:
cre = instructions(timestamp = get_timestamp(), address = address, instruction = instruction)
db.session.add(cre)
db.session.commit()
return {
"status": True,
"message": "successfully water supply instruction",
"address": address,
"instruction": instruction
}
except:
return {
"status": False,
"message": "failed water supply instruction"
}
# 全給水履歴の取得
@app.route('/api/supply', methods=['GET'])
def get_all_supply():
try:
supply = water_supply.query.all()
return {
"status": True,
"message": "successfully get all supply",
"num_results": f"{len(supply)}",
"data": [
{
"id": v.id,
"timestamp": v.timestamp,
"type": v.type,
"address": v.address
}
for v in supply
]
}
except:
return {
"status": False,
"message": "failed get all supply"
}
@app.route('/api/supply/latest', methods=['GET'])
def get_all_latest_supply():
try:
supply = water_supply.query.all()
if supply: # データ存在チェック
return {
"status": True,
"message": "successfully get latest supply",
"data": {
"id": supply[-1].id,
"timestamp": supply[-1].timestamp,
"type": supply[-1].type,
"address": supply[-1].address
}
}
else:
return {
"status": False,
"message": "no data available"
}
except:
return {
"status": False,
"message": "failed get latest supply"
}
# 全給水履歴のカウントを取得
# num_resultsと同じ
# 使わない可能性あり
@app.route('/api/supply/count', methods=['GET'])
def get_all_supply_count():
supply = water_supply.query.all()
return {
"status": True,
"message": "successfully get all supply count",
"num_results": f"{len(supply)}"
}
# 特定のaddressの給水履歴の取得
@app.route('/api/supply/<int:address>', methods=['GET'])
def get_supply(address):
try:
supply = water_supply.query.filter_by(address=address).all()
return {
"status": True,
"message": "successfully get supply",
"num_results": f"{len(supply)}",
"data": [
{
"id": v.id,
"timestamp": v.timestamp,
"type": v.type,
"address": v.address
}
for v in supply
]
}
except:
return {
"status": False,
"message": "failed get supply"
}
# 特定のaddressの最新の給水履歴の取得
# return 要検討
@app.route('/api/supply/<int:address>/latest', methods=['GET'])
def get_latest_supply(address):
try:
supply = water_supply.query.filter_by(address=address).all()
if supply: # データ存在チェック
return {
"status": True,
"message": "successfully get latest supply",
"data": {
"id": supply[-1].id,
"timestamp": supply[-1].timestamp,
"type": supply[-1].type,
"address": supply[-1].address
}
}
else:
return {
"status": False,
"message": "no data available"
}
except:
return {
"status": False,
"message": "failed get latest supply"
}
# 特定のaddressの給水履歴のカウントを取得
# num_resultsと同じ
# 使わない可能性あり
@app.route('/api/supply/<int:address>/count', methods=['GET'])
def get_supply_count(address):
supply = water_supply.query.filter_by(address=address).all()
return {
"status": True,
"message": "successfully get supply count",
"num_results": f"{len(supply)}"
}
# 全給水履歴の保存
@app.route('/api/supply/<int:address>', methods=['POST'])
def set_supply(address):
type = request.json["type"]
if not address_exists(address): # addressが存在チェック
return {
"status": False,
"message": "address does not exist",
"address": address
}
try:
cre = water_supply(timestamp = get_timestamp(), type = type, address = address)
db.session.add(cre)
db.session.commit()
return {
"status": True,
"message": "successfully water supply",
"address": address,
"type": type
}
except:
return {
"status": False,
"message": "failed water supply"
}
@app.route('/api/wetness_value', methods=['GET'])
def get_all_wetness_value():
try:
wetness = wetness_value.query.all()
return {
"status": True,
"message": "successfully get all wetness value",
"num_results": f"{len(wetness)}",
"data": [
{
"id": v.id,
"timestamp": v.timestamp,
"value": v.value,
"address": v.address
}
for v in wetness
]
}
except:
return {
"status": False,
"message": "failed get all wetness value"
}
@app.route('/api/wetness_value/latest', methods=['GET'])
def get_all_latest_wetness_value():
try:
wetness = wetness_value.query.all()
if wetness: # データ存在チェック
return {
"status": True,
"message": "successfully get latest wetness value",
"data": {
"id": wetness[-1].id,
"timestamp": wetness[-1].timestamp,
"value": wetness[-1].value,
"address": wetness[-1].address
}
}
else:
return {
"status": False,
"message": "no data available"
}
except:
return {
"status": False,
"message": "failed get latest wetness value"
}
@app.route('/api/wetness_value/count', methods=['GET'])
def get_all_wetness_value_count():
wetness = wetness_value.query.all()
return {
"status": True,
"message": "successfully get all wetness value count",
"num_results": f"{len(wetness)}"
}
@app.route('/api/wetness_value/<int:address>', methods=['GET'])
def get_wetness_value(address):
try:
wetness = wetness_value.query.filter_by(address=address).all()
return {
"status": True,
"message": "successfully get wetness value",
"num_results": f"{len(wetness)}",
"data": [
{
"id": v.id,
"timestamp": v.timestamp,
"value": v.value,
"address": v.address
}
for v in wetness
]
}
except:
return {
"status": False,
"message": "failed get wetness value"
}
@app.route('/api/wetness_value/<int:address>/latest', methods=['GET'])
def get_latest_wetness_value(address):
try:
wetness = wetness_value.query.filter_by(address=address).all()
if wetness: # データ存在チェック
return {
"status": True,
"message": "successfully get latest wetness value",
"data": {
"id": wetness[-1].id,
"timestamp": wetness[-1].timestamp,
"value": wetness[-1].value,
"address": wetness[-1].address
}
}
else:
return {
"status": False,
"message": "no data available"
}
except:
return {
"status": False,
"message": "failed get latest wetness value"
}
@app.route('/api/wetness_value/<int:address>/count', methods=['GET'])
def get_wetness_value_count(address):
wetness = wetness_value.query.filter_by(address=address).all()
return {
"status": True,
"message": "successfully get wetness value count",
"num_results": f"{len(wetness)}"
}
@app.route('/api/wetness_value/<int:address>', methods=['POST'])
def set_wetness_value(address):
value = request.json["value"]
if not address_exists(address):
return {
"status": False,
"message": "address does not exist",
"address": address
}
try:
cre = wetness_value(timestamp = get_timestamp(), value = value, address = address)
db.session.add(cre)
db.session.commit()
return {
"status": True,
"message": "successfully set wetness value",
"address": address,
"value": value
}
except:
return {
"status": False,
"message": "failed set wetness value"
}
@app.route('/api/temperature_value', methods=['GET'])
def get_all_temperature_value():
try:
temperature = temperature_value.query.all()
return {
"status": True,
"message": "successfully get all temperature value",
"num_results": f"{len(temperature)}",
"data": [
{
"id": v.id,
"timestamp": v.timestamp,
"value": v.value,
"address": v.address
}
for v in temperature
]
}
except:
return {
"status": False,
"message": "failed get all temperature value"
}
@app.route('/api/temperature_value/latest', methods=['GET'])
def get_all_latest_temperature_value():
try:
temperature = temperature_value.query.all()
if temperature: # データ存在チェック
return {
"status": True,
"message": "successfully get latest temperature value",
"data": {
"id": temperature[-1].id,
"timestamp": temperature[-1].timestamp,
"value": temperature[-1].value,
"address": temperature[-1].address
}
}
else:
return {
"status": False,
"message": "no data available"
}
except:
return {
"status": False,
"message": "failed get latest temperature value"
}
@app.route('/api/temperature_value/count', methods=['GET'])
def get_all_temperature_value_count():
temperature = temperature_value.query.all()
return {
"status": True,
"message": "successfully get all temperature value count",
"num_results": f"{len(temperature)}"
}
@app.route('/api/temperature_value/<int:address>', methods=['GET'])
def get_temperature_value(address):
try:
temperature = temperature_value.query.filter_by(address=address).all()
return {
"status": True,
"message": "successfully get temperature value",
"num_results": f"{len(temperature)}",
"data": [
{
"id": v.id,
"timestamp": v.timestamp,
"value": v.value,
"address": v.address
}
for v in temperature
]
}
except:
return {
"status": False,
"message": "failed get temperature value"
}
@app.route('/api/temperature_value/<int:address>/latest', methods=['GET'])
def get_latest_temperature_value(address):
try:
temperature = temperature_value.query.filter_by(address=address).all()
if temperature: # データ存在チェック
return {
"status": True,
"message": "successfully get latest temperature value",
"data": {
"id": temperature[-1].id,
"timestamp": temperature[-1].timestamp,
"value": temperature[-1].value,
"address": temperature[-1].address
}
}
else:
return {
"status": False,
"message": "no data available"
}
except:
return {
"status": False,
"message": "failed get latest temperature value"
}
@app.route('/api/temperature_value/<int:address>/count', methods=['GET'])
def get_temperature_value_count(address):
temperature = temperature_value.query.filter_by(address=address).all()
return {
"status": True,
"message": "successfully get temperature value count",
"num_results": f"{len(temperature)}"
}
@app.route('/api/temperature_value/<int:address>', methods=['POST'])
def set_temperature_value(address):
value = request.json["value"]
if not address_exists(address):
return {
"status": False,
"message": "address does not exist",
"address": address
}
try:
cre = temperature_value(timestamp = get_timestamp(), value = value, address = address)
db.session.add(cre)
db.session.commit()
return {
"status": True,
"message": "successfully set temperature value",
"address": address,
"value": value
}
except:
return {
"status": False,
"message": "failed set temperature value"
}
@app.route('/api/humidity_value', methods=['GET'])
def get_all_humidity_value():
try:
humidity = humidity_value.query.all()
return {
"status": True,
"message": "successfully get all humidity value",
"num_results": f"{len(humidity)}",
"data": [
{
"id": v.id,
"timestamp": v.timestamp,
"value": v.value,
"address": v.address
}
for v in humidity
]
}
except:
return {
"status": False,
"message": "failed get all humidity value"
}
@app.route('/api/humidity_value/latest', methods=['GET'])
def get_all_latest_humidity_value():
try:
humidity = humidity_value.query.all()
if humidity: # データ存在チェック
return {
"status": True,
"message": "successfully get latest humidity value",
"data": {
"id": humidity[-1].id,
"timestamp": humidity[-1].timestamp,
"value": humidity[-1].value,
"address": humidity[-1].address
}
}
else:
return {
"status": False,
"message": "no data available"
}
except:
return {
"status": False,
"message": "failed get latest humidity value"
}