gpt4 book ai didi

用于在 azure 上创建表的 python 脚本

转载 作者:行者123 更新时间:2023-12-02 07:29:33 25 4
gpt4 key购买 nike

因此,我尝试运行一个 python 脚本,该脚本通过 Rpi 从 Z 波传感器读取数据,并在我设置的 pi 上使用 crontab,以便 python 脚本每分钟运行一次并将其存储在文本文件中。然后我有另一个 python 脚本,它将数据以表格形式发送到 azure 云存储。问题是,它确实创建了一个表,但出现错误“此 XML 文件似乎没有与之关联的任何样式信息”。我一直在试图弄清楚,但我看不出有什么问题。

这是向云端发送数据的python脚本

from azure.storage import TableService, Entity
from datetime import datetime
import socket

ac_name = 'account name'
#you will have to use your own azure account
primary_key = 'use your key'
table_name = 'Readings'

current_temperature = 0

def get_connection_string_and_create_table():
global table_service
table_service = TableService(account_name = ac_name,account_key=primary_key)
#table_service.delete_table(table_name = table_name) #TO BE USED IF THE TABLE NEEDS TO BE DELETED
table_service.create_table(table=table_name)

def insert_data():
reading = create_entity()
try:
if check_internet_available():
table_service.insert_entity(table_name = table_name,entity = reading)
return True
except Exception, e:
return False


def create_entity():

"""
- Creates the data block that would be sent to the cloud, named as Entity
"""

time_now = datetime.strftime(datetime.now(),'%d-%m-%Y %H:%M:%S')
print('Date time is {0}'.format(time_now))
reading = Entity()
reading.PartitionKey = 'Room1'
reading.RowKey = time_now
reading.Timestamp = datetime.now()
reading.Temperature = str(current_temperature)
return reading

def initialize_azure():
get_connection_string_and_create_table()

def send_data_to_cloud(temperature):
global current_temperature
current_temperature = temperature
sent_success = insert_data()
print temperature
print sent_success
return sent_success


def check_internet_available():

