def get_List(self, sPay): List=[] iCt=0 clsConst = com_appConst.appConstClass() if (len(sPay) > mMaxSend): sPay=sPay[0:mMaxSend] items =sPay.split(" ") #print("len="+ str(len(items))) #print items if(len(items) < 1): return sBuff="" sTmp="" for item in items: sTmp=item if( len(sTmp) > mMaxOne): sTmp=sTmp[0:mMaxOne] #print sTmp sAft=sBuff+sTmp if(len(sAft) >= mMaxOne): List.append(sBuff) sBuff="" iCt=0 if(iCt > 0): sBuff=sBuff+" "+ sTmp else: sBuff=sBuff+ sTmp iCt=iCt+1 if(len(sTmp)>0): List.append(sTmp) #print List return List
def exec_parse(self, sXml, sCity): ret = {} clsConst = com_appConst.appConstClass() clsWdat = com_wdat.wdatClass() clsWdat.delete_dat() iCt = 0 try: dom = xml.dom.minidom.parseString(sXml) for node in dom.getElementsByTagName('channel'): for title in node.getElementsByTagName("title"): sTitle = title.firstChild.data #print " " + title.tagName + "=" +sTitle #print " ct="+ str(iCt)+", " + title.tagName + "=" +sTitle if (iCt == 1): #print " 1=" + title.tagName + "=" +sTitle sRet = clsWdat.get_wString(sTitle) print "sRet=" + sRet clsWdat.saveData(sRet, sCity) iCt = iCt + 1 except: print "failue, exec_parse" raise finally: print "End ,exec_parse" return ret
def get_master(self, sId ): ret={} clsConst = com_appConst.appConstClass() headers = { "X-Parse-Application-Id": clsConst.mParse_APP_ID , "X-Parse-REST-API-Key": clsConst.mParse_REST_ID , "Content-Type": "application/json" } dtParam = {'mc_id': int(sId) }; params = urllib.urlencode({"where":json.dumps(dtParam)}) try: r = requests.get('https://api.parse.com/1/classes/MsObject?' +params, headers=headers , timeout=30) print r.status_code dRest = r.json() nCt= len(dRest["results"]) print nCt if(nCt > 0): print dRest["results"][0] ret=dRest["results"][0] except: print "failue, send_parse" raise finally: print "End ,send_parse" return ret
def send_push(self, dict, sKey): sRet="" clsConst = com_appConst.appConstClass() sParam ="mc_id="+ str(int(dict["mc_id"])) sParam+="&rkey="+ clsConst.mREST_KEY sParam+="&snum_1="+str(int(dict["snum_1"])) sParam+="&snum_2="+str(int(dict["snum_2"])) sParam+="&snum_3="+str(int(dict["snum_3"])) sParam+="&snum_4="+str(int(dict["snum_4"])) sUrl= clsConst.mURL +"/php/mc_post2.php?" +sParam try: r = requests.get(sUrl , timeout=30) print r.status_code sText= r.text print sText if (mHEAD in sText): dic = sText.split(mHEAD) if (len(dic) > 1): print dic[1] sRet=dic[1] except: print "failue, send_parse" raise finally: print "End ,send_parse" return sRet
def get_List(self, sPay): List = [] iCt = 0 clsConst = com_appConst.appConstClass() if (len(sPay) > mMaxSend): sPay = sPay[0:mMaxSend] items = sPay.split(" ") #print("len="+ str(len(items))) #print items if (len(items) < 1): return sBuff = "" sTmp = "" for item in items: sTmp = item if (len(sTmp) > mMaxOne): sTmp = sTmp[0:mMaxOne] #print sTmp sAft = sBuff + sTmp if (len(sAft) >= mMaxOne): List.append(sBuff) sBuff = "" iCt = 0 if (iCt > 0): sBuff = sBuff + " " + sTmp else: sBuff = sBuff + sTmp iCt = iCt + 1 if (len(sTmp) > 0): List.append(sTmp) #print List return List
def init_proc(): ser = serial.Serial(mDevice, 9600) clsConst = com_appConst.appConstClass() clsParam = com_getparam.getparamClass() clsCom = com_func.funcClass() clsLog = com_logging2.loggingClass() clsHttp = com_putHttp.putHttpClass() from datetime import datetime tmBefPush = datetime.now() while True: val = ser.readline() bFrom = clsParam.Is_fromMC(val) if bFrom == True: dic = clsParam.getDict(val) sTime = datetime.now().strftime("%Y%m%d%H%M%S") tmNow = datetime.now() if is_validTime(tmBefPush, tmNow, clsConst.mTimePut): tmBefPush = datetime.now() try: sRes = "" sMsg = clsHttp.send_push(dic, clsConst.mREST_KEY) sRes = sHEAD + sMsg ser.write(sRes) except: print "--------------------------------------------" print traceback.format_exc(sys.exc_info()[2]) print "--------------------------------------------" clsLog.debug(traceback.format_exc(sys.exc_info()[2])) print("IN :" + val)
def init_proc(): ser=serial.Serial(mDevice ,9600) clsConst = com_appConst.appConstClass() clsParam = com_getparam.getparamClass() clsCom = com_func.funcClass() clsLog = com_logging2.loggingClass() clsHttp= com_putHttp.putHttpClass() from datetime import datetime tmBefPush = datetime.now() while True: val=ser.readline() bFrom = clsParam.Is_fromMC(val) if bFrom==True: dic= clsParam.getDict(val) sTime = datetime.now().strftime("%Y%m%d%H%M%S") tmNow = datetime.now() if is_validTime(tmBefPush, tmNow, clsConst.mTimePut ): tmBefPush = datetime.now() try: sRes=""; sMsg = clsHttp.send_push( dic, clsConst.mREST_KEY ) sRes =sHEAD + sMsg ser.write(sRes) except: print "--------------------------------------------" print traceback.format_exc(sys.exc_info()[2]) print "--------------------------------------------" clsLog.debug( traceback.format_exc(sys.exc_info()[2]) ) print("IN :" + val)
def send_parse(self, dict, sTime): clsConst = com_appConst.appConstClass() headers = { "X-Parse-Application-Id": clsConst.mParse_APP_ID, "X-Parse-REST-API-Key": clsConst.mParse_REST_ID, "Content-Type": "application/json" } dtParam = {'mc_id': int(dict["mc_id"])} dtParam["snum1"] = int(dict["snum_1"]) dtParam["snum2"] = int(dict["snum_2"]) dtParam["snum3"] = int(dict["snum_3"]) dtParam["snum4"] = int(dict["snum_4"]) dtParam["dtnum"] = int(sTime) try: r = requests.post('https://api.parse.com/1/classes/SenObject1', headers=headers, data=json.dumps(dtParam), timeout=30) print r.status_code print r.json() except: print "failue, send_parse" raise finally: print "End ,send_parse"
def send_pubRGB(self, sPay, sTopic): iWait = 0 clsConst = com_appConst.appConstClass() publish.single(topic=sTopic, payload=sPay, hostname=clsConst.mMQTT_HostName, port=clsConst.mMQTT_Port) time.sleep(iWait) return
def delete_dat(self ): ret=False clsConst = com_appConst.appConstClass() connection = MySQLdb.connect(host=clsConst.mHost, db=clsConst.mDB_NAME, user=clsConst.mUser, passwd=clsConst.mPass, charset="utf8") cursor = connection.cursor() sSql="delete from T_MATRIX_DAT;" cursor.execute(sSql) connection.commit() connection.close() ret=True return ret
def delete_dat(self, sCity): ret=False clsConst = com_appConst.appConstClass() connection = MySQLdb.connect(host=clsConst.mHost, db=clsConst.mDB_NAME, user=clsConst.mUser, passwd=clsConst.mPass, charset="utf8") cursor = connection.cursor() sSql="delete from t_wdat where CITY='" + sCity +"'" cursor.execute(sSql) connection.commit() connection.close() ret=True return ret
def get_u8code(self, sCode): sRet = "" clsConst = com_appConst.appConstClass() if (sCode == clsConst.mCodeHARE): sRet = clsConst.mHareStr if (sCode == clsConst.mCodeKUMORI): sRet = clsConst.mKumoriStr if (sCode == clsConst.mCodeAME): sRet = clsConst.mAmeStr return sRet
def delete_dat(self): ret = False clsConst = com_appConst.appConstClass() connection = MySQLdb.connect(host=clsConst.mHost, db=clsConst.mDB_NAME, user=clsConst.mUser, passwd=clsConst.mPass, charset="utf8") cursor = connection.cursor() sSql = "delete from T_MATRIX_DAT;" cursor.execute(sSql) connection.commit() connection.close() ret = True return ret
def test_parse(self, sXml ): ret={} clsConst = com_appConst.appConstClass() try: dom = xml.dom.minidom.parseString(sXml) for node in dom.getElementsByTagName('item'): for url in node.getElementsByTagName("itemName"): print " " + url.tagName + "=" + url.firstChild.data for item_id in node.getElementsByTagName("itemId"): print " " + item_id.tagName + "=" + item_id.firstChild.data except: print "failue, test_parse" raise finally: print "End ,test_parse" return ret
def test_parse(self, sXml): ret = {} clsConst = com_appConst.appConstClass() try: dom = xml.dom.minidom.parseString(sXml) for node in dom.getElementsByTagName('item'): for url in node.getElementsByTagName("itemName"): print " " + url.tagName + "=" + url.firstChild.data for item_id in node.getElementsByTagName("itemId"): print " " + item_id.tagName + "=" + item_id.firstChild.data except: print "failue, test_parse" raise finally: print "End ,test_parse" return ret
def saveSensor(self, topic, payload): ret=False clsConst = com_appConst.appConstClass() connection = MySQLdb.connect(host=clsConst.mHost, db=clsConst.mDB_NAME, user=clsConst.mUser, passwd=clsConst.mPass, charset="utf8") cursor = connection.cursor() sSql=u"INSERT INTO t_sensor ( topic, payload,created)" sSql=sSql+" values (" sSql=sSql+"'"+topic +"'" sSql=sSql+","+ payload sSql=sSql+",now() );" #print sSql cursor.execute(sSql) connection.commit() cursor.close() connection.close() ret=True return ret
def exec_parse(self, sXml ): ret={} clsConst = com_appConst.appConstClass() clsNews = com_news.newsClass() clsNews.delete_dat() try: dom = xml.dom.minidom.parseString(sXml) for node in dom.getElementsByTagName('item'): for title in node.getElementsByTagName("title"): sTitle = title.firstChild.data #print " " + title.tagName + "=" +sTitle clsNews.saveData(sTitle) except: print "failue, exec_parse" raise finally: print "End ,exec_parse" return ret
def send_pubw(self, sPay ,sTopic): clsConst = com_appConst.appConstClass() iWait=0 iAdd=3 if (len(sPay) < 1): return if( len(sPay) > 20 ): sPay=sPay[0:20] if( len(sPay) > 10 ): iAdd=6; print("sLen=") print(len(sPay)) publish.single(topic=sTopic, payload=sPay, hostname=clsConst.mMQTT_HostName , port=clsConst.mMQTT_Port ) print("iWait=") iWait = (len(sPay) * clsConst.mWaitMsec) / 1000 iWait = iWait+iAdd print(str(iWait) ) time.sleep(iWait) return
def get_wdata(self, iMax): #sRet="" ret=[] dic = {"id": 0L, "title" : "" } sSql="SELECT title from t_news order by id ASC limit " +str(iMax) clsConst = com_appConst.appConstClass() connection = MySQLdb.connect(host=clsConst.mHost, db=clsConst.mDB_NAME, user=clsConst.mUser, passwd=clsConst.mPass, charset="utf8") cursor = connection.cursor() cursor.execute( sSql ) result = cursor.fetchall() for row in result: dic = {"id": 0L, "title" : "" } dic["title"] =row[0] ret.append(dic) cursor.close() connection.close() return ret
def saveData(self, sCond , sCity): ret=False clsConst = com_appConst.appConstClass() connection = MySQLdb.connect(host=clsConst.mHost, db=clsConst.mDB_NAME, user=clsConst.mUser, passwd=clsConst.mPass, charset="utf8") cursor = connection.cursor() sSql=u"INSERT INTO T_MATRIX_DAT (COND, CITY" sSql=sSql+",created" sSql=sSql+") values (" sSql=sSql+ " \""+ sCond +"\" " sSql=sSql+ ", \""+ sCity +"\" " sSql=sSql+",now() );" #print sSql cursor.execute(sSql) connection.commit() cursor.close() connection.close() ret=True return ret
def get_wdata(self, city): #sRet="" dic = {"id": 0L, "city" : "", "cond" : "" ,"temp_H" : "", "temp_L" : ""} sSql="SELECT city, cond, TEMP_H, TEMP_L from t_wdat " sSql=sSql+" where CITY='"+ city +"' limit 1" clsConst = com_appConst.appConstClass() connection = MySQLdb.connect(host=clsConst.mHost, db=clsConst.mDB_NAME, user=clsConst.mUser, passwd=clsConst.mPass, charset="utf8") cursor = connection.cursor() cursor.execute( sSql ) result = cursor.fetchall() for row in result: dic["city"] =row[0] dic["cond"] =row[1] dic["temp_H"] =row[2] dic["temp_L"] =row[3] cursor.close() connection.close() return dic
def get_wData(self, sCity): sRet = "" sSql = "SELECT COND from T_MATRIX_DAT " sSql = sSql + " where CITY=" + str(sCity) sSql = sSql + " limit 1" clsConst = com_appConst.appConstClass() connection = MySQLdb.connect(host=clsConst.mHost, db=clsConst.mDB_NAME, user=clsConst.mUser, passwd=clsConst.mPass, charset="utf8") cursor = connection.cursor() cursor.execute(sSql) result = cursor.fetchall() for row in result: sRet = row[0] cursor.close() connection.close() return sRet
def saveSensor(self, topic, payload): ret = False clsConst = com_appConst.appConstClass() connection = MySQLdb.connect(host=clsConst.mHost, db=clsConst.mDB_NAME, user=clsConst.mUser, passwd=clsConst.mPass, charset="utf8") cursor = connection.cursor() sSql = u"INSERT INTO t_sensor ( topic, payload,created)" sSql = sSql + " values (" sSql = sSql + "'" + topic + "'" sSql = sSql + "," + payload sSql = sSql + ",now() );" #print sSql cursor.execute(sSql) connection.commit() cursor.close() connection.close() ret = True return ret
def get_sendWdata(self, sCity, sTopic): sPay = "" clsConst = com_appConst.appConstClass() clsWeaher = com_weather.weatherClass() dic = clsWeaher.get_wdata(sCity) print (len(dic)) print (dic) if len(dic) < 1: return # city self.send_pubw(dic["city"], sTopic) # cond self.send_pubw(dic["cond"], sTopic) # temp-H print ("temp_H=") print (str(dic["temp_H"])) sTemp = "High Temp:" + str(dic["temp_H"]) + "(C)" self.send_pubw(sTemp, sTopic) # temp-L sTemp = "Low Temp:" + str(dic["temp_L"]) + "(C)" self.send_pubw(sTemp, sTopic) return
def saveData(self, sCond, sCity): ret = False clsConst = com_appConst.appConstClass() connection = MySQLdb.connect(host=clsConst.mHost, db=clsConst.mDB_NAME, user=clsConst.mUser, passwd=clsConst.mPass, charset="utf8") cursor = connection.cursor() sSql = u"INSERT INTO T_MATRIX_DAT (COND, CITY" sSql = sSql + ",created" sSql = sSql + ") values (" sSql = sSql + " \"" + sCond + "\" " sSql = sSql + ", \"" + sCity + "\" " sSql = sSql + ",now() );" #print sSql cursor.execute(sSql) connection.commit() cursor.close() connection.close() ret = True return ret
def get_colorData(self, mcid): #sRet="" ret=[] #dic = {"id": 0L, "title" : "" } sSql="SELECT s_rgb from t_color " sSql=sSql+" where mcid="+ str(mcid) sSql=sSql+" and vflg=1" sSql=sSql+" order by modified DESC limit 100" # sSql=sSql+" order by id ASC limit 100" clsConst = com_appConst.appConstClass() connection = MySQLdb.connect(host=clsConst.mHost, db=clsConst.mDB_NAME, user=clsConst.mUser, passwd=clsConst.mPass, charset="utf8") cursor = connection.cursor() cursor.execute( sSql ) result = cursor.fetchall() for row in result: dic = {"id": 0L, "s_rgb" : "" } dic["s_rgb"] =row[0] ret.append(dic) cursor.close() connection.close() return ret
def send_parse(self, dict, sTime): clsConst = com_appConst.appConstClass() headers = { "X-Parse-Application-Id": clsConst.mParse_APP_ID , "X-Parse-REST-API-Key": clsConst.mParse_REST_ID , "Content-Type": "application/json" } dtParam ={'mc_id': int(dict["mc_id"]) } dtParam["snum1"] = int(dict["snum_1"]) dtParam["snum2"] = int(dict["snum_2"]) dtParam["snum3"] = int(dict["snum_3"]) dtParam["snum4"] = int(dict["snum_4"]) dtParam["dtnum"] = int(sTime) try: r = requests.post('https://api.parse.com/1/classes/SenObject1', headers=headers , data=json.dumps(dtParam), timeout=30) print r.status_code print r.json() except: print "failue, send_parse" raise finally: print "End ,send_parse"
def saveData(self, getDat ): ret=False clsConst = com_appConst.appConstClass() connection = MySQLdb.connect(host=clsConst.mHost, db=clsConst.mDB_NAME, user=clsConst.mUser, passwd=clsConst.mPass, charset="utf8") cursor = connection.cursor() sSql=u"INSERT INTO t_wdat (CITY" sSql=sSql+",COND" sSql=sSql+",TEMP_H" sSql=sSql+",TEMP_L" sSql=sSql+",created" sSql=sSql+") values (" sSql=sSql+ " '"+ getDat['location']['city'] +"'" sSql=sSql+ ",'"+ getDat['condition']['text'] +"'" sSql=sSql+ ","+ getDat['forecasts'][1]['high'] sSql=sSql+ ","+ getDat['forecasts'][1]['low'] sSql=sSql+",now() );" cursor.execute(sSql) connection.commit() cursor.close() connection.close() ret=True return ret
def get_wdata(sCity): sPay="" clsConst = com_appConst.appConstClass() clsWeaher= com_weather.weatherClass() clsPub=com_mqttPub.mqttPubClass() dic= clsWeaher.get_wdata(sCity) print(len(dic)) print(dic) if (len(dic) < 1): return #city clsPub.send_pubw(dic["city"] ,mTopic) #cond clsPub.send_pubw(dic["cond"] ,mTopic) #temp-H print("temp_H=") print(str(dic["temp_H"])) sTemp="High Temp:" + str(dic["temp_H"]) + "(C)" clsPub.send_pubw(sTemp ,mTopic) #temp-L sTemp="Low Temp:" + str(dic["temp_L"]) + "(C)" clsPub.send_pubw(sTemp ,mTopic) return
def get_wdata(): clsConst = com_appConst.appConstClass() clsWeaher= com_weather.weatherClass() sCity="Tokyo" clsWeaher.delete_dat(sCity) #tokyo result = pywapi.get_weather_from_yahoo('JAXX0085') #oosaka #result = pywapi.get_weather_from_yahoo('JAXX0071') #fukuoka #result = pywapi.get_weather_from_yahoo('JAXX0009') #sapporo #result = pywapi.get_weather_from_yahoo('JAXX0078') print '---' print result['title'] print 'city: ' + result['location']['city'] print 'condition: ' + result['condition']['text'] print 'temp: ' + result['condition']['temp'] + '(C)' print 'High Temp: ' + result['forecasts'][1]['high'] print 'Low Temp: ' + result['forecasts'][1]['low'] print '---' clsWeaher.saveData( result)
def send_pubw(self, sPay ,sTopic): clsConst = com_appConst.appConstClass() iWait=0 # iAdd=7 iAdd=10 if (len(sPay) < 1): return # if( len(sPay) > 20 ): # sPay=sPay[0:20] if( len(sPay) > mMaxOne ): sPay=sPay[0:mMaxOne] if( len(sPay) > 10 ): iAdd=(len(sPay) * 1000) * 0.5 iAdd=iAdd / 1000 print("sLen=") print(len(sPay)) #print("sPay=" +sPay) publish.single(topic=sTopic, payload=sPay, hostname=clsConst.mMQTT_HostName , port=clsConst.mMQTT_Port ) print("iWait=") iWait = (len(sPay) * clsConst.mWaitMsec) / 1000 iWait = iWait+iAdd print(str(iWait) ) time.sleep(iWait) return
def send_valve(self, dict, sTime, dMst): clsConst = com_appConst.appConstClass() clsCom = com_func.funcClass() headers = { "X-Parse-Application-Id": clsConst.mParse_APP_ID , "X-Parse-REST-API-Key": clsConst.mParse_REST_ID , "Content-Type": "application/json" } k_flg_1=0 k_flg_2=0 k_flg_3=0 k_flg_4=0 if dMst["vnum_1"]==1L: k_flg_1= clsCom.Is_validValve( int(dict["snum_1"]) ,dMst["moi_num"] ) if dMst["vnum_2"]==1L: k_flg_2= clsCom.Is_validValve( int(dict["snum_2"]) ,dMst["moi_num"]) if dMst["vnum_3"]==1L: k_flg_3= clsCom.Is_validValve( int(dict["snum_3"]) ,dMst["moi_num"]) if dMst["vnum_4"]==1L: k_flg_4= clsCom.Is_validValve( int(dict["snum_4"]) ,dMst["moi_num"]) dtParam ={'mc_id': int(dict["mc_id"]) } dtParam["vnum1"] = k_flg_1 dtParam["vnum2"] = k_flg_2 dtParam["vnum3"] = k_flg_3 dtParam["vnum4"] = k_flg_4 dtParam["dtnum"] = int(sTime) try: r = requests.post('https://api.parse.com/1/classes/Vitem1', headers=headers , data=json.dumps(dtParam), timeout=30) print r.status_code print r.json() except: print "failue, send_parse" raise finally: print "End ,send_parse"
def send_pubRGB(self, sPay ,sTopic): iWait=0 clsConst = com_appConst.appConstClass() publish.single(topic=sTopic, payload=sPay, hostname=clsConst.mMQTT_HostName , port=clsConst.mMQTT_Port ) time.sleep(iWait) return
def Is_validValve(self, iSen , iMoi): clsConst = com_appConst.appConstClass() ret=clsConst.mNG_CODE if(iMoi > iSen ): ret=clsConst.mOK_CODE return ret
sHEAD="res_dat=" def is_validTime(tmBef, tmNow, iMax): ret=False tmSpan = tmNow - tmBef iSpan = tmSpan.total_seconds() print "iSpan="+ str(iSpan) if iSpan > iMax: ret=True return ret if __name__ == "__main__": ser=serial.Serial(mDevice ,9600) clsConst = com_appConst.appConstClass() clsParam = com_getparam.getparamClass() clsCom = com_func.funcClass() clsParse =com_putParse.putParseClass() clsLog = com_logging2.loggingClass() # clsPush =com_parsePush.parsePushClass() from datetime import datetime tmBef = datetime.now() tmBefPush = datetime.now() while True: val=ser.readline() bFrom = clsParam.Is_fromMC(val) if bFrom==True: dic= clsParam.getDict(val)
def Is_validValve(self, iSen, iMoi): clsConst = com_appConst.appConstClass() ret = clsConst.mNG_CODE if (iMoi > iSen): ret = clsConst.mOK_CODE return ret