Beispiel #1
0
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)
Beispiel #2
0
    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)