"""
- Checks internet availability, the data will be sent to cloud
only if there is an active internet connection

remote_server = 'www.google.com'
try:
host = socket.gethostbyname(remote_server)
s = socket.create_connection((host,80),2)
return True
except:
pass
return False


if __name__ == '__main__':

get_connection_string_and_create_table()
f = open('Meter.txt', 'r')
send_data_to_cloud(f.readline())

这是通过 API 的 HTTP 请求读取数据的脚本

import urllib2
import json
import requests
import time
import json

class EnergyConsumptionControl:

# Class constructor
def __init__(self):
# Global Variables
self.energyMeterDeviceUrl = "http://IpaddressofyourRpi"
self.session = requests.Session()
# Logging in
self.Login()


def ActivateDevice(self, deviceID):
energyMeterDeviceSwitchOn = ":8083/ZWaveAPI/Run/devices[" + str(deviceID) + "].SwitchBinary.Set(255)"
response = self.SendGetCommand(energyMeterDeviceSwitchOn) # Command to turn device on
return [response]


def DeactivateDevice(self, deviceID):
energyMeterDeviceSwitchOff = ":8083/ZWaveAPI/Run/devices[" + str(deviceID) + "].SwitchBinary.Set(0)"
response = self.SendGetCommand(energyMeterDeviceSwitchOff) # Command to turn device off
return [response]


def GetDeviceState(self, deviceID):
# Call the Get() function to update the SwitchBinary data
energyMeterDeviceSwitchGet = ":8083/ZWaveAPI/Run/devices[" + str(deviceID) + "].SwitchBinary.Get()"
response = self.SendGetCommand(energyMeterDeviceSwitchGet)
# Call to return the SwitchBinary JSON object
energyMeterDeviceSwitchGet = ":8083/ZWaveAPI/Run/devices[" + str(deviceID) + "].SwitchBinary"
response = self.SendGetDeviceStateCommand(energyMeterDeviceSwitchGet)
return response


# def GetDeviceMeter(self, deviceID):
# # Refreshing server-side information on the Meter through device interrogation
# energyMeterDeviceMeterGetVals = ":8083/ZWaveAPI/Run/devices[" + str(deviceID) + "].Meter.Get()"
# response = self.SendGetCommand(energyMeterDeviceMeterGetVals)
# # Retrieving the JSON of all Meter related data
# energyMeterDeviceMeterGet = ":8083/ZWaveAPI/Run/devices[" + str(deviceID) + "].Meter"
# response = self.GetMeterWattage(energyMeterDeviceMeterGet)
# return [response]

def GetDeviceEnergyConsumption(self, deviceID):
# Refreshing server-side information on the Meter through device interrogation
energyMeterDeviceMeterRefreshVals = ":8083/ZWaveAPI/Run/devices[" + str(deviceID) + "].Meter"
response = self.SendGetCommand(energyMeterDeviceMeterRefreshVals)

# Retrieving the JSON of all Meter related data
energyMeterDeviceMeterGetVals = ":8083/ZWaveAPI/Run/devices[" + str(deviceID) + "].Meter"
energyConsumption = self.GetMeterConsumption(energyMeterDeviceMeterGetVals)

response = self.ResetDeviceMeter(deviceID)
return energyConsumption
def GetDeviceEnergyWattage(self, deviceID):
# Refreshing server-side information on the Meter through device interrogation
energyMeterDeviceMeterRefreshVals = ":8083/ZWaveAPI/Run/devices[" + str(deviceID) + "].Meter.Get()"
response = self.SendGetCommand(energyMeterDeviceMeterRefreshVals)

# Retrieving the JSON of all Meter related data
energyMeterDeviceMeterGetVals = ":8083/ZWaveAPI/Run/devices[" + str(deviceID) + "].Meter"
energyWattage = self.GetMeterWattage(energyMeterDeviceMeterGetVals)
print energyWattage
return energyWattage


def ResetDeviceMeter(self, deviceID):
energyMeterDeviceMeterReset = ":8083/ZWaveAPI/Run/devices[" + str(deviceID) + "].Meter.Reset(255)"
response = self.SendPostCommand(energyMeterDeviceMeterReset) # Command to reset device meter value
return [response]


def Login(self):
data = {
"form": True,
"login": "admin",
"password": "#password",
"keepme": False,
"default_ui": 1
}

# Authenticating
headers = {'Content-Type': 'application/json'}
response = self.session.post(self.energyMeterDeviceUrl + ':8083/ZAutomation/api/v1/login', headers=headers,
data=json.dumps(data))

# Generic function to send GET commands to individual Z-Wave devices
def SendGetCommand(self, command):
status = self.session.get(self.energyMeterDeviceUrl + command)
print status
print status.text
return [status]

# Generic function to send POST commands to individual Z-Wave devices
def SendPostCommand(self, command):
status = self.session.post(self.energyMeterDeviceUrl + command)
print status
print status.text
return [status]

def GetMeterWattage(self, command):
status = self.session.get(self.energyMeterDeviceUrl + command)
json_data = json.loads(status.text)
# Parsing the JSON data to get just the energy wattage (W)
return json_data['data']['2']['val']['value']


def GetMeterConsumption(self, command):
status = self.session.get(self.energyMeterDeviceUrl + command)
json_data = json.loads(status.text)
# Parsing the JSON data to get just the energy consumption (kWh)
return json_data['data']['0']['val']['value']
def SendGetDeviceStateCommand(self, command):
status = self.session.get(self.energyMeterDeviceUrl + command)
json_data = json.loads(status.text)
# Parsing the JSON data to get just the 'level'
return int(json_data['data']['level']['value'])


control = EnergyConsumptionControl()
response = control.GetDeviceEnergyConsumption(8)
print response


control = EnergyConsumptionControl()
response = control.ActivateDevice(8)
print response

有没有办法将两者结合起来,以便它调用数据并将其发送到云端?如何修复错误消息

最佳答案

当没有用于呈现的演示文稿/样式信息时,会发生此错误,在这种情况下,它是用于通信的 Xml,因此可能不需要。这是另一个相关的线程 - 这可能会有所帮助。

This XML file does not appear to have any style information associated with it

关于用于在 azure 上创建表的 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39760427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com