<commit_before>import datetime
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser

from .models import House, Nodes, NodeState, CurrentState, IRcommend
from .serializers import HouseSerializer, NodesSerializer, NodeslistSerializer
from SmartHome.node.tasks import node_N_all_open, node_N_all_close

import time


class JSONResponse(HttpResponse):
	# """
	# An HttpResponse that renders its content into JSON.
	# """
	def __init__(self, data, **kwargs):
		content = JSONRenderer().render(data)
		kwargs['content_type'] = 'application/json'
		super(JSONResponse, self).__init__(content, **kwargs)

@csrf_exempt
def House_list(request):
	if request.method == 'GET':
		HouseSetting = House.objects.all()
		serializer = HouseSerializer(HouseSetting, many=True)
		return JSONResponse(serializer.data)

	elif request.method == 'POST':
		data = JSONParser().parse(request)
		ID = data['GroupID'] # 檢查ID是否有重複
		# Create_or_update 
		try:
			house = House.objects.get(GroupID = ID)
			serializer = HouseSerializer(house, data=data) # update
		except House.DoesNotExist:
			serializer = HouseSerializer(data=data) # creat
		if serializer.is_valid():
			serializer.save()
			return HttpResponse(status=204)
		return JSONResponse(serializer.errors, status=400)
	return HttpResponse(status=404)

		
@csrf_exempt
def House_detail(request, GroupID):
	try:
		house = House.objects.get(GroupID = GroupID)
	except House.DoesNotExist:
		return HttpResponse(status=404)

	if request.method == 'GET':
		serializer = HouseSerializer(house)
		return JSONResponse(serializer.data)

	elif request.method == 'PUT':
		data = JSONParser().parse(request)
		serializer = HouseSerializer(house, data=data)
		if serializer.is_valid():
			serializer.save()
			return JSONResponse(serializer.data)
		return JSONResponse(serializer.errors, status=400)

	elif request.method == 'DELETE':
		house.delete()
		return HttpResponse(status=204)
	return HttpResponse(status=404)



# Node_list
# Get:表列所有Node, 耗電量, 所屬Group, State 
@csrf_exempt
def Node_list(request):
	if request.method == 'GET':
		Nodelist = Nodes.objects.all()
		serializer = NodeslistSerializer(Nodelist, many=True)
		return JSONResponse(serializer.data)
	return HttpResponse(status=404)

# Node_detail
# Get:表列特定Node的詳細資料(Address, ID, ...) 歷史耗電量(1Week), 歷史State(1week) 
# PUT:改變NodeState，命令開關或是轉台調溫度
@csrf_exempt
def Node_detail(request, NodeID): 
	try:
		node_obj = Nodes.objects.get(ID = NodeID)
	except Nodes.DoesNotExist:
		return HttpResponse(status=404)

	if request.method == 'GET':
		serializer = NodesSerializer(node_obj)
		return JSONResponse(serializer.data)

	elif request.method == 'PUT':
		# 這裡要插入控制Node的Code
		# 記得發送完控制訊號要寫入資料庫NodeState
		
		node_N_all_open.apply_async()
		time.sleep(5)

		node_N_all_close.apply_async()

		data = JSONParser().parse(request)

		data['NodeID'] = NodeID
		commd = data['State']
		# 檢查commd是node可接受命令
		vailded = False
		nodeType = node_obj.Type
		if nodeType == 'N': 
			if commd in [0, 1]:
				vailded = True
		elif nodeType == 'L':
			if commd in range(8):
				vailded = True
		elif nodeType == 'IR':
			vailded = True
		else:
			return HttpResponse(status=500)
		#-----------------------------#		
		## 檢查通過則下命令給node!!!
		#-----------------------------#
		if vailded: 
			NodeState.objects.create(NodeID = node_obj, State = commd, Added = datetime.datetime.now())
			msg = 'Node '+str(NodeID)+' state seting to '+ str(data['State'])
			print(msg)
			return JSONResponse(data)
		return JSONResponse(data, status=400)

	return HttpResponse(status=404)
		


<commit_msg>第一次測試開關過<commit_after>import datetime
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser

