/*
 * $RCSfile: scenes.h,v $  $Revision: 1.19 $  $Name:  $
 * $Id: scenes.h,v 1.19 2006/10/22 15:22:42 bpaauwe Exp $
 * $Author: bpaauwe $
 * $Date: 2006/10/22 15:22:42 $
 * ----------------------------------------------------------------------------
 *
 *  Copyright (c) Bob Paauwe (2006)
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT
 *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 *  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 *  more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * ----------------------------------------------------------------------------
 *
 *  Description:
 *    Header file for link data base stuff.
 * ----------------------------------------------------------------------------
 */
#ifndef _SCENES_H
#define _SCENES_H

#include <stdio.h>
#include "libilib/ilib.h"

#define MAX_LINKS 417

typedef struct _link_record {
	unsigned char flags;
	unsigned char group_number;
	unsigned char device_id[3];
	unsigned char data1;
	unsigned char data2;
	unsigned char data3;
} link_record_t;

typedef struct device_t {
	char *device_name;
	unsigned char changed;
	short device_type;
	int device_index;
	unsigned char device_id[3];
	int num_of_entries;
	link_record_t db_records[MAX_LINKS];
	link_record_t current_lt[MAX_LINKS];
	unsigned char local_on_level;
	unsigned char local_ramp_rate;
} device_t;

typedef struct _device_list {
	device_t *device;
	link_record_t link;
	struct _device_list *next;
} device_list_t;

typedef struct _scene {
	char *name;
	int group;
	device_t *controller;
	int responders;
	device_list_t *responder_list;
	int index;
	struct _scene *next;
} scene_t;

int program_device(ilib_t *iplc, device_t *device);
int get_device_link(ilib_t *iplc, unsigned char *dev_id, int link_num,
		link_record_t *entry);
int set_device_link(ilib_t *iplc, unsigned char *dev_id, int link_num,
		link_record_t *entry);
int clear_device_link(ilib_t *iplc, unsigned char *dev_id, int link_num);
void verify_device_links(ilib_t *iplc, device_t *device);
void update_device_links(ilib_t *iplc, device_t *device);
void get_device_list();
void get_scene_info(char *fname);
void build_tables();
void clear_tables();
void free_tables();
void save_scene_info(char *fname);
device_t *find_device(unsigned char *device_id);
scene_t *find_scene(int scene_index);
int scene_exist(device_t *control, unsigned char group);
scene_t *get_scene_by_name(char *scene_name);
scene_t *get_scene_by_group_master(int group, unsigned char *master_id);
int get_scene_by_group_plc(ilib_t *plc, int group);
int get_plc_scene_by_index(ilib_t *plc, int index);
void delete_device(int device_index);
void delete_device_from_scenes(int dev_index);
void delete_scene(int scene_index);
void free_scene_list();
void free_device_list();
void discover_scenes(ilib_t *iplc);
void find_responders(scene_t *scene, unsigned char *id);
void rename_scene(int scene_index, char *new_name);
unsigned char *str_id_to_hex(char *device_id);
unsigned char atoh(char h);
int ahtoi(char *h);
int getline(FILE *fp, char *s, int lim);
#endif
