HTCPCP के माध्यम से Ajenti कॉफी निर्माताओं के लिए वेब इंटरफ़ेस

कॉफी के लिए चारों ओर दौड़ने और कॉफी मेकर पर बटन दबाने से थक गए? इसके लिए एक वेब इंटरफेस बनाओ!





लेख के सभी कोड गिथब पर उपलब्ध हैं।

ट्रेनिंग

अजेंटी को PyPI के साथ डाउनलोड करें और इसे अनज़िप करें।
निर्भरता स्थापित करें:
sudo pip install -Ur requirements.txt 


इसके अलावा, आपको HTCPCP- संगत कॉफी मेकर की आवश्यकता होती है, और यदि आपके पास एक नहीं है, तो आप एक साधारण एमुलेटर का उपयोग कर सकते हैं जो दूध और चॉकलेट को जोड़ने का समर्थन करता है!

HTCPCP क्लाइंट

Ajenti / plugins में htcpcp फ़ोल्डर बनाएँ, और इसमें प्लगइन के लिए फ़ाइल संरचना:

 ajenti/plugins/htcpcp ├── __init__.py ├── client.py └── layout  └── config.xml 


__Init__.py में प्लगइन के बारे में जानकारी है:

 from ajenti.api import * from ajenti.plugins import * info = PluginInfo( title='HTCPCP Example', icon='coffee', dependencies=[ PluginDependency('main') ], ) def init(): import client import main 


Client.py में, HTCPCP प्रोटोकॉल के साथ काम करने के लिए एक क्लास बनाएँ:

 import requests from ajenti.api import * from ajenti.plugins.configurator.api import ClassConfigEditor @plugin class HTCPCPClientConfigEditor (ClassConfigEditor): """ A plugin that handles editing of HTCPCPClient's classconfig """ title = 'HTCPCP Client' icon = 'coffee' def init(self): self.append(self.ui.inflate('htcpcp:config')) # htcpcp/layout/config.xml class CoffeeAddition (object): def __init__(self, name): self.name = name self.selected = False @plugin class HTCPCPClient (BasePlugin): classconfig_editor = HTCPCPClientConfigEditor # connect this plugin with a ConfigEditor default_classconfig = {'url': 'htcpcp://127.0.0.1:5000'} def init(self): self.additions = [] def check_connectivity(self): resp = requests.request('PROPFIND', self.get_url()) if resp.status_code == 418: raise Exception('This coffee pot is a teapot') def refresh(self): resp = requests.request('PROPFIND', self.get_url()) self.additions = [CoffeeAddition(x) for x in resp.headers['Additions-List'].split(';')] def get_url(self): return self.classconfig['url'].replace('htcpcp', 'http') def brew(self): """ Brew coffee with selected additions """ return requests.request('BREW', self.get_url(), headers={ 'Accept-Additions': ';'.join(x.name for x in self.additions if x.selected) }) def retrieve(self): return requests.request('GET', self.get_url()) 


ध्यान दें कि हम एक अलग कॉफ़ी कॉफी URL का उपयोग करने के लिए HTCPCPClient वर्ग को फिर से कॉन्फ़िगर करने के लिए उपयोगकर्ता को सक्षम करने के लिए कॉन्फ़िगेयर एपीआई का उपयोग करते हैं।

पैनल में एक खाली अनुभाग बनाएँ ( main.py ):
 from ajenti.api import * from ajenti.plugins.main.api import SectionPlugin from client import HTCPCPClient @plugin class CoffeePlugin (SectionPlugin): """ A HTCPCP capable coffeepot control plugin """ def init(self): self.title = 'Coffeepot' self.icon = 'coffee' self.category = _('System') # IoC:   HTCPCPClient self.pot = HTCPCPClient.get() 


वर्ग को कॉन्फ़िगर करने और पैनल लॉन्च करने के लिए थोड़ा यूआई जोड़ें:

 <bind:dict id="bind"> <!--        .classconfig  --> <formline text="Coffeepot URL"> <textbox bind="url" /> </formline> </bind:dict> 


 make run 


अब, कॉन्फ़िगर> प्लगइन्स अनुभाग में जा रहा है, हम अपना प्लगइन देखते हैं और URL कॉन्फ़िगर कर सकते हैं।



इंटरफ़ेस

चलो कुछ सुंदर बटन बनाते हैं!

htcpcp / layout / main.xml :
  <hc> <button id="brew" icon="arrow-right" text="Brew" /> <button id="retrieve" icon="coffee" text="Retrieve" /> <button id="refresh" icon="refresh" text="Refresh" /> </hc> 


