Skip to content

Commit

Permalink
Merge pull request #5 from Gifford47/development
Browse files Browse the repository at this point in the history
Merge REST API enhancements into Master
  • Loading branch information
Gifford47 authored Apr 8, 2023
2 parents f2a92b7 + 4f15bcf commit 8bc3a80
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 110 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ Go to [Openhab-DEMO](https://demo.openhab.org/settings/items/add-from-textual-de

### Convert to .exe

\####PyQT5:

####PyQT5:
* python -m PyQt5.uic.pyuic tasmohabUI.ui -o tasmohabUI.py # for UI components
* pyrcc5 resource.qrc -o resource_rc.py # for resource items f.e. images (ressource.qrc) ####PyQT6:
* pyrcc5 resource.qrc -o resource_rc.py # for resource items f.e. images (ressource.qrc)
####PyQT6:
* pyuic6 -x tasmohabUI.ui -o tasmohabUI.py
* pyuic6 -x dev_config.ui -o dev_config.py ####PyQt6 Designer:
* pyuic6 -x dev_config.ui -o dev_config.py
####PyQt6 Designer:
* pyqt6-tools designer

#### PyInstaller:
Expand Down
29 changes: 21 additions & 8 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

def handle_thing(ip: str, action: str, user: str, passw: str, body=None):
try:
endpoint = thing[action]['link']
if action == 'update':
endpoint = thing[action]['link']+body['UID']
else:
endpoint = thing[action]['link']
except Exception as e:
print(e)
return 'handle_thing:wrong action used.'
Expand All @@ -14,16 +17,21 @@ def handle_thing(ip: str, action: str, user: str, passw: str, body=None):
print(r)
if hasattr(r, 'status_code'):
if str(r.status_code) in thing[action]['response']:
return 'HTTP Response:' + str(r.status_code) + ' ' + thing[action]['response'][str(r.status_code)]
#return 'HTTP Response:' + str(r.status_code) + ' ' + thing[action]['response'][str(r.status_code)]
return r.status_code, thing[action]['response'][str(r.status_code)]
else:
return 'HTTP Response:' + str(r.status_code) + ' ' + str(r.text)
#return 'HTTP Response:' + str(r.status_code) + ' ' + str(r.text)
return r.status_code, str(r.text)
else:
return r


def handle_item(ip: str, action: str, user: str, passw: str, body=None):
try:
endpoint = item[action]['link']
if action == 'update':
endpoint = item[action]['link']+body['UID']
else:
endpoint = item[action]['link']
except Exception as e:
print(e)
return 'handle_item:wrong action used.'
Expand All @@ -32,9 +40,11 @@ def handle_item(ip: str, action: str, user: str, passw: str, body=None):
print(r)
if hasattr(r, 'status_code'):
if str(r.status_code) in item[action]['response']:
return 'HTTP Response:' + str(r.status_code) + ' ' + item[action]['response'][str(r.status_code)]
#return 'HTTP Response:' + str(r.status_code) + ' ' + item[action]['response'][str(r.status_code)]
return r.status_code, item[action]['response'][str(r.status_code)]
else:
return 'HTTP Response:' + str(r.status_code) + ' ' + str(r.text)
#return 'HTTP Response:' + str(r.status_code) + ' ' + str(r.text)
return r.status_code, str(r.text)
else:
return r

Expand All @@ -51,9 +61,11 @@ def handle_link(ip: str, action: str, user: str, passw: str, body=None):
print(r)
if hasattr(r, 'status_code'):
if str(r.status_code) in links[action]['response']:
return 'HTTP Response:' + str(r.status_code) + ' ' + links[action]['response'][str(r.status_code)]
#return 'HTTP Response:' + str(r.status_code) + ' ' + links[action]['response'][str(r.status_code)]
return r.status_code,links[action]['response'][str(r.status_code)]
else:
return 'HTTP Response:' + str(r.status_code) + ' ' + str(r.text)
#return 'HTTP Response:' + str(r.status_code) + ' ' + str(r.text)
return r.status_code, str(r.text)
else:
return r

Expand Down Expand Up @@ -146,6 +158,7 @@ def http_put(url, u, p, data=None):
'response':{
'200':'OK',
'404':'Thing not found',
'405':'Method not allowed',
'409':'Thing could not be updated as it is not editable'}
}
}
Expand Down
Binary file modified requirements.txt
Binary file not shown.
57 changes: 45 additions & 12 deletions tasmohab.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import subprocess
import traceback
import yaml
import random
from collections import defaultdict
from datetime import datetime

