Prolog "moon"

Admin User, erstellt 07. März 2024
         
/**
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
/**
* java -cp C:\Projects\Dogelog\Prototyping\experiment\other\01_dummy\lib\playerj\canned\dogelog.zip;
* C:\Projects\Dogelog\Prototyping\experiment\other\01_dummy\lib\playerj\canned
* Index moon.p
* http://localhost:8084/
* Error 404: Not found
* http://localhost:8084/style.css
* The corresponding style sheet
* http://localhost:8084/moon.cgi?day=YYYY-MM-DD or http://localhost:8084/moon.cgi
* The moon phase HTML page.
*/
:- ensure_loaded(library(misc/markup)).
:- ensure_loaded(library(misc/vector)).
:- ensure_loaded(library(util/spin)).
:- ensure_loaded(library(util/files)).
main :-
http_server_new(S),
http_server_on(S,'request', [P,Q], dispatch(P,Q)),
http_server_listen(S, 8084).
/********************************************************************/
/* HTTP Displatch */
/********************************************************************/
dispatch(Req, Res) :-
http_current_path(Req, Url),
url_search_params(Url, Path, List),
dispatch(Path, List, Req, Res), !.
dispatch(_, Res) :-
http_write_head(Res, 404, ['content-type'-'text/plain; charset=utf-8']),
http_text_new(Res, S),
write(S, 'Error 404: Not found'),
close(S).
dispatch('/style.css', _, _, Res) :-
http_write_head(Res, 200, ['content-type'-'text/css; charset=utf-8']),
http_text_new(Res, S),
open('style.css', read, T),
(enum_lines(T, Line), put_atom(S, Line), fail; true),
close(T),
close(S).
/********************************************************************/
/* HTML Page */
/********************************************************************/
dispatch('/moon.cgi', List, _, Res) :-
http_write_head(Res, 200, ['content-type'-'text/html; charset=utf-8']),
http_text_new(Res, S),
dom_output_new(S, T),
(member(day - Day2, List) -> sys_time_atom('%Y-%m-%d', Time, Day2); statistics(wall, Time)),
phase(Time, Phase),
tag(T, '<html>'),
tag(T, '<head>'),
tag(T, '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>'),
tag(T, '<title editable="nocomment">'), write(T, 'Mondphase'), tag(T, '</title>'),
tag(T, '<link href="style.css" rel="stylesheet" type="text/css"/>'),
tag(T, '</head>'),
tag(T, '<body class="showbody">'),
tag(T, '<center>'),
K is truncate(Phase+0.5), descr(K, Descr),
tag(T, '<h3>'), write(T, Descr), tag(T, '</h3>'),
moon(T, Phase),
tag(T, '<br/>'),
TimePrev is Time-86400000, TimeNext is Time+86400000,
sys_time_atom('%Y-%m-%d', TimePrev, DayPrev),
sys_time_atom('%Y-%m-%d', Time, Day),
sys_time_atom('%Y-%m-%d', TimeNext, DayNext),
tag_format(T, '<a href="moon.cgi?day=~c">', [DayPrev]), write(T, '◀'), tag(T, '</a>'),
write(T, ' '), write(T, Day), write(T, ' '),
tag_format(T, '<a href="moon.cgi?day=~c">', [DayNext]), write(T, '▶'), tag(T, '</a>'),
tag(T, '</center>'),
tag(T, '</body>'),
tag(T, '</html>'),
close(T).
/********************************************************************/
/* SVG Element */
/********************************************************************/
% moon(+Stream, +Float)
moon(S, N) :-
X is min(60,(N-2)*30), abs_flag(X, RX, SG),
X2 is min(60,(6-N)*30), abs_flag(X2, RX2, SG2),
svg_begin(S, [width(40),height(40)]),
svg_rect(S, 0, 0, 200, 160, 'empy'),
svg_circle(S, 100, 80, 60, 'dark'),
svg_path(S, ['M',100,20,'A',RX,60,0,0,SG,100,140,
'A',RX2,60,0,0,SG2,100,20,'Z'], 'light'),
svg_end(S).
% abs_flag(+Float, -Float, -Integer)
abs_flag(X, RX, 1) :- X < 0, !, RX is -X.
abs_flag(X, X, 0).
% descr(+Integer, -Atom)
descr(1, R) :- !, R = 'Zunehmender Sichelmond'.
descr(2, R) :- !, R = 'Zunehmend Halbmond'.
descr(3, R) :- !, R = 'Zunehmend Dreiviertelmond'.
descr(4, R) :- !, R = 'Vollmond'.
descr(5, R) :- !, R = 'Abnehmend Dreiviertelmond'.
descr(6, R) :- !, R = 'Abnehmend Halbmond'.
descr(7, R) :- !, R = 'Abnehmend Sichelmond'.
descr(_, 'Neumond').
% phase(+Integer, -Float)
phase(T, N) :-
D is T/86400000,
P is (D - 6.5)/29.5305882,
N is (P-truncate(P))*8.
/********************************************************************/
/* Initialization Goal */
/********************************************************************/
% :- initialization(main).