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

Feature/passive navi base #8

Merged
merged 8 commits into from Jan 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
.DS_Store
config.ini
LiveContents/static/css/flickity
1 change: 0 additions & 1 deletion LiveContents/static/css/flickity
Submodule flickity deleted from 7491e0
87 changes: 87 additions & 0 deletions PassiveNavigation/navi/app.py
@@ -0,0 +1,87 @@
import json
# import wiringpi as wp
from flask import Flask, request, render_template

from multiprocessing.managers import BaseManager
from time import sleep

#適当 あとで決める
LED_PIN = 21
app = Flask(__name__)

# # メソッドを定義する
# BaseManager.register('get_queue',callable=lambda:queue)
#
# # サーバの設定
# m = BaseManager(address=('192.168.43.70',8001),authkey=b'a')
#
# # サーバに接続
# m.connect()
#
# # サーバのQueueを取得する
# queue = m.get_queue()

class Navi:
def __init__(self, json_data):
self.needs_data = json_data['needs']

def is_needed(self, posted_data):
result = ''

for n in self.needs_data:
matched_type = self._check_type(n['type'], posted_data['type'])
matched_keywords = self._check_keywords(n['keywords'], posted_data['keywords'].split(','))
if matched_type and matched_keywords:
result = 'マッチしました!\nタイプ:' + matched_type + '\n'
result = result + 'キーワード:' + matched_keywords + '\n'
result = result + '住所:' + posted_data['address']

return result

# 存在するならその店舗タイプを、そうでなければ空文字を返す
def _check_type(self, navi_type, store_type):
if navi_type == store_type:
return navi_type
else:
return ''

# 存在するならマッチしたキーワードを' 'で区切った文字列を、そうでなければ空文字を返す
def _check_keywords(self, navi_keywords, store_keywords):
result = ''
for k in store_keywords:
if k in navi_keywords:
result = result + k + ' '

return result


@app.route('/', methods=['GET', 'POST'])
def recieve_store_data():
if request.method == 'POST':
posted_data = request.json
send_message = navi.is_needed(posted_data)
if send_message:
print(send_message)
open('./temp.txt', 'w').write('----\n' + send_message + '\n----\n')
return 'hello'

if request.method == 'GET':
return render_template('index.html')

if __name__ == '__main__':
# wp.wiringPiSetupGpio()
# # LED_PINを出力用にセット
# wp.pinMode(LED_PIN, 1)

try:
# ローカルJSONファイルの読み込み
with open('navi_data.json', 'r') as f:
data = json.load(f)
print(data)
except json.JSONDecodeError as e:
print('JSONDecodeError: ', e)

#init
navi = Navi(data);
open('./temp.txt', 'w').write('')
app.run(host="0.0.0.0", debug=False, threaded=True)
28 changes: 28 additions & 0 deletions PassiveNavigation/navi/display.py
@@ -0,0 +1,28 @@
import tkinter as tk

class Display():
def __init__(self):
self.before_text = ''

self.root = tk.Tk()
self.root.title('PassiveNavigation')
self.root.geometry('480x320+0+0')

self.text_widget = tk.Text(self.root)
self.text_widget.grid(column=0, row=0, sticky=(tk.N, tk.S, tk.E, tk.W))

self.root.columnconfigure(0, weight=1)
self.root.rowconfigure(0, weight=1)

self.update_clock()
self.root.mainloop()

def update_clock(self):
tmp_text = open('./temp.txt', 'r').read()
if self.before_text != tmp_text:
self.text_widget.insert('end', tmp_text);
self.before_text = tmp_text
self.root.after(1000, self.update_clock)

if __name__ == '__main__':
display = Display()
12 changes: 12 additions & 0 deletions PassiveNavigation/navi/navi_data.json
@@ -0,0 +1,12 @@
{
"needs" :[
{
"type" : "飲食店",
"keywords" : "ステーキ,団子,寿司"
},
{
"type" : "お土産",
"keywords" : "置物,お菓子"
}
]
}
6 changes: 6 additions & 0 deletions PassiveNavigation/navi/temp.txt
@@ -0,0 +1,6 @@
----
マッチしました!
タイプ:飲食店
キーワード:団子 寿司
住所:川崎市Hoge区Piyo町1町目2番地3号
----
35 changes: 35 additions & 0 deletions PassiveNavigation/store/app.py
@@ -0,0 +1,35 @@
import requests
import json
import os
import time


NETWORK_BROADCAST_ADDRESS = os.environ.get('NAVI_NETWORK')

class Store:
def __init__(self, store_info, network=NETWORK_BROADCAST_ADDRESS):
self.store_info = store_info
self.network = network

def broadcast_store_data(self):
r = requests.post('http://' + self.network, \
json.dumps(self.store_info), \
headers={'Content-Type':'application/json'})
print(r)

if __name__ == '__main__':
try:
# ローカルJSONファイルの読み込み
with open('store_data.json', 'r') as f:
data = json.load(f)
print(data)
except json.JSONDecodeError as e:
print('JSONDecodeError: ', e)

store = Store(data);

while 1:
store.broadcast_store_data()
time.sleep(1)

# app.run(host="0.0.0.0", debug=False, threaded=True)
6 changes: 6 additions & 0 deletions PassiveNavigation/store/store_data.json
@@ -0,0 +1,6 @@
{
"name" : "団子商店",
"type" : "飲食店",
"keywords" : "団子,飲食店,甘味,どら焼き,大福,お茶,軽食,寿司",
"address" : "川崎市Hoge区Piyo町1町目2番地3号"
}