Expand Down Expand Up @@ -531,6 +532,7 @@ def add_ui_widget_peripheral(self, name, row):
self.objects_grid.addWidget(lbl, row, self.tbl_columns['Peripheral Name']) # add the peripheral name/ sensor name

def add_ui_widgets_user(self, layout, row, peripheral, peripheral_no='default'):
peripheral_no = str(peripheral_no) # convert from int to string
try:
line = QLineEdit(openhab.std_items[peripheral_no]['feature']) # feature
except:
Expand All @@ -545,7 +547,7 @@ def add_ui_widgets_user(self, layout, row, peripheral, peripheral_no='default'):
cb = QComboBox() # item type
cb.addItems(openhab.item_types)
try:
cb.setCurrentIndex(openhab.std_items[peripheral_no]['std_type'])
cb.setCurrentIndex(openhab.std_items[(peripheral_no)]['std_type'])
except:
cb.setCurrentIndex(openhab.item_types.index('number')) # if index is not found
layout.addWidget(cb, row, self.tbl_columns['Item Type'])
Expand Down Expand Up @@ -625,7 +627,12 @@ def get_peripheral_name_by_no(self, peripheral_no):
if peripheral_no in openhab.std_items:
return openhab.std_items[peripheral_no]['name']
else:
return None
try: # try to find gpio number in gpio device status
for key in json_dev_status['gpio']:
if peripheral_no in json_dev_status['gpio'][key]:
return json_dev_status['gpio'][key][peripheral_no]
except Exception as e:
return random.randint(0,999) # else return random number

def update_json_to_yaml_config_data(self):
global json_config_data
Expand Down Expand Up @@ -973,18 +980,44 @@ def save_final_obj_via_rest(self):
# create thing
self.txt_output_thing.setText(json.dumps(thing_data, indent=4, sort_keys=True))
response = api.handle_thing(ip, action, body=thing_data, user=api_user, passw=api_pass)
self.append_to_log('Thing:'+response)
log.append('Thing:'+response)
if response[0] == 409: # thing exists
buttonReply = QMessageBox.question(self, 'Thing existing',
'The thing "'+thing_data['UID']+'" is existing. Would you like to update the existing thing?',
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No, QMessageBox.StandardButton.No)
if buttonReply == QMessageBox.StandardButton.Yes:
response = api.handle_thing(ip, 'update', body=thing_data, user=api_user, passw=api_pass)
if buttonReply == QMessageBox.StandardButton.No:
QMessageBox.information(self, 'Abort!', 'No action done!')
self.btn_save_final_via_rest.setEnabled(True)
return
if response[0] != 201: # in case of any failure ...
self.append_to_log('Thing:' + str(response[0]) + ' ' + response[1])
log.append('Thing:' + str(response[0]) + ' ' + response[1])
QMessageBox.information(self, 'REST Feedback', 'Received feedback via REST:\n' + "\n".join(log))
else:
self.append_to_log('Thing:' + str(response[0]) + ' ' + response[1])
log.append('Thing:' + str(response[0]) + ' ' + response[1])

#self.append_to_log('Thing:'+response)
#log.append('Thing:'+response)
# create items
self.txt_output_item.setText(json.dumps(item_data, indent=4, sort_keys=True))
response = api.handle_item(ip, action, body=item_data, user=api_user, passw=api_pass)
self.append_to_log('Item:'+response)
log.append('Item:'+response)
if self.cb_create_items.isChecked():
self.txt_output_item.setText(json.dumps(item_data, indent=4, sort_keys=True))
response = api.handle_item(ip, action, body=item_data, user=api_user, passw=api_pass)
self.append_to_log('Item:' + str(response[0]) + ' ' + response[1])
log.append('Item:' + str(response[0]) + ' ' + response[1])

#self.append_to_log('Item:'+response)
#log.append('Item:'+response)
# create links to items
for link in item_links_as_list:
response = api.handle_link(ip, action, body=link, user=api_user, passw=api_pass)
self.append_to_log('Sending link:'+json.dumps(link)+'\n'+'Response:'+response)
log.append('Sending link:'+response)
if self.cb_link_items.isChecked():
for link in item_links_as_list:
response = api.handle_link(ip, action, body=link, user=api_user, passw=api_pass)
self.append_to_log('Sending link:'+json.dumps(link)+'\n'+'Response:'+str(response[0]) + ' ' + response[1])
log.append('Sending link:' + str(response[0]) + ' ' + response[1])