from .models import House, Nodes, NodeState, CurrentState, IRcommend
from .serializers import HouseSerializer, NodesSerializer, NodeslistSerializer
from SmartHome.node.tasks import node_N_all_open, node_N_all_close

import time


class JSONResponse(HttpResponse):
	# """
	# An HttpResponse that renders its content into JSON.
	# """
	def __init__(self, data, **kwargs):
		content = JSONRenderer().render(data)
		kwargs['content_type'] = 'application/json'
		super(JSONResponse, self).__init__(content, **kwargs)

@csrf_exempt
def House_list(request):
	if request.method == 'GET':
		HouseSetting = House.objects.all()
		serializer = HouseSerializer(HouseSetting, many=True)
		return JSONResponse(serializer.data)

	elif request.method == 'POST':
		data = JSONParser().parse(request)
		ID = data['GroupID'] # 檢查ID是否有重複
		# Create_or_update 
		try:
			house = House.objects.get(GroupID = ID)
			serializer = HouseSerializer(house, data=data) # update
		except House.DoesNotExist:
			serializer = HouseSerializer(data=data) # creat
		if serializer.is_valid():
			serializer.save()
			return HttpResponse(status=204)
		return JSONResponse(serializer.errors, status=400)
	return HttpResponse(status=404)

		
@csrf_exempt
def House_detail(request, GroupID):
	try:
		house = House.objects.get(GroupID = GroupID)
	except House.DoesNotExist:
		return HttpResponse(status=404)

	if request.method == 'GET':
		serializer = HouseSerializer(house)
		return JSONResponse(serializer.data)

	elif request.method == 'PUT':
		data = JSONParser().parse(request)
		serializer = HouseSerializer(house, data=data)
		if serializer.is_valid():
			serializer.save()
			return JSONResponse(serializer.data)
		return JSONResponse(serializer.errors, status=400)

	elif request.method == 'DELETE':
		house.delete()
		return HttpResponse(status=204)
	return HttpResponse(status=404)



# Node_list
# Get:表列所有Node, 耗電量, 所屬Group, State 
@csrf_exempt
def Node_list(request):
	if request.method == 'GET':
		Nodelist = Nodes.objects.all()
		serializer = NodeslistSerializer(Nodelist, many=True)
		return JSONResponse(serializer.data)
	return HttpResponse(status=404)

# Node_detail
# Get:表列特定Node的詳細資料(Address, ID, ...) 歷史耗電量(1Week), 歷史State(1week) 
# PUT:改變NodeState，命令開關或是轉台調溫度
@csrf_exempt
def Node_detail(request, NodeID): 
	try:
		node_obj = Nodes.objects.get(ID = NodeID)
	except Nodes.DoesNotExist:
		return HttpResponse(status=404)

	if request.method == 'GET':
		serializer = NodesSerializer(node_obj)
		return JSONResponse(serializer.data)

	elif request.method == 'PUT':
		# 這裡要插入控制Node的Code
		# 記得發送完控制訊號要寫入資料庫NodeState
		
		data = JSONParser().parse(request)

		data['NodeID'] = NodeID
		commd = data['State']
		# 檢查commd是node可接受命令
		vailded = False
		nodeType = node_obj.Type
		if nodeType == 'N': 
			if commd in [0, 1]:
				vailded = True
		elif nodeType == 'L':
			if commd in range(8):
				vailded = True
		elif nodeType == 'IR':
			vailded = True
		else:
			return HttpResponse(status=500)
		#-----------------------------#		
		## 檢查通過則下命令給node!!!
		#-----------------------------#
		if vailded: 
			NodeState.objects.create(NodeID = node_obj, State = commd, Added = datetime.datetime.now())
			msg = 'Node '+str(NodeID)+' state seting to '+ str(data['State'])
			if data['State'] ==1 :
				node_N_all_open.apply_async()
			elif data['State'] ==0:
				node_N_all_close.apply_async()
			
			print(msg)

			return JSONResponse(data)
		return JSONResponse(data, status=400)

	return HttpResponse(status=404)
		



{'fraction_non_alphanumeric': 0.12910639448425038, 'fraction_numerical': 0.011491145058807625, 'mean_word_length': 4.5415730337078655, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4701203', 'n_tokens_mistral': 2864, 'n_tokens_neox': 2627, 'n_words': 531}