PK!55python_nhc/__init__.py__version__ = '0.1.0' from .python_nhc import NhcHub PK!upython_nhc/nhc_action.py# -*- coding: utf-8 -*- class NhcAction: actionTypes = {'1': 'lights', '4': 'shutters'} def __init__(self, id, name, type, state): """NhcAction constructor""" self._id = id self._name = name self._type = type self._state = state def __repr__(self): """Object representation""" return "NhcAction {}({}) -> {}".format( self._name, self._id, self._state) def setState(self, newState): self._state = newState return 1 def getNewStateCommand(self, expectedNewState): TCP_Message = '{"cmd":"executeactions","id":"' + str(self._id) + '","value1":"' + str(expectedNewState) + '"}' return TCP_Message def getState(self): return self._state def getType(self): return self._type def isLight(self): if self._type == '1': return True return False def isShutter(self): if self._type == '4': return True return False PK!}<<python_nhc/nhc_connection.py# -*- coding: utf-8 -*- import nclib NHC_PORT = "8000" class NhcConnection: def __init__(self, host, port=NHC_PORT): self._socket = nclib.Netcat((host, port), udp=False) def __del__(self): self._socket.shutdown(1) self._socket.close() def _receive_until(self, s): return self._socket.recv_until(s) def receive(self): return self._socket.recv().decode() def read(self): return self._receive_until(b'\r') def send(self, msg): self._socket.send(msg.encode()) return self.read() PK!ǹ_ _ python_nhc/python_nhc.py# -*- coding: utf-8 -*- import json import logging import threading from .nhc_connection import NhcConnection from .nhc_action import NhcAction logger = logging.getLogger() logger.setLevel(logging.INFO) class NhcHub: def __init__(self, host, port=8000): self._host = host self._port = port self._actions = self.listActions() daemon = threading.Thread(name='daemon', target=self.run) daemon.setDaemon(True) daemon.start() return None def run(self): connection = NhcConnection(self._host, self._port) logger.info(">Connection on {}:{}".format(self._host, self._port)) connection.send("{\"cmd\": \"startevents\"}") while True: data = connection.receive() if not data: break elif not data.isspace(): d = json.loads(data) logger.info(">> Data recieved: {}".format(d)) # Modify status of action when event recieved if 'event' in d.keys(): d_ev = json.loads(json.dumps(d['data'][0])) actionId = d_ev['id'] newVal = d_ev['value1'] logger.info(">>new event: id:{} , val:{}".format( actionId, newVal)) a = self.getAction(actionId) a.setState(newVal) connection.close() return None def getActions(self): return self._actions def getAction(self, actionId): return self._actions[actionId] def getLights(self): lights = [] for a in self.getActions: if a.isLight(): lights.append(a) return lights def getShutters(self): shutters = [] for a in self.getActions: if a.isShutters(): shutters.append(a) return shutters def listActions(self): actions = {} connection = NhcConnection(self._host, self._port) data = connection.send("{\"cmd\": \"listactions\"}") logger.info(">> Data recieved: {}".format(data)) data = json.loads(data) for a in data['data']: actionId = a['id'] actionName = a['name'] actionType = a['type'] actionState = a['value1'] actions[actionId] = NhcAction(actionId, actionName, actionType, actionState) logger.info(">> Action created: {}".format(actions[actionId])) return actions def listLocations(self): connection = NhcConnection(self._host, self._port) data = connection.send("{\"cmd\": \"listlocations\"}") logger.info(">> Data recieved: {}".format(data)) s.close() return None def modifyActionState(self, actionId, newState): a = self.getAction(actionId) command = a.getNewStateCommand(newState) connection = NhcConnection(self._host, self._port) data = connection.send(command) logger.info(">> Data recieved: {}".format(data)) return None PK!J}''"python_nhc-0.1.0.dist-info/LICENSEMIT License Copyright (c) 2018 gawood Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HڽTU python_nhc-0.1.0.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!Hn#python_nhc-0.1.0.dist-info/METADATANB1}yob0iDDP&VI-ɜﶡt}YIe8ٮj烦W;O5ݖ޴;9m 𔈟>["խ17X N^gjNE{z}:ЗTɗr/kTf/Kx]cks,%>G ǽPUxPK!55python_nhc/__init__.pyPK!uipython_nhc/nhc_action.pyPK!}<<python_nhc/nhc_connection.pyPK!ǹ_ _ (python_nhc/python_nhc.pyPK!J}''"python_nhc-0.1.0.dist-info/LICENSEPK!HڽTU $python_nhc-0.1.0.dist-info/WHEELPK!Hn#python_nhc-0.1.0.dist-info/METADATAPK!Hhχt!python_nhc-0.1.0.dist-info/RECORDPKX