# -*- coding: utf-8 -*-
"""
    demeter web
    name:admin.py
    author:rabin
"""
import time
from demeter.core import *
timeSleep = 1000

class Loop(object):

	def run(self):
		self.handle()
		time.sleep(timeSleep)

	# 开关设备
	def switch(self, value):
		Demeter.service('device').switchMul(value)

	def handle(self):
		model = Demeter.model('device_set_loop')
		cur = Demeter.time()
		date = Demeter.date(cur, '%Y-%m-%d-%w-%H-%M')
		date = date.split('-')
		week = date[3]
		day = date[2]
		model.status = True
		model.hour = date[4]
		model.minute = date[5]
		data = model.select()
		if data:
			for value in data:
				state = False
				value['loop'] = value['loop'].split(',')
				if value['looptype'] == 2:
					if week in value['loop']:
						state = True
				elif value['looptype'] == 3:
					if day in value['loop']:
						state = True
				if state:
					self.switch(value)