htcpcp / main.py :
 from ajenti.api import * from ajenti.plugins.main.api import SectionPlugin from ajenti.ui import on from ajenti.ui.binder import Binder from client import HTCPCPClient @plugin class CoffeePlugin (SectionPlugin): """ A HTCPCP capable coffeepot control plugin """ def init(self): self.title = 'Coffeepot' self.icon = 'coffee' self.category = _('System') self.append(self.ui.inflate('htcpcp:main')) # htcpcp/layout/main.xml self.pot = HTCPCPClient.get() def on_page_load(self): try: self.pot.check_connectivity() except Exception, e: self.context.notify('error', 'Could not access the coffee pot: %s!' % str(e)) self.context.launch('configure-plugin', plugin=self.pot) #   Configure    @on('brew', 'click') def on_brew(self): resp = self.pot.brew() if resp.status_code == 200: self.context.notify('info', 'Brewing') else: self.context.notify('error', resp.text) @on('refresh', 'click') def on_refresh(self): #     self.pot.refresh() @on('retrieve', 'click') def on_retrieve(self): resp = self.pot.retrieve() if resp.status_code == 200: self.context.notify('info', resp.text) else: self.context.notify('error', resp.text) 


अब आप बटन दबा सकते हैं और कॉफी बना सकते हैं :)



डेटा प्रदर्शित करें


यह केवल समर्थित कॉफी एडिटिव्स और उनके चयन का प्रदर्शन करने के लिए बनी हुई है। इसके लिए, यूआई के साथ सीधे बाइंडर वर्ग का उपयोग करके डेटा बाइंडिंग का उपयोग करना सबसे सुविधाजनक है।

Additives के लिए चेकबॉक्स की सूची प्रदर्शित करने के लिए main.xml तत्वों को जोड़ें:
 <vc> <body> <pad id="pot-root"> <!-- id    --> <bind:collection bind="additions"> <!--    CoffeeAdditions  HTCPCPClient.additions --> <vc bind="__items"> <!--       <vc> --> <label style="bold" text="Available additions:" /> </vc> <bind:template> <!--   --> <checkbox bind:value="selected" bind:text="name" /> <!--  value   addition.selected,  text - c addition.name --> </bind:template> </bind:collection> </pad> </body> <hc> <button id="brew" icon="arrow-right" text="Brew" /> <button id="retrieve" icon="coffee" text="Retrieve" /> <button id="refresh" icon="refresh" text="Refresh" /> </hc> </vc> 


और main.py में हम डेटा के साथ इंटरफ़ेस को भरने के लिए Binder का उपयोग करते हैं, और फिर एडिटिव्स की स्थिति को अपडेट करते हैं (चेकबॉक्स की स्थिति के आधार पर चयनित या नहीं):

 from ajenti.api import * from ajenti.plugins.main.api import SectionPlugin from ajenti.ui import on from ajenti.ui.binder import Binder from client import HTCPCPClient @plugin class CoffeePlugin (SectionPlugin): """ A HTCPCP capable coffeepot control plugin """ def init(self): self.title = 'Coffeepot' self.icon = 'coffee' self.category = _('System') self.append(self.ui.inflate('htcpcp:main')) self.pot = HTCPCPClient.get() #  binder    ( HTCPCPClient self.pot    id=pot-root) self.binder = Binder(self.pot, self.find('pot-root')) def on_page_load(self): try: self.pot.check_connectivity() except Exception, e: self.context.notify('error', 'Could not access the coffee pot: %s!' % str(e)) self.context.launch('configure-plugin', plugin=self.pot) return if not self.pot.additions: #     ,   self.pot.refresh() #    UI self.binder.populate() @on('brew', 'click') def on_brew(self): #    UI self.binder.update() resp = self.pot.brew() if resp.status_code == 200: self.context.notify('info', 'Brewing') else: self.context.notify('error', resp.text) @on('refresh', 'click') def on_refresh(self): self.pot.refresh() #    UI self.binder.populate() @on('retrieve', 'click') def on_retrieve(self): resp = self.pot.retrieve() if resp.status_code == 200: self.context.notify('info', resp.text) else: self.context.notify('error', resp.text) 


हो गया :)


मैं आपको याद दिलाता हूं कि लेख से कोड गिथब पर उपलब्ध है, और उदाहरणों पर प्रलेखन और झांकना भी नहीं भूलना चाहिए

Source: https://habr.com/ru/post/In215083/


All Articles