#self.append_to_log('Sending link:'+json.dumps(link)+'\n'+'Response:'+response)
#log.append('Sending link:'+response)

self.btn_save_final_via_rest.setEnabled(True)
QMessageBox.information(self, 'REST Feedback', 'Received feedback via REST:\n'+"\n".join(log))
Expand Down
85 changes: 49 additions & 36 deletions tasmohabUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,22 @@ def setupUi(self, MainWindow):
self.gridLayout_14.setObjectName("gridLayout_14")
self.gridLayout_16 = QtWidgets.QGridLayout()
self.gridLayout_16.setObjectName("gridLayout_16")
self.btn_save_final_via_rest = QtWidgets.QPushButton(self.tab_3)
self.btn_save_final_via_rest.setEnabled(False)
self.btn_save_final_via_rest.setStyleSheet("background-color: rgb(85, 255, 127);")
self.btn_save_final_via_rest.setObjectName("btn_save_final_via_rest")
self.gridLayout_16.addWidget(self.btn_save_final_via_rest, 4, 1, 1, 1)
self.cmb_oh_ips = QtWidgets.QComboBox(self.tab_3)
self.cmb_oh_ips.setObjectName("cmb_oh_ips")
self.gridLayout_16.addWidget(self.cmb_oh_ips, 1, 1, 1, 1)
self.btn_save_final_to_file = QtWidgets.QPushButton(self.tab_3)
self.btn_save_final_to_file.setEnabled(False)
self.btn_save_final_to_file.setStyleSheet("background-color: rgb(85, 255, 127);")
self.btn_save_final_to_file.setObjectName("btn_save_final_to_file")
self.gridLayout_16.addWidget(self.btn_save_final_to_file, 4, 0, 1, 1)
self.label_12 = QtWidgets.QLabel(self.tab_3)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_12.setFont(font)
self.label_12.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label_12.setObjectName("label_12")
self.gridLayout_16.addWidget(self.label_12, 0, 0, 1, 1)
self.label_13 = QtWidgets.QLabel(self.tab_3)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_13.setFont(font)
self.label_13.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label_13.setObjectName("label_13")
self.gridLayout_16.addWidget(self.label_13, 0, 1, 1, 1)
self.gridLayout_18 = QtWidgets.QGridLayout()
self.gridLayout_18.setObjectName("gridLayout_18")
self.txt_thing_file = QtWidgets.QLineEdit(self.tab_3)
Expand Down Expand Up @@ -330,22 +333,30 @@ def setupUi(self, MainWindow):
self.lbl_item_file.setObjectName("lbl_item_file")
self.gridLayout_18.addWidget(self.lbl_item_file, 2, 0, 1, 1)
self.gridLayout_16.addLayout(self.gridLayout_18, 1, 0, 1, 1)
self.label_13 = QtWidgets.QLabel(self.tab_3)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_13.setFont(font)
self.label_13.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label_13.setObjectName("label_13")
self.gridLayout_16.addWidget(self.label_13, 0, 1, 1, 1)
self.label_12 = QtWidgets.QLabel(self.tab_3)
font = QtGui.QFont()
font.setBold(True)
font.setWeight(75)
self.label_12.setFont(font)
self.label_12.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.label_12.setObjectName("label_12")
self.gridLayout_16.addWidget(self.label_12, 0, 0, 1, 1)
self.btn_save_final_via_rest = QtWidgets.QPushButton(self.tab_3)
self.btn_save_final_via_rest.setEnabled(False)
self.btn_save_final_via_rest.setStyleSheet("background-color: rgb(85, 255, 127);")
self.btn_save_final_via_rest.setObjectName("btn_save_final_via_rest")
self.gridLayout_16.addWidget(self.btn_save_final_via_rest, 4, 1, 1, 1)
self.btn_save_final_to_file = QtWidgets.QPushButton(self.tab_3)
self.btn_save_final_to_file.setEnabled(False)
self.btn_save_final_to_file.setStyleSheet("background-color: rgb(85, 255, 127);")
self.btn_save_final_to_file.setObjectName("btn_save_final_to_file")
self.gridLayout_16.addWidget(self.btn_save_final_to_file, 4, 0, 1, 1)
self.gridLayout_19 = QtWidgets.QGridLayout()
self.gridLayout_19.setObjectName("gridLayout_19")
self.cmb_oh_ips = QtWidgets.QComboBox(self.tab_3)
self.cmb_oh_ips.setObjectName("cmb_oh_ips")
self.gridLayout_19.addWidget(self.cmb_oh_ips, 0, 0, 1, 1)
self.cb_link_items = QtWidgets.QCheckBox(self.tab_3)
self.cb_link_items.setChecked(True)
self.cb_link_items.setObjectName("cb_link_items")
self.gridLayout_19.addWidget(self.cb_link_items, 2, 0, 1, 1)
self.cb_create_items = QtWidgets.QCheckBox(self.tab_3)
self.cb_create_items.setChecked(True)
self.cb_create_items.setObjectName("cb_create_items")
self.gridLayout_19.addWidget(self.cb_create_items, 1, 0, 1, 1)
self.gridLayout_16.addLayout(self.gridLayout_19, 1, 1, 1, 1)
self.gridLayout_14.addLayout(self.gridLayout_16, 2, 0, 1, 1)
self.gridLayout_7 = QtWidgets.QGridLayout()
self.gridLayout_7.setObjectName("gridLayout_7")
Expand Down Expand Up @@ -533,17 +544,19 @@ def retranslateUi(self, MainWindow):
self.btn_gen_fin_objts.setToolTip(_translate("MainWindow", "<html><head/><body><p>The YAML config is translated to JSON. The selected template file is then performed.</p></body></html>"))
self.btn_gen_fin_objts.setText(_translate("MainWindow", "Generate final objects"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Device Config Preview"))
self.btn_save_final_via_rest.setToolTip(_translate("MainWindow", "<html><head/><body><p>The above configuration is saved via REST API.</p><p>Please check the correct settings in tasmohab.cfg.</p><p>Also basic authentication under API Security-&gt; Show advanced must be enabled!</p></body></html>"))
self.btn_save_final_via_rest.setText(_translate("MainWindow", "Save Thing and Item via REST API"))
self.cmb_oh_ips.setToolTip(_translate("MainWindow", "<html><head/><body><p>select the right OpenHab instance for REST API</p></body></html>"))
self.btn_save_final_to_file.setToolTip(_translate("MainWindow", "<html><head/><body><p>The above configuration is saved via txt files.</p></body></html>"))
self.btn_save_final_to_file.setText(_translate("MainWindow", "Save Thing and Item to file"))
self.label_12.setText(_translate("MainWindow", "Config text File"))
self.label_13.setText(_translate("MainWindow", "REST API"))
self.txt_thing_file.setToolTip(_translate("MainWindow", "select where the things file should be stored"))
self.lbl_thing_file.setText(_translate("MainWindow", "Things path"))
self.txt_item_file.setToolTip(_translate("MainWindow", "select where the items file should be stored"))
self.lbl_item_file.setText(_translate("MainWindow", "Items path"))
self.label_13.setText(_translate("MainWindow", "REST API"))
self.label_12.setText(_translate("MainWindow", "Config text File"))
self.btn_save_final_via_rest.setToolTip(_translate("MainWindow", "<html><head/><body><p>The above configuration is saved via REST API.</p><p>Please check the correct settings in tasmohab.cfg.</p><p>Also basic authentication under API Security-&gt; Show advanced must be enabled!</p></body></html>"))
self.btn_save_final_via_rest.setText(_translate("MainWindow", "Save Thing and Item via REST API"))
self.btn_save_final_to_file.setToolTip(_translate("MainWindow", "<html><head/><body><p>The above configuration is saved via txt files.</p></body></html>"))
self.btn_save_final_to_file.setText(_translate("MainWindow", "Save Thing and Item to file"))
self.cmb_oh_ips.setToolTip(_translate("MainWindow", "<html><head/><body><p>select the right OpenHab instance for REST API</p></body></html>"))
self.cb_link_items.setText(_translate("MainWindow", "Link all items"))
self.cb_create_items.setText(_translate("MainWindow", "Create items"))
self.lbl_output.setText(_translate("MainWindow", "Thing"))
self.lbl_output_2.setText(_translate("MainWindow", "Items"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_3), _translate("MainWindow", "OH Object Output"))
Expand Down
Loading

0 comments on commit 8bc3a80

Please sign in to comment.