def confUpdate(self): self.sleep_period = int( GLOB.readConfig(configFileNM, 'FLOWERCARE', 'period', '300')) self.miflora_cache_timeout = self.sleep_period - 1 self.used_adapter = GLOB.readConfig(configFileNM, 'FLOWERCARE', 'adapter', 'hci0') self.reporting_mode = 'json' self.daemon_enabled = True self.user_id = GLOB.readConfig(configFileNM, 'SETUP', 'user_id', '0') self.device_id = GLOB.readConfig(configFileNM, 'AGENT', 'id', '0') self.default_base_topic = 'miflora' self.local_id = GLOB.readConfig(configFileNM, 'WEATHER', 'location0', '') # Check configuration if self.reporting_mode not in [ 'mqtt-json', 'mqtt-homie', 'json', 'mqtt-smarthome', 'homeassistant-mqtt', 'thingsboard-json', 'wirenboard-mqtt' ]: LOG.writeLn( 'Configuration parameter reporting_mode set to an invalid value ' ) sys.exit(1) if (self.device_id == '0' or self.user_id == '0'): LOG.writeLn('setup.ini file load error') sys.exit(1) #LOG.writeLn('Configuration accepted') print('** USER = %s' % self.user_id) print('** PRODUCT = %s' % self.device_id) print('** interval = %d' % self.sleep_period) print('** location = %s' % self.local_id) print('--------------------------------------------------') print('')
def on_message(client, userdata, msg): strJson = msg.payload.decode() #print(msg.topic+" "+str(msg.payload)) #토픽과 메세지를 출력한다. LOG.writeLn("[MQTT] "+ msg.topic+" "+ strJson) #토픽과 메세지를 출력한다. try: if GLOB.saveJsonData(COM.gJsonDir+"action.json", strJson) == 0: remote = '0' grp1 = GLOB.getJsonVal(strJson, 'light', '99') if (grp1 != '99') : GLOB.writeConfig(controlFileNM, 'CONTROL', 'light', grp1) remote = '1' grp2 = GLOB.getJsonVal(strJson, 'heater', '99') if (grp2 != '99') : GLOB.writeConfig(controlFileNM, 'CONTROL', 'heater', grp2) remote = '1' grp3 = GLOB.getJsonVal(strJson, 'fan', '99') if (grp3 != '99') : GLOB.writeConfig(controlFileNM, 'CONTROL', 'fan', grp3) remote = '1' grp4 = GLOB.getJsonVal(strJson, 'pump', '99') if (grp4 != '99') : GLOB.writeConfig(controlFileNM, 'CONTROL', 'pump', grp4) remote = '1' active = GLOB.getJsonVal(strJson, 'active', '99') if (active != '99') : remote = active GLOB.writeConfig(controlFileNM, 'CONTROL', 'active', remote) #print("grp data %s %s" % (grp1, grp2)) if GLOB.appendWATERsControlInfo(COM.gJsonDir+"device.json", GLOB.readConfig(controlFileNM, 'CONTROL', 'active', '0') , GLOB.readConfig(controlFileNM, 'CONTROL', 'light', '0') , GLOB.readConfig(controlFileNM, 'CONTROL', 'pump', '0') , GLOB.readConfig(controlFileNM, 'CONTROL', 'fan', '0') , GLOB.readConfig(controlFileNM, 'CONTROL', 'heater', '0') ) == 0 : REQUEST.updateDIYs(user_id, dev_id) except Exception as e : LOG.writeLn("[MQTT] : error : %s" % e)
def on_message(client, userdata, msg): strJson = msg.payload.decode() LOG.writeLn("[MQTT] " + msg.topic + " " + strJson) #토픽과 메세지를 출력한다. try: if GLOB.saveJsonData(COM.gJsonDir + "action.json", strJson) == 0: remote = '0' grp1 = GLOB.getJsonVal(strJson, 'interval', '0') if (grp1 != '0'): GLOB.writeConfig(configFileNM, 'SETUP', 'interval', grp1) remote = '1' grp2 = GLOB.getJsonVal(strJson, 'resolution_x', '0') if (grp2 != '0'): GLOB.writeConfig(configFileNM, 'SETUP', 'resolution_x', grp2) remote = '1' grp3 = GLOB.getJsonVal(strJson, 'resolution_y', '0') if (grp3 != '0'): GLOB.writeConfig(configFileNM, 'SETUP', 'resolution_y', grp3) remote = '1' grp4 = GLOB.getJsonVal(strJson, 'rotation', '99') if (grp4 != '99'): GLOB.writeConfig(configFileNM, 'SETUP', 'rotation', grp4) remote = '1' grp5 = GLOB.getJsonVal(strJson, 'start_time', '99') if (grp5 != '99'): GLOB.writeConfig(configFileNM, 'SETUP', 'start_time', grp5) remote = '1' grp6 = GLOB.getJsonVal(strJson, 'end_time', '99') if (grp6 != '99'): GLOB.writeConfig(configFileNM, 'SETUP', 'end_time', grp6) remote = '1' print('grps = %s %s %s %s %s %s [%s]' % (grp1, grp2, grp3, grp4, grp5, grp6, remote)) if remote == '1': if GLOB.appendTIMELAPsControlInfo( COM.gJsonDir + "device.json", GLOB.readConfig(configFileNM, 'SETUP', 'interval', '0'), GLOB.readConfig(configFileNM, 'SETUP', 'resolution_x', '0'), GLOB.readConfig(configFileNM, 'SETUP', 'resolution_y', '0'), GLOB.readConfig(configFileNM, 'SETUP', 'rotation', '0'), GLOB.readConfig(configFileNM, 'SETUP', 'start_time', '0'), GLOB.readConfig(configFileNM, 'SETUP', 'end_time', '0')) == 0: REQUEST.updateDIYs(user_id, dev_id) LOG.writeLn("[MQTT] Command recive and reboot in 3 sec") time.sleep(3) GLOB.run_command("sudo reboot") except Exception as e: LOG.writeLn("[MQTT] : error : %s" % e)
STAT_FAN = 0 STAT_HEATER = 0 cntLoadError = 0 def clearControlFile(): GLOB.writeConfig(controlFileNM, 'CONTROL', 'light' , '0') GLOB.writeConfig(controlFileNM, 'CONTROL', 'pump' , '0') GLOB.writeConfig(controlFileNM, 'CONTROL', 'fan' , '0') GLOB.writeConfig(controlFileNM, 'CONTROL', 'heater', '0') try: GPIO.output(LIGHT, GPIO.HIGH) time.sleep(1) LOG.writeLn("[CONTROL] : h/w Control Start %s" % GLOB.readConfig(configFileNM, 'AGENT', 'id', '0')) while (True): time.sleep(1) try: STAT_LIGHT = int(GLOB.readConfig(controlFileNM, 'CONTROL', 'light' , '99')) #0:ON/OFF, 1: LED 켜기, 0: LED 끄기 STAT_PUMP = int(GLOB.readConfig(controlFileNM, 'CONTROL', 'pump' , '99')) #0:ON/OFF, 1: PUMP 켜기, 0: PUMP 끄기 STAT_FAN = int(GLOB.readConfig(controlFileNM, 'CONTROL', 'fan' , '99')) #0:ON/OFF, 1: FAN 켜기, 0: FAN 끄기 STAT_HEATER = int(GLOB.readConfig(controlFileNM, 'CONTROL', 'heater', '99')) #0:ON/OFF, 1: HEATER 켜기, 0: HEATER 끄기 if (max(STAT_LIGHT, STAT_PUMP, STAT_FAN, STAT_HEATER) == 99): LOG.writeLn("[CONTROL] : config load error then continues") cntLoadError = cntLoadError + 1 if (cntLoadError > 10) : clearControlFile()
print("another process running!") exit() #프로그램 시작 import paho.mqtt.client as mqtt from frogmon.uCommon import COM from frogmon.uGlobal import GLOB from frogmon.uRequest import REQUEST from frogmon.uLogger import LOG #from frogmon.ulogger import LOG configFileNM = COM.gHomeDir+COM.gSetupFile controlFileNM = COM.gHomeDir+COM.gControlFile mSvr_addr = GLOB.readConfig(configFileNM, 'MQTT', 'host_addr', 'frogmon.synology.me') mSvr_port = GLOB.readConfig(configFileNM, 'MQTT', 'host_port', '8359') user_id = GLOB.readConfig(configFileNM, 'SETUP', 'user_id', '0') dev_id = GLOB.readConfig(configFileNM, 'AGENT', 'id', '0') mSub_nm = "DIYs/%s/%s" % (user_id, dev_id) #mSub_nm = "%s" % (user_id) #서버로부터 CONNTACK 응답을 받을 때 호출되는 콜백 def on_connect(client, userdata, flags, rc): LOG.writeLn("[MQTT] Connected with result code "+str(rc)) #client.subscribe("$SYS/#") client.subscribe("%s" % mSub_nm) #구독 "nodemcu" #서버로부터 publish message를 받을 때 호출되는 콜백
import glob import subprocess import shutil from frogmon.uConfig import CONF from frogmon.uLogger import LOG from frogmon.uCommon import COM from frogmon.uGlobal import GLOB # 프로그램 시작 GLOB.directoryInit(GLOB.whoami(), "DOORs") configFileNM = COM.gHomeDir + COM.gSetupFile controlFileNM = COM.gHomeDir + COM.gControlFile user_id = GLOB.readConfig(configFileNM, 'SETUP', 'user_id', '0') dev_id = GLOB.readConfig(configFileNM, 'AGENT', 'id', '0') # 함수 정의 mWhoami = GLOB.whoami() mUsbDir = "/mnt/usb_stick" mFaceDir = COM.gHomeDir + "faces/" GLOB.folderMaker(mFaceDir) print("whoami = %s" % mWhoami) print("usb dir = %s" % mUsbDir) print("face home dir = %s" % mFaceDir) mSourcePath = []
data['RES_Y'] = mResolution_y data['ROTATION'] = mRotation data['START_TIME'] = mStart_time data['END_TIME'] = mEnd_time data['DAY_COUNT'] = mCount data['LOCAL_IP'] = GLOB.get_ip_address() LOG.writeLn('json file save:%s' % COM.gJsonDir + 'device.json') with open(COM.gJsonDir + 'device.json', 'w', encoding='utf-8') as make_file: json.dump(data, make_file, indent="\t") # init configFileNM = COM.gHomeDir + COM.gSetupFile mUser_id = GLOB.readConfig(configFileNM, 'SETUP', 'user_id', 'empty') mProduct_id = GLOB.readConfig(configFileNM, 'SETUP', 'product_id', 'empty') mVersion = GLOB.readConfig(configFileNM, 'SETUP', 'version', 'ver 1.0') mInterval_sec = int(GLOB.readConfig(configFileNM, 'SETUP', 'interval', '600')) mResolution_x = int( GLOB.readConfig(configFileNM, 'SETUP', 'resolution_x', '640')) mResolution_y = int( GLOB.readConfig(configFileNM, 'SETUP', 'resolution_y', '480')) mRotation = int(GLOB.readConfig(configFileNM, 'SETUP', 'rotation', '0')) mStart_time = int(GLOB.readConfig(configFileNM, 'SETUP', 'start_time', '0')) mEnd_time = int(GLOB.readConfig(configFileNM, 'SETUP', 'end_time', '23')) mCount = 0 GLOB.setUpdateTime() makeJson()
def atOnce(self): self.chkSensor() GLOB.setUpdateTime() self.confUpdate() for [flora_name, flora] in self.flores.items(): data = dict() attempts = 2 flora['poller']._cache = None flora['poller']._last_read = None flora['stats']['count'] = flora['stats']['count'] + 1 #print_line('Retrieving data from sensor "{}" ...'.format(flora['name_pretty'])) while attempts != 0 and not flora['poller']._cache: try: flora['poller'].fill_cache() flora['poller'].parameter_value(MI_LIGHT) except (IOError, BluetoothBackendException): attempts = attempts - 1 if attempts > 0: LOG.writeLn('Retrying ...') flora['poller']._cache = None flora['poller']._last_read = None if not flora['poller']._cache: flora['stats']['failure'] = flora['stats']['failure'] + 1 LOG.writeLn( 'Failed to retrieve data from Mi Flora sensor "{}" ({}), success rate: {:.0%}' .format( flora['name_pretty'], flora['mac'], flora['stats']['success'] / flora['stats']['count'])) print() continue else: flora['stats']['success'] = flora['stats']['success'] + 1 for param, _ in mParameters.items(): data[param] = flora['poller'].parameter_value(param) rc = -1 try: data['timestamp'] = COM.gstrDATE data['name'] = flora_name data['name_pretty'] = flora['name_pretty'] data['mac'] = flora['mac'] data['firmware'] = flora['firmware'] #print('Data for "{}": {}'.format(flora_name, json.dumps(data))) with open(COM.gJsonDir + flora_name + '.json', 'w', encoding="utf-8") as make_file: json.dump(data, make_file, ensure_ascii=False, indent="\t") #to make device.json file datas = [] datas.append(self.device_id) datas.append(GLOB.readConfig(configFileNM, 'SETUP', 'mod', '')) datas.append( GLOB.readConfig(configFileNM, 'AGENT', 'alarm', '0')) datas.append(str(data['temperature'])) datas.append('0') datas.append(str(data['moisture'])) datas.append(str(data['conductivity'])) datas.append(str(data['light'])) datas.append(str(data['battery'])) datas.append( GLOB.readConfig(controlFileNM, 'CONTROL', 'active', '0')) # active datas.append( GLOB.readConfig(controlFileNM, 'CONTROL', 'light', '0')) # LED 조명 datas.append( GLOB.readConfig(controlFileNM, 'CONTROL', 'pump', '0')) # 워터펌프 datas.append( GLOB.readConfig(controlFileNM, 'CONTROL', 'fan', '0')) # 팬 datas.append( GLOB.readConfig(controlFileNM, 'CONTROL', 'heater', '0')) # 히터 datas.append(COM.gstrYMDHMS) datas.append(GLOB.loadTeamviewerID()) rc = self.encodeStatJson(COM.gJsonDir + 'device.json', datas) if rc == 0: LOG.writeLn( '%s [%s] %s %s %s %s %s' % (data['name'], data['battery'], data['temperature'], data['moisture'], data['light'], data['conductivity'], data['mac'])) except Exception as e: LOG.writeLn("[ERROR][FLOWERCARE] : %s" % e) if rc == 0: if GLOB.appendWATERsControlInfo( COM.gJsonDir + "device.json", GLOB.readConfig(controlFileNM, 'CONTROL', 'active', '0'), GLOB.readConfig(controlFileNM, 'CONTROL', 'light', '0'), GLOB.readConfig(controlFileNM, 'CONTROL', 'pump', '0'), GLOB.readConfig(controlFileNM, 'CONTROL', 'fan', '0'), GLOB.readConfig(controlFileNM, 'CONTROL', 'heater', '0')) == 0: REQUEST.updateDIYs(self.user_id, self.device_id)