client.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import pandas as pd
  2. # from pycaret.classification import *
  3. import librosa as fe
  4. import awswrangler as wr
  5. from datetime import datetime
  6. import requests
  7. def preproc(df, id, tb_name):
  8. df['timestamp'] = pd.to_datetime(df.timestamp)
  9. df.set_index('timestamp', inplace=True)
  10. df_proc = df[df.device_id == id]
  11. if tb_name=='temperature_humidity':
  12. df_proc = df_proc.resample(rule='T').median()
  13. return df_proc[['hum', 'temp']].fillna(method='ffill').fillna(method='bfill')
  14. elif tb_name=='illumination':
  15. df_proc.rename(columns={'evt':'illum'}, inplace=True)
  16. df_proc = df_proc.resample(rule='T').median()
  17. return df_proc[['illum']].fillna(method='ffill').fillna(method='bfill')
  18. elif tb_name=='motion_door':
  19. if id == '00158d0002d545b4':
  20. df_proc.rename(columns={'evt':'motion'}, inplace=True)
  21. df_proc = df_proc.resample(rule='T').count()
  22. return df_proc['motion'].fillna(value=0)
  23. elif id == '00158d0005bb96f3':
  24. df_proc.rename(columns={'evt':'door'}, inplace=True)
  25. df_proc = df_proc.resample(rule='T').count()
  26. return df_proc['door'].fillna(value=0)
  27. else:
  28. return None
  29. else :
  30. return None
  31. def get_db(tb_name, dev_id, db_name="ambt_b2c", date=None):
  32. query = f"SELECT * FROM {tb_name} where device_id = '{dev_id}' and timestamp <= '{date}' order by timestamp desc limit 10"
  33. df = wr.athena.read_sql_query(sql=query, database=db_name)
  34. df = preproc(df, dev_id, tb_name)
  35. print(df.tail(1))
  36. return df
  37. def get_date_colum(df):
  38. tmp = df.iloc[-1:,:].reset_index().T.to_dict()[0]
  39. tmp['dayofweek'] = tmp['timestamp'].dayofweek
  40. tmp['month'] = tmp['timestamp'].month
  41. tmp['day'] = tmp['timestamp'].day
  42. tmp['hour'] = tmp['timestamp'].hour
  43. tmp['minute'] = tmp['timestamp'].minute
  44. return tmp
  45. def get_feature(db_name="ambt_b2c", date=None):
  46. '''
  47. input : table name 과 device id 는 수동으로 조절 해야 함
  48. outpu : 지정한 date의 featrue 를 {컬럼명:컬럼값} 으로 변환하여 리턴
  49. '''
  50. if date == None :
  51. date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  52. print(f'forced setting : time stamp = {date}' )
  53. df = get_db('temperature_humidity', '00158d00028d93d8', db_name, date) #3회의실온습도센서
  54. df = df.join( get_db('illumination', '00158d0006c9d5ed', db_name, date) ) #3회의실조도센서
  55. df = df.join( get_db('motion_door', '00158d0002d545b4', db_name, date) ) #3회의실모션센서
  56. df = df.join( get_db('motion_door', '00158d0005bb96f3', db_name, date) ) #3회의실문열림센서
  57. df = df.resample(rule='T').max().fillna(value=0)
  58. return get_date_colum(df)
  59. def test(date=None):
  60. if date==None: return 0
  61. tmp = get_feature(date=date)
  62. print(tmp)
  63. tmp.pop('timestamp')
  64. feature = [f'{x}={tmp[x]}' for x in tmp]
  65. URL = 'http://localhost:8000/predict?{}'.format('&'.join(feature))
  66. print(URL)
  67. response = requests.get(URL)
  68. return response
  69. def my_read(date=None, fn='data/data_all.csv'):
  70. df = pd.read_csv(fn)
  71. return df
  72. def test_csv(df, date=None):
  73. if date==None: return 0
  74. label = df['label']
  75. tmp = df[df['timestamp'] == date.strftime('%Y-%m-%d %H:%M:%S')]
  76. tmp['timestamp'] = pd.to_datetime(tmp['timestamp'])
  77. tmp = get_date_colum(tmp)
  78. tmp.pop('timestamp')
  79. tmp.pop('label')
  80. tmp.pop('index')
  81. feature = [f'{x}={tmp[x]}' for x in tmp]
  82. # print(feature)
  83. URL = 'http://localhost:8000/predict?{}'.format('&'.join(feature))
  84. print(URL)
  85. response = requests.get(URL)
  86. return response
  87. from pandas.tseries.offsets import Day, Hour, Minute, Second
  88. if __name__ == '__main__':
  89. '''
  90. 예제 client code
  91. '''
  92. # tmp = get_feature(date='2021-09-09 11:05:00')
  93. # tmp = get_feature(date='2021-08-30 08:06:00') # 08:07~08:15
  94. # tmp = get_feature(date='2021-08-30 09:01:00') # 08:07~08:15
  95. # 10:10~10:13 = true
  96. sq = pd.date_range('2021-08-30 08:10:00', periods=10, freq=Minute(1))
  97. print(sq)
  98. r = []
  99. df = pd.read_csv('data/data_all.csv')
  100. for x in sq:
  101. # ret = test(x)
  102. ret = test_csv(df, x)
  103. r.append( [x, ret] )
  104. print(x, ret.text)
  105. for x, ret in r:
  106. print(x, ret.text)
  107. # todo : requests 커튼, 전등