diff --git a/.gitignore b/.gitignore index 617023d..2fc61e8 100644 --- a/.gitignore +++ b/.gitignore @@ -14,7 +14,4 @@ DbFuncs/__pycache__/ model/__pycache__/ # ignore __pycache__ directory -__pycache__/ - -# ignore test directory -test/ \ No newline at end of file +__pycache__/ \ No newline at end of file diff --git a/DbFuncs/db.py b/DbFuncs/db.py index f7edba9..b5e7f57 100644 --- a/DbFuncs/db.py +++ b/DbFuncs/db.py @@ -1,6 +1,7 @@ import sqlite3 import datetime from config import utils +import os # import os # import sys @@ -22,8 +23,8 @@ def insert_ads(ad): insert_query = """insert into ads (type, content) values (?, ?)""" - blodData = utils.convert_to_blod_data(ad.content) - data = (ad.type, (blodData)) + # blodData = utils.convert_to_blod_data(ad.content) + data = (ad.type, ad.content) cursor.execute(insert_query, data) @@ -47,10 +48,11 @@ def insert_machine(machine): conn = sqlite3.connect(utils.dbPath) cursor = conn.cursor() - insert_query = """insert into machines (name, unit, value) - values (?, ?, ?)""" - - data = (machine.name, machine.unit, machine.value) + insert_query = """insert into machines (name, unit, value, thumbnail) + values (?, ?, ?, ?)""" + + # blodData = utils.convert_to_blod_data(machine.thumbnail) + data = (machine.name, machine.unit, machine.value, machine.thumbnail) cursor.execute(insert_query, data) @@ -76,13 +78,14 @@ def insert_product(product): conn = sqlite3.connect(utils.dbPath) cursor = conn.cursor() - insert_query = """insert into products (itemno, name, thumbnail, nicotine, batterypack, tankvolumn, price, currency, caution) - values (?, ?, ?, ?, ?, ?, ?, ?, ?)""" - imageBlob = utils.convert_to_blod_data(product.thumbnail) - data = (product.itemno, product.name, (imageBlob), product.nicotine, product.batterypack, product.tankvolumn, product.price, product.currency, product.caution) + insert_query = """insert into products (itemno, name, thumbnail, nicotine, batterypack, tankvolumn, price, currency, caution, stock) + values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""" + # imageBlob = utils.convert_to_blod_data(product.thumbnail) + data = (product.itemno, product.name, product.thumbnail, product.nicotine, product.batterypack, + product.tankvolumn, product.price, product.currency, product.caution, product.stock) cursor.execute(insert_query, data) - print("inserted successfully") + print("inserted product successfully") conn.commit() cursor.close() @@ -156,7 +159,9 @@ def get_product(id): cursor.execute(select_query, (id,)) record = cursor.fetchone() - product = convert_to_product(record) + product = None + if record: + product = convert_to_product(record) conn.commit() cursor.close() @@ -181,8 +186,10 @@ def get_ad(): cursor.execute(select_query) record = cursor.fetchone() - ad = convert_to_ad(record) - print(f'len(ad):{ad.type}') + ad = None + if record: + ad = convert_to_ad(record) + conn.commit() cursor.close() @@ -206,7 +213,9 @@ def get_ad_row(id): cursor.execute(select_query, (id,)) record = cursor.fetchone() - ad = convert_to_ad(record) + ad = None + if record: + ad = convert_to_ad(record) conn.commit() cursor.close() @@ -223,29 +232,31 @@ def get_ad_row(id): # get Ad -def get_machine(id): +def get_machines(): try: conn = sqlite3.connect(utils.dbPath) cursor = conn.cursor() - select_query = '''select * from machines where id = ?''' - cursor.execute(select_query, (id,)) - record = cursor.fetchone() + select_query = '''select * from machines''' + cursor.execute(select_query) + record = cursor.fetchall() - machine = convert_to_machine(record) + machines = [] + for item in record: + machine = convert_to_machine(item) + machines.append(machine) conn.commit() cursor.close() except sqlite3.Error as error: print('get_machine_fail', error) - machine = None finally: if conn: conn.close() print('connection is closed') - return machine + return machines def get_product_count(): try: @@ -307,9 +318,28 @@ def delete_ads(): conn.close() print('connection is closed') +# delete Ads +def delete_products(): + try: + conn = sqlite3.connect(utils.dbPath) + cursor = conn.cursor() + + delete_query = '''delete from products''' + cursor.execute(delete_query) + + conn.commit() + cursor.close() + + except sqlite3.Error as error: + print('delete_products_fail', error) + finally: + if conn: + conn.close() + print('connection is closed') + def convert_to_product(record): product = Product(record[0], record[1], record[2], record[3], record[4], - record[5], record[6], record[7], record[8], record[9]) + record[5], record[6], record[7], record[8], record[9], record[10]) return product @@ -318,6 +348,5 @@ def convert_to_ad(record): return ad def convert_to_machine(record): - machine = Machine(record[0], record[1], record[2], record[3]) + machine = Machine(record[0], record[1], record[2], record[3], record[4]) return machine - diff --git a/DbFuncs/db_create.py b/DbFuncs/db_create.py index eb7946e..e50b5b5 100644 --- a/DbFuncs/db_create.py +++ b/DbFuncs/db_create.py @@ -28,7 +28,8 @@ def create_product_table_if_not_exists(): tankvolumn TEXT NOT NULL, price REAL NOT NULL, currency TEXT NOT NULL, - caution TEXT NOT NULL + caution TEXT NOT NULL, + stock INTEGER NOT NULL )''' # Create a users table @@ -79,7 +80,8 @@ def create_machine_table_if_not_exists(): id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, unit TEXT NOT NULL, - value TEXT NOT NULL + value TEXT NOT NULL, + thumbnail BLOB NOT NULL )''' # Create a users table diff --git a/DbFuncs/db_get_list_test.py b/DbFuncs/db_get_list_test.py index 7b6cb5f..b527165 100644 --- a/DbFuncs/db_get_list_test.py +++ b/DbFuncs/db_get_list_test.py @@ -74,8 +74,8 @@ def insert_ads(ad): insert_query = """insert into ads (type, content) values (?, ?)""" - blodData = convert_to_blod_data(ad.content) + print(blodData) data = (ad.type, (blodData)) cursor.execute(insert_query, data) @@ -94,8 +94,3 @@ def insert_ads(ad): conn.close() print('connection is closed') return False - -# update_product(1, '../img/owl.jpg', 500) -ad = Ad(1, 'PPT', '../222.pptx') -insert_ads(ad) -# db.get_ad_row(31) \ No newline at end of file diff --git a/DbFuncs/sql.db b/DbFuncs/sql.db index 883dcc1..2603e4a 100644 Binary files a/DbFuncs/sql.db and b/DbFuncs/sql.db differ diff --git a/ad.py b/ad.py index 507ba9a..600bb3f 100644 --- a/ad.py +++ b/ad.py @@ -8,6 +8,7 @@ import base64 from kivy.uix.boxlayout import BoxLayout from kivy.uix.videoplayer import VideoPlayer +from kivy.uix.video import Video from kivy.uix.image import Image from pptx import Presentation from config import utils @@ -25,14 +26,14 @@ def __init__(self, **kwargs): Clock.schedule_once(self.retrieve_layout) def retrieve_layout(self, dt): - ad = db.get_ad_row(35) + ad = db.get_ad() if ad: if ad.type == 'MP4': - temp_file = './img/ad.mp4' + path = os.path.dirname(__file__) + temp_file = path + '/img/ad.mp4' utils.write_to_file(ad.content, temp_file) videoPlayerLayout = VideoPlayerLayout(temp_file) - print(f'self.manager{self.manager}') videoPlayerLayout.manager = self.manager videoPlayerLayout.bind(on_touch_up=videoPlayerLayout.on_video_touch_up) @@ -96,7 +97,7 @@ def __init__(self, temp_file, **kwargs): self.temp_file = temp_file # Create a VideoPlayer widget - self.player = VideoPlayer(source=temp_file, state='play', + self.player = Video(source=temp_file, state='play', options={'eos': 'loop'}) # Add the VideoPlayer widget to the BoxLayout diff --git a/api.py b/api.py index e69de29..7653e47 100644 --- a/api.py +++ b/api.py @@ -0,0 +1,125 @@ +# from websocket import create_connection +import requests +import time +import json +import asyncio +import websockets +import certifi +from model.Machine import Machine +from model.Ad import Ad +from model.Product import Product +from DbFuncs import db +import base64 +import ssl +import certifi + +hostName = 'https://212.224.86.112:8443' + +# def create_connect(): +# ws = create_connection("ws://echo.websocket.events/") +# print(ws.recv()) +# print("Sending 'Hello, World'...") +# ws.send("Hello, World") +# print("Sent") +# print("Receiving...") +# result = ws.recv() +# print("Received '%s'" % result) +# ws.close() + +async def connect_to_server(): + ssl_context = ssl.create_default_context() + ssl_context.load_verify_locations(certifi.where()) + + async with websockets.connect('wss://212.224.86.112:8443', ssl = ssl_context) as websocket: + sendData = {'action': 'MachineConnect'} + await websocket.send(json.dumps(sendData)) + + # Receive data + response = await websocket.recv() + responseData = json.loads(response) + + print(f"Received: {response}") + print(f"Received data: {responseData}") + + if responseData['status']: + while True: + statusData = { + 'action': "MachineSendStatus", + 'payload': { + 'serialno': "123-456-678", + 'temparature': "XXX", + 'token': responseData['token'], + } + } + await websocket.send(json.dumps(statusData)) + statusResponse = await websocket.recv() + statusResponseData = json.loads(statusResponse) + +# asyncio.get_event_loop().run_until_complete(connect_to_server()) + + +async def connect_and_send(): + async with websockets.connect('wss://example.com/ws') as websocket: + await websocket.send("Hello, server!") + +# asyncio.get_event_loop().run_until_complete(connect_and_send()) + +def send_get_machine_info(): + # delete machine table + db.delete_machines() + + # Send an HTTP GET request to a URL of your choice + url = "/api/machine/get_machine_info" + response = requests.post(hostName + url, verify=False) + + # Check the response status code + if response.status_code == 200: + responseData = response.json() # {status : , message : , details : [{},]} + machineData = responseData['details'] + for item in machineData: + params = Machine(0, item['name'], item['unit'], item['value'], base64.b64decode(item['thumbnail'])) + db.insert_machine(params) + else: + print(f"Request failed with status code: {response.status_code}") + +def send_get_ads_info(): + url = "/api/machine/get_ads_info" + db.delete_ads() + # Send an HTTP GET request to a URL of your choice + response = requests.post(hostName + url, verify=False) + + # Check the response status code + if response.status_code == 200: + responseData = response.json() # {status : , message : , details : [{},]} + adData = responseData['details'] + # params = Ad(0, adData['type'], bytes(adData['content'], 'utf-8')) + params = Ad(0, adData['type'], base64.b64decode(adData['content'])) + db.insert_ads(params) + else: + print(f"Request failed with status code: {response.status_code}") + +def send_get_products_info(): + url = "/api/machine/get_product_info" + # delete machine table + db.delete_products() + + # Send an HTTP GET request to a URL of your choice + response = requests.post(hostName + url, verify=False) + + # Check the response status code + if response.status_code == 200: + productData = response.json() # {status : , message : , details : [{},]} + for item in productData: + params = Product(0, item['itemno'], item['name'], base64.b64decode(item['thumbnail']), item['nicotine'], item['batterypack'], + item['tankvolumn'], item['price'], item['currency'], item['caution'], item['stock']) + db.insert_product(params) + else: + print(f"Request failed with status code: {response.status_code}") + +def send_sell_product(): + url = "api/machine/sell_product" + + +# send_get_machine_info() +# send_get_products_info() +# send_get_ads_info() \ No newline at end of file diff --git a/commands b/commands new file mode 100644 index 0000000..6af2920 --- /dev/null +++ b/commands @@ -0,0 +1,35 @@ +sudo apt update +sudo apt upgrade +sudo add-apt-repository ppa:deadsnakes/p +pa +sudo apt install python3.9 + +python3.9 -m venv venv39 + +######################### this path is mine, replace your path ############### +/home/crazy/Downloads/Work/Kivy/venv39/bin/python3.9 -Im ensurepip --upgrade --default-pip + +sudo apt install python3.9-venv + +python3.9 -m venv myenv + +source myenv/bin/activate + +pip3.9 install kivy + +pip3.9 install Pillow +pip3.9 install python-pptx + +pip3.9 install websocket +pip3.9 install websockets + +apt-get install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libgstreamer-plugins-bad1.0-dev gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav gstreamer1.0-tools gstreamer1.0-x gstreamer1.0-alsa gstreamer1.0-gl gstreamer1.0-gtk3 gstreamer1.0-qt5 gstreamer1.0-pulseaudio + +pip3.9 install ffpyplayer + + + + +##########run command +#######go to your workspace +python3.9 main.py \ No newline at end of file diff --git a/config/utils.py b/config/utils.py index f44abe5..f663df9 100644 --- a/config/utils.py +++ b/config/utils.py @@ -1,3 +1,4 @@ +import os dbPath = './DbFuncs/sql.db' screenX = 600 @@ -20,6 +21,6 @@ def write_to_file(data, filename): print("Stored blob data into: ", filename, "\n") - -# path = 'D:\\tmp\\' + product[1] + '.png' +# path = os.path.dirname( __file__ ) + '/test.mp4' +# convert_to_blod_data(path) # utils.write_to_file(product[2], path) \ No newline at end of file diff --git a/convert.pptx b/convert.pptx deleted file mode 100644 index 0d28b1c..0000000 Binary files a/convert.pptx and /dev/null differ diff --git a/item.py b/item.py index 8890a95..49849ea 100644 --- a/item.py +++ b/item.py @@ -44,7 +44,7 @@ def draw_page(self, id): # self.ids.input_money_button.width = inputMoneyLabel.width # self.ids.input_money_button.text = inputMoneyLabel.text - self.draw_bigo() + self.draw_bigo(product.caution) self.ids.back_img.bind(on_touch_down = self.on_back_press) @@ -54,7 +54,7 @@ def draw_title(self, name): titleLabel.color = (1, 0, 0, 1) titleLabel.font_size = 30 titleLabel.size_hint_x = None - titleLabel.width = 200 + titleLabel.width = 300 # titleLabel.padding = [20, 5, 5, 5] titleLayout.add_widget(titleLabel) @@ -65,11 +65,11 @@ def draw_product_info(self, product): image.texture = img.texture self.ids.item_image_layout.add_widget(image) - self.draw_label('Abc1234') + self.draw_label(product.itemno) self.draw_label('600') - self.draw_label('20mg') - self.draw_label('40asda0mAh') - self.draw_label('2ml') + self.draw_label(product.nicotine) + self.draw_label(product.batterypack) + self.draw_label(product.tankvolumn) priceStr = str(product.price) + ' EUR' priceLabel = Label( @@ -96,10 +96,23 @@ def draw_label(self, value): self.ids.info_layout.add_widget(boxLayout) # display bigo (labels, img on the top-right ) - def draw_bigo(self): + def draw_bigo(self, cautionText): bigoLayout = self.ids.bigo_layout + + for i in range (1): + caution = Label(text=cautionText) + caution.halign='left' + caution.valign = 'middle' + caution.color = (.1,.1,.1,.5) + caution.size_hint_x = None + caution.size = (500, caution.height /2 ) + print(f'height:{caution.height}') + caution.text_size = (caution.width, None) + + bigoLayout.add_widget(caution) + # Create an Image widget - img = Image(source='./img/bigo.png') # Replace 'your_image.png' with your image file path + img = Image(source='./img/bigo.png') # Calculate the position for the image (top-right corner) image_x = utils.screenX - img.width - 20 image_y = utils.screenY / 3 - img.height / 2 -100 @@ -107,6 +120,7 @@ def draw_bigo(self): with bigoLayout.canvas: Rectangle(pos=(image_x, image_y), size=img.size, texture=img.texture) + def on_back_press(self, instance, touch): if instance.collide_point(*touch.pos): self.manager.current = 'List' diff --git a/kv/list.kv b/kv/list.kv index 83ea42b..fd44bff 100644 --- a/kv/list.kv +++ b/kv/list.kv @@ -72,12 +72,6 @@ WindowManager: pos: 0, 0 scroll_type: ["bars","content"] bar_width: 5 - canvas.before: - Color: - rgb: 192 ,100 ,0 - Rectangle: - pos: self.pos - size: self.size GridLayout: id:image_layout cols:2 @@ -149,13 +143,13 @@ WindowManager: rgb: (192,0,0) Line: width: 1 - rectangle: self.x , self.y - 15 , self.width-10, self.height-10 + rectangle: self.x , self.y, self.width-10, self.height-30 GridLayout: rows: 5 spacing: 5 Label: - text: 'Art.sdasdads:' + text: 'Art.Nr:' color: (0,0,0,1) halign: 'left' valign: 'middle' @@ -260,28 +254,7 @@ WindowManager: Line: width: 1 rectangle: self.x + 5 , self.y + 20 , self.width - 10, self.height - 50 - Label: - text: 'Hello worldHello worldHello worldworldHello world' - color: (0.1,0.1,0.1,.5) - halign: 'left' - valign: 'middle' - size_hint_x: None - size: (500, self.height) - text_size: self.width, None - Label: - text: 'Hello world' - color: (0.1,0.1,0.1,.5) - halign: 'left' - valign: 'middle' - size_hint_x: None - text_size: self.width, None - Label: - text: 'Hello world' - color: (0.1,0.1,0.1,.5) - halign: 'left' - valign: 'middle' - size_hint_x: None - text_size: self.width, None + : : diff --git a/list.py b/list.py index 905d415..9386cda 100644 --- a/list.py +++ b/list.py @@ -107,9 +107,7 @@ def on_up_img_click(self, instance, touch): if imageLayout.children: firstChild = imageLayout.children[-1] imageScrollView.scroll_to(firstChild) - - - + def on_down_img_click(self, instance, touch): if instance.collide_point(*touch.pos): # image_scroll_view.scroll_y = 1 @@ -128,22 +126,29 @@ def on_down_img_click(self, instance, touch): def retrieve_category_layout(self, dt): categoryLayout = self.ids.category_layout # Access the image_layout widget - - for i in range(8): - image = self.on_draw_category_item() + machines = db.get_machines() + # machines = db.get_products() + for machine in machines: + image = self.on_draw_category_item(machine) categoryLayout.add_widget(image) categoryLayout.width += image.width - def on_draw_category_item(self): + def on_draw_category_item(self, machine): boxlayout = BoxLayout(orientation='vertical') - image = CategoryItem(source='./img/category.png') - specLabel = Label(text='bis zu 600') + + image = CategoryItem() + image_stream = io.BytesIO(machine.thumbnail) + img = CoreImage(image_stream, ext='png') + image.texture = img.texture + specLabel = Label(text= machine.value) specLabel.color = (0,0,0,1) specLabel.font_size = 15 - nameLabel = Label(text='Zuge') + + nameLabel = Label(text= (machine.name)) nameLabel.color = (0,0,0,1) nameLabel.font_size = 15 nameLabel.background_color = (1,1,1,1) + boxlayout.add_widget(image) boxlayout.add_widget(specLabel) boxlayout.add_widget(nameLabel) diff --git a/main.py b/main.py index f39d827..fcd6669 100644 --- a/main.py +++ b/main.py @@ -27,56 +27,45 @@ from item import ItemScreen from list import ListScreen from ad import AdScreen +import os +import api dbFlag = False class WindowManager(ScreenManager): - def __init__(self, **kwargs): - super().__init__(**kwargs) - with self.canvas.before: - Color(1, 1, 1, 1) # a white color - self.rect = Rectangle(pos=self.pos, size=self.size) - self.bind(pos=self.update_rect, size=self.update_rect) + pass + # def __init__(self, **kwargs): + # super().__init__(**kwargs) + # with self.canvas.before: + # Color(1, 1, 1, 1) # a white color + # self.rect = Rectangle(pos=self.pos, size=self.size) + # self.bind(pos=self.update_rect, size=self.update_rect) - def update_rect(self, *args): - self.rect.pos = self.pos - self.rect.size = self.size + # def update_rect(self, *args): + # self.rect.pos = self.pos + # self.rect.size = self.size kv = Builder.load_file('./kv/list.kv') class MainApp(App): # Main Application def build(self): + db_create.create_tables() + self.connect_to_server() + Window.size = (utils.screenX, utils.screenY) - sm = WindowManager() sm.add_widget(AdScreen(name='Ad')) sm.add_widget(ListScreen(name='List')) sm.add_widget(ItemScreen(name='Item')) - # create db and insert data - if dbFlag: - self.insertProduct() - return sm # create db and insert data - def insertProduct(self): - - # db.delete_ads() - db_create.create_tables() - - - names = ['1-1.png', '1-2.png', '2-1.png', '2-2.png', '1-1.png', '1-2.png', '2-1.png'] - path = './img' - for name in names: - image = path + "/" + name - data = Product(1, '1234', 'Prodcut1', image, '20mg', '300mAh', 'XXX', 10, 'EUR', 'This is .....') - # db.insert_product(data) - - ad = Ad(1, 'PPT', './pptx.pptx') - # ad = Ad(1, 'MP4', './test.mp4') - db.insert_ads(ad) + def connect_to_server(self): + api.send_get_ads_info() + api.send_get_machine_info() + api.send_get_products_info() if __name__ == '__main__': diff --git a/model/Machine.py b/model/Machine.py index 5dd64ef..72eacd4 100644 --- a/model/Machine.py +++ b/model/Machine.py @@ -1,6 +1,7 @@ class Machine: - def __init__(self, id, name, unit, value): + def __init__(self, id, name, unit, value, thumbnail): self.id = id self.name = name self.unit = unit - self.value = value \ No newline at end of file + self.value = value + self.thumbnail = thumbnail \ No newline at end of file diff --git a/model/Product.py b/model/Product.py index 97fe566..be67e6d 100644 --- a/model/Product.py +++ b/model/Product.py @@ -1,7 +1,7 @@ class Product: - def __init__(self, id, itemno, name, thumbnail, nicotine, batterypack, tankvolumn, price, currency, caution): + def __init__(self, id, itemno, name, thumbnail, nicotine, batterypack, tankvolumn, price, currency, caution, stock): self.id = id - self.itemno = name + self.itemno = itemno self.name = name self.thumbnail = thumbnail self.nicotine = nicotine @@ -9,4 +9,5 @@ def __init__(self, id, itemno, name, thumbnail, nicotine, batterypack, tankvolum self.tankvolumn = tankvolumn self.price = price self.currency = currency - self.caution = caution \ No newline at end of file + self.caution = caution + self.stock = stock \ No newline